• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "components/data_reduction_proxy/browser/data_reduction_proxy_params.h"
6 
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "components/data_reduction_proxy/common/data_reduction_proxy_switches.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 
12 namespace {
13 // Test values to replacethe values specified in preprocessor defines.
14 static const char kDefaultKey[] = "test-key";
15 static const char kDefaultDevOrigin[] = "https://dev.net:443/";
16 static const char kDefaultOrigin[] = "https://origin.net:443/";
17 static const char kDefaultFallbackOrigin[] = "http://fallback.net:80/";
18 static const char kDefaultSSLOrigin[] = "http://ssl.net:1080/";
19 static const char kDefaultAltOrigin[] = "https://alt.net:443/";
20 static const char kDefaultAltFallbackOrigin[] = "http://altfallback.net:80/";
21 static const char kDefaultProbeURL[] = "http://probe.net/";
22 
23 static const char kFlagKey[] = "test-flag-key";
24 static const char kFlagOrigin[] = "https://origin.org:443/";
25 static const char kFlagFallbackOrigin[] = "http://fallback.org:80/";
26 static const char kFlagSSLOrigin[] = "http://ssl.org:1080/";
27 static const char kFlagAltOrigin[] = "https://alt.org:443/";
28 static const char kFlagAltFallbackOrigin[] = "http://altfallback.org:80/";
29 static const char kFlagProbeURL[] = "http://probe.org/";
30 
31 // Used to emulate having constants defined by the preprocessor.
32 static const unsigned int HAS_NOTHING = 0x0;
33 static const unsigned int HAS_KEY = 0x1;
34 static const unsigned int HAS_DEV_ORIGIN = 0x2;
35 static const unsigned int HAS_ORIGIN = 0x4;
36 static const unsigned int HAS_FALLBACK_ORIGIN = 0x8;
37 static const unsigned int HAS_SSL_ORIGIN = 0x10;
38 static const unsigned int HAS_ALT_ORIGIN = 0x20;
39 static const unsigned int HAS_ALT_FALLBACK_ORIGIN = 0x40;
40 static const unsigned int HAS_PROBE_URL = 0x80;
41 static const unsigned int HAS_EVERYTHING = 0xff;
42 }  // namespace
43 
44 namespace data_reduction_proxy {
45 namespace {
46 class TestDataReductionProxyParams : public DataReductionProxyParams {
47  public:
48 
TestDataReductionProxyParams(int flags,unsigned int has_definitions)49   TestDataReductionProxyParams(int flags,
50                                unsigned int has_definitions)
51       : DataReductionProxyParams(flags,
52                                  false),
53         has_definitions_(has_definitions) {
54     init_result_ = Init(flags & DataReductionProxyParams::kAllowed,
55                         flags & DataReductionProxyParams::kFallbackAllowed,
56                         flags & DataReductionProxyParams::kAlternativeAllowed);
57   }
58 
init_result() const59   bool init_result() const {
60     return init_result_;
61   }
62 
63  protected:
GetDefaultKey() const64   virtual std::string GetDefaultKey() const OVERRIDE {
65     return GetDefinition(HAS_KEY, kDefaultKey);
66   }
67 
GetDefaultDevOrigin() const68   virtual std::string GetDefaultDevOrigin() const OVERRIDE {
69     return GetDefinition(HAS_DEV_ORIGIN, kDefaultDevOrigin);
70   }
71 
GetDefaultOrigin() const72   virtual std::string GetDefaultOrigin() const OVERRIDE {
73     return GetDefinition(HAS_ORIGIN, kDefaultOrigin);
74   }
75 
GetDefaultFallbackOrigin() const76   virtual std::string GetDefaultFallbackOrigin() const OVERRIDE {
77     return GetDefinition(HAS_FALLBACK_ORIGIN, kDefaultFallbackOrigin);
78   }
79 
GetDefaultSSLOrigin() const80   virtual std::string GetDefaultSSLOrigin() const OVERRIDE {
81     return GetDefinition(HAS_SSL_ORIGIN, kDefaultSSLOrigin);
82   }
83 
GetDefaultAltOrigin() const84   virtual std::string GetDefaultAltOrigin() const OVERRIDE {
85     return GetDefinition(HAS_ALT_ORIGIN, kDefaultAltOrigin);
86   }
87 
GetDefaultAltFallbackOrigin() const88   virtual std::string GetDefaultAltFallbackOrigin() const OVERRIDE {
89     return GetDefinition(HAS_ALT_FALLBACK_ORIGIN, kDefaultAltFallbackOrigin);
90   }
91 
GetDefaultProbeURL() const92   virtual std::string GetDefaultProbeURL() const OVERRIDE {
93     return GetDefinition(HAS_PROBE_URL, kDefaultProbeURL);
94   }
95 
96  private:
GetDefinition(unsigned int has_def,const std::string & definition) const97   std::string GetDefinition(unsigned int has_def,
98                             const std::string& definition) const {
99     return ((has_definitions_ & has_def) ? definition : std::string());
100   }
101 
102   unsigned int has_definitions_;
103   bool init_result_;
104 };
105 }  // namespace
106 
107 class DataReductionProxyParamsTest : public testing::Test {
108  public:
CheckParams(const TestDataReductionProxyParams & params,bool expected_init_result,bool expected_allowed,bool expected_fallback_allowed,bool expected_alternative_allowed,bool expected_promo_allowed)109   void CheckParams(const TestDataReductionProxyParams& params,
110                    bool expected_init_result,
111                    bool expected_allowed,
112                    bool expected_fallback_allowed,
113                    bool expected_alternative_allowed,
114                    bool expected_promo_allowed) {
115     EXPECT_EQ(expected_init_result, params.init_result());
116     EXPECT_EQ(expected_allowed, params.allowed());
117     EXPECT_EQ(expected_fallback_allowed, params.fallback_allowed());
118     EXPECT_EQ(expected_alternative_allowed, params.alternative_allowed());
119     EXPECT_EQ(expected_promo_allowed, params.promo_allowed());
120   }
CheckValues(const TestDataReductionProxyParams & params,const std::string expected_key,const std::string & expected_origin,const std::string & expected_fallback_origin,const std::string & expected_ssl_origin,const std::string & expected_alt_origin,const std::string & expected_alt_fallback_origin,const std::string & expected_probe_url)121   void CheckValues(const TestDataReductionProxyParams& params,
122                    const std::string expected_key,
123                    const std::string& expected_origin,
124                    const std::string& expected_fallback_origin,
125                    const std::string& expected_ssl_origin,
126                    const std::string& expected_alt_origin,
127                    const std::string& expected_alt_fallback_origin,
128                    const std::string& expected_probe_url) {
129     EXPECT_EQ(expected_key, params.key());
130     EXPECT_EQ(GURL(expected_origin), params.origin());
131     EXPECT_EQ(GURL(expected_fallback_origin), params.fallback_origin());
132     EXPECT_EQ(GURL(expected_ssl_origin), params.ssl_origin());
133     EXPECT_EQ(GURL(expected_alt_origin), params.alt_origin());
134     EXPECT_EQ(GURL(expected_alt_fallback_origin), params.alt_fallback_origin());
135     EXPECT_EQ(GURL(expected_probe_url), params.probe_url());
136   }
137 };
138 
TEST_F(DataReductionProxyParamsTest,EverythingDefined)139 TEST_F(DataReductionProxyParamsTest, EverythingDefined) {
140   TestDataReductionProxyParams params(
141       DataReductionProxyParams::kAllowed |
142       DataReductionProxyParams::kFallbackAllowed |
143       DataReductionProxyParams::kPromoAllowed, HAS_EVERYTHING);
144   CheckParams(params, true, true, true, false, true);
145   CheckValues(params,
146               kDefaultKey,
147               kDefaultDevOrigin,
148               kDefaultFallbackOrigin,
149               kDefaultSSLOrigin,
150               kDefaultAltOrigin,
151               kDefaultAltFallbackOrigin,
152               kDefaultProbeURL);
153 }
154 
TEST_F(DataReductionProxyParamsTest,NoDevOrigin)155 TEST_F(DataReductionProxyParamsTest, NoDevOrigin) {
156   TestDataReductionProxyParams params(
157       DataReductionProxyParams::kAllowed |
158       DataReductionProxyParams::kFallbackAllowed |
159       DataReductionProxyParams::kPromoAllowed,
160       HAS_EVERYTHING & ~HAS_DEV_ORIGIN);
161   CheckParams(params, true, true, true, false, true);
162   CheckValues(params,
163               kDefaultKey,
164               kDefaultOrigin,
165               kDefaultFallbackOrigin,
166               kDefaultSSLOrigin,
167               kDefaultAltOrigin,
168               kDefaultAltFallbackOrigin,
169               kDefaultProbeURL);
170 }
171 
TEST_F(DataReductionProxyParamsTest,Flags)172 TEST_F(DataReductionProxyParamsTest, Flags) {
173   CommandLine::ForCurrentProcess()->AppendSwitchASCII(
174       switches::kDataReductionProxyKey, kFlagKey);
175   CommandLine::ForCurrentProcess()->AppendSwitchASCII(
176       switches::kDataReductionProxy, kFlagOrigin);
177   CommandLine::ForCurrentProcess()->AppendSwitchASCII(
178       switches::kDataReductionProxyFallback, kFlagFallbackOrigin);
179   CommandLine::ForCurrentProcess()->AppendSwitchASCII(
180       switches::kDataReductionSSLProxy, kFlagSSLOrigin);
181   CommandLine::ForCurrentProcess()->AppendSwitchASCII(
182       switches::kDataReductionProxyAlt, kFlagAltOrigin);
183   CommandLine::ForCurrentProcess()->AppendSwitchASCII(
184       switches::kDataReductionProxyAltFallback, kFlagAltFallbackOrigin);
185   CommandLine::ForCurrentProcess()->AppendSwitchASCII(
186       switches::kDataReductionProxyProbeURL, kFlagProbeURL);
187   TestDataReductionProxyParams params(
188       DataReductionProxyParams::kAllowed |
189       DataReductionProxyParams::kFallbackAllowed |
190       DataReductionProxyParams::kAlternativeAllowed |
191       DataReductionProxyParams::kPromoAllowed, HAS_EVERYTHING);
192   CheckParams(params, true, true, true, true, true);
193   CheckValues(params,
194               kFlagKey,
195               kFlagOrigin,
196               kFlagFallbackOrigin,
197               kFlagSSLOrigin,
198               kFlagAltOrigin,
199               kFlagAltFallbackOrigin,
200               kFlagProbeURL);
201 }
202 
TEST_F(DataReductionProxyParamsTest,InvalidConfigurations)203 TEST_F(DataReductionProxyParamsTest, InvalidConfigurations) {
204   const struct {
205     bool allowed;
206     bool fallback_allowed;
207     bool alternative_allowed;
208     bool promo_allowed;
209     unsigned int missing_definitions;
210     bool expected_result;
211   } tests[]  = {
212     { true, true, true, true, HAS_NOTHING, true },
213     { true, true, true, true, HAS_KEY, true },
214     { true, true, true, true, HAS_DEV_ORIGIN, true },
215     { true, true, true, true, HAS_ORIGIN, true },
216     { true, true, true, true, HAS_ORIGIN | HAS_DEV_ORIGIN, false },
217     { true, true, true, true, HAS_FALLBACK_ORIGIN, false },
218     { true, true, true, true, HAS_SSL_ORIGIN, false },
219     { true, true, true, true, HAS_ALT_ORIGIN, false },
220     { true, true, true, true, HAS_ALT_FALLBACK_ORIGIN, false },
221     { true, true, true, true, HAS_PROBE_URL, false },
222 
223     { true, false, true, true, HAS_NOTHING, true },
224     { true, false, true, true, HAS_KEY, true },
225     { true, false, true, true, HAS_ORIGIN | HAS_DEV_ORIGIN, false },
226     { true, false, true, true, HAS_FALLBACK_ORIGIN, true },
227     { true, false, true, true, HAS_SSL_ORIGIN, false },
228     { true, false, true, true, HAS_ALT_ORIGIN, false },
229     { true, false, true, true, HAS_ALT_FALLBACK_ORIGIN, true },
230     { true, false, true, true, HAS_PROBE_URL, false },
231 
232     { true, true, false, true, HAS_NOTHING, true },
233     { true, true, false, true, HAS_KEY, true },
234     { true, true, false, true, HAS_ORIGIN | HAS_DEV_ORIGIN, false },
235     { true, true, false, true, HAS_FALLBACK_ORIGIN, false },
236     { true, true, false, true, HAS_SSL_ORIGIN, true },
237     { true, true, false, true, HAS_ALT_ORIGIN, true },
238     { true, true, false, true, HAS_ALT_FALLBACK_ORIGIN, true },
239     { true, true, false, true, HAS_PROBE_URL, false },
240 
241     { true, false, false, true, HAS_KEY, true },
242     { true, false, false, true, HAS_ORIGIN | HAS_DEV_ORIGIN, false },
243     { true, false, false, true, HAS_FALLBACK_ORIGIN, true },
244     { true, false, false, true, HAS_SSL_ORIGIN, true },
245     { true, false, false, true, HAS_ALT_ORIGIN, true },
246     { true, false, false, true, HAS_ALT_FALLBACK_ORIGIN, true },
247     { true, false, false, true, HAS_PROBE_URL, false },
248 
249     { false, true, true, true, HAS_NOTHING, false },
250     { false, true, true, true, HAS_KEY, false },
251     { false, true, true, true, HAS_ORIGIN | HAS_DEV_ORIGIN, false },
252     { false, true, true, true, HAS_FALLBACK_ORIGIN, false },
253     { false, true, true, true, HAS_SSL_ORIGIN, false },
254     { false, true, true, true, HAS_ALT_ORIGIN, false },
255     { false, true, true, true, HAS_ALT_FALLBACK_ORIGIN, false },
256     { false, true, true, true, HAS_PROBE_URL, false },
257   };
258 
259   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
260     int flags = 0;
261     if (tests[i].allowed)
262       flags |= DataReductionProxyParams::kAllowed;
263     if (tests[i].fallback_allowed)
264       flags |= DataReductionProxyParams::kFallbackAllowed;
265     if (tests[i].alternative_allowed)
266       flags |= DataReductionProxyParams::kAlternativeAllowed;
267     if (tests[i].promo_allowed)
268       flags |= DataReductionProxyParams::kPromoAllowed;
269     TestDataReductionProxyParams params(
270         flags,
271         HAS_EVERYTHING & ~(tests[i].missing_definitions));
272     EXPECT_EQ(tests[i].expected_result, params.init_result());
273   }
274 }
275 
TEST_F(DataReductionProxyParamsTest,IsDataReductionProxy)276 TEST_F(DataReductionProxyParamsTest, IsDataReductionProxy) {
277   const struct {
278     net::HostPortPair host_port_pair;
279     bool fallback_allowed;
280     bool expected_result;
281     net::HostPortPair expected_first;
282     net::HostPortPair expected_second;
283   } tests[]  = {
284       { net::HostPortPair::FromURL(GURL(kDefaultOrigin)),
285         true,
286         true,
287         net::HostPortPair::FromURL(GURL(kDefaultOrigin)),
288         net::HostPortPair::FromURL(GURL(kDefaultFallbackOrigin))
289       },
290       { net::HostPortPair::FromURL(GURL(kDefaultOrigin)),
291         false,
292         true,
293         net::HostPortPair::FromURL(GURL(kDefaultOrigin)),
294         net::HostPortPair::FromURL(GURL())
295       },
296       { net::HostPortPair::FromURL(GURL(kDefaultFallbackOrigin)),
297         true,
298         true,
299         net::HostPortPair::FromURL(GURL(kDefaultFallbackOrigin)),
300         net::HostPortPair::FromURL(GURL())
301       },
302       { net::HostPortPair::FromURL(GURL(kDefaultFallbackOrigin)),
303         false,
304         false,
305         net::HostPortPair::FromURL(GURL()),
306         net::HostPortPair::FromURL(GURL())
307       },
308       { net::HostPortPair::FromURL(GURL(kDefaultAltOrigin)),
309         true,
310         true,
311         net::HostPortPair::FromURL(GURL(kDefaultAltOrigin)),
312         net::HostPortPair::FromURL(GURL(kDefaultAltFallbackOrigin))
313       },
314       { net::HostPortPair::FromURL(GURL(kDefaultAltOrigin)),
315         false,
316         true,
317         net::HostPortPair::FromURL(GURL(kDefaultAltOrigin)),
318         net::HostPortPair::FromURL(GURL())
319       },
320       { net::HostPortPair::FromURL(GURL(kDefaultAltFallbackOrigin)),
321         true,
322         true,
323         net::HostPortPair::FromURL(GURL(kDefaultAltFallbackOrigin)),
324         net::HostPortPair::FromURL(GURL())
325       },
326       { net::HostPortPair::FromURL(GURL(kDefaultAltFallbackOrigin)),
327         false,
328         false,
329         net::HostPortPair::FromURL(GURL()),
330         net::HostPortPair::FromURL(GURL())
331       },
332       { net::HostPortPair::FromURL(GURL(kDefaultSSLOrigin)),
333         true,
334         true,
335         net::HostPortPair::FromURL(GURL(kDefaultSSLOrigin)),
336         net::HostPortPair::FromURL(GURL())
337       },
338   };
339   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
340     int flags = DataReductionProxyParams::kAllowed |
341                 DataReductionProxyParams::kAlternativeAllowed;
342     if (tests[i].fallback_allowed)
343       flags |= DataReductionProxyParams::kFallbackAllowed;
344     TestDataReductionProxyParams params(flags,
345                                         HAS_EVERYTHING & ~HAS_DEV_ORIGIN);
346     std::pair<GURL, GURL> proxy_servers;
347     EXPECT_EQ(tests[i].expected_result,
348               params.IsDataReductionProxy(
349                   tests[i].host_port_pair, &proxy_servers));
350     EXPECT_TRUE(tests[i].expected_first.Equals(
351         net::HostPortPair::FromURL(proxy_servers.first)));
352     EXPECT_TRUE(tests[i].expected_second.Equals(
353         net::HostPortPair::FromURL(proxy_servers.second)));
354   }
355 }
356 
357 }  // namespace data_reduction_proxy
358