15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/prefs/pref_hash_calculator.h"
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <string>
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/strings/string_util.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/values.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST(PrefHashCalculatorTest, TestCurrentAlgorithm) {
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::StringValue string_value_1("string value 1");
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::StringValue string_value_2("string value 2");
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::DictionaryValue dictionary_value_1;
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  dictionary_value_1.SetInteger("int value", 1);
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  dictionary_value_1.Set("nested empty map", new base::DictionaryValue);
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::DictionaryValue dictionary_value_1_equivalent;
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  dictionary_value_1_equivalent.SetInteger("int value", 1);
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::DictionaryValue dictionary_value_2;
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  dictionary_value_2.SetInteger("int value", 2);
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PrefHashCalculator calc1("seed1", "deviceid");
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PrefHashCalculator calc1_dup("seed1", "deviceid");
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PrefHashCalculator calc2("seed2", "deviceid");
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PrefHashCalculator calc3("seed1", "deviceid2");
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Two calculators with same seed produce same hash.
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(calc1.Calculate("pref_path", &string_value_1),
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            calc1_dup.Calculate("pref_path", &string_value_1));
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(PrefHashCalculator::VALID,
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            calc1_dup.Validate(
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "pref_path",
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                &string_value_1,
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                calc1.Calculate("pref_path", &string_value_1)));
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Different seeds, different hashes.
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_NE(calc1.Calculate("pref_path", &string_value_1),
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            calc2.Calculate("pref_path", &string_value_1));
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(PrefHashCalculator::INVALID,
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            calc2.Validate(
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "pref_path",
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                &string_value_1,
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                calc1.Calculate("pref_path", &string_value_1)));
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Different device IDs, different hashes.
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_NE(calc1.Calculate("pref_path", &string_value_1),
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            calc3.Calculate("pref_path", &string_value_1));
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Different values, different hashes.
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_NE(calc1.Calculate("pref_path", &string_value_1),
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            calc1.Calculate("pref_path", &string_value_2));
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Different paths, different hashes.
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_NE(calc1.Calculate("pref_path", &string_value_1),
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            calc1.Calculate("pref_path_2", &string_value_1));
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Works for dictionaries.
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(calc1.Calculate("pref_path", &dictionary_value_1),
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            calc1.Calculate("pref_path", &dictionary_value_1));
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_NE(calc1.Calculate("pref_path", &dictionary_value_1),
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            calc1.Calculate("pref_path", &dictionary_value_2));
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Empty dictionary children are pruned.
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(calc1.Calculate("pref_path", &dictionary_value_1),
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            calc1.Calculate("pref_path", &dictionary_value_1_equivalent));
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // NULL value is supported.
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_FALSE(calc1.Calculate("pref_path", NULL).empty());
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Tests the output against a known value to catch unexpected algorithm changes.
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// The test hashes below must NEVER be updated, the serialization algorithm used
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// must always be able to generate data that will produce these exact hashes.
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST(PrefHashCalculatorTest, CatchHashChanges) {
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static const char kSeed[] = "0123456789ABCDEF0123456789ABCDEF";
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static const char kDeviceId[] = "test_device_id1";
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_ptr<base::Value> null_value(base::Value::CreateNullValue());
82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  scoped_ptr<base::Value> bool_value(new base::FundamentalValue(false));
835f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  scoped_ptr<base::Value> int_value(new base::FundamentalValue(1234567890));
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_ptr<base::Value> double_value(
85116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      new base::FundamentalValue(123.0987654321));
865f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  scoped_ptr<base::Value> string_value(
875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      new base::StringValue("testing with special chars:\n<>{}:^^@#$\\/"));
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // For legacy reasons, we have to support pruning of empty lists/dictionaries
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // and nested empty ists/dicts in the hash generation algorithm.
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_ptr<base::DictionaryValue> nested_empty_dict(
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      new base::DictionaryValue);
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  nested_empty_dict->Set("a", new base::DictionaryValue);
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  nested_empty_dict->Set("b", new base::ListValue);
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_ptr<base::ListValue> nested_empty_list(
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      new base::ListValue);
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  nested_empty_list->Append(new base::DictionaryValue);
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  nested_empty_list->Append(new base::ListValue);
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  nested_empty_list->Append(nested_empty_dict->DeepCopy());
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // A dictionary with an empty dictionary, an empty list, and nested empty
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // dictionaries/lists in it.
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_ptr<base::DictionaryValue> dict_value(new base::DictionaryValue);
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  dict_value->Set("a", new base::StringValue("foo"));
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  dict_value->Set("d", new base::ListValue);
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  dict_value->Set("b", new base::DictionaryValue);
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  dict_value->Set("c", new base::StringValue("baz"));
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  dict_value->Set("e", nested_empty_dict.release());
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  dict_value->Set("f", nested_empty_list.release());
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_ptr<base::ListValue> list_value(new base::ListValue);
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  list_value->AppendBoolean(true);
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  list_value->AppendInteger(100);
1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  list_value->AppendDouble(1.0);
1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(base::Value::TYPE_NULL, null_value->GetType());
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(base::Value::TYPE_BOOLEAN, bool_value->GetType());
1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(base::Value::TYPE_INTEGER, int_value->GetType());
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(base::Value::TYPE_DOUBLE, double_value->GetType());
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(base::Value::TYPE_STRING, string_value->GetType());
1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(base::Value::TYPE_DICTIONARY, dict_value->GetType());
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(base::Value::TYPE_LIST, list_value->GetType());
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Test every value type independently. Intentionally omits TYPE_BINARY which
1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // isn't even allowed in JSONWriter's input.
1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static const char kExpectedNullValue[] =
1275f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      "82A9F3BBC7F9FF84C76B033C854E79EEB162783FA7B3E99FF9372FA8E12C44F7";
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(PrefHashCalculator::VALID,
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            PrefHashCalculator(kSeed, kDeviceId).Validate(
1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "pref.path", null_value.get(), kExpectedNullValue));
1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static const char kExpectedBooleanValue[] =
1335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      "A520D8F43EA307B0063736DC9358C330539D0A29417580514C8B9862632C4CCC";
1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(PrefHashCalculator::VALID,
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            PrefHashCalculator(kSeed, kDeviceId).Validate(
1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "pref.path", bool_value.get(), kExpectedBooleanValue));
1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static const char kExpectedIntegerValue[] =
1395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      "8D60DA1F10BF5AA29819D2D66D7CCEF9AABC5DA93C11A0D2BD21078D63D83682";
1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(PrefHashCalculator::VALID,
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            PrefHashCalculator(kSeed, kDeviceId).Validate(
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "pref.path", int_value.get(), kExpectedIntegerValue));
1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static const char kExpectedDoubleValue[] =
1455f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      "C9D94772516125BEEDAE68C109D44BC529E719EE020614E894CC7FB4098C545D";
1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(PrefHashCalculator::VALID,
1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            PrefHashCalculator(kSeed, kDeviceId).Validate(
1485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "pref.path", double_value.get(), kExpectedDoubleValue));
1495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static const char kExpectedStringValue[] =
1515f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      "05ACCBD3B05C45C36CD06190F63EC577112311929D8380E26E5F13182EB68318";
1525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(PrefHashCalculator::VALID,
1535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            PrefHashCalculator(kSeed, kDeviceId).Validate(
1545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "pref.path", string_value.get(), kExpectedStringValue));
1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static const char kExpectedDictValue[] =
1575f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      "7A84DCC710D796C771F789A4DA82C952095AA956B6F1667EE42D0A19ECAA3C4A";
1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(PrefHashCalculator::VALID,
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            PrefHashCalculator(kSeed, kDeviceId).Validate(
1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "pref.path", dict_value.get(), kExpectedDictValue));
1615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static const char kExpectedListValue[] =
1635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      "8D5A25972DF5AE20D041C780E7CA54E40F614AD53513A0724EE8D62D4F992740";
1645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(PrefHashCalculator::VALID,
1655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            PrefHashCalculator(kSeed, kDeviceId).Validate(
1665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "pref.path", list_value.get(), kExpectedListValue));
1675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Also test every value type together in the same dictionary.
1695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::DictionaryValue everything;
1705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  everything.Set("null", null_value.release());
1715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  everything.Set("bool", bool_value.release());
1725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  everything.Set("int", int_value.release());
1735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  everything.Set("double", double_value.release());
1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  everything.Set("string", string_value.release());
1755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  everything.Set("list", list_value.release());
1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  everything.Set("dict", dict_value.release());
1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static const char kExpectedEverythingValue[] =
1785f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      "B97D09BE7005693574DCBDD03D8D9E44FB51F4008B73FB56A49A9FA671A1999B";
1795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(PrefHashCalculator::VALID,
1805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            PrefHashCalculator(kSeed, kDeviceId).Validate(
1815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                "pref.path", &everything, kExpectedEverythingValue));
1825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1845f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)TEST(PrefHashCalculatorTest, TestCompatibilityWithLegacyPrefMetricsServiceId) {
1855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static const char kSeed[] = {
1865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
1905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
1925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
1945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  };
1955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static const char kDeviceId[] =
1965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "D730D9CBD98C734A4FB097A1922275FE9F7E026A4EA1BE0E84";
1975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static const char kExpectedValue[] =
1985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      "845EF34663FF8D32BE6707F40258FBA531C2BFC532E3B014AFB3476115C2A9DE";
1995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::ListValue startup_urls;
2015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  startup_urls.Set(0, new base::StringValue("http://www.chromium.org/"));
2025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2035f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  EXPECT_EQ(PrefHashCalculator::VALID_SECURE_LEGACY,
2045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            PrefHashCalculator(std::string(kSeed, arraysize(kSeed)), kDeviceId).
2055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            Validate("session.startup_urls", &startup_urls, kExpectedValue));
2065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
207