// Copyright 2013 Google Inc. All Rights Reserved. // // This source file provides a basic set of unit tests for the Content // Decryption Module (CDM). It exercises much of the API that will be // required by the host application to get the license and keys for // rendering protected content. #include "cdm_test_config.h" #include "test_host_4.h" #include "test_util.h" #include #include #include "cdm_client_property_set.h" #include "clock.h" #include "config_test_env.h" #include "content_decryption_module.h" #include "device_cert.h" #include "license_request.h" #include "log.h" #include "properties.h" #include "scoped_ptr.h" #include "string_conversions.h" #include "url_request.h" #include "wv_content_decryption_module_4.h" static const int kTestPolicyRenewalDelaySeconds = 180; static const int kDelayWaitToForRenewalMessageSeconds = 2; static const int kHttpOk = 200; static const int kSessionId1 = 1; static const int kSessionId2 = 2; static const int kSessionId3 = 3; namespace { // Default key system identifier. const char kKeySystemWidevine[] = "com.widevine.alpha"; // Default mime type for session creation. const char kMimeType[] = "video/mp4"; // Known filename for certificate manipulation. const std::string kCertFilename = "cert.bin"; // Key ID of key used to encrypt the test content. // This is used to look up the content key. const std::vector kTestKeyId = wvcdm::a2b_hex("371ea35e1a985d75d198a7f41020dc23"); // Dummy encrypted data. const std::vector kInputVector1 = wvcdm::a2b_hex( "64ab17b3e3dfab47245c7cce4543d4fc7a26dcf248f19f9b59f3c92601440b36" "17c8ed0c96c656549e461f38708cd47a434066f8df28ccc28b79252eee3f9c2d" "7f6c68ebe40141fe818fe082ca523c03d69ddaf183a93c022327fedc5582c5ab" "ca9d342b71263a67f9cb2336f12108aaaef464f17177e44e9b0c4e56e61da53c" "2150b4405cc82d994dfd9bf4087c761956d6688a9705db4cf350381085f383c4" "9666d4aed135c519c1f0b5cba06e287feea96ea367bf54e7368dcf998276c6e4" "6497e0c50e20fef74e42cb518fe7f22ef27202428688f86404e8278587017012" "c1d65537c6cbd7dde04aae338d68115a9f430afc100ab83cdadf45dca39db685"); const std::vector kIv1 = wvcdm::a2b_hex("f6f4b1e600a5b67813ed2bded913ba9f"); // Expected output for kInputVector1. const std::vector kOutputVector1 = wvcdm::a2b_hex( "217ce9bde99bd91e9733a1a00b9b557ac3a433dc92633546156817fae26b6e1c" "942ac20a89ff79f4c2f25fba99d6a44618a8c0420b27d54e3da17b77c9d43cca" "595d259a1e4a8b6d7744cd98c5d3f921adc252eb7d8af6b916044b676a574747" "8df21fdc42f166880d97a2225cd5c9ea5e7b752f4cf81bbdbe98e542ee10e1c6" "ad868a6ac55c10d564fc23b8acff407daaf4ed2743520e02cda9680d9ea88e91" "029359c4cf5906b6ab5bf60fbb3f1a1c7c59acfc7e4fb4ad8e623c04d503a3dd" "4884604c8da8a53ce33db9ff8f1c5bb6bb97f37b39906bf41596555c1bcce9ed" "08a899cd760ff0899a1170c2f224b9c52997a0785b7fe170805fd3e8b1127659"); // Dummy encrypted data. This is a combination of clear and encrypted data. const std::vector kInputVector2 = wvcdm::a2b_hex( // subsample 0 "abcdef" "53cc758763904ea5870458e6b23d36db1e6d7f7aaa2f3eeebb5393a7264991e7" "ce4f57b198326e1a208a821799b2a29c90567ab57321b06e51fc20dc9bc5fc55" "10720a8bb1f5e002c3e50ff70d2d806a9432cad237050d09581f5b0d59b00090" "b3ad69b4087f5a155b17e13c44d33fa007475d207fc4ac2ef3b571ecb9" // subsample 1 "0123456789" "f3c852" "ce00dc4806f0c6856ae1732e20308096478e1d822d75c2bb768119565d3bd6e6" "901e36164f4802355ee758fc46ef6cf5f852dd5256c7b1e5f96d29" // subsample 2 "deadbeefbaadf00d" "3b20525d5e" "78b8e5aa344d5c4e425e67ddf889ea7c4bb1d49af67eba67718b765e0a940402" "8d306f4ce693ad6dc0a931d507fa14fff4d293d4170280b3e0fca2d628f722e8" ); const std::vector kIv2 = wvcdm::a2b_hex("6ba18dd40f49da7f64c368e4db43fc88"); // Expected output for kInputVector2. const std::vector kOutputVector2 = wvcdm::a2b_hex( // subsample 0 "abcdef" "52e65334501acadf78e2b26460def3ac973771ed7c64001a2e82917342a7eab3" "047f5e85449692fae8f677be425a47bdea850df5a3ffff17043afb1f2b437ab2" "b1d5e0784c4ed8f97fc24b8f565e85ed63fb7d1365980d9aea7b8b58f488f83c" "1ce80b6096c60f3b113c988ff185b26e798da8fc6f327e4ff00e4b3fbf" // subsample 1 "0123456789" "b1ed0a" "a054bce40ccb0ebc70b181d1a12055f46ac55e29c7c2473a29d2a366d240ec48" "7cede274f012813a877f99159e7062b6a37cfc9327a7bc2195814e" // subsample 2 "deadbeefbaadf00d" "653b818d1d" "4ab9a9128361d8ca6a9d2766df5c096ee29f4f5204febdf217a94a5b560cd692" "cc36d3e071df789fdeac2fb7ec6dcd7af94bb1f85c22025b25e702e38212b927" ); // Dummy encrypted data. This will be decrypted with a data_offset // instead of subsamples. const std::vector kInputVector3 = wvcdm::a2b_hex( "64ab17b3e3dfab47245c7cce4543d4fc7a26dcf248f19f9b59f3c92601440b36" "17c8ed0c96c656549e461f38708cd47a434066f8df28ccc28b79252eee3f9c2d" "7f6c68ebe40141fe818fe082ca523c03d69ddaf183a93c022327fedc5582c5ab" "ca9d342b71263a67f9cb2336f12108aaaef464f17177e44e9b0c4e56e61da53c" "2150b4405cc82d994dfd9bf4087c761956d6688a9705db4cf350381085f383c4" "9666d4aed135c519c1f0b5cba06e287feea96ea367bf54e7368dcf998276c6e4" "6497e0c50e20fef74e42cb518fe7f22ef27202428688f86404e8278587017012" "c1d65537c6cbd7dde04aae338d68115a9f430afc100ab83cdadf45dca39db685"); const std::vector kIv3 = wvcdm::a2b_hex("f6f4b1e600a5b67813ed2bded913ba9f"); // The data_offset for kInputVector3. const uint32_t kInputOffset3 = 9; // Expected output for kInputVector3 offset by kInputOffset3. const std::vector kOutputVector3 = wvcdm::a2b_hex( "19ab304b49908e2395b32f26bf471adf4a4bc92f9e999cca8476d24a257931b4" "c5fd177693ed55e31cd2b85dc196b2b722cd8854eb9334f3dab0b5bd26aa5e66" "a9d1cfbba877c9456b11dc99a6bdc7015ca1544f7ce66171a8179eca19efe515" "8c4c1d0612dff64100387065da108fdbfcc14738202ac3d27520eb48c020ddb7" "714dca22e5e2241aff6932dba1587a97ac1a952827d411d8582dfecc2e9e1494" "644046ca7044bc41c3c5e0a3a405d5551f3f5bdd6f36042e2f0f3693778b9277" "6ed8d106647a7539df7d30288803cd9ca1c274bebe688151c72b451f571a441f" "83d0ff77d8d57dcb395122e175f4944569917627d6c3dc"); // Dummy data used as a server certificate. Not a real cert. const std::vector kFakeServerCertficiate = wvcdm::a2b_hex( "65393939a0e4a28fb07aa8237bdc9e2fd30fdf763061ed9ed91b368a2b78fc2d" "15f0b6add5d56d05f7e5852aeae67de6127d6b61b39bb5d6e9657f352ba75e72" "c436f334878568504f697ad01aa329efbadebc3b9aaf502ada9b8e6fcf066252" "76690a0f50cd3852dd39f7c5444402f86831d8bb2cbbd7cba11ab1caa35445fe" "35a332529845f2f4b5d5a0b1e2c0855fc2d644443eb967e1f9030fd0d9e6375b" "a5100a997ff8a958606d59a00151d251eaf69f9e00465b5aa4c23ef33a11b05b" "212c1ff830fcd095c681ef25a18db08d7bddfeee16763e9fec06daa8275de2b0" "554a0bec821fb7bb6fbda081d8cecce6c51195e6b6a1c0fcbb2470bcff2a962e" "57e767f83cc8b6c31d41d7c59526128f19cbf625fa4f5f382393a3bd2b76463a" "fe97e4a6f30f631c83308aa5fdccc2d1765c113d474bf2496e03c030c18e5ca8" "84cb98fa120804baa245682966926ccbf555450437de10e549afc088f8c36f63" "8a943178bdd58e4ef1f7d501e2296bbe2df57ce8816d6ae9e7ab18b1e01dac9b" "8c312298356cad58c6ed46b1cd3a895e496c66f5229da39e260a7c1f782653bd" "ade5f6132fc4771bb8caca80eb063abb47144abc44aaba8d23fcc721199291b3" "0b95bc7d310c3f90756916552151a6008feba73ddab87454822914732d6ee78a" "3587f33c698b0ab90f01b38a71abd01660a5ef1473e4556aa8c17d34679065c5" "689dd21026601e94146254346f4ccd979bc378046473b3de64b8654e18406e94" "b673dc13fdcc70213365bd098f212ed7a973ef35da18e16b1c118f8d4eda0b39" "ffb8084ca93a44923df8475e3feec6c0941a2a37d5df514e686dd182dc9ebbff" "41d8d80aa39d059de3b7849d4e5946b09937c6e7a6f6392ea5e9370ba1867553" "b716e31433c2e3ca3eff1e7c08a31b201fd18c363b8daeed59df7afb68ad5166" "659914a2e137c9d2e5940aaa921694c8d84d5ea1c83486e473e3021cb825c0ed" "f778c621598d35e44843cd53e32f90925e74bc8eb469889fb221a2c592b43d94" "2079d6393ab76e47d30dbf837fa5ca07d7186710973c5bb17c04b3207a85a166" "153053f6b0ec35c6b124efd43be274cdf8300234806a72231272d13b331cafd3" "dab01002b5e1961a5f3221d4c589fac3e42c1d1de9f244090e31a08999d3434d" "15a5159f09fa3307e0d9466ac40af63c0a62f8d2719e1df80bb24d4d7ab256f7" "0906ee342a47a9bd6805d796cf928f0d5251701ba8e6888675b1b6fd03e77df0" "8150495c778cb7942d8c060ace4b080ad22e44854988d5e2232f5dbcf2db559f" "24bae8667c33cea77f1c6a58d9dd010363c233cff6d5d26f5f77230ee681456c" "35a8f90d37c2fc0ab07a2a795431f829cca574fd37d8822e04fc500ba468f08a" "556e53ba8992dd1ccbd44559a7b93bebc27cec81a834755cf110bce183481e42"); void* GetCdmHost(int host_interface_version, void* user_data) { if (host_interface_version != cdm::Host_4::kVersion) return NULL; return user_data; } } // namespace namespace wvcdm { class CdmApi4Test : public testing::Test { public: CdmApi4Test() : cdm_(NULL) {} ~CdmApi4Test() {} protected: virtual void SetUp() { // Create the Host. host_.reset(new TestHost_4()); // Load the device cert that was already "saved" to the device. std::string cert(reinterpret_cast(kDeviceCert), kDeviceCertSize); host_->file_store[kCertFilename] = cert; // Initialize the CDM module before creating a CDM instance. InitializeCdmModule(); // Create the CDM. cdm_ = reinterpret_cast(::CreateCdmInstance( cdm::ContentDecryptionModule_4::kVersion, kKeySystemWidevine, strlen(kKeySystemWidevine), GetCdmHost, host_.get())); if (cdm_ == NULL) { FAIL() << "Fatal CDM creation error!"; } // Tell the Host about the CDM. host_->SetCdmPtr(cdm_); } void CreateSession(const uint32_t session_id, const std::string& init_data, cdm::SessionType session_type) { cdm_->CreateSession(session_id, kMimeType, strlen(kMimeType), reinterpret_cast(init_data.data()), init_data.length(), session_type); } void CreateSessionWithMimeType(const uint32_t session_id, const std::string& mime_type) { cdm_->CreateSession(session_id, mime_type.c_str(), mime_type.length(), reinterpret_cast(g_key_id.data()), g_key_id.length(), cdm::kTemporary); } void LoadSession(uint32_t session_id, const std::string& web_session_id) { cdm_->LoadSession(session_id, web_session_id.data(), web_session_id.length()); } // posts a request and extracts the drm message from the response std::string GetKeyRequestResponse( const TestHost_4::SessionMessage& session_msg, const std::string& default_url, bool is_provision) { std::string url; if (session_msg.default_url.empty()) { url = default_url; } else { // Note that the client auth string is assumed to already be appended when // the CDM tells us what URL to use. url = session_msg.default_url; } UrlRequest url_request(url); EXPECT_TRUE(url_request.is_connected()); if (!url_request.is_connected()) { return ""; } if (!is_provision) { url_request.PostRequest(session_msg.message); } else { url_request.PostCertRequestInQueryString(session_msg.message); } std::string response; int resp_bytes = url_request.GetResponse(&response); // Some license servers return 400 for invalid message, some // return 500; treat anything other than 200 as an invalid message. int status_code = url_request.GetStatusCode(response); EXPECT_EQ(kHttpOk, status_code); if (status_code != kHttpOk) { return ""; } else { std::string drm_msg; LicenseRequest lic_request; lic_request.GetDrmMessage(response, drm_msg); LOGV("drm msg: %u bytes\n%s", drm_msg.size(), HexEncode(reinterpret_cast(drm_msg.data()), drm_msg.size()).c_str()); return drm_msg; } } void ProcessKeyResponse(bool expect_url_in_message) { ProcessKeyResponse(expect_url_in_message, g_license_server + g_client_auth); } void ProcessKeyResponse(bool expect_url_in_message, const std::string& default_url) { ProcessServerResponse(expect_url_in_message, false, default_url); } void ProcessProvisionResponse() { ProcessServerResponse(true, true, ""); } void ProcessServerResponse(bool expect_url_in_message, bool is_provision, const std::string& default_url) { TestHost_4::SessionMessage session_msg = host_->GetLastSessionMessage(); ASSERT_FALSE(session_msg.message.empty()); // Use EXPECT_bool instead of EXPECT_EQ to get better error printing. if (expect_url_in_message) { EXPECT_FALSE(session_msg.default_url.empty()); } else { EXPECT_TRUE(session_msg.default_url.empty()); } std::string drm_msg = GetKeyRequestResponse(session_msg, default_url, is_provision); UpdateSession(session_msg.session_id, (const uint8_t*)drm_msg.c_str(), drm_msg.length()); } void ReleaseSession(uint32_t session_id) { cdm_->ReleaseSession(session_id); } void RemoveSession(uint32_t session_id, const std::string& web_session_id) { cdm_->RemoveSession(session_id, web_session_id.data(), web_session_id.length()); } void UpdateSession(uint32_t session_id, const uint8_t* response, uint32_t response_size) { cdm_->UpdateSession(session_id, response, response_size); } cdm::Status UsePrivacyMode() { return cdm_->UsePrivacyMode(); } cdm::Status SetServerCertificate(const std::vector& cert) { return cdm_->SetServerCertificate(&cert[0], cert.size()); } bool SessionErrorPresent(uint32_t session_id) { TestHost_4::SessionError serr = host_->GetLastSessionError(); if (serr.session_id == session_id) { return true; } return false; } bool SessionErrorPresent(uint32_t session_id, cdm::Status error_code) { TestHost_4::SessionError serr = host_->GetLastSessionError(); if (serr.session_id == session_id && serr.error_code == error_code) { return true; } return false; } cdm::InputBuffer BuildInputBuffer(const std::vector& encrypted, const std::vector& iv) { cdm::InputBuffer buf; buf.data = &encrypted[0]; buf.data_size = encrypted.size(); buf.key_id = &kTestKeyId[0]; buf.key_id_size = kTestKeyId.size(); buf.iv = &iv[0]; buf.iv_size = iv.size(); buf.data_offset = 0; buf.timestamp = 10; return buf; } cdm::InputBuffer BuildInputBuffer( const std::vector& encrypted, const std::vector& iv, const std::vector& sub) { cdm::InputBuffer buf = BuildInputBuffer(encrypted, iv); buf.subsamples = &sub[0]; buf.num_subsamples = sub.size(); return buf; } cdm::InputBuffer BuildInputBuffer(const std::vector& encrypted, const std::vector& iv, const uint32_t data_offset) { cdm::InputBuffer buf = BuildInputBuffer(encrypted, iv); buf.data_offset = data_offset; return buf; } std::vector BuildMultipleSubsamples() { std::vector sub; sub.push_back(cdm::SubsampleEntry(3, 125)); sub.push_back(cdm::SubsampleEntry(5, 62)); sub.push_back(cdm::SubsampleEntry(8, 69)); return sub; } std::vector BuildSingleSubsample(size_t size) { std::vector sub; sub.push_back(cdm::SubsampleEntry(0, size)); return sub; } void GetOfflineConfiguration(std::string* key_id, std::string* license_server, std::string* client_auth) { // This method compares three different places settings could be found: // 1) A configuration object using the default settings. // 2) The global variables prefixed with g_. // 3) A configuration object configured for offline playback. // The desire is to use 3 unless the user customized the settings on the // command line, in which case we want to defer to them and use 2. The "if" // statements check if 1 and 2 are the same. If so, we know the user // did not customize the settings on the command line, and therefore we can // use 3. Otherwise, we use 2. This will never return the values from 1; // 1 is only used to check if 2 has been altered by the user. ConfigTestEnv std_config(kLicenseServerId); // TODO (juce): Switch this to kLicenseServerId once // kContentProtectionServer is the default. ConfigTestEnv config(wvcdm::kContentProtectionServer, false); if (g_key_id.compare(a2bs_hex(std_config.key_id())) == 0) key_id->assign(wvcdm::a2bs_hex(config.key_id())); else key_id->assign(g_key_id); if (g_license_server.compare(std_config.license_server()) == 0) license_server->assign(config.license_server()); else license_server->assign(g_license_server); if (g_client_auth.compare(std_config.client_auth()) == 0) client_auth->assign(config.client_auth()); else client_auth->assign(g_client_auth); } cdm::ContentDecryptionModule_4* cdm_; // owned by host_ wvcdm::scoped_ptr host_; }; namespace { class DummyCDM : public cdm::ContentDecryptionModule_4 { public: DummyCDM() : timer_fired_(false), last_context_(NULL) {} virtual void CreateSession(uint32_t session_id, const char* mime_type, uint32_t mime_type_size, const uint8_t* init_data, uint32_t init_data_size, cdm::SessionType session_type) OVERRIDE {} virtual void LoadSession(uint32_t session_id, const char* web_session_id, uint32_t web_session_id_length) OVERRIDE {} virtual void UpdateSession(uint32_t session_id, const uint8_t* response, uint32_t response_size) OVERRIDE {} virtual bool IsKeyValid(const uint8_t*, int) OVERRIDE { return false; } virtual void ReleaseSession(uint32_t session_id) OVERRIDE {} virtual void RemoveSession(uint32_t session_id, const char* web_session_id, uint32_t web_session_id_length) {} virtual cdm::Status UsePrivacyMode() OVERRIDE { return cdm::kSuccess; } virtual cdm::Status SetServerCertificate( const uint8_t* server_certificate_data, uint32_t server_certificate_data_size) OVERRIDE { return cdm::kSuccess; } virtual void TimerExpired(void* context) OVERRIDE { timer_fired_ = true; last_context_ = context; } virtual cdm::Status Decrypt(const cdm::InputBuffer&, cdm::DecryptedBlock*) OVERRIDE { return cdm::kSessionError; } virtual cdm::Status DecryptDecodeAndRender( const cdm::InputBuffer& encrypted_buffer, cdm::StreamType stream_type) OVERRIDE { return cdm::kDecryptError; } virtual void Destroy() OVERRIDE { delete this; } bool TimerFired() const { return timer_fired_; } void* LastTimerContext() const { return last_context_; } void ResetTimerStatus() { timer_fired_ = false; last_context_ = NULL; } private: bool timer_fired_; void* last_context_; }; } // namespace TEST_F(CdmApi4Test, TestHostTimer) { // Validate that the TestHost timers are processed in the correct order. // To do this, we replace the cdm with a dummy that only tracks timers. DummyCDM* cdm = new DummyCDM(); // The old CDM is destroyed by SetCdmPtr. cdm_ = cdm; host_->SetCdmPtr(cdm); const double kTimerDelaySeconds = 1.0; const int64_t kTimerDelayMs = kTimerDelaySeconds * 1000; void* kCtx1 = reinterpret_cast(0x1); void* kCtx2 = reinterpret_cast(0x2); host_->SetTimer(kTimerDelayMs * 1, kCtx1); host_->SetTimer(kTimerDelayMs * 2, kCtx2); host_->FastForwardTime(kTimerDelaySeconds); EXPECT_TRUE(cdm->TimerFired()); EXPECT_EQ(kCtx1, cdm->LastTimerContext()); cdm->ResetTimerStatus(); host_->FastForwardTime(kTimerDelaySeconds); EXPECT_TRUE(cdm->TimerFired()); EXPECT_EQ(kCtx2, cdm->LastTimerContext()); cdm->ResetTimerStatus(); host_->FastForwardTime(kTimerDelaySeconds); EXPECT_FALSE(cdm->TimerFired()); } TEST_F(CdmApi4Test, DeviceCertificateTest) { if (Properties::use_certificates_as_identification()) { // Clear any existing certificates host_->file_store.erase(kCertFilename); CreateSession(kSessionId1, g_key_id, cdm::kTemporary); ASSERT_TRUE(SessionErrorPresent(kSessionId1, cdm::kNeedsDeviceCertificate)); // The Host must handle the certificate provisioning request. CreateSession(kSessionId2, "", cdm::kProvisioning); ASSERT_FALSE(SessionErrorPresent(kSessionId2)); ProcessProvisionResponse(); ASSERT_FALSE(SessionErrorPresent(kSessionId2)); CreateSession(kSessionId3, g_key_id, cdm::kTemporary); ASSERT_FALSE(SessionErrorPresent(kSessionId3)); } else { LOGI( "Skipping CdmApi4Test::DeviceCertificateTest because this platform " "does not support device certificates."); } } // Note that these tests, BaseMessageTest, NormalDecryption, TimeTest, and // others are dependent on getting back a license from the license server where // the url for the license server is defined in the conf_test_env.cpp. If these // tests fail immediately, verify that the license server URL is correct // and works in your test environment. TEST_F(CdmApi4Test, BaseMessageTest) { CreateSession(kSessionId1, g_key_id, cdm::kTemporary); ProcessKeyResponse(false); ASSERT_FALSE(SessionErrorPresent(kSessionId1)); ReleaseSession(kSessionId1); } TEST_F(CdmApi4Test, NormalDecryption) { CreateSession(kSessionId1, g_key_id, cdm::kTemporary); ProcessKeyResponse(false); ASSERT_FALSE(SessionErrorPresent(kSessionId1)); // Level 1 / Level 2 payload comes back in the cpu memory as cleartext. std::vector encrypted = kInputVector1; std::vector iv = kIv1; std::vector expected = kOutputVector1; std::vector sub = BuildSingleSubsample(encrypted.size()); cdm::InputBuffer buf = BuildInputBuffer(encrypted, iv, sub); TestDecryptedBlock output; cdm::Status status = cdm_->Decrypt(buf, &output); EXPECT_EQ(cdm::kSuccess, status); EXPECT_EQ( 0, memcmp(output.DecryptedBuffer()->Data(), &expected[0], buf.data_size)); ReleaseSession(kSessionId1); } TEST_F(CdmApi4Test, NormalSubSampleDecryptionWithSubsampleInfo) { CreateSession(kSessionId1, g_key_id, cdm::kTemporary); ProcessKeyResponse(false); ASSERT_FALSE(SessionErrorPresent(kSessionId1)); // Level 1 / Level 2 payload comes back in the cpu memory as cleartext. std::vector encrypted = kInputVector2; std::vector iv = kIv2; std::vector expected = kOutputVector2; std::vector sub = BuildMultipleSubsamples(); cdm::InputBuffer buf = BuildInputBuffer(encrypted, iv, sub); TestDecryptedBlock output; cdm::Status status = cdm_->Decrypt(buf, &output); EXPECT_EQ(cdm::kSuccess, status); EXPECT_EQ( 0, memcmp(output.DecryptedBuffer()->Data(), &expected[0], buf.data_size)); ReleaseSession(kSessionId1); } TEST_F(CdmApi4Test, NormalSubSampleDecryptionWithMissingSubsampleInfo) { CreateSession(kSessionId1, g_key_id, cdm::kTemporary); ProcessKeyResponse(false); ASSERT_FALSE(SessionErrorPresent(kSessionId1)); std::vector encrypted = kInputVector2; std::vector iv = kIv2; std::vector expected = kOutputVector2; std::vector sub = BuildMultipleSubsamples(); // Don't add these subsamples yet! cdm::InputBuffer buf = BuildInputBuffer(encrypted, iv); buf.num_subsamples = sub.size(); TestDecryptedBlock output; cdm::Status status = cdm_->Decrypt(buf, &output); EXPECT_EQ(cdm::kDecryptError, status); // Add the subsamples pointer and expect success. buf.subsamples = &sub[0]; status = cdm_->Decrypt(buf, &output); EXPECT_EQ(cdm::kSuccess, status); EXPECT_EQ( 0, memcmp(output.DecryptedBuffer()->Data(), &expected[0], buf.data_size)); ReleaseSession(kSessionId1); } TEST_F(CdmApi4Test, DecryptWithDataOffset) { CreateSession(kSessionId1, g_key_id, cdm::kTemporary); ProcessKeyResponse(false); ASSERT_FALSE(SessionErrorPresent(kSessionId1)); std::vector encrypted = kInputVector3; std::vector iv = kIv3; std::vector expected = kOutputVector3; cdm::InputBuffer buf = BuildInputBuffer(encrypted, iv, kInputOffset3); TestDecryptedBlock output; cdm::Status status = cdm_->Decrypt(buf, &output); cdm::Buffer* output_buf = output.DecryptedBuffer(); EXPECT_EQ(cdm::kSuccess, status); EXPECT_EQ(buf.data_size - buf.data_offset, output_buf->Size()); EXPECT_EQ(0, memcmp(output.DecryptedBuffer()->Data(), &expected[0], output_buf->Size())); ReleaseSession(kSessionId1); } TEST_F(CdmApi4Test, DecryptReturnsSizedBuffer) { CreateSession(kSessionId1, g_key_id, cdm::kTemporary); ProcessKeyResponse(false); ASSERT_FALSE(SessionErrorPresent(kSessionId1)); std::vector encrypted = kInputVector1; std::vector iv = kIv1; std::vector expected = kOutputVector1; std::vector sub = BuildSingleSubsample(encrypted.size()); cdm::InputBuffer buf = BuildInputBuffer(encrypted, iv, sub); TestDecryptedBlock output; cdm::Status status = cdm_->Decrypt(buf, &output); EXPECT_EQ(cdm::kSuccess, status); cdm::Buffer* buffer = output.DecryptedBuffer(); EXPECT_NE((void*)NULL, buffer); if (buffer) { EXPECT_EQ(expected.size(), output.DecryptedBuffer()->Size()); } ReleaseSession(kSessionId1); } // This exercises both timers and renewal. TEST_F(CdmApi4Test, RenewalTest) { CreateSession(kSessionId1, g_key_id, cdm::kTemporary); ProcessKeyResponse(false); ASSERT_FALSE(SessionErrorPresent(kSessionId1)); // We expect that by the time we've added a key, the CDM has set a timer. // Otherwise, it couldn't correctly handle renewal. EXPECT_NE(0, host_->NumTimers()); host_->FastForwardTime(kTestPolicyRenewalDelaySeconds + kDelayWaitToForRenewalMessageSeconds); // When the timer expired, we should have sent a renewal, so we can // add this renewed key now, assuming things are working as expected. ProcessKeyResponse(true); // And the key should have been added. ASSERT_FALSE(SessionErrorPresent(kSessionId1)); ReleaseSession(kSessionId1); } TEST_F(CdmApi4Test, SecureDecryptionLevel1) { CreateSession(kSessionId1, g_key_id, cdm::kTemporary); ProcessKeyResponse(false); ASSERT_FALSE(SessionErrorPresent(kSessionId1)); // Level 1 passes encrypted payload straight through. By calling the CDM's // DecryptDecodeAndRender, OEMCrypto_DecryptCTR will be told to use Direct // Rendering. std::vector encrypted = kInputVector1; std::vector iv = kIv1; std::vector sub = BuildSingleSubsample(encrypted.size()); cdm::InputBuffer buf = BuildInputBuffer(encrypted, iv, sub); cdm::Status status; status = cdm_->DecryptDecodeAndRender(buf, cdm::kStreamTypeVideo); EXPECT_EQ(cdm::kSuccess, status); status = cdm_->DecryptDecodeAndRender(buf, cdm::kStreamTypeAudio); EXPECT_EQ(cdm::kSuccess, status); ReleaseSession(kSessionId1); } TEST_F(CdmApi4Test, SecureDecryptionLevel1WithSubsampleInfo) { CreateSession(kSessionId1, g_key_id, cdm::kTemporary); ProcessKeyResponse(false); ASSERT_FALSE(SessionErrorPresent(kSessionId1)); // Level 1 passes encrypted payload straight through. By calling the // CDM's DecryptDecodeAndRender, OEMCrypto_DecryptCTR will be told // to use Direct Rendering. std::vector encrypted = kInputVector2; std::vector iv = kIv2; std::vector sub = BuildMultipleSubsamples(); cdm::InputBuffer buf = BuildInputBuffer(encrypted, iv, sub); cdm::Status status; status = cdm_->DecryptDecodeAndRender(buf, cdm::kStreamTypeVideo); EXPECT_EQ(cdm::kSuccess, status); status = cdm_->DecryptDecodeAndRender(buf, cdm::kStreamTypeAudio); EXPECT_EQ(cdm::kSuccess, status); ReleaseSession(kSessionId1); } TEST_F(CdmApi4Test, SecureDecryptionLevel1WithMissingSubsampleInfo) { CreateSession(kSessionId1, g_key_id, cdm::kTemporary); ProcessKeyResponse(false); ASSERT_FALSE(SessionErrorPresent(kSessionId1)); std::vector encrypted = kInputVector2; std::vector iv = kIv2; std::vector sub = BuildMultipleSubsamples(); // Don't add these subsamples yet! cdm::InputBuffer buf = BuildInputBuffer(encrypted, iv); buf.num_subsamples = sub.size(); cdm::Status status; status = cdm_->DecryptDecodeAndRender(buf, cdm::kStreamTypeVideo); EXPECT_EQ(cdm::kDecryptError, status); status = cdm_->DecryptDecodeAndRender(buf, cdm::kStreamTypeAudio); EXPECT_EQ(cdm::kDecryptError, status); // Add the subsamples pointer and expect success. buf.subsamples = &sub[0]; status = cdm_->DecryptDecodeAndRender(buf, cdm::kStreamTypeVideo); EXPECT_EQ(cdm::kSuccess, status); status = cdm_->DecryptDecodeAndRender(buf, cdm::kStreamTypeAudio); EXPECT_EQ(cdm::kSuccess, status); ReleaseSession(kSessionId1); } TEST_F(CdmApi4Test, GenerateKeyRequestFailureSendsKeyError) { // Pass a bogus key id and expect failure. // Expect the CDM to pass a key error back to the host. CreateSession(kSessionId1, g_wrong_key_id, cdm::kTemporary); EXPECT_EQ(1, host_->SessionErrorsSize()); EXPECT_EQ(0, host_->SessionMessagesSize()); ReleaseSession(kSessionId1); } TEST_F(CdmApi4Test, UpdateSessionFailureSendsKeyError) { CreateSession(kSessionId1, g_key_id, cdm::kTemporary); TestHost_4::SessionMessage session_msg = host_->GetLastSessionMessage(); EXPECT_TRUE(session_msg.default_url.empty()); std::string drm_msg = GetKeyRequestResponse( session_msg, g_license_server + g_client_auth, false); // Call UpdateSession with a bad session id, and expect the CDM to pass a key // error back to the host. UpdateSession(kSessionId2, reinterpret_cast(drm_msg.c_str()), drm_msg.length()); EXPECT_EQ(1, host_->SessionErrorsSize()); // Call UpdateSession with a bad license, and expect the CDM to pass a key // error back to the host. static const std::string kBadLicense = "!kGoodLicense"; UpdateSession(kSessionId1, reinterpret_cast(kBadLicense.c_str()), kBadLicense.length()); EXPECT_EQ(2, host_->SessionErrorsSize()); ReleaseSession(kSessionId1); } TEST_F(CdmApi4Test, IsKeyValidDetectsValidKey) { EXPECT_FALSE(cdm_->IsKeyValid( reinterpret_cast(kTestKeyId.data()), kTestKeyId.size())); CreateSession(kSessionId1, g_key_id, cdm::kTemporary); ProcessKeyResponse(false); ASSERT_FALSE(SessionErrorPresent(kSessionId1)); EXPECT_TRUE(cdm_->IsKeyValid( reinterpret_cast(kTestKeyId.data()), kTestKeyId.size())); ReleaseSession(kSessionId1); EXPECT_FALSE(cdm_->IsKeyValid( reinterpret_cast(kTestKeyId.data()), kTestKeyId.size())); } TEST_F(CdmApi4Test, OfflineLicense) { // override default settings unless configured through the command line std::string key_id; std::string license_server; std::string client_auth; GetOfflineConfiguration(&key_id, &license_server, &client_auth); CreateSession(kSessionId1, key_id, cdm::kPersistent); ASSERT_FALSE(SessionErrorPresent(kSessionId1)); ProcessKeyResponse(false, license_server + client_auth); ASSERT_FALSE(SessionErrorPresent(kSessionId1)); ReleaseSession(kSessionId1); } TEST_F(CdmApi4Test, RestoreOfflineLicense) { // override default settings unless configured through the command line std::string key_id; std::string license_server; std::string client_auth; GetOfflineConfiguration(&key_id, &license_server, &client_auth); CreateSession(kSessionId1, key_id, cdm::kPersistent); ASSERT_FALSE(SessionErrorPresent(kSessionId1)); ProcessKeyResponse(false, license_server + client_auth); ASSERT_FALSE(SessionErrorPresent(kSessionId1)); std::string web_session_id = host_->session_map[kSessionId1]; EXPECT_FALSE(web_session_id.empty()); ReleaseSession(kSessionId1); LoadSession(kSessionId2, web_session_id); ASSERT_FALSE(SessionErrorPresent(kSessionId2)); ReleaseSession(kSessionId2); } TEST_F(CdmApi4Test, ReleaseOfflineLicense) { // override default settings unless configured through the command line std::string key_id; std::string license_server; std::string client_auth; GetOfflineConfiguration(&key_id, &license_server, &client_auth); CreateSession(kSessionId1, key_id, cdm::kPersistent); ASSERT_FALSE(SessionErrorPresent(kSessionId1)); ProcessKeyResponse(false, license_server + client_auth); ASSERT_FALSE(SessionErrorPresent(kSessionId1)); std::string web_session_id = host_->session_map[kSessionId1]; EXPECT_FALSE(web_session_id.empty()); ReleaseSession(kSessionId1); RemoveSession(kSessionId2, web_session_id); ProcessKeyResponse(true, license_server + client_auth); ASSERT_FALSE(SessionErrorPresent(kSessionId2)); } TEST_F(CdmApi4Test, MimeTypeMatters) { CreateSessionWithMimeType(kSessionId1, "video/mp4"); ASSERT_FALSE(SessionErrorPresent(kSessionId1)); ReleaseSession(kSessionId1); CreateSessionWithMimeType(kSessionId1, "video/webm"); ASSERT_FALSE(SessionErrorPresent(kSessionId1)); ReleaseSession(kSessionId1); CreateSessionWithMimeType(kSessionId1, "video/blah"); ASSERT_TRUE(SessionErrorPresent(kSessionId1)); } TEST_F(CdmApi4Test, UsePrivacyMode) { ASSERT_EQ(cdm::kSuccess, UsePrivacyMode()); const CdmClientPropertySet& property_set = reinterpret_cast(cdm_)->property_set_; EXPECT_TRUE(property_set.use_privacy_mode()); } TEST_F(CdmApi4Test, UsePrivacyModeFailsWithOpenSessions) { CreateSession(kSessionId1, g_key_id, cdm::kTemporary); ASSERT_FALSE(SessionErrorPresent(kSessionId1)); EXPECT_NE(cdm::kSuccess, UsePrivacyMode()); const CdmClientPropertySet& property_set = reinterpret_cast(cdm_)->property_set_; EXPECT_FALSE(property_set.use_privacy_mode()); ReleaseSession(kSessionId1); } TEST_F(CdmApi4Test, SetExplicitServerCertificate) { ASSERT_EQ(cdm::kSuccess, SetServerCertificate(kFakeServerCertficiate)); const CdmClientPropertySet& property_set = reinterpret_cast(cdm_)->property_set_; EXPECT_TRUE(property_set.use_privacy_mode()); const std::string& set_cert = property_set.service_certificate(); ASSERT_EQ(kFakeServerCertficiate.size(), set_cert.size()); EXPECT_EQ(0, memcmp(&kFakeServerCertficiate[0], &set_cert[0], set_cert.size())); } TEST_F(CdmApi4Test, SetServerCertificateFailsWithOpenSessions) { CreateSession(kSessionId1, g_key_id, cdm::kTemporary); ASSERT_FALSE(SessionErrorPresent(kSessionId1)); EXPECT_NE(cdm::kSuccess, SetServerCertificate(kFakeServerCertficiate)); const CdmClientPropertySet& property_set = reinterpret_cast(cdm_)->property_set_; EXPECT_FALSE(property_set.use_privacy_mode()); ReleaseSession(kSessionId1); } } // namespace wvcdm