Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / libaddressinput / src / cpp / test / validating_storage_test.cc
1 // Copyright (C) 2013 Google Inc.
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 #include "validating_storage.h"
16
17 #include <libaddressinput/callback.h>
18 #include <libaddressinput/storage.h>
19 #include <libaddressinput/util/basictypes.h>
20 #include <libaddressinput/util/scoped_ptr.h>
21
22 #include <cstddef>
23 #include <string>
24
25 #include <gtest/gtest.h>
26
27 #include "fake_storage.h"
28
29 #define CHECKSUM "dd63dafcbd4d5b28badfcaf86fb6fcdb"
30 #define DATA "{'foo': 'bar'}"
31 #define OLD_TIMESTAMP "0"
32
33 namespace {
34
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;
40
41 const char kKey[] = "key";
42 const char kValidatedData[] = DATA;
43 const char kStaleWrappedData[] = "timestamp=" OLD_TIMESTAMP "\n"
44                                  "checksum=" CHECKSUM "\n"
45                                  DATA;
46 const char kEmptyData[] = "";
47
48 // Tests for ValidatingStorage object.
49 class ValidatingStorageTest : public testing::Test {
50  protected:
51   ValidatingStorageTest()
52       : wrapped_storage_(new FakeStorage),
53         storage_(wrapped_storage_),
54         success_(false),
55         key_(),
56         data_(),
57         data_ready_(BuildCallback(this, &ValidatingStorageTest::OnDataReady)) {}
58
59   virtual ~ValidatingStorageTest() {}
60
61   Storage* const wrapped_storage_;  // Owned by |storage_|.
62   ValidatingStorage storage_;
63   bool success_;
64   std::string key_;
65   std::string data_;
66   const scoped_ptr<const ValidatingStorage::Callback> data_ready_;
67
68  private:
69   void OnDataReady(bool success, const std::string& key, std::string* data) {
70     ASSERT_FALSE(success && data == NULL);
71     success_ = success;
72     key_ = key;
73     if (data != NULL) {
74       data_ = *data;
75       delete data;
76     }
77   }
78
79   DISALLOW_COPY_AND_ASSIGN(ValidatingStorageTest);
80 };
81
82 TEST_F(ValidatingStorageTest, GoodData) {
83   storage_.Put(kKey, new std::string(kValidatedData));
84   storage_.Get(kKey, *data_ready_);
85
86   EXPECT_TRUE(success_);
87   EXPECT_EQ(kKey, key_);
88   EXPECT_EQ(kValidatedData, data_);
89 }
90
91 TEST_F(ValidatingStorageTest, EmptyData) {
92   storage_.Put(kKey, new std::string(kEmptyData));
93   storage_.Get(kKey, *data_ready_);
94
95   EXPECT_TRUE(success_);
96   EXPECT_EQ(kKey, key_);
97   EXPECT_EQ(kEmptyData, data_);
98 }
99
100 TEST_F(ValidatingStorageTest, MissingKey) {
101   storage_.Get(kKey, *data_ready_);
102
103   EXPECT_FALSE(success_);
104   EXPECT_EQ(kKey, key_);
105   EXPECT_TRUE(data_.empty());
106 }
107
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_);
112
113   EXPECT_FALSE(success_);
114   EXPECT_EQ(kKey, key_);
115   EXPECT_TRUE(data_.empty());
116 }
117
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_);
122
123   EXPECT_FALSE(success_);
124   EXPECT_EQ(kKey, key_);
125   EXPECT_EQ(kValidatedData, data_);
126 }
127
128 }  // namespace