device_info_prefs_unittest.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2019 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/sync_device_info/device_info_prefs.h"
  5. #include "base/strings/stringprintf.h"
  6. #include "base/test/simple_test_clock.h"
  7. #include "base/values.h"
  8. #include "components/prefs/pref_registry_simple.h"
  9. #include "components/prefs/scoped_user_pref_update.h"
  10. #include "components/prefs/testing_pref_service.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. namespace syncer {
  13. namespace {
  14. class DeviceInfoPrefsTest : public testing::Test {
  15. protected:
  16. DeviceInfoPrefsTest() : device_info_prefs_(&pref_service_, &clock_) {
  17. DeviceInfoPrefs::RegisterProfilePrefs(pref_service_.registry());
  18. }
  19. ~DeviceInfoPrefsTest() override = default;
  20. DeviceInfoPrefs device_info_prefs_;
  21. base::SimpleTestClock clock_;
  22. TestingPrefServiceSimple pref_service_;
  23. };
  24. TEST_F(DeviceInfoPrefsTest, ShouldGarbageCollectExpiredCacheGuids) {
  25. const base::TimeDelta kMaxDaysLocalCacheGuidsStored = base::Days(10);
  26. device_info_prefs_.AddLocalCacheGuid("guid1");
  27. clock_.Advance(kMaxDaysLocalCacheGuidsStored - base::Minutes(1));
  28. device_info_prefs_.AddLocalCacheGuid("guid2");
  29. // First garbage collection immediately before taking effect, hence a no-op.
  30. device_info_prefs_.GarbageCollectExpiredCacheGuids();
  31. EXPECT_TRUE(device_info_prefs_.IsRecentLocalCacheGuid("guid1"));
  32. EXPECT_TRUE(device_info_prefs_.IsRecentLocalCacheGuid("guid2"));
  33. // Advancing one day causes the first GUID to be garbage-collected.
  34. clock_.Advance(base::Days(1));
  35. device_info_prefs_.GarbageCollectExpiredCacheGuids();
  36. EXPECT_FALSE(device_info_prefs_.IsRecentLocalCacheGuid("guid1"));
  37. EXPECT_TRUE(device_info_prefs_.IsRecentLocalCacheGuid("guid2"));
  38. }
  39. // Regression test for crbug.com/1029673.
  40. TEST_F(DeviceInfoPrefsTest, ShouldCleanUpCorruptEntriesUponGarbageCollection) {
  41. const char kDeviceInfoRecentGUIDsWithTimestamps[] =
  42. "sync.local_device_guids_with_timestamp";
  43. // Add one valid entry for sanity-checking (should not be deleted).
  44. device_info_prefs_.AddLocalCacheGuid("guid1");
  45. // Manipulate the preference directly to add a corrupt entry to the list,
  46. // which is a string instead of a dictionary.
  47. ListPrefUpdate cache_guids_update(&pref_service_,
  48. kDeviceInfoRecentGUIDsWithTimestamps);
  49. base::Value::List& update_list = cache_guids_update->GetList();
  50. update_list.Insert(update_list.begin(), base::Value("corrupt_string_entry"));
  51. // Add another corrupt entry: in this case the entry is a dictionary, but it
  52. // contains no timestamp.
  53. update_list.Insert(update_list.begin(),
  54. base::Value(base::Value::Type::DICTIONARY));
  55. // The end result is the list contains three entries among which one is valid.
  56. ASSERT_EQ(
  57. 3u,
  58. pref_service_.GetValueList(kDeviceInfoRecentGUIDsWithTimestamps).size());
  59. ASSERT_TRUE(device_info_prefs_.IsRecentLocalCacheGuid("guid1"));
  60. // Garbage collection should clean up the corrupt entries.
  61. device_info_prefs_.GarbageCollectExpiredCacheGuids();
  62. ASSERT_TRUE(device_info_prefs_.IsRecentLocalCacheGuid("guid1"));
  63. // |guid1| should be the only entry in the list.
  64. EXPECT_EQ(
  65. 1u,
  66. pref_service_.GetValueList(kDeviceInfoRecentGUIDsWithTimestamps).size());
  67. }
  68. TEST_F(DeviceInfoPrefsTest, ShouldTruncateAfterMaximumNumberOfGuids) {
  69. const int kMaxLocalCacheGuidsStored = 30;
  70. device_info_prefs_.AddLocalCacheGuid("orig_guid");
  71. // Fill up exactly the maximum number, without triggering a truncation.
  72. for (int i = 0; i < kMaxLocalCacheGuidsStored - 1; i++) {
  73. device_info_prefs_.AddLocalCacheGuid(base::StringPrintf("guid%d", i));
  74. }
  75. EXPECT_TRUE(device_info_prefs_.IsRecentLocalCacheGuid("orig_guid"));
  76. // Adding one more should truncate exactly one.
  77. device_info_prefs_.AddLocalCacheGuid("newest_guid");
  78. EXPECT_FALSE(device_info_prefs_.IsRecentLocalCacheGuid("orig_guid"));
  79. EXPECT_TRUE(device_info_prefs_.IsRecentLocalCacheGuid("newest_guid"));
  80. EXPECT_TRUE(device_info_prefs_.IsRecentLocalCacheGuid("guid1"));
  81. }
  82. } // namespace
  83. } // namespace syncer