summaryrefslogtreecommitdiffstats
path: root/chrome/browser/invalidation/invalidator_storage_unittest.cc
blob: 841bead435cd6cef76278390484f8d9e0eba003e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/invalidation/invalidator_storage.h"

#include "base/prefs/pref_service.h"
#include "base/strings/string_util.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_pref_service_syncable.h"
#include "sync/notifier/unacked_invalidation_set_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace invalidation {

class InvalidatorStorageTest : public testing::Test {
 public:
  InvalidatorStorageTest() {}

  virtual void SetUp() {
    InvalidatorStorage::RegisterProfilePrefs(pref_service_.registry());
  }

 protected:
  TestingPrefServiceSyncable pref_service_;
};

// Clearing the storage should erase all version map entries, bootstrap data,
// and the client ID.
TEST_F(InvalidatorStorageTest, Clear) {
  InvalidatorStorage storage(&pref_service_);
  EXPECT_TRUE(storage.GetBootstrapData().empty());
  EXPECT_TRUE(storage.GetInvalidatorClientId().empty());

  storage.ClearAndSetNewClientId("fake_id");
  EXPECT_EQ("fake_id", storage.GetInvalidatorClientId());

  storage.SetBootstrapData("test");
  EXPECT_EQ("test", storage.GetBootstrapData());

  storage.Clear();

  EXPECT_TRUE(storage.GetBootstrapData().empty());
  EXPECT_TRUE(storage.GetInvalidatorClientId().empty());
}

TEST_F(InvalidatorStorageTest, SetGetNotifierClientId) {
  InvalidatorStorage storage(&pref_service_);
  const std::string client_id("fK6eDzAIuKqx9A4+93bljg==");

  storage.ClearAndSetNewClientId(client_id);
  EXPECT_EQ(client_id, storage.GetInvalidatorClientId());
}

TEST_F(InvalidatorStorageTest, SetGetBootstrapData) {
  InvalidatorStorage storage(&pref_service_);
  const std::string mess("n\0tK\0\0l\344", 8);
  ASSERT_FALSE(base::IsStringUTF8(mess));

  storage.SetBootstrapData(mess);
  EXPECT_EQ(mess, storage.GetBootstrapData());
}

TEST_F(InvalidatorStorageTest, SaveGetInvalidations_Empty) {
  InvalidatorStorage storage(&pref_service_);
  syncer::UnackedInvalidationsMap empty_map;
  ASSERT_TRUE(empty_map.empty());

  storage.SetSavedInvalidations(empty_map);
  syncer::UnackedInvalidationsMap restored_map =
      storage.GetSavedInvalidations();
  EXPECT_TRUE(restored_map.empty());
}

TEST_F(InvalidatorStorageTest, SaveGetInvalidations) {
  InvalidatorStorage storage(&pref_service_);

  ObjectId id1(10, "object1");
  syncer::UnackedInvalidationSet storage1(id1);
  syncer::Invalidation unknown_version_inv =
      syncer::Invalidation::InitUnknownVersion(id1);
  syncer::Invalidation known_version_inv =
      syncer::Invalidation::Init(id1, 314, "payload");
  storage1.Add(unknown_version_inv);
  storage1.Add(known_version_inv);

  ObjectId id2(10, "object2");
  syncer::UnackedInvalidationSet storage2(id2);
  syncer::Invalidation obj2_inv =
      syncer::Invalidation::Init(id2, 1234, "payl\0ad\xff");
  storage2.Add(obj2_inv);

  syncer::UnackedInvalidationsMap map;
  map.insert(std::make_pair(storage1.object_id(), storage1));
  map.insert(std::make_pair(storage2.object_id(), storage2));

  storage.SetSavedInvalidations(map);
  syncer::UnackedInvalidationsMap restored_map =
      storage.GetSavedInvalidations();

  EXPECT_THAT(map, syncer::test_util::Eq(restored_map));
}

}  // namespace invalidation