1 // Copyright (C) 2013 Google Inc.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 #include "validating_storage.h"
17 #include <libaddressinput/callback.h>
18 #include <libaddressinput/storage.h>
19 #include <libaddressinput/util/basictypes.h>
20 #include <libaddressinput/util/scoped_ptr.h>
25 #include <gtest/gtest.h>
27 #include "fake_storage.h"
29 #define CHECKSUM "dd63dafcbd4d5b28badfcaf86fb6fcdb"
30 #define DATA "{'foo': 'bar'}"
31 #define OLD_TIMESTAMP "0"
35 using i18n::addressinput::BuildCallback;
36 using i18n::addressinput::FakeStorage;
37 using i18n::addressinput::scoped_ptr;
38 using i18n::addressinput::Storage;
39 using i18n::addressinput::ValidatingStorage;
41 const char kKey[] = "key";
42 const char kValidatedData[] = DATA;
43 const char kStaleWrappedData[] = "timestamp=" OLD_TIMESTAMP "\n"
44 "checksum=" CHECKSUM "\n"
46 const char kEmptyData[] = "";
48 // Tests for ValidatingStorage object.
49 class ValidatingStorageTest : public testing::Test {
51 ValidatingStorageTest()
52 : wrapped_storage_(new FakeStorage),
53 storage_(wrapped_storage_),
57 data_ready_(BuildCallback(this, &ValidatingStorageTest::OnDataReady)) {}
59 virtual ~ValidatingStorageTest() {}
61 Storage* const wrapped_storage_; // Owned by |storage_|.
62 ValidatingStorage storage_;
66 const scoped_ptr<const ValidatingStorage::Callback> data_ready_;
69 void OnDataReady(bool success, const std::string& key, std::string* data) {
70 ASSERT_FALSE(success && data == NULL);
79 DISALLOW_COPY_AND_ASSIGN(ValidatingStorageTest);
82 TEST_F(ValidatingStorageTest, GoodData) {
83 storage_.Put(kKey, new std::string(kValidatedData));
84 storage_.Get(kKey, *data_ready_);
86 EXPECT_TRUE(success_);
87 EXPECT_EQ(kKey, key_);
88 EXPECT_EQ(kValidatedData, data_);
91 TEST_F(ValidatingStorageTest, EmptyData) {
92 storage_.Put(kKey, new std::string(kEmptyData));
93 storage_.Get(kKey, *data_ready_);
95 EXPECT_TRUE(success_);
96 EXPECT_EQ(kKey, key_);
97 EXPECT_EQ(kEmptyData, data_);
100 TEST_F(ValidatingStorageTest, MissingKey) {
101 storage_.Get(kKey, *data_ready_);
103 EXPECT_FALSE(success_);
104 EXPECT_EQ(kKey, key_);
105 EXPECT_TRUE(data_.empty());
108 TEST_F(ValidatingStorageTest, GarbageData) {
109 storage_.Put(kKey, new std::string(kValidatedData));
110 wrapped_storage_->Put(kKey, new std::string("garbage"));
111 storage_.Get(kKey, *data_ready_);
113 EXPECT_FALSE(success_);
114 EXPECT_EQ(kKey, key_);
115 EXPECT_TRUE(data_.empty());
118 TEST_F(ValidatingStorageTest, StaleData) {
119 storage_.Put(kKey, new std::string(kValidatedData));
120 wrapped_storage_->Put(kKey, new std::string(kStaleWrappedData));
121 storage_.Get(kKey, *data_ready_);
123 EXPECT_FALSE(success_);
124 EXPECT_EQ(kKey, key_);
125 EXPECT_EQ(kValidatedData, data_);