OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
Alexei Svitkine (slow)
2015/01/14 17:29:06
2015 now.
Steven Holte
2015/01/14 21:12:16
Done.
| |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/rappor/rappor_prefs.h" | |
6 | |
7 #include "base/base64.h" | |
8 #include "base/prefs/testing_pref_service.h" | |
9 #include "base/test/histogram_tester.h" | |
10 #include "components/rappor/byte_vector_utils.h" | |
11 #include "components/rappor/proto/rappor_metric.pb.h" | |
12 #include "components/rappor/rappor_pref_names.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 namespace rappor { | |
16 | |
17 namespace internal { | |
18 | |
19 namespace { | |
20 | |
21 // Convert a secret to base 64 and store it in preferences. | |
22 void StoreSecret(TestingPrefServiceSimple* test_prefs, | |
Alexei Svitkine (slow)
2015/01/14 17:29:05
Nit: Modifiable params (i.e. test_prefs) should be
Steven Holte
2015/01/14 21:12:16
Done.
| |
23 const std::string& secret) { | |
24 std::string secret_base64; | |
25 base::Base64Encode(secret, &secret_base64); | |
26 test_prefs->SetString(prefs::kRapporSecret, secret_base64); | |
27 } | |
28 | |
29 // Verify that the current value of the secret pref matches the loaded secret. | |
30 void ExpectConsistentSecret(const TestingPrefServiceSimple& test_prefs, | |
31 const std::string& loaded_secret) { | |
32 std::string pref = test_prefs.GetString(prefs::kRapporSecret); | |
33 std::string decoded_pref; | |
34 EXPECT_TRUE(base::Base64Decode(pref, &decoded_pref)); | |
35 EXPECT_EQ(loaded_secret, decoded_pref); | |
36 } | |
37 | |
38 } // namespace | |
39 | |
40 TEST(RapporPrefsTest, EmptyCohort) { | |
41 base::HistogramTester histogram; | |
Alexei Svitkine (slow)
2015/01/14 17:29:05
Nit: tester or histogram_tester, it's not a histog
Steven Holte
2015/01/14 21:12:16
Done.
| |
42 TestingPrefServiceSimple test_prefs; | |
43 RegisterPrefs(test_prefs.registry()); | |
Alexei Svitkine (slow)
2015/01/14 17:29:05
All these tests begin the same. Please make a test
Steven Holte
2015/01/14 21:12:16
Done.
| |
44 test_prefs.ClearPref(prefs::kRapporCohortSeed); | |
45 // Loaded cohort should have been rerolled into a valid number. | |
46 int32_t cohort = LoadCohort(&test_prefs); | |
47 histogram.ExpectUniqueSample(kLoadCohortHistogramName, LOAD_EMPTY_VALUE, 1); | |
48 EXPECT_GE(cohort, 0); | |
49 EXPECT_LT(cohort, RapporParameters::kMaxCohorts); | |
50 // The preferences should be consistent with the loaded value. | |
51 int32_t pref = test_prefs.GetInteger(prefs::kRapporCohortSeed); | |
52 EXPECT_EQ(pref, cohort); | |
53 } | |
54 | |
55 TEST(RapporPrefsTest, LoadCohort) { | |
56 base::HistogramTester histogram; | |
57 TestingPrefServiceSimple test_prefs; | |
58 RegisterPrefs(test_prefs.registry()); | |
59 test_prefs.SetInteger(prefs::kRapporCohortSeed, 1); | |
60 // Loading the valid cohort should just retrieve it. | |
61 int32_t cohort = LoadCohort(&test_prefs); | |
62 histogram.ExpectUniqueSample(kLoadCohortHistogramName, LOAD_SUCCESS, 1); | |
63 EXPECT_EQ(1, cohort); | |
64 // The preferences should be consistent with the loaded value. | |
65 int32_t pref = test_prefs.GetInteger(prefs::kRapporCohortSeed); | |
66 EXPECT_EQ(pref, cohort); | |
67 } | |
68 | |
69 TEST(RapporPrefsTest, CorruptCohort) { | |
70 base::HistogramTester histogram; | |
71 TestingPrefServiceSimple test_prefs; | |
72 RegisterPrefs(test_prefs.registry()); | |
73 // Set an invalid cohort value in the preference. | |
74 test_prefs.SetInteger(prefs::kRapporCohortSeed, -10); | |
75 // Loaded cohort should have been rerolled into a valid number. | |
76 int32_t cohort = LoadCohort(&test_prefs); | |
77 histogram.ExpectUniqueSample(kLoadCohortHistogramName, LOAD_CORRUPT_VALUE, 1); | |
78 EXPECT_GE(cohort, 0); | |
79 EXPECT_LT(cohort, RapporParameters::kMaxCohorts); | |
80 // The preferences should be consistent with the loaded value. | |
81 int32_t pref = test_prefs.GetInteger(prefs::kRapporCohortSeed); | |
82 EXPECT_EQ(pref, cohort); | |
83 } | |
84 | |
85 TEST(RapporPrefsTest, EmptySecret) { | |
86 base::HistogramTester histogram; | |
87 TestingPrefServiceSimple test_prefs; | |
88 RegisterPrefs(test_prefs.registry()); | |
89 test_prefs.ClearPref(prefs::kRapporSecret); | |
90 // Loaded secret should be rerolled from empty. | |
91 std::string secret2 = LoadSecret(&test_prefs); | |
92 histogram.ExpectUniqueSample(kLoadSecretHistogramName, LOAD_EMPTY_VALUE, 1); | |
93 EXPECT_EQ(HmacByteVectorGenerator::kEntropyInputSize, secret2.size()); | |
94 // The stored preference should also be updated. | |
95 ExpectConsistentSecret(test_prefs, secret2); | |
96 } | |
97 | |
98 TEST(RapporPrefsTest, LoadSecret) { | |
99 base::HistogramTester histogram; | |
100 TestingPrefServiceSimple test_prefs; | |
101 RegisterPrefs(test_prefs.registry()); | |
102 std::string secret1 = HmacByteVectorGenerator::GenerateEntropyInput(); | |
103 StoreSecret(&test_prefs, secret1); | |
104 // Secret should load successfully. | |
105 std::string secret2 = LoadSecret(&test_prefs); | |
106 histogram.ExpectUniqueSample(kLoadSecretHistogramName, LOAD_SUCCESS, 1); | |
107 EXPECT_EQ(secret1, secret2); | |
108 // The stored preference should also be unchanged. | |
109 ExpectConsistentSecret(test_prefs, secret2); | |
110 } | |
111 | |
112 TEST(RapporPrefsTest, CorruptSecret) { | |
113 base::HistogramTester histogram; | |
114 TestingPrefServiceSimple test_prefs; | |
115 RegisterPrefs(test_prefs.registry()); | |
116 // Store an invalid secret in the preferences that won't decode as base64. | |
117 test_prefs.SetString(prefs::kRapporSecret, "!!INVALID!!"); | |
118 // We should have rerolled a new secret. | |
119 std::string secret2 = LoadSecret(&test_prefs); | |
120 histogram.ExpectUniqueSample(kLoadSecretHistogramName, LOAD_CORRUPT_VALUE, 1); | |
121 EXPECT_EQ(HmacByteVectorGenerator::kEntropyInputSize, secret2.size()); | |
122 // The stored preference should also be updated. | |
123 ExpectConsistentSecret(test_prefs, secret2); | |
124 } | |
125 | |
126 TEST(RapporPrefsTest, DecodableCorruptSecret) { | |
127 base::HistogramTester histogram; | |
128 TestingPrefServiceSimple test_prefs; | |
129 RegisterPrefs(test_prefs.registry()); | |
130 // Store an invalid secret in the preferences that will decode as base64. | |
131 std::string secret1 = "!!INVALID!!"; | |
132 StoreSecret(&test_prefs, secret1); | |
133 // We should have rerolled a new secret. | |
134 std::string secret2 = LoadSecret(&test_prefs); | |
135 histogram.ExpectUniqueSample(kLoadSecretHistogramName, LOAD_CORRUPT_VALUE, 1); | |
136 EXPECT_NE(secret1, secret2); | |
137 EXPECT_EQ(HmacByteVectorGenerator::kEntropyInputSize, secret2.size()); | |
138 // The stored preference should also be updated. | |
139 ExpectConsistentSecret(test_prefs, secret2); | |
140 } | |
141 | |
142 } // namespace internal | |
143 | |
144 } // namespace rappor | |
OLD | NEW |