Home | History | Annotate | Download | only in spdyproxy
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_configurator.h"
      6 
      7 #include <string>
      8 
      9 #include "base/memory/scoped_ptr.h"
     10 #include "base/prefs/pref_registry_simple.h"
     11 #include "base/prefs/testing_pref_service.h"
     12 #include "base/values.h"
     13 #include "chrome/common/pref_names.h"
     14 #include "testing/gmock/include/gmock/gmock.h"
     15 #include "testing/gtest/include/gtest/gtest.h"
     16 
     17 class DataReductionProxyConfigTest : public testing::Test {
     18  public:
     19   virtual void SetUp() {
     20     PrefRegistrySimple* registry = pref_service_.registry();
     21     registry->RegisterDictionaryPref(prefs::kProxy);
     22     config_.reset(new DataReductionProxyChromeConfigurator(&pref_service_));
     23   }
     24 
     25   void CheckProxyConfig(
     26       const std::string& expected_mode,
     27       const std::string& expected_server,
     28       const std::string& expected_bypass_list) {
     29 
     30     const base::DictionaryValue* dict =
     31         pref_service_.GetDictionary(prefs::kProxy);
     32     std::string mode;
     33     std::string server;
     34     std::string bypass_list;
     35     dict->GetString("mode", &mode);
     36     ASSERT_EQ(expected_mode, mode);
     37     dict->GetString("server", &server);
     38     ASSERT_EQ(expected_server, server);
     39     dict->GetString("bypass_list", &bypass_list);
     40     ASSERT_EQ(expected_bypass_list, bypass_list);
     41   }
     42 
     43   scoped_ptr<DataReductionProxyChromeConfigurator> config_;
     44   TestingPrefServiceSimple pref_service_;
     45 };
     46 
     47 TEST_F(DataReductionProxyConfigTest, TestUnrestricted) {
     48   config_->Enable(false,
     49                   false,
     50                   "https://www.foo.com:443/",
     51                   "http://www.bar.com:80/",
     52                   "");
     53   CheckProxyConfig(
     54       "fixed_servers",
     55       "http=https://www.foo.com:443,http://www.bar.com:80,direct://;",
     56       "");
     57 }
     58 
     59 TEST_F(DataReductionProxyConfigTest, TestUnrestrictedSSL) {
     60   config_->Enable(false,
     61                   false,
     62                   "https://www.foo.com:443/",
     63                   "http://www.bar.com:80/",
     64                   "http://www.ssl.com:80/");
     65   CheckProxyConfig(
     66       "fixed_servers",
     67       "http=https://www.foo.com:443,http://www.bar.com:80,direct://;"
     68       "https=http://www.ssl.com:80,direct://;",
     69       "");
     70 }
     71 
     72 TEST_F(DataReductionProxyConfigTest, TestUnrestrictedWithBypassRule) {
     73   config_->AddHostPatternToBypass("<local>");
     74   config_->AddHostPatternToBypass("*.goo.com");
     75   config_->Enable(false,
     76                   false,
     77                   "https://www.foo.com:443/",
     78                   "http://www.bar.com:80/",
     79                   "");
     80   CheckProxyConfig(
     81       "fixed_servers",
     82       "http=https://www.foo.com:443,http://www.bar.com:80,direct://;",
     83       "<local>, *.goo.com");
     84 }
     85 
     86 TEST_F(DataReductionProxyConfigTest, TestUnrestrictedWithoutFallback) {
     87   config_->Enable(false, false, "https://www.foo.com:443/", "", "");
     88   CheckProxyConfig("fixed_servers",
     89                    "http=https://www.foo.com:443,direct://;",
     90                    "");
     91 }
     92 
     93 TEST_F(DataReductionProxyConfigTest, TestRestricted) {
     94   config_->Enable(true,
     95                   false,
     96                   "https://www.foo.com:443/",
     97                   "http://www.bar.com:80/",
     98                   "");
     99   CheckProxyConfig("fixed_servers",
    100                    "http=http://www.bar.com:80,direct://;",
    101                    "");
    102 }
    103 
    104 TEST_F(DataReductionProxyConfigTest, TestFallbackRestricted) {
    105   config_->Enable(false,
    106                   true,
    107                   "https://www.foo.com:443/",
    108                   "http://www.bar.com:80/",
    109                   "");
    110   CheckProxyConfig("fixed_servers",
    111                    "http=https://www.foo.com:443,direct://;",
    112                    "");
    113 }
    114 
    115 TEST_F(DataReductionProxyConfigTest, TestBothRestricted) {
    116   config_->Enable(true,
    117                   true,
    118                   "https://www.foo.com:443/",
    119                   "http://www.bar.com:80/",
    120                   "");
    121   CheckProxyConfig("system", "", "");
    122 }
    123 
    124 TEST_F(DataReductionProxyConfigTest, TestDisable) {
    125   config_->Disable();
    126   CheckProxyConfig("system", "", "");
    127 }
    128 
    129 
    130 TEST_F(DataReductionProxyConfigTest, TestBypassList) {
    131   config_->AddHostPatternToBypass("http://www.google.com");
    132   config_->AddHostPatternToBypass("fefe:13::abc/33");
    133   config_->AddURLPatternToBypass("foo.org/images/*");
    134   config_->AddURLPatternToBypass("http://foo.com/*");
    135   config_->AddURLPatternToBypass("http://baz.com:22/bar/*");
    136   config_->AddURLPatternToBypass("http://*bat.com/bar/*");
    137 
    138   std::string expected[] = {
    139     "http://www.google.com",
    140     "fefe:13::abc/33",
    141     "foo.org",
    142     "http://foo.com",
    143     "http://baz.com:22",
    144     "http://*bat.com"
    145   };
    146 
    147   ASSERT_EQ(config_->bypass_rules_.size(), 6u);
    148   int i = 0;
    149   for (std::vector<std::string>::iterator it = config_->bypass_rules_.begin();
    150        it != config_->bypass_rules_.end(); ++it) {
    151     EXPECT_EQ(expected[i++], *it);
    152   }
    153 }
    154 
    155