| Index: third_party/libaddressinput/chromium/cpp/test/retriever_test.cc
|
| diff --git a/third_party/libaddressinput/chromium/cpp/test/retriever_test.cc b/third_party/libaddressinput/chromium/cpp/test/retriever_test.cc
|
| index 8d8e04039d27c35ed2ffd465a80c0496b1ebae88..2da5ac19a637d06450a2b2182f84ab28cd934881 100644
|
| --- a/third_party/libaddressinput/chromium/cpp/test/retriever_test.cc
|
| +++ b/third_party/libaddressinput/chromium/cpp/test/retriever_test.cc
|
| @@ -16,8 +16,11 @@
|
|
|
| #include <libaddressinput/callback.h>
|
| #include <libaddressinput/downloader.h>
|
| +#include <libaddressinput/storage.h>
|
| #include <libaddressinput/util/scoped_ptr.h>
|
|
|
| +#include <cstddef>
|
| +#include <ctime>
|
| #include <string>
|
|
|
| #include <gtest/gtest.h>
|
| @@ -29,16 +32,34 @@
|
| namespace i18n {
|
| namespace addressinput {
|
|
|
| +namespace {
|
| +
|
| const char kKey[] = "data/CA/AB--fr";
|
|
|
| // Empty data that the downloader can return.
|
| const char kEmptyData[] = "{}";
|
|
|
| +// The MD5 checksum for kEmptyData. Retriever uses MD5 to validate data
|
| +// integrity.
|
| +const char kEmptyDataChecksum[] = "99914b932bd37a50b983c5e7c90ae93b";
|
| +
|
| +// Returns a string serialization of the current time.
|
| +std::string BuildCurrentTimestampString() {
|
| + time_t timestamp = time(NULL);
|
| + char timestamp_string[2 + 3 * sizeof timestamp];
|
| + snprintf(timestamp_string, sizeof timestamp_string, "%ld", timestamp);
|
| + return timestamp_string;
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| // Tests for Retriever object.
|
| class RetrieverTest : public testing::Test {
|
| protected:
|
| RetrieverTest()
|
| - : success_(false),
|
| + : storage_(NULL),
|
| + retriever_(),
|
| + success_(false),
|
| key_(),
|
| data_() {
|
| ResetRetriever(FakeDownloader::kFakeDataUrl);
|
| @@ -52,10 +73,11 @@ class RetrieverTest : public testing::Test {
|
| }
|
|
|
| void ResetRetriever(const std::string& url) {
|
| + storage_ = new FakeStorage;
|
| retriever_.reset(
|
| new Retriever(url,
|
| scoped_ptr<Downloader>(new FakeDownloader),
|
| - scoped_ptr<Storage>(new FakeStorage)));
|
| + scoped_ptr<Storage>(storage_)));
|
| }
|
|
|
| std::string GetUrlForKey(const std::string& key) {
|
| @@ -66,6 +88,7 @@ class RetrieverTest : public testing::Test {
|
| return retriever_->GetKeyForUrl(url);
|
| }
|
|
|
| + Storage* storage_; // Owned by |retriever_|.
|
| scoped_ptr<Retriever> retriever_;
|
| bool success_;
|
| std::string key_;
|
| @@ -147,6 +170,66 @@ TEST_F(RetrieverTest, FaultyDownloaderFallback) {
|
| EXPECT_NE(kEmptyData, data_);
|
| }
|
|
|
| +TEST_F(RetrieverTest, NoChecksumAndTimestampWillRedownload) {
|
| + storage_->Put(kKey, kEmptyData);
|
| + retriever_->Retrieve(kKey, BuildCallback());
|
| + EXPECT_TRUE(success_);
|
| + EXPECT_EQ(kKey, key_);
|
| + EXPECT_FALSE(data_.empty());
|
| + EXPECT_NE(kEmptyData, data_);
|
| +}
|
| +
|
| +TEST_F(RetrieverTest, ChecksumAndTimestampWillNotRedownload) {
|
| + storage_->Put(kKey,
|
| + "timestamp=" + BuildCurrentTimestampString() + "\n" +
|
| + "checksum=" + kEmptyDataChecksum + "\n" +
|
| + kEmptyData);
|
| + retriever_->Retrieve(kKey, BuildCallback());
|
| + EXPECT_TRUE(success_);
|
| + EXPECT_EQ(kKey, key_);
|
| + EXPECT_EQ(kEmptyData, data_);
|
| +}
|
| +
|
| +TEST_F(RetrieverTest, OldTimestampWillRedownload) {
|
| + storage_->Put(kKey,
|
| + std::string("timestamp=0\n") +
|
| + "checksum=" + kEmptyDataChecksum + "\n" +
|
| + kEmptyData);
|
| + retriever_->Retrieve(kKey, BuildCallback());
|
| + EXPECT_TRUE(success_);
|
| + EXPECT_EQ(kKey, key_);
|
| + EXPECT_FALSE(data_.empty());
|
| + EXPECT_NE(kEmptyData, data_);
|
| +}
|
| +
|
| +TEST_F(RetrieverTest, OldTimestampOkIfDownloadFails) {
|
| + storage_ = new FakeStorage;
|
| + Retriever bad_retriever(FakeDownloader::kFakeDataUrl,
|
| + scoped_ptr<Downloader>(new FaultyDownloader),
|
| + scoped_ptr<Storage>(storage_));
|
| + storage_->Put(kKey,
|
| + std::string("timestamp=0\n") +
|
| + "checksum=" + kEmptyDataChecksum + "\n" +
|
| + kEmptyData);
|
| + bad_retriever.Retrieve(kKey, BuildCallback());
|
| + EXPECT_TRUE(success_);
|
| + EXPECT_EQ(kKey, key_);
|
| + EXPECT_EQ(kEmptyData, data_);
|
| +}
|
| +
|
| +TEST_F(RetrieverTest, WrongChecksumWillRedownload) {
|
| + static const char kNonEmptyData[] = "{\"non-empty\": \"data\"}";
|
| + storage_->Put(kKey,
|
| + "timestamp=" + BuildCurrentTimestampString() + "\n" +
|
| + "checksum=" + kEmptyDataChecksum + "\n" +
|
| + kNonEmptyData);
|
| + retriever_->Retrieve(kKey, BuildCallback());
|
| + EXPECT_TRUE(success_);
|
| + EXPECT_EQ(kKey, key_);
|
| + EXPECT_FALSE(data_.empty());
|
| + EXPECT_NE(kNonEmptyData, data_);
|
| +}
|
| +
|
| // The downloader that doesn't get back to you.
|
| class HangingDownloader : public Downloader {
|
| public:
|
|
|