| 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 <windows.h> | |
| 17 #include <atlconv.h> | |
| 18 #include <algorithm> | |
| 19 #include <cstring> | |
| 20 #include "base/basictypes.h" | |
| 21 #include "omaha/base/module_utils.h" | |
| 22 #include "omaha/base/omaha_version.h" | |
| 23 #include "omaha/base/reg_key.h" | |
| 24 #include "omaha/base/utils.h" | |
| 25 #include "omaha/base/vistautil.h" | |
| 26 #include "omaha/net/cup_request.h" | |
| 27 #include "omaha/net/cup_utils.h" | |
| 28 #include "omaha/net/http_client.h" | |
| 29 #include "omaha/net/network_config.h" | |
| 30 #include "omaha/testing/unit_test.h" | |
| 31 | |
| 32 namespace omaha { | |
| 33 | |
| 34 class NetworkConfigTest : public testing::Test { | |
| 35 protected: | |
| 36 NetworkConfigTest() {} | |
| 37 | |
| 38 static void SetUpTestCase() {} | |
| 39 | |
| 40 static void TearDownTestCase() {} | |
| 41 | |
| 42 virtual void SetUp() {} | |
| 43 | |
| 44 virtual void TearDown() {} | |
| 45 }; | |
| 46 | |
| 47 TEST_F(NetworkConfigTest, GetAccessType) { | |
| 48 EXPECT_EQ(NetworkConfig::GetAccessType(ProxyConfig()), | |
| 49 WINHTTP_ACCESS_TYPE_NO_PROXY); | |
| 50 | |
| 51 ProxyConfig config; | |
| 52 config.auto_detect = true; | |
| 53 EXPECT_EQ(NetworkConfig::GetAccessType(config), | |
| 54 WINHTTP_ACCESS_TYPE_AUTO_DETECT); | |
| 55 | |
| 56 config = ProxyConfig(); | |
| 57 config.auto_config_url = _T("http://foo"); | |
| 58 EXPECT_EQ(NetworkConfig::GetAccessType(config), | |
| 59 WINHTTP_ACCESS_TYPE_AUTO_DETECT); | |
| 60 | |
| 61 config = ProxyConfig(); | |
| 62 config.auto_detect = true; | |
| 63 config.proxy = _T("foo"); | |
| 64 EXPECT_EQ(NetworkConfig::GetAccessType(config), | |
| 65 WINHTTP_ACCESS_TYPE_AUTO_DETECT); | |
| 66 | |
| 67 config = ProxyConfig(); | |
| 68 config.proxy = _T("foo"); | |
| 69 EXPECT_EQ(NetworkConfig::GetAccessType(config), | |
| 70 WINHTTP_ACCESS_TYPE_NAMED_PROXY); | |
| 71 } | |
| 72 | |
| 73 TEST_F(NetworkConfigTest, CupCredentials) { | |
| 74 NetworkConfig* network_config = NULL; | |
| 75 EXPECT_HRESULT_SUCCEEDED( | |
| 76 NetworkConfigManager::Instance().GetUserNetworkConfig(&network_config)); | |
| 77 EXPECT_HRESULT_SUCCEEDED(network_config->SetCupCredentials(NULL)); | |
| 78 | |
| 79 CupCredentials cup_credentials1; | |
| 80 EXPECT_HRESULT_FAILED(network_config->GetCupCredentials(&cup_credentials1)); | |
| 81 | |
| 82 // Start with some random bytes. Persist them as sk and as B64-encoded c. | |
| 83 // Read back and verify they match. | |
| 84 uint8 data[20] = {0}; | |
| 85 EXPECT_TRUE(GenRandom(data, arraysize(data))); | |
| 86 | |
| 87 const uint8* first = data; | |
| 88 const uint8* last = data + arraysize(data); | |
| 89 cup_credentials1.sk.insert(cup_credentials1.sk.begin(), first, last); | |
| 90 cup_credentials1.c = cup_utils::B64Encode(data, arraysize(data)); | |
| 91 | |
| 92 EXPECT_HRESULT_SUCCEEDED( | |
| 93 network_config->SetCupCredentials(&cup_credentials1)); | |
| 94 | |
| 95 CupCredentials cup_credentials2; | |
| 96 EXPECT_HRESULT_SUCCEEDED( | |
| 97 network_config->GetCupCredentials(&cup_credentials2)); | |
| 98 EXPECT_EQ(cup_credentials1.sk.size(), cup_credentials2.sk.size()); | |
| 99 EXPECT_TRUE(memcmp(&cup_credentials1.sk.front(), | |
| 100 &cup_credentials2.sk.front(), | |
| 101 cup_credentials1.sk.size()) == 0); | |
| 102 EXPECT_STREQ(cup_credentials1.c, cup_credentials2.c); | |
| 103 | |
| 104 EXPECT_HRESULT_SUCCEEDED(network_config->SetCupCredentials(NULL)); | |
| 105 EXPECT_HRESULT_FAILED(network_config->GetCupCredentials(&cup_credentials1)); | |
| 106 } | |
| 107 | |
| 108 TEST_F(NetworkConfigTest, JoinStrings) { | |
| 109 EXPECT_STREQ(NetworkConfig::JoinStrings(NULL, NULL, NULL), _T("")); | |
| 110 | |
| 111 CString result; | |
| 112 EXPECT_STREQ(NetworkConfig::JoinStrings(NULL, NULL, _T("-")), _T("-")); | |
| 113 EXPECT_STREQ(NetworkConfig::JoinStrings(_T("foo"), _T("bar"), _T("-")), | |
| 114 _T("foo-bar")); | |
| 115 } | |
| 116 | |
| 117 TEST_F(NetworkConfigTest, GetUserAgentTest) { | |
| 118 CString version(GetVersionString()); | |
| 119 EXPECT_FALSE(version.IsEmpty()); | |
| 120 CString actual_user_agent(NetworkConfig::GetUserAgent()); | |
| 121 CString expected_user_agent; | |
| 122 expected_user_agent.Format(_T("Google Update/%s"), version); | |
| 123 EXPECT_STREQ(actual_user_agent, expected_user_agent); | |
| 124 } | |
| 125 | |
| 126 // Hosts names used in the test are only used as string literals. | |
| 127 TEST_F(NetworkConfigTest, RemoveDuplicates) { | |
| 128 // 'source' is not considered in the hash computation. | |
| 129 std::vector<ProxyConfig> configurations; | |
| 130 ProxyConfig cfg1; | |
| 131 cfg1.source = "foo"; | |
| 132 ProxyConfig cfg2; | |
| 133 cfg2.source = "bar"; | |
| 134 configurations.push_back(cfg1); | |
| 135 configurations.push_back(cfg2); | |
| 136 NetworkConfig::RemoveDuplicates(&configurations); | |
| 137 EXPECT_EQ(1, configurations.size()); | |
| 138 configurations.clear(); | |
| 139 | |
| 140 // Remove redundant direct connection configurations. | |
| 141 ProxyConfig direct_config; | |
| 142 configurations.push_back(direct_config); | |
| 143 configurations.push_back(direct_config); | |
| 144 NetworkConfig::RemoveDuplicates(&configurations); | |
| 145 EXPECT_EQ(1, configurations.size()); | |
| 146 configurations.clear(); | |
| 147 | |
| 148 // Remove redundant WPAD configurations. | |
| 149 ProxyConfig wpad_config; | |
| 150 wpad_config.auto_detect = true; | |
| 151 configurations.push_back(wpad_config); | |
| 152 configurations.push_back(wpad_config); | |
| 153 NetworkConfig::RemoveDuplicates(&configurations); | |
| 154 EXPECT_EQ(1, configurations.size()); | |
| 155 configurations.clear(); | |
| 156 | |
| 157 // Remove redundant WPAD with auto config url configurations. | |
| 158 ProxyConfig wpad_url_config; | |
| 159 wpad_url_config.auto_detect = true; | |
| 160 wpad_url_config.auto_config_url = _T("http://www.google.com/wpad.dat"); | |
| 161 configurations.push_back(wpad_url_config); | |
| 162 configurations.push_back(wpad_url_config); | |
| 163 NetworkConfig::RemoveDuplicates(&configurations); | |
| 164 EXPECT_EQ(1, configurations.size()); | |
| 165 configurations.clear(); | |
| 166 | |
| 167 // Remove redundant named proxy configurations. | |
| 168 ProxyConfig named_proxy_config; | |
| 169 named_proxy_config.proxy = _T("www1.google.com:3128"); | |
| 170 configurations.push_back(named_proxy_config); | |
| 171 configurations.push_back(named_proxy_config); | |
| 172 NetworkConfig::RemoveDuplicates(&configurations); | |
| 173 EXPECT_EQ(1, configurations.size()); | |
| 174 configurations.clear(); | |
| 175 | |
| 176 // Does not remove distinct configurations. | |
| 177 ProxyConfig named_proxy_config_alt; | |
| 178 named_proxy_config_alt.proxy = _T("www2.google.com:3128"); | |
| 179 configurations.push_back(named_proxy_config); | |
| 180 configurations.push_back(named_proxy_config_alt); | |
| 181 configurations.push_back(direct_config); | |
| 182 configurations.push_back(wpad_config); | |
| 183 configurations.push_back(wpad_url_config); | |
| 184 NetworkConfig::RemoveDuplicates(&configurations); | |
| 185 EXPECT_EQ(5, configurations.size()); | |
| 186 } | |
| 187 | |
| 188 TEST_F(NetworkConfigTest, ParseNetConfig) { | |
| 189 ProxyConfig config = NetworkConfig::ParseNetConfig(_T("")); | |
| 190 EXPECT_EQ(false, config.auto_detect); | |
| 191 EXPECT_EQ(true, config.auto_config_url.IsEmpty()); | |
| 192 EXPECT_EQ(true, config.proxy.IsEmpty()); | |
| 193 | |
| 194 config = NetworkConfig::ParseNetConfig(_T("wpad=false")); | |
| 195 EXPECT_EQ(false, config.auto_detect); | |
| 196 EXPECT_EQ(true, config.auto_config_url.IsEmpty()); | |
| 197 EXPECT_EQ(true, config.proxy.IsEmpty()); | |
| 198 | |
| 199 config = NetworkConfig::ParseNetConfig(_T("wpad=true")); | |
| 200 EXPECT_EQ(true, config.auto_detect); | |
| 201 EXPECT_EQ(true, config.auto_config_url.IsEmpty()); | |
| 202 EXPECT_EQ(true, config.proxy.IsEmpty()); | |
| 203 | |
| 204 config = NetworkConfig::ParseNetConfig(_T("script=foo;proxy=bar")); | |
| 205 EXPECT_EQ(false, config.auto_detect); | |
| 206 EXPECT_STREQ(_T("foo"), config.auto_config_url); | |
| 207 EXPECT_EQ(_T("bar"), config.proxy); | |
| 208 | |
| 209 config = NetworkConfig::ParseNetConfig(_T("proxy=foobar")); | |
| 210 EXPECT_EQ(false, config.auto_detect); | |
| 211 EXPECT_EQ(true, config.auto_config_url.IsEmpty()); | |
| 212 EXPECT_EQ(_T("foobar"), config.proxy); | |
| 213 } | |
| 214 | |
| 215 TEST_F(NetworkConfigTest, ConfigurationOverride) { | |
| 216 NetworkConfig* network_config = NULL; | |
| 217 EXPECT_HRESULT_SUCCEEDED( | |
| 218 NetworkConfigManager::Instance().GetUserNetworkConfig(&network_config)); | |
| 219 | |
| 220 ProxyConfig actual, expected; | |
| 221 expected.auto_detect = true; | |
| 222 network_config->SetConfigurationOverride(&expected); | |
| 223 EXPECT_HRESULT_SUCCEEDED(network_config->GetConfigurationOverride(&actual)); | |
| 224 EXPECT_EQ(expected.auto_detect, actual.auto_detect); | |
| 225 | |
| 226 network_config->SetConfigurationOverride(NULL); | |
| 227 EXPECT_EQ(E_FAIL, network_config->GetConfigurationOverride(&actual)); | |
| 228 } | |
| 229 | |
| 230 TEST_F(NetworkConfigTest, GetProxyForUrlLocal) { | |
| 231 TCHAR module_directory[MAX_PATH] = {0}; | |
| 232 ASSERT_TRUE(GetModuleDirectory(NULL, module_directory)); | |
| 233 CString pac_file_path; | |
| 234 pac_file_path.Format(_T("%s\\unittest_support\\localproxytest.pac"), | |
| 235 module_directory); | |
| 236 | |
| 237 HttpClient::ProxyInfo proxy_info = {}; | |
| 238 | |
| 239 // The PAC file should emit a preset response for any URL with a hostname | |
| 240 // matching *.omahaproxytest.com and DIRECT otherwise. | |
| 241 | |
| 242 EXPECT_HRESULT_SUCCEEDED(NetworkConfig::GetProxyForUrlLocal( | |
| 243 _T("http://regex.matches.domain.omahaproxytest.com/test_url/index.html"), | |
| 244 pac_file_path, &proxy_info)); | |
| 245 EXPECT_EQ(WINHTTP_ACCESS_TYPE_NAMED_PROXY, proxy_info.access_type); | |
| 246 EXPECT_STREQ(_T("omaha_unittest1;omaha_unittest2:8080"), | |
| 247 CString(proxy_info.proxy)); | |
| 248 EXPECT_EQ(NULL, proxy_info.proxy_bypass); | |
| 249 | |
| 250 if (proxy_info.proxy) { | |
| 251 ::GlobalFree(const_cast<TCHAR*>(proxy_info.proxy)); | |
| 252 } | |
| 253 if (proxy_info.proxy_bypass) { | |
| 254 ::GlobalFree(const_cast<TCHAR*>(proxy_info.proxy_bypass)); | |
| 255 } | |
| 256 | |
| 257 EXPECT_HRESULT_SUCCEEDED(NetworkConfig::GetProxyForUrlLocal( | |
| 258 _T("http://should.not.match.domain.example.com/test_url/index.html"), | |
| 259 pac_file_path, &proxy_info)); | |
| 260 EXPECT_EQ(WINHTTP_ACCESS_TYPE_NO_PROXY, proxy_info.access_type); | |
| 261 EXPECT_EQ(NULL, proxy_info.proxy); | |
| 262 EXPECT_EQ(NULL, proxy_info.proxy_bypass); | |
| 263 } | |
| 264 | |
| 265 class NetworkConfigManagerTest : public testing::Test { | |
| 266 protected: | |
| 267 NetworkConfigManagerTest() {} | |
| 268 | |
| 269 static void SetUpTestCase() {} | |
| 270 | |
| 271 static void TearDownTestCase() {} | |
| 272 | |
| 273 virtual void SetUp() { | |
| 274 NetworkConfigManager::Instance().ClearCupCredentials(); | |
| 275 } | |
| 276 | |
| 277 virtual void TearDown() { | |
| 278 NetworkConfigManager::Instance().ClearCupCredentials(); | |
| 279 } | |
| 280 }; | |
| 281 | |
| 282 TEST_F(NetworkConfigManagerTest, CupCredentials) { | |
| 283 NetworkConfigManager& ncm(NetworkConfigManager::Instance()); | |
| 284 | |
| 285 CupCredentials cup_credentials; | |
| 286 EXPECT_EQ(E_INVALIDARG, ncm.SetCupCredentials(cup_credentials)); | |
| 287 | |
| 288 ncm.ClearCupCredentials(); | |
| 289 | |
| 290 EXPECT_EQ(HRESULT_FROM_WIN32(ERROR_NOT_FOUND), | |
| 291 ncm.GetCupCredentials(&cup_credentials)); | |
| 292 | |
| 293 const int kKeySizeBytes = 16; | |
| 294 cup_credentials.sk.resize(kKeySizeBytes); | |
| 295 EXPECT_TRUE(GenRandom(&cup_credentials.sk.front(), | |
| 296 cup_credentials.sk.size())); | |
| 297 cup_credentials.c = "a cookie"; | |
| 298 | |
| 299 EXPECT_HRESULT_SUCCEEDED(ncm.SetCupCredentials(cup_credentials)); | |
| 300 | |
| 301 CupCredentials actual_cup_credentials; | |
| 302 EXPECT_HRESULT_SUCCEEDED(ncm.GetCupCredentials(&actual_cup_credentials)); | |
| 303 | |
| 304 EXPECT_TRUE(std::equal(actual_cup_credentials.sk.begin(), | |
| 305 actual_cup_credentials.sk.end(), | |
| 306 cup_credentials.sk.begin())); | |
| 307 EXPECT_STREQ(actual_cup_credentials.c, cup_credentials.c); | |
| 308 } | |
| 309 | |
| 310 } // namespace omaha | |
| 311 | |
| OLD | NEW |