Files
ce_cdm/cdm/test/perf_test_dynamic.cpp
2024-03-28 19:15:22 -07:00

85 lines
2.3 KiB
C++

// 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 <dlfcn.h>
#include <gtest/gtest.h>
#include <stdio.h>
#include <fstream>
#include <tuple>
#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_8IStorageEbb";
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<InitFuncType, CreateFuncType> 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<InitFuncType>(dlsym(dll, kInitName));
auto create = reinterpret_cast<CreateFuncType>(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 <CDM_SO_PATH> <CERT_PATH>\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);
}