// Copyright 2021 Google LLC. All Rights Reserved. This file and proprietary // source code may only be used and distributed under the Widevine License // Agreement. // This uses dlopen() which is only usable on POSIX platforms. The function // names assume GCC/Clang. #include #include #include #include #include #include "clock.h" #include "perf_test.h" #include "test_host.h" namespace widevine { constexpr char kInitName[] = "_ZN8widevine3Cdm10initializeENS0_16SecureOutputTypeEPNS0_8IStorageEPNS0_" "6IClockEPNS0_6ITimerEPNS0_7ILoggerENS0_8LogLevelE"; constexpr char kCreateName[] = "_ZN8widevine3Cdm6createEPNS0_14IEventListenerEPNS0_8IStorageEb"; bool ReadFile(const std::string& path, std::string* output) { constexpr size_t kReadSize = 8 * 1024; std::ifstream fs(path, std::ios::in | std::ios::binary); if (!fs) return false; while (true) { const size_t offset = output->size(); output->resize(output->size() + kReadSize); fs.read(&output->at(offset), kReadSize); if (fs.eof()) { output->resize(offset + fs.gcount()); return true; } else if (!fs) { fprintf(stderr, "Error reading from cert file\n"); return false; } } } std::tuple LoadCdm(const char* path) { // Note we will leak the library object; but this is just for tests and will // be unloaded when we exit anyway. auto dll = dlopen(path, RTLD_NOW); if (!dll) { fprintf(stderr, "Error loading so file: %s\n", dlerror()); exit(1); } auto init = reinterpret_cast(dlsym(dll, kInitName)); auto create = reinterpret_cast(dlsym(dll, kCreateName)); if (!init || !create) { fprintf(stderr, "Error finding CDM functions: %s\n", dlerror()); exit(1); } return std::make_tuple(init, create); } } // namespace widevine namespace wvutil { int64_t Clock::GetCurrentTime() { return g_host->now() / 1000; } } // namespace wvutil int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv); if (argc != 3) { fprintf(stderr, "Usage: %s \n", argv[0]); return 1; } std::string cert; if (!widevine::ReadFile(argv[2], &cert)) return 1; auto funcs = widevine::LoadCdm(argv[1]); return widevine::PerfTestMain(std::get<0>(funcs), std::get<1>(funcs), cert); }