monthly_use_case_impl_unittest.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/components/device_activity/monthly_use_case_impl.h"
  5. #include "ash/components/device_activity/device_activity_controller.h"
  6. #include "ash/components/device_activity/fresnel_pref_names.h"
  7. #include "base/strings/string_util.h"
  8. #include "base/time/time.h"
  9. #include "components/prefs/testing_pref_service.h"
  10. #include "components/version_info/channel.h"
  11. #include "testing/gmock/include/gmock/gmock-matchers.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. #include "third_party/private_membership/src/private_membership_rlwe_client.h"
  14. namespace ash {
  15. namespace device_activity {
  16. namespace psm_rlwe = private_membership::rlwe;
  17. namespace {
  18. // Initialize fake values used by the |MonthlyUseCaseImpl|.
  19. constexpr char kFakePsmDeviceActiveSecret[] = "FAKE_PSM_DEVICE_ACTIVE_SECRET";
  20. constexpr ChromeDeviceMetadataParameters kFakeChromeParameters = {
  21. version_info::Channel::STABLE /* chromeos_channel */,
  22. MarketSegment::MARKET_SEGMENT_UNKNOWN /* market_segment */,
  23. };
  24. } // namespace
  25. // TODO(hirthanan): Move shared tests to DeviceActiveUseCase base class.
  26. class MonthlyUseCaseImplTest : public testing::Test {
  27. public:
  28. MonthlyUseCaseImplTest() = default;
  29. MonthlyUseCaseImplTest(const MonthlyUseCaseImplTest&) = delete;
  30. MonthlyUseCaseImplTest& operator=(const MonthlyUseCaseImplTest&) = delete;
  31. ~MonthlyUseCaseImplTest() override = default;
  32. protected:
  33. // testing::Test:
  34. void SetUp() override {
  35. DeviceActivityController::RegisterPrefs(local_state_.registry());
  36. monthly_use_case_impl_ = std::make_unique<MonthlyUseCaseImpl>(
  37. kFakePsmDeviceActiveSecret, kFakeChromeParameters, &local_state_);
  38. }
  39. void TearDown() override { monthly_use_case_impl_.reset(); }
  40. std::unique_ptr<MonthlyUseCaseImpl> monthly_use_case_impl_;
  41. // Fake pref service for unit testing the local state.
  42. TestingPrefServiceSimple local_state_;
  43. };
  44. TEST_F(MonthlyUseCaseImplTest, CheckIfLastKnownPingTimestampNotSet) {
  45. EXPECT_FALSE(monthly_use_case_impl_->IsLastKnownPingTimestampSet());
  46. }
  47. TEST_F(MonthlyUseCaseImplTest, CheckIfLastKnownPingTimestampSet) {
  48. // Create fixed timestamp to see if local state updates value correctly.
  49. base::Time new_monthly_ts;
  50. EXPECT_TRUE(
  51. base::Time::FromString("01 Jan 2022 23:59:59 GMT", &new_monthly_ts));
  52. // Update local state with fixed timestamp.
  53. monthly_use_case_impl_->SetLastKnownPingTimestamp(new_monthly_ts);
  54. EXPECT_EQ(monthly_use_case_impl_->GetLastKnownPingTimestamp(),
  55. new_monthly_ts);
  56. EXPECT_TRUE(monthly_use_case_impl_->IsLastKnownPingTimestampSet());
  57. }
  58. TEST_F(MonthlyUseCaseImplTest, CheckGenerateUTCWindowIdentifierHasValidFormat) {
  59. // Create fixed timestamp used to generate a fixed window identifier.
  60. base::Time new_monthly_ts;
  61. EXPECT_TRUE(
  62. base::Time::FromString("01 Jan 2022 23:59:59 GMT", &new_monthly_ts));
  63. std::string window_id =
  64. monthly_use_case_impl_->GenerateUTCWindowIdentifier(new_monthly_ts);
  65. EXPECT_EQ(window_id.size(), 6);
  66. EXPECT_EQ(window_id, "202201");
  67. }
  68. TEST_F(MonthlyUseCaseImplTest, CheckPsmIdEmptyIfWindowIdIsNotSet) {
  69. // |monthly_use_case_impl_| must set the window id before generating the psm
  70. // id.
  71. EXPECT_THAT(monthly_use_case_impl_->GetPsmIdentifier(),
  72. testing::Eq(absl::nullopt));
  73. }
  74. TEST_F(MonthlyUseCaseImplTest, CheckPsmIdGeneratedCorrectly) {
  75. // Create fixed timestamp used to generate a fixed window identifier.
  76. // The window id must be set before generating the psm id.
  77. base::Time new_monthly_ts;
  78. EXPECT_TRUE(
  79. base::Time::FromString("01 Jan 2022 23:59:59 GMT", &new_monthly_ts));
  80. std::string window_id =
  81. monthly_use_case_impl_->GenerateUTCWindowIdentifier(new_monthly_ts);
  82. monthly_use_case_impl_->SetWindowIdentifier(window_id);
  83. absl::optional<psm_rlwe::RlwePlaintextId> psm_id =
  84. monthly_use_case_impl_->GetPsmIdentifier();
  85. EXPECT_TRUE(psm_id.has_value());
  86. // Verify the PSM value is correct for parameters supplied by the unit tests.
  87. std::string unhashed_psm_id = base::JoinString(
  88. {psm_rlwe::RlweUseCase_Name(monthly_use_case_impl_->GetPsmUseCase()),
  89. window_id},
  90. "|");
  91. std::string expected_psm_id_hex = monthly_use_case_impl_->GetDigestString(
  92. kFakePsmDeviceActiveSecret, unhashed_psm_id);
  93. EXPECT_EQ(psm_id.value().sensitive_id(), expected_psm_id_hex);
  94. }
  95. TEST_F(MonthlyUseCaseImplTest, PingRequiredInNonOverlappingUTCWindows) {
  96. base::Time last_monthly_ts;
  97. base::Time current_monthly_ts;
  98. EXPECT_TRUE(
  99. base::Time::FromString("01 Jan 2022 00:00:00 GMT", &last_monthly_ts));
  100. monthly_use_case_impl_->SetLastKnownPingTimestamp(last_monthly_ts);
  101. EXPECT_TRUE(
  102. base::Time::FromString("25 Feb 2022 00:00:00 GMT", &current_monthly_ts));
  103. EXPECT_TRUE(monthly_use_case_impl_->IsDevicePingRequired(current_monthly_ts));
  104. }
  105. TEST_F(MonthlyUseCaseImplTest, PingNotRequiredInOverlappingUTCWindows) {
  106. base::Time last_monthly_ts;
  107. base::Time current_monthly_ts;
  108. EXPECT_TRUE(
  109. base::Time::FromString("01 Jan 2022 12:59:59 GMT", &last_monthly_ts));
  110. monthly_use_case_impl_->SetLastKnownPingTimestamp(last_monthly_ts);
  111. EXPECT_TRUE(
  112. base::Time::FromString("25 Jan 2022 15:59:59 GMT", &current_monthly_ts));
  113. EXPECT_FALSE(
  114. monthly_use_case_impl_->IsDevicePingRequired(current_monthly_ts));
  115. }
  116. TEST_F(MonthlyUseCaseImplTest, CheckIfPingRequiredInUTCBoundaryCases) {
  117. base::Time last_monthly_ts;
  118. base::Time current_monthly_ts;
  119. EXPECT_TRUE(
  120. base::Time::FromString("31 Jan 2022 23:59:59 GMT", &last_monthly_ts));
  121. monthly_use_case_impl_->SetLastKnownPingTimestamp(last_monthly_ts);
  122. EXPECT_TRUE(
  123. base::Time::FromString("01 Feb 2022 00:00:00 GMT", &current_monthly_ts));
  124. EXPECT_TRUE(monthly_use_case_impl_->IsDevicePingRequired(current_monthly_ts));
  125. // Set last_monthly_ts as a date after current_monthly_ts.
  126. EXPECT_TRUE(
  127. base::Time::FromString("01 Feb 2022 00:00:00 GMT", &last_monthly_ts));
  128. monthly_use_case_impl_->SetLastKnownPingTimestamp(last_monthly_ts);
  129. EXPECT_TRUE(
  130. base::Time::FromString("31 Jan 2022 23:59:59 GMT", &current_monthly_ts));
  131. // Since the current_monthly_ts is prior to the last_monthly_ts, the function
  132. // should return false.
  133. EXPECT_FALSE(
  134. monthly_use_case_impl_->IsDevicePingRequired(current_monthly_ts));
  135. }
  136. TEST_F(MonthlyUseCaseImplTest, SameMonthTimestampsHaveSameWindowId) {
  137. base::Time monthly_ts_1;
  138. base::Time monthly_ts_2;
  139. EXPECT_TRUE(
  140. base::Time::FromString("01 Jan 2022 00:00:00 GMT", &monthly_ts_1));
  141. EXPECT_TRUE(
  142. base::Time::FromString("31 Jan 2022 23:59:59 GMT", &monthly_ts_2));
  143. EXPECT_EQ(monthly_use_case_impl_->GenerateUTCWindowIdentifier(monthly_ts_1),
  144. monthly_use_case_impl_->GenerateUTCWindowIdentifier(monthly_ts_2));
  145. }
  146. TEST_F(MonthlyUseCaseImplTest, DifferentWindowIdGeneratesDifferentPsmId) {
  147. base::Time monthly_ts_1;
  148. base::Time monthly_ts_2;
  149. EXPECT_TRUE(
  150. base::Time::FromString("01 Jan 2022 00:00:00 GMT", &monthly_ts_1));
  151. EXPECT_TRUE(
  152. base::Time::FromString("01 Feb 2022 00:00:00 GMT", &monthly_ts_2));
  153. std::string window_id_1 =
  154. monthly_use_case_impl_->GenerateUTCWindowIdentifier(monthly_ts_1);
  155. std::string window_id_2 =
  156. monthly_use_case_impl_->GenerateUTCWindowIdentifier(monthly_ts_2);
  157. monthly_use_case_impl_->SetWindowIdentifier(window_id_1);
  158. absl::optional<psm_rlwe::RlwePlaintextId> psm_id_1 =
  159. monthly_use_case_impl_->GetPsmIdentifier();
  160. monthly_use_case_impl_->SetWindowIdentifier(window_id_2);
  161. absl::optional<psm_rlwe::RlwePlaintextId> psm_id_2 =
  162. monthly_use_case_impl_->GetPsmIdentifier();
  163. EXPECT_NE(psm_id_1.value().sensitive_id(), psm_id_2.value().sensitive_id());
  164. }
  165. } // namespace device_activity
  166. } // namespace ash