15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/invalidation/invalidator_storage.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string_util.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/pref_names.h"
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/test/base/testing_pref_service_syncable.h"
11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/invalidation/unacked_invalidation_set_test_util.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
14a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)namespace invalidation {
15a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class InvalidatorStorageTest : public testing::Test {
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
18f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  InvalidatorStorageTest() {}
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void SetUp() {
217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    InvalidatorStorage::RegisterProfilePrefs(pref_service_.registry());
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TestingPrefServiceSyncable pref_service_;
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Clearing the storage should erase all version map entries, bootstrap data,
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// and the client ID.
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(InvalidatorStorageTest, Clear) {
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  InvalidatorStorage storage(&pref_service_);
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(storage.GetBootstrapData().empty());
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(storage.GetInvalidatorClientId().empty());
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
350529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  storage.ClearAndSetNewClientId("fake_id");
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ("fake_id", storage.GetInvalidatorClientId());
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  storage.SetBootstrapData("test");
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ("test", storage.GetBootstrapData());
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  storage.Clear();
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(storage.GetBootstrapData().empty());
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(storage.GetInvalidatorClientId().empty());
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST_F(InvalidatorStorageTest, SetGetNotifierClientId) {
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  InvalidatorStorage storage(&pref_service_);
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const std::string client_id("fK6eDzAIuKqx9A4+93bljg==");
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  storage.ClearAndSetNewClientId(client_id);
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_EQ(client_id, storage.GetInvalidatorClientId());
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(InvalidatorStorageTest, SetGetBootstrapData) {
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  InvalidatorStorage storage(&pref_service_);
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string mess("n\0tK\0\0l\344", 8);
58010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  ASSERT_FALSE(base::IsStringUTF8(mess));
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  storage.SetBootstrapData(mess);
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(mess, storage.GetBootstrapData());
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
64f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)TEST_F(InvalidatorStorageTest, SaveGetInvalidations_Empty) {
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  InvalidatorStorage storage(&pref_service_);
66f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  syncer::UnackedInvalidationsMap empty_map;
67f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ASSERT_TRUE(empty_map.empty());
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
69f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  storage.SetSavedInvalidations(empty_map);
70f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  syncer::UnackedInvalidationsMap restored_map =
71f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      storage.GetSavedInvalidations();
72f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_TRUE(restored_map.empty());
73f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
75f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)TEST_F(InvalidatorStorageTest, SaveGetInvalidations) {
76f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  InvalidatorStorage storage(&pref_service_);
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
78f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ObjectId id1(10, "object1");
79f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  syncer::UnackedInvalidationSet storage1(id1);
80f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  syncer::Invalidation unknown_version_inv =
81f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      syncer::Invalidation::InitUnknownVersion(id1);
82f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  syncer::Invalidation known_version_inv =
83f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      syncer::Invalidation::Init(id1, 314, "payload");
84f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  storage1.Add(unknown_version_inv);
85f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  storage1.Add(known_version_inv);
86f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
87f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ObjectId id2(10, "object2");
88f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  syncer::UnackedInvalidationSet storage2(id2);
89f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  syncer::Invalidation obj2_inv =
90f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      syncer::Invalidation::Init(id2, 1234, "payl\0ad\xff");
91f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  storage2.Add(obj2_inv);
92f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
93f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  syncer::UnackedInvalidationsMap map;
94f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  map.insert(std::make_pair(storage1.object_id(), storage1));
95f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  map.insert(std::make_pair(storage2.object_id(), storage2));
96f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
97f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  storage.SetSavedInvalidations(map);
98f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  syncer::UnackedInvalidationsMap restored_map =
99f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      storage.GetSavedInvalidations();
100f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
101f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EXPECT_THAT(map, syncer::test_util::Eq(restored_map));
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
104a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}  // namespace invalidation
105