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