1 //
2 // Copyright (C) 2012 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "shill/connection.h"
18
19 #include <arpa/inet.h>
20 #include <linux/rtnetlink.h>
21
22 #include <memory>
23 #include <string>
24 #include <vector>
25
26 #include <gtest/gtest.h>
27 #include <gmock/gmock.h>
28
29 #include "shill/ipconfig.h"
30 #include "shill/mock_connection.h"
31 #include "shill/mock_control.h"
32 #include "shill/mock_device.h"
33 #include "shill/mock_device_info.h"
34 #include "shill/mock_firewall_proxy.h"
35 #if !defined(__ANDROID__)
36 #include "shill/mock_resolver.h"
37 #else
38 #include "shill/mock_dns_server_proxy.h"
39 #include "shill/mock_dns_server_proxy_factory.h"
40 #endif // __ANDROID__
41 #include "shill/mock_routing_table.h"
42 #include "shill/net/mock_rtnl_handler.h"
43 #include "shill/routing_table_entry.h"
44
45 using std::string;
46 using std::vector;
47 using testing::_;
48 using testing::Mock;
49 using testing::NiceMock;
50 using testing::Return;
51 using testing::ReturnRef;
52 using testing::StrictMock;
53 using testing::Test;
54
55 namespace shill {
56
57 namespace {
58 const char kTestDeviceName0[] = "netdev0";
59 const int kTestDeviceInterfaceIndex0 = 123;
60 const char kTestDeviceName1[] = "netdev1";
61 const int kTestDeviceInterfaceIndex1 = 321;
62 const char kIPAddress0[] = "192.168.1.1";
63 const char kGatewayAddress0[] = "192.168.1.254";
64 const char kBroadcastAddress0[] = "192.168.1.255";
65 const char kNameServer0[] = "8.8.8.8";
66 const char kNameServer1[] = "8.8.9.9";
67 const int32_t kPrefix0 = 24;
68 const int32_t kPrefix1 = 31;
69 const char kSearchDomain0[] = "chromium.org";
70 const char kSearchDomain1[] = "google.com";
71 const char kIPv6Address[] = "2001:db8::1";
72 const char kIPv6NameServer0[] = "2001:db9::1";
73 const char kIPv6NameServer1[] = "2001:db9::2";
74 } // namespace
75
76 class ConnectionTest : public Test {
77 public:
ConnectionTest()78 ConnectionTest()
79 : device_info_(new StrictMock<MockDeviceInfo>(
80 &control_,
81 nullptr,
82 nullptr,
83 nullptr)),
84 connection_(new Connection(
85 kTestDeviceInterfaceIndex0,
86 kTestDeviceName0,
87 Technology::kUnknown,
88 device_info_.get(),
89 &control_)),
90 ipconfig_(new IPConfig(&control_, kTestDeviceName0)),
91 ip6config_(new IPConfig(&control_, kTestDeviceName0)),
92 local_address_(IPAddress::kFamilyIPv4),
93 broadcast_address_(IPAddress::kFamilyIPv4),
94 gateway_address_(IPAddress::kFamilyIPv4),
95 default_address_(IPAddress::kFamilyIPv4),
96 local_ipv6_address_(IPAddress::kFamilyIPv6) {}
97
SetUp()98 virtual void SetUp() {
99 ReplaceSingletons(connection_);
100 properties_.address = kIPAddress0;
101 properties_.subnet_prefix = kPrefix0;
102 properties_.gateway = kGatewayAddress0;
103 properties_.broadcast_address = kBroadcastAddress0;
104 properties_.dns_servers.push_back(kNameServer0);
105 properties_.dns_servers.push_back(kNameServer1);
106 properties_.domain_search.push_back(kSearchDomain0);
107 properties_.domain_search.push_back(kSearchDomain1);
108 properties_.address_family = IPAddress::kFamilyIPv4;
109 UpdateProperties();
110 ipv6_properties_.address = kIPv6Address;
111 ipv6_properties_.dns_servers.push_back(kIPv6NameServer0);
112 ipv6_properties_.dns_servers.push_back(kIPv6NameServer1);
113 ipv6_properties_.address_family = IPAddress::kFamilyIPv6;
114 UpdateIPv6Properties();
115 EXPECT_TRUE(local_address_.SetAddressFromString(kIPAddress0));
116 EXPECT_TRUE(broadcast_address_.SetAddressFromString(kBroadcastAddress0));
117 EXPECT_TRUE(gateway_address_.SetAddressFromString(kGatewayAddress0));
118 EXPECT_TRUE(local_ipv6_address_.SetAddressFromString(kIPv6Address));
119 }
120
TearDown()121 virtual void TearDown() {
122 AddDestructorExpectations();
123 connection_ = nullptr;
124 }
125
ReplaceSingletons(ConnectionRefPtr connection)126 void ReplaceSingletons(ConnectionRefPtr connection) {
127 #if !defined(__ANDROID__)
128 connection->resolver_ = &resolver_;
129 #else
130 connection->dns_server_proxy_factory_ = &dns_server_proxy_factory_;
131 #endif // __ANDROID__
132 connection->routing_table_ = &routing_table_;
133 connection->rtnl_handler_ = &rtnl_handler_;
134 }
135
UpdateProperties()136 void UpdateProperties() {
137 ipconfig_->UpdateProperties(properties_, true);
138 }
139
UpdateIPv6Properties()140 void UpdateIPv6Properties() {
141 ip6config_->UpdateProperties(ipv6_properties_, true);
142 }
143
PinHostRoute(ConnectionRefPtr connection,const IPAddress trusted_ip,const IPAddress gateway)144 bool PinHostRoute(ConnectionRefPtr connection,
145 const IPAddress trusted_ip,
146 const IPAddress gateway) {
147 return connection->PinHostRoute(trusted_ip, gateway);
148 }
149
GetLocalAddress(ConnectionRefPtr connection)150 const IPAddress& GetLocalAddress(ConnectionRefPtr connection) {
151 return connection->local_;
152 }
153
GetGatewayAddress(ConnectionRefPtr connection)154 const IPAddress& GetGatewayAddress(ConnectionRefPtr connection) {
155 return connection->gateway_;
156 }
157
GetHasBroadcastDomain(ConnectionRefPtr connection)158 bool GetHasBroadcastDomain(ConnectionRefPtr connection) {
159 return connection->has_broadcast_domain_;
160 }
161
GetDefaultMetric()162 uint32_t GetDefaultMetric() {
163 return Connection::kDefaultMetric;
164 }
165
GetNonDefaultMetricBase()166 uint32_t GetNonDefaultMetricBase() {
167 return Connection::kNonDefaultMetricBase;
168 }
169
SetLocal(const IPAddress & local)170 void SetLocal(const IPAddress& local) {
171 connection_->local_ = local;
172 }
173
174 #if defined(__ANDROID__)
175 // Setting expectations for creating/starting DNS server proxy.
ExpectDNSServerProxyCreation(const vector<string> & dns_servers,bool verify_servers)176 void ExpectDNSServerProxyCreation(const vector<string>& dns_servers,
177 bool verify_servers) {
178 MockDNSServerProxy* dns_server_proxy = new MockDNSServerProxy();
179 if (verify_servers) {
180 EXPECT_CALL(dns_server_proxy_factory_,
181 CreateDNSServerProxy(dns_servers))
182 .WillOnce(Return(dns_server_proxy));
183 } else {
184 EXPECT_CALL(dns_server_proxy_factory_, CreateDNSServerProxy(_))
185 .WillOnce(Return(dns_server_proxy));
186 }
187 EXPECT_CALL(*dns_server_proxy, Start());
188 }
189 #endif // __ANDROID__
190
191 protected:
192 class DisconnectCallbackTarget {
193 public:
DisconnectCallbackTarget()194 DisconnectCallbackTarget()
195 : callback_(base::Bind(&DisconnectCallbackTarget::CallTarget,
196 base::Unretained(this))) {}
197
198 MOCK_METHOD0(CallTarget, void());
callback()199 const base::Closure& callback() { return callback_; }
200
201 private:
202 base::Closure callback_;
203 };
204
AddDestructorExpectations()205 void AddDestructorExpectations() {
206 EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex0));
207 EXPECT_CALL(routing_table_, FlushRoutesWithTag(kTestDeviceInterfaceIndex0));
208 EXPECT_CALL(*device_info_.get(),
209 FlushAddresses(kTestDeviceInterfaceIndex0));
210 }
211
212 // Returns a new test connection object. The caller usually needs to call
213 // AddDestructorExpectations before destroying the object.
GetNewConnection()214 ConnectionRefPtr GetNewConnection() {
215 ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex0,
216 kTestDeviceName0,
217 Technology::kUnknown,
218 device_info_.get(),
219 &control_));
220 ReplaceSingletons(connection);
221 return connection;
222 }
223
224 std::unique_ptr<StrictMock<MockDeviceInfo>> device_info_;
225 ConnectionRefPtr connection_;
226 MockControl control_;
227 IPConfigRefPtr ipconfig_;
228 IPConfigRefPtr ip6config_;
229 IPConfig::Properties properties_;
230 IPConfig::Properties ipv6_properties_;
231 IPAddress local_address_;
232 IPAddress broadcast_address_;
233 IPAddress gateway_address_;
234 IPAddress default_address_;
235 IPAddress local_ipv6_address_;
236 #if !defined(__ANDROID__)
237 StrictMock<MockResolver> resolver_;
238 #else
239 StrictMock<MockDNSServerProxyFactory> dns_server_proxy_factory_;
240 #endif // __ANDROID__
241 StrictMock<MockRoutingTable> routing_table_;
242 StrictMock<MockRTNLHandler> rtnl_handler_;
243 };
244
245 namespace {
246
247 MATCHER_P2(IsIPAddress, address, prefix, "") {
248 IPAddress match_address(address);
249 match_address.set_prefix(prefix);
250 return match_address.Equals(arg);
251 }
252
253 MATCHER_P(IsIPv6Address, address, "") {
254 IPAddress match_address(address);
255 return match_address.Equals(arg);
256 }
257
258 MATCHER(IsNonNullCallback, "") {
259 return !arg.is_null();
260 }
261
262 MATCHER_P(IsValidRoutingTableEntry, dst, "") {
263 return dst.Equals(arg.dst);
264 }
265
266 MATCHER_P(IsLinkRouteTo, dst, "") {
267 return dst.HasSameAddressAs(arg.dst) &&
268 arg.dst.prefix() ==
269 IPAddress::GetMaxPrefixLength(IPAddress::kFamilyIPv4) &&
270 !arg.src.IsValid() && !arg.gateway.IsValid() &&
271 arg.scope == RT_SCOPE_LINK && !arg.from_rtnl;
272 }
273
274 } // namespace
275
TEST_F(ConnectionTest,InitState)276 TEST_F(ConnectionTest, InitState) {
277 EXPECT_EQ(kTestDeviceInterfaceIndex0, connection_->interface_index_);
278 EXPECT_EQ(kTestDeviceName0, connection_->interface_name_);
279 EXPECT_FALSE(connection_->is_default());
280 EXPECT_FALSE(connection_->routing_request_count_);
281 }
282
TEST_F(ConnectionTest,AddConfig)283 TEST_F(ConnectionTest, AddConfig) {
284 EXPECT_CALL(*device_info_,
285 HasOtherAddress(kTestDeviceInterfaceIndex0,
286 IsIPAddress(local_address_, kPrefix0)))
287 .WillOnce(Return(false));
288 EXPECT_CALL(rtnl_handler_,
289 AddInterfaceAddress(kTestDeviceInterfaceIndex0,
290 IsIPAddress(local_address_, kPrefix0),
291 IsIPAddress(broadcast_address_, 0),
292 IsIPAddress(default_address_, 0)));
293 EXPECT_CALL(routing_table_,
294 SetDefaultRoute(kTestDeviceInterfaceIndex0,
295 IsIPAddress(gateway_address_, 0),
296 GetNonDefaultMetricBase() +
297 kTestDeviceInterfaceIndex0,
298 RT_TABLE_MAIN));
299 EXPECT_CALL(routing_table_,
300 ConfigureRoutes(kTestDeviceInterfaceIndex0,
301 ipconfig_,
302 GetDefaultMetric(),
303 RT_TABLE_MAIN));
304 EXPECT_CALL(rtnl_handler_, SetInterfaceMTU(kTestDeviceInterfaceIndex0,
305 IPConfig::kDefaultMTU));
306 connection_->UpdateFromIPConfig(ipconfig_);
307 IPAddress test_local_address(local_address_);
308 test_local_address.set_prefix(kPrefix0);
309 EXPECT_TRUE(test_local_address.Equals(GetLocalAddress(connection_)));
310 EXPECT_TRUE(gateway_address_.Equals(GetGatewayAddress(connection_)));
311 EXPECT_TRUE(GetHasBroadcastDomain(connection_));
312 EXPECT_FALSE(connection_->IsIPv6());
313
314 EXPECT_CALL(routing_table_,
315 CreateLinkRoute(kTestDeviceInterfaceIndex0,
316 IsIPAddress(local_address_, kPrefix0),
317 IsIPAddress(gateway_address_, 0),
318 RT_TABLE_MAIN))
319 .WillOnce(Return(true))
320 .WillOnce(Return(false));
321 EXPECT_TRUE(connection_->CreateGatewayRoute());
322 EXPECT_FALSE(connection_->CreateGatewayRoute());
323 connection_->has_broadcast_domain_ = false;
324 EXPECT_FALSE(connection_->CreateGatewayRoute());
325
326 EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0,
327 GetDefaultMetric()));
328 #if !defined(__ANDROID__)
329 EXPECT_CALL(resolver_, SetDNSFromLists(
330 ipconfig_->properties().dns_servers,
331 ipconfig_->properties().domain_search));
332 #else
333 ExpectDNSServerProxyCreation(ipconfig_->properties().dns_servers, true);
334 #endif // __ANDROID__
335 scoped_refptr<MockDevice> device(new StrictMock<MockDevice>(
336 &control_,
337 nullptr,
338 nullptr,
339 nullptr,
340 kTestDeviceName0,
341 string(),
342 kTestDeviceInterfaceIndex0));
343 EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0))
344 .WillOnce(Return(device));
345 EXPECT_CALL(*device.get(), RequestPortalDetection())
346 .WillOnce(Return(true));
347 EXPECT_CALL(routing_table_, FlushCache())
348 .WillOnce(Return(true));
349 connection_->SetIsDefault(true);
350 Mock::VerifyAndClearExpectations(&routing_table_);
351 EXPECT_TRUE(connection_->is_default());
352
353 EXPECT_CALL(routing_table_,
354 SetDefaultMetric(kTestDeviceInterfaceIndex0,
355 GetNonDefaultMetricBase() +
356 kTestDeviceInterfaceIndex0));
357 EXPECT_CALL(routing_table_, FlushCache())
358 .WillOnce(Return(true));
359 connection_->SetIsDefault(false);
360 EXPECT_FALSE(connection_->is_default());
361 }
362
TEST_F(ConnectionTest,AddConfigUserTrafficOnly)363 TEST_F(ConnectionTest, AddConfigUserTrafficOnly) {
364 ConnectionRefPtr connection = GetNewConnection();
365 const std::string kExcludeAddress1 = "192.0.1.0/24";
366 const std::string kExcludeAddress2 = "192.0.2.0/24";
367 IPAddress address1(IPAddress::kFamilyIPv4);
368 IPAddress address2(IPAddress::kFamilyIPv4);
369 EXPECT_TRUE(address1.SetAddressAndPrefixFromString(kExcludeAddress1));
370 EXPECT_TRUE(address2.SetAddressAndPrefixFromString(kExcludeAddress2));
371 EXPECT_CALL(*device_info_,
372 HasOtherAddress(kTestDeviceInterfaceIndex0,
373 IsIPAddress(local_address_, kPrefix0)))
374 .WillOnce(Return(false));
375 EXPECT_CALL(rtnl_handler_,
376 AddInterfaceAddress(kTestDeviceInterfaceIndex0,
377 IsIPAddress(local_address_, kPrefix0),
378 IsIPAddress(broadcast_address_, 0),
379 IsIPAddress(default_address_, 0)));
380 EXPECT_CALL(routing_table_,
381 ConfigureRoutes(kTestDeviceInterfaceIndex0, ipconfig_,
382 GetDefaultMetric(), 1));
383 EXPECT_CALL(
384 routing_table_,
385 RequestRouteToHost(IsIPAddress(address1, address1.prefix()), -1,
386 kTestDeviceInterfaceIndex0, IsNonNullCallback(), 1))
387 .WillOnce(Return(true));
388 EXPECT_CALL(rtnl_handler_, SetInterfaceMTU(kTestDeviceInterfaceIndex0,
389 IPConfig::kDefaultMTU));
390
391 MockFirewallProxy* firewall_proxy = new MockFirewallProxy();
392 connection->firewall_proxy_.reset(firewall_proxy);
393 EXPECT_CALL(*firewall_proxy, RequestVpnSetup(_, _));
394 properties_.user_traffic_only = true;
395 properties_.default_route = false;
396 properties_.exclusion_list.push_back(kExcludeAddress1);
397 properties_.exclusion_list.push_back(kExcludeAddress2);
398 UpdateProperties();
399 connection->UpdateFromIPConfig(ipconfig_);
400
401 scoped_refptr<MockDevice> device1(
402 new MockDevice(&control_, nullptr, nullptr, nullptr, kTestDeviceName1,
403 string(), kTestDeviceInterfaceIndex1));
404 scoped_refptr<MockConnection> mock_connection(
405 new MockConnection(device_info_.get()));
406 ConnectionRefPtr device_connection = mock_connection.get();
407
408 EXPECT_CALL(*device_info_.get(),
409 FlushAddresses(mock_connection->interface_index()));
410 const string kInterfaceName(kTestDeviceName1);
411 EXPECT_CALL(*mock_connection, interface_name())
412 .WillRepeatedly(ReturnRef(kInterfaceName));
413 EXPECT_CALL(*device1, connection())
414 .WillRepeatedly(testing::ReturnRef(device_connection));
415 EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex1))
416 .WillOnce(Return(device1));
417
418 EXPECT_CALL(routing_table_, AddRoute(kTestDeviceInterfaceIndex1,
419 IsValidRoutingTableEntry(address2)))
420 .WillOnce(Return(true));
421
422 connection->OnRouteQueryResponse(
423 kTestDeviceInterfaceIndex1,
424 RoutingTableEntry(default_address_, default_address_, default_address_, 1,
425 1, false));
426
427 IPAddress test_local_address(local_address_);
428 test_local_address.set_prefix(kPrefix0);
429 EXPECT_TRUE(test_local_address.Equals(GetLocalAddress(connection)));
430 EXPECT_TRUE(gateway_address_.Equals(GetGatewayAddress(connection)));
431 EXPECT_TRUE(GetHasBroadcastDomain(connection));
432 EXPECT_FALSE(connection->IsIPv6());
433
434 EXPECT_CALL(routing_table_,
435 CreateLinkRoute(kTestDeviceInterfaceIndex0,
436 IsIPAddress(local_address_, kPrefix0),
437 IsIPAddress(gateway_address_, 0), 1))
438 .WillOnce(Return(true))
439 .WillOnce(Return(false));
440 EXPECT_TRUE(connection->CreateGatewayRoute());
441 EXPECT_FALSE(connection->CreateGatewayRoute());
442 connection->has_broadcast_domain_ = false;
443 EXPECT_FALSE(connection->CreateGatewayRoute());
444
445 EXPECT_CALL(routing_table_,
446 SetDefaultMetric(kTestDeviceInterfaceIndex0, GetDefaultMetric()));
447 #if !defined(__ANDROID__)
448 EXPECT_CALL(resolver_,
449 SetDNSFromLists(ipconfig_->properties().dns_servers,
450 ipconfig_->properties().domain_search));
451 #else
452 ExpectDNSServerProxyCreation(ipconfig_->properties().dns_servers, true);
453 #endif // __ANDROID__
454 scoped_refptr<MockDevice> device(new StrictMock<MockDevice>(
455 &control_, nullptr, nullptr, nullptr, kTestDeviceName0, string(),
456 kTestDeviceInterfaceIndex0));
457 EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0))
458 .WillOnce(Return(device));
459 EXPECT_CALL(*device.get(), RequestPortalDetection()).WillOnce(Return(true));
460 EXPECT_CALL(routing_table_, FlushCache()).WillOnce(Return(true));
461 connection->SetIsDefault(true);
462 Mock::VerifyAndClearExpectations(&routing_table_);
463 EXPECT_TRUE(connection->is_default());
464
465 EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0,
466 GetNonDefaultMetricBase() +
467 kTestDeviceInterfaceIndex0));
468 EXPECT_CALL(routing_table_, FlushCache()).WillOnce(Return(true));
469 connection->SetIsDefault(false);
470 EXPECT_FALSE(connection->is_default());
471 AddDestructorExpectations();
472 EXPECT_CALL(*firewall_proxy, RemoveVpnSetup());
473 }
474
TEST_F(ConnectionTest,AddConfigIPv6)475 TEST_F(ConnectionTest, AddConfigIPv6) {
476 EXPECT_CALL(*device_info_,
477 HasOtherAddress(kTestDeviceInterfaceIndex0,
478 IsIPv6Address(local_ipv6_address_)))
479 .WillOnce(Return(false));
480 EXPECT_CALL(rtnl_handler_,
481 AddInterfaceAddress(kTestDeviceInterfaceIndex0,
482 IsIPv6Address(local_ipv6_address_),
483 _,
484 _));
485 EXPECT_CALL(routing_table_,
486 ConfigureRoutes(kTestDeviceInterfaceIndex0,
487 ip6config_,
488 GetDefaultMetric(),
489 RT_TABLE_MAIN));
490 EXPECT_CALL(rtnl_handler_, SetInterfaceMTU(kTestDeviceInterfaceIndex0,
491 IPConfig::kDefaultMTU));
492 connection_->UpdateFromIPConfig(ip6config_);
493 IPAddress test_local_address(local_ipv6_address_);
494 EXPECT_TRUE(test_local_address.Equals(GetLocalAddress(connection_)));
495 EXPECT_TRUE(connection_->IsIPv6());
496 }
497
TEST_F(ConnectionTest,AddConfigWithPeer)498 TEST_F(ConnectionTest, AddConfigWithPeer) {
499 const string kPeerAddress("192.168.1.222");
500 IPAddress peer_address(IPAddress::kFamilyIPv4);
501 EXPECT_TRUE(peer_address.SetAddressFromString(kPeerAddress));
502 properties_.peer_address = kPeerAddress;
503 properties_.gateway = string();
504 UpdateProperties();
505 EXPECT_CALL(*device_info_,
506 HasOtherAddress(kTestDeviceInterfaceIndex0,
507 IsIPAddress(local_address_, kPrefix0)))
508 .WillOnce(Return(false));
509 EXPECT_CALL(rtnl_handler_,
510 AddInterfaceAddress(kTestDeviceInterfaceIndex0,
511 IsIPAddress(local_address_, kPrefix0),
512 IsIPAddress(broadcast_address_, 0),
513 IsIPAddress(peer_address, 0)));
514 EXPECT_CALL(routing_table_, SetDefaultRoute(_, _, _, _)).Times(0);
515 EXPECT_CALL(routing_table_,
516 ConfigureRoutes(kTestDeviceInterfaceIndex0,
517 ipconfig_,
518 GetDefaultMetric(),
519 RT_TABLE_MAIN));
520 EXPECT_CALL(rtnl_handler_, SetInterfaceMTU(kTestDeviceInterfaceIndex0,
521 IPConfig::kDefaultMTU));
522 connection_->UpdateFromIPConfig(ipconfig_);
523 EXPECT_FALSE(GetHasBroadcastDomain(connection_));
524 }
525
TEST_F(ConnectionTest,AddConfigWithBrokenNetmask)526 TEST_F(ConnectionTest, AddConfigWithBrokenNetmask) {
527 // Assign a prefix that makes the gateway unreachable.
528 properties_.subnet_prefix = kPrefix1;
529 UpdateProperties();
530
531 // Connection should add a link route which will allow the
532 // gateway to be reachable.
533 IPAddress gateway_address(IPAddress::kFamilyIPv4);
534 EXPECT_TRUE(gateway_address.SetAddressFromString(kGatewayAddress0));
535 EXPECT_CALL(routing_table_, AddRoute(kTestDeviceInterfaceIndex0,
536 IsLinkRouteTo(gateway_address)))
537 .WillOnce(Return(true));
538 EXPECT_CALL(*device_info_,
539 HasOtherAddress(kTestDeviceInterfaceIndex0,
540 IsIPAddress(local_address_, kPrefix1)))
541 .WillOnce(Return(false));
542 EXPECT_CALL(rtnl_handler_,
543 AddInterfaceAddress(kTestDeviceInterfaceIndex0,
544 IsIPAddress(local_address_, kPrefix1),
545 IsIPAddress(broadcast_address_, 0),
546 IsIPAddress(default_address_, 0)));
547 EXPECT_CALL(routing_table_,
548 SetDefaultRoute(kTestDeviceInterfaceIndex0,
549 IsIPAddress(gateway_address_, 0),
550 GetNonDefaultMetricBase() +
551 kTestDeviceInterfaceIndex0,
552 RT_TABLE_MAIN));
553 EXPECT_CALL(routing_table_,
554 ConfigureRoutes(kTestDeviceInterfaceIndex0,
555 ipconfig_,
556 GetDefaultMetric(),
557 RT_TABLE_MAIN));
558 EXPECT_CALL(rtnl_handler_, SetInterfaceMTU(kTestDeviceInterfaceIndex0,
559 IPConfig::kDefaultMTU));
560 connection_->UpdateFromIPConfig(ipconfig_);
561 }
562
TEST_F(ConnectionTest,AddConfigReverse)563 TEST_F(ConnectionTest, AddConfigReverse) {
564 EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0,
565 GetDefaultMetric()));
566 vector<string> empty_list;
567 #if !defined(__ANDROID__)
568 EXPECT_CALL(resolver_, SetDNSFromLists(empty_list, empty_list));
569 #else
570 ExpectDNSServerProxyCreation(empty_list, true);
571 #endif // __ANDROID__
572 scoped_refptr<MockDevice> device(new StrictMock<MockDevice>(
573 &control_,
574 nullptr,
575 nullptr,
576 nullptr,
577 kTestDeviceName0,
578 string(),
579 kTestDeviceInterfaceIndex0));
580 EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0))
581 .WillOnce(Return(device));
582 EXPECT_CALL(*device.get(), RequestPortalDetection())
583 .WillOnce(Return(true));
584 EXPECT_CALL(routing_table_, FlushCache())
585 .WillOnce(Return(true));
586 connection_->SetIsDefault(true);
587 Mock::VerifyAndClearExpectations(&routing_table_);
588
589 EXPECT_CALL(*device_info_,
590 HasOtherAddress(kTestDeviceInterfaceIndex0,
591 IsIPAddress(local_address_, kPrefix0)))
592 .WillOnce(Return(false));
593 EXPECT_CALL(rtnl_handler_,
594 AddInterfaceAddress(kTestDeviceInterfaceIndex0,
595 IsIPAddress(local_address_, kPrefix0),
596 IsIPAddress(broadcast_address_, 0),
597 IsIPAddress(default_address_, 0)));
598 EXPECT_CALL(routing_table_, SetDefaultRoute(kTestDeviceInterfaceIndex0,
599 IsIPAddress(gateway_address_, 0),
600 GetDefaultMetric(),
601 RT_TABLE_MAIN));
602 EXPECT_CALL(routing_table_,
603 ConfigureRoutes(kTestDeviceInterfaceIndex0,
604 ipconfig_,
605 GetDefaultMetric(),
606 RT_TABLE_MAIN));
607 #if !defined(__ANDROID__)
608 EXPECT_CALL(resolver_,
609 SetDNSFromLists(ipconfig_->properties().dns_servers,
610 ipconfig_->properties().domain_search));
611 #else
612 ExpectDNSServerProxyCreation(ipconfig_->properties().dns_servers, true);
613 #endif // __ANDROID__
614 EXPECT_CALL(rtnl_handler_, SetInterfaceMTU(kTestDeviceInterfaceIndex0,
615 IPConfig::kDefaultMTU));
616 connection_->UpdateFromIPConfig(ipconfig_);
617 }
618
TEST_F(ConnectionTest,AddConfigWithDNSDomain)619 TEST_F(ConnectionTest, AddConfigWithDNSDomain) {
620 const string kDomainName("chromium.org");
621 properties_.domain_search.clear();
622 properties_.domain_name = kDomainName;
623 UpdateProperties();
624 EXPECT_CALL(*device_info_, HasOtherAddress(_, _))
625 .WillOnce(Return(false));
626 EXPECT_CALL(rtnl_handler_, AddInterfaceAddress(_, _, _, _));
627 EXPECT_CALL(routing_table_, SetDefaultRoute(_, _, _, _));
628 EXPECT_CALL(routing_table_, ConfigureRoutes(_, _, _, _));
629 EXPECT_CALL(rtnl_handler_, SetInterfaceMTU(_, _));
630 connection_->UpdateFromIPConfig(ipconfig_);
631
632 EXPECT_CALL(routing_table_, SetDefaultMetric(_, _));
633 vector<string> domain_search_list;
634 domain_search_list.push_back(kDomainName + ".");
635 #if !defined(__ANDROID__)
636 EXPECT_CALL(resolver_, SetDNSFromLists(_, domain_search_list));
637 #else
638 ExpectDNSServerProxyCreation(vector<string>(), false);
639 #endif // __ANDROID__
640 DeviceRefPtr device;
641 EXPECT_CALL(*device_info_, GetDevice(_)).WillOnce(Return(device));
642 EXPECT_CALL(routing_table_, FlushCache()).WillOnce(Return(true));
643 connection_->SetIsDefault(true);
644 }
645
TEST_F(ConnectionTest,HasOtherAddress)646 TEST_F(ConnectionTest, HasOtherAddress) {
647 EXPECT_CALL(*device_info_,
648 HasOtherAddress(kTestDeviceInterfaceIndex0,
649 IsIPAddress(local_address_, kPrefix0)))
650 .WillOnce(Return(true));
651 EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex0));
652 EXPECT_CALL(*device_info_, FlushAddresses(kTestDeviceInterfaceIndex0));
653 EXPECT_CALL(rtnl_handler_,
654 AddInterfaceAddress(kTestDeviceInterfaceIndex0,
655 IsIPAddress(local_address_, kPrefix0),
656 IsIPAddress(broadcast_address_, 0),
657 IsIPAddress(default_address_, 0)));
658 EXPECT_CALL(routing_table_,
659 SetDefaultRoute(kTestDeviceInterfaceIndex0,
660 IsIPAddress(gateway_address_, 0),
661 GetNonDefaultMetricBase() +
662 kTestDeviceInterfaceIndex0,
663 RT_TABLE_MAIN));
664 EXPECT_CALL(routing_table_,
665 ConfigureRoutes(kTestDeviceInterfaceIndex0,
666 ipconfig_,
667 GetDefaultMetric(),
668 RT_TABLE_MAIN));
669 EXPECT_CALL(rtnl_handler_, SetInterfaceMTU(kTestDeviceInterfaceIndex0,
670 IPConfig::kDefaultMTU));
671 connection_->UpdateFromIPConfig(ipconfig_);
672 }
673
TEST_F(ConnectionTest,UpdateDNSServers)674 TEST_F(ConnectionTest, UpdateDNSServers) {
675 const char* kDnsServers[] = {"1.1.1.1", "1.1.1.2"};
676 vector<string> dns_servers(kDnsServers, std::end(kDnsServers));
677
678 // Non-default connection.
679 connection_->is_default_ = false;
680 #if !defined(__ANDROID__)
681 EXPECT_CALL(resolver_, SetDNSFromLists(_, _)).Times(0);
682 #else
683 EXPECT_CALL(dns_server_proxy_factory_, CreateDNSServerProxy(_)).Times(0);
684 #endif // __ANDROID__
685 connection_->UpdateDNSServers(dns_servers);
686 #if !defined(__ANDROID__)
687 Mock::VerifyAndClearExpectations(&resolver_);
688 #else
689 Mock::VerifyAndClearExpectations(&dns_server_proxy_factory_);
690 #endif // __ANDROID__
691
692 // Default connection.
693 connection_->is_default_ = true;
694 #if !defined(__ANDROID__)
695 EXPECT_CALL(resolver_, SetDNSFromLists(dns_servers, _));
696 #else
697 ExpectDNSServerProxyCreation(dns_servers, true);
698 #endif // __ANDROID__
699 connection_->UpdateDNSServers(dns_servers);
700 #if !defined(__ANDROID__)
701 Mock::VerifyAndClearExpectations(&resolver_);
702 #else
703 Mock::VerifyAndClearExpectations(&dns_server_proxy_factory_);
704 #endif // __ANDROID__
705 }
706
TEST_F(ConnectionTest,RouteRequest)707 TEST_F(ConnectionTest, RouteRequest) {
708 ConnectionRefPtr connection = GetNewConnection();
709 scoped_refptr<MockDevice> device(new StrictMock<MockDevice>(
710 &control_,
711 nullptr,
712 nullptr,
713 nullptr,
714 kTestDeviceName0,
715 string(),
716 kTestDeviceInterfaceIndex0));
717 EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0))
718 .WillRepeatedly(Return(device));
719 EXPECT_CALL(*device.get(), SetLooseRouting(true)).Times(1);
720 connection->RequestRouting();
721 connection->RequestRouting();
722
723 // The first release should only decrement the reference counter.
724 connection->ReleaseRouting();
725
726 // Another release will re-enable reverse-path filter.
727 EXPECT_CALL(*device.get(), SetLooseRouting(false));
728 EXPECT_CALL(routing_table_, FlushCache());
729 connection->ReleaseRouting();
730
731 // The destructor will remove the routes and addresses.
732 AddDestructorExpectations();
733 }
734
TEST_F(ConnectionTest,Destructor)735 TEST_F(ConnectionTest, Destructor) {
736 ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex1,
737 kTestDeviceName1,
738 Technology::kUnknown,
739 device_info_.get(),
740 &control_));
741 #if !defined(__ANDROID__)
742 connection->resolver_ = &resolver_;
743 #else
744 connection->dns_server_proxy_factory_ = &dns_server_proxy_factory_;
745 #endif // __ANDROID__
746 connection->routing_table_ = &routing_table_;
747 connection->rtnl_handler_ = &rtnl_handler_;
748 EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex1));
749 EXPECT_CALL(routing_table_, FlushRoutesWithTag(kTestDeviceInterfaceIndex1));
750 EXPECT_CALL(*device_info_, FlushAddresses(kTestDeviceInterfaceIndex1));
751 connection = nullptr;
752 }
753
TEST_F(ConnectionTest,RequestHostRoute)754 TEST_F(ConnectionTest, RequestHostRoute) {
755 ConnectionRefPtr connection = GetNewConnection();
756 IPAddress address(IPAddress::kFamilyIPv4);
757 ASSERT_TRUE(address.SetAddressFromString(kIPAddress0));
758 size_t prefix_len = 16;
759 address.set_prefix(prefix_len);
760 EXPECT_CALL(routing_table_,
761 RequestRouteToHost(IsIPAddress(address, prefix_len),
762 -1,
763 kTestDeviceInterfaceIndex0,
764 IsNonNullCallback(),
765 RT_TABLE_MAIN))
766 .WillOnce(Return(true));
767 EXPECT_TRUE(connection->RequestHostRoute(address));
768
769 // The destructor will remove the routes and addresses.
770 AddDestructorExpectations();
771 }
772
TEST_F(ConnectionTest,BlackholeIPv6)773 TEST_F(ConnectionTest, BlackholeIPv6) {
774 properties_.blackhole_ipv6 = true;
775 UpdateProperties();
776 EXPECT_CALL(*device_info_, HasOtherAddress(_, _))
777 .WillOnce(Return(false));
778 EXPECT_CALL(rtnl_handler_, AddInterfaceAddress(_, _, _, _));
779 EXPECT_CALL(routing_table_, SetDefaultRoute(_, _, _, _));
780 EXPECT_CALL(routing_table_, ConfigureRoutes(_, _, _, _));
781 EXPECT_CALL(routing_table_,
782 CreateBlackholeRoute(kTestDeviceInterfaceIndex0,
783 IPAddress::kFamilyIPv6,
784 Connection::kDefaultMetric,
785 RT_TABLE_MAIN))
786 .WillOnce(Return(true));
787 EXPECT_CALL(rtnl_handler_, SetInterfaceMTU(kTestDeviceInterfaceIndex0,
788 IPConfig::kDefaultMTU));
789 connection_->UpdateFromIPConfig(ipconfig_);
790 }
791
TEST_F(ConnectionTest,PinHostRoute)792 TEST_F(ConnectionTest, PinHostRoute) {
793 ConnectionRefPtr connection = GetNewConnection();
794
795 IPAddress gateway(IPAddress::kFamilyIPv4);
796 IPAddress trusted_ip(IPAddress::kFamilyIPv4);
797
798 // Should fail because neither IP address is set.
799 EXPECT_FALSE(PinHostRoute(connection, trusted_ip, gateway));
800
801 static const char kGateway[] = "10.242.2.13";
802 ASSERT_TRUE(gateway.SetAddressFromString(kGateway));
803
804 // Should fail because trusted IP is not set.
805 EXPECT_FALSE(PinHostRoute(connection, trusted_ip, gateway));
806
807 static const char kTrustedIP[] = "10.0.1.1/8";
808 ASSERT_TRUE(trusted_ip.SetAddressAndPrefixFromString(kTrustedIP));
809
810 // Should pass without calling RequestRouteToHost since if the gateway
811 // is not set, there is no work to be done.
812 EXPECT_CALL(routing_table_, RequestRouteToHost(_, _, _, _, _)).Times(0);
813 EXPECT_TRUE(PinHostRoute(connection, trusted_ip,
814 IPAddress(gateway.family())));
815 Mock::VerifyAndClearExpectations(&routing_table_);
816
817 EXPECT_CALL(routing_table_,
818 RequestRouteToHost(IsIPAddress(trusted_ip, trusted_ip.prefix()),
819 -1, kTestDeviceInterfaceIndex0, _,
820 RT_TABLE_MAIN)).WillOnce(Return(false));
821 EXPECT_FALSE(PinHostRoute(connection, trusted_ip, gateway));
822
823 EXPECT_CALL(routing_table_,
824 RequestRouteToHost(IsIPAddress(trusted_ip, trusted_ip.prefix()),
825 -1, kTestDeviceInterfaceIndex0, _,
826 RT_TABLE_MAIN)).WillOnce(Return(true));
827 EXPECT_TRUE(PinHostRoute(connection, trusted_ip, gateway));
828
829 // The destructor will remove the routes and addresses.
830 AddDestructorExpectations();
831 }
832
TEST_F(ConnectionTest,FixGatewayReachability)833 TEST_F(ConnectionTest, FixGatewayReachability) {
834 ConnectionRefPtr connection = GetNewConnection();
835 static const char kLocal[] = "10.242.2.13";
836 IPAddress local(IPAddress::kFamilyIPv4);
837 ASSERT_TRUE(local.SetAddressFromString(kLocal));
838 const int kPrefix = 24;
839 local.set_prefix(kPrefix);
840 IPAddress gateway(IPAddress::kFamilyIPv4);
841 IPAddress peer(IPAddress::kFamilyIPv4);
842 IPAddress trusted_ip(IPAddress::kFamilyIPv4);
843
844 // Should fail because no gateway is set.
845 EXPECT_FALSE(connection->FixGatewayReachability(
846 local, &peer, &gateway, trusted_ip));
847 EXPECT_EQ(kPrefix, local.prefix());
848 EXPECT_FALSE(peer.IsValid());
849 EXPECT_FALSE(gateway.IsValid());
850
851 // Should succeed because with the given prefix, this gateway is reachable.
852 static const char kReachableGateway[] = "10.242.2.14";
853 ASSERT_TRUE(gateway.SetAddressFromString(kReachableGateway));
854 IPAddress gateway_backup(gateway);
855 peer = IPAddress(IPAddress::kFamilyIPv4);
856 EXPECT_TRUE(connection->FixGatewayReachability(
857 local, &peer, &gateway, trusted_ip));
858 // Prefix should remain unchanged.
859 EXPECT_EQ(kPrefix, local.prefix());
860 // Peer should remain unchanged.
861 EXPECT_FALSE(peer.IsValid());
862 // Gateway should remain unchanged.
863 EXPECT_TRUE(gateway_backup.Equals(gateway));
864
865 // Should succeed because we created a link route to the gateway.
866 static const char kRemoteGateway[] = "10.242.3.14";
867 ASSERT_TRUE(gateway.SetAddressFromString(kRemoteGateway));
868 gateway_backup = gateway;
869 peer = IPAddress(IPAddress::kFamilyIPv4);
870 EXPECT_CALL(routing_table_, AddRoute(kTestDeviceInterfaceIndex0,
871 IsLinkRouteTo(gateway)))
872 .WillOnce(Return(true));
873 EXPECT_TRUE(connection->FixGatewayReachability(
874 local, &peer, &gateway, trusted_ip));
875
876 // Peer should remain unchanged.
877 EXPECT_FALSE(peer.IsValid());
878 // Gateway should remain unchanged.
879 EXPECT_TRUE(gateway_backup.Equals(gateway));
880
881 // Should fail if AddRoute() fails.
882 EXPECT_CALL(routing_table_, AddRoute(kTestDeviceInterfaceIndex0,
883 IsLinkRouteTo(gateway)))
884 .WillOnce(Return(false));
885 EXPECT_FALSE(connection->FixGatewayReachability(
886 local, &peer, &gateway, trusted_ip));
887
888 // If this is a peer-to-peer interface and the peer matches the gateway,
889 // we should succeed.
890 local.set_prefix(kPrefix);
891 static const char kUnreachableGateway[] = "11.242.2.14";
892 ASSERT_TRUE(gateway.SetAddressFromString(kUnreachableGateway));
893 gateway_backup = gateway;
894 ASSERT_TRUE(peer.SetAddressFromString(kUnreachableGateway));
895 EXPECT_TRUE(connection->FixGatewayReachability(
896 local, &peer, &gateway, trusted_ip));
897 EXPECT_EQ(kPrefix, local.prefix());
898 EXPECT_TRUE(peer.Equals(gateway));
899 EXPECT_TRUE(gateway_backup.Equals(gateway));
900
901 // If there is a peer specified and it does not match the gateway (even
902 // if it was reachable via netmask), we should fail.
903 ASSERT_TRUE(gateway.SetAddressFromString(kReachableGateway));
904 EXPECT_FALSE(connection->FixGatewayReachability(
905 local, &peer, &gateway, trusted_ip));
906 EXPECT_EQ(kPrefix, local.prefix());
907 EXPECT_FALSE(peer.Equals(gateway));
908
909 // If this is a peer-to-peer interface and the peer matches the gateway,
910 // but it also matches the trusted IP address, the gateway and peer address
911 // should be modified to allow routing to work correctly.
912 ASSERT_TRUE(gateway.SetAddressFromString(kUnreachableGateway));
913 ASSERT_TRUE(peer.SetAddressFromString(kUnreachableGateway));
914 ASSERT_TRUE(trusted_ip.SetAddressAndPrefixFromString(
915 string(kUnreachableGateway) + "/32"));
916 EXPECT_TRUE(connection->FixGatewayReachability(
917 local, &peer, &gateway, trusted_ip));
918 EXPECT_TRUE(peer.IsDefault());
919 EXPECT_TRUE(gateway.IsDefault());
920
921 // The destructor will remove the routes and addresses.
922 AddDestructorExpectations();
923 }
924
TEST_F(ConnectionTest,Binders)925 TEST_F(ConnectionTest, Binders) {
926 EXPECT_TRUE(connection_->binders_.empty());
927 DisconnectCallbackTarget target0;
928 DisconnectCallbackTarget target1;
929 DisconnectCallbackTarget target2;
930 DisconnectCallbackTarget target3;
931 Connection::Binder binder0("binder0", target0.callback());
932 Connection::Binder binder1("binder1", target1.callback());
933 Connection::Binder binder2("binder2", target2.callback());
934 Connection::Binder binder3("binder3", target3.callback());
935
936 binder0.Attach(connection_);
937 binder1.Attach(connection_);
938
939 EXPECT_CALL(target1, CallTarget()).Times(0);
940 binder1.Attach(connection_);
941
942 binder3.Attach(connection_);
943 binder2.Attach(connection_);
944
945 EXPECT_CALL(target3, CallTarget()).Times(0);
946 binder3.Attach(nullptr);
947
948 ASSERT_EQ(3, connection_->binders_.size());
949 EXPECT_TRUE(connection_->binders_.at(0) == &binder0);
950 EXPECT_TRUE(connection_->binders_.at(1) == &binder1);
951 EXPECT_TRUE(connection_->binders_.at(2) == &binder2);
952
953 EXPECT_CALL(target0, CallTarget()).Times(1);
954 EXPECT_CALL(target1, CallTarget()).Times(1);
955 EXPECT_CALL(target2, CallTarget()).Times(1);
956 connection_->NotifyBindersOnDisconnect();
957 EXPECT_TRUE(connection_->binders_.empty());
958
959 // Should be a no-op.
960 connection_->NotifyBindersOnDisconnect();
961 }
962
TEST_F(ConnectionTest,Binder)963 TEST_F(ConnectionTest, Binder) {
964 // No connection should be bound initially.
965 Connection::Binder* binder = &connection_->lower_binder_;
966 EXPECT_EQ(connection_->interface_name(), binder->name_);
967 EXPECT_FALSE(binder->client_disconnect_callback_.is_null());
968 EXPECT_FALSE(binder->IsBound());
969
970 ConnectionRefPtr connection1 = GetNewConnection();
971 EXPECT_TRUE(connection1->binders_.empty());
972
973 // Bind lower |connection1| and check if it's bound.
974 binder->Attach(connection1);
975 EXPECT_TRUE(binder->IsBound());
976 EXPECT_EQ(connection1.get(), binder->connection().get());
977 ASSERT_FALSE(connection1->binders_.empty());
978 EXPECT_TRUE(binder == connection1->binders_.at(0));
979
980 // Unbind lower |connection1| and check if it's unbound.
981 binder->Attach(nullptr);
982 EXPECT_FALSE(binder->IsBound());
983 EXPECT_TRUE(connection1->binders_.empty());
984
985 ConnectionRefPtr connection2 = GetNewConnection();
986
987 // Bind lower |connection1| to upper |connection2| and destroy the upper
988 // |connection2|. Make sure lower |connection1| is unbound (i.e., the
989 // disconnect callback is deregistered).
990 connection2->lower_binder_.Attach(connection1);
991 EXPECT_FALSE(connection1->binders_.empty());
992 AddDestructorExpectations();
993 connection2 = nullptr;
994 EXPECT_TRUE(connection1->binders_.empty());
995
996 // Bind lower |connection1| to upper |connection_| and destroy lower
997 // |connection1|. Make sure lower |connection1| is unbound from upper
998 // |connection_| and upper |connection_|'s registered disconnect callbacks are
999 // run.
1000 binder->Attach(connection1);
1001 DisconnectCallbackTarget target;
1002 Connection::Binder test_binder("from_test", target.callback());
1003 test_binder.Attach(connection_);
1004 EXPECT_CALL(target, CallTarget()).Times(1);
1005 ASSERT_FALSE(connection_->binders_.empty());
1006 AddDestructorExpectations();
1007 connection1 = nullptr;
1008 EXPECT_FALSE(binder->IsBound());
1009 EXPECT_FALSE(test_binder.IsBound());
1010 EXPECT_TRUE(connection_->binders_.empty());
1011
1012 {
1013 // Binding a connection to itself should be safe.
1014 ConnectionRefPtr connection = GetNewConnection();
1015
1016 connection->lower_binder_.Attach(connection);
1017
1018 EXPECT_FALSE(connection->binders_.empty());
1019
1020 DisconnectCallbackTarget target;
1021 Connection::Binder binder("test", target.callback());
1022 binder.Attach(connection);
1023
1024 AddDestructorExpectations();
1025 EXPECT_CALL(target, CallTarget()).Times(1);
1026 connection = nullptr;
1027 }
1028 {
1029 // Circular binding of multiple connections should be safe.
1030 ConnectionRefPtr connection_a = GetNewConnection();
1031 ConnectionRefPtr connection_b = GetNewConnection();
1032
1033 connection_a->lower_binder_.Attach(connection_b);
1034 connection_b->lower_binder_.Attach(connection_a);
1035
1036 EXPECT_FALSE(connection_a->binders_.empty());
1037 EXPECT_FALSE(connection_b->binders_.empty());
1038
1039 DisconnectCallbackTarget target_a;
1040 DisconnectCallbackTarget target_b;
1041 Connection::Binder binder_a("test_a", target_a.callback());
1042 Connection::Binder binder_b("test_b", target_b.callback());
1043 binder_a.Attach(connection_a);
1044 binder_b.Attach(connection_b);
1045
1046 AddDestructorExpectations();
1047 EXPECT_CALL(target_a, CallTarget()).Times(1);
1048 EXPECT_CALL(target_b, CallTarget()).Times(1);
1049 connection_b = nullptr;
1050
1051 EXPECT_TRUE(connection_a->binders_.empty());
1052
1053 AddDestructorExpectations();
1054 connection_a = nullptr;
1055 }
1056 {
1057 // Test the weak pointer to the bound Connection. This is not a case that
1058 // should occur but the weak pointer should handle it gracefully.
1059 DisconnectCallbackTarget target;
1060 Connection::Binder binder("test_weak", target.callback());
1061 ConnectionRefPtr connection = GetNewConnection();
1062 binder.Attach(connection);
1063
1064 // Make sure the connection doesn't notify the binder on destruction.
1065 connection->binders_.clear();
1066 AddDestructorExpectations();
1067 EXPECT_CALL(target, CallTarget()).Times(0);
1068 connection = nullptr;
1069
1070 // Ensure no crash -- the weak pointer to connection should be nullptr.
1071 EXPECT_FALSE(binder.connection());
1072 binder.Attach(nullptr);
1073 }
1074 }
1075
TEST_F(ConnectionTest,OnRouteQueryResponse)1076 TEST_F(ConnectionTest, OnRouteQueryResponse) {
1077 Connection::Binder* binder = &connection_->lower_binder_;
1078 ConnectionRefPtr connection = GetNewConnection();
1079 scoped_refptr<MockDevice> device(new StrictMock<MockDevice>(
1080 &control_,
1081 nullptr,
1082 nullptr,
1083 nullptr,
1084 kTestDeviceName1,
1085 string(),
1086 kTestDeviceInterfaceIndex1));
1087
1088 // Make sure we unbind the old lower connection even if we can't lookup the
1089 // lower connection device.
1090 binder->Attach(connection);
1091 scoped_refptr<MockDevice> null_device;
1092 EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex1))
1093 .WillOnce(Return(null_device));
1094 connection_->OnRouteQueryResponse(
1095 kTestDeviceInterfaceIndex1, RoutingTableEntry());
1096 EXPECT_FALSE(binder->IsBound());
1097
1098 // Check for graceful handling of a connection loop.
1099 EXPECT_CALL(*device, connection())
1100 .WillRepeatedly(testing::ReturnRef(connection_));
1101 EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex1))
1102 .WillOnce(Return(device));
1103 connection_->OnRouteQueryResponse(
1104 kTestDeviceInterfaceIndex1, RoutingTableEntry());
1105 EXPECT_FALSE(binder->IsBound());
1106
1107 // Check for graceful handling of a device with no connection.
1108 ConnectionRefPtr device_connection;
1109 EXPECT_CALL(*device, connection())
1110 .WillRepeatedly(testing::ReturnRef(device_connection));
1111 EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex1))
1112 .WillOnce(Return(device));
1113 connection_->OnRouteQueryResponse(
1114 kTestDeviceInterfaceIndex1, RoutingTableEntry());
1115 EXPECT_FALSE(binder->IsBound());
1116
1117 // Create a mock connection that will be used for binding.
1118 scoped_refptr<MockConnection> mock_connection(
1119 new StrictMock<MockConnection>(device_info_.get()));
1120 EXPECT_CALL(*device_info_.get(),
1121 FlushAddresses(mock_connection->interface_index()));
1122 const string kInterfaceName(kTestDeviceName0);
1123 EXPECT_CALL(*mock_connection, interface_name())
1124 .WillRepeatedly(ReturnRef(kInterfaceName));
1125 device_connection = mock_connection.get();
1126 EXPECT_CALL(*device, connection())
1127 .WillRepeatedly(testing::ReturnRef(device_connection));
1128 EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex1))
1129 .WillOnce(Return(device));
1130
1131 // Check that the binding process completes, causing its upper
1132 // connection to create a gateway route.
1133 EXPECT_CALL(*mock_connection, CreateGatewayRoute())
1134 .WillOnce(Return(true));
1135
1136 // Ensure that the Device is notified of the change to the connection.
1137 EXPECT_CALL(*device, OnConnectionUpdated()).Times(1);
1138 connection_->OnRouteQueryResponse(
1139 kTestDeviceInterfaceIndex1, RoutingTableEntry());
1140
1141 // Check that the upper connection is bound to the lower connection.
1142 EXPECT_TRUE(binder->IsBound());
1143 EXPECT_EQ(mock_connection.get(), binder->connection().get());
1144
1145 AddDestructorExpectations();
1146 connection = nullptr;
1147 }
1148
TEST_F(ConnectionTest,GetCarrierConnection)1149 TEST_F(ConnectionTest, GetCarrierConnection) {
1150 EXPECT_EQ(connection_.get(), connection_->GetCarrierConnection().get());
1151
1152 ConnectionRefPtr connection1 = GetNewConnection();
1153 ConnectionRefPtr connection2 = GetNewConnection();
1154 ConnectionRefPtr connection3 = GetNewConnection();
1155
1156 connection_->lower_binder_.Attach(connection1);
1157 EXPECT_EQ(connection1.get(), connection_->GetCarrierConnection().get());
1158
1159 connection1->lower_binder_.Attach(connection2);
1160 EXPECT_EQ(connection2.get(), connection_->GetCarrierConnection().get());
1161
1162 connection2->lower_binder_.Attach(connection3);
1163 EXPECT_EQ(connection3.get(), connection_->GetCarrierConnection().get());
1164
1165 // Create a cycle back to |connection1|.
1166 connection3->lower_binder_.Attach(connection1);
1167 EXPECT_EQ(nullptr, connection_->GetCarrierConnection().get());
1168
1169 AddDestructorExpectations();
1170 connection3 = nullptr;
1171
1172 AddDestructorExpectations();
1173 connection2 = nullptr;
1174
1175 AddDestructorExpectations();
1176 connection1 = nullptr;
1177 }
1178
TEST_F(ConnectionTest,GetSubnetName)1179 TEST_F(ConnectionTest, GetSubnetName) {
1180 EXPECT_EQ("", connection_->GetSubnetName());
1181 IPAddress local("1.2.3.4");
1182 local.set_prefix(24);
1183 SetLocal(local);
1184 EXPECT_EQ("1.2.3.0/24", connection_->GetSubnetName());
1185 }
1186
TEST_F(ConnectionTest,SetMTU)1187 TEST_F(ConnectionTest, SetMTU) {
1188 testing::InSequence seq;
1189 EXPECT_CALL(rtnl_handler_, SetInterfaceMTU(
1190 kTestDeviceInterfaceIndex0, IPConfig::kDefaultMTU));
1191 connection_->SetMTU(0);
1192
1193 EXPECT_CALL(rtnl_handler_, SetInterfaceMTU(
1194 kTestDeviceInterfaceIndex0, IPConfig::kDefaultMTU));
1195 connection_->SetMTU(IPConfig::kUndefinedMTU);
1196
1197 // Test IPv4 minimum MTU.
1198 SetLocal(local_address_);
1199 EXPECT_CALL(rtnl_handler_, SetInterfaceMTU(
1200 kTestDeviceInterfaceIndex0, IPConfig::kMinIPv4MTU));
1201 connection_->SetMTU(1);
1202
1203 EXPECT_CALL(rtnl_handler_, SetInterfaceMTU(
1204 kTestDeviceInterfaceIndex0, IPConfig::kMinIPv4MTU));
1205 connection_->SetMTU(IPConfig::kMinIPv4MTU - 1);
1206
1207 EXPECT_CALL(rtnl_handler_, SetInterfaceMTU(
1208 kTestDeviceInterfaceIndex0, IPConfig::kMinIPv4MTU));
1209 connection_->SetMTU(IPConfig::kMinIPv4MTU);
1210
1211 EXPECT_CALL(rtnl_handler_, SetInterfaceMTU(
1212 kTestDeviceInterfaceIndex0, IPConfig::kMinIPv4MTU + 1));
1213 connection_->SetMTU(IPConfig::kMinIPv4MTU + 1);
1214
1215 // Test IPv6 minimum MTU.
1216 SetLocal(local_ipv6_address_);
1217 EXPECT_CALL(rtnl_handler_, SetInterfaceMTU(
1218 kTestDeviceInterfaceIndex0, IPConfig::kMinIPv6MTU));
1219 connection_->SetMTU(1);
1220
1221 EXPECT_CALL(rtnl_handler_, SetInterfaceMTU(
1222 kTestDeviceInterfaceIndex0, IPConfig::kMinIPv6MTU));
1223 connection_->SetMTU(IPConfig::kMinIPv6MTU - 1);
1224
1225 EXPECT_CALL(rtnl_handler_, SetInterfaceMTU(
1226 kTestDeviceInterfaceIndex0, IPConfig::kMinIPv6MTU));
1227 connection_->SetMTU(IPConfig::kMinIPv6MTU);
1228
1229 EXPECT_CALL(rtnl_handler_, SetInterfaceMTU(
1230 kTestDeviceInterfaceIndex0, IPConfig::kMinIPv6MTU + 1));
1231 connection_->SetMTU(IPConfig::kMinIPv6MTU + 1);
1232 }
1233
1234 } // namespace shill
1235