Andrew de los Reyes | 9cd120d | 2010-11-18 17:50:03 -0800 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium OS 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 | |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame^] | 5 | #include <deque> |
Andrew de los Reyes | 9cd120d | 2010-11-18 17:50:03 -0800 | [diff] [blame] | 6 | #include <string> |
Andrew de los Reyes | 9cd120d | 2010-11-18 17:50:03 -0800 | [diff] [blame] | 7 | |
| 8 | #include <gtest/gtest.h> |
| 9 | |
| 10 | #include "update_engine/chrome_proxy_resolver.h" |
| 11 | #include "update_engine/mock_dbus_interface.h" |
| 12 | |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame^] | 13 | using std::deque; |
Andrew de los Reyes | 9cd120d | 2010-11-18 17:50:03 -0800 | [diff] [blame] | 14 | using std::string; |
Andrew de los Reyes | 9cd120d | 2010-11-18 17:50:03 -0800 | [diff] [blame] | 15 | using ::testing::_; |
| 16 | using ::testing::Return; |
| 17 | using ::testing::SetArgumentPointee; |
| 18 | using ::testing::StrEq; |
| 19 | |
| 20 | namespace chromeos_update_engine { |
| 21 | |
| 22 | class ChromeProxyResolverTest : public ::testing::Test { }; |
| 23 | |
| 24 | TEST(ChromeProxyResolverTest, GetProxiesForUrlWithSettingsTest) { |
| 25 | const string kSingle = |
| 26 | "{\"mode\":3,\"single\":{\"server\":\"192.168.42.86:80\",\"src\":0}}"; |
| 27 | const string kSocks = |
| 28 | "{\"mode\":4,\"socks\":{\"server\":\"socks5://192.168.42.83:5555\"," |
| 29 | "\"src\":0}}"; |
| 30 | const string kAll = |
| 31 | "{\"http\":{\"server\":\"http_proxy:11\",\"src\":0}," |
| 32 | "\"https\":{\"server\":\"https://https_proxy:22\",\"src\":0}," |
| 33 | "\"mode\":4," |
| 34 | "\"socks\":{\"server\":\"socks5://socks:55\",\"src\":0}}"; |
| 35 | const string kHttpHttps = |
| 36 | "{\"http\":{\"server\":\"http_proxy:11\",\"src\":0}," |
| 37 | "\"https\":{\"server\":\"https://https_proxy:22\",\"src\":0}," |
| 38 | "\"mode\":4}"; |
| 39 | |
| 40 | ChromeProxyResolver resolver(NULL); |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame^] | 41 | deque<string> out; |
Andrew de los Reyes | 9cd120d | 2010-11-18 17:50:03 -0800 | [diff] [blame] | 42 | string urls[] = {"http://foo.com/update", "https://bar.com/foo.gz"}; |
| 43 | string multi_settings[] = {kAll, kHttpHttps}; |
| 44 | for (size_t i = 0; i < arraysize(urls); i++) { |
| 45 | const string& url = urls[i]; |
| 46 | LOG(INFO) << "url: " << url; |
| 47 | EXPECT_TRUE(resolver.GetProxiesForUrlWithSettings(url, kSingle, &out)); |
| 48 | EXPECT_EQ(2, out.size()); |
| 49 | EXPECT_EQ("http://192.168.42.86:80", out[0]); |
| 50 | EXPECT_EQ(kNoProxy, out[1]); |
| 51 | |
| 52 | EXPECT_TRUE(resolver.GetProxiesForUrlWithSettings(url, kSocks, &out)); |
| 53 | EXPECT_EQ(2, out.size()); |
| 54 | EXPECT_EQ("socks5://192.168.42.83:5555", out[0]); |
| 55 | EXPECT_EQ(kNoProxy, out[1]); |
| 56 | |
| 57 | for (size_t j = 0; j < arraysize(multi_settings); j++) { |
| 58 | const string& settings = multi_settings[j]; |
| 59 | EXPECT_TRUE(resolver.GetProxiesForUrlWithSettings(url, settings, &out)); |
| 60 | EXPECT_EQ(j == 0 ? 3 : 2, out.size()); |
| 61 | if (i == 0) |
| 62 | EXPECT_EQ("http://http_proxy:11", out[0]); |
| 63 | else |
| 64 | EXPECT_EQ("https://https_proxy:22", out[0]); |
| 65 | if (j == 0) |
| 66 | EXPECT_EQ("socks5://socks:55", out[1]); |
| 67 | EXPECT_EQ(kNoProxy, out.back()); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Bad JSON |
| 72 | EXPECT_FALSE(resolver.GetProxiesForUrlWithSettings("http://foo.com", |
| 73 | "}", |
| 74 | &out)); |
| 75 | |
| 76 | // Bad proxy scheme |
| 77 | EXPECT_TRUE(resolver.GetProxiesForUrlWithSettings("http://foo.com", |
| 78 | "{\"mode\":1}", |
| 79 | &out)); |
| 80 | EXPECT_EQ(1, out.size()); |
| 81 | EXPECT_EQ(kNoProxy, out[0]); |
| 82 | } |
| 83 | |
| 84 | TEST(ChromeProxyResolverTest, DbusInterfaceTest) { |
| 85 | long number = 1; |
| 86 | DBusGConnection* kMockSystemBus = |
| 87 | reinterpret_cast<DBusGConnection*>(number++); |
| 88 | DBusGProxy* kMockSessionManagerProxy = |
| 89 | reinterpret_cast<DBusGProxy*>(number++); |
| 90 | |
| 91 | const char settings[] = |
| 92 | "{\"mode\":4,\"socks\":{\"server\":\"socks5://192.168.52.83:5555\"," |
| 93 | "\"src\":0}}"; |
| 94 | |
| 95 | MockDbusGlib dbus_iface; |
| 96 | ChromeProxyResolver resolver(&dbus_iface); |
| 97 | |
| 98 | GArray* ret_array = g_array_new(FALSE, FALSE, 1); |
| 99 | |
| 100 | EXPECT_CALL(dbus_iface, BusGet(DBUS_BUS_SYSTEM, _)) |
| 101 | .Times(1) |
| 102 | .WillRepeatedly(Return(kMockSystemBus)); |
| 103 | EXPECT_CALL(dbus_iface, |
| 104 | ProxyNewForNameOwner(kMockSystemBus, |
| 105 | StrEq(kSessionManagerService), |
| 106 | StrEq(kSessionManagerPath), |
| 107 | StrEq(kSessionManagerInterface), |
| 108 | _)) |
| 109 | .WillOnce(Return(kMockSessionManagerProxy)); |
| 110 | EXPECT_CALL(dbus_iface, ProxyCall( |
| 111 | kMockSessionManagerProxy, |
| 112 | StrEq(kSessionManagerRetrievePropertyMethod), |
| 113 | _, |
| 114 | G_TYPE_STRING, StrEq(kSessionManagerProxySettingsKey), |
| 115 | G_TYPE_INVALID, |
| 116 | G_TYPE_STRING, _, |
| 117 | DBUS_TYPE_G_UCHAR_ARRAY, _)) |
| 118 | .WillOnce(DoAll(SetArgumentPointee<7>(strdup(settings)), |
| 119 | SetArgumentPointee<9>(ret_array), |
| 120 | Return(TRUE))); |
| 121 | |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame^] | 122 | deque<string> proxies; |
Andrew de los Reyes | 9cd120d | 2010-11-18 17:50:03 -0800 | [diff] [blame] | 123 | EXPECT_TRUE(resolver.GetProxiesForUrl("http://user:pass@foo.com:22", |
| 124 | &proxies)); |
| 125 | EXPECT_EQ(2, proxies.size()); |
| 126 | EXPECT_EQ("socks5://192.168.52.83:5555", proxies[0]); |
| 127 | EXPECT_EQ(kNoProxy, proxies[1]); |
| 128 | } |
| 129 | |
| 130 | TEST(ChromeProxyResolverTest, GetProxyTypeTest) { |
| 131 | curl_proxytype type; |
| 132 | EXPECT_TRUE(ChromeProxyResolver::GetProxyType("socks://f.ru", &type)); |
| 133 | EXPECT_EQ(CURLPROXY_SOCKS5_HOSTNAME, type); |
| 134 | EXPECT_TRUE(ChromeProxyResolver::GetProxyType("socks5://f.ru:9", &type)); |
| 135 | EXPECT_EQ(CURLPROXY_SOCKS5_HOSTNAME, type); |
| 136 | EXPECT_TRUE(ChromeProxyResolver::GetProxyType("socks4://f.ru:9", &type)); |
| 137 | EXPECT_EQ(CURLPROXY_SOCKS4A, type); |
| 138 | EXPECT_TRUE(ChromeProxyResolver::GetProxyType("http://f.no:88", &type)); |
| 139 | EXPECT_EQ(CURLPROXY_HTTP, type); |
| 140 | EXPECT_TRUE(ChromeProxyResolver::GetProxyType("https://f.no:88", &type)); |
| 141 | EXPECT_EQ(CURLPROXY_HTTP, type); |
| 142 | EXPECT_FALSE(ChromeProxyResolver::GetProxyType(kNoProxy, &type)); |
| 143 | EXPECT_FALSE(ChromeProxyResolver::GetProxyType("foo://", &type)); |
| 144 | EXPECT_FALSE(ChromeProxyResolver::GetProxyType("missing.com:8", &type)); |
| 145 | } |
| 146 | |
| 147 | } // namespace chromeos_update_engine |