OLD | NEW |
(Empty) | |
| 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 class TestDataReductionProxyParams : public DataReductionProxyParams { |
| 46 public: |
| 47 |
| 48 TestDataReductionProxyParams(bool allowed, |
| 49 bool fallback_allowed, |
| 50 bool alt_allowed, |
| 51 bool promo_allowed, |
| 52 unsigned int has_definitions) |
| 53 : DataReductionProxyParams(allowed, |
| 54 fallback_allowed, |
| 55 alt_allowed, |
| 56 promo_allowed, |
| 57 false), |
| 58 has_definitions_(has_definitions) { |
| 59 init_result_ = Init(allowed, fallback_allowed, alt_allowed); |
| 60 } |
| 61 |
| 62 bool init_result() const { |
| 63 return init_result_; |
| 64 } |
| 65 |
| 66 protected: |
| 67 virtual std::string GetDefaultKey() const OVERRIDE { |
| 68 return GetDefinition(HAS_KEY, kDefaultKey); |
| 69 } |
| 70 |
| 71 virtual std::string GetDefaultDevOrigin() const OVERRIDE { |
| 72 return GetDefinition(HAS_DEV_ORIGIN, kDefaultDevOrigin); |
| 73 } |
| 74 |
| 75 virtual std::string GetDefaultOrigin() const OVERRIDE { |
| 76 return GetDefinition(HAS_ORIGIN, kDefaultOrigin); |
| 77 } |
| 78 |
| 79 virtual std::string GetDefaultFallbackOrigin() const OVERRIDE { |
| 80 return GetDefinition(HAS_FALLBACK_ORIGIN, kDefaultFallbackOrigin); |
| 81 } |
| 82 |
| 83 virtual std::string GetDefaultSSLOrigin() const OVERRIDE { |
| 84 return GetDefinition(HAS_SSL_ORIGIN, kDefaultSSLOrigin); |
| 85 } |
| 86 |
| 87 virtual std::string GetDefaultAltOrigin() const OVERRIDE { |
| 88 return GetDefinition(HAS_ALT_ORIGIN, kDefaultAltOrigin); |
| 89 } |
| 90 |
| 91 virtual std::string GetDefaultAltFallbackOrigin() const OVERRIDE { |
| 92 return GetDefinition(HAS_ALT_FALLBACK_ORIGIN, kDefaultAltFallbackOrigin); |
| 93 } |
| 94 |
| 95 virtual std::string GetDefaultProbeURL() const OVERRIDE { |
| 96 return GetDefinition(HAS_PROBE_URL, kDefaultProbeURL); |
| 97 } |
| 98 |
| 99 private: |
| 100 std::string GetDefinition(unsigned int has_def, |
| 101 const std::string& definition) const { |
| 102 return ((has_definitions_ & has_def) ? definition : std::string()); |
| 103 } |
| 104 |
| 105 unsigned int has_definitions_; |
| 106 bool init_result_; |
| 107 }; |
| 108 |
| 109 class DataReductionProxyParamsTest : public testing::Test { |
| 110 public: |
| 111 void CheckParams(const TestDataReductionProxyParams& params, |
| 112 bool expected_init_result, |
| 113 bool expected_allowed, |
| 114 bool expected_fallback_allowed, |
| 115 bool expected_alternative_allowed, |
| 116 bool expected_promo_allowed) { |
| 117 EXPECT_EQ(expected_init_result, params.init_result()); |
| 118 EXPECT_EQ(expected_allowed, params.allowed()); |
| 119 EXPECT_EQ(expected_fallback_allowed, params.fallback_allowed()); |
| 120 EXPECT_EQ(expected_alternative_allowed, params.alternative_allowed()); |
| 121 EXPECT_EQ(expected_promo_allowed, params.promo_allowed()); |
| 122 } |
| 123 void CheckValues(const TestDataReductionProxyParams& params, |
| 124 const std::string expected_key, |
| 125 const std::string& expected_origin, |
| 126 const std::string& expected_fallback_origin, |
| 127 const std::string& expected_ssl_origin, |
| 128 const std::string& expected_alt_origin, |
| 129 const std::string& expected_alt_fallback_origin, |
| 130 const std::string& expected_probe_url) { |
| 131 EXPECT_EQ(expected_key, params.key()); |
| 132 EXPECT_EQ(GURL(expected_origin), params.origin()); |
| 133 EXPECT_EQ(GURL(expected_fallback_origin), params.fallback_origin()); |
| 134 EXPECT_EQ(GURL(expected_ssl_origin), params.ssl_origin()); |
| 135 EXPECT_EQ(GURL(expected_alt_origin), params.alt_origin()); |
| 136 EXPECT_EQ(GURL(expected_alt_fallback_origin), params.alt_fallback_origin()); |
| 137 EXPECT_EQ(GURL(expected_probe_url), params.probe_url()); |
| 138 } |
| 139 }; |
| 140 |
| 141 TEST_F(DataReductionProxyParamsTest, EverythingDefined) { |
| 142 TestDataReductionProxyParams params(true, true, false, true, HAS_EVERYTHING); |
| 143 CheckParams(params, true, true, true, false, true); |
| 144 CheckValues(params, |
| 145 kDefaultKey, |
| 146 kDefaultDevOrigin, |
| 147 kDefaultFallbackOrigin, |
| 148 kDefaultSSLOrigin, |
| 149 kDefaultAltOrigin, |
| 150 kDefaultAltFallbackOrigin, |
| 151 kDefaultProbeURL); |
| 152 } |
| 153 |
| 154 TEST_F(DataReductionProxyParamsTest, NoDevOrigin) { |
| 155 TestDataReductionProxyParams params( |
| 156 true, true, false, true, HAS_EVERYTHING & ~HAS_DEV_ORIGIN); |
| 157 CheckParams(params, true, true, true, false, true); |
| 158 CheckValues(params, |
| 159 kDefaultKey, |
| 160 kDefaultOrigin, |
| 161 kDefaultFallbackOrigin, |
| 162 kDefaultSSLOrigin, |
| 163 kDefaultAltOrigin, |
| 164 kDefaultAltFallbackOrigin, |
| 165 kDefaultProbeURL); |
| 166 } |
| 167 |
| 168 TEST_F(DataReductionProxyParamsTest, Flags) { |
| 169 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 170 switches::kDataReductionProxyKey, kFlagKey); |
| 171 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 172 switches::kDataReductionProxy, kFlagOrigin); |
| 173 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 174 switches::kDataReductionProxyFallback, kFlagFallbackOrigin); |
| 175 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 176 switches::kDataReductionSSLProxy, kFlagSSLOrigin); |
| 177 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 178 switches::kDataReductionProxyAlt, kFlagAltOrigin); |
| 179 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 180 switches::kDataReductionProxyAltFallback, kFlagAltFallbackOrigin); |
| 181 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 182 switches::kDataReductionProxyProbeURL, kFlagProbeURL); |
| 183 TestDataReductionProxyParams params( |
| 184 true, true, true, true, HAS_EVERYTHING); |
| 185 CheckParams(params, true, true, true, true, true); |
| 186 CheckValues(params, |
| 187 kFlagKey, |
| 188 kFlagOrigin, |
| 189 kFlagFallbackOrigin, |
| 190 kFlagSSLOrigin, |
| 191 kFlagAltOrigin, |
| 192 kFlagAltFallbackOrigin, |
| 193 kFlagProbeURL); |
| 194 } |
| 195 |
| 196 TEST_F(DataReductionProxyParamsTest, FlagsNoKey) { |
| 197 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 198 switches::kDataReductionProxy, kFlagOrigin); |
| 199 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 200 switches::kDataReductionProxyFallback, kFlagFallbackOrigin); |
| 201 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 202 switches::kDataReductionSSLProxy, kFlagSSLOrigin); |
| 203 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 204 switches::kDataReductionProxyAlt, kFlagAltOrigin); |
| 205 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 206 switches::kDataReductionProxyAltFallback, kFlagAltFallbackOrigin); |
| 207 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 208 switches::kDataReductionProxyProbeURL, kFlagProbeURL); |
| 209 TestDataReductionProxyParams params( |
| 210 true, true, true, true, HAS_EVERYTHING); |
| 211 EXPECT_FALSE(params.init_result()); |
| 212 } |
| 213 |
| 214 TEST_F(DataReductionProxyParamsTest, InvalidConfigurations) { |
| 215 const struct { |
| 216 bool allowed; |
| 217 bool fallback_allowed; |
| 218 bool alternative_allowed; |
| 219 bool promo_allowed; |
| 220 unsigned int missing_definitions; |
| 221 bool expected_result; |
| 222 } tests[] = { |
| 223 { true, true, true, true, HAS_NOTHING, true }, |
| 224 { true, true, true, true, HAS_KEY, false }, |
| 225 { true, true, true, true, HAS_DEV_ORIGIN, true }, |
| 226 { true, true, true, true, HAS_ORIGIN, true }, |
| 227 { true, true, true, true, HAS_ORIGIN | HAS_DEV_ORIGIN, false }, |
| 228 { true, true, true, true, HAS_FALLBACK_ORIGIN, false }, |
| 229 { true, true, true, true, HAS_SSL_ORIGIN, false }, |
| 230 { true, true, true, true, HAS_ALT_ORIGIN, false }, |
| 231 { true, true, true, true, HAS_ALT_FALLBACK_ORIGIN, false }, |
| 232 { true, true, true, true, HAS_PROBE_URL, false }, |
| 233 |
| 234 { true, false, true, true, HAS_NOTHING, true }, |
| 235 { true, false, true, true, HAS_KEY, false }, |
| 236 { true, false, true, true, HAS_ORIGIN | HAS_DEV_ORIGIN, false }, |
| 237 { true, false, true, true, HAS_FALLBACK_ORIGIN, true }, |
| 238 { true, false, true, true, HAS_SSL_ORIGIN, false }, |
| 239 { true, false, true, true, HAS_ALT_ORIGIN, false }, |
| 240 { true, false, true, true, HAS_ALT_FALLBACK_ORIGIN, true }, |
| 241 { true, false, true, true, HAS_PROBE_URL, false }, |
| 242 |
| 243 { true, true, false, true, HAS_NOTHING, true }, |
| 244 { true, true, false, true, HAS_KEY, false }, |
| 245 { true, true, false, true, HAS_ORIGIN | HAS_DEV_ORIGIN, false }, |
| 246 { true, true, false, true, HAS_FALLBACK_ORIGIN, false }, |
| 247 { true, true, false, true, HAS_SSL_ORIGIN, true }, |
| 248 { true, true, false, true, HAS_ALT_ORIGIN, true }, |
| 249 { true, true, false, true, HAS_ALT_FALLBACK_ORIGIN, true }, |
| 250 { true, true, false, true, HAS_PROBE_URL, false }, |
| 251 |
| 252 { true, false, false, true, HAS_KEY, false }, |
| 253 { true, false, false, true, HAS_ORIGIN | HAS_DEV_ORIGIN, false }, |
| 254 { true, false, false, true, HAS_FALLBACK_ORIGIN, true }, |
| 255 { true, false, false, true, HAS_SSL_ORIGIN, true }, |
| 256 { true, false, false, true, HAS_ALT_ORIGIN, true }, |
| 257 { true, false, false, true, HAS_ALT_FALLBACK_ORIGIN, true }, |
| 258 { true, false, false, true, HAS_PROBE_URL, false }, |
| 259 |
| 260 { false, true, true, true, HAS_NOTHING, false }, |
| 261 { false, true, true, true, HAS_KEY, false }, |
| 262 { false, true, true, true, HAS_ORIGIN | HAS_DEV_ORIGIN, false }, |
| 263 { false, true, true, true, HAS_FALLBACK_ORIGIN, false }, |
| 264 { false, true, true, true, HAS_SSL_ORIGIN, false }, |
| 265 { false, true, true, true, HAS_ALT_ORIGIN, false }, |
| 266 { false, true, true, true, HAS_ALT_FALLBACK_ORIGIN, false }, |
| 267 { false, true, true, true, HAS_PROBE_URL, false }, |
| 268 }; |
| 269 |
| 270 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 271 TestDataReductionProxyParams params( |
| 272 tests[i].allowed, |
| 273 tests[i].fallback_allowed, |
| 274 tests[i].alternative_allowed, |
| 275 tests[i].promo_allowed, |
| 276 HAS_EVERYTHING & ~(tests[i].missing_definitions)); |
| 277 EXPECT_EQ(tests[i].expected_result, params.init_result()); |
| 278 } |
| 279 } |
| 280 |
| 281 } // namespace data_reduction_proxy |
OLD | NEW |