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 33namespace { 34 35using i18n::addressinput::BuildCallback; 36using i18n::addressinput::FakeStorage; 37using i18n::addressinput::scoped_ptr; 38using i18n::addressinput::Storage; 39using i18n::addressinput::ValidatingStorage; 40 41const char kKey[] = "key"; 42const char kValidatedData[] = DATA; 43const char kStaleWrappedData[] = "timestamp=" OLD_TIMESTAMP "\n" 44 "checksum=" CHECKSUM "\n" 45 DATA; 46const char kEmptyData[] = ""; 47 48// Tests for ValidatingStorage object. 49class 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 Storage* const wrapped_storage_; // Owned by |storage_|. 60 ValidatingStorage storage_; 61 bool success_; 62 std::string key_; 63 std::string data_; 64 const scoped_ptr<const ValidatingStorage::Callback> data_ready_; 65 66 private: 67 void OnDataReady(bool success, const std::string& key, std::string* data) { 68 ASSERT_FALSE(success && data == NULL); 69 success_ = success; 70 key_ = key; 71 if (data != NULL) { 72 data_ = *data; 73 delete data; 74 } 75 } 76 77 DISALLOW_COPY_AND_ASSIGN(ValidatingStorageTest); 78}; 79 80TEST_F(ValidatingStorageTest, GoodData) { 81 storage_.Put(kKey, new std::string(kValidatedData)); 82 storage_.Get(kKey, *data_ready_); 83 84 EXPECT_TRUE(success_); 85 EXPECT_EQ(kKey, key_); 86 EXPECT_EQ(kValidatedData, data_); 87} 88 89TEST_F(ValidatingStorageTest, EmptyData) { 90 storage_.Put(kKey, new std::string(kEmptyData)); 91 storage_.Get(kKey, *data_ready_); 92 93 EXPECT_TRUE(success_); 94 EXPECT_EQ(kKey, key_); 95 EXPECT_EQ(kEmptyData, data_); 96} 97 98TEST_F(ValidatingStorageTest, MissingKey) { 99 storage_.Get(kKey, *data_ready_); 100 101 EXPECT_FALSE(success_); 102 EXPECT_EQ(kKey, key_); 103 EXPECT_TRUE(data_.empty()); 104} 105 106TEST_F(ValidatingStorageTest, GarbageData) { 107 storage_.Put(kKey, new std::string(kValidatedData)); 108 wrapped_storage_->Put(kKey, new std::string("garbage")); 109 storage_.Get(kKey, *data_ready_); 110 111 EXPECT_FALSE(success_); 112 EXPECT_EQ(kKey, key_); 113 EXPECT_TRUE(data_.empty()); 114} 115 116TEST_F(ValidatingStorageTest, StaleData) { 117 storage_.Put(kKey, new std::string(kValidatedData)); 118 wrapped_storage_->Put(kKey, new std::string(kStaleWrappedData)); 119 storage_.Get(kKey, *data_ready_); 120 121 EXPECT_FALSE(success_); 122 EXPECT_EQ(kKey, key_); 123 EXPECT_EQ(kValidatedData, data_); 124} 125 126} // namespace 127