// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary // source code may only be used and distributed under the Widevine Master // License Agreement. // This file adds some print methods so that when unit tests fail, the // will print the name of an enumeration instead of the numeric value. #include "test_base.h" #include #include #include #include #include #include "cdm_engine.h" #include "crypto_session.h" #include "file_store.h" #include "log.h" #include "oec_device_features.h" #include "oec_test_data.h" #include "properties.h" #include "test_printers.h" #include "url_request.h" namespace wvcdm { TestCryptoSession::TestCryptoSession(metrics::CryptoMetrics* crypto_metrics) : CryptoSession(crypto_metrics) { // The first CryptoSession should have initialized OEMCrypto. This is right // after that, so should tell oemcrypto to use a test keybox. if (session_count() == 1) { WvCdmTestBase::InstallTestRootOfTrust(); } } bool TestCryptoSession::GenerateNonce(uint32_t* nonce) { for (int i = 0; !CryptoSession::GenerateNonce(nonce); i++) { LOGV("Recovering from nonce flood."); if (i > 2) return false; sleep(1); } return true; } class TestCryptoSessionFactory : public CryptoSessionFactory { CryptoSession* MakeCryptoSession(metrics::CryptoMetrics* crypto_metrics) { return new TestCryptoSession(crypto_metrics); } }; void WvCdmTestBase::SetUp() { ::testing::Test::SetUp(); Properties::Init(); const ::testing::TestInfo* const test_info = ::testing::UnitTest::GetInstance()->current_test_info(); LOGD("Running test %s.%s", test_info->test_case_name(), test_info->name()); CryptoSession::SetCryptoSessionFactory(new TestCryptoSessionFactory()); // TODO(fredgc): Add a test version of DeviceFiles. } void WvCdmTestBase::InstallTestRootOfTrust() { switch (wvoec::global_features.derive_key_method) { case wvoec::DeviceFeatures::LOAD_TEST_KEYBOX: ASSERT_EQ(OEMCrypto_SUCCESS, OEMCrypto_LoadTestKeybox( reinterpret_cast(&wvoec::kTestKeybox), sizeof(wvoec::kTestKeybox))); break; case wvoec::DeviceFeatures::LOAD_TEST_RSA_KEY: // Rare case: used by devices with baked in DRM cert. ASSERT_EQ(OEMCrypto_SUCCESS, OEMCrypto_LoadTestRSAKey()); break; case wvoec::DeviceFeatures::TEST_PROVISION_30: // Can use oem certificate to install test rsa key. break; default: FAIL() << "Cannot run test without test keybox or RSA key installed."; } } } // namespace wvcdm