// 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_CDM_TEST_TEST_HOST_H_ #define WVCDM_CDM_TEST_TEST_HOST_H_ #include #include #include #include #include "cdm.h" #include "stderr_logger.h" #include "test_sleep.h" // This provides a host environment for running CDM tests. It implements the // IStorage, IClock and ITimer interfaces that a host would normally implement, // while allowing them to be manipulated by the test. class TestHost : public widevine::Cdm::IClock, public widevine::Cdm::ITimer, public wvutil::TestSleep::CallBack { public: class Storage : public widevine::Cdm::IStorage { public: typedef std::map StorageMap; explicit Storage(bool is_global); ~Storage() override {} void Reset(); // Save and load the storage from a string buffer so it can be on disk. bool LoadFromString(const std::string& data); bool SaveToString(std::string* data) const; // Reset the file system to contain the specified files. void ResetFiles(const StorageMap& files) { files_ = files; }; const StorageMap& files() const { return files_; } // widevine::Cdm::IStorage bool read(const std::string& name, std::string* data) override; bool write(const std::string& name, const std::string& data) override; bool exists(const std::string& name) override; bool remove(const std::string& name) override; int32_t size(const std::string& name) override; bool list(std::vector* names) override; private: bool is_global_; StorageMap files_; bool CheckFilename(const std::string& name); }; TestHost(); ~TestHost() override; void Reset(); // Used for manipulating and inspecting timer states during testing. void ElapseTime(int64_t milliseconds) override; size_t NumTimers() const; Storage& global_storage() { return global_storage_; } Storage& per_origin_storage() { return per_origin_storage_; } // widevine::Cdm::IClock int64_t now() override; // widevine::Cdm::ITimer void setTimeout(int64_t delay_ms, IClient* client, void* context) override; void cancel(IClient* client) override; // If this is set, then the storage will return this as a baked in cert. // Trying to write a new cert will generate an error. const std::string& baked_in_cert() const { return baked_in_cert_; }; void set_baked_in_cert(const std::string& baked_in_cert) { baked_in_cert_ = baked_in_cert; }; private: struct Timer { Timer(int64_t expiry_time, IClient* client, void* context) : expiry_time_(expiry_time), client_(client), context_(context) {} bool operator<(const Timer& other) const { // We want to reverse the order so that the smallest expiry times go to // the top of the priority queue. return expiry_time_ > other.expiry_time_; } int64_t expiry_time() { return expiry_time_; } IClient* client() { return client_; } void* context() { return context_; } private: int64_t expiry_time_; IClient* client_; void* context_; }; int64_t now_; std::priority_queue timers_; Storage global_storage_; Storage per_origin_storage_; std::string baked_in_cert_; }; // Owned and managed by the test runner. extern TestHost* g_host; extern widevine::StderrLogger g_stderr_logger; extern std::string g_sandbox_id; #endif // WVCDM_CDM_TEST_TEST_HOST_H_