Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(130)

Side by Side Diff: chrome/browser/prefs/pref_hash_calculator_unittest.cc

Issue 90563003: Fix a race condition in preference metric reporting. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Also filter 'empty loads' as in new run or corrupted pref file scenarios. Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/prefs/pref_hash_calculator.cc ('k') | chrome/browser/prefs/pref_hash_filter.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2013 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
5 #include "chrome/browser/prefs/pref_hash_calculator.h"
6
7 #include <string>
8
9 #include "base/values.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 TEST(PrefHashCalculatorTest, TestCurrentAlgorithm) {
13 base::StringValue string_value_1("string value 1");
14 base::StringValue string_value_2("string value 2");
15 base::DictionaryValue dictionary_value_1;
16 dictionary_value_1.SetInteger("int value", 1);
17 dictionary_value_1.Set("nested empty map", new DictionaryValue);
18 base::DictionaryValue dictionary_value_1_equivalent;
19 dictionary_value_1_equivalent.SetInteger("int value", 1);
20 base::DictionaryValue dictionary_value_2;
21 dictionary_value_2.SetInteger("int value", 2);
22
23 PrefHashCalculator calc1("seed1", "deviceid");
24 PrefHashCalculator calc1_dup("seed1", "deviceid");
25 PrefHashCalculator calc2("seed2", "deviceid");
26 PrefHashCalculator calc3("seed1", "deviceid2");
27
28 // Two calculators with same seed produce same hash.
29 ASSERT_EQ(calc1.Calculate("pref_path", &string_value_1),
30 calc1_dup.Calculate("pref_path", &string_value_1));
31 ASSERT_EQ(PrefHashCalculator::VALID,
32 calc1_dup.Validate(
33 "pref_path",
34 &string_value_1,
35 calc1.Calculate("pref_path", &string_value_1)));
36
37 // Different seeds, different hashes.
38 ASSERT_NE(calc1.Calculate("pref_path", &string_value_1),
39 calc2.Calculate("pref_path", &string_value_1));
40 ASSERT_EQ(PrefHashCalculator::INVALID,
41 calc2.Validate(
42 "pref_path",
43 &string_value_1,
44 calc1.Calculate("pref_path", &string_value_1)));
45
46 // Different device IDs, different hashes.
47 ASSERT_NE(calc1.Calculate("pref_path", &string_value_1),
48 calc3.Calculate("pref_path", &string_value_1));
49
50 // Different values, different hashes.
51 ASSERT_NE(calc1.Calculate("pref_path", &string_value_1),
52 calc1.Calculate("pref_path", &string_value_2));
53
54 // Different paths, different hashes.
55 ASSERT_NE(calc1.Calculate("pref_path", &string_value_1),
56 calc1.Calculate("pref_path_2", &string_value_1));
57
58 // Works for dictionaries.
59 ASSERT_EQ(calc1.Calculate("pref_path", &dictionary_value_1),
60 calc1.Calculate("pref_path", &dictionary_value_1));
61 ASSERT_NE(calc1.Calculate("pref_path", &dictionary_value_1),
62 calc1.Calculate("pref_path", &dictionary_value_2));
63
64 // Empty dictionary children are pruned.
65 ASSERT_EQ(calc1.Calculate("pref_path", &dictionary_value_1),
66 calc1.Calculate("pref_path", &dictionary_value_1_equivalent));
67
68 // NULL value is supported.
69 ASSERT_FALSE(calc1.Calculate("pref_path", NULL).empty());
70 }
71
72 // Tests the output against a known value to catch unexpected algorithm changes.
73 TEST(PrefHashCalculatorTest, CatchHashChanges) {
74 const char* kDeviceId = "test_device_id1";
75 {
76 static const char kExpectedValue[] =
77 "5CE37D7EBCBC9BE510F0F5E7C326CA92C1673713C3717839610AEA1A217D8BB8";
78
79 base::ListValue list;
80 list.Set(0, new base::FundamentalValue(true));
81 list.Set(1, new base::FundamentalValue(100));
82 list.Set(2, new base::FundamentalValue(1.0));
83
84 // 32 NULL bytes is the seed that was used to generate the hash in old
85 // tests. Use it again to ensure that we haven't altered the algorithm.
86 EXPECT_EQ(PrefHashCalculator::VALID,
87 PrefHashCalculator(std::string(32u, 0), kDeviceId).Validate(
88 "pref.path2", &list, kExpectedValue));
89 }
90 {
91 static const char kExpectedValue[] =
92 "A50FE7EB31BFBC32B8A27E71730AF15421178A9B5815644ACE174B18966735B9";
93
94 DictionaryValue dict;
95 dict.Set("a", new StringValue("foo"));
96 dict.Set("d", new StringValue("bad"));
97 dict.Set("b", new StringValue("bar"));
98 dict.Set("c", new StringValue("baz"));
99
100 // 32 NULL bytes is the seed that was used to generate the hash in old
101 // tests. Use it again to ensure that we haven't altered the algorithm.
102 EXPECT_EQ(PrefHashCalculator::VALID,
103 PrefHashCalculator(std::string(32u, 0), kDeviceId).Validate(
104 "pref.path1", &dict, kExpectedValue));
105 }
106 }
107
108 TEST(PrefHashCalculatorTest, TestLegacyAlgorithm) {
109 const char* kExpectedValue =
110 "C503FB7C65EEFD5C07185F616A0AA67923C069909933F362022B1F187E73E9A2";
111 const char* kDeviceId = "deviceid";
112
113 DictionaryValue dict;
114 dict.Set("a", new StringValue("foo"));
115 dict.Set("d", new StringValue("bad"));
116 dict.Set("b", new StringValue("bar"));
117 dict.Set("c", new StringValue("baz"));
118
119 // 32 NULL bytes is the seed that was used to generate the legacy hash.
120 EXPECT_EQ(PrefHashCalculator::VALID_LEGACY,
121 PrefHashCalculator(std::string(32u, 0), kDeviceId).Validate(
122 "pref.path1", &dict, kExpectedValue));
123
124 }
OLDNEW
« no previous file with comments | « chrome/browser/prefs/pref_hash_calculator.cc ('k') | chrome/browser/prefs/pref_hash_filter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698