// // Copyright 2013 Google Inc. All Rights Reserved. // #define LOG_NDEBUG 0 #define LOG_TAG "PluginTest" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "DrmEngine.h" #include "HTTPUtil.h" #include "PsshInitData.h" #ifdef __cplusplus extern "C" { #endif #define TEST_INFO(fmt, args...) printf("%s(), %d: " fmt, __FUNCTION__,__LINE__, ## args) #define TEST_FAIL (1) #define TEST_SUCCESS (0) #define MAX_INITDATA_LEN (1000) #define MAX_ERRORDETAILMSG_LEN (128) #define MAX_LICENSERSP_LEN (10000) //set multi handle para #define DRM_NUM (2) #define SESSION_NUM (2) #define PLAYREADY_DEFAULT_LICENSE_URL "http://playready.directtaps.net/pr/svc/rightsmanager.asmx" #define ASSERT_TRUE(A) \ if (false == (A)) \ { \ HI_LOGE("tested line not pass!\n"); \ } #define ASSERT_EQ(A, B) \ if ((A) != (B)) \ { \ HI_LOGE("tested line not pass!\n"); \ } #define EXPECT_TRUE(A) \ if (false == (A)) \ { \ HI_LOGE("tested line not pass!\n"); \ return TEST_FAIL; \ } #define EXPECT_FALSE(A) \ if (true == (A)) \ { \ HI_LOGE("tested line not pass!\n"); \ return TEST_FAIL; \ } #define EXPECT_EQ(A, B) \ if ((A) != (B)) \ { \ HI_LOGE("tested line not pass!\n"); \ return TEST_FAIL; \ } #define EXPECT_NE(A, B) \ if ((A) == (B)) \ { \ HI_LOGE("tested line not pass!\n"); \ return TEST_FAIL; \ } #define EXPECT_GT(A, B) \ if ((A) <= (B)) \ { \ HI_LOGE("tested line not pass!\n"); \ return TEST_FAIL; \ } #define CHECK_NULLPTR(ptr) { \ if (HI_NULL == (ptr)) \ { \ HI_LOGE("%s %d nullptr error!\n", __FUNCTION__, __LINE__); \ return HI_FAILURE; \ } \ } namespace { using android::String8; using std::string; static HI_U32 drmclose_FLAG = 0; static HI_U32 decryptclose_FLAG = 0; static HI_S32 VectorToArray(const Vector &vectorData, HI_U8* arrayData, HI_U32* arrayLen) { CHECK_NULLPTR(arrayData); CHECK_NULLPTR(arrayLen); #if 1 printf("------input vectorData------\n"); for(HI_U32 i=0;i &vectorData) { CHECK_NULLPTR(arrayData); vectorData.clear(); vectorData.appendArray(arrayData, arrayLen); return HI_SUCCESS; } static HI_S32 String8ToArray(const String8 &string8Data, HI_U8* arrayData, HI_U32* arrayDataLen ) { const HI_U8 *charData = reinterpret_cast(string8Data.string()); *arrayDataLen = string8Data.length(); memcpy(arrayData, charData, *arrayDataLen + 1); arrayData[*arrayDataLen] = '\0'; return HI_SUCCESS; } static HI_S32 ArrayToString8(const HI_U8 &arrayData, String8 *string8Data) { /*will be implemented if necessary*/ return HI_SUCCESS; } static void printVector(const Vector &vectorData) { HI_U32 len= vectorData.size(); printf("vector="); for(HI_U32 i=0;isetTo(&xmlData.string()[start], end - start); return true; } Vector getInitData(const String8& drmHeaderXml) { String16 drmHeader(drmHeaderXml); Vector initData; initData.appendArray(reinterpret_cast(drmHeader.string()), drmHeader.size() * sizeof(drmHeader[0])); return initData; } inline String8 getString8FromUtf16String(const android::Vector& utf16Array) { return String8(reinterpret_cast(utf16Array.array()), utf16Array.size() / sizeof(char16_t)); } HI_U8 verifyLicenseRequest(HI_DRMENGINE_KEY_REQUEST* keyRequest, const Vector& drmHeader) { HI_DRM_KEYREQUEST_TYPE eKeyRequestType = (HI_DRM_KEYREQUEST_TYPE)(keyRequest->eKeyRequestType); HI_LOGD("eKeyRequestType:%d", eKeyRequestType); EXPECT_EQ(eKeyRequestType, HI_DRM_KEYRST_TYPE_INITIAL); String8 defaultUrl(keyRequest->defaultUrl,strlen(keyRequest->defaultUrl)); HI_LOGD("defaultUrl:%s", defaultUrl.string()); String8 expectUrl(PLAYREADY_DEFAULT_LICENSE_URL); EXPECT_EQ(defaultUrl, expectUrl); const String8 requestXml(reinterpret_cast(keyRequest->requestData), keyRequest->requestDataLen); String8 challenge; EXPECT_TRUE(extractElement(requestXml, String8("challenge"), &challenge)); String8 contentHeader; EXPECT_TRUE(extractElement(challenge, String8("ContentHeader"), &contentHeader)); const String8 drmHeaderXml(getString8FromUtf16String(drmHeader)); EXPECT_TRUE(drmHeaderXml.find(contentHeader.string()) >= 0); return TEST_SUCCESS; } // Got license header xml from Microsoft Test server // $ curl -O http://playready.directtaps.net/smoothstreaming/SSWSS720H264PR/SuperSpeedway_720.ism/Manifest // $ xmlstarlet sel -t -m '//ProtectionHeader[1]' -v . -n < Manifest | base64 -d const String8 kTestDrmHeaderXml( "" "16AESCTR" "AmfjCTOPbEOl3WD/5mcecA==BGw1aYZ1YXM=" "7.1.1064.0" "http://playready.directtaps.net/pr/svc/rightsmanager.asmx" "AH+03juKbUGbHl1V/QIwRA==" ""); const String8 kInvalidTestDrmHeaderXml( "" "16AESCTR" // remove KID to corrupt DrmHeader //"AmfjCTOPbEOl3WD/5mcecA==BGw1aYZ1YXM=" "7.1.1064.0" "http://playready.directtaps.net/pr/svc/rightsmanager.asmx" "AH+03juKbUGbHl1V/QIwRA==" ""); const String8 kPluginPath("/vendor/lib/mediadrm/libplayreadydrmplugin.so"); const HI_U8 kRandomUuid[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }; const HI_U8 kPlayReadyUuid[16] = { 0x9a, 0x04, 0xF0, 0x79, 0x98, 0x40, 0x42, 0x86, 0xAB, 0x92, 0xE6, 0x5B, 0xE0, 0x88, 0x5F, 0x95 }; const HI_U8 kTestKeyId[16] = { 0x09, 0xe3, 0x67, 0x02, 0x8f, 0x33, 0x43, 0x6c, 0xa5, 0xdd, 0x60, 0xff, 0xe6, 0x67, 0x1e, 0x70 }; const HI_U8 kTestIv[16] = { 0xf4, 0xb3, 0x10, 0x77, 0x75, 0x3e, 0x8b, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; const HI_U8 kTestData[10] = { 0x06, 0xf5, 0xd0, 0xad, 0x80, 0x3d, 0x62, 0xc7, 0xbd, 0xf5 }; const HI_U8 kTestResult[10] = { 0x21, 0x0a, 0x90, 0x28, 0x7d, 0x60, 0x14, 0x3e, 0xb1, 0xc0 }; const HI_U8 kInvalidKeyId[16] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; } // namespace anonymous namespace android { class ModularInterfaceTest { public: ModularInterfaceTest(); HI_U8 isCryptoSchemeSupported(); HI_U8 OpenSession(); HI_U8 GenerateKeyRequest(); HI_U8 GenerateKeyRequestWithInvalidHeader(); HI_U8 GenerateKeyRequestWithCustomData(); HI_U8 Decrypt(); HI_U8 drmEngineDecryptPrepare(HI_VOID* v_pDrmHandle, HI_DRMENGINE_SESSION session) { HI_LOGD("ENTER\n"); HI_LOGD("pDrmHandle = %p, sessionId = %s, Len = %d\n",v_pDrmHandle, session.sessionId,session.sessionIdLen); HI_U8 initData[MAX_INITDATA_LEN]; HI_U32 initDataLen = 0; //HI_U8 licenseResponse[MAX_LICENSERSP_LEN]; HI_U8* licenseResponse; HI_U32 licenseResponseLen = 0; HI_U8 keySetId[MAX_KEYSETID_LEN]; HI_U32 keySetIdLen = 0; Vector vInitData = getInitData(kTestDrmHeaderXml); VectorToArray(vInitData, initData, &initDataLen); HI_DRMENGINE_KEY_REQUEST keyRequest; HI_CHAR const* mimeType ="video/mp4"; EXPECT_EQ(OK, HI_DrmEngine_getKeyRequest(v_pDrmHandle, session, initData, initDataLen, mimeType, (HI_DRMENGINE_KEY_TYPE)(DrmPlugin::kKeyType_Streaming), &keyRequest)); verifyLicenseRequest(&keyRequest, vInitData); Vector vLicenseResponse; const String8 kSoapAction("http://schemas.microsoft.com/DRM/2007/03/protocols/AcquireLicense"); EXPECT_TRUE(DoSoapAction(String8(keyRequest.defaultUrl, strlen(keyRequest.defaultUrl)), kSoapAction, String8(reinterpret_cast(keyRequest.requestData), keyRequest.requestDataLen), &vLicenseResponse)); licenseResponse = (HI_U8*)malloc(vLicenseResponse.size()); VectorToArray(vLicenseResponse, licenseResponse, &licenseResponseLen); EXPECT_EQ(OK,HI_DrmEngine_provideKeyResponse(v_pDrmHandle, session, licenseResponse, licenseResponseLen, keySetId, &keySetIdLen)); free(licenseResponse); return TEST_SUCCESS; } HI_U8 Setup() { HI_LOGD("ENTER\n"); HI_LOGD("Set drmNum = %d, sessionNum = %d\n", drmNum, sessionNum); for(HI_U32 i=0;i vInitData = getInitData(kTestDrmHeaderXml); VectorToArray(vInitData, initData, pInitDataLen); HI_DRMENGINE_KEY_REQUEST keyRequest; HI_CHAR const* mimeType ="video/mp4"; EXPECT_EQ(OK, HI_DrmEngine_getKeyRequest(pDrmHandleSet[0], session[0], initData, initDataLen, mimeType, (HI_DRMENGINE_KEY_TYPE)(DrmPlugin::kKeyType_Streaming), &keyRequest)); EXPECT_EQ(OK, HI_DrmEngine_closeSession(pDrmHandleSet[0], session[0])); return verifyLicenseRequest(&keyRequest, vInitData); } HI_U8 ModularInterfaceTest::GenerateKeyRequestWithInvalidHeader() { HI_LOGD("ENTER\n"); HI_DRMENGINE_SESSION session[DRM_NUM*SESSION_NUM]; HI_U8 invalidInitData[MAX_INITDATA_LEN]; HI_U32 invalidInitDataLen = 0; HI_U32* pInvalidInitDataLen = &invalidInitDataLen; EXPECT_EQ(OK, HI_DrmEngine_openSession(pDrmHandleSet[0], &session[0])); Vector vInvalidInitData = getInitData(kInvalidTestDrmHeaderXml); VectorToArray(vInvalidInitData, invalidInitData, pInvalidInitDataLen); HI_DRMENGINE_KEY_REQUEST keyRequest; HI_CHAR const* mimeType ="video/mp4"; HI_S32 ret = HI_DrmEngine_getKeyRequest(pDrmHandleSet[0], session[0], invalidInitData, invalidInitDataLen, mimeType, (HI_DRMENGINE_KEY_TYPE)(DrmPlugin::kKeyType_Streaming), &keyRequest); printf("getKeyRequest ret in GenerateKeyRequestWithInvalidHeader:%d\n", ret); EXPECT_NE(OK, ret); EXPECT_EQ(OK, HI_DrmEngine_closeSession(pDrmHandleSet[0], session[0])); return TEST_SUCCESS; } HI_U8 ModularInterfaceTest::Decrypt() { HI_LOGD("ENTER\n"); HI_DRMENGINE_SUBSAMPLES subSamples; subSamples.u32numBytesOfClearData = 0; subSamples.u32numBytesOfEncryptedData = 10; HI_U8 decryptData[10]; memset(decryptData,0,sizeof(kTestResult)); HI_U8 errorDetailMsg[MAX_ERRORDETAILMSG_LEN]; HI_U32 errorDetailMsgLen = 0; HI_U32* pErrorDetailMsgLen; pErrorDetailMsgLen = &errorDetailMsgLen; HI_S32 ret = HI_FAILURE; HI_U32 count = 0; for(HI_U32 i = 0;i < drmNum * sessionNum; i++) { //00 count = 0; HI_LOGD("[session:%d]-----decrypt test %d------\n",i,count++); ret = HI_DrmEngine_decrypt(sessionDecrypt[i], HI_TRUE, kTestKeyId, kTestIv, HI_DRMENGINE_CRYPTO_MODE_AES_CTR, kTestData, 10, &subSamples, 1, decryptData, errorDetailMsg, pErrorDetailMsgLen); HI_LOGD("Return code of decrypt secure:%d", ret); EXPECT_NE(OK, ret); memset(decryptData,0,sizeof(kTestResult)); //01 HI_LOGD("[session:%d]-----decrypt test %d------\n",i,count++); ret = HI_DrmEngine_decrypt(sessionDecrypt[i], HI_FALSE, kTestKeyId, kTestIv, HI_DRMENGINE_CRYPTO_MODE_AES_CTR, kTestData, 10, &subSamples, -1, decryptData, errorDetailMsg, pErrorDetailMsgLen); HI_LOGD("Return code of decrypt secure:%d", ret); EXPECT_NE(OK, ret); memset(decryptData,0,sizeof(kTestResult)); //02 HI_LOGD("[session:%d]-----decrypt test %d------\n",i,count++); ret = HI_DrmEngine_decrypt(sessionDecrypt[i], HI_FALSE, kTestKeyId, kTestIv, HI_DRMENGINE_CRYPTO_MODE_AES_CTR, kTestData, -1, &subSamples, 1, decryptData, errorDetailMsg, pErrorDetailMsgLen); HI_LOGD("Return code of decrypt secure:%d", ret); EXPECT_NE(OK, ret); memset(decryptData,0,sizeof(kTestResult)); //03 HI_LOGD("[session:%d]-----decrypt test %d------\n",i,count++); ret = HI_DrmEngine_decrypt(sessionDecrypt[i], HI_FALSE, kTestKeyId, kTestIv, HI_DRMENGINE_CRYPTO_MODE_AES_CTR, HI_NULL, 10, &subSamples, 1, decryptData, errorDetailMsg, pErrorDetailMsgLen); HI_LOGD("Return code of decrypt secure:%d", ret); EXPECT_NE(OK, ret); memset(decryptData,0,sizeof(kTestResult)); //04 HI_LOGD("[session:%d]-----decrypt test %d------\n",i,count++); ret = HI_DrmEngine_decrypt(sessionDecrypt[i], HI_FALSE, kInvalidKeyId, kTestIv, HI_DRMENGINE_CRYPTO_MODE_AES_CTR, kTestData, 10, &subSamples, 1, decryptData, errorDetailMsg, pErrorDetailMsgLen); HI_LOGD("Return code of decrypt secure:%d", ret); EXPECT_NE(OK, ret); memset(decryptData,0,sizeof(kTestResult)); //05 HI_LOGD("[session:%d]-----decrypt test %d------\n",i,count++); ret = HI_DrmEngine_decrypt(sessionDecrypt[i], HI_FALSE, kTestKeyId, kTestIv, HI_DRMENGINE_CRYPTO_MODE_AES_CTR, kTestData, 10, &subSamples, 1, decryptData, errorDetailMsg, pErrorDetailMsgLen); HI_LOGD("Return code of decrypt non-secure:%d", ret); EXPECT_EQ(0, memcmp(decryptData, kTestResult, sizeof(kTestResult))); memset(decryptData,0,sizeof(kTestResult)); } return TEST_SUCCESS; } void testModularInterface() { TEST_INFO("Test modualr interface start!\n"); ModularInterfaceTest modualrInterfaceTest; if(!drmclose_FLAG) { if(modualrInterfaceTest.Setup()) { TEST_INFO("[1] Setup failed.\n"); } else { TEST_INFO("[1] Setup success.\n"); } if(modualrInterfaceTest.isCryptoSchemeSupported()) { TEST_INFO("[2] isCryptoSchemeSupported failed.\n"); } else { TEST_INFO("[2] isCryptoSchemeSupported success.\n"); } if(modualrInterfaceTest.OpenSession()) { TEST_INFO("[3] OpenSession failed.\n"); } else { TEST_INFO("[3] OpenSession success.\n"); } if(modualrInterfaceTest.GenerateKeyRequest()) { TEST_INFO("[4] GenerateKeyRequest failed.\n"); } else { TEST_INFO("[4] GenerateKeyRequest success.\n"); } if(modualrInterfaceTest.GenerateKeyRequestWithInvalidHeader()) { TEST_INFO("[5] GenerateKeyRequestWithInvalidHeader failed.\n"); } else { TEST_INFO("[5] GenerateKeyRequestWithInvalidHeader success.\n"); } if(modualrInterfaceTest.TearDown()) { TEST_INFO("[6] TearDown failed.\n"); } else { TEST_INFO("[6] TearDown success.\n"); } } //decrypt if(!decryptclose_FLAG) { if(modualrInterfaceTest.DecryptSetup()) { TEST_INFO("[7] DecryptSetup failed.\n"); } else { TEST_INFO("[7] DecryptSetup success.\n"); } if(modualrInterfaceTest.Decrypt()) { TEST_INFO("[8] Decrypt failed.\n"); } else { TEST_INFO("[8] Decrypt success.\n"); } if(modualrInterfaceTest.DecryptTearDown()) { TEST_INFO("[9] DecryptTearDown failed.\n"); } else { TEST_INFO("[9] DecryptTearDown success.\n"); } } } } // namespace android int main(int argc, char** argv) { testModularInterface(); return 0; } #ifdef __cplusplus } #endif