56 lines
1.6 KiB
C++
56 lines
1.6 KiB
C++
// Copyright 2019 Google LLC. All Rights Reserved. This file and proprietary
|
|
// source code may only be used and distributed under the Widevine License
|
|
// Agreement.
|
|
//
|
|
// Clock - A fake clock just for running tests. This is used when running
|
|
// OEMCrypto unit tests. It is not used when tests include the CE CDM source
|
|
// code because that uses the clock in cdm/test_host.cpp instead.
|
|
#include <inttypes.h>
|
|
|
|
#include <chrono>
|
|
|
|
#include "clock.h"
|
|
#include "test_sleep.h"
|
|
#include "wv_duration.h"
|
|
#include "wv_timestamp.h"
|
|
|
|
namespace wvutil {
|
|
|
|
namespace {
|
|
// A fake clock that only advances when TestSleep::Sleep is called.
|
|
class FakeClock : public TestSleep::CallBack {
|
|
public:
|
|
FakeClock() {
|
|
auto now = std::chrono::system_clock().now();
|
|
now_ = Timestamp::FromUnixMilliseconds(
|
|
std::chrono::floor<Milliseconds>(now.time_since_epoch()));
|
|
TestSleep::AddCallback(this);
|
|
}
|
|
~FakeClock() { TestSleep::RemoveCallback(this); }
|
|
void ElapseTime(int64_t milliseconds) override {
|
|
now_ += Duration::FromMilliseconds(milliseconds);
|
|
}
|
|
|
|
Timestamp now() const { return now_; }
|
|
|
|
private:
|
|
Timestamp now_;
|
|
};
|
|
|
|
FakeClock* g_fake_clock = nullptr;
|
|
} // namespace
|
|
|
|
// On devices running a fake OEMCrypto, we can use a fake sleep and fake time.
|
|
int64_t Clock::GetCurrentTime() {
|
|
TestSleep::SyncFakeClock();
|
|
if (g_fake_clock == nullptr) g_fake_clock = new FakeClock();
|
|
return static_cast<int64_t>(g_fake_clock->now().epoch_seconds().count());
|
|
}
|
|
|
|
Timestamp Clock::GetCurrentTimestamp() {
|
|
TestSleep::SyncFakeClock();
|
|
if (g_fake_clock == nullptr) g_fake_clock = new FakeClock();
|
|
return g_fake_clock->now();
|
|
}
|
|
} // namespace wvutil
|