From 6cda6717a9a130f9dd31d0289cd4e5f8b814c335 Mon Sep 17 00:00:00 2001 From: Rahul Frias Date: Sun, 13 Mar 2022 18:35:54 -0700 Subject: [PATCH] Reboot test: Initialize fake clock [ Merge of http://go/wvgerrit/143630 ] When we run a test with the fake clock, the clock had been initialized to the current time, or to 0. This causes a problem for reboot tests because the clock might go backwards over the reboot. With this change, we monitor the clock at the end of one reboot pass and initialize the clock for the next pass based on the previous value. Bug: 26163469 Test: GtsMediaTestCases on sunfish Change-Id: Ibd0024f963634382af70553fced38da6e1d857d2 --- libwvdrmengine/cdm/core/test/reboot_test.cpp | 34 ++++++++++++++++++++ libwvdrmengine/cdm/core/test/reboot_test.h | 6 ++++ libwvdrmengine/cdm/core/test/test_base.cpp | 14 ++++++++ libwvdrmengine/cdm/util/test/test_sleep.cpp | 15 +++++++++ libwvdrmengine/cdm/util/test/test_sleep.h | 4 +++ 5 files changed, 73 insertions(+) diff --git a/libwvdrmengine/cdm/core/test/reboot_test.cpp b/libwvdrmengine/cdm/core/test/reboot_test.cpp index 58d736e1..3cedd366 100644 --- a/libwvdrmengine/cdm/core/test/reboot_test.cpp +++ b/libwvdrmengine/cdm/core/test/reboot_test.cpp @@ -11,6 +11,7 @@ #include #include "log.h" +#include "test_sleep.h" using wvutil::a2b_hex; using wvutil::FileSystem; @@ -227,6 +228,23 @@ void RebootTest::TearDown() { WvCdmTestBase::TearDown(); } +int64_t RebootTest::LoadTime(const std::string& key) { + int64_t value = 0; + std::istringstream input(persistent_data_[key]); + input >> value; + if (input.fail()) { + LOGE("Could not parse time '%s'", persistent_data_[key].c_str()); + } + if (!input.eof()) { + LOGE("Extra text at end of time '%s'", persistent_data_[key].c_str()); + } + return value; +} + +void RebootTest::SaveTime(const std::string& key, int64_t time) { + persistent_data_[key] = std::to_string(time); +} + /** Test the dump and restore functions above. This does not test CDM functionality. */ TEST_F(RebootTest, TestDumpUtil) { @@ -277,4 +295,20 @@ TEST_F(RebootTest, FilesArePersistent) { } } +/** Verify that the clock moves forward over a reboot. */ +TEST_F(RebootTest, TimeMovesForward) { + wvutil::TestSleep::Sleep(2); + const int64_t start = wvutil::Clock().GetCurrentTime(); + wvutil::TestSleep::Sleep(2); + const int64_t end = wvutil::Clock().GetCurrentTime(); + EXPECT_NEAR(end - start, 2.0, 1.0); + const std::string key = "end_time"; + if (test_pass() == 0) { + // Save off the end of pass 1. + SaveTime(key, end); + } else { + int64_t previous_end = LoadTime(key); + EXPECT_LT(previous_end, start); + } +} } // namespace wvcdm diff --git a/libwvdrmengine/cdm/core/test/reboot_test.h b/libwvdrmengine/cdm/core/test/reboot_test.h index a0649bb1..0a9234be 100644 --- a/libwvdrmengine/cdm/core/test/reboot_test.h +++ b/libwvdrmengine/cdm/core/test/reboot_test.h @@ -36,6 +36,12 @@ class RebootTest : public WvCdmTestBaseWithEngine { static int test_pass() { return default_config_.test_pass(); } + // Load a previously saved time. Returns 0 if the value does not exist or + // cannot be parsed. + int64_t LoadTime(const std::string& key); + // Save a time to persistent storage. + void SaveTime(const std::string& key, int64_t time); + protected: void SetUp() override; void TearDown() override; diff --git a/libwvdrmengine/cdm/core/test/test_base.cpp b/libwvdrmengine/cdm/core/test/test_base.cpp index 1543acd5..3cfacd6e 100644 --- a/libwvdrmengine/cdm/core/test/test_base.cpp +++ b/libwvdrmengine/cdm/core/test/test_base.cpp @@ -122,6 +122,12 @@ void show_menu(const char* prog_name, const std::string& extra_help_text) { << " be used with a real OEMCrypto." << std::endl << std::endl; + std::cout << " --initial_time=