blob: 735eb438652b52e26eef805df03c566608117268 [file] [log] [blame]
Thieu Le3426c8f2012-01-11 17:35:11 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewartdd60e452011-08-08 11:38:36 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Paul Stewart9a908082011-08-31 12:18:48 -07005#include <arpa/inet.h>
6#include <linux/rtnetlink.h>
7
Paul Stewartc8f4bef2011-12-13 09:45:51 -08008#include <string>
Paul Stewart9a908082011-08-31 12:18:48 -07009#include <vector>
10
11#include <base/memory/scoped_ptr.h>
Paul Stewartdd60e452011-08-08 11:38:36 -070012#include <gtest/gtest.h>
13#include <gmock/gmock.h>
14
15#include "shill/connection.h"
Paul Stewartdd60e452011-08-08 11:38:36 -070016#include "shill/ipconfig.h"
Darin Petkov13e6d552012-05-09 14:22:23 +020017#include "shill/mock_connection.h"
Paul Stewartdd60e452011-08-08 11:38:36 -070018#include "shill/mock_control.h"
Paul Stewartc8f4bef2011-12-13 09:45:51 -080019#include "shill/mock_device.h"
Paul Stewart9a908082011-08-31 12:18:48 -070020#include "shill/mock_device_info.h"
Paul Stewartdd60e452011-08-08 11:38:36 -070021#include "shill/mock_resolver.h"
22#include "shill/mock_routing_table.h"
23#include "shill/mock_rtnl_handler.h"
24#include "shill/routing_table_entry.h"
25
Paul Stewartc8f4bef2011-12-13 09:45:51 -080026using std::string;
Paul Stewart9a908082011-08-31 12:18:48 -070027using std::vector;
Paul Stewartdd60e452011-08-08 11:38:36 -070028using testing::_;
Paul Stewarte78ec542012-06-08 18:28:50 -070029using testing::Mock;
Paul Stewartdd60e452011-08-08 11:38:36 -070030using testing::NiceMock;
31using testing::Return;
Paul Stewart4a6748d2012-07-17 14:31:36 -070032using testing::ReturnRef;
Paul Stewartdd60e452011-08-08 11:38:36 -070033using testing::StrictMock;
34using testing::Test;
35
36namespace shill {
37
38namespace {
39const char kTestDeviceName0[] = "netdev0";
40const int kTestDeviceInterfaceIndex0 = 123;
41const char kTestDeviceName1[] = "netdev1";
42const int kTestDeviceInterfaceIndex1 = 321;
43const char kIPAddress0[] = "192.168.1.1";
44const char kGatewayAddress0[] = "192.168.1.254";
Paul Stewart5b7ba8c2012-04-18 09:08:00 -070045const char kGatewayAddress1[] = "192.168.2.254";
Paul Stewart9a908082011-08-31 12:18:48 -070046const char kBroadcastAddress0[] = "192.168.1.255";
Paul Stewartdd60e452011-08-08 11:38:36 -070047const char kNameServer0[] = "8.8.8.8";
48const char kNameServer1[] = "8.8.9.9";
Paul Stewart5b7ba8c2012-04-18 09:08:00 -070049const int32 kPrefix0 = 24;
50const int32 kPrefix1 = 31;
Paul Stewartdd60e452011-08-08 11:38:36 -070051const char kSearchDomain0[] = "chromium.org";
52const char kSearchDomain1[] = "google.com";
53} // namespace {}
54
55class ConnectionTest : public Test {
56 public:
57 ConnectionTest()
Paul Stewart9a908082011-08-31 12:18:48 -070058 : device_info_(new StrictMock<MockDeviceInfo>(
59 &control_,
60 static_cast<EventDispatcher*>(NULL),
Thieu Le3426c8f2012-01-11 17:35:11 -080061 static_cast<Metrics*>(NULL),
Paul Stewart9a908082011-08-31 12:18:48 -070062 static_cast<Manager*>(NULL))),
63 connection_(new Connection(
64 kTestDeviceInterfaceIndex0,
65 kTestDeviceName0,
Paul Stewarte00600e2012-03-16 07:08:00 -070066 Technology::kUnknown,
Paul Stewartbf667612012-06-29 14:49:54 -070067 device_info_.get(),
68 false)),
Paul Stewart5b7ba8c2012-04-18 09:08:00 -070069 ipconfig_(new IPConfig(&control_, kTestDeviceName0)),
70 local_address_(IPAddress::kFamilyIPv4),
71 broadcast_address_(IPAddress::kFamilyIPv4),
72 gateway_address_(IPAddress::kFamilyIPv4),
73 default_address_(IPAddress::kFamilyIPv4) {}
Paul Stewartdd60e452011-08-08 11:38:36 -070074
75 virtual void SetUp() {
Paul Stewartc8f4bef2011-12-13 09:45:51 -080076 ReplaceSingletons(connection_);
Paul Stewart5b7ba8c2012-04-18 09:08:00 -070077 properties_.address = kIPAddress0;
78 properties_.subnet_prefix = kPrefix0;
79 properties_.gateway = kGatewayAddress0;
80 properties_.broadcast_address = kBroadcastAddress0;
81 properties_.dns_servers.push_back(kNameServer0);
82 properties_.dns_servers.push_back(kNameServer1);
83 properties_.domain_search.push_back(kSearchDomain0);
84 properties_.domain_search.push_back(kSearchDomain1);
85 properties_.address_family = IPAddress::kFamilyIPv4;
86 UpdateProperties();
87 EXPECT_TRUE(local_address_.SetAddressFromString(kIPAddress0));
88 EXPECT_TRUE(broadcast_address_.SetAddressFromString(kBroadcastAddress0));
89 EXPECT_TRUE(gateway_address_.SetAddressFromString(kGatewayAddress0));
Paul Stewartdd60e452011-08-08 11:38:36 -070090 }
91
Paul Stewart9a908082011-08-31 12:18:48 -070092 virtual void TearDown() {
Darin Petkov13e6d552012-05-09 14:22:23 +020093 AddDestructorExpectations();
Paul Stewarte93b0382012-04-24 13:11:28 -070094 connection_ = NULL;
Paul Stewart9a908082011-08-31 12:18:48 -070095 }
96
Paul Stewartc8f4bef2011-12-13 09:45:51 -080097 void ReplaceSingletons(ConnectionRefPtr connection) {
98 connection->resolver_ = &resolver_;
99 connection->routing_table_ = &routing_table_;
100 connection->rtnl_handler_ = &rtnl_handler_;
101 }
102
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700103 void UpdateProperties() {
104 ipconfig_->UpdateProperties(properties_, true);
105 }
106
Paul Stewarte93b0382012-04-24 13:11:28 -0700107 bool PinHostRoute(ConnectionRefPtr connection,
108 const IPConfig::Properties &properties) {
109 return connection->PinHostRoute(properties);
110 }
111
Paul Stewart4a6748d2012-07-17 14:31:36 -0700112 const IPAddress &GetLocalAddress(ConnectionRefPtr connection) {
113 return connection->local_;
114 }
115
116 const IPAddress &GetGatewayAddress(ConnectionRefPtr connection) {
117 return connection->gateway_;
118 }
119
120 bool GetHasBroadcastDomain(ConnectionRefPtr connection) {
121 return connection->has_broadcast_domain_;
122 }
123
Paul Stewart05a42c22012-08-02 16:47:21 -0700124 uint32 GetDefaultMetric() {
125 return Connection::kDefaultMetric;
126 }
127
128 uint32 GetNonDefaultMetricBase() {
129 return Connection::kNonDefaultMetricBase;
130 }
131
Paul Stewartdd60e452011-08-08 11:38:36 -0700132 protected:
Darin Petkov13e6d552012-05-09 14:22:23 +0200133 class DisconnectCallbackTarget {
134 public:
135 DisconnectCallbackTarget()
136 : callback_(base::Bind(&DisconnectCallbackTarget::CallTarget,
137 base::Unretained(this))) {}
138
139 MOCK_METHOD0(CallTarget, void());
140 const base::Closure &callback() { return callback_; }
141
142 private:
143 base::Closure callback_;
144 };
145
146 void AddDestructorExpectations() {
147 EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex0));
148 EXPECT_CALL(routing_table_, FlushRoutesWithTag(kTestDeviceInterfaceIndex0));
149 EXPECT_CALL(*device_info_.get(),
150 FlushAddresses(kTestDeviceInterfaceIndex0));
151 }
152
153 // Returns a new test connection object. The caller usually needs to call
154 // AddDestructorExpectations before destroying the object.
155 ConnectionRefPtr GetNewConnection() {
156 ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex0,
157 kTestDeviceName0,
158 Technology::kUnknown,
Paul Stewartbf667612012-06-29 14:49:54 -0700159 device_info_.get(),
160 false));
Darin Petkov13e6d552012-05-09 14:22:23 +0200161 ReplaceSingletons(connection);
162 return connection;
163 }
164
Paul Stewart9a908082011-08-31 12:18:48 -0700165 scoped_ptr<StrictMock<MockDeviceInfo> > device_info_;
Paul Stewartdd60e452011-08-08 11:38:36 -0700166 ConnectionRefPtr connection_;
167 MockControl control_;
168 IPConfigRefPtr ipconfig_;
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700169 IPConfig::Properties properties_;
170 IPAddress local_address_;
171 IPAddress broadcast_address_;
172 IPAddress gateway_address_;
173 IPAddress default_address_;
Paul Stewartdd60e452011-08-08 11:38:36 -0700174 StrictMock<MockResolver> resolver_;
175 StrictMock<MockRoutingTable> routing_table_;
176 StrictMock<MockRTNLHandler> rtnl_handler_;
177};
178
Darin Petkov13e6d552012-05-09 14:22:23 +0200179namespace {
Paul Stewartdd60e452011-08-08 11:38:36 -0700180
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700181MATCHER_P2(IsIPAddress, address, prefix, "") {
182 IPAddress match_address(address);
183 match_address.set_prefix(prefix);
184 return match_address.Equals(arg);
185}
186
Darin Petkov13e6d552012-05-09 14:22:23 +0200187MATCHER(IsNonNullCallback, "") {
188 return !arg.is_null();
189}
190
191} // namespace
192
193TEST_F(ConnectionTest, InitState) {
194 EXPECT_EQ(kTestDeviceInterfaceIndex0, connection_->interface_index_);
195 EXPECT_EQ(kTestDeviceName0, connection_->interface_name_);
196 EXPECT_FALSE(connection_->is_default());
197 EXPECT_FALSE(connection_->routing_request_count_);
198}
199
Paul Stewartdd60e452011-08-08 11:38:36 -0700200TEST_F(ConnectionTest, AddConfig) {
Paul Stewart05a42c22012-08-02 16:47:21 -0700201 EXPECT_CALL(*device_info_,
202 HasOtherAddress(kTestDeviceInterfaceIndex0,
203 IsIPAddress(local_address_, kPrefix0)))
204 .WillOnce(Return(false));
Paul Stewartdd60e452011-08-08 11:38:36 -0700205 EXPECT_CALL(rtnl_handler_,
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700206 AddInterfaceAddress(kTestDeviceInterfaceIndex0,
207 IsIPAddress(local_address_, kPrefix0),
208 IsIPAddress(broadcast_address_, 0),
209 IsIPAddress(default_address_, 0)));
Paul Stewart7cfca042011-12-08 14:18:17 -0800210 EXPECT_CALL(routing_table_,
211 SetDefaultRoute(kTestDeviceInterfaceIndex0,
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700212 IsIPAddress(gateway_address_, 0),
Paul Stewart05a42c22012-08-02 16:47:21 -0700213 GetNonDefaultMetricBase() +
Paul Stewart7cfca042011-12-08 14:18:17 -0800214 kTestDeviceInterfaceIndex0));
Paul Stewart3f68bb12012-03-15 13:33:10 -0700215 EXPECT_CALL(routing_table_,
216 ConfigureRoutes(kTestDeviceInterfaceIndex0,
217 ipconfig_,
Paul Stewart05a42c22012-08-02 16:47:21 -0700218 GetDefaultMetric()));
Paul Stewartdd60e452011-08-08 11:38:36 -0700219 connection_->UpdateFromIPConfig(ipconfig_);
Paul Stewart4a6748d2012-07-17 14:31:36 -0700220 IPAddress test_local_address(local_address_);
221 test_local_address.set_prefix(kPrefix0);
222 EXPECT_TRUE(test_local_address.Equals(GetLocalAddress(connection_)));
223 EXPECT_TRUE(gateway_address_.Equals(GetGatewayAddress(connection_)));
224 EXPECT_TRUE(GetHasBroadcastDomain(connection_));
225
226 EXPECT_CALL(routing_table_,
227 CreateLinkRoute(kTestDeviceInterfaceIndex0,
228 IsIPAddress(local_address_, kPrefix0),
229 IsIPAddress(gateway_address_, 0)))
230 .WillOnce(Return(true))
231 .WillOnce(Return(false));
232 EXPECT_TRUE(connection_->CreateGatewayRoute());
233 EXPECT_FALSE(connection_->CreateGatewayRoute());
234 connection_->has_broadcast_domain_ = false;
235 EXPECT_FALSE(connection_->CreateGatewayRoute());
Paul Stewartdd60e452011-08-08 11:38:36 -0700236
237 EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0,
Paul Stewart05a42c22012-08-02 16:47:21 -0700238 GetDefaultMetric()));
Paul Stewartdd60e452011-08-08 11:38:36 -0700239 EXPECT_CALL(resolver_, SetDNSFromLists(
240 ipconfig_->properties().dns_servers,
Paul Stewartbf667612012-06-29 14:49:54 -0700241 ipconfig_->properties().domain_search,
242 Resolver::kDefaultTimeout));
Paul Stewartdd60e452011-08-08 11:38:36 -0700243
Paul Stewartc681fa02012-03-02 19:40:04 -0800244 scoped_refptr<MockDevice> device(new StrictMock<MockDevice>(
245 &control_,
246 reinterpret_cast<EventDispatcher *>(NULL),
247 reinterpret_cast<Metrics *>(NULL),
248 reinterpret_cast<Manager *>(NULL),
249 kTestDeviceName0,
250 string(),
251 kTestDeviceInterfaceIndex0));
252 EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0))
253 .WillOnce(Return(device));
254 EXPECT_CALL(*device.get(), RequestPortalDetection())
255 .WillOnce(Return(true));
Paul Stewarte78ec542012-06-08 18:28:50 -0700256 EXPECT_CALL(routing_table_, FlushCache())
257 .WillOnce(Return(true));
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800258 connection_->SetIsDefault(true);
Paul Stewarte78ec542012-06-08 18:28:50 -0700259 Mock::VerifyAndClearExpectations(&routing_table_);
Paul Stewartdd60e452011-08-08 11:38:36 -0700260 EXPECT_TRUE(connection_->is_default());
261
Paul Stewart7cfca042011-12-08 14:18:17 -0800262 EXPECT_CALL(routing_table_,
263 SetDefaultMetric(kTestDeviceInterfaceIndex0,
Paul Stewart05a42c22012-08-02 16:47:21 -0700264 GetNonDefaultMetricBase() +
Paul Stewart7cfca042011-12-08 14:18:17 -0800265 kTestDeviceInterfaceIndex0));
Paul Stewarte78ec542012-06-08 18:28:50 -0700266 EXPECT_CALL(routing_table_, FlushCache())
267 .WillOnce(Return(true));
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800268 connection_->SetIsDefault(false);
Paul Stewartdd60e452011-08-08 11:38:36 -0700269 EXPECT_FALSE(connection_->is_default());
270}
271
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700272TEST_F(ConnectionTest, AddConfigWithPeer) {
273 const string kPeerAddress("192.168.1.222");
274 IPAddress peer_address(IPAddress::kFamilyIPv4);
275 EXPECT_TRUE(peer_address.SetAddressFromString(kPeerAddress));
276 properties_.peer_address = kPeerAddress;
277 properties_.gateway = string();
278 UpdateProperties();
Paul Stewart05a42c22012-08-02 16:47:21 -0700279 EXPECT_CALL(*device_info_,
280 HasOtherAddress(kTestDeviceInterfaceIndex0,
281 IsIPAddress(local_address_, kPrefix0)))
282 .WillOnce(Return(false));
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700283 EXPECT_CALL(rtnl_handler_,
284 AddInterfaceAddress(kTestDeviceInterfaceIndex0,
285 IsIPAddress(local_address_, kPrefix0),
286 IsIPAddress(broadcast_address_, 0),
287 IsIPAddress(peer_address, 0)));
288 EXPECT_CALL(routing_table_, SetDefaultRoute(_, _, _)).Times(0);
289 EXPECT_CALL(routing_table_,
290 ConfigureRoutes(kTestDeviceInterfaceIndex0,
291 ipconfig_,
Paul Stewart05a42c22012-08-02 16:47:21 -0700292 GetDefaultMetric()));
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700293 connection_->UpdateFromIPConfig(ipconfig_);
Paul Stewart4a6748d2012-07-17 14:31:36 -0700294 EXPECT_FALSE(GetHasBroadcastDomain(connection_));
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700295}
296
297TEST_F(ConnectionTest, AddConfigWithBrokenNetmask) {
298 // Assign a prefix that makes the gateway unreachable.
299 properties_.subnet_prefix = kPrefix1;
300 UpdateProperties();
301
302 // Connection should override with a prefix which will allow the
303 // gateway to be reachable.
Paul Stewart05a42c22012-08-02 16:47:21 -0700304 EXPECT_CALL(*device_info_,
305 HasOtherAddress(kTestDeviceInterfaceIndex0,
306 IsIPAddress(local_address_, kPrefix0)))
307 .WillOnce(Return(false));
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700308 EXPECT_CALL(rtnl_handler_,
309 AddInterfaceAddress(kTestDeviceInterfaceIndex0,
310 IsIPAddress(local_address_, kPrefix0),
311 IsIPAddress(broadcast_address_, 0),
312 IsIPAddress(default_address_, 0)));
313 EXPECT_CALL(routing_table_,
314 SetDefaultRoute(kTestDeviceInterfaceIndex0,
315 IsIPAddress(gateway_address_, 0),
Paul Stewart05a42c22012-08-02 16:47:21 -0700316 GetNonDefaultMetricBase() +
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700317 kTestDeviceInterfaceIndex0));
318 EXPECT_CALL(routing_table_,
319 ConfigureRoutes(kTestDeviceInterfaceIndex0,
320 ipconfig_,
Paul Stewart05a42c22012-08-02 16:47:21 -0700321 GetDefaultMetric()));
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700322 connection_->UpdateFromIPConfig(ipconfig_);
323
324 // Assign a gateway address that violates the minimum plausible prefix
325 // the Connection can assign.
326 properties_.gateway = kGatewayAddress1;
327 UpdateProperties();
328
Paul Stewart49258292012-05-26 06:37:14 -0700329 IPAddress gateway_address1(IPAddress::kFamilyIPv4);
330 EXPECT_TRUE(gateway_address1.SetAddressFromString(kGatewayAddress1));
331 // Connection cannot override this prefix, so it will switch to a
332 // model where the peer address is set to the value of the gateway
333 // address.
Paul Stewart05a42c22012-08-02 16:47:21 -0700334 EXPECT_CALL(*device_info_,
335 HasOtherAddress(kTestDeviceInterfaceIndex0,
336 IsIPAddress(local_address_, kPrefix1)))
337 .WillOnce(Return(false));
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700338 EXPECT_CALL(rtnl_handler_,
339 AddInterfaceAddress(kTestDeviceInterfaceIndex0,
340 IsIPAddress(local_address_, kPrefix1),
341 IsIPAddress(broadcast_address_, 0),
Paul Stewart49258292012-05-26 06:37:14 -0700342 IsIPAddress(gateway_address1, 0)));
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700343 EXPECT_CALL(routing_table_,
344 SetDefaultRoute(kTestDeviceInterfaceIndex0, _, _));
345 EXPECT_CALL(routing_table_,
346 ConfigureRoutes(kTestDeviceInterfaceIndex0, _, _));
347 connection_->UpdateFromIPConfig(ipconfig_);
348}
349
Paul Stewartdd60e452011-08-08 11:38:36 -0700350TEST_F(ConnectionTest, AddConfigReverse) {
351 EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0,
Paul Stewart05a42c22012-08-02 16:47:21 -0700352 GetDefaultMetric()));
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800353 vector<string> empty_list;
Paul Stewartbf667612012-06-29 14:49:54 -0700354 EXPECT_CALL(resolver_, SetDNSFromLists(empty_list, empty_list,
355 Resolver::kDefaultTimeout));
Paul Stewartc681fa02012-03-02 19:40:04 -0800356 scoped_refptr<MockDevice> device(new StrictMock<MockDevice>(
357 &control_,
358 reinterpret_cast<EventDispatcher *>(NULL),
359 reinterpret_cast<Metrics *>(NULL),
360 reinterpret_cast<Manager *>(NULL),
361 kTestDeviceName0,
362 string(),
363 kTestDeviceInterfaceIndex0));
364 EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0))
365 .WillOnce(Return(device));
366 EXPECT_CALL(*device.get(), RequestPortalDetection())
367 .WillOnce(Return(true));
Paul Stewarte78ec542012-06-08 18:28:50 -0700368 EXPECT_CALL(routing_table_, FlushCache())
369 .WillOnce(Return(true));
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800370 connection_->SetIsDefault(true);
Paul Stewarte78ec542012-06-08 18:28:50 -0700371 Mock::VerifyAndClearExpectations(&routing_table_);
Paul Stewartdd60e452011-08-08 11:38:36 -0700372
Paul Stewart05a42c22012-08-02 16:47:21 -0700373 EXPECT_CALL(*device_info_,
374 HasOtherAddress(kTestDeviceInterfaceIndex0,
375 IsIPAddress(local_address_, kPrefix0)))
376 .WillOnce(Return(false));
Paul Stewartdd60e452011-08-08 11:38:36 -0700377 EXPECT_CALL(rtnl_handler_,
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700378 AddInterfaceAddress(kTestDeviceInterfaceIndex0,
379 IsIPAddress(local_address_, kPrefix0),
380 IsIPAddress(broadcast_address_, 0),
381 IsIPAddress(default_address_, 0)));
Paul Stewartdd60e452011-08-08 11:38:36 -0700382 EXPECT_CALL(routing_table_, SetDefaultRoute(kTestDeviceInterfaceIndex0,
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700383 IsIPAddress(gateway_address_, 0),
Paul Stewart05a42c22012-08-02 16:47:21 -0700384 GetDefaultMetric()));
Paul Stewart3f68bb12012-03-15 13:33:10 -0700385 EXPECT_CALL(routing_table_,
386 ConfigureRoutes(kTestDeviceInterfaceIndex0,
387 ipconfig_,
Paul Stewart05a42c22012-08-02 16:47:21 -0700388 GetDefaultMetric()));
Paul Stewartbf667612012-06-29 14:49:54 -0700389 EXPECT_CALL(resolver_, SetDNSFromIPConfig(ipconfig_,
390 Resolver::kDefaultTimeout));
Paul Stewartdd60e452011-08-08 11:38:36 -0700391
392 connection_->UpdateFromIPConfig(ipconfig_);
393}
394
Paul Stewartbf667612012-06-29 14:49:54 -0700395TEST_F(ConnectionTest, AddConfigShortTimeout) {
396 ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex0,
397 kTestDeviceName0,
398 Technology::kUnknown,
399 device_info_.get(),
400 true));
401 ReplaceSingletons(connection);
402 EXPECT_CALL(*device_info_, HasOtherAddress(_, _)).WillOnce(Return(false));
403 EXPECT_CALL(rtnl_handler_, AddInterfaceAddress(_, _, _, _))
404 .WillRepeatedly(Return(true));
405 EXPECT_CALL(routing_table_, SetDefaultRoute(_, _, _))
406 .WillRepeatedly(Return(true));
407 EXPECT_CALL(routing_table_, ConfigureRoutes(_, _, _))
408 .WillRepeatedly(Return(true));
409 connection->UpdateFromIPConfig(ipconfig_);
410 EXPECT_CALL(routing_table_, SetDefaultMetric(_, _));
411 EXPECT_CALL(resolver_, SetDNSFromLists(
412 ipconfig_->properties().dns_servers,
413 ipconfig_->properties().domain_search,
414 Resolver::kShortTimeout));
415 scoped_refptr<MockDevice> device(new StrictMock<MockDevice>(
416 &control_,
417 reinterpret_cast<EventDispatcher *>(NULL),
418 reinterpret_cast<Metrics *>(NULL),
419 reinterpret_cast<Manager *>(NULL),
420 kTestDeviceName0,
421 string(),
422 kTestDeviceInterfaceIndex0));
423 EXPECT_CALL(*device_info_, GetDevice(_)).WillOnce(Return(device));
424 EXPECT_CALL(*device.get(), RequestPortalDetection()).WillOnce(Return(true));
425 EXPECT_CALL(routing_table_, FlushCache()).WillOnce(Return(true));
426 connection->SetIsDefault(true);
427 EXPECT_CALL(*device_info_, HasOtherAddress(_, _)).WillOnce(Return(false));
428 EXPECT_CALL(resolver_, SetDNSFromIPConfig(ipconfig_,
429 Resolver::kShortTimeout));
430 connection->UpdateFromIPConfig(ipconfig_);
431 AddDestructorExpectations();
432}
433
Paul Stewart05a42c22012-08-02 16:47:21 -0700434TEST_F(ConnectionTest, HasOtherAddress) {
435 EXPECT_CALL(*device_info_,
436 HasOtherAddress(kTestDeviceInterfaceIndex0,
437 IsIPAddress(local_address_, kPrefix0)))
438 .WillOnce(Return(true));
439 EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex0));
440 EXPECT_CALL(*device_info_, FlushAddresses(kTestDeviceInterfaceIndex0));
441 EXPECT_CALL(rtnl_handler_,
442 AddInterfaceAddress(kTestDeviceInterfaceIndex0,
443 IsIPAddress(local_address_, kPrefix0),
444 IsIPAddress(broadcast_address_, 0),
445 IsIPAddress(default_address_, 0)));
446 EXPECT_CALL(routing_table_,
447 SetDefaultRoute(kTestDeviceInterfaceIndex0,
448 IsIPAddress(gateway_address_, 0),
449 GetNonDefaultMetricBase() +
450 kTestDeviceInterfaceIndex0));
451 EXPECT_CALL(routing_table_,
452 ConfigureRoutes(kTestDeviceInterfaceIndex0,
453 ipconfig_,
454 GetDefaultMetric()));
455 connection_->UpdateFromIPConfig(ipconfig_);
456}
457
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800458TEST_F(ConnectionTest, RouteRequest) {
Darin Petkov13e6d552012-05-09 14:22:23 +0200459 ConnectionRefPtr connection = GetNewConnection();
Paul Stewartf748a362012-03-07 12:01:20 -0800460 scoped_refptr<MockDevice> device(new StrictMock<MockDevice>(
461 &control_,
462 reinterpret_cast<EventDispatcher *>(NULL),
463 reinterpret_cast<Metrics *>(NULL),
464 reinterpret_cast<Manager *>(NULL),
465 kTestDeviceName0,
466 string(),
467 kTestDeviceInterfaceIndex0));
468 EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0))
469 .WillRepeatedly(Return(device));
470 EXPECT_CALL(*device.get(), DisableReversePathFilter()).Times(1);
471 connection->RequestRouting();
472 connection->RequestRouting();
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800473
Paul Stewartf748a362012-03-07 12:01:20 -0800474 // The first release should only decrement the reference counter.
475 connection->ReleaseRouting();
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800476
Paul Stewartf748a362012-03-07 12:01:20 -0800477 // Another release will re-enable reverse-path filter.
478 EXPECT_CALL(*device.get(), EnableReversePathFilter());
479 EXPECT_CALL(routing_table_, FlushCache());
480 connection->ReleaseRouting();
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800481
Paul Stewartf748a362012-03-07 12:01:20 -0800482 // The destructor will remove the routes and addresses.
Darin Petkov13e6d552012-05-09 14:22:23 +0200483 AddDestructorExpectations();
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800484}
485
Paul Stewartdd60e452011-08-08 11:38:36 -0700486TEST_F(ConnectionTest, Destructor) {
Darin Petkov13e6d552012-05-09 14:22:23 +0200487 ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex1,
488 kTestDeviceName1,
489 Technology::kUnknown,
Paul Stewartbf667612012-06-29 14:49:54 -0700490 device_info_.get(),
491 false));
Darin Petkov13e6d552012-05-09 14:22:23 +0200492 connection->resolver_ = &resolver_;
493 connection->routing_table_ = &routing_table_;
494 connection->rtnl_handler_ = &rtnl_handler_;
Thieu Lefb46caf2012-03-08 11:57:15 -0800495 EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex1));
Paul Stewarte93b0382012-04-24 13:11:28 -0700496 EXPECT_CALL(routing_table_, FlushRoutesWithTag(kTestDeviceInterfaceIndex1));
Paul Stewart9a908082011-08-31 12:18:48 -0700497 EXPECT_CALL(*device_info_, FlushAddresses(kTestDeviceInterfaceIndex1));
Darin Petkov13e6d552012-05-09 14:22:23 +0200498 connection = NULL;
Paul Stewartdd60e452011-08-08 11:38:36 -0700499}
500
Paul Stewartf748a362012-03-07 12:01:20 -0800501TEST_F(ConnectionTest, RequestHostRoute) {
Darin Petkov13e6d552012-05-09 14:22:23 +0200502 ConnectionRefPtr connection = GetNewConnection();
Paul Stewartf748a362012-03-07 12:01:20 -0800503 IPAddress address(IPAddress::kFamilyIPv4);
504 ASSERT_TRUE(address.SetAddressFromString(kIPAddress0));
505 size_t prefix_len = address.GetLength() * 8;
Darin Petkov13e6d552012-05-09 14:22:23 +0200506 EXPECT_CALL(routing_table_,
507 RequestRouteToHost(IsIPAddress(address, prefix_len),
508 -1,
509 kTestDeviceInterfaceIndex0,
510 IsNonNullCallback()))
Paul Stewartf748a362012-03-07 12:01:20 -0800511 .WillOnce(Return(true));
512 EXPECT_TRUE(connection->RequestHostRoute(address));
513
514 // The destructor will remove the routes and addresses.
Darin Petkov13e6d552012-05-09 14:22:23 +0200515 AddDestructorExpectations();
Paul Stewarte93b0382012-04-24 13:11:28 -0700516}
517
518TEST_F(ConnectionTest, PinHostRoute) {
519 static const char kGateway[] = "10.242.2.13";
520 static const char kNetwork[] = "10.242.2.1";
521
Darin Petkov13e6d552012-05-09 14:22:23 +0200522 ConnectionRefPtr connection = GetNewConnection();
Paul Stewarte93b0382012-04-24 13:11:28 -0700523
524 IPConfig::Properties props;
525 props.address_family = IPAddress::kFamilyIPv4;
526 EXPECT_FALSE(PinHostRoute(connection, props));
527
528 props.gateway = kGateway;
529 EXPECT_FALSE(PinHostRoute(connection, props));
530
531 props.gateway.clear();
532 props.trusted_ip = "xxx";
533 EXPECT_FALSE(PinHostRoute(connection, props));
534
535 props.gateway = kGateway;
536 EXPECT_FALSE(PinHostRoute(connection, props));
537
538 props.trusted_ip = kNetwork;
539 IPAddress address(IPAddress::kFamilyIPv4);
540 ASSERT_TRUE(address.SetAddressFromString(kNetwork));
541 size_t prefix_len = address.GetLength() * 8;
542 EXPECT_CALL(routing_table_, RequestRouteToHost(
Darin Petkovabf6d282012-05-08 15:49:05 +0200543 IsIPAddress(address, prefix_len), -1, kTestDeviceInterfaceIndex0, _))
Paul Stewarte93b0382012-04-24 13:11:28 -0700544 .WillOnce(Return(false));
545 EXPECT_FALSE(PinHostRoute(connection, props));
546
547 EXPECT_CALL(routing_table_, RequestRouteToHost(
Darin Petkovabf6d282012-05-08 15:49:05 +0200548 IsIPAddress(address, prefix_len), -1, kTestDeviceInterfaceIndex0, _))
Paul Stewarte93b0382012-04-24 13:11:28 -0700549 .WillOnce(Return(true));
550 EXPECT_TRUE(PinHostRoute(connection, props));
551
552 // The destructor will remove the routes and addresses.
Darin Petkov13e6d552012-05-09 14:22:23 +0200553 AddDestructorExpectations();
Paul Stewartf748a362012-03-07 12:01:20 -0800554}
555
Paul Stewart53a30382012-04-26 09:06:59 -0700556TEST_F(ConnectionTest, FixGatewayReachability) {
557 static const char kLocal[] = "10.242.2.13";
558 IPAddress local(IPAddress::kFamilyIPv4);
559 ASSERT_TRUE(local.SetAddressFromString(kLocal));
560 const int kPrefix = 24;
561 local.set_prefix(kPrefix);
562 IPAddress gateway(IPAddress::kFamilyIPv4);
563 IPAddress peer(IPAddress::kFamilyIPv4);
564
565 // Should fail because no gateway is set.
Paul Stewart49258292012-05-26 06:37:14 -0700566 EXPECT_FALSE(Connection::FixGatewayReachability(&local, &peer, gateway));
Paul Stewart53a30382012-04-26 09:06:59 -0700567 EXPECT_EQ(kPrefix, local.prefix());
Paul Stewart49258292012-05-26 06:37:14 -0700568 EXPECT_FALSE(peer.IsValid());
Paul Stewart53a30382012-04-26 09:06:59 -0700569
570 // Should succeed because with the given prefix, this gateway is reachable.
571 static const char kReachableGateway[] = "10.242.2.14";
572 ASSERT_TRUE(gateway.SetAddressFromString(kReachableGateway));
Paul Stewart49258292012-05-26 06:37:14 -0700573 peer = IPAddress(IPAddress::kFamilyIPv4);
574 EXPECT_TRUE(Connection::FixGatewayReachability(&local, &peer, gateway));
Paul Stewart53a30382012-04-26 09:06:59 -0700575 // Prefix should remain unchanged.
576 EXPECT_EQ(kPrefix, local.prefix());
Paul Stewart49258292012-05-26 06:37:14 -0700577 // Peer should remain unchanged.
578 EXPECT_FALSE(peer.IsValid());
Paul Stewart53a30382012-04-26 09:06:59 -0700579
580 // Should succeed because we modified the prefix to match the gateway.
581 static const char kExpandableGateway[] = "10.242.3.14";
582 ASSERT_TRUE(gateway.SetAddressFromString(kExpandableGateway));
Paul Stewart49258292012-05-26 06:37:14 -0700583 peer = IPAddress(IPAddress::kFamilyIPv4);
584 EXPECT_TRUE(Connection::FixGatewayReachability(&local, &peer, gateway));
Paul Stewart53a30382012-04-26 09:06:59 -0700585 // Prefix should have opened up by 1 bit.
586 EXPECT_EQ(kPrefix - 1, local.prefix());
Paul Stewart49258292012-05-26 06:37:14 -0700587 // Peer should remain unchanged.
588 EXPECT_FALSE(peer.IsValid());
Paul Stewart53a30382012-04-26 09:06:59 -0700589
Paul Stewart2aa5d7d2012-06-21 22:16:54 -0700590 // Should change models to assuming point-to-point because we cannot
Paul Stewart49258292012-05-26 06:37:14 -0700591 // plausibly expand the prefix past 8.
Paul Stewart53a30382012-04-26 09:06:59 -0700592 local.set_prefix(kPrefix);
593 static const char kUnreachableGateway[] = "11.242.2.14";
594 ASSERT_TRUE(gateway.SetAddressFromString(kUnreachableGateway));
Paul Stewart49258292012-05-26 06:37:14 -0700595 peer = IPAddress(IPAddress::kFamilyIPv4);
596 EXPECT_TRUE(Connection::FixGatewayReachability(&local, &peer, gateway));
Paul Stewart53a30382012-04-26 09:06:59 -0700597 // Prefix should not have changed.
598 EXPECT_EQ(kPrefix, local.prefix());
Paul Stewart49258292012-05-26 06:37:14 -0700599 // Peer address should be set to the gateway address.
600 EXPECT_TRUE(peer.Equals(gateway));
Paul Stewart53a30382012-04-26 09:06:59 -0700601
Paul Stewart2aa5d7d2012-06-21 22:16:54 -0700602 // Should also use point-to-point model if the netmask is set to the
603 // "all-ones" addresss, even if this address could have been made
604 // accessible by plausibly changing the prefix.
605 const int kIPv4MaxPrefix =
606 IPAddress::GetMaxPrefixLength(IPAddress::kFamilyIPv4);
607 local.set_prefix(kIPv4MaxPrefix);
608 ASSERT_TRUE(gateway.SetAddressFromString(kExpandableGateway));
609 peer = IPAddress(IPAddress::kFamilyIPv4);
610 EXPECT_TRUE(Connection::FixGatewayReachability(&local, &peer, gateway));
611 // Prefix should not have changed.
612 EXPECT_EQ(kIPv4MaxPrefix, local.prefix());
613 // Peer address should be set to the gateway address.
614 EXPECT_TRUE(peer.Equals(gateway));
615
Paul Stewart49258292012-05-26 06:37:14 -0700616 // If this is a peer-to-peer interface and the peer matches the gateway,
617 // we should succeed.
Paul Stewart2aa5d7d2012-06-21 22:16:54 -0700618 local.set_prefix(kPrefix);
619 ASSERT_TRUE(gateway.SetAddressFromString(kUnreachableGateway));
Paul Stewart53a30382012-04-26 09:06:59 -0700620 ASSERT_TRUE(peer.SetAddressFromString(kUnreachableGateway));
Paul Stewart49258292012-05-26 06:37:14 -0700621 EXPECT_TRUE(Connection::FixGatewayReachability(&local, &peer, gateway));
Paul Stewart53a30382012-04-26 09:06:59 -0700622 EXPECT_EQ(kPrefix, local.prefix());
Paul Stewart49258292012-05-26 06:37:14 -0700623 EXPECT_TRUE(peer.Equals(gateway));
Paul Stewart53a30382012-04-26 09:06:59 -0700624
625 // If there is a peer specified and it does not match the gateway (even
626 // if it was reachable via netmask), we should fail.
627 ASSERT_TRUE(gateway.SetAddressFromString(kReachableGateway));
Paul Stewart49258292012-05-26 06:37:14 -0700628 EXPECT_FALSE(Connection::FixGatewayReachability(&local, &peer, gateway));
Paul Stewart53a30382012-04-26 09:06:59 -0700629 EXPECT_EQ(kPrefix, local.prefix());
Paul Stewart49258292012-05-26 06:37:14 -0700630 EXPECT_FALSE(peer.Equals(gateway));
Paul Stewart53a30382012-04-26 09:06:59 -0700631}
632
Darin Petkov13e6d552012-05-09 14:22:23 +0200633TEST_F(ConnectionTest, Binders) {
634 EXPECT_TRUE(connection_->binders_.empty());
635 DisconnectCallbackTarget target0;
636 DisconnectCallbackTarget target1;
637 DisconnectCallbackTarget target2;
638 DisconnectCallbackTarget target3;
639 Connection::Binder binder0("binder0", target0.callback());
640 Connection::Binder binder1("binder1", target1.callback());
641 Connection::Binder binder2("binder2", target2.callback());
642 Connection::Binder binder3("binder3", target3.callback());
643
644 binder0.Attach(connection_);
645 binder1.Attach(connection_);
646
647 EXPECT_CALL(target1, CallTarget()).Times(0);
648 binder1.Attach(connection_);
649
650 binder3.Attach(connection_);
651 binder2.Attach(connection_);
652
653 EXPECT_CALL(target3, CallTarget()).Times(0);
654 binder3.Attach(NULL);
655
656 ASSERT_EQ(3, connection_->binders_.size());
657 EXPECT_TRUE(connection_->binders_.at(0) == &binder0);
658 EXPECT_TRUE(connection_->binders_.at(1) == &binder1);
659 EXPECT_TRUE(connection_->binders_.at(2) == &binder2);
660
661 EXPECT_CALL(target0, CallTarget()).Times(1);
662 EXPECT_CALL(target1, CallTarget()).Times(1);
663 EXPECT_CALL(target2, CallTarget()).Times(1);
664 connection_->NotifyBindersOnDisconnect();
665 EXPECT_TRUE(connection_->binders_.empty());
666
667 // Should be a no-op.
668 connection_->NotifyBindersOnDisconnect();
669}
670
671TEST_F(ConnectionTest, Binder) {
672 // No connection should be bound initially.
673 Connection::Binder *binder = &connection_->lower_binder_;
674 EXPECT_EQ(connection_->interface_name(), binder->name_);
675 EXPECT_FALSE(binder->client_disconnect_callback_.is_null());
676 EXPECT_FALSE(binder->IsBound());
677
678 ConnectionRefPtr connection1 = GetNewConnection();
679 EXPECT_TRUE(connection1->binders_.empty());
680
681 // Bind lower |connection1| and check if it's bound.
682 binder->Attach(connection1);
683 EXPECT_TRUE(binder->IsBound());
684 EXPECT_EQ(connection1.get(), binder->connection().get());
685 ASSERT_FALSE(connection1->binders_.empty());
686 EXPECT_TRUE(binder == connection1->binders_.at(0));
687
688 // Unbind lower |connection1| and check if it's unbound.
689 binder->Attach(NULL);
690 EXPECT_FALSE(binder->IsBound());
691 EXPECT_TRUE(connection1->binders_.empty());
692
693 ConnectionRefPtr connection2 = GetNewConnection();
694
695 // Bind lower |connection1| to upper |connection2| and destroy the upper
696 // |connection2|. Make sure lower |connection1| is unbound (i.e., the
697 // disconnect callback is deregistered).
698 connection2->lower_binder_.Attach(connection1);
699 EXPECT_FALSE(connection1->binders_.empty());
700 AddDestructorExpectations();
701 connection2 = NULL;
702 EXPECT_TRUE(connection1->binders_.empty());
703
704 // Bind lower |connection1| to upper |connection_| and destroy lower
705 // |connection1|. Make sure lower |connection1| is unbound from upper
706 // |connection_| and upper |connection_|'s registered disconnect callbacks are
707 // run.
708 binder->Attach(connection1);
709 DisconnectCallbackTarget target;
710 Connection::Binder test_binder("from_test", target.callback());
711 test_binder.Attach(connection_);
712 EXPECT_CALL(target, CallTarget()).Times(1);
713 ASSERT_FALSE(connection_->binders_.empty());
714 AddDestructorExpectations();
715 connection1 = NULL;
716 EXPECT_FALSE(binder->IsBound());
717 EXPECT_FALSE(test_binder.IsBound());
718 EXPECT_TRUE(connection_->binders_.empty());
719
720 {
721 // Binding a connection to itself should be safe.
722 ConnectionRefPtr connection = GetNewConnection();
723
724 connection->lower_binder_.Attach(connection);
725
726 EXPECT_FALSE(connection->binders_.empty());
727
728 DisconnectCallbackTarget target;
729 Connection::Binder binder("test", target.callback());
730 binder.Attach(connection);
731
732 AddDestructorExpectations();
733 EXPECT_CALL(target, CallTarget()).Times(1);
734 connection = NULL;
735 }
Darin Petkov13e6d552012-05-09 14:22:23 +0200736 {
737 // Circular binding of multiple connections should be safe.
738 ConnectionRefPtr connection_a = GetNewConnection();
739 ConnectionRefPtr connection_b = GetNewConnection();
740
741 connection_a->lower_binder_.Attach(connection_b);
742 connection_b->lower_binder_.Attach(connection_a);
743
744 EXPECT_FALSE(connection_a->binders_.empty());
745 EXPECT_FALSE(connection_b->binders_.empty());
746
747 DisconnectCallbackTarget target_a;
748 DisconnectCallbackTarget target_b;
749 Connection::Binder binder_a("test_a", target_a.callback());
750 Connection::Binder binder_b("test_b", target_b.callback());
751 binder_a.Attach(connection_a);
752 binder_b.Attach(connection_b);
753
754 AddDestructorExpectations();
755 EXPECT_CALL(target_a, CallTarget()).Times(1);
756 EXPECT_CALL(target_b, CallTarget()).Times(1);
757 connection_b = NULL;
758
759 EXPECT_TRUE(connection_a->binders_.empty());
760
761 AddDestructorExpectations();
762 connection_a = NULL;
763 }
Darin Petkovef1f9fe2012-05-11 16:51:52 +0200764 {
765 // Test the weak pointer to the bound Connection. This is not a case that
766 // should occur but the weak pointer should handle it gracefully.
767 DisconnectCallbackTarget target;
768 Connection::Binder binder("test_weak", target.callback());
769 ConnectionRefPtr connection = GetNewConnection();
770 binder.Attach(connection);
771
772 // Make sure the connection doesn't notify the binder on destruction.
773 connection->binders_.clear();
774 AddDestructorExpectations();
775 EXPECT_CALL(target, CallTarget()).Times(0);
776 connection = NULL;
777
778 // Ensure no crash -- the weak pointer to connection should be NULL.
Darin Petkov5eb05422012-05-11 15:45:25 +0200779 EXPECT_FALSE(binder.connection());
Darin Petkovef1f9fe2012-05-11 16:51:52 +0200780 binder.Attach(NULL);
781 }
Darin Petkov13e6d552012-05-09 14:22:23 +0200782}
783
784TEST_F(ConnectionTest, OnRouteQueryResponse) {
785 Connection::Binder *binder = &connection_->lower_binder_;
786 ConnectionRefPtr connection = GetNewConnection();
787 scoped_refptr<MockDevice> device(new StrictMock<MockDevice>(
788 &control_,
789 reinterpret_cast<EventDispatcher *>(NULL),
790 reinterpret_cast<Metrics *>(NULL),
791 reinterpret_cast<Manager *>(NULL),
792 kTestDeviceName1,
793 string(),
794 kTestDeviceInterfaceIndex1));
795
796 // Make sure we unbind the old lower connection even if we can't lookup the
797 // lower connection device.
798 binder->Attach(connection);
799 scoped_refptr<MockDevice> null_device;
800 EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex1))
801 .WillOnce(Return(null_device));
802 connection_->OnRouteQueryResponse(
803 kTestDeviceInterfaceIndex1, RoutingTableEntry());
804 EXPECT_FALSE(binder->IsBound());
805
806 // Check for graceful handling of a device with no connection.
807 EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex1))
808 .WillOnce(Return(device));
809 connection_->OnRouteQueryResponse(
810 kTestDeviceInterfaceIndex1, RoutingTableEntry());
811 EXPECT_FALSE(binder->IsBound());
812
Paul Stewart4a6748d2012-07-17 14:31:36 -0700813 // Create a mock connection that will be used fo binding.
814 scoped_refptr<MockConnection> mock_connection(
815 new StrictMock<MockConnection>(device_info_.get()));
816 EXPECT_CALL(*device_info_.get(),
817 FlushAddresses(mock_connection->interface_index()));
818 const string kInterfaceName(kTestDeviceName0);
819 EXPECT_CALL(*mock_connection, interface_name())
820 .WillRepeatedly(ReturnRef(kInterfaceName));
821 device->connection_ = mock_connection;
Darin Petkov13e6d552012-05-09 14:22:23 +0200822 EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex1))
823 .WillOnce(Return(device));
Paul Stewart4a6748d2012-07-17 14:31:36 -0700824
825 // Check that the the binding process completes, causing its upper
826 // connection to create a gateway route.
827 EXPECT_CALL(*mock_connection, CreateGatewayRoute())
828 .WillOnce(Return(true));
Darin Petkov13e6d552012-05-09 14:22:23 +0200829 connection_->OnRouteQueryResponse(
830 kTestDeviceInterfaceIndex1, RoutingTableEntry());
Paul Stewart4a6748d2012-07-17 14:31:36 -0700831
832 // Check that the upper connection is bound to the lower connection.
Darin Petkov13e6d552012-05-09 14:22:23 +0200833 EXPECT_TRUE(binder->IsBound());
Paul Stewart4a6748d2012-07-17 14:31:36 -0700834 EXPECT_EQ(mock_connection.get(), binder->connection().get());
Darin Petkov13e6d552012-05-09 14:22:23 +0200835
836 device->connection_ = NULL;
837 AddDestructorExpectations();
838 connection = NULL;
839}
840
Paul Stewartdd60e452011-08-08 11:38:36 -0700841} // namespace shill