File: safe_seed_manager_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 (300 lines) | stat: -rw-r--r-- 11,576 bytes parent folder | download | duplicates (5)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright 2018 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/variations/service/safe_seed_manager.h"

#include <memory>
#include <string>

#include "base/base_switches.h"
#include "base/command_line.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "base/version_info/channel.h"
#include "components/metrics/clean_exit_beacon.h"
#include "components/prefs/testing_pref_service.h"
#include "components/variations/client_filterable_state.h"
#include "components/variations/pref_names.h"
#include "components/variations/service/safe_seed_manager_base.h"
#include "components/variations/variations_safe_seed_store_local_state.h"
#include "components/variations/variations_seed_store.h"
#include "components/variations/variations_switches.h"
#include "components/variations/variations_test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace variations {

namespace {

const char kTestSeed[] = "compressed, base-64 encoded serialized seed data";
const char kTestSignature[] = "a completely unforged signature, I promise!";
const int kTestSeedMilestone = 92;
const char kTestLocale[] = "en-US";
const char kTestPermanentConsistencyCountry[] = "US";
const char kTestSessionConsistencyCountry[] = "CA";

base::Time GetTestFetchTime() {
  return base::Time::FromDeltaSinceWindowsEpoch(base::Days(123));
}

// A simple fake data store.
class FakeSeedStore : public VariationsSeedStore {
 public:
  explicit FakeSeedStore(TestingPrefServiceSimple* local_state)
      : VariationsSeedStore(local_state,
                            /*initial_seed=*/nullptr,
                            /*signature_verification_enabled=*/true,
                            std::make_unique<VariationsSafeSeedStoreLocalState>(
                                local_state,
                                /*seed_file_dir=*/base::FilePath(),
                                version_info::Channel::UNKNOWN,
                                /*entropy_providers=*/nullptr),
                            version_info::Channel::UNKNOWN,
                            /*seed_file_dir=*/base::FilePath(),
                            /*entropy_providers=*/nullptr) {}

  FakeSeedStore(const FakeSeedStore&) = delete;
  FakeSeedStore& operator=(const FakeSeedStore&) = delete;

  ~FakeSeedStore() override = default;

  bool StoreSafeSeed(const std::string& seed_data,
                     const std::string& base64_seed_signature,
                     int seed_milestone,
                     const ClientFilterableState& client_state,
                     base::Time seed_fetch_time) override {
    seed_data_ = seed_data;
    signature_ = base64_seed_signature;
    seed_milestone_ = seed_milestone;
    date_ = client_state.reference_date;
    locale_ = client_state.locale;
    permanent_consistency_country_ = client_state.permanent_consistency_country;
    session_consistency_country_ = client_state.session_consistency_country;
    fetch_time_ = seed_fetch_time;
    return true;
  }

  const std::string& seed_data() const { return seed_data_; }
  const std::string& signature() const { return signature_; }
  int seed_milestone() const { return seed_milestone_; }
  const base::Time& date() const { return date_; }
  const std::string& locale() const { return locale_; }
  const std::string& permanent_consistency_country() const {
    return permanent_consistency_country_;
  }
  const std::string& session_consistency_country() const {
    return session_consistency_country_;
  }
  const base::Time& fetch_time() const { return fetch_time_; }

 private:
  // The stored data.
  std::string seed_data_;
  std::string signature_;
  int seed_milestone_ = 0;
  base::Time date_;
  std::string locale_;
  std::string permanent_consistency_country_;
  std::string session_consistency_country_;
  base::Time fetch_time_;
};

// Passes the default test values as the active state into |safe_seed_manager|
// and |local_state|.
void SetDefaultActiveState(SafeSeedManager* safe_seed_manager,
                           PrefService* local_state) {
  std::unique_ptr<ClientFilterableState> client_state =
      CreateDummyClientFilterableState();
  client_state->locale = kTestLocale;
  client_state->permanent_consistency_country =
      kTestPermanentConsistencyCountry;
  client_state->session_consistency_country = kTestSessionConsistencyCountry;
  client_state->reference_date = base::Time::UnixEpoch();

  local_state->SetInteger(prefs::kVariationsSeedMilestone, kTestSeedMilestone);

  safe_seed_manager->SetActiveSeedState(
      kTestSeed, kTestSignature, kTestSeedMilestone, std::move(client_state),
      GetTestFetchTime());
}

// Verifies that the default test values were written to the seed store.
void ExpectDefaultActiveState(const FakeSeedStore& seed_store) {
  EXPECT_EQ(kTestSeed, seed_store.seed_data());
  EXPECT_EQ(kTestSignature, seed_store.signature());
  EXPECT_EQ(kTestSeedMilestone, seed_store.seed_milestone());
  EXPECT_EQ(kTestLocale, seed_store.locale());
  EXPECT_EQ(kTestPermanentConsistencyCountry,
            seed_store.permanent_consistency_country());
  EXPECT_EQ(kTestSessionConsistencyCountry,
            seed_store.session_consistency_country());
  EXPECT_EQ(base::Time::UnixEpoch(), seed_store.date());
  EXPECT_EQ(GetTestFetchTime(), seed_store.fetch_time());
}

}  // namespace

class SafeSeedManagerTest : public ::testing::Test {
 public:
  SafeSeedManagerTest() {
    metrics::CleanExitBeacon::RegisterPrefs(prefs_.registry());
    SafeSeedManager::RegisterPrefs(prefs_.registry());
    VariationsSeedStore::RegisterPrefs(prefs_.registry());
  }
  ~SafeSeedManagerTest() override = default;

 protected:
  base::test::TaskEnvironment task_environment_;
  TestingPrefServiceSimple prefs_;
};

TEST_F(SafeSeedManagerTest, RecordSuccessfulFetch_FirstCallSavesSafeSeed) {
  SafeSeedManager safe_seed_manager(&prefs_);
  FakeSeedStore seed_store(&prefs_);
  SetDefaultActiveState(&safe_seed_manager, &prefs_);

  safe_seed_manager.RecordSuccessfulFetch(&seed_store);
  ExpectDefaultActiveState(seed_store);
}

TEST_F(SafeSeedManagerTest, RecordSuccessfulFetch_RepeatedCallsRetainSafeSeed) {
  SafeSeedManager safe_seed_manager(&prefs_);
  FakeSeedStore seed_store(&prefs_);
  SetDefaultActiveState(&safe_seed_manager, &prefs_);

  safe_seed_manager.RecordSuccessfulFetch(&seed_store);
  safe_seed_manager.RecordSuccessfulFetch(&seed_store);
  safe_seed_manager.RecordSuccessfulFetch(&seed_store);
  ExpectDefaultActiveState(seed_store);
}

TEST_F(SafeSeedManagerTest,
       RecordSuccessfulFetch_NoActiveState_DoesntSaveSafeSeed) {
  SafeSeedManager safe_seed_manager(&prefs_);
  FakeSeedStore seed_store(&prefs_);
  // Omit setting any active state.

  safe_seed_manager.RecordSuccessfulFetch(&seed_store);
  EXPECT_EQ(std::string(), seed_store.seed_data());
  EXPECT_EQ(std::string(), seed_store.signature());
  EXPECT_EQ(0, seed_store.seed_milestone());
  EXPECT_EQ(std::string(), seed_store.locale());
  EXPECT_EQ(std::string(), seed_store.permanent_consistency_country());
  EXPECT_EQ(std::string(), seed_store.session_consistency_country());
  EXPECT_EQ(base::Time(), seed_store.date());
  EXPECT_EQ(base::Time(), seed_store.fetch_time());
}

TEST_F(SafeSeedManagerTest, FetchFailureMetrics_DefaultPrefs) {
  base::HistogramTester histogram_tester;
  SafeSeedManager safe_seed_manager(&prefs_);
  histogram_tester.ExpectUniqueSample(
      "Variations.SafeMode.Streak.FetchFailures", 0, 1);
}

TEST_F(SafeSeedManagerTest, FetchFailureMetrics_NoFailures) {
  prefs_.SetInteger(prefs::kVariationsFailedToFetchSeedStreak, 0);

  base::HistogramTester histogram_tester;
  SafeSeedManager safe_seed_manager(&prefs_);
  histogram_tester.ExpectUniqueSample(
      "Variations.SafeMode.Streak.FetchFailures", 0, 1);
}

TEST_F(SafeSeedManagerTest, FetchFailureMetrics_SomeFailures) {
  prefs_.SetInteger(prefs::kVariationsFailedToFetchSeedStreak, 2);

  base::HistogramTester histogram_tester;
  SafeSeedManager safe_seed_manager(&prefs_);
  histogram_tester.ExpectUniqueSample(
      "Variations.SafeMode.Streak.FetchFailures", 2, 1);
}

TEST_F(SafeSeedManagerTest, ShouldRunInSafeMode_OverriddenByCommandlineFlag) {
  // So many failures.
  prefs_.SetInteger(prefs::kVariationsCrashStreak, 100);
  prefs_.SetInteger(prefs::kVariationsFailedToFetchSeedStreak, 100);
  base::CommandLine::ForCurrentProcess()->AppendSwitch(
      switches::kDisableVariationsSafeMode);

  SafeSeedManager safe_seed_manager(&prefs_);
  EXPECT_EQ(SeedType::kRegularSeed, safe_seed_manager.GetSeedType());
}

TEST_F(SafeSeedManagerTest, ShouldRunInSafeMode_NoCrashes_NoFetchFailures) {
  prefs_.SetInteger(prefs::kVariationsCrashStreak, 0);
  prefs_.SetInteger(prefs::kVariationsFailedToFetchSeedStreak, 0);

  SafeSeedManager safe_seed_manager(&prefs_);
  EXPECT_EQ(SeedType::kRegularSeed, safe_seed_manager.GetSeedType());
}

TEST_F(SafeSeedManagerTest, ShouldRunInSafeMode_NoPrefs) {
  // Don't explicitly set either of the prefs. The implicit/default values
  // should be zero.
  SafeSeedManager safe_seed_manager(&prefs_);
  EXPECT_EQ(SeedType::kRegularSeed, safe_seed_manager.GetSeedType());
}

TEST_F(SafeSeedManagerTest, ShouldRunInSafeMode_FewCrashes_FewFetchFailures) {
  prefs_.SetInteger(prefs::kVariationsCrashStreak, 2);
  prefs_.SetInteger(prefs::kVariationsFailedToFetchSeedStreak, 2);

  SafeSeedManager safe_seed_manager(&prefs_);
  EXPECT_EQ(SeedType::kRegularSeed, safe_seed_manager.GetSeedType());
}

TEST_F(SafeSeedManagerTest, ShouldRunInSafeMode_ManyCrashes_NoFetchFailures) {
  prefs_.SetInteger(prefs::kVariationsCrashStreak, 3);
  prefs_.SetInteger(prefs::kVariationsFailedToFetchSeedStreak, 0);

  SafeSeedManager safe_seed_manager(&prefs_);
  EXPECT_EQ(SeedType::kSafeSeed, safe_seed_manager.GetSeedType());
}

TEST_F(SafeSeedManagerTest,
       ShouldRunInSafeMode_ManyMoreCrashes_NoFetchFailures) {
  prefs_.SetInteger(prefs::kVariationsCrashStreak, 6);
  prefs_.SetInteger(prefs::kVariationsFailedToFetchSeedStreak, 0);

  SafeSeedManager safe_seed_manager(&prefs_);
  EXPECT_EQ(SeedType::kNullSeed, safe_seed_manager.GetSeedType());
}

TEST_F(SafeSeedManagerTest, ShouldRunInSafeMode_NoCrashes_ManyFetchFailures) {
  prefs_.SetInteger(prefs::kVariationsCrashStreak, 0);
  prefs_.SetInteger(prefs::kVariationsFailedToFetchSeedStreak, 25);

  SafeSeedManager safe_seed_manager(&prefs_);
  EXPECT_EQ(SeedType::kSafeSeed, safe_seed_manager.GetSeedType());
}

TEST_F(SafeSeedManagerTest,
       ShouldRunInSafeMode_NoCrashes_ManyMoreFetchFailures) {
  prefs_.SetInteger(prefs::kVariationsCrashStreak, 0);
  prefs_.SetInteger(prefs::kVariationsFailedToFetchSeedStreak, 50);

  SafeSeedManager safe_seed_manager(&prefs_);
  EXPECT_EQ(SeedType::kNullSeed, safe_seed_manager.GetSeedType());
}

TEST_F(SafeSeedManagerTest, ShouldRunInSafeMode_ManyCrashes_ManyFetchFailures) {
  prefs_.SetInteger(prefs::kVariationsCrashStreak, 3);
  prefs_.SetInteger(prefs::kVariationsFailedToFetchSeedStreak, 25);

  SafeSeedManager safe_seed_manager(&prefs_);
  EXPECT_EQ(SeedType::kSafeSeed, safe_seed_manager.GetSeedType());
}

TEST_F(SafeSeedManagerTest,
       ShouldRunInSafeMode_ManyMoreCrashes_ManyMoreFetchFailures) {
  prefs_.SetInteger(prefs::kVariationsCrashStreak, 6);
  prefs_.SetInteger(prefs::kVariationsFailedToFetchSeedStreak, 50);

  SafeSeedManager safe_seed_manager(&prefs_);
  EXPECT_EQ(SeedType::kNullSeed, safe_seed_manager.GetSeedType());
}

}  // namespace variations