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.
5 #include "components/data_reduction_proxy/browser/data_reduction_proxy_params.h"
9 #include "base/command_line.h"
10 #include "base/logging.h"
11 #include "components/data_reduction_proxy/browser/data_reduction_proxy_params_test_utils.h"
12 #include "components/data_reduction_proxy/common/data_reduction_proxy_switches.h"
13 #include "net/proxy/proxy_retry_info.h"
14 #include "net/proxy/proxy_server.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace data_reduction_proxy {
18 class DataReductionProxyParamsTest : public testing::Test {
20 void CheckParams(const TestDataReductionProxyParams& params,
21 bool expected_init_result,
22 bool expected_allowed,
23 bool expected_fallback_allowed,
24 bool expected_alternative_allowed,
25 bool expected_promo_allowed) {
26 EXPECT_EQ(expected_init_result, params.init_result());
27 EXPECT_EQ(expected_allowed, params.allowed());
28 EXPECT_EQ(expected_fallback_allowed, params.fallback_allowed());
29 EXPECT_EQ(expected_alternative_allowed, params.alternative_allowed());
30 EXPECT_EQ(expected_promo_allowed, params.promo_allowed());
32 void CheckValues(const TestDataReductionProxyParams& params,
33 const std::string& expected_origin,
34 const std::string& expected_fallback_origin,
35 const std::string& expected_ssl_origin,
36 const std::string& expected_alt_origin,
37 const std::string& expected_alt_fallback_origin,
38 const std::string& expected_probe_url) {
39 EXPECT_EQ(GURL(expected_origin), params.origin());
40 EXPECT_EQ(GURL(expected_fallback_origin), params.fallback_origin());
41 EXPECT_EQ(GURL(expected_ssl_origin), params.ssl_origin());
42 EXPECT_EQ(GURL(expected_alt_origin), params.alt_origin());
43 EXPECT_EQ(GURL(expected_alt_fallback_origin), params.alt_fallback_origin());
44 EXPECT_EQ(GURL(expected_probe_url), params.probe_url());
48 TEST_F(DataReductionProxyParamsTest, EverythingDefined) {
49 TestDataReductionProxyParams params(
50 DataReductionProxyParams::kAllowed |
51 DataReductionProxyParams::kFallbackAllowed |
52 DataReductionProxyParams::kPromoAllowed,
53 TestDataReductionProxyParams::HAS_EVERYTHING);
54 CheckParams(params, true, true, true, false, true);
56 TestDataReductionProxyParams::DefaultDevOrigin(),
57 TestDataReductionProxyParams::DefaultDevFallbackOrigin(),
58 TestDataReductionProxyParams::DefaultSSLOrigin(),
59 TestDataReductionProxyParams::DefaultAltOrigin(),
60 TestDataReductionProxyParams::DefaultAltFallbackOrigin(),
61 TestDataReductionProxyParams::DefaultProbeURL());
64 TEST_F(DataReductionProxyParamsTest, NoDevOrigin) {
65 TestDataReductionProxyParams params(
66 DataReductionProxyParams::kAllowed |
67 DataReductionProxyParams::kFallbackAllowed |
68 DataReductionProxyParams::kPromoAllowed,
69 TestDataReductionProxyParams::HAS_EVERYTHING &
70 ~TestDataReductionProxyParams::HAS_DEV_ORIGIN &
71 ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN);
72 CheckParams(params, true, true, true, false, true);
74 TestDataReductionProxyParams::DefaultOrigin(),
75 TestDataReductionProxyParams::DefaultFallbackOrigin(),
76 TestDataReductionProxyParams::DefaultSSLOrigin(),
77 TestDataReductionProxyParams::DefaultAltOrigin(),
78 TestDataReductionProxyParams::DefaultAltFallbackOrigin(),
79 TestDataReductionProxyParams::DefaultProbeURL());
82 TEST_F(DataReductionProxyParamsTest, Flags) {
83 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
84 switches::kDataReductionProxy,
85 TestDataReductionProxyParams::FlagOrigin());
86 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
87 switches::kDataReductionProxyFallback,
88 TestDataReductionProxyParams::FlagFallbackOrigin());
89 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
90 switches::kDataReductionSSLProxy,
91 TestDataReductionProxyParams::FlagSSLOrigin());
92 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
93 switches::kDataReductionProxyAlt,
94 TestDataReductionProxyParams::FlagAltOrigin());
95 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
96 switches::kDataReductionProxyAltFallback,
97 TestDataReductionProxyParams::FlagAltFallbackOrigin());
98 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
99 switches::kDataReductionProxyProbeURL,
100 TestDataReductionProxyParams::FlagProbeURL());
101 TestDataReductionProxyParams params(
102 DataReductionProxyParams::kAllowed |
103 DataReductionProxyParams::kFallbackAllowed |
104 DataReductionProxyParams::kAlternativeAllowed |
105 DataReductionProxyParams::kPromoAllowed,
106 TestDataReductionProxyParams::HAS_EVERYTHING);
107 CheckParams(params, true, true, true, true, true);
109 TestDataReductionProxyParams::FlagOrigin(),
110 TestDataReductionProxyParams::FlagFallbackOrigin(),
111 TestDataReductionProxyParams::FlagSSLOrigin(),
112 TestDataReductionProxyParams::FlagAltOrigin(),
113 TestDataReductionProxyParams::FlagAltFallbackOrigin(),
114 TestDataReductionProxyParams::FlagProbeURL());
117 TEST_F(DataReductionProxyParamsTest, InvalidConfigurations) {
120 bool fallback_allowed;
121 bool alternative_allowed;
122 bool alternative_fallback_allowed;
124 unsigned int missing_definitions;
125 bool expected_result;
133 TestDataReductionProxyParams::HAS_NOTHING,
142 TestDataReductionProxyParams::HAS_DEV_ORIGIN |
143 TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN,
152 TestDataReductionProxyParams::HAS_ORIGIN,
161 TestDataReductionProxyParams::HAS_ORIGIN |
162 TestDataReductionProxyParams::HAS_DEV_ORIGIN |
163 TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN,
172 TestDataReductionProxyParams::HAS_FALLBACK_ORIGIN |
173 TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN,
182 TestDataReductionProxyParams::HAS_SSL_ORIGIN,
191 TestDataReductionProxyParams::HAS_ALT_ORIGIN,
200 TestDataReductionProxyParams::HAS_ALT_FALLBACK_ORIGIN,
209 TestDataReductionProxyParams::HAS_ALT_FALLBACK_ORIGIN,
218 TestDataReductionProxyParams::HAS_PROBE_URL,
227 TestDataReductionProxyParams::HAS_NOTHING,
236 TestDataReductionProxyParams::HAS_ORIGIN |
237 TestDataReductionProxyParams::HAS_DEV_ORIGIN |
238 TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN,
247 TestDataReductionProxyParams::HAS_FALLBACK_ORIGIN,
256 TestDataReductionProxyParams::HAS_SSL_ORIGIN,
265 TestDataReductionProxyParams::HAS_ALT_ORIGIN,
274 TestDataReductionProxyParams::HAS_ALT_FALLBACK_ORIGIN,
283 TestDataReductionProxyParams::HAS_ALT_FALLBACK_ORIGIN,
292 TestDataReductionProxyParams::HAS_PROBE_URL,
301 TestDataReductionProxyParams::HAS_NOTHING,
310 TestDataReductionProxyParams::HAS_ORIGIN |
311 TestDataReductionProxyParams::HAS_DEV_ORIGIN |
312 TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN,
321 TestDataReductionProxyParams::HAS_FALLBACK_ORIGIN |
322 TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN,
331 TestDataReductionProxyParams::HAS_SSL_ORIGIN,
340 TestDataReductionProxyParams::HAS_ALT_ORIGIN,
349 TestDataReductionProxyParams::HAS_ALT_FALLBACK_ORIGIN,
358 TestDataReductionProxyParams::HAS_ALT_FALLBACK_ORIGIN,
367 TestDataReductionProxyParams::HAS_PROBE_URL,
376 TestDataReductionProxyParams::HAS_ORIGIN |
377 TestDataReductionProxyParams::HAS_DEV_ORIGIN |
378 TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN,
387 TestDataReductionProxyParams::HAS_FALLBACK_ORIGIN,
396 TestDataReductionProxyParams::HAS_SSL_ORIGIN,
405 TestDataReductionProxyParams::HAS_ALT_ORIGIN,
414 TestDataReductionProxyParams::HAS_ALT_FALLBACK_ORIGIN,
423 TestDataReductionProxyParams::HAS_ALT_FALLBACK_ORIGIN,
432 TestDataReductionProxyParams::HAS_PROBE_URL,
441 TestDataReductionProxyParams::HAS_NOTHING,
450 TestDataReductionProxyParams::HAS_ORIGIN |
451 TestDataReductionProxyParams::HAS_DEV_ORIGIN |
452 TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN,
461 TestDataReductionProxyParams::HAS_FALLBACK_ORIGIN,
470 TestDataReductionProxyParams::HAS_SSL_ORIGIN,
479 TestDataReductionProxyParams::HAS_ALT_ORIGIN,
488 TestDataReductionProxyParams::HAS_ALT_FALLBACK_ORIGIN,
497 TestDataReductionProxyParams::HAS_ALT_FALLBACK_ORIGIN,
506 TestDataReductionProxyParams::HAS_PROBE_URL,
511 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
513 if (tests[i].allowed)
514 flags |= DataReductionProxyParams::kAllowed;
515 if (tests[i].fallback_allowed)
516 flags |= DataReductionProxyParams::kFallbackAllowed;
517 if (tests[i].alternative_allowed)
518 flags |= DataReductionProxyParams::kAlternativeAllowed;
519 if (tests[i].alternative_fallback_allowed)
520 flags |= DataReductionProxyParams::kAlternativeFallbackAllowed;
521 if (tests[i].promo_allowed)
522 flags |= DataReductionProxyParams::kPromoAllowed;
523 TestDataReductionProxyParams params(
525 TestDataReductionProxyParams::HAS_EVERYTHING &
526 ~(tests[i].missing_definitions));
527 EXPECT_EQ(tests[i].expected_result, params.init_result()) << i;
531 TEST_F(DataReductionProxyParamsTest, IsDataReductionProxy) {
533 net::HostPortPair host_port_pair;
534 bool fallback_allowed;
535 bool alt_fallback_allowed;
537 bool expected_result;
538 net::HostPortPair expected_first;
539 net::HostPortPair expected_second;
540 bool expected_is_fallback;
541 bool expected_is_alternative;
542 bool expected_is_ssl;
545 net::HostPortPair::FromURL(GURL(
546 TestDataReductionProxyParams::DefaultOrigin())),
551 net::HostPortPair::FromURL(GURL(
552 TestDataReductionProxyParams::DefaultOrigin())),
553 net::HostPortPair::FromURL(GURL(
554 TestDataReductionProxyParams::DefaultFallbackOrigin())),
560 net::HostPortPair::FromURL(GURL(
561 TestDataReductionProxyParams::DefaultOrigin())),
566 net::HostPortPair::FromURL(GURL(
567 TestDataReductionProxyParams::DefaultOrigin())),
568 net::HostPortPair::FromURL(GURL()),
574 net::HostPortPair::FromURL(GURL(
575 TestDataReductionProxyParams::DefaultFallbackOrigin())),
580 net::HostPortPair::FromURL(GURL(
581 TestDataReductionProxyParams::DefaultFallbackOrigin())),
582 net::HostPortPair::FromURL(GURL()),
588 net::HostPortPair::FromURL(GURL(
589 TestDataReductionProxyParams::DefaultFallbackOrigin())),
594 net::HostPortPair::FromURL(GURL()),
595 net::HostPortPair::FromURL(GURL()),
601 net::HostPortPair::FromURL(GURL(
602 TestDataReductionProxyParams::DefaultAltOrigin())),
607 net::HostPortPair::FromURL(GURL(
608 TestDataReductionProxyParams::DefaultAltOrigin())),
609 net::HostPortPair::FromURL(GURL(
610 TestDataReductionProxyParams::DefaultAltFallbackOrigin())),
616 net::HostPortPair::FromURL(GURL(
617 TestDataReductionProxyParams::DefaultAltOrigin())),
622 net::HostPortPair::FromURL(GURL(
623 TestDataReductionProxyParams::DefaultAltOrigin())),
624 net::HostPortPair::FromURL(GURL()),
630 net::HostPortPair::FromURL(
631 GURL(TestDataReductionProxyParams::DefaultAltFallbackOrigin())),
636 net::HostPortPair::FromURL(GURL(
637 TestDataReductionProxyParams::DefaultAltFallbackOrigin())),
638 net::HostPortPair::FromURL(GURL()),
644 net::HostPortPair::FromURL(GURL(
645 TestDataReductionProxyParams::DefaultAltFallbackOrigin())),
650 net::HostPortPair::FromURL(GURL()),
651 net::HostPortPair::FromURL(GURL()),
657 net::HostPortPair::FromURL(GURL(
658 TestDataReductionProxyParams::DefaultSSLOrigin())),
663 net::HostPortPair::FromURL(GURL(
664 TestDataReductionProxyParams::DefaultSSLOrigin())),
665 net::HostPortPair::FromURL(GURL()),
671 net::HostPortPair::FromURL(GURL(
672 TestDataReductionProxyParams::DefaultDevOrigin())),
677 net::HostPortPair::FromURL(GURL(
678 TestDataReductionProxyParams::DefaultDevOrigin())),
679 net::HostPortPair::FromURL(GURL(
680 TestDataReductionProxyParams::DefaultDevFallbackOrigin())),
686 net::HostPortPair::FromURL(GURL(
687 TestDataReductionProxyParams::DefaultOrigin())),
692 net::HostPortPair::FromURL(GURL()),
693 net::HostPortPair::FromURL(GURL()),
699 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
700 int flags = DataReductionProxyParams::kAllowed |
701 DataReductionProxyParams::kAlternativeAllowed;
702 if (tests[i].fallback_allowed)
703 flags |= DataReductionProxyParams::kFallbackAllowed;
704 if (tests[i].alt_fallback_allowed)
705 flags |= DataReductionProxyParams::kAlternativeFallbackAllowed;
706 unsigned int has_definitions = TestDataReductionProxyParams::HAS_EVERYTHING;
707 if (!tests[i].set_dev_origin) {
708 has_definitions &= ~TestDataReductionProxyParams::HAS_DEV_ORIGIN;
709 has_definitions &= ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN;
711 TestDataReductionProxyParams params(flags, has_definitions);
712 DataReductionProxyTypeInfo proxy_type_info;
713 EXPECT_EQ(tests[i].expected_result,
714 params.IsDataReductionProxy(
715 tests[i].host_port_pair, &proxy_type_info)) << i;
716 EXPECT_TRUE(tests[i].expected_first.Equals(
717 net::HostPortPair::FromURL(proxy_type_info.proxy_servers.first))) << i;
718 EXPECT_TRUE(tests[i].expected_second.Equals(
719 net::HostPortPair::FromURL(proxy_type_info.proxy_servers.second))) << i;
720 EXPECT_EQ(tests[i].expected_is_fallback, proxy_type_info.is_fallback) << i;
721 EXPECT_EQ(tests[i].expected_is_alternative, proxy_type_info.is_alternative)
723 EXPECT_EQ(tests[i].expected_is_ssl, proxy_type_info.is_ssl) << i;
727 std::string GetRetryMapKeyFromOrigin(std::string origin) {
728 // The retry map has the scheme prefix for https but not for http
729 return net::ProxyServer(GURL(origin).SchemeIs(url::kHttpsScheme) ?
730 net::ProxyServer::SCHEME_HTTPS : net::ProxyServer::SCHEME_HTTP,
731 net::HostPortPair::FromURL(GURL(origin))).ToURI();
734 TEST_F(DataReductionProxyParamsTest, AreProxiesBypassed) {
738 bool fallback_allowed;
742 // proxies in retry map
744 bool fallback_origin;
746 bool alt_fallback_origin;
749 bool expected_result;
757 // proxies in retry map
772 // proxies in retry map
787 // proxies in retry map
802 // proxies in retry map
817 // proxies in retry map
832 // proxies in retry map
847 // proxies in retry map
862 // proxies in retry map
877 // proxies in retry map
892 // proxies in retry map
907 // proxies in retry map
922 // proxies in retry map
937 // proxies in retry map
952 // proxies in retry map
967 // proxies in retry map
982 // proxies in retry map
997 // proxies in retry map
1012 // proxies in retry map
1027 // proxies in retry map
1042 // proxies in retry map
1057 // proxies in retry map
1068 // The retry map has the scheme prefix for https but not for http.
1069 std::string origin = GetRetryMapKeyFromOrigin(
1070 TestDataReductionProxyParams::DefaultOrigin());
1071 std::string fallback_origin =GetRetryMapKeyFromOrigin(
1072 TestDataReductionProxyParams::DefaultFallbackOrigin());
1073 std::string alt_origin = GetRetryMapKeyFromOrigin(
1074 TestDataReductionProxyParams::DefaultAltOrigin());
1075 std::string alt_fallback_origin = GetRetryMapKeyFromOrigin(
1076 TestDataReductionProxyParams::DefaultAltFallbackOrigin());
1077 std::string ssl_origin = GetRetryMapKeyFromOrigin(
1078 TestDataReductionProxyParams::DefaultSSLOrigin());
1080 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
1082 if (tests[i].allowed)
1083 flags |= DataReductionProxyParams::kAllowed;
1084 if (tests[i].alt_allowed)
1085 flags |= DataReductionProxyParams::kAlternativeAllowed;
1086 if (tests[i].fallback_allowed)
1087 flags |= DataReductionProxyParams::kFallbackAllowed;
1088 unsigned int has_definitions =
1089 TestDataReductionProxyParams::HAS_EVERYTHING &
1090 ~TestDataReductionProxyParams::HAS_DEV_ORIGIN &
1091 ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN;
1092 TestDataReductionProxyParams params(flags, has_definitions);
1094 net::ProxyRetryInfoMap retry_map;
1095 net::ProxyRetryInfo retry_info;
1097 if (tests[i].origin)
1098 retry_map[origin] = retry_info;
1099 if (tests[i].fallback_origin)
1100 retry_map[fallback_origin] = retry_info;
1101 if (tests[i].alt_origin)
1102 retry_map[alt_origin] = retry_info;
1103 if (tests[i].alt_fallback_origin)
1104 retry_map[alt_fallback_origin] = retry_info;
1105 if (tests[i].ssl_origin)
1106 retry_map[ssl_origin] = retry_info;
1108 bool was_bypassed = params.AreProxiesBypassed(retry_map,
1112 EXPECT_EQ(tests[i].expected_result, was_bypassed);
1115 } // namespace data_reduction_proxy