blob: fb3f18e90b10d2c1dd9292447839991b2aab09c0 [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";
Ben Chana6bfe872012-09-26 09:48:34 -070053} // namespace
Paul Stewartdd60e452011-08-08 11:38:36 -070054
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,
mukesh agrawal23ac6b72013-01-31 18:52:37 -080067 device_info_.get())),
Paul Stewart5b7ba8c2012-04-18 09:08:00 -070068 ipconfig_(new IPConfig(&control_, kTestDeviceName0)),
69 local_address_(IPAddress::kFamilyIPv4),
70 broadcast_address_(IPAddress::kFamilyIPv4),
71 gateway_address_(IPAddress::kFamilyIPv4),
72 default_address_(IPAddress::kFamilyIPv4) {}
Paul Stewartdd60e452011-08-08 11:38:36 -070073
74 virtual void SetUp() {
Paul Stewartc8f4bef2011-12-13 09:45:51 -080075 ReplaceSingletons(connection_);
Paul Stewart5b7ba8c2012-04-18 09:08:00 -070076 properties_.address = kIPAddress0;
77 properties_.subnet_prefix = kPrefix0;
78 properties_.gateway = kGatewayAddress0;
79 properties_.broadcast_address = kBroadcastAddress0;
80 properties_.dns_servers.push_back(kNameServer0);
81 properties_.dns_servers.push_back(kNameServer1);
82 properties_.domain_search.push_back(kSearchDomain0);
83 properties_.domain_search.push_back(kSearchDomain1);
84 properties_.address_family = IPAddress::kFamilyIPv4;
85 UpdateProperties();
86 EXPECT_TRUE(local_address_.SetAddressFromString(kIPAddress0));
87 EXPECT_TRUE(broadcast_address_.SetAddressFromString(kBroadcastAddress0));
88 EXPECT_TRUE(gateway_address_.SetAddressFromString(kGatewayAddress0));
Paul Stewartdd60e452011-08-08 11:38:36 -070089 }
90
Paul Stewart9a908082011-08-31 12:18:48 -070091 virtual void TearDown() {
Darin Petkov13e6d552012-05-09 14:22:23 +020092 AddDestructorExpectations();
Paul Stewarte93b0382012-04-24 13:11:28 -070093 connection_ = NULL;
Paul Stewart9a908082011-08-31 12:18:48 -070094 }
95
Paul Stewartc8f4bef2011-12-13 09:45:51 -080096 void ReplaceSingletons(ConnectionRefPtr connection) {
97 connection->resolver_ = &resolver_;
98 connection->routing_table_ = &routing_table_;
99 connection->rtnl_handler_ = &rtnl_handler_;
100 }
101
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700102 void UpdateProperties() {
Paul Stewartc5099532013-12-12 07:53:15 -0800103 ipconfig_->UpdateProperties(properties_);
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700104 }
105
Paul Stewarte93b0382012-04-24 13:11:28 -0700106 bool PinHostRoute(ConnectionRefPtr connection,
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800107 const IPAddress trusted_ip,
108 const IPAddress gateway) {
109 return connection->PinHostRoute(trusted_ip, gateway);
Paul Stewarte93b0382012-04-24 13:11:28 -0700110 }
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,
mukesh agrawal23ac6b72013-01-31 18:52:37 -0800159 device_info_.get()));
Darin Petkov13e6d552012-05-09 14:22:23 +0200160 ReplaceSingletons(connection);
161 return connection;
162 }
163
Paul Stewart9a908082011-08-31 12:18:48 -0700164 scoped_ptr<StrictMock<MockDeviceInfo> > device_info_;
Paul Stewartdd60e452011-08-08 11:38:36 -0700165 ConnectionRefPtr connection_;
166 MockControl control_;
167 IPConfigRefPtr ipconfig_;
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700168 IPConfig::Properties properties_;
169 IPAddress local_address_;
170 IPAddress broadcast_address_;
171 IPAddress gateway_address_;
172 IPAddress default_address_;
Paul Stewartdd60e452011-08-08 11:38:36 -0700173 StrictMock<MockResolver> resolver_;
174 StrictMock<MockRoutingTable> routing_table_;
175 StrictMock<MockRTNLHandler> rtnl_handler_;
176};
177
Darin Petkov13e6d552012-05-09 14:22:23 +0200178namespace {
Paul Stewartdd60e452011-08-08 11:38:36 -0700179
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700180MATCHER_P2(IsIPAddress, address, prefix, "") {
181 IPAddress match_address(address);
182 match_address.set_prefix(prefix);
183 return match_address.Equals(arg);
184}
185
Darin Petkov13e6d552012-05-09 14:22:23 +0200186MATCHER(IsNonNullCallback, "") {
187 return !arg.is_null();
188}
189
190} // namespace
191
192TEST_F(ConnectionTest, InitState) {
193 EXPECT_EQ(kTestDeviceInterfaceIndex0, connection_->interface_index_);
194 EXPECT_EQ(kTestDeviceName0, connection_->interface_name_);
195 EXPECT_FALSE(connection_->is_default());
196 EXPECT_FALSE(connection_->routing_request_count_);
197}
198
Paul Stewartdd60e452011-08-08 11:38:36 -0700199TEST_F(ConnectionTest, AddConfig) {
Paul Stewart05a42c22012-08-02 16:47:21 -0700200 EXPECT_CALL(*device_info_,
201 HasOtherAddress(kTestDeviceInterfaceIndex0,
202 IsIPAddress(local_address_, kPrefix0)))
203 .WillOnce(Return(false));
Paul Stewartdd60e452011-08-08 11:38:36 -0700204 EXPECT_CALL(rtnl_handler_,
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700205 AddInterfaceAddress(kTestDeviceInterfaceIndex0,
206 IsIPAddress(local_address_, kPrefix0),
207 IsIPAddress(broadcast_address_, 0),
208 IsIPAddress(default_address_, 0)));
Paul Stewart7cfca042011-12-08 14:18:17 -0800209 EXPECT_CALL(routing_table_,
210 SetDefaultRoute(kTestDeviceInterfaceIndex0,
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700211 IsIPAddress(gateway_address_, 0),
Paul Stewart05a42c22012-08-02 16:47:21 -0700212 GetNonDefaultMetricBase() +
Paul Stewart7cfca042011-12-08 14:18:17 -0800213 kTestDeviceInterfaceIndex0));
Paul Stewart3f68bb12012-03-15 13:33:10 -0700214 EXPECT_CALL(routing_table_,
215 ConfigureRoutes(kTestDeviceInterfaceIndex0,
216 ipconfig_,
Paul Stewart05a42c22012-08-02 16:47:21 -0700217 GetDefaultMetric()));
Paul Stewartdd60e452011-08-08 11:38:36 -0700218 connection_->UpdateFromIPConfig(ipconfig_);
Paul Stewart4a6748d2012-07-17 14:31:36 -0700219 IPAddress test_local_address(local_address_);
220 test_local_address.set_prefix(kPrefix0);
221 EXPECT_TRUE(test_local_address.Equals(GetLocalAddress(connection_)));
222 EXPECT_TRUE(gateway_address_.Equals(GetGatewayAddress(connection_)));
223 EXPECT_TRUE(GetHasBroadcastDomain(connection_));
224
225 EXPECT_CALL(routing_table_,
226 CreateLinkRoute(kTestDeviceInterfaceIndex0,
227 IsIPAddress(local_address_, kPrefix0),
228 IsIPAddress(gateway_address_, 0)))
229 .WillOnce(Return(true))
230 .WillOnce(Return(false));
231 EXPECT_TRUE(connection_->CreateGatewayRoute());
232 EXPECT_FALSE(connection_->CreateGatewayRoute());
233 connection_->has_broadcast_domain_ = false;
234 EXPECT_FALSE(connection_->CreateGatewayRoute());
Paul Stewartdd60e452011-08-08 11:38:36 -0700235
236 EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0,
Paul Stewart05a42c22012-08-02 16:47:21 -0700237 GetDefaultMetric()));
Paul Stewartdd60e452011-08-08 11:38:36 -0700238 EXPECT_CALL(resolver_, SetDNSFromLists(
239 ipconfig_->properties().dns_servers,
mukesh agrawal23ac6b72013-01-31 18:52:37 -0800240 ipconfig_->properties().domain_search));
Paul Stewartdd60e452011-08-08 11:38:36 -0700241
Paul Stewartc681fa02012-03-02 19:40:04 -0800242 scoped_refptr<MockDevice> device(new StrictMock<MockDevice>(
243 &control_,
244 reinterpret_cast<EventDispatcher *>(NULL),
245 reinterpret_cast<Metrics *>(NULL),
246 reinterpret_cast<Manager *>(NULL),
247 kTestDeviceName0,
248 string(),
249 kTestDeviceInterfaceIndex0));
250 EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0))
251 .WillOnce(Return(device));
252 EXPECT_CALL(*device.get(), RequestPortalDetection())
253 .WillOnce(Return(true));
Paul Stewarte78ec542012-06-08 18:28:50 -0700254 EXPECT_CALL(routing_table_, FlushCache())
255 .WillOnce(Return(true));
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800256 connection_->SetIsDefault(true);
Paul Stewarte78ec542012-06-08 18:28:50 -0700257 Mock::VerifyAndClearExpectations(&routing_table_);
Paul Stewartdd60e452011-08-08 11:38:36 -0700258 EXPECT_TRUE(connection_->is_default());
259
Paul Stewart7cfca042011-12-08 14:18:17 -0800260 EXPECT_CALL(routing_table_,
261 SetDefaultMetric(kTestDeviceInterfaceIndex0,
Paul Stewart05a42c22012-08-02 16:47:21 -0700262 GetNonDefaultMetricBase() +
Paul Stewart7cfca042011-12-08 14:18:17 -0800263 kTestDeviceInterfaceIndex0));
Paul Stewarte78ec542012-06-08 18:28:50 -0700264 EXPECT_CALL(routing_table_, FlushCache())
265 .WillOnce(Return(true));
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800266 connection_->SetIsDefault(false);
Paul Stewartdd60e452011-08-08 11:38:36 -0700267 EXPECT_FALSE(connection_->is_default());
268}
269
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700270TEST_F(ConnectionTest, AddConfigWithPeer) {
271 const string kPeerAddress("192.168.1.222");
272 IPAddress peer_address(IPAddress::kFamilyIPv4);
273 EXPECT_TRUE(peer_address.SetAddressFromString(kPeerAddress));
274 properties_.peer_address = kPeerAddress;
275 properties_.gateway = string();
276 UpdateProperties();
Paul Stewart05a42c22012-08-02 16:47:21 -0700277 EXPECT_CALL(*device_info_,
278 HasOtherAddress(kTestDeviceInterfaceIndex0,
279 IsIPAddress(local_address_, kPrefix0)))
280 .WillOnce(Return(false));
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700281 EXPECT_CALL(rtnl_handler_,
282 AddInterfaceAddress(kTestDeviceInterfaceIndex0,
283 IsIPAddress(local_address_, kPrefix0),
284 IsIPAddress(broadcast_address_, 0),
285 IsIPAddress(peer_address, 0)));
286 EXPECT_CALL(routing_table_, SetDefaultRoute(_, _, _)).Times(0);
287 EXPECT_CALL(routing_table_,
288 ConfigureRoutes(kTestDeviceInterfaceIndex0,
289 ipconfig_,
Paul Stewart05a42c22012-08-02 16:47:21 -0700290 GetDefaultMetric()));
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700291 connection_->UpdateFromIPConfig(ipconfig_);
Paul Stewart4a6748d2012-07-17 14:31:36 -0700292 EXPECT_FALSE(GetHasBroadcastDomain(connection_));
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700293}
294
295TEST_F(ConnectionTest, AddConfigWithBrokenNetmask) {
296 // Assign a prefix that makes the gateway unreachable.
297 properties_.subnet_prefix = kPrefix1;
298 UpdateProperties();
299
300 // Connection should override with a prefix which will allow the
301 // gateway to be reachable.
Paul Stewart05a42c22012-08-02 16:47:21 -0700302 EXPECT_CALL(*device_info_,
303 HasOtherAddress(kTestDeviceInterfaceIndex0,
304 IsIPAddress(local_address_, kPrefix0)))
305 .WillOnce(Return(false));
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700306 EXPECT_CALL(rtnl_handler_,
307 AddInterfaceAddress(kTestDeviceInterfaceIndex0,
308 IsIPAddress(local_address_, kPrefix0),
309 IsIPAddress(broadcast_address_, 0),
310 IsIPAddress(default_address_, 0)));
311 EXPECT_CALL(routing_table_,
312 SetDefaultRoute(kTestDeviceInterfaceIndex0,
313 IsIPAddress(gateway_address_, 0),
Paul Stewart05a42c22012-08-02 16:47:21 -0700314 GetNonDefaultMetricBase() +
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700315 kTestDeviceInterfaceIndex0));
316 EXPECT_CALL(routing_table_,
317 ConfigureRoutes(kTestDeviceInterfaceIndex0,
318 ipconfig_,
Paul Stewart05a42c22012-08-02 16:47:21 -0700319 GetDefaultMetric()));
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700320 connection_->UpdateFromIPConfig(ipconfig_);
321
322 // Assign a gateway address that violates the minimum plausible prefix
323 // the Connection can assign.
324 properties_.gateway = kGatewayAddress1;
325 UpdateProperties();
326
Paul Stewart49258292012-05-26 06:37:14 -0700327 IPAddress gateway_address1(IPAddress::kFamilyIPv4);
328 EXPECT_TRUE(gateway_address1.SetAddressFromString(kGatewayAddress1));
329 // Connection cannot override this prefix, so it will switch to a
330 // model where the peer address is set to the value of the gateway
331 // address.
Paul Stewart05a42c22012-08-02 16:47:21 -0700332 EXPECT_CALL(*device_info_,
333 HasOtherAddress(kTestDeviceInterfaceIndex0,
334 IsIPAddress(local_address_, kPrefix1)))
335 .WillOnce(Return(false));
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700336 EXPECT_CALL(rtnl_handler_,
337 AddInterfaceAddress(kTestDeviceInterfaceIndex0,
338 IsIPAddress(local_address_, kPrefix1),
339 IsIPAddress(broadcast_address_, 0),
Paul Stewart49258292012-05-26 06:37:14 -0700340 IsIPAddress(gateway_address1, 0)));
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700341 EXPECT_CALL(routing_table_,
342 SetDefaultRoute(kTestDeviceInterfaceIndex0, _, _));
343 EXPECT_CALL(routing_table_,
344 ConfigureRoutes(kTestDeviceInterfaceIndex0, _, _));
345 connection_->UpdateFromIPConfig(ipconfig_);
346}
347
Paul Stewartdd60e452011-08-08 11:38:36 -0700348TEST_F(ConnectionTest, AddConfigReverse) {
349 EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0,
Paul Stewart05a42c22012-08-02 16:47:21 -0700350 GetDefaultMetric()));
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800351 vector<string> empty_list;
mukesh agrawal23ac6b72013-01-31 18:52:37 -0800352 EXPECT_CALL(resolver_, SetDNSFromLists(empty_list, empty_list));
Paul Stewartc681fa02012-03-02 19:40:04 -0800353 scoped_refptr<MockDevice> device(new StrictMock<MockDevice>(
354 &control_,
355 reinterpret_cast<EventDispatcher *>(NULL),
356 reinterpret_cast<Metrics *>(NULL),
357 reinterpret_cast<Manager *>(NULL),
358 kTestDeviceName0,
359 string(),
360 kTestDeviceInterfaceIndex0));
361 EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0))
362 .WillOnce(Return(device));
363 EXPECT_CALL(*device.get(), RequestPortalDetection())
364 .WillOnce(Return(true));
Paul Stewarte78ec542012-06-08 18:28:50 -0700365 EXPECT_CALL(routing_table_, FlushCache())
366 .WillOnce(Return(true));
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800367 connection_->SetIsDefault(true);
Paul Stewarte78ec542012-06-08 18:28:50 -0700368 Mock::VerifyAndClearExpectations(&routing_table_);
Paul Stewartdd60e452011-08-08 11:38:36 -0700369
Paul Stewart05a42c22012-08-02 16:47:21 -0700370 EXPECT_CALL(*device_info_,
371 HasOtherAddress(kTestDeviceInterfaceIndex0,
372 IsIPAddress(local_address_, kPrefix0)))
373 .WillOnce(Return(false));
Paul Stewartdd60e452011-08-08 11:38:36 -0700374 EXPECT_CALL(rtnl_handler_,
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700375 AddInterfaceAddress(kTestDeviceInterfaceIndex0,
376 IsIPAddress(local_address_, kPrefix0),
377 IsIPAddress(broadcast_address_, 0),
378 IsIPAddress(default_address_, 0)));
Paul Stewartdd60e452011-08-08 11:38:36 -0700379 EXPECT_CALL(routing_table_, SetDefaultRoute(kTestDeviceInterfaceIndex0,
Paul Stewart5b7ba8c2012-04-18 09:08:00 -0700380 IsIPAddress(gateway_address_, 0),
Paul Stewart05a42c22012-08-02 16:47:21 -0700381 GetDefaultMetric()));
Paul Stewart3f68bb12012-03-15 13:33:10 -0700382 EXPECT_CALL(routing_table_,
383 ConfigureRoutes(kTestDeviceInterfaceIndex0,
384 ipconfig_,
Paul Stewart05a42c22012-08-02 16:47:21 -0700385 GetDefaultMetric()));
mukesh agrawal23ac6b72013-01-31 18:52:37 -0800386 EXPECT_CALL(resolver_,
387 SetDNSFromLists(ipconfig_->properties().dns_servers,
388 ipconfig_->properties().domain_search));
Paul Stewartdd60e452011-08-08 11:38:36 -0700389
390 connection_->UpdateFromIPConfig(ipconfig_);
391}
392
Paul Stewart4d1868b2012-09-10 11:58:46 -0700393TEST_F(ConnectionTest, AddConfigWithDNSDomain) {
394 const string kDomainName("chromium.org");
395 properties_.domain_search.clear();
396 properties_.domain_name = kDomainName;
397 UpdateProperties();
398 EXPECT_CALL(*device_info_, HasOtherAddress(_, _))
399 .WillOnce(Return(false));
400 EXPECT_CALL(rtnl_handler_, AddInterfaceAddress(_, _, _, _));
401 EXPECT_CALL(routing_table_, SetDefaultRoute(_, _, _));
402 EXPECT_CALL(routing_table_, ConfigureRoutes(_, _, _));
403 connection_->UpdateFromIPConfig(ipconfig_);
404
405 EXPECT_CALL(routing_table_, SetDefaultMetric(_, _));
406 vector<string> domain_search_list;
407 domain_search_list.push_back(kDomainName + ".");
mukesh agrawal23ac6b72013-01-31 18:52:37 -0800408 EXPECT_CALL(resolver_, SetDNSFromLists(_, domain_search_list));
Paul Stewart4d1868b2012-09-10 11:58:46 -0700409 DeviceRefPtr device;
410 EXPECT_CALL(*device_info_, GetDevice(_)).WillOnce(Return(device));
411 EXPECT_CALL(routing_table_, FlushCache()).WillOnce(Return(true));
412 connection_->SetIsDefault(true);
413}
414
Paul Stewart05a42c22012-08-02 16:47:21 -0700415TEST_F(ConnectionTest, HasOtherAddress) {
416 EXPECT_CALL(*device_info_,
417 HasOtherAddress(kTestDeviceInterfaceIndex0,
418 IsIPAddress(local_address_, kPrefix0)))
419 .WillOnce(Return(true));
420 EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex0));
421 EXPECT_CALL(*device_info_, FlushAddresses(kTestDeviceInterfaceIndex0));
422 EXPECT_CALL(rtnl_handler_,
423 AddInterfaceAddress(kTestDeviceInterfaceIndex0,
424 IsIPAddress(local_address_, kPrefix0),
425 IsIPAddress(broadcast_address_, 0),
426 IsIPAddress(default_address_, 0)));
427 EXPECT_CALL(routing_table_,
428 SetDefaultRoute(kTestDeviceInterfaceIndex0,
429 IsIPAddress(gateway_address_, 0),
430 GetNonDefaultMetricBase() +
431 kTestDeviceInterfaceIndex0));
432 EXPECT_CALL(routing_table_,
433 ConfigureRoutes(kTestDeviceInterfaceIndex0,
434 ipconfig_,
435 GetDefaultMetric()));
436 connection_->UpdateFromIPConfig(ipconfig_);
437}
438
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800439TEST_F(ConnectionTest, RouteRequest) {
Darin Petkov13e6d552012-05-09 14:22:23 +0200440 ConnectionRefPtr connection = GetNewConnection();
Paul Stewartf748a362012-03-07 12:01:20 -0800441 scoped_refptr<MockDevice> device(new StrictMock<MockDevice>(
442 &control_,
443 reinterpret_cast<EventDispatcher *>(NULL),
444 reinterpret_cast<Metrics *>(NULL),
445 reinterpret_cast<Manager *>(NULL),
446 kTestDeviceName0,
447 string(),
448 kTestDeviceInterfaceIndex0));
449 EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0))
450 .WillRepeatedly(Return(device));
451 EXPECT_CALL(*device.get(), DisableReversePathFilter()).Times(1);
452 connection->RequestRouting();
453 connection->RequestRouting();
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800454
Paul Stewartf748a362012-03-07 12:01:20 -0800455 // The first release should only decrement the reference counter.
456 connection->ReleaseRouting();
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800457
Paul Stewartf748a362012-03-07 12:01:20 -0800458 // Another release will re-enable reverse-path filter.
459 EXPECT_CALL(*device.get(), EnableReversePathFilter());
460 EXPECT_CALL(routing_table_, FlushCache());
461 connection->ReleaseRouting();
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800462
Paul Stewartf748a362012-03-07 12:01:20 -0800463 // The destructor will remove the routes and addresses.
Darin Petkov13e6d552012-05-09 14:22:23 +0200464 AddDestructorExpectations();
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800465}
466
Paul Stewartdd60e452011-08-08 11:38:36 -0700467TEST_F(ConnectionTest, Destructor) {
Darin Petkov13e6d552012-05-09 14:22:23 +0200468 ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex1,
469 kTestDeviceName1,
470 Technology::kUnknown,
mukesh agrawal23ac6b72013-01-31 18:52:37 -0800471 device_info_.get()));
Darin Petkov13e6d552012-05-09 14:22:23 +0200472 connection->resolver_ = &resolver_;
473 connection->routing_table_ = &routing_table_;
474 connection->rtnl_handler_ = &rtnl_handler_;
Thieu Lefb46caf2012-03-08 11:57:15 -0800475 EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex1));
Paul Stewarte93b0382012-04-24 13:11:28 -0700476 EXPECT_CALL(routing_table_, FlushRoutesWithTag(kTestDeviceInterfaceIndex1));
Paul Stewart9a908082011-08-31 12:18:48 -0700477 EXPECT_CALL(*device_info_, FlushAddresses(kTestDeviceInterfaceIndex1));
Darin Petkov13e6d552012-05-09 14:22:23 +0200478 connection = NULL;
Paul Stewartdd60e452011-08-08 11:38:36 -0700479}
480
Paul Stewartf748a362012-03-07 12:01:20 -0800481TEST_F(ConnectionTest, RequestHostRoute) {
Darin Petkov13e6d552012-05-09 14:22:23 +0200482 ConnectionRefPtr connection = GetNewConnection();
Paul Stewartf748a362012-03-07 12:01:20 -0800483 IPAddress address(IPAddress::kFamilyIPv4);
484 ASSERT_TRUE(address.SetAddressFromString(kIPAddress0));
485 size_t prefix_len = address.GetLength() * 8;
Darin Petkov13e6d552012-05-09 14:22:23 +0200486 EXPECT_CALL(routing_table_,
487 RequestRouteToHost(IsIPAddress(address, prefix_len),
488 -1,
489 kTestDeviceInterfaceIndex0,
490 IsNonNullCallback()))
Paul Stewartf748a362012-03-07 12:01:20 -0800491 .WillOnce(Return(true));
492 EXPECT_TRUE(connection->RequestHostRoute(address));
493
494 // The destructor will remove the routes and addresses.
Darin Petkov13e6d552012-05-09 14:22:23 +0200495 AddDestructorExpectations();
Paul Stewarte93b0382012-04-24 13:11:28 -0700496}
497
Ben Chana0163122012-09-25 15:10:52 -0700498TEST_F(ConnectionTest, BlackholeIPv6) {
499 properties_.blackhole_ipv6 = true;
500 UpdateProperties();
501 EXPECT_CALL(*device_info_, HasOtherAddress(_, _))
502 .WillOnce(Return(false));
503 EXPECT_CALL(rtnl_handler_, AddInterfaceAddress(_, _, _, _));
504 EXPECT_CALL(routing_table_, SetDefaultRoute(_, _, _));
505 EXPECT_CALL(routing_table_, ConfigureRoutes(_, _, _));
506 EXPECT_CALL(routing_table_,
507 CreateBlackholeRoute(kTestDeviceInterfaceIndex0,
508 IPAddress::kFamilyIPv6,
509 Connection::kDefaultMetric))
510 .WillOnce(Return(true));
511 connection_->UpdateFromIPConfig(ipconfig_);
512}
513
Paul Stewarte93b0382012-04-24 13:11:28 -0700514TEST_F(ConnectionTest, PinHostRoute) {
Darin Petkov13e6d552012-05-09 14:22:23 +0200515 ConnectionRefPtr connection = GetNewConnection();
Paul Stewarte93b0382012-04-24 13:11:28 -0700516
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800517 IPAddress gateway(IPAddress::kFamilyIPv4);
518 IPAddress trusted_ip(IPAddress::kFamilyIPv4);
Paul Stewarte93b0382012-04-24 13:11:28 -0700519
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800520 // Should fail because neither IP address is set.
521 EXPECT_FALSE(PinHostRoute(connection, trusted_ip, gateway));
Paul Stewarte93b0382012-04-24 13:11:28 -0700522
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800523 static const char kGateway[] = "10.242.2.13";
524 ASSERT_TRUE(gateway.SetAddressFromString(kGateway));
Paul Stewarte93b0382012-04-24 13:11:28 -0700525
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800526 // Should fail because trusted IP is not set.
527 EXPECT_FALSE(PinHostRoute(connection, trusted_ip, gateway));
Paul Stewarte93b0382012-04-24 13:11:28 -0700528
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800529 static const char kTrustedIP[] = "10.0.1.1";
530 ASSERT_TRUE(trusted_ip.SetAddressFromString(kTrustedIP));
531
Paul Stewarte435d342013-09-27 16:41:00 -0700532 // Should pass without calling RequestRouteToHost since if the gateway
533 // is not set, there is no work to be done.
534 EXPECT_CALL(routing_table_, RequestRouteToHost(_, _, _, _)).Times(0);
535 EXPECT_TRUE(PinHostRoute(connection, trusted_ip,
536 IPAddress(gateway.family())));
537 Mock::VerifyAndClearExpectations(&routing_table_);
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800538
539 size_t prefix_len = IPAddress::GetMaxPrefixLength(trusted_ip.family());
Paul Stewarte93b0382012-04-24 13:11:28 -0700540 EXPECT_CALL(routing_table_, RequestRouteToHost(
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800541 IsIPAddress(trusted_ip, prefix_len), -1, kTestDeviceInterfaceIndex0, _))
Paul Stewarte93b0382012-04-24 13:11:28 -0700542 .WillOnce(Return(false));
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800543 EXPECT_FALSE(PinHostRoute(connection, trusted_ip, gateway));
Paul Stewarte93b0382012-04-24 13:11:28 -0700544
545 EXPECT_CALL(routing_table_, RequestRouteToHost(
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800546 IsIPAddress(trusted_ip, prefix_len), -1, kTestDeviceInterfaceIndex0, _))
Paul Stewarte93b0382012-04-24 13:11:28 -0700547 .WillOnce(Return(true));
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800548 EXPECT_TRUE(PinHostRoute(connection, trusted_ip, gateway));
Paul Stewarte93b0382012-04-24 13:11:28 -0700549
550 // The destructor will remove the routes and addresses.
Darin Petkov13e6d552012-05-09 14:22:23 +0200551 AddDestructorExpectations();
Paul Stewartf748a362012-03-07 12:01:20 -0800552}
553
Paul Stewart53a30382012-04-26 09:06:59 -0700554TEST_F(ConnectionTest, FixGatewayReachability) {
555 static const char kLocal[] = "10.242.2.13";
556 IPAddress local(IPAddress::kFamilyIPv4);
557 ASSERT_TRUE(local.SetAddressFromString(kLocal));
558 const int kPrefix = 24;
559 local.set_prefix(kPrefix);
560 IPAddress gateway(IPAddress::kFamilyIPv4);
561 IPAddress peer(IPAddress::kFamilyIPv4);
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800562 IPAddress trusted_ip(IPAddress::kFamilyIPv4);
Paul Stewart53a30382012-04-26 09:06:59 -0700563
564 // Should fail because no gateway is set.
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800565 EXPECT_FALSE(Connection::FixGatewayReachability(
566 &local, &peer, &gateway, trusted_ip));
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 Stewart73fcc3f2013-02-25 12:16:53 -0800569 EXPECT_FALSE(gateway.IsValid());
Paul Stewart53a30382012-04-26 09:06:59 -0700570
571 // Should succeed because with the given prefix, this gateway is reachable.
572 static const char kReachableGateway[] = "10.242.2.14";
573 ASSERT_TRUE(gateway.SetAddressFromString(kReachableGateway));
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800574 IPAddress gateway_backup(gateway);
Paul Stewart49258292012-05-26 06:37:14 -0700575 peer = IPAddress(IPAddress::kFamilyIPv4);
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800576 EXPECT_TRUE(Connection::FixGatewayReachability(
577 &local, &peer, &gateway, trusted_ip));
Paul Stewart53a30382012-04-26 09:06:59 -0700578 // Prefix should remain unchanged.
579 EXPECT_EQ(kPrefix, local.prefix());
Paul Stewart49258292012-05-26 06:37:14 -0700580 // Peer should remain unchanged.
581 EXPECT_FALSE(peer.IsValid());
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800582 // Gateway should remain unchanged.
583 EXPECT_TRUE(gateway_backup.Equals(gateway));
Paul Stewart53a30382012-04-26 09:06:59 -0700584
585 // Should succeed because we modified the prefix to match the gateway.
586 static const char kExpandableGateway[] = "10.242.3.14";
587 ASSERT_TRUE(gateway.SetAddressFromString(kExpandableGateway));
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800588 gateway_backup = gateway;
Paul Stewart49258292012-05-26 06:37:14 -0700589 peer = IPAddress(IPAddress::kFamilyIPv4);
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800590 EXPECT_TRUE(Connection::FixGatewayReachability(
591 &local, &peer, &gateway, trusted_ip));
Paul Stewart53a30382012-04-26 09:06:59 -0700592 // Prefix should have opened up by 1 bit.
593 EXPECT_EQ(kPrefix - 1, local.prefix());
Paul Stewart49258292012-05-26 06:37:14 -0700594 // Peer should remain unchanged.
595 EXPECT_FALSE(peer.IsValid());
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800596 // Gateway should remain unchanged.
597 EXPECT_TRUE(gateway_backup.Equals(gateway));
Paul Stewart53a30382012-04-26 09:06:59 -0700598
Paul Stewart2aa5d7d2012-06-21 22:16:54 -0700599 // Should change models to assuming point-to-point because we cannot
Paul Stewart49258292012-05-26 06:37:14 -0700600 // plausibly expand the prefix past 8.
Paul Stewart53a30382012-04-26 09:06:59 -0700601 local.set_prefix(kPrefix);
602 static const char kUnreachableGateway[] = "11.242.2.14";
603 ASSERT_TRUE(gateway.SetAddressFromString(kUnreachableGateway));
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800604 gateway_backup = gateway;
Paul Stewart49258292012-05-26 06:37:14 -0700605 peer = IPAddress(IPAddress::kFamilyIPv4);
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800606 EXPECT_TRUE(Connection::FixGatewayReachability(
607 &local, &peer, &gateway, trusted_ip));
Paul Stewart53a30382012-04-26 09:06:59 -0700608 // Prefix should not have changed.
609 EXPECT_EQ(kPrefix, local.prefix());
Paul Stewart49258292012-05-26 06:37:14 -0700610 // Peer address should be set to the gateway address.
611 EXPECT_TRUE(peer.Equals(gateway));
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800612 // Gateway should remain unchanged.
613 EXPECT_TRUE(gateway_backup.Equals(gateway));
Paul Stewart53a30382012-04-26 09:06:59 -0700614
Paul Stewart2aa5d7d2012-06-21 22:16:54 -0700615 // Should also use point-to-point model if the netmask is set to the
616 // "all-ones" addresss, even if this address could have been made
617 // accessible by plausibly changing the prefix.
618 const int kIPv4MaxPrefix =
619 IPAddress::GetMaxPrefixLength(IPAddress::kFamilyIPv4);
620 local.set_prefix(kIPv4MaxPrefix);
621 ASSERT_TRUE(gateway.SetAddressFromString(kExpandableGateway));
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800622 gateway_backup = gateway;
Paul Stewart2aa5d7d2012-06-21 22:16:54 -0700623 peer = IPAddress(IPAddress::kFamilyIPv4);
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800624 EXPECT_TRUE(Connection::FixGatewayReachability(
625 &local, &peer, &gateway, trusted_ip));
Paul Stewart2aa5d7d2012-06-21 22:16:54 -0700626 // Prefix should not have changed.
627 EXPECT_EQ(kIPv4MaxPrefix, local.prefix());
628 // Peer address should be set to the gateway address.
629 EXPECT_TRUE(peer.Equals(gateway));
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800630 // Gateway should remain unchanged.
631 EXPECT_TRUE(gateway_backup.Equals(gateway));
Paul Stewart2aa5d7d2012-06-21 22:16:54 -0700632
Paul Stewart49258292012-05-26 06:37:14 -0700633 // If this is a peer-to-peer interface and the peer matches the gateway,
634 // we should succeed.
Paul Stewart2aa5d7d2012-06-21 22:16:54 -0700635 local.set_prefix(kPrefix);
636 ASSERT_TRUE(gateway.SetAddressFromString(kUnreachableGateway));
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800637 gateway_backup = gateway;
Paul Stewart53a30382012-04-26 09:06:59 -0700638 ASSERT_TRUE(peer.SetAddressFromString(kUnreachableGateway));
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800639 EXPECT_TRUE(Connection::FixGatewayReachability(
640 &local, &peer, &gateway, trusted_ip));
Paul Stewart53a30382012-04-26 09:06:59 -0700641 EXPECT_EQ(kPrefix, local.prefix());
Paul Stewart49258292012-05-26 06:37:14 -0700642 EXPECT_TRUE(peer.Equals(gateway));
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800643 EXPECT_TRUE(gateway_backup.Equals(gateway));
Paul Stewart53a30382012-04-26 09:06:59 -0700644
645 // If there is a peer specified and it does not match the gateway (even
646 // if it was reachable via netmask), we should fail.
647 ASSERT_TRUE(gateway.SetAddressFromString(kReachableGateway));
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800648 EXPECT_FALSE(Connection::FixGatewayReachability(
649 &local, &peer, &gateway, trusted_ip));
Paul Stewart53a30382012-04-26 09:06:59 -0700650 EXPECT_EQ(kPrefix, local.prefix());
Paul Stewart49258292012-05-26 06:37:14 -0700651 EXPECT_FALSE(peer.Equals(gateway));
Paul Stewart73fcc3f2013-02-25 12:16:53 -0800652
653 // If this is a peer-to-peer interface and the peer matches the gateway,
654 // but it also matches the trusted IP address, the gateway and peer address
655 // should be modified to allow routing to work correctly.
656 ASSERT_TRUE(gateway.SetAddressFromString(kUnreachableGateway));
657 ASSERT_TRUE(peer.SetAddressFromString(kUnreachableGateway));
658 ASSERT_TRUE(trusted_ip.SetAddressFromString(kUnreachableGateway));
659 EXPECT_TRUE(Connection::FixGatewayReachability(
660 &local, &peer, &gateway, trusted_ip));
661 EXPECT_TRUE(peer.IsDefault());
662 EXPECT_TRUE(gateway.IsDefault());
Paul Stewart53a30382012-04-26 09:06:59 -0700663}
664
Darin Petkov13e6d552012-05-09 14:22:23 +0200665TEST_F(ConnectionTest, Binders) {
666 EXPECT_TRUE(connection_->binders_.empty());
667 DisconnectCallbackTarget target0;
668 DisconnectCallbackTarget target1;
669 DisconnectCallbackTarget target2;
670 DisconnectCallbackTarget target3;
671 Connection::Binder binder0("binder0", target0.callback());
672 Connection::Binder binder1("binder1", target1.callback());
673 Connection::Binder binder2("binder2", target2.callback());
674 Connection::Binder binder3("binder3", target3.callback());
675
676 binder0.Attach(connection_);
677 binder1.Attach(connection_);
678
679 EXPECT_CALL(target1, CallTarget()).Times(0);
680 binder1.Attach(connection_);
681
682 binder3.Attach(connection_);
683 binder2.Attach(connection_);
684
685 EXPECT_CALL(target3, CallTarget()).Times(0);
686 binder3.Attach(NULL);
687
688 ASSERT_EQ(3, connection_->binders_.size());
689 EXPECT_TRUE(connection_->binders_.at(0) == &binder0);
690 EXPECT_TRUE(connection_->binders_.at(1) == &binder1);
691 EXPECT_TRUE(connection_->binders_.at(2) == &binder2);
692
693 EXPECT_CALL(target0, CallTarget()).Times(1);
694 EXPECT_CALL(target1, CallTarget()).Times(1);
695 EXPECT_CALL(target2, CallTarget()).Times(1);
696 connection_->NotifyBindersOnDisconnect();
697 EXPECT_TRUE(connection_->binders_.empty());
698
699 // Should be a no-op.
700 connection_->NotifyBindersOnDisconnect();
701}
702
703TEST_F(ConnectionTest, Binder) {
704 // No connection should be bound initially.
705 Connection::Binder *binder = &connection_->lower_binder_;
706 EXPECT_EQ(connection_->interface_name(), binder->name_);
707 EXPECT_FALSE(binder->client_disconnect_callback_.is_null());
708 EXPECT_FALSE(binder->IsBound());
709
710 ConnectionRefPtr connection1 = GetNewConnection();
711 EXPECT_TRUE(connection1->binders_.empty());
712
713 // Bind lower |connection1| and check if it's bound.
714 binder->Attach(connection1);
715 EXPECT_TRUE(binder->IsBound());
716 EXPECT_EQ(connection1.get(), binder->connection().get());
717 ASSERT_FALSE(connection1->binders_.empty());
718 EXPECT_TRUE(binder == connection1->binders_.at(0));
719
720 // Unbind lower |connection1| and check if it's unbound.
721 binder->Attach(NULL);
722 EXPECT_FALSE(binder->IsBound());
723 EXPECT_TRUE(connection1->binders_.empty());
724
725 ConnectionRefPtr connection2 = GetNewConnection();
726
727 // Bind lower |connection1| to upper |connection2| and destroy the upper
728 // |connection2|. Make sure lower |connection1| is unbound (i.e., the
729 // disconnect callback is deregistered).
730 connection2->lower_binder_.Attach(connection1);
731 EXPECT_FALSE(connection1->binders_.empty());
732 AddDestructorExpectations();
733 connection2 = NULL;
734 EXPECT_TRUE(connection1->binders_.empty());
735
736 // Bind lower |connection1| to upper |connection_| and destroy lower
737 // |connection1|. Make sure lower |connection1| is unbound from upper
738 // |connection_| and upper |connection_|'s registered disconnect callbacks are
739 // run.
740 binder->Attach(connection1);
741 DisconnectCallbackTarget target;
742 Connection::Binder test_binder("from_test", target.callback());
743 test_binder.Attach(connection_);
744 EXPECT_CALL(target, CallTarget()).Times(1);
745 ASSERT_FALSE(connection_->binders_.empty());
746 AddDestructorExpectations();
747 connection1 = NULL;
748 EXPECT_FALSE(binder->IsBound());
749 EXPECT_FALSE(test_binder.IsBound());
750 EXPECT_TRUE(connection_->binders_.empty());
751
752 {
753 // Binding a connection to itself should be safe.
754 ConnectionRefPtr connection = GetNewConnection();
755
756 connection->lower_binder_.Attach(connection);
757
758 EXPECT_FALSE(connection->binders_.empty());
759
760 DisconnectCallbackTarget target;
761 Connection::Binder binder("test", target.callback());
762 binder.Attach(connection);
763
764 AddDestructorExpectations();
765 EXPECT_CALL(target, CallTarget()).Times(1);
766 connection = NULL;
767 }
Darin Petkov13e6d552012-05-09 14:22:23 +0200768 {
769 // Circular binding of multiple connections should be safe.
770 ConnectionRefPtr connection_a = GetNewConnection();
771 ConnectionRefPtr connection_b = GetNewConnection();
772
773 connection_a->lower_binder_.Attach(connection_b);
774 connection_b->lower_binder_.Attach(connection_a);
775
776 EXPECT_FALSE(connection_a->binders_.empty());
777 EXPECT_FALSE(connection_b->binders_.empty());
778
779 DisconnectCallbackTarget target_a;
780 DisconnectCallbackTarget target_b;
781 Connection::Binder binder_a("test_a", target_a.callback());
782 Connection::Binder binder_b("test_b", target_b.callback());
783 binder_a.Attach(connection_a);
784 binder_b.Attach(connection_b);
785
786 AddDestructorExpectations();
787 EXPECT_CALL(target_a, CallTarget()).Times(1);
788 EXPECT_CALL(target_b, CallTarget()).Times(1);
789 connection_b = NULL;
790
791 EXPECT_TRUE(connection_a->binders_.empty());
792
793 AddDestructorExpectations();
794 connection_a = NULL;
795 }
Darin Petkovef1f9fe2012-05-11 16:51:52 +0200796 {
797 // Test the weak pointer to the bound Connection. This is not a case that
798 // should occur but the weak pointer should handle it gracefully.
799 DisconnectCallbackTarget target;
800 Connection::Binder binder("test_weak", target.callback());
801 ConnectionRefPtr connection = GetNewConnection();
802 binder.Attach(connection);
803
804 // Make sure the connection doesn't notify the binder on destruction.
805 connection->binders_.clear();
806 AddDestructorExpectations();
807 EXPECT_CALL(target, CallTarget()).Times(0);
808 connection = NULL;
809
810 // Ensure no crash -- the weak pointer to connection should be NULL.
Darin Petkov5eb05422012-05-11 15:45:25 +0200811 EXPECT_FALSE(binder.connection());
Darin Petkovef1f9fe2012-05-11 16:51:52 +0200812 binder.Attach(NULL);
813 }
Darin Petkov13e6d552012-05-09 14:22:23 +0200814}
815
816TEST_F(ConnectionTest, OnRouteQueryResponse) {
817 Connection::Binder *binder = &connection_->lower_binder_;
818 ConnectionRefPtr connection = GetNewConnection();
819 scoped_refptr<MockDevice> device(new StrictMock<MockDevice>(
820 &control_,
821 reinterpret_cast<EventDispatcher *>(NULL),
822 reinterpret_cast<Metrics *>(NULL),
823 reinterpret_cast<Manager *>(NULL),
824 kTestDeviceName1,
825 string(),
826 kTestDeviceInterfaceIndex1));
827
828 // Make sure we unbind the old lower connection even if we can't lookup the
829 // lower connection device.
830 binder->Attach(connection);
831 scoped_refptr<MockDevice> null_device;
832 EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex1))
833 .WillOnce(Return(null_device));
834 connection_->OnRouteQueryResponse(
835 kTestDeviceInterfaceIndex1, RoutingTableEntry());
836 EXPECT_FALSE(binder->IsBound());
837
838 // Check for graceful handling of a device with no connection.
839 EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex1))
840 .WillOnce(Return(device));
841 connection_->OnRouteQueryResponse(
842 kTestDeviceInterfaceIndex1, RoutingTableEntry());
843 EXPECT_FALSE(binder->IsBound());
844
mukesh agrawalf407d592013-07-31 11:37:57 -0700845 // Create a mock connection that will be used for binding.
Paul Stewart4a6748d2012-07-17 14:31:36 -0700846 scoped_refptr<MockConnection> mock_connection(
847 new StrictMock<MockConnection>(device_info_.get()));
848 EXPECT_CALL(*device_info_.get(),
849 FlushAddresses(mock_connection->interface_index()));
850 const string kInterfaceName(kTestDeviceName0);
851 EXPECT_CALL(*mock_connection, interface_name())
852 .WillRepeatedly(ReturnRef(kInterfaceName));
853 device->connection_ = mock_connection;
Darin Petkov13e6d552012-05-09 14:22:23 +0200854 EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex1))
855 .WillOnce(Return(device));
Paul Stewart4a6748d2012-07-17 14:31:36 -0700856
Ben Chana6bfe872012-09-26 09:48:34 -0700857 // Check that the binding process completes, causing its upper
Paul Stewart4a6748d2012-07-17 14:31:36 -0700858 // connection to create a gateway route.
859 EXPECT_CALL(*mock_connection, CreateGatewayRoute())
860 .WillOnce(Return(true));
Paul Stewart8596f9f2013-03-14 07:58:26 -0700861
862 // Ensure that the Device is notified of the change to the connection.
863 EXPECT_CALL(*device, OnConnectionUpdated()).Times(1);
Darin Petkov13e6d552012-05-09 14:22:23 +0200864 connection_->OnRouteQueryResponse(
865 kTestDeviceInterfaceIndex1, RoutingTableEntry());
Paul Stewart4a6748d2012-07-17 14:31:36 -0700866
867 // Check that the upper connection is bound to the lower connection.
Darin Petkov13e6d552012-05-09 14:22:23 +0200868 EXPECT_TRUE(binder->IsBound());
Paul Stewart4a6748d2012-07-17 14:31:36 -0700869 EXPECT_EQ(mock_connection.get(), binder->connection().get());
Darin Petkov13e6d552012-05-09 14:22:23 +0200870
871 device->connection_ = NULL;
872 AddDestructorExpectations();
873 connection = NULL;
874}
875
Alex Deymofddc09a2013-07-03 18:41:31 -0700876TEST_F(ConnectionTest, GetCarrierConnection) {
877 EXPECT_EQ(connection_.get(), connection_->GetCarrierConnection().get());
878
879 ConnectionRefPtr connection1 = GetNewConnection();
880 ConnectionRefPtr connection2 = GetNewConnection();
881 ConnectionRefPtr connection3 = GetNewConnection();
882
883 connection_->lower_binder_.Attach(connection1);
884 EXPECT_EQ(connection1.get(), connection_->GetCarrierConnection().get());
885
886 connection1->lower_binder_.Attach(connection2);
887 EXPECT_EQ(connection2.get(), connection_->GetCarrierConnection().get());
888
889 connection2->lower_binder_.Attach(connection3);
890 EXPECT_EQ(connection3.get(), connection_->GetCarrierConnection().get());
891
892 // Create a cycle back to |connection1|.
893 connection3->lower_binder_.Attach(connection1);
894 EXPECT_EQ(NULL, connection_->GetCarrierConnection().get());
895
896 AddDestructorExpectations();
897 connection3 = NULL;
898
899 AddDestructorExpectations();
900 connection2 = NULL;
901
902 AddDestructorExpectations();
903 connection1 = NULL;
904}
905
Paul Stewartdd60e452011-08-08 11:38:36 -0700906} // namespace shill