| OLD | NEW |
| (Empty) |
| 1 // Copyright 2007-2009 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // ======================================================================== | |
| 15 | |
| 16 #include <cstdio> | |
| 17 #include <vector> | |
| 18 #include "base/basictypes.h" | |
| 19 #include "base/scoped_ptr.h" | |
| 20 #include "omaha/base/app_util.h" | |
| 21 #include "omaha/base/browser_utils.h" | |
| 22 #include "omaha/net/detector.h" | |
| 23 #include "omaha/net/network_config.h" | |
| 24 #include "omaha/testing/unit_test.h" | |
| 25 | |
| 26 namespace omaha { | |
| 27 | |
| 28 class FirefoxProxyDetectorTest : public testing::Test { | |
| 29 public: | |
| 30 FirefoxProxyDetectorTest() {} | |
| 31 | |
| 32 virtual void SetUp() { | |
| 33 detector_.reset(new FirefoxProxyDetector); | |
| 34 } | |
| 35 | |
| 36 virtual void TearDown() { | |
| 37 } | |
| 38 | |
| 39 HRESULT BuildProxyString(const CString& http_host, | |
| 40 const CString& http_port, | |
| 41 const CString& ssl_host, | |
| 42 const CString& ssl_port, | |
| 43 CString* proxy) { | |
| 44 return detector_->BuildProxyString(http_host, | |
| 45 http_port, | |
| 46 ssl_host, | |
| 47 ssl_port, | |
| 48 proxy); | |
| 49 } | |
| 50 | |
| 51 void ParsePrefsLine(const char* ansi_line, | |
| 52 CString* proxy_type, | |
| 53 CString* proxy_config_url, | |
| 54 CString* proxy_http_host, | |
| 55 CString* proxy_http_port, | |
| 56 CString* proxy_ssl_host, | |
| 57 CString* proxy_ssl_port) { | |
| 58 detector_->ParsePrefsLine(ansi_line, | |
| 59 proxy_type, | |
| 60 proxy_config_url, | |
| 61 proxy_http_host, | |
| 62 proxy_http_port, | |
| 63 proxy_ssl_host, | |
| 64 proxy_ssl_port); | |
| 65 } | |
| 66 | |
| 67 | |
| 68 HRESULT ParsePrefsFile(const TCHAR* name, | |
| 69 const TCHAR* file_path, | |
| 70 ProxyConfig* config) { | |
| 71 return detector_->ParsePrefsFile(name, file_path, config); | |
| 72 } | |
| 73 | |
| 74 // Builds a mock prefs file to test the parsing code. | |
| 75 bool BuildPrefsFile(const CString& type, | |
| 76 const CString& config_url, | |
| 77 const CString& http_host, | |
| 78 const CString& http_port, | |
| 79 const CString& ssl_host, | |
| 80 const CString& ssl_port, | |
| 81 CString* file_path); | |
| 82 | |
| 83 scoped_ptr<FirefoxProxyDetector> detector_; | |
| 84 | |
| 85 private: | |
| 86 DISALLOW_EVIL_CONSTRUCTORS(FirefoxProxyDetectorTest); | |
| 87 }; | |
| 88 | |
| 89 bool FirefoxProxyDetectorTest::BuildPrefsFile(const CString& type, | |
| 90 const CString& config_url, | |
| 91 const CString& http_host, | |
| 92 const CString& http_port, | |
| 93 const CString& ssl_host, | |
| 94 const CString& ssl_port, | |
| 95 CString* file_path) { | |
| 96 CString temp_dir(app_util::GetTempDir()); | |
| 97 file_path->Format(_T("%somaha_test_%x.js"), temp_dir, ::GetTickCount()); | |
| 98 | |
| 99 FILE* prefs_file = NULL; | |
| 100 fopen_s(&prefs_file, CStringA(*file_path), "w"); | |
| 101 if (!prefs_file) { | |
| 102 return false; | |
| 103 } | |
| 104 | |
| 105 fprintf(prefs_file, | |
| 106 "user_pref(\"network.proxy.type\", %s);\n", | |
| 107 CStringA(type)); | |
| 108 fprintf(prefs_file, | |
| 109 "user_pref(\"network.proxy.autoconfig_url\", \"%s\");\n", | |
| 110 CStringA(config_url)); | |
| 111 fprintf(prefs_file, | |
| 112 "user_pref(\"network.proxy.http\", \"%s\");\n", | |
| 113 CStringA(http_host)); | |
| 114 fprintf(prefs_file, | |
| 115 "user_pref(\"network.proxy.http_port\", %s);\n", | |
| 116 CStringA(http_port)); | |
| 117 fprintf(prefs_file, | |
| 118 "user_pref(\"network.proxy.ssl\", \"%s\");\n", | |
| 119 CStringA(ssl_host)); | |
| 120 fprintf(prefs_file, | |
| 121 "user_pref(\"network.proxy.ssl_port\", %s);\n", | |
| 122 CStringA(ssl_port)); | |
| 123 | |
| 124 fclose(prefs_file); | |
| 125 | |
| 126 return true; | |
| 127 } | |
| 128 | |
| 129 TEST_F(FirefoxProxyDetectorTest, BuildProxyString) { | |
| 130 CString http_host = _T("foo"); | |
| 131 CString http_port = _T("80"); | |
| 132 CString ssl_host; | |
| 133 CString ssl_port; | |
| 134 CString proxy; | |
| 135 | |
| 136 EXPECT_SUCCEEDED(BuildProxyString(http_host, | |
| 137 http_port, | |
| 138 ssl_host, | |
| 139 ssl_port, | |
| 140 &proxy)); | |
| 141 EXPECT_STREQ(proxy, _T("http=foo:80")); | |
| 142 | |
| 143 http_host = _T("foo"); | |
| 144 http_port = _T("80"); | |
| 145 ssl_host = _T("bar"); | |
| 146 ssl_port = _T("8080"); | |
| 147 | |
| 148 EXPECT_SUCCEEDED(BuildProxyString(http_host, | |
| 149 http_port, | |
| 150 ssl_host, | |
| 151 ssl_port, | |
| 152 &proxy)); | |
| 153 EXPECT_STREQ(proxy, _T("http=foo:80;https=bar:8080")); | |
| 154 | |
| 155 http_host.Empty(); | |
| 156 http_port.Empty(); | |
| 157 ssl_host = _T("bar"); | |
| 158 ssl_port = _T("8080"); | |
| 159 | |
| 160 EXPECT_SUCCEEDED(BuildProxyString(http_host, | |
| 161 http_port, | |
| 162 ssl_host, | |
| 163 ssl_port, | |
| 164 &proxy)); | |
| 165 EXPECT_STREQ(proxy, _T("https=bar:8080")); | |
| 166 } | |
| 167 | |
| 168 TEST_F(FirefoxProxyDetectorTest, ParsePrefsLine) { | |
| 169 CString type; | |
| 170 CString config_url; | |
| 171 CString http_host; | |
| 172 CString http_port; | |
| 173 CString ssl_host; | |
| 174 CString ssl_port; | |
| 175 | |
| 176 // Parse "type". | |
| 177 const char* ansi_line = "user_pref(\"network.proxy.type\", 4);"; | |
| 178 ParsePrefsLine(ansi_line, | |
| 179 &type, | |
| 180 &config_url, | |
| 181 &http_host, | |
| 182 &http_port, | |
| 183 &ssl_host, | |
| 184 &ssl_port); | |
| 185 EXPECT_STREQ(type, _T("4")); | |
| 186 | |
| 187 // Parse "config_url". | |
| 188 ansi_line = | |
| 189 "user_pref(\"network.proxy.autoconfig_url\", \"http://wpad/wpad.dat\");"; | |
| 190 ParsePrefsLine(ansi_line, | |
| 191 &type, | |
| 192 &config_url, | |
| 193 &http_host, | |
| 194 &http_port, | |
| 195 &ssl_host, | |
| 196 &ssl_port); | |
| 197 EXPECT_STREQ(config_url, _T("\"http://wpad/wpad.dat\"")); | |
| 198 | |
| 199 // Parse "http_host". | |
| 200 ansi_line = "user_pref(\"network.proxy.http\", \"127.0.0.1\");"; | |
| 201 ParsePrefsLine(ansi_line, | |
| 202 &type, | |
| 203 &config_url, | |
| 204 &http_host, | |
| 205 &http_port, | |
| 206 &ssl_host, | |
| 207 &ssl_port); | |
| 208 EXPECT_STREQ(http_host, _T("\"127.0.0.1\"")); | |
| 209 | |
| 210 // Parse "http_port". | |
| 211 ansi_line = "user_pref(\"network.proxy.http_port\", 8888);"; | |
| 212 ParsePrefsLine(ansi_line, | |
| 213 &type, | |
| 214 &config_url, | |
| 215 &http_host, | |
| 216 &http_port, | |
| 217 &ssl_host, | |
| 218 &ssl_port); | |
| 219 EXPECT_STREQ(http_port, _T("8888")); | |
| 220 | |
| 221 // Parse "ssl_host". | |
| 222 ansi_line = "user_pref(\"network.proxy.ssl\", \"10.0.0.1\");"; | |
| 223 ParsePrefsLine(ansi_line, | |
| 224 &type, | |
| 225 &config_url, | |
| 226 &http_host, | |
| 227 &http_port, | |
| 228 &ssl_host, | |
| 229 &ssl_port); | |
| 230 EXPECT_STREQ(ssl_host, _T("\"10.0.0.1\"")); | |
| 231 | |
| 232 // Parse "ssl_port". | |
| 233 ansi_line = "user_pref(\"network.proxy.ssl_port\", 8080);"; | |
| 234 ParsePrefsLine(ansi_line, | |
| 235 &type, | |
| 236 &config_url, | |
| 237 &http_host, | |
| 238 &http_port, | |
| 239 &ssl_host, | |
| 240 &ssl_port); | |
| 241 EXPECT_STREQ(ssl_port, _T("8080")); | |
| 242 } | |
| 243 | |
| 244 TEST_F(FirefoxProxyDetectorTest, ParsePrefsFile) { | |
| 245 // Direct connection | |
| 246 CString prefs_file; | |
| 247 bool res = BuildPrefsFile(_T("0"), | |
| 248 _T("http://foobar"), | |
| 249 _T("foo"), | |
| 250 _T("80"), | |
| 251 _T("bar"), | |
| 252 _T("8080"), | |
| 253 &prefs_file); | |
| 254 ASSERT_TRUE(res); | |
| 255 ProxyConfig config; | |
| 256 EXPECT_SUCCEEDED(ParsePrefsFile(_T(""), prefs_file, &config)); | |
| 257 EXPECT_FALSE(config.auto_detect); | |
| 258 EXPECT_TRUE(config.auto_config_url.IsEmpty()); | |
| 259 EXPECT_TRUE(config.proxy.IsEmpty()); | |
| 260 EXPECT_TRUE(config.proxy_bypass.IsEmpty()); | |
| 261 EXPECT_TRUE(::DeleteFile(prefs_file)); | |
| 262 | |
| 263 // Named proxy. | |
| 264 res = BuildPrefsFile(_T("1"), | |
| 265 _T("http://foobar"), | |
| 266 _T("foo"), | |
| 267 _T("80"), | |
| 268 _T("bar"), | |
| 269 _T("8080"), | |
| 270 &prefs_file); | |
| 271 ASSERT_TRUE(res); | |
| 272 config = ProxyConfig(); | |
| 273 EXPECT_SUCCEEDED(ParsePrefsFile(_T(""), prefs_file, &config)); | |
| 274 EXPECT_FALSE(config.auto_detect); | |
| 275 EXPECT_TRUE(config.auto_config_url.IsEmpty()); | |
| 276 EXPECT_STREQ(config.proxy, _T("http=foo:80;https=bar:8080")); | |
| 277 EXPECT_TRUE(config.proxy_bypass.IsEmpty()); | |
| 278 EXPECT_TRUE(::DeleteFile(prefs_file)); | |
| 279 | |
| 280 // Auto config url. | |
| 281 res = BuildPrefsFile(_T("2"), | |
| 282 _T("http://foobar"), | |
| 283 _T("foo"), | |
| 284 _T("80"), | |
| 285 _T("bar"), | |
| 286 _T("8080"), | |
| 287 &prefs_file); | |
| 288 ASSERT_TRUE(res); | |
| 289 config = ProxyConfig(); | |
| 290 EXPECT_SUCCEEDED(ParsePrefsFile(_T(""), prefs_file, &config)); | |
| 291 EXPECT_FALSE(config.auto_detect); | |
| 292 EXPECT_STREQ(config.auto_config_url, _T("http://foobar")); | |
| 293 EXPECT_TRUE(config.proxy.IsEmpty()); | |
| 294 EXPECT_TRUE(config.proxy_bypass.IsEmpty()); | |
| 295 EXPECT_TRUE(::DeleteFile(prefs_file)); | |
| 296 | |
| 297 // Auto detect. | |
| 298 res = BuildPrefsFile(_T("4"), | |
| 299 _T("http://foobar"), | |
| 300 _T("foo"), | |
| 301 _T("80"), | |
| 302 _T("bar"), | |
| 303 _T("8080"), | |
| 304 &prefs_file); | |
| 305 ASSERT_TRUE(res); | |
| 306 config = ProxyConfig(); | |
| 307 EXPECT_SUCCEEDED(ParsePrefsFile(_T(""), prefs_file, &config)); | |
| 308 EXPECT_TRUE(config.auto_detect); | |
| 309 EXPECT_TRUE(config.auto_config_url.IsEmpty()); | |
| 310 EXPECT_TRUE(config.proxy.IsEmpty()); | |
| 311 EXPECT_TRUE(config.proxy_bypass.IsEmpty()); | |
| 312 EXPECT_TRUE(::DeleteFile(prefs_file)); | |
| 313 } | |
| 314 | |
| 315 // Tries to detect the configuration if a profile is available. | |
| 316 TEST_F(FirefoxProxyDetectorTest, Detect) { | |
| 317 CString name, path; | |
| 318 if (FAILED(GetFirefoxDefaultProfile(&name, &path))) { | |
| 319 return; | |
| 320 } | |
| 321 ProxyConfig config; | |
| 322 EXPECT_SUCCEEDED(detector_->Detect(&config)); | |
| 323 } | |
| 324 | |
| 325 } // namespace omaha | |
| 326 | |
| OLD | NEW |