(This is a merge of http://go/wvgerrit/152969.) C++ makes absolutely no guarantees about the order of initialization of global variables in different compilation units. The class-scope static WvCdmTestBase::default_config_ in test_base.cpp invokes the ConfigTestEnv constructor on creation, which depends on the prior initialization of several file-scope static variables in config_test_env.cpp. Since those are different compilation units, there is no guarantee that they will initialize in the correct order to avoid referencing uninitialized memory. This is one of the reasons Google Style really encourages people not to have global-scope variables with complex types. As it happens, on all our internal platforms, these files get linked in such a way that the variables get initialized in the right order and there is no crash. But that's not guaranteed, and some partners have reported crashes here. In at least one case, the "right" linker order was platform-dependent, and the partner ended up having to maintain separate linker orders for separate platforms. This patch defers default_config_ initialization until WvCdmTestBase::Initialize() is called. By that time, all static variables will be initialized, so it will be safe to reference them. Bug: 173252165 Test: x86-64 Test: build_and_run_all_unit_tests.sh Change-Id: If31128a999c7d6945f47293ca57f08e43d8274de
121 lines
4.5 KiB
C++
121 lines
4.5 KiB
C++
// Copyright 2018 Google LLC. All Rights Reserved. This file and proprietary
|
|
// source code may only be used and distributed under the Widevine License
|
|
// Agreement.
|
|
|
|
#ifndef WVCDM_CORE_TEST_BASE_H_
|
|
#define WVCDM_CORE_TEST_BASE_H_
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <memory>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "cdm_engine.h"
|
|
#include "config_test_env.h"
|
|
#include "crypto_session.h"
|
|
#include "metrics_collections.h"
|
|
#include "oemcrypto_types.h"
|
|
#include "string_conversions.h"
|
|
|
|
namespace wvcdm {
|
|
// This is the base class for Widevine CDM integration tests. It's main use is
|
|
// to configure OEMCrypto to use a test keybox.
|
|
class WvCdmTestBase : public ::testing::Test {
|
|
public:
|
|
WvCdmTestBase();
|
|
~WvCdmTestBase() override {}
|
|
void SetUp() override;
|
|
virtual std::string binary_key_id() const {
|
|
return wvutil::a2bs_hex(config_.key_id());
|
|
}
|
|
|
|
// Returns true if the test program should continue, if false, the caller
|
|
// should exit. This should be called by main() to allow the user to pass in
|
|
// command line switches. The |extra_help_text| parameter can be used to
|
|
// append platform-specific information to the usage information printed when
|
|
// invalid flags are detected. For instance, a platform might add information
|
|
// about platform-specific flags that were already parsed before calling
|
|
// Initialize().
|
|
static bool Initialize(int argc, const char* const argv[],
|
|
const std::string& extra_help_text = std::string());
|
|
// Install a test keybox, if appropriate.
|
|
static void InstallTestRootOfTrust();
|
|
|
|
// Send provisioning request to the server and handle response.
|
|
virtual void Provision();
|
|
// Calls Provision() if not already provisioned.
|
|
virtual void EnsureProvisioned();
|
|
|
|
// Fill a buffer with some nonconstant data of the given size. The first byte
|
|
// will be set to <init> to help you find the buffer when debugging.
|
|
static void StripeBuffer(std::vector<uint8_t>* buffer, size_t size,
|
|
uint8_t init);
|
|
|
|
// Helper method for doing cryptography.
|
|
static std::string Aes128CbcEncrypt(std::vector<uint8_t> key,
|
|
const std::vector<uint8_t>& clear,
|
|
const std::vector<uint8_t> iv);
|
|
// Helper method for doing cryptography.
|
|
static std::string Aes128CbcDecrypt(std::vector<uint8_t> key,
|
|
const std::vector<uint8_t>& clear,
|
|
const std::vector<uint8_t> iv);
|
|
// Helper method for doing cryptography.
|
|
static std::string SignHMAC(const std::string& message,
|
|
const std::vector<uint8_t>& key);
|
|
|
|
// The default test configuration. This is created in Initialize() and
|
|
// influenced by command line arguments before any tests are created.
|
|
static std::unique_ptr<ConfigTestEnv> default_config_;
|
|
|
|
// If the tests should use the QA test keybox.
|
|
static bool use_qa_test_keybox_;
|
|
|
|
// Configuration for an individual test. This is initialized to be the
|
|
// default configuration, but can be modified by the test itself.
|
|
ConfigTestEnv config_;
|
|
|
|
// This should be set by test subclasses BEFORE calling SetUp -- i.e. in the
|
|
// tests's constructor.
|
|
bool binary_provisioning_;
|
|
};
|
|
|
|
// This just makes the constructor public so that we can create one with dummy
|
|
// metrics and file system.
|
|
class TestCdmEngine : public CdmEngine {
|
|
public:
|
|
TestCdmEngine(wvutil::FileSystem* file_system,
|
|
std::shared_ptr<metrics::EngineMetrics> metrics)
|
|
: CdmEngine(file_system, metrics) {}
|
|
};
|
|
|
|
class WvCdmTestBaseWithEngine : public WvCdmTestBase {
|
|
public:
|
|
WvCdmTestBaseWithEngine()
|
|
: dummy_engine_metrics_(new metrics::EngineMetrics()),
|
|
cdm_engine_(&file_system_, dummy_engine_metrics_) {}
|
|
|
|
protected:
|
|
wvutil::FileSystem file_system_;
|
|
std::shared_ptr<metrics::EngineMetrics> dummy_engine_metrics_;
|
|
TestCdmEngine cdm_engine_;
|
|
};
|
|
|
|
class TestCryptoSession : public CryptoSession {
|
|
public:
|
|
explicit TestCryptoSession(metrics::CryptoMetrics* crypto_metrics);
|
|
// This intercepts nonce flood errors, which is useful for tests that request
|
|
// many nonces and are not time critical.
|
|
CdmResponseType GenerateNonce(uint32_t* nonce) override;
|
|
};
|
|
|
|
// Given a PSSH data structure, this makes a PSSH string for use in
|
|
// generating a license request.
|
|
std::string MakePSSH(const video_widevine::WidevinePsshData& header);
|
|
// Given a serialized PSSH data, this generates a full PSSH string.
|
|
std::string MakePSSH(const std::string& serialized_header);
|
|
|
|
} // namespace wvcdm
|
|
|
|
#endif // WVCDM_CORE_TEST_BASE_H_
|