1// Copyright (c) 2012 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 "components/invalidation/invalidator_storage.h"
6
7#include "base/prefs/pref_service.h"
8#include "base/strings/string_util.h"
9#include "chrome/common/pref_names.h"
10#include "chrome/test/base/testing_pref_service_syncable.h"
11#include "components/invalidation/unacked_invalidation_set_test_util.h"
12#include "testing/gtest/include/gtest/gtest.h"
13
14namespace invalidation {
15
16class InvalidatorStorageTest : public testing::Test {
17 public:
18  InvalidatorStorageTest() {}
19
20  virtual void SetUp() {
21    InvalidatorStorage::RegisterProfilePrefs(pref_service_.registry());
22  }
23
24 protected:
25  TestingPrefServiceSyncable pref_service_;
26};
27
28// Clearing the storage should erase all version map entries, bootstrap data,
29// and the client ID.
30TEST_F(InvalidatorStorageTest, Clear) {
31  InvalidatorStorage storage(&pref_service_);
32  EXPECT_TRUE(storage.GetBootstrapData().empty());
33  EXPECT_TRUE(storage.GetInvalidatorClientId().empty());
34
35  storage.ClearAndSetNewClientId("fake_id");
36  EXPECT_EQ("fake_id", storage.GetInvalidatorClientId());
37
38  storage.SetBootstrapData("test");
39  EXPECT_EQ("test", storage.GetBootstrapData());
40
41  storage.Clear();
42
43  EXPECT_TRUE(storage.GetBootstrapData().empty());
44  EXPECT_TRUE(storage.GetInvalidatorClientId().empty());
45}
46
47TEST_F(InvalidatorStorageTest, SetGetNotifierClientId) {
48  InvalidatorStorage storage(&pref_service_);
49  const std::string client_id("fK6eDzAIuKqx9A4+93bljg==");
50
51  storage.ClearAndSetNewClientId(client_id);
52  EXPECT_EQ(client_id, storage.GetInvalidatorClientId());
53}
54
55TEST_F(InvalidatorStorageTest, SetGetBootstrapData) {
56  InvalidatorStorage storage(&pref_service_);
57  const std::string mess("n\0tK\0\0l\344", 8);
58  ASSERT_FALSE(base::IsStringUTF8(mess));
59
60  storage.SetBootstrapData(mess);
61  EXPECT_EQ(mess, storage.GetBootstrapData());
62}
63
64TEST_F(InvalidatorStorageTest, SaveGetInvalidations_Empty) {
65  InvalidatorStorage storage(&pref_service_);
66  syncer::UnackedInvalidationsMap empty_map;
67  ASSERT_TRUE(empty_map.empty());
68
69  storage.SetSavedInvalidations(empty_map);
70  syncer::UnackedInvalidationsMap restored_map =
71      storage.GetSavedInvalidations();
72  EXPECT_TRUE(restored_map.empty());
73}
74
75TEST_F(InvalidatorStorageTest, SaveGetInvalidations) {
76  InvalidatorStorage storage(&pref_service_);
77
78  ObjectId id1(10, "object1");
79  syncer::UnackedInvalidationSet storage1(id1);
80  syncer::Invalidation unknown_version_inv =
81      syncer::Invalidation::InitUnknownVersion(id1);
82  syncer::Invalidation known_version_inv =
83      syncer::Invalidation::Init(id1, 314, "payload");
84  storage1.Add(unknown_version_inv);
85  storage1.Add(known_version_inv);
86
87  ObjectId id2(10, "object2");
88  syncer::UnackedInvalidationSet storage2(id2);
89  syncer::Invalidation obj2_inv =
90      syncer::Invalidation::Init(id2, 1234, "payl\0ad\xff");
91  storage2.Add(obj2_inv);
92
93  syncer::UnackedInvalidationsMap map;
94  map.insert(std::make_pair(storage1.object_id(), storage1));
95  map.insert(std::make_pair(storage2.object_id(), storage2));
96
97  storage.SetSavedInvalidations(map);
98  syncer::UnackedInvalidationsMap restored_map =
99      storage.GetSavedInvalidations();
100
101  EXPECT_THAT(map, syncer::test_util::Eq(restored_map));
102}
103
104}  // namespace invalidation
105