| Index: net/filter/sdch_policy_delegate_unittest.cc
|
| diff --git a/net/filter/sdch_policy_delegate_unittest.cc b/net/filter/sdch_policy_delegate_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..434c7a65674742a77f91b7d25326af492d5b1031
|
| --- /dev/null
|
| +++ b/net/filter/sdch_policy_delegate_unittest.cc
|
| @@ -0,0 +1,182 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "net/filter/sdch_policy_delegate.h"
|
| +
|
| +#include "base/memory/ptr_util.h"
|
| +#include "base/strings/string_util.h"
|
| +#include "net/filter/mock_source_stream.h"
|
| +#include "net/filter/sdch_source_stream.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace net {
|
| +
|
| +namespace {
|
| +
|
| +class TestContext : public SdchPolicyDelegate::Context {
|
| + public:
|
| + TestContext()
|
| + : mime_type_("text/plain"),
|
| + url_("https://foobar.com"),
|
| + response_code_(200),
|
| + cached_content_(false) {}
|
| + ~TestContext() override {}
|
| +
|
| + bool GetMimeType(std::string* mime_type) const override {
|
| + *mime_type = mime_type_;
|
| + return !mime_type_.empty();
|
| + }
|
| +
|
| + bool GetURL(GURL* url) const override {
|
| + *url = url_;
|
| + return !url_.is_empty();
|
| + }
|
| +
|
| + bool IsCachedContent() const override { return cached_content_; }
|
| +
|
| + SdchManager* GetSdchManager() const override {
|
| + return const_cast<SdchManager*>(&manager_);
|
| + }
|
| +
|
| + SdchManager::DictionarySet* SdchDictionariesAdvertised() const override {
|
| + return nullptr;
|
| + }
|
| +
|
| + int GetResponseCode() const override { return response_code_; }
|
| +
|
| + const BoundNetLog& GetNetLog() const override { return net_log_; }
|
| +
|
| + void set_mime_type(const std::string& mime_type) { mime_type_ = mime_type; }
|
| +
|
| + void set_url(const GURL& url) { url_ = url; }
|
| +
|
| + void set_response_code(int response_code) { response_code_ = response_code; }
|
| +
|
| + void set_cached_content(bool cached_content) {
|
| + cached_content_ = cached_content;
|
| + }
|
| +
|
| + bool IsDomainBlackListed() {
|
| + return manager_.BlackListDomainCount(url_.host());
|
| + }
|
| +
|
| + private:
|
| + std::string mime_type_;
|
| + GURL url_;
|
| + int response_code_;
|
| + bool cached_content_;
|
| + SdchManager manager_;
|
| + BoundNetLog net_log_;
|
| +};
|
| +
|
| +class MockSdchSourceStream : public SdchSourceStream {
|
| + public:
|
| + MockSdchSourceStream(SdchPolicyDelegate* delegate)
|
| + : SdchSourceStream(base::WrapUnique(new MockSourceStream()), delegate),
|
| + stopped_decoding_(false),
|
| + replaced_output_(false) {}
|
| + ~MockSdchSourceStream() override {}
|
| +
|
| + void StopDecoding() override { stopped_decoding_ = true; }
|
| + void ReplaceOutput(const char* data, size_t size) override {
|
| + replaced_output_ = true;
|
| + output_ = std::string(data, size);
|
| + }
|
| +
|
| + bool stopped_decoding() { return stopped_decoding_; }
|
| + bool replaced_output() { return replaced_output_; }
|
| + std::string output() { return output_; }
|
| +
|
| + bool DidGetMetaRefresh() {
|
| + std::string output = base::ToLowerASCII(output_);
|
| + return replaced_output_ && output.find("refresh") != output.npos;
|
| + }
|
| +
|
| + private:
|
| + bool stopped_decoding_;
|
| + bool replaced_output_;
|
| + std::string output_;
|
| +};
|
| +
|
| +} // namespace
|
| +
|
| +class SdchPolicyDelegateTest : public ::testing::Test {
|
| + public:
|
| + SdchPolicyDelegateTest() {}
|
| + void SetUp() override {
|
| + ::testing::Test::SetUp();
|
| +
|
| + // Ownership of context_ is given to delegate_ but this class retains a
|
| + // pointer to it anyway for testing.
|
| + context_ = new TestContext();
|
| + delegate_.reset(new SdchPolicyDelegate(base::WrapUnique(context_)));
|
| + source_.reset(new MockSdchSourceStream(delegate_.get()));
|
| + }
|
| +
|
| + protected:
|
| + TestContext* context_;
|
| + std::unique_ptr<SdchPolicyDelegate> delegate_;
|
| + std::unique_ptr<MockSdchSourceStream> source_;
|
| +};
|
| +
|
| +TEST_F(SdchPolicyDelegateTest, Http404DictionaryErrorNotDecoded) {
|
| + context_->set_response_code(404);
|
| + EXPECT_TRUE(delegate_->OnDictionaryError(source_.get()));
|
| + EXPECT_TRUE(source_->stopped_decoding());
|
| +}
|
| +
|
| +TEST_F(SdchPolicyDelegateTest, HttpNon404HtmlErrorDictionaryErrorBlacklists) {
|
| + context_->set_mime_type("text/html");
|
| + context_->set_response_code(403);
|
| + EXPECT_TRUE(delegate_->OnDictionaryError(source_.get()));
|
| + EXPECT_TRUE(source_->DidGetMetaRefresh());
|
| +}
|
| +
|
| +TEST_F(SdchPolicyDelegateTest, HttpNon404TextErrorDictionaryErrorFails) {
|
| + context_->set_mime_type("text/plain");
|
| + context_->set_response_code(403);
|
| + EXPECT_FALSE(delegate_->OnDictionaryError(source_.get()));
|
| +}
|
| +
|
| +TEST_F(SdchPolicyDelegateTest, CachedUnencodedDictionaryErrorStopsDecoding) {
|
| + context_->set_cached_content(true);
|
| + EXPECT_TRUE(delegate_->OnDictionaryError(source_.get()));
|
| + EXPECT_TRUE(source_->stopped_decoding());
|
| +}
|
| +
|
| +TEST_F(SdchPolicyDelegateTest, CachedEncodedDictionaryErrorGetsRefresh) {
|
| + context_->set_cached_content(true);
|
| + context_->set_mime_type("text/html");
|
| + EXPECT_FALSE(delegate_->OnGetDictionary("abcdefgh", nullptr));
|
| + EXPECT_TRUE(delegate_->OnDictionaryError(source_.get()));
|
| + EXPECT_TRUE(source_->DidGetMetaRefresh());
|
| +}
|
| +
|
| +TEST_F(SdchPolicyDelegateTest, CachedHtmlDecodingErrorDoesNotBlacklist) {
|
| + context_->set_mime_type("text/html");
|
| + context_->set_cached_content(true);
|
| + EXPECT_FALSE(context_->IsDomainBlackListed());
|
| + EXPECT_TRUE(delegate_->OnDecodingError(source_.get()));
|
| + EXPECT_TRUE(source_->replaced_output());
|
| + EXPECT_FALSE(context_->IsDomainBlackListed());
|
| +}
|
| +
|
| +TEST_F(SdchPolicyDelegateTest, HtmlDecodingErrorGetsMetaRefresh) {
|
| + context_->set_mime_type("text/html");
|
| + EXPECT_FALSE(context_->IsDomainBlackListed());
|
| + EXPECT_TRUE(delegate_->OnDecodingError(source_.get()));
|
| + EXPECT_TRUE(source_->DidGetMetaRefresh());
|
| + EXPECT_TRUE(context_->IsDomainBlackListed());
|
| +}
|
| +
|
| +TEST_F(SdchPolicyDelegateTest, NonHtmlDecodingErrorGetsError) {
|
| + context_->set_mime_type("text/plain");
|
| + EXPECT_FALSE(context_->IsDomainBlackListed());
|
| + EXPECT_FALSE(delegate_->OnDecodingError(source_.get()));
|
| + EXPECT_FALSE(source_->replaced_output());
|
| + EXPECT_FALSE(source_->stopped_decoding());
|
| + EXPECT_TRUE(context_->IsDomainBlackListed());
|
| +}
|
| +
|
| +} // namespace net
|
|
|