OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 |
| 5 #include <deque> |
| 6 #include <string> |
| 7 |
| 8 #include <gtest/gtest.h> |
| 9 |
| 10 #include "update_engine/chrome_browser_proxy_resolver.h" |
| 11 #include "update_engine/mock_dbus_interface.h" |
| 12 |
| 13 using std::deque; |
| 14 using std::string; |
| 15 using ::testing::_; |
| 16 using ::testing::Return; |
| 17 using ::testing::SetArgumentPointee; |
| 18 using ::testing::StrEq; |
| 19 |
| 20 namespace chromeos_update_engine { |
| 21 |
| 22 class ChromeBrowserProxyResolverTest : public ::testing::Test { }; |
| 23 |
| 24 TEST(ChromeBrowserProxyResolverTest, ParseTest) { |
| 25 // Test ideas from |
| 26 // http://src.chromium.org/svn/trunk/src/net/proxy/proxy_list_unittest.cc |
| 27 const char* inputs[] = { |
| 28 "PROXY foopy:10", |
| 29 " DIRECT", // leading space. |
| 30 "PROXY foopy1 ; proxy foopy2;\t DIRECT", |
| 31 "proxy foopy1 ; SOCKS foopy2", |
| 32 "DIRECT ; proxy foopy1 ; DIRECT ; SOCKS5 foopy2;DIRECT ", |
| 33 "DIRECT ; proxy foopy1:80; DIRECT ; DIRECT", |
| 34 "PROXY-foopy:10", |
| 35 "PROXY", |
| 36 "PROXY foopy1 ; JUNK ; JUNK ; SOCKS5 foopy2 ; ;", |
| 37 "HTTP foopy1; SOCKS5 foopy2" |
| 38 }; |
| 39 deque<string> outputs[arraysize(inputs)]; |
| 40 outputs[0].push_back("http://foopy:10"); |
| 41 outputs[0].push_back(kNoProxy); |
| 42 outputs[1].push_back(kNoProxy); |
| 43 outputs[2].push_back("http://foopy1"); |
| 44 outputs[2].push_back("http://foopy2"); |
| 45 outputs[2].push_back(kNoProxy); |
| 46 outputs[3].push_back("http://foopy1"); |
| 47 outputs[3].push_back("socks4://foopy2"); |
| 48 outputs[3].push_back(kNoProxy); |
| 49 outputs[4].push_back(kNoProxy); |
| 50 outputs[4].push_back("http://foopy1"); |
| 51 outputs[4].push_back(kNoProxy); |
| 52 outputs[4].push_back("socks5://foopy2"); |
| 53 outputs[4].push_back(kNoProxy); |
| 54 outputs[5].push_back(kNoProxy); |
| 55 outputs[5].push_back("http://foopy1:80"); |
| 56 outputs[5].push_back(kNoProxy); |
| 57 outputs[5].push_back(kNoProxy); |
| 58 outputs[6].push_back(kNoProxy); |
| 59 outputs[7].push_back(kNoProxy); |
| 60 outputs[8].push_back("http://foopy1"); |
| 61 outputs[8].push_back("socks5://foopy2"); |
| 62 outputs[8].push_back(kNoProxy); |
| 63 outputs[9].push_back("socks5://foopy2"); |
| 64 outputs[9].push_back(kNoProxy); |
| 65 |
| 66 for (size_t i = 0; i < arraysize(inputs); i++) { |
| 67 deque<string> results = |
| 68 ChromeBrowserProxyResolver::ParseProxyString(inputs[i]); |
| 69 deque<string>& expected = outputs[i]; |
| 70 EXPECT_EQ(results.size(), expected.size()) << "i = " << i; |
| 71 if (expected.size() != results.size()) |
| 72 continue; |
| 73 for (size_t j = 0; j < expected.size(); j++) { |
| 74 EXPECT_EQ(expected[j], results[j]) << "i = " << i; |
| 75 } |
| 76 } |
| 77 } |
| 78 |
| 79 namespace { |
| 80 void DbusInterfaceTestResolved(const std::deque<std::string>& proxies, |
| 81 void* data) { |
| 82 EXPECT_EQ(2, proxies.size()); |
| 83 EXPECT_EQ("socks5://192.168.52.83:5555", proxies[0]); |
| 84 EXPECT_EQ(kNoProxy, proxies[1]); |
| 85 g_main_loop_quit(reinterpret_cast<GMainLoop*>(data)); |
| 86 } |
| 87 void DbusInterfaceTestResolvedNoReply(const std::deque<std::string>& proxies, |
| 88 void* data) { |
| 89 EXPECT_EQ(1, proxies.size()); |
| 90 EXPECT_EQ(kNoProxy, proxies[0]); |
| 91 g_main_loop_quit(reinterpret_cast<GMainLoop*>(data)); |
| 92 } |
| 93 struct SendReplyArgs { |
| 94 DBusConnection* connection; |
| 95 DBusMessage* message; |
| 96 ChromeBrowserProxyResolver* resolver; |
| 97 }; |
| 98 gboolean SendReply(gpointer data) { |
| 99 LOG(INFO) << "Calling SendReply"; |
| 100 SendReplyArgs* args = reinterpret_cast<SendReplyArgs*>(data); |
| 101 ChromeBrowserProxyResolver::StaticFilterMessage( |
| 102 args->connection, |
| 103 args->message, |
| 104 args->resolver); |
| 105 return FALSE; // Don't keep calling this function |
| 106 } |
| 107 |
| 108 // chrome_replies should be set to whether or not we fake a reply from |
| 109 // chrome. If there's no reply, the resolver should time out. |
| 110 void RunTest(bool chrome_replies) { |
| 111 long number = 1; |
| 112 DBusGConnection* kMockSystemGBus = |
| 113 reinterpret_cast<DBusGConnection*>(number++); |
| 114 DBusConnection* kMockSystemBus = |
| 115 reinterpret_cast<DBusConnection*>(number++); |
| 116 DBusGProxy* kMockDbusProxy = |
| 117 reinterpret_cast<DBusGProxy*>(number++); |
| 118 DBusMessage* kMockDbusMessage = |
| 119 reinterpret_cast<DBusMessage*>(number++); |
| 120 |
| 121 const char kUrl[] = "http://example.com/blah"; |
| 122 |
| 123 MockDbusGlib dbus_iface; |
| 124 |
| 125 EXPECT_CALL(dbus_iface, BusGet(_, _)) |
| 126 .Times(3) |
| 127 .WillRepeatedly(Return(kMockSystemGBus)); |
| 128 EXPECT_CALL(dbus_iface, |
| 129 ConnectionGetConnection(kMockSystemGBus)) |
| 130 .Times(2) |
| 131 .WillRepeatedly(Return(kMockSystemBus)); |
| 132 EXPECT_CALL(dbus_iface, DbusBusAddMatch(kMockSystemBus, _, _)); |
| 133 EXPECT_CALL(dbus_iface, |
| 134 DbusConnectionAddFilter(kMockSystemBus, _, _, _)) |
| 135 .WillOnce(Return(1)); |
| 136 EXPECT_CALL(dbus_iface, |
| 137 ProxyNewForNameOwner(kMockSystemGBus, |
| 138 StrEq(kLibCrosServiceName), |
| 139 StrEq(kLibCrosServicePath), |
| 140 StrEq(kLibCrosServiceInterface), |
| 141 _)) |
| 142 .WillOnce(Return(kMockDbusProxy)); |
| 143 EXPECT_CALL(dbus_iface, ProxyCall( |
| 144 kMockDbusProxy, |
| 145 StrEq(kLibCrosServiceResolveNetworkProxyMethodName), |
| 146 _, |
| 147 G_TYPE_STRING, StrEq(kUrl), |
| 148 G_TYPE_STRING, StrEq(kLibCrosProxyResolveSignalInterface), |
| 149 G_TYPE_STRING, StrEq(kLibCrosProxyResolveName), |
| 150 G_TYPE_INVALID)) |
| 151 .WillOnce(Return(TRUE)); |
| 152 EXPECT_CALL(dbus_iface, |
| 153 DbusConnectionRemoveFilter(kMockSystemBus, _, _)); |
| 154 if (chrome_replies) { |
| 155 EXPECT_CALL(dbus_iface, |
| 156 DbusMessageIsSignal(kMockDbusMessage, |
| 157 kLibCrosProxyResolveSignalInterface, |
| 158 kLibCrosProxyResolveName)) |
| 159 .WillOnce(Return(1)); |
| 160 EXPECT_CALL(dbus_iface, |
| 161 DbusMessageGetArgs(kMockDbusMessage, _, |
| 162 DBUS_TYPE_STRING, _, |
| 163 DBUS_TYPE_STRING, _, |
| 164 DBUS_TYPE_STRING, _, |
| 165 DBUS_TYPE_INVALID)) |
| 166 .WillOnce(DoAll(SetArgumentPointee<3>(strdup(kUrl)), |
| 167 SetArgumentPointee<5>( |
| 168 strdup("SOCKS5 192.168.52.83:5555;DIRECT")), |
| 169 Return(TRUE))); |
| 170 } |
| 171 |
| 172 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 173 |
| 174 ChromeBrowserProxyResolver resolver(&dbus_iface); |
| 175 EXPECT_TRUE(resolver.Init()); |
| 176 resolver.set_timeout(1); |
| 177 SendReplyArgs args = { |
| 178 kMockSystemBus, |
| 179 kMockDbusMessage, |
| 180 &resolver |
| 181 }; |
| 182 if (chrome_replies) |
| 183 g_idle_add(SendReply, &args); |
| 184 EXPECT_TRUE(resolver.GetProxiesForUrl(kUrl, |
| 185 chrome_replies ? |
| 186 &DbusInterfaceTestResolved : |
| 187 &DbusInterfaceTestResolvedNoReply, |
| 188 loop)); |
| 189 g_main_loop_run(loop); |
| 190 g_main_loop_unref(loop); |
| 191 } |
| 192 } // namespace {} |
| 193 |
| 194 TEST(ChromeBrowserProxyResolverTest, SuccessTest) { |
| 195 RunTest(true); |
| 196 } |
| 197 |
| 198 TEST(ChromeBrowserProxyResolverTest, NoReplyTest) { |
| 199 RunTest(false); |
| 200 } |
| 201 |
| 202 TEST(ChromeBrowserProxyResolverTest, NoChromeTest) { |
| 203 long number = 1; |
| 204 DBusGConnection* kMockSystemGBus = |
| 205 reinterpret_cast<DBusGConnection*>(number++); |
| 206 DBusConnection* kMockSystemBus = |
| 207 reinterpret_cast<DBusConnection*>(number++); |
| 208 |
| 209 const char kUrl[] = "http://example.com/blah"; |
| 210 |
| 211 MockDbusGlib dbus_iface; |
| 212 |
| 213 EXPECT_CALL(dbus_iface, BusGet(_, _)) |
| 214 .Times(2) |
| 215 .WillRepeatedly(Return(kMockSystemGBus)); |
| 216 EXPECT_CALL(dbus_iface, |
| 217 ConnectionGetConnection(kMockSystemGBus)) |
| 218 .Times(1) |
| 219 .WillOnce(Return(kMockSystemBus)); |
| 220 EXPECT_CALL(dbus_iface, DbusBusAddMatch(kMockSystemBus, _, _)); |
| 221 EXPECT_CALL(dbus_iface, |
| 222 DbusConnectionAddFilter(kMockSystemBus, _, _, _)) |
| 223 .WillOnce(Return(1)); |
| 224 EXPECT_CALL(dbus_iface, |
| 225 ProxyNewForNameOwner(kMockSystemGBus, |
| 226 StrEq(kLibCrosServiceName), |
| 227 StrEq(kLibCrosServicePath), |
| 228 StrEq(kLibCrosServiceInterface), |
| 229 _)) |
| 230 .WillOnce(Return(static_cast<DBusGProxy*>(NULL))); |
| 231 EXPECT_CALL(dbus_iface, |
| 232 DbusConnectionRemoveFilter(kMockSystemBus, _, _)); |
| 233 ChromeBrowserProxyResolver resolver(&dbus_iface); |
| 234 EXPECT_FALSE(resolver.Init()); |
| 235 EXPECT_FALSE(resolver.GetProxiesForUrl(kUrl, NULL, NULL)); |
| 236 } |
| 237 |
| 238 } // namespace chromeos_update_engine |
OLD | NEW |