File: device_info_prefs_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (103 lines) | stat: -rw-r--r-- 4,069 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/sync_device_info/device_info_prefs.h"

#include "base/strings/stringprintf.h"
#include "base/test/simple_test_clock.h"
#include "base/values.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/prefs/testing_pref_service.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace syncer {
namespace {

class DeviceInfoPrefsTest : public testing::Test {
 protected:
  DeviceInfoPrefsTest() : device_info_prefs_(&pref_service_, &clock_) {
    DeviceInfoPrefs::RegisterProfilePrefs(pref_service_.registry());
  }
  ~DeviceInfoPrefsTest() override = default;

  DeviceInfoPrefs device_info_prefs_;
  base::SimpleTestClock clock_;
  TestingPrefServiceSimple pref_service_;
};

TEST_F(DeviceInfoPrefsTest, ShouldGarbageCollectExpiredCacheGuids) {
  const base::TimeDelta kMaxDaysLocalCacheGuidsStored = base::Days(10);

  device_info_prefs_.AddLocalCacheGuid("guid1");
  clock_.Advance(kMaxDaysLocalCacheGuidsStored - base::Minutes(1));
  device_info_prefs_.AddLocalCacheGuid("guid2");

  // First garbage collection immediately before taking effect, hence a no-op.
  device_info_prefs_.GarbageCollectExpiredCacheGuids();
  EXPECT_TRUE(device_info_prefs_.IsRecentLocalCacheGuid("guid1"));
  EXPECT_TRUE(device_info_prefs_.IsRecentLocalCacheGuid("guid2"));

  // Advancing one day causes the first GUID to be garbage-collected.
  clock_.Advance(base::Days(1));
  device_info_prefs_.GarbageCollectExpiredCacheGuids();
  EXPECT_FALSE(device_info_prefs_.IsRecentLocalCacheGuid("guid1"));
  EXPECT_TRUE(device_info_prefs_.IsRecentLocalCacheGuid("guid2"));
}

// Regression test for crbug.com/1029673.
TEST_F(DeviceInfoPrefsTest, ShouldCleanUpCorruptEntriesUponGarbageCollection) {
  const char kDeviceInfoRecentGUIDsWithTimestamps[] =
      "sync.local_device_guids_with_timestamp";

  // Add one valid entry for sanity-checking (should not be deleted).
  device_info_prefs_.AddLocalCacheGuid("guid1");

  // Manipulate the preference directly to add a corrupt entry to the list,
  // which is a string instead of a dictionary.
  ScopedListPrefUpdate cache_guids_update(&pref_service_,
                                          kDeviceInfoRecentGUIDsWithTimestamps);
  base::Value::List& update_list = cache_guids_update.Get();
  update_list.Append(base::Value("corrupt_string_entry"));

  // Add another corrupt entry: in this case the entry is a dictionary, but it
  // contains no timestamp.
  update_list.Append(base::Value::Dict{});

  // The end result is the list contains three entries among which one is valid.
  ASSERT_EQ(3u,
            pref_service_.GetList(kDeviceInfoRecentGUIDsWithTimestamps).size());
  ASSERT_TRUE(device_info_prefs_.IsRecentLocalCacheGuid("guid1"));

  // Garbage collection should clean up the corrupt entries.
  device_info_prefs_.GarbageCollectExpiredCacheGuids();
  ASSERT_TRUE(device_info_prefs_.IsRecentLocalCacheGuid("guid1"));

  // |guid1| should be the only entry in the list.
  EXPECT_EQ(1u,
            pref_service_.GetList(kDeviceInfoRecentGUIDsWithTimestamps).size());
}

TEST_F(DeviceInfoPrefsTest, ShouldTruncateAfterMaximumNumberOfGuids) {
  const int kMaxLocalCacheGuidsStored = 30;

  device_info_prefs_.AddLocalCacheGuid("orig_guid");

  // Fill up exactly the maximum number, without triggering a truncation.
  for (int i = 0; i < kMaxLocalCacheGuidsStored - 1; i++) {
    device_info_prefs_.AddLocalCacheGuid(base::StringPrintf("guid%d", i));
  }

  EXPECT_TRUE(device_info_prefs_.IsRecentLocalCacheGuid("orig_guid"));

  // Adding one more should truncate exactly one.
  device_info_prefs_.AddLocalCacheGuid("newest_guid");
  EXPECT_FALSE(device_info_prefs_.IsRecentLocalCacheGuid("orig_guid"));
  EXPECT_TRUE(device_info_prefs_.IsRecentLocalCacheGuid("newest_guid"));
  EXPECT_TRUE(device_info_prefs_.IsRecentLocalCacheGuid("guid1"));
}

}  // namespace

}  // namespace syncer