From 7112a5eec3d1dfd1a6e4bfd66e0ce5f563d802ee Mon Sep 17 00:00:00 2001 From: Fred Gylys-Colwell Date: Mon, 13 Feb 2017 18:10:58 -0800 Subject: [PATCH] Add more slack to timing tests Merge from Widevine repo of http://go/wvgerrit/24042 This CL adjusts the tolerance in tests that check the license_received time. This was periodically failing because a nonce flood might delay the test by 1 second, which was being rounded up to 2. The tolerance is now 3. The time is explicily used when it is available. Some extra logging is also added to the mock. bug:31458046 Change-Id: I450880cb3cd8bd5ef66cba13b94dd963d2663d9a --- .../oemcrypto/mock/src/oemcrypto_session.cpp | 3 ++- .../oemcrypto/test/oec_session_util.cpp | 15 ++++++++++++--- libwvdrmengine/oemcrypto/test/oec_session_util.h | 1 - libwvdrmengine/oemcrypto/test/oemcrypto_test.cpp | 2 +- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/libwvdrmengine/oemcrypto/mock/src/oemcrypto_session.cpp b/libwvdrmengine/oemcrypto/mock/src/oemcrypto_session.cpp index fba20b50..743716cb 100644 --- a/libwvdrmengine/oemcrypto/mock/src/oemcrypto_session.cpp +++ b/libwvdrmengine/oemcrypto/mock/src/oemcrypto_session.cpp @@ -971,7 +971,8 @@ OEMCryptoResult SessionContext::SelectContentKey(const KeyId& key_id) { const KeyControlBlock& control = current_content_key()->control(); if (control.duration() > 0) { if (control.duration() < CurrentTimer()) { - LOGE("[SelectContentKey(): KEY_EXPIRED]"); + LOGE("[SelectContentKey(): KEY_EXPIRED %d versus %d]", + control.duration(), CurrentTimer()); return OEMCrypto_ERROR_KEY_EXPIRED; } } diff --git a/libwvdrmengine/oemcrypto/test/oec_session_util.cpp b/libwvdrmengine/oemcrypto/test/oec_session_util.cpp index f581f59e..d84285a7 100644 --- a/libwvdrmengine/oemcrypto/test/oec_session_util.cpp +++ b/libwvdrmengine/oemcrypto/test/oec_session_util.cpp @@ -263,6 +263,7 @@ void Session::RefreshTestKeys(const size_t key_count, uint32_t control_bits, uint32_t nonce, OEMCryptoResult expected_result) { // Note: we store the message in encrypted_license_, but the refresh key // message is not actually encrypted. It is, however, signed. + // FillRefreshMessage fills the message with a duration of kLongDuration. FillRefreshMessage(key_count, control_bits, nonce); ServerSignBuffer(reinterpret_cast(&padded_message_), message_size_, &signature_); @@ -274,9 +275,14 @@ void Session::RefreshTestKeys(const size_t key_count, uint32_t control_bits, ASSERT_EQ(expected_result, sts); ASSERT_NO_FATAL_FAILURE(TestDecryptCTR()); - sleep(kShortSleep); // Should still be valid key. + // This should still be valid key, even if the refresh failed, because this + // is before the original license duration. + sleep(kShortSleep); ASSERT_NO_FATAL_FAILURE(TestDecryptCTR(false)); - sleep(kShortSleep + kLongSleep); // Should be after first expiration. + // This should be after duration of the original license, but before the + // expiration of the refresh message. This should succeed if and only if the + // refresh succeeded. + sleep(kShortSleep + kLongSleep); if (expected_result == OEMCrypto_SUCCESS) { ASSERT_NO_FATAL_FAILURE(TestDecryptCTR(false, OEMCrypto_SUCCESS)); } else { @@ -920,7 +926,7 @@ void Session::VerifyPST(const Test_PST_Report& expected) { ASSERT_EQ(expected.pst, computed_pst); EXPECT_NEAR(expected.seconds_since_license_received, computed.seconds_since_license_received(), - kLicenseReceivedTimeTolerance); + kTimeTolerance); // Decrypt times only valid on licenses that have been active. if (expected.status == kActive || expected.status == kInactiveUsed) { EXPECT_NEAR(expected.seconds_since_first_decrypt, @@ -943,6 +949,9 @@ void Session::VerifyPST(const Test_PST_Report& expected) { SHA_DIGEST_LENGTH)); } +// This might adjust t to be "seconds since now". If t is small, we assume it +// is "seconds since now", but if the value of t is large, assume it is +// "absolute time" and convert to "seconds since now". static int64_t MaybeAdjustTime(int64_t t, time_t now) { int64_t k10Minutes = 60 * 10; // in seconds. if (t > k10Minutes) return now - t; diff --git a/libwvdrmengine/oemcrypto/test/oec_session_util.h b/libwvdrmengine/oemcrypto/test/oec_session_util.h index bc60baa6..03716f60 100644 --- a/libwvdrmengine/oemcrypto/test/oec_session_util.h +++ b/libwvdrmengine/oemcrypto/test/oec_session_util.h @@ -51,7 +51,6 @@ const int kLongSleep = 2 * kSpeedMultiplier; const uint32_t kDuration = 2 * kSpeedMultiplier; const uint32_t kLongDuration = 5 * kSpeedMultiplier; const int32_t kTimeTolerance = 3 * kSpeedMultiplier; -const int kLicenseReceivedTimeTolerance = kSpeedMultiplier; const time_t kUsageTableTimeTolerance = 10 * kSpeedMultiplier; } // namespace diff --git a/libwvdrmengine/oemcrypto/test/oemcrypto_test.cpp b/libwvdrmengine/oemcrypto/test/oemcrypto_test.cpp index 296c4950..7b4544b6 100644 --- a/libwvdrmengine/oemcrypto/test/oemcrypto_test.cpp +++ b/libwvdrmengine/oemcrypto/test/oemcrypto_test.cpp @@ -4801,7 +4801,7 @@ TEST_P(UsageTableTestWithMAC, ReloadOfflineLicenseWithRefresh) { ASSERT_NO_FATAL_FAILURE(s.ReloadUsageEntry()); ASSERT_NO_FATAL_FAILURE(s.LoadTestKeys(pst, new_mac_keys_)); ASSERT_NO_FATAL_FAILURE(s.UpdateUsageEntry(&encrypted_usage_header_)); - ASSERT_NO_FATAL_FAILURE(s.GenerateVerifyReport(pst, kUnused)); + ASSERT_NO_FATAL_FAILURE(s.GenerateVerifyReport(pst, kUnused, loaded, 0, 0)); ASSERT_NO_FATAL_FAILURE(s.TestDecryptCTR()); time_t decrypt_time = time(NULL); ASSERT_NO_FATAL_FAILURE(s.UpdateUsageEntry(&encrypted_usage_header_));