entropy_state_unittest.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright 2020 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 "components/metrics/entropy_state.h"
  5. #include <string>
  6. #include "base/strings/string_number_conversions.h"
  7. #include "base/strings/string_util.h"
  8. #include "components/metrics/metrics_pref_names.h"
  9. #include "components/metrics/metrics_service.h"
  10. #include "components/prefs/testing_pref_service.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. namespace metrics {
  13. class EntropyStateTest : public testing::Test {
  14. public:
  15. EntropyStateTest() { MetricsService::RegisterPrefs(prefs_.registry()); }
  16. EntropyStateTest(const EntropyStateTest&) = delete;
  17. EntropyStateTest& operator=(const EntropyStateTest&) = delete;
  18. protected:
  19. TestingPrefServiceSimple prefs_;
  20. };
  21. TEST_F(EntropyStateTest, LowEntropySourceNotReset) {
  22. EntropyState entropy_state(&prefs_);
  23. // Get the low entropy source once, to initialize it.
  24. entropy_state.GetLowEntropySource();
  25. // Now, set it to 0 and ensure it doesn't get reset.
  26. entropy_state.low_entropy_source_ = 0;
  27. EXPECT_EQ(0, entropy_state.GetLowEntropySource());
  28. // Call it another time, just to make sure.
  29. EXPECT_EQ(0, entropy_state.GetLowEntropySource());
  30. }
  31. TEST_F(EntropyStateTest, PseudoLowEntropySourceNotReset) {
  32. EntropyState entropy_state(&prefs_);
  33. // Get the pseudo low entropy source once, to initialize it.
  34. entropy_state.GetPseudoLowEntropySource();
  35. // Now, set it to 0 and ensure it doesn't get reset.
  36. entropy_state.pseudo_low_entropy_source_ = 0;
  37. EXPECT_EQ(0, entropy_state.GetPseudoLowEntropySource());
  38. // Call it another time, just to make sure.
  39. EXPECT_EQ(0, entropy_state.GetPseudoLowEntropySource());
  40. }
  41. TEST_F(EntropyStateTest, HaveNoLowEntropySource) {
  42. EntropyState entropy_state(&prefs_);
  43. // If we have none of the new, old, or pseudo low entropy sources in prefs,
  44. // then the new source should be created...
  45. int new_low_source = entropy_state.GetLowEntropySource();
  46. EXPECT_TRUE(EntropyState::IsValidLowEntropySource(new_low_source))
  47. << new_low_source;
  48. int pseudo_low_source = entropy_state.GetPseudoLowEntropySource();
  49. EXPECT_TRUE(EntropyState::IsValidLowEntropySource(pseudo_low_source))
  50. << pseudo_low_source;
  51. // ...but the old source should not...
  52. EXPECT_EQ(EntropyState::kLowEntropySourceNotSet,
  53. entropy_state.GetOldLowEntropySource());
  54. // ...and the high entropy source should include the *new* low entropy source.
  55. std::string high_source = entropy_state.GetHighEntropySource(
  56. "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEF");
  57. EXPECT_TRUE(base::EndsWith(high_source, base::NumberToString(new_low_source),
  58. base::CompareCase::SENSITIVE))
  59. << high_source;
  60. }
  61. TEST_F(EntropyStateTest, HaveOnlyNewLowEntropySource) {
  62. // If we have the new low entropy sources in prefs, but not the old one...
  63. const int new_low_source = 1234;
  64. prefs_.SetInteger(prefs::kMetricsLowEntropySource, new_low_source);
  65. EntropyState entropy_state(&prefs_);
  66. // ...then the new source should be loaded...
  67. EXPECT_EQ(new_low_source, entropy_state.GetLowEntropySource());
  68. // ...but the old source should not be created...
  69. EXPECT_EQ(EntropyState::kLowEntropySourceNotSet,
  70. entropy_state.GetOldLowEntropySource());
  71. // ...and the high entropy source should include the *new* low entropy source.
  72. std::string high_source = entropy_state.GetHighEntropySource(
  73. "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEF");
  74. EXPECT_TRUE(base::EndsWith(high_source, base::NumberToString(new_low_source),
  75. base::CompareCase::SENSITIVE))
  76. << high_source;
  77. }
  78. TEST_F(EntropyStateTest, HaveOnlyOldLowEntropySource) {
  79. // If we have the old low entropy sources in prefs, but not the new one...
  80. const int old_low_source = 5678;
  81. prefs_.SetInteger(prefs::kMetricsOldLowEntropySource, old_low_source);
  82. // ...then the new source should be created...
  83. EntropyState entropy_state(&prefs_);
  84. int new_low_source = entropy_state.GetLowEntropySource();
  85. EXPECT_TRUE(EntropyState::IsValidLowEntropySource(new_low_source))
  86. << new_low_source;
  87. // ...and the old source should be loaded...
  88. EXPECT_EQ(old_low_source, entropy_state.GetOldLowEntropySource());
  89. // ...and the high entropy source should include the *old* low entropy source.
  90. std::string high_source = entropy_state.GetHighEntropySource(
  91. "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEF");
  92. EXPECT_TRUE(base::EndsWith(high_source, base::NumberToString(old_low_source),
  93. base::CompareCase::SENSITIVE))
  94. << high_source;
  95. }
  96. TEST_F(EntropyStateTest, HaveAllLowEntropySources) {
  97. // If we have all three of new, old, and pseudo low entropy sources in
  98. // prefs...
  99. const int new_low_source = 1234;
  100. const int old_low_source = 5678;
  101. const int pseudo_low_source = 4321;
  102. prefs_.SetInteger(prefs::kMetricsLowEntropySource, new_low_source);
  103. prefs_.SetInteger(prefs::kMetricsOldLowEntropySource, old_low_source);
  104. prefs_.SetInteger(prefs::kMetricsPseudoLowEntropySource, pseudo_low_source);
  105. // ...then all three should be loaded...
  106. EntropyState entropy_state(&prefs_);
  107. EXPECT_EQ(new_low_source, entropy_state.GetLowEntropySource());
  108. EXPECT_EQ(old_low_source, entropy_state.GetOldLowEntropySource());
  109. EXPECT_EQ(pseudo_low_source, entropy_state.GetPseudoLowEntropySource());
  110. // ...and the high entropy source should include the *old* low entropy source.
  111. std::string high_source = entropy_state.GetHighEntropySource(
  112. "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEF");
  113. EXPECT_TRUE(base::EndsWith(high_source, base::NumberToString(old_low_source),
  114. base::CompareCase::SENSITIVE))
  115. << high_source;
  116. }
  117. TEST_F(EntropyStateTest, CorruptNewLowEntropySources) {
  118. EntropyState entropy_state(&prefs_);
  119. const int corrupt_sources[] = {-12345, -1, 8000, 12345};
  120. for (int corrupt_source : corrupt_sources) {
  121. // If the new low entropy source has been corrupted...
  122. EXPECT_FALSE(EntropyState::IsValidLowEntropySource(corrupt_source))
  123. << corrupt_source;
  124. prefs_.SetInteger(prefs::kMetricsLowEntropySource, corrupt_source);
  125. // ...then a new source should be created.
  126. int loaded_source = entropy_state.GetLowEntropySource();
  127. EXPECT_TRUE(EntropyState::IsValidLowEntropySource(loaded_source))
  128. << loaded_source;
  129. }
  130. }
  131. TEST_F(EntropyStateTest, CorruptOldLowEntropySources) {
  132. EntropyState entropy_state(&prefs_);
  133. const int corrupt_sources[] = {-12345, -1, 8000, 12345};
  134. for (int corrupt_source : corrupt_sources) {
  135. // If the old low entropy source has been corrupted...
  136. EXPECT_FALSE(EntropyState::IsValidLowEntropySource(corrupt_source))
  137. << corrupt_source;
  138. prefs_.SetInteger(prefs::kMetricsOldLowEntropySource, corrupt_source);
  139. // ...then it should be ignored.
  140. EXPECT_EQ(EntropyState::kLowEntropySourceNotSet,
  141. entropy_state.GetOldLowEntropySource());
  142. }
  143. }
  144. TEST_F(EntropyStateTest, ClearPrefs) {
  145. prefs_.SetInteger(prefs::kMetricsLowEntropySource, 1234);
  146. prefs_.SetInteger(prefs::kMetricsOldLowEntropySource, 5678);
  147. prefs_.SetInteger(prefs::kMetricsPseudoLowEntropySource, 4321);
  148. EntropyState::ClearPrefs(&prefs_);
  149. EXPECT_FALSE(prefs_.HasPrefPath(prefs::kMetricsLowEntropySource));
  150. EXPECT_FALSE(prefs_.HasPrefPath(prefs::kMetricsOldLowEntropySource));
  151. EXPECT_FALSE(prefs_.HasPrefPath(prefs::kMetricsPseudoLowEntropySource));
  152. }
  153. } // namespace metrics