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

Side by Side Diff: components/user_prefs/tracked/pref_hash_store_impl_unittest.cc

Issue 2304573002: Add ComputeMac and ComputeSplitMacs methods to pref_hash_store. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove stray line break Created 4 years, 3 months 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/user_prefs/tracked/pref_hash_store_impl.h" 5 #include "components/user_prefs/tracked/pref_hash_store_impl.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/values.h" 10 #include "base/values.h"
11 #include "components/user_prefs/tracked/dictionary_hash_store_contents.h" 11 #include "components/user_prefs/tracked/dictionary_hash_store_contents.h"
12 #include "components/user_prefs/tracked/hash_store_contents.h" 12 #include "components/user_prefs/tracked/hash_store_contents.h"
13 #include "components/user_prefs/tracked/pref_hash_store_impl.h" 13 #include "components/user_prefs/tracked/pref_hash_store_impl.h"
14 #include "components/user_prefs/tracked/pref_hash_store_transaction.h" 14 #include "components/user_prefs/tracked/pref_hash_store_transaction.h"
15 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
16 16
17 class PrefHashStoreImplTest : public testing::Test { 17 class PrefHashStoreImplTest : public testing::Test {
18 protected: 18 protected:
19 std::unique_ptr<HashStoreContents> CreateHashStoreContents() { 19 std::unique_ptr<HashStoreContents> CreateHashStoreContents() {
20 return std::unique_ptr<HashStoreContents>( 20 return std::unique_ptr<HashStoreContents>(
21 new DictionaryHashStoreContents(&pref_store_contents_)); 21 new DictionaryHashStoreContents(&pref_store_contents_));
22 } 22 }
23 23
24 private: 24 private:
25 base::DictionaryValue pref_store_contents_; 25 base::DictionaryValue pref_store_contents_;
26 }; 26 };
27 27
28 TEST_F(PrefHashStoreImplTest, ComputeMac) {
29 base::StringValue string_1("string1");
30 base::StringValue string_2("string2");
31 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
32
33 std::string computed_mac_1 = pref_hash_store.ComputeMac("path1", &string_1);
34 std::string computed_mac_2 = pref_hash_store.ComputeMac("path1", &string_2);
35 std::string computed_mac_3 = pref_hash_store.ComputeMac("path2", &string_1);
36
37 // Quick sanity checks here, see pref_hash_calculator_unittest.cc for more
38 // complete tests.
gab 2016/09/01 21:21:56 Also EXPECT_EQ(computed_mac_1, pref_hash_store.Co
proberge 2016/09/06 18:43:39 Done.
39 EXPECT_NE(computed_mac_1, computed_mac_2);
40 EXPECT_NE(computed_mac_1, computed_mac_3);
41 EXPECT_EQ(64, computed_mac_1.size());
42 }
43
44 TEST_F(PrefHashStoreImplTest, ComputeSplitMacs) {
45 base::DictionaryValue dict;
46 dict.Set("a", new base::StringValue("string1"));
47 dict.Set("b", new base::StringValue("string2"));
48 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
49
50 std::unique_ptr<base::DictionaryValue> computed_macs =
51 pref_hash_store.ComputeSplitMacs("foo.bar", &dict);
52
53 std::string mac_1;
54 std::string mac_2;
55 ASSERT_TRUE(computed_macs->GetString("a", &mac_1));
56 ASSERT_TRUE(computed_macs->GetString("b", &mac_2));
57
58 EXPECT_EQ(2, computed_macs->size());
59
60 base::StringValue string_1("string1");
61 base::StringValue string_2("string2");
62 EXPECT_EQ(pref_hash_store.ComputeMac("foo.bar.a", &string_1), mac_1);
63 EXPECT_EQ(pref_hash_store.ComputeMac("foo.bar.b", &string_2), mac_2);
64 }
65
28 TEST_F(PrefHashStoreImplTest, AtomicHashStoreAndCheck) { 66 TEST_F(PrefHashStoreImplTest, AtomicHashStoreAndCheck) {
29 base::StringValue string_1("string1"); 67 base::StringValue string_1("string1");
30 base::StringValue string_2("string2"); 68 base::StringValue string_2("string2");
31 69
32 { 70 {
33 // 32 NULL bytes is the seed that was used to generate the legacy hash. 71 // 32 NULL bytes is the seed that was used to generate the legacy hash.
34 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); 72 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
35 std::unique_ptr<PrefHashStoreTransaction> transaction( 73 std::unique_ptr<PrefHashStoreTransaction> transaction(
36 pref_hash_store.BeginTransaction(CreateHashStoreContents())); 74 pref_hash_store.BeginTransaction(CreateHashStoreContents()));
37 75
(...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after
465 // Load a new |pref_hash_store2| in which the hashes dictionary is trusted. 503 // Load a new |pref_hash_store2| in which the hashes dictionary is trusted.
466 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); 504 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true);
467 std::unique_ptr<PrefHashStoreTransaction> transaction( 505 std::unique_ptr<PrefHashStoreTransaction> transaction(
468 pref_hash_store2.BeginTransaction(CreateHashStoreContents())); 506 pref_hash_store2.BeginTransaction(CreateHashStoreContents()));
469 std::vector<std::string> invalid_keys; 507 std::vector<std::string> invalid_keys;
470 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, 508 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
471 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); 509 transaction->CheckSplitValue("path1", &dict, &invalid_keys));
472 EXPECT_TRUE(invalid_keys.empty()); 510 EXPECT_TRUE(invalid_keys.empty());
473 } 511 }
474 } 512 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698