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

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: Respond to Gab's comments. 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
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_EQ(calc1.Calculate("pref_path", NULL),
70 calc1.Calculate("pref_path", NULL));
gab 2013/12/06 17:23:38 What's the value of checking the same call twice h
erikwright (departed) 2013/12/09 17:59:40 Done.
71 }
gab 2013/12/06 17:23:38 I would also like to see tests with hardcoded hash
erikwright (departed) 2013/12/09 17:59:40 Done.
72
73 TEST(PrefHashCalculatorTest, TestLegacyAlgorithm) {
74 DictionaryValue dict;
75 dict.Set("a", new StringValue("foo"));
76 dict.Set("d", new StringValue("bad"));
77 dict.Set("b", new StringValue("bar"));
78 dict.Set("c", new StringValue("baz"));
79
80 // 32 NULL bytes is the seed that was used to generate the legacy hash.
gab 2013/12/06 17:23:38 Why 32 NULL bytes?! Won't an empty string do the s
erikwright (departed) 2013/12/06 19:00:47 Like the comment says. That is what the old test u
81 EXPECT_EQ(PrefHashCalculator::VALID_LEGACY,
82 PrefHashCalculator(std::string(32u, 0), "deviceid").Validate(
83 "pref.path1",
84 &dict,
85 "C503FB7C65EEFD5C07185F616A0AA679"
86 "23C069909933F362022B1F187E73E9A2"));
gab 2013/12/06 17:23:38 I would prefer having this all on one line; it was
erikwright (departed) 2013/12/09 17:59:40 Done.
87 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698