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