Add test base that catches nonce flood
Merge from Widevine repo of http://go/wvgerrit/56520 This CL adds a test base that installs a test keybox and catches nonce flood errors for all CDM tests. In order to do this, a new class is added called a CryptoSessionFactory. The default factory just creates a new CryptoSession. All places in the code that create a new CryptoSession now call the static method MakeCryptoSession, which uses the current factory to create a CryptoSession. If MakeCryptoSession is called and there is no current factory, a default factory is created. The CryptoSession constructor is now private, so that we do not accidentally try to create one without using the factory. For the new test base, we first create a special test CryptoSessionFactory that creates a TestCryptoSession. The test factory catches the first call to MakeCryptoSession and injects an installation of the test keybox after OEMCrypto_Initialize is called. The TestCryptoSession injects a sleep statement and a retry whenever it detects a nonce flood. Test: current unit tests still pass. bug: 72354901 Fix Generic Crypto tests. bug: 111361440 Remove #ifdef from unit tests Change-Id: I248e7f3c53721c04d2af412ef835e19bb4d15d9a
This commit is contained in:
82
libwvdrmengine/cdm/core/test/test_base.cpp
Normal file
82
libwvdrmengine/cdm/core/test/test_base.cpp
Normal file
@@ -0,0 +1,82 @@
|
||||
// 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 <getopt.h>
|
||||
|
||||
#include <openssl/aes.h>
|
||||
#include <openssl/bio.h>
|
||||
#include <openssl/cmac.h>
|
||||
#include <openssl/err.h>
|
||||
|
||||
#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<const uint8_t*>(&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
|
||||
Reference in New Issue
Block a user