Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Alex Deymo | cddd2d0 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 5 | #include "shill/connection.h" |
| 6 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 7 | #include <arpa/inet.h> |
| 8 | #include <linux/rtnetlink.h> |
| 9 | |
Ben Chan | 22f1fbc | 2014-10-17 14:18:07 -0700 | [diff] [blame] | 10 | #include <memory> |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 11 | #include <string> |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 12 | #include <vector> |
| 13 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 14 | #include <gtest/gtest.h> |
| 15 | #include <gmock/gmock.h> |
| 16 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 17 | #include "shill/ipconfig.h" |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 18 | #include "shill/mock_connection.h" |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 19 | #include "shill/mock_control.h" |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 20 | #include "shill/mock_device.h" |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 21 | #include "shill/mock_device_info.h" |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 22 | #include "shill/mock_resolver.h" |
| 23 | #include "shill/mock_routing_table.h" |
Peter Qiu | 8d6b597 | 2014-10-28 15:33:34 -0700 | [diff] [blame] | 24 | #include "shill/net/mock_rtnl_handler.h" |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 25 | #include "shill/routing_table_entry.h" |
| 26 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 27 | using std::string; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 28 | using std::vector; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 29 | using testing::_; |
Paul Stewart | e78ec54 | 2012-06-08 18:28:50 -0700 | [diff] [blame] | 30 | using testing::Mock; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 31 | using testing::NiceMock; |
| 32 | using testing::Return; |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 33 | using testing::ReturnRef; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 34 | using testing::StrictMock; |
| 35 | using testing::Test; |
| 36 | |
| 37 | namespace shill { |
| 38 | |
| 39 | namespace { |
| 40 | const char kTestDeviceName0[] = "netdev0"; |
| 41 | const int kTestDeviceInterfaceIndex0 = 123; |
| 42 | const char kTestDeviceName1[] = "netdev1"; |
| 43 | const int kTestDeviceInterfaceIndex1 = 321; |
| 44 | const char kIPAddress0[] = "192.168.1.1"; |
| 45 | const char kGatewayAddress0[] = "192.168.1.254"; |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 46 | const char kGatewayAddress1[] = "192.168.2.254"; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 47 | const char kBroadcastAddress0[] = "192.168.1.255"; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 48 | const char kNameServer0[] = "8.8.8.8"; |
| 49 | const char kNameServer1[] = "8.8.9.9"; |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 50 | const int32_t kPrefix0 = 24; |
| 51 | const int32_t kPrefix1 = 31; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 52 | const char kSearchDomain0[] = "chromium.org"; |
| 53 | const char kSearchDomain1[] = "google.com"; |
Peter Qiu | b25083f | 2014-08-25 13:22:31 -0700 | [diff] [blame] | 54 | const char kIPv6Address[] = "2001:db8::1"; |
| 55 | const char kIPv6NameServer0[] = "2001:db9::1"; |
| 56 | const char kIPv6NameServer1[] = "2001:db9::2"; |
Ben Chan | a6bfe87 | 2012-09-26 09:48:34 -0700 | [diff] [blame] | 57 | } // namespace |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 58 | |
| 59 | class ConnectionTest : public Test { |
| 60 | public: |
| 61 | ConnectionTest() |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 62 | : device_info_(new StrictMock<MockDeviceInfo>( |
| 63 | &control_, |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 64 | nullptr, |
| 65 | nullptr, |
| 66 | nullptr)), |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 67 | connection_(new Connection( |
| 68 | kTestDeviceInterfaceIndex0, |
| 69 | kTestDeviceName0, |
Paul Stewart | e00600e | 2012-03-16 07:08:00 -0700 | [diff] [blame] | 70 | Technology::kUnknown, |
mukesh agrawal | 23ac6b7 | 2013-01-31 18:52:37 -0800 | [diff] [blame] | 71 | device_info_.get())), |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 72 | ipconfig_(new IPConfig(&control_, kTestDeviceName0)), |
Peter Qiu | b25083f | 2014-08-25 13:22:31 -0700 | [diff] [blame] | 73 | ip6config_(new IPConfig(&control_, kTestDeviceName0)), |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 74 | local_address_(IPAddress::kFamilyIPv4), |
| 75 | broadcast_address_(IPAddress::kFamilyIPv4), |
| 76 | gateway_address_(IPAddress::kFamilyIPv4), |
Peter Qiu | b25083f | 2014-08-25 13:22:31 -0700 | [diff] [blame] | 77 | default_address_(IPAddress::kFamilyIPv4), |
| 78 | local_ipv6_address_(IPAddress::kFamilyIPv6) {} |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 79 | |
| 80 | virtual void SetUp() { |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 81 | ReplaceSingletons(connection_); |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 82 | properties_.address = kIPAddress0; |
| 83 | properties_.subnet_prefix = kPrefix0; |
| 84 | properties_.gateway = kGatewayAddress0; |
| 85 | properties_.broadcast_address = kBroadcastAddress0; |
| 86 | properties_.dns_servers.push_back(kNameServer0); |
| 87 | properties_.dns_servers.push_back(kNameServer1); |
| 88 | properties_.domain_search.push_back(kSearchDomain0); |
| 89 | properties_.domain_search.push_back(kSearchDomain1); |
| 90 | properties_.address_family = IPAddress::kFamilyIPv4; |
| 91 | UpdateProperties(); |
Peter Qiu | b25083f | 2014-08-25 13:22:31 -0700 | [diff] [blame] | 92 | ipv6_properties_.address = kIPv6Address; |
| 93 | ipv6_properties_.dns_servers.push_back(kIPv6NameServer0); |
| 94 | ipv6_properties_.dns_servers.push_back(kIPv6NameServer1); |
| 95 | ipv6_properties_.address_family = IPAddress::kFamilyIPv6; |
| 96 | UpdateIPv6Properties(); |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 97 | EXPECT_TRUE(local_address_.SetAddressFromString(kIPAddress0)); |
| 98 | EXPECT_TRUE(broadcast_address_.SetAddressFromString(kBroadcastAddress0)); |
| 99 | EXPECT_TRUE(gateway_address_.SetAddressFromString(kGatewayAddress0)); |
Peter Qiu | b25083f | 2014-08-25 13:22:31 -0700 | [diff] [blame] | 100 | EXPECT_TRUE(local_ipv6_address_.SetAddressFromString(kIPv6Address)); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 103 | virtual void TearDown() { |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 104 | AddDestructorExpectations(); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 105 | connection_ = nullptr; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 108 | void ReplaceSingletons(ConnectionRefPtr connection) { |
| 109 | connection->resolver_ = &resolver_; |
| 110 | connection->routing_table_ = &routing_table_; |
| 111 | connection->rtnl_handler_ = &rtnl_handler_; |
| 112 | } |
| 113 | |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 114 | void UpdateProperties() { |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 115 | ipconfig_->UpdateProperties(properties_); |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Peter Qiu | b25083f | 2014-08-25 13:22:31 -0700 | [diff] [blame] | 118 | void UpdateIPv6Properties() { |
| 119 | ip6config_->UpdateProperties(ipv6_properties_); |
| 120 | } |
| 121 | |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 122 | bool PinHostRoute(ConnectionRefPtr connection, |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 123 | const IPAddress trusted_ip, |
| 124 | const IPAddress gateway) { |
| 125 | return connection->PinHostRoute(trusted_ip, gateway); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 128 | const IPAddress &GetLocalAddress(ConnectionRefPtr connection) { |
| 129 | return connection->local_; |
| 130 | } |
| 131 | |
| 132 | const IPAddress &GetGatewayAddress(ConnectionRefPtr connection) { |
| 133 | return connection->gateway_; |
| 134 | } |
| 135 | |
| 136 | bool GetHasBroadcastDomain(ConnectionRefPtr connection) { |
| 137 | return connection->has_broadcast_domain_; |
| 138 | } |
| 139 | |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 140 | uint32_t GetDefaultMetric() { |
Paul Stewart | 05a42c2 | 2012-08-02 16:47:21 -0700 | [diff] [blame] | 141 | return Connection::kDefaultMetric; |
| 142 | } |
| 143 | |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 144 | uint32_t GetNonDefaultMetricBase() { |
Paul Stewart | 05a42c2 | 2012-08-02 16:47:21 -0700 | [diff] [blame] | 145 | return Connection::kNonDefaultMetricBase; |
| 146 | } |
| 147 | |
Paul Stewart | 2cb3fa7 | 2014-11-13 01:43:12 -0800 | [diff] [blame] | 148 | void SetLocal(const IPAddress &local) { |
| 149 | connection_->local_ = local; |
| 150 | } |
| 151 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 152 | protected: |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 153 | class DisconnectCallbackTarget { |
| 154 | public: |
| 155 | DisconnectCallbackTarget() |
| 156 | : callback_(base::Bind(&DisconnectCallbackTarget::CallTarget, |
| 157 | base::Unretained(this))) {} |
| 158 | |
| 159 | MOCK_METHOD0(CallTarget, void()); |
| 160 | const base::Closure &callback() { return callback_; } |
| 161 | |
| 162 | private: |
| 163 | base::Closure callback_; |
| 164 | }; |
| 165 | |
| 166 | void AddDestructorExpectations() { |
| 167 | EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex0)); |
| 168 | EXPECT_CALL(routing_table_, FlushRoutesWithTag(kTestDeviceInterfaceIndex0)); |
| 169 | EXPECT_CALL(*device_info_.get(), |
| 170 | FlushAddresses(kTestDeviceInterfaceIndex0)); |
| 171 | } |
| 172 | |
| 173 | // Returns a new test connection object. The caller usually needs to call |
| 174 | // AddDestructorExpectations before destroying the object. |
| 175 | ConnectionRefPtr GetNewConnection() { |
| 176 | ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex0, |
| 177 | kTestDeviceName0, |
| 178 | Technology::kUnknown, |
mukesh agrawal | 23ac6b7 | 2013-01-31 18:52:37 -0800 | [diff] [blame] | 179 | device_info_.get())); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 180 | ReplaceSingletons(connection); |
| 181 | return connection; |
| 182 | } |
| 183 | |
Ben Chan | 22f1fbc | 2014-10-17 14:18:07 -0700 | [diff] [blame] | 184 | std::unique_ptr<StrictMock<MockDeviceInfo>> device_info_; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 185 | ConnectionRefPtr connection_; |
| 186 | MockControl control_; |
| 187 | IPConfigRefPtr ipconfig_; |
Peter Qiu | b25083f | 2014-08-25 13:22:31 -0700 | [diff] [blame] | 188 | IPConfigRefPtr ip6config_; |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 189 | IPConfig::Properties properties_; |
Peter Qiu | b25083f | 2014-08-25 13:22:31 -0700 | [diff] [blame] | 190 | IPConfig::Properties ipv6_properties_; |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 191 | IPAddress local_address_; |
| 192 | IPAddress broadcast_address_; |
| 193 | IPAddress gateway_address_; |
| 194 | IPAddress default_address_; |
Peter Qiu | b25083f | 2014-08-25 13:22:31 -0700 | [diff] [blame] | 195 | IPAddress local_ipv6_address_; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 196 | StrictMock<MockResolver> resolver_; |
| 197 | StrictMock<MockRoutingTable> routing_table_; |
| 198 | StrictMock<MockRTNLHandler> rtnl_handler_; |
| 199 | }; |
| 200 | |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 201 | namespace { |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 202 | |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 203 | MATCHER_P2(IsIPAddress, address, prefix, "") { |
| 204 | IPAddress match_address(address); |
| 205 | match_address.set_prefix(prefix); |
| 206 | return match_address.Equals(arg); |
| 207 | } |
| 208 | |
Peter Qiu | b25083f | 2014-08-25 13:22:31 -0700 | [diff] [blame] | 209 | MATCHER_P(IsIPv6Address, address, "") { |
| 210 | IPAddress match_address(address); |
| 211 | return match_address.Equals(arg); |
| 212 | } |
| 213 | |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 214 | MATCHER(IsNonNullCallback, "") { |
| 215 | return !arg.is_null(); |
| 216 | } |
| 217 | |
| 218 | } // namespace |
| 219 | |
| 220 | TEST_F(ConnectionTest, InitState) { |
| 221 | EXPECT_EQ(kTestDeviceInterfaceIndex0, connection_->interface_index_); |
| 222 | EXPECT_EQ(kTestDeviceName0, connection_->interface_name_); |
| 223 | EXPECT_FALSE(connection_->is_default()); |
| 224 | EXPECT_FALSE(connection_->routing_request_count_); |
| 225 | } |
| 226 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 227 | TEST_F(ConnectionTest, AddConfig) { |
Paul Stewart | 05a42c2 | 2012-08-02 16:47:21 -0700 | [diff] [blame] | 228 | EXPECT_CALL(*device_info_, |
| 229 | HasOtherAddress(kTestDeviceInterfaceIndex0, |
| 230 | IsIPAddress(local_address_, kPrefix0))) |
| 231 | .WillOnce(Return(false)); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 232 | EXPECT_CALL(rtnl_handler_, |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 233 | AddInterfaceAddress(kTestDeviceInterfaceIndex0, |
| 234 | IsIPAddress(local_address_, kPrefix0), |
| 235 | IsIPAddress(broadcast_address_, 0), |
| 236 | IsIPAddress(default_address_, 0))); |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 237 | EXPECT_CALL(routing_table_, |
| 238 | SetDefaultRoute(kTestDeviceInterfaceIndex0, |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 239 | IsIPAddress(gateway_address_, 0), |
Paul Stewart | 05a42c2 | 2012-08-02 16:47:21 -0700 | [diff] [blame] | 240 | GetNonDefaultMetricBase() + |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 241 | kTestDeviceInterfaceIndex0)); |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 242 | EXPECT_CALL(routing_table_, |
| 243 | ConfigureRoutes(kTestDeviceInterfaceIndex0, |
| 244 | ipconfig_, |
Paul Stewart | 05a42c2 | 2012-08-02 16:47:21 -0700 | [diff] [blame] | 245 | GetDefaultMetric())); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 246 | connection_->UpdateFromIPConfig(ipconfig_); |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 247 | IPAddress test_local_address(local_address_); |
| 248 | test_local_address.set_prefix(kPrefix0); |
| 249 | EXPECT_TRUE(test_local_address.Equals(GetLocalAddress(connection_))); |
| 250 | EXPECT_TRUE(gateway_address_.Equals(GetGatewayAddress(connection_))); |
| 251 | EXPECT_TRUE(GetHasBroadcastDomain(connection_)); |
Peter Qiu | b25083f | 2014-08-25 13:22:31 -0700 | [diff] [blame] | 252 | EXPECT_FALSE(connection_->IsIPv6()); |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 253 | |
| 254 | EXPECT_CALL(routing_table_, |
| 255 | CreateLinkRoute(kTestDeviceInterfaceIndex0, |
| 256 | IsIPAddress(local_address_, kPrefix0), |
| 257 | IsIPAddress(gateway_address_, 0))) |
| 258 | .WillOnce(Return(true)) |
| 259 | .WillOnce(Return(false)); |
| 260 | EXPECT_TRUE(connection_->CreateGatewayRoute()); |
| 261 | EXPECT_FALSE(connection_->CreateGatewayRoute()); |
| 262 | connection_->has_broadcast_domain_ = false; |
| 263 | EXPECT_FALSE(connection_->CreateGatewayRoute()); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 264 | |
| 265 | EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0, |
Paul Stewart | 05a42c2 | 2012-08-02 16:47:21 -0700 | [diff] [blame] | 266 | GetDefaultMetric())); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 267 | EXPECT_CALL(resolver_, SetDNSFromLists( |
| 268 | ipconfig_->properties().dns_servers, |
mukesh agrawal | 23ac6b7 | 2013-01-31 18:52:37 -0800 | [diff] [blame] | 269 | ipconfig_->properties().domain_search)); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 270 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 271 | scoped_refptr<MockDevice> device(new StrictMock<MockDevice>( |
| 272 | &control_, |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 273 | nullptr, |
| 274 | nullptr, |
| 275 | nullptr, |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 276 | kTestDeviceName0, |
| 277 | string(), |
| 278 | kTestDeviceInterfaceIndex0)); |
| 279 | EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0)) |
| 280 | .WillOnce(Return(device)); |
| 281 | EXPECT_CALL(*device.get(), RequestPortalDetection()) |
| 282 | .WillOnce(Return(true)); |
Paul Stewart | e78ec54 | 2012-06-08 18:28:50 -0700 | [diff] [blame] | 283 | EXPECT_CALL(routing_table_, FlushCache()) |
| 284 | .WillOnce(Return(true)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 285 | connection_->SetIsDefault(true); |
Paul Stewart | e78ec54 | 2012-06-08 18:28:50 -0700 | [diff] [blame] | 286 | Mock::VerifyAndClearExpectations(&routing_table_); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 287 | EXPECT_TRUE(connection_->is_default()); |
| 288 | |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 289 | EXPECT_CALL(routing_table_, |
| 290 | SetDefaultMetric(kTestDeviceInterfaceIndex0, |
Paul Stewart | 05a42c2 | 2012-08-02 16:47:21 -0700 | [diff] [blame] | 291 | GetNonDefaultMetricBase() + |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 292 | kTestDeviceInterfaceIndex0)); |
Paul Stewart | e78ec54 | 2012-06-08 18:28:50 -0700 | [diff] [blame] | 293 | EXPECT_CALL(routing_table_, FlushCache()) |
| 294 | .WillOnce(Return(true)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 295 | connection_->SetIsDefault(false); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 296 | EXPECT_FALSE(connection_->is_default()); |
| 297 | } |
| 298 | |
Peter Qiu | b25083f | 2014-08-25 13:22:31 -0700 | [diff] [blame] | 299 | TEST_F(ConnectionTest, AddConfigIPv6) { |
| 300 | EXPECT_CALL(*device_info_, |
| 301 | HasOtherAddress(kTestDeviceInterfaceIndex0, |
| 302 | IsIPv6Address(local_ipv6_address_))) |
| 303 | .WillOnce(Return(false)); |
| 304 | EXPECT_CALL(rtnl_handler_, |
| 305 | AddInterfaceAddress(kTestDeviceInterfaceIndex0, |
| 306 | IsIPv6Address(local_ipv6_address_), |
| 307 | _, |
| 308 | _)); |
| 309 | EXPECT_CALL(routing_table_, |
| 310 | ConfigureRoutes(kTestDeviceInterfaceIndex0, |
| 311 | ip6config_, |
| 312 | GetDefaultMetric())); |
| 313 | connection_->UpdateFromIPConfig(ip6config_); |
| 314 | IPAddress test_local_address(local_ipv6_address_); |
| 315 | EXPECT_TRUE(test_local_address.Equals(GetLocalAddress(connection_))); |
| 316 | EXPECT_TRUE(connection_->IsIPv6()); |
| 317 | } |
| 318 | |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 319 | TEST_F(ConnectionTest, AddConfigWithPeer) { |
| 320 | const string kPeerAddress("192.168.1.222"); |
| 321 | IPAddress peer_address(IPAddress::kFamilyIPv4); |
| 322 | EXPECT_TRUE(peer_address.SetAddressFromString(kPeerAddress)); |
| 323 | properties_.peer_address = kPeerAddress; |
| 324 | properties_.gateway = string(); |
| 325 | UpdateProperties(); |
Paul Stewart | 05a42c2 | 2012-08-02 16:47:21 -0700 | [diff] [blame] | 326 | EXPECT_CALL(*device_info_, |
| 327 | HasOtherAddress(kTestDeviceInterfaceIndex0, |
| 328 | IsIPAddress(local_address_, kPrefix0))) |
| 329 | .WillOnce(Return(false)); |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 330 | EXPECT_CALL(rtnl_handler_, |
| 331 | AddInterfaceAddress(kTestDeviceInterfaceIndex0, |
| 332 | IsIPAddress(local_address_, kPrefix0), |
| 333 | IsIPAddress(broadcast_address_, 0), |
| 334 | IsIPAddress(peer_address, 0))); |
| 335 | EXPECT_CALL(routing_table_, SetDefaultRoute(_, _, _)).Times(0); |
| 336 | EXPECT_CALL(routing_table_, |
| 337 | ConfigureRoutes(kTestDeviceInterfaceIndex0, |
| 338 | ipconfig_, |
Paul Stewart | 05a42c2 | 2012-08-02 16:47:21 -0700 | [diff] [blame] | 339 | GetDefaultMetric())); |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 340 | connection_->UpdateFromIPConfig(ipconfig_); |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 341 | EXPECT_FALSE(GetHasBroadcastDomain(connection_)); |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | TEST_F(ConnectionTest, AddConfigWithBrokenNetmask) { |
| 345 | // Assign a prefix that makes the gateway unreachable. |
| 346 | properties_.subnet_prefix = kPrefix1; |
| 347 | UpdateProperties(); |
| 348 | |
| 349 | // Connection should override with a prefix which will allow the |
| 350 | // gateway to be reachable. |
Paul Stewart | 05a42c2 | 2012-08-02 16:47:21 -0700 | [diff] [blame] | 351 | EXPECT_CALL(*device_info_, |
| 352 | HasOtherAddress(kTestDeviceInterfaceIndex0, |
| 353 | IsIPAddress(local_address_, kPrefix0))) |
| 354 | .WillOnce(Return(false)); |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 355 | EXPECT_CALL(rtnl_handler_, |
| 356 | AddInterfaceAddress(kTestDeviceInterfaceIndex0, |
| 357 | IsIPAddress(local_address_, kPrefix0), |
| 358 | IsIPAddress(broadcast_address_, 0), |
| 359 | IsIPAddress(default_address_, 0))); |
| 360 | EXPECT_CALL(routing_table_, |
| 361 | SetDefaultRoute(kTestDeviceInterfaceIndex0, |
| 362 | IsIPAddress(gateway_address_, 0), |
Paul Stewart | 05a42c2 | 2012-08-02 16:47:21 -0700 | [diff] [blame] | 363 | GetNonDefaultMetricBase() + |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 364 | kTestDeviceInterfaceIndex0)); |
| 365 | EXPECT_CALL(routing_table_, |
| 366 | ConfigureRoutes(kTestDeviceInterfaceIndex0, |
| 367 | ipconfig_, |
Paul Stewart | 05a42c2 | 2012-08-02 16:47:21 -0700 | [diff] [blame] | 368 | GetDefaultMetric())); |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 369 | connection_->UpdateFromIPConfig(ipconfig_); |
| 370 | |
| 371 | // Assign a gateway address that violates the minimum plausible prefix |
| 372 | // the Connection can assign. |
| 373 | properties_.gateway = kGatewayAddress1; |
| 374 | UpdateProperties(); |
| 375 | |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 376 | IPAddress gateway_address1(IPAddress::kFamilyIPv4); |
| 377 | EXPECT_TRUE(gateway_address1.SetAddressFromString(kGatewayAddress1)); |
| 378 | // Connection cannot override this prefix, so it will switch to a |
| 379 | // model where the peer address is set to the value of the gateway |
| 380 | // address. |
Paul Stewart | 05a42c2 | 2012-08-02 16:47:21 -0700 | [diff] [blame] | 381 | EXPECT_CALL(*device_info_, |
| 382 | HasOtherAddress(kTestDeviceInterfaceIndex0, |
| 383 | IsIPAddress(local_address_, kPrefix1))) |
| 384 | .WillOnce(Return(false)); |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 385 | EXPECT_CALL(rtnl_handler_, |
| 386 | AddInterfaceAddress(kTestDeviceInterfaceIndex0, |
| 387 | IsIPAddress(local_address_, kPrefix1), |
| 388 | IsIPAddress(broadcast_address_, 0), |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 389 | IsIPAddress(gateway_address1, 0))); |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 390 | EXPECT_CALL(routing_table_, |
| 391 | SetDefaultRoute(kTestDeviceInterfaceIndex0, _, _)); |
| 392 | EXPECT_CALL(routing_table_, |
| 393 | ConfigureRoutes(kTestDeviceInterfaceIndex0, _, _)); |
| 394 | connection_->UpdateFromIPConfig(ipconfig_); |
| 395 | } |
| 396 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 397 | TEST_F(ConnectionTest, AddConfigReverse) { |
| 398 | EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0, |
Paul Stewart | 05a42c2 | 2012-08-02 16:47:21 -0700 | [diff] [blame] | 399 | GetDefaultMetric())); |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 400 | vector<string> empty_list; |
mukesh agrawal | 23ac6b7 | 2013-01-31 18:52:37 -0800 | [diff] [blame] | 401 | EXPECT_CALL(resolver_, SetDNSFromLists(empty_list, empty_list)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 402 | scoped_refptr<MockDevice> device(new StrictMock<MockDevice>( |
| 403 | &control_, |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 404 | nullptr, |
| 405 | nullptr, |
| 406 | nullptr, |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 407 | kTestDeviceName0, |
| 408 | string(), |
| 409 | kTestDeviceInterfaceIndex0)); |
| 410 | EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0)) |
| 411 | .WillOnce(Return(device)); |
| 412 | EXPECT_CALL(*device.get(), RequestPortalDetection()) |
| 413 | .WillOnce(Return(true)); |
Paul Stewart | e78ec54 | 2012-06-08 18:28:50 -0700 | [diff] [blame] | 414 | EXPECT_CALL(routing_table_, FlushCache()) |
| 415 | .WillOnce(Return(true)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 416 | connection_->SetIsDefault(true); |
Paul Stewart | e78ec54 | 2012-06-08 18:28:50 -0700 | [diff] [blame] | 417 | Mock::VerifyAndClearExpectations(&routing_table_); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 418 | |
Paul Stewart | 05a42c2 | 2012-08-02 16:47:21 -0700 | [diff] [blame] | 419 | EXPECT_CALL(*device_info_, |
| 420 | HasOtherAddress(kTestDeviceInterfaceIndex0, |
| 421 | IsIPAddress(local_address_, kPrefix0))) |
| 422 | .WillOnce(Return(false)); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 423 | EXPECT_CALL(rtnl_handler_, |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 424 | AddInterfaceAddress(kTestDeviceInterfaceIndex0, |
| 425 | IsIPAddress(local_address_, kPrefix0), |
| 426 | IsIPAddress(broadcast_address_, 0), |
| 427 | IsIPAddress(default_address_, 0))); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 428 | EXPECT_CALL(routing_table_, SetDefaultRoute(kTestDeviceInterfaceIndex0, |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 429 | IsIPAddress(gateway_address_, 0), |
Paul Stewart | 05a42c2 | 2012-08-02 16:47:21 -0700 | [diff] [blame] | 430 | GetDefaultMetric())); |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 431 | EXPECT_CALL(routing_table_, |
| 432 | ConfigureRoutes(kTestDeviceInterfaceIndex0, |
| 433 | ipconfig_, |
Paul Stewart | 05a42c2 | 2012-08-02 16:47:21 -0700 | [diff] [blame] | 434 | GetDefaultMetric())); |
mukesh agrawal | 23ac6b7 | 2013-01-31 18:52:37 -0800 | [diff] [blame] | 435 | EXPECT_CALL(resolver_, |
| 436 | SetDNSFromLists(ipconfig_->properties().dns_servers, |
| 437 | ipconfig_->properties().domain_search)); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 438 | |
| 439 | connection_->UpdateFromIPConfig(ipconfig_); |
| 440 | } |
| 441 | |
Paul Stewart | 4d1868b | 2012-09-10 11:58:46 -0700 | [diff] [blame] | 442 | TEST_F(ConnectionTest, AddConfigWithDNSDomain) { |
| 443 | const string kDomainName("chromium.org"); |
| 444 | properties_.domain_search.clear(); |
| 445 | properties_.domain_name = kDomainName; |
| 446 | UpdateProperties(); |
| 447 | EXPECT_CALL(*device_info_, HasOtherAddress(_, _)) |
| 448 | .WillOnce(Return(false)); |
| 449 | EXPECT_CALL(rtnl_handler_, AddInterfaceAddress(_, _, _, _)); |
| 450 | EXPECT_CALL(routing_table_, SetDefaultRoute(_, _, _)); |
| 451 | EXPECT_CALL(routing_table_, ConfigureRoutes(_, _, _)); |
| 452 | connection_->UpdateFromIPConfig(ipconfig_); |
| 453 | |
| 454 | EXPECT_CALL(routing_table_, SetDefaultMetric(_, _)); |
| 455 | vector<string> domain_search_list; |
| 456 | domain_search_list.push_back(kDomainName + "."); |
mukesh agrawal | 23ac6b7 | 2013-01-31 18:52:37 -0800 | [diff] [blame] | 457 | EXPECT_CALL(resolver_, SetDNSFromLists(_, domain_search_list)); |
Paul Stewart | 4d1868b | 2012-09-10 11:58:46 -0700 | [diff] [blame] | 458 | DeviceRefPtr device; |
| 459 | EXPECT_CALL(*device_info_, GetDevice(_)).WillOnce(Return(device)); |
| 460 | EXPECT_CALL(routing_table_, FlushCache()).WillOnce(Return(true)); |
| 461 | connection_->SetIsDefault(true); |
| 462 | } |
| 463 | |
Paul Stewart | 05a42c2 | 2012-08-02 16:47:21 -0700 | [diff] [blame] | 464 | TEST_F(ConnectionTest, HasOtherAddress) { |
| 465 | EXPECT_CALL(*device_info_, |
| 466 | HasOtherAddress(kTestDeviceInterfaceIndex0, |
| 467 | IsIPAddress(local_address_, kPrefix0))) |
| 468 | .WillOnce(Return(true)); |
| 469 | EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex0)); |
| 470 | EXPECT_CALL(*device_info_, FlushAddresses(kTestDeviceInterfaceIndex0)); |
| 471 | EXPECT_CALL(rtnl_handler_, |
| 472 | AddInterfaceAddress(kTestDeviceInterfaceIndex0, |
| 473 | IsIPAddress(local_address_, kPrefix0), |
| 474 | IsIPAddress(broadcast_address_, 0), |
| 475 | IsIPAddress(default_address_, 0))); |
| 476 | EXPECT_CALL(routing_table_, |
| 477 | SetDefaultRoute(kTestDeviceInterfaceIndex0, |
| 478 | IsIPAddress(gateway_address_, 0), |
| 479 | GetNonDefaultMetricBase() + |
| 480 | kTestDeviceInterfaceIndex0)); |
| 481 | EXPECT_CALL(routing_table_, |
| 482 | ConfigureRoutes(kTestDeviceInterfaceIndex0, |
| 483 | ipconfig_, |
| 484 | GetDefaultMetric())); |
| 485 | connection_->UpdateFromIPConfig(ipconfig_); |
| 486 | } |
| 487 | |
Peter Qiu | a89154b | 2014-05-23 15:45:42 -0700 | [diff] [blame] | 488 | TEST_F(ConnectionTest, UpdateDNSServers) { |
| 489 | const char* kDnsServers[] = {"1.1.1.1", "1.1.1.2"}; |
| 490 | vector<string> dns_servers(kDnsServers, std::end(kDnsServers)); |
| 491 | |
| 492 | // Non-default connection. |
| 493 | connection_->is_default_ = false; |
| 494 | EXPECT_CALL(resolver_, SetDNSFromLists(_, _)).Times(0); |
| 495 | connection_->UpdateDNSServers(dns_servers); |
| 496 | Mock::VerifyAndClearExpectations(&resolver_); |
| 497 | |
| 498 | // Default connection. |
| 499 | connection_->is_default_ = true; |
| 500 | EXPECT_CALL(resolver_, SetDNSFromLists(dns_servers, _)); |
| 501 | connection_->UpdateDNSServers(dns_servers); |
| 502 | Mock::VerifyAndClearExpectations(&resolver_); |
| 503 | } |
| 504 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 505 | TEST_F(ConnectionTest, RouteRequest) { |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 506 | ConnectionRefPtr connection = GetNewConnection(); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 507 | scoped_refptr<MockDevice> device(new StrictMock<MockDevice>( |
| 508 | &control_, |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 509 | nullptr, |
| 510 | nullptr, |
| 511 | nullptr, |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 512 | kTestDeviceName0, |
| 513 | string(), |
| 514 | kTestDeviceInterfaceIndex0)); |
| 515 | EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0)) |
| 516 | .WillRepeatedly(Return(device)); |
Paul Stewart | 2cb3fa7 | 2014-11-13 01:43:12 -0800 | [diff] [blame] | 517 | EXPECT_CALL(*device.get(), SetLooseRouting(true)).Times(1); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 518 | connection->RequestRouting(); |
| 519 | connection->RequestRouting(); |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 520 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 521 | // The first release should only decrement the reference counter. |
| 522 | connection->ReleaseRouting(); |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 523 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 524 | // Another release will re-enable reverse-path filter. |
Paul Stewart | 2cb3fa7 | 2014-11-13 01:43:12 -0800 | [diff] [blame] | 525 | EXPECT_CALL(*device.get(), SetLooseRouting(false)); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 526 | EXPECT_CALL(routing_table_, FlushCache()); |
| 527 | connection->ReleaseRouting(); |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 528 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 529 | // The destructor will remove the routes and addresses. |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 530 | AddDestructorExpectations(); |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 531 | } |
| 532 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 533 | TEST_F(ConnectionTest, Destructor) { |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 534 | ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex1, |
| 535 | kTestDeviceName1, |
| 536 | Technology::kUnknown, |
mukesh agrawal | 23ac6b7 | 2013-01-31 18:52:37 -0800 | [diff] [blame] | 537 | device_info_.get())); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 538 | connection->resolver_ = &resolver_; |
| 539 | connection->routing_table_ = &routing_table_; |
| 540 | connection->rtnl_handler_ = &rtnl_handler_; |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 541 | EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex1)); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 542 | EXPECT_CALL(routing_table_, FlushRoutesWithTag(kTestDeviceInterfaceIndex1)); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 543 | EXPECT_CALL(*device_info_, FlushAddresses(kTestDeviceInterfaceIndex1)); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 544 | connection = nullptr; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 547 | TEST_F(ConnectionTest, RequestHostRoute) { |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 548 | ConnectionRefPtr connection = GetNewConnection(); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 549 | IPAddress address(IPAddress::kFamilyIPv4); |
| 550 | ASSERT_TRUE(address.SetAddressFromString(kIPAddress0)); |
| 551 | size_t prefix_len = address.GetLength() * 8; |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 552 | EXPECT_CALL(routing_table_, |
| 553 | RequestRouteToHost(IsIPAddress(address, prefix_len), |
| 554 | -1, |
| 555 | kTestDeviceInterfaceIndex0, |
| 556 | IsNonNullCallback())) |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 557 | .WillOnce(Return(true)); |
| 558 | EXPECT_TRUE(connection->RequestHostRoute(address)); |
| 559 | |
| 560 | // The destructor will remove the routes and addresses. |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 561 | AddDestructorExpectations(); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Ben Chan | a016312 | 2012-09-25 15:10:52 -0700 | [diff] [blame] | 564 | TEST_F(ConnectionTest, BlackholeIPv6) { |
| 565 | properties_.blackhole_ipv6 = true; |
| 566 | UpdateProperties(); |
| 567 | EXPECT_CALL(*device_info_, HasOtherAddress(_, _)) |
| 568 | .WillOnce(Return(false)); |
| 569 | EXPECT_CALL(rtnl_handler_, AddInterfaceAddress(_, _, _, _)); |
| 570 | EXPECT_CALL(routing_table_, SetDefaultRoute(_, _, _)); |
| 571 | EXPECT_CALL(routing_table_, ConfigureRoutes(_, _, _)); |
| 572 | EXPECT_CALL(routing_table_, |
| 573 | CreateBlackholeRoute(kTestDeviceInterfaceIndex0, |
| 574 | IPAddress::kFamilyIPv6, |
| 575 | Connection::kDefaultMetric)) |
| 576 | .WillOnce(Return(true)); |
| 577 | connection_->UpdateFromIPConfig(ipconfig_); |
| 578 | } |
| 579 | |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 580 | TEST_F(ConnectionTest, PinHostRoute) { |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 581 | ConnectionRefPtr connection = GetNewConnection(); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 582 | |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 583 | IPAddress gateway(IPAddress::kFamilyIPv4); |
| 584 | IPAddress trusted_ip(IPAddress::kFamilyIPv4); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 585 | |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 586 | // Should fail because neither IP address is set. |
| 587 | EXPECT_FALSE(PinHostRoute(connection, trusted_ip, gateway)); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 588 | |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 589 | static const char kGateway[] = "10.242.2.13"; |
| 590 | ASSERT_TRUE(gateway.SetAddressFromString(kGateway)); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 591 | |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 592 | // Should fail because trusted IP is not set. |
| 593 | EXPECT_FALSE(PinHostRoute(connection, trusted_ip, gateway)); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 594 | |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 595 | static const char kTrustedIP[] = "10.0.1.1"; |
| 596 | ASSERT_TRUE(trusted_ip.SetAddressFromString(kTrustedIP)); |
| 597 | |
Paul Stewart | e435d34 | 2013-09-27 16:41:00 -0700 | [diff] [blame] | 598 | // Should pass without calling RequestRouteToHost since if the gateway |
| 599 | // is not set, there is no work to be done. |
| 600 | EXPECT_CALL(routing_table_, RequestRouteToHost(_, _, _, _)).Times(0); |
| 601 | EXPECT_TRUE(PinHostRoute(connection, trusted_ip, |
| 602 | IPAddress(gateway.family()))); |
| 603 | Mock::VerifyAndClearExpectations(&routing_table_); |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 604 | |
| 605 | size_t prefix_len = IPAddress::GetMaxPrefixLength(trusted_ip.family()); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 606 | EXPECT_CALL(routing_table_, RequestRouteToHost( |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 607 | IsIPAddress(trusted_ip, prefix_len), -1, kTestDeviceInterfaceIndex0, _)) |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 608 | .WillOnce(Return(false)); |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 609 | EXPECT_FALSE(PinHostRoute(connection, trusted_ip, gateway)); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 610 | |
| 611 | EXPECT_CALL(routing_table_, RequestRouteToHost( |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 612 | IsIPAddress(trusted_ip, prefix_len), -1, kTestDeviceInterfaceIndex0, _)) |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 613 | .WillOnce(Return(true)); |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 614 | EXPECT_TRUE(PinHostRoute(connection, trusted_ip, gateway)); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 615 | |
| 616 | // The destructor will remove the routes and addresses. |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 617 | AddDestructorExpectations(); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 618 | } |
| 619 | |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 620 | TEST_F(ConnectionTest, FixGatewayReachability) { |
| 621 | static const char kLocal[] = "10.242.2.13"; |
| 622 | IPAddress local(IPAddress::kFamilyIPv4); |
| 623 | ASSERT_TRUE(local.SetAddressFromString(kLocal)); |
| 624 | const int kPrefix = 24; |
| 625 | local.set_prefix(kPrefix); |
| 626 | IPAddress gateway(IPAddress::kFamilyIPv4); |
| 627 | IPAddress peer(IPAddress::kFamilyIPv4); |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 628 | IPAddress trusted_ip(IPAddress::kFamilyIPv4); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 629 | |
| 630 | // Should fail because no gateway is set. |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 631 | EXPECT_FALSE(Connection::FixGatewayReachability( |
| 632 | &local, &peer, &gateway, trusted_ip)); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 633 | EXPECT_EQ(kPrefix, local.prefix()); |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 634 | EXPECT_FALSE(peer.IsValid()); |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 635 | EXPECT_FALSE(gateway.IsValid()); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 636 | |
| 637 | // Should succeed because with the given prefix, this gateway is reachable. |
| 638 | static const char kReachableGateway[] = "10.242.2.14"; |
| 639 | ASSERT_TRUE(gateway.SetAddressFromString(kReachableGateway)); |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 640 | IPAddress gateway_backup(gateway); |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 641 | peer = IPAddress(IPAddress::kFamilyIPv4); |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 642 | EXPECT_TRUE(Connection::FixGatewayReachability( |
| 643 | &local, &peer, &gateway, trusted_ip)); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 644 | // Prefix should remain unchanged. |
| 645 | EXPECT_EQ(kPrefix, local.prefix()); |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 646 | // Peer should remain unchanged. |
| 647 | EXPECT_FALSE(peer.IsValid()); |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 648 | // Gateway should remain unchanged. |
| 649 | EXPECT_TRUE(gateway_backup.Equals(gateway)); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 650 | |
| 651 | // Should succeed because we modified the prefix to match the gateway. |
| 652 | static const char kExpandableGateway[] = "10.242.3.14"; |
| 653 | ASSERT_TRUE(gateway.SetAddressFromString(kExpandableGateway)); |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 654 | gateway_backup = gateway; |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 655 | peer = IPAddress(IPAddress::kFamilyIPv4); |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 656 | EXPECT_TRUE(Connection::FixGatewayReachability( |
| 657 | &local, &peer, &gateway, trusted_ip)); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 658 | // Prefix should have opened up by 1 bit. |
| 659 | EXPECT_EQ(kPrefix - 1, local.prefix()); |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 660 | // Peer should remain unchanged. |
| 661 | EXPECT_FALSE(peer.IsValid()); |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 662 | // Gateway should remain unchanged. |
| 663 | EXPECT_TRUE(gateway_backup.Equals(gateway)); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 664 | |
Paul Stewart | 2aa5d7d | 2012-06-21 22:16:54 -0700 | [diff] [blame] | 665 | // Should change models to assuming point-to-point because we cannot |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 666 | // plausibly expand the prefix past 8. |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 667 | local.set_prefix(kPrefix); |
| 668 | static const char kUnreachableGateway[] = "11.242.2.14"; |
| 669 | ASSERT_TRUE(gateway.SetAddressFromString(kUnreachableGateway)); |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 670 | gateway_backup = gateway; |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 671 | peer = IPAddress(IPAddress::kFamilyIPv4); |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 672 | EXPECT_TRUE(Connection::FixGatewayReachability( |
| 673 | &local, &peer, &gateway, trusted_ip)); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 674 | // Prefix should not have changed. |
| 675 | EXPECT_EQ(kPrefix, local.prefix()); |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 676 | // Peer address should be set to the gateway address. |
| 677 | EXPECT_TRUE(peer.Equals(gateway)); |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 678 | // Gateway should remain unchanged. |
| 679 | EXPECT_TRUE(gateway_backup.Equals(gateway)); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 680 | |
Paul Stewart | 2aa5d7d | 2012-06-21 22:16:54 -0700 | [diff] [blame] | 681 | // Should also use point-to-point model if the netmask is set to the |
| 682 | // "all-ones" addresss, even if this address could have been made |
| 683 | // accessible by plausibly changing the prefix. |
| 684 | const int kIPv4MaxPrefix = |
| 685 | IPAddress::GetMaxPrefixLength(IPAddress::kFamilyIPv4); |
| 686 | local.set_prefix(kIPv4MaxPrefix); |
| 687 | ASSERT_TRUE(gateway.SetAddressFromString(kExpandableGateway)); |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 688 | gateway_backup = gateway; |
Paul Stewart | 2aa5d7d | 2012-06-21 22:16:54 -0700 | [diff] [blame] | 689 | peer = IPAddress(IPAddress::kFamilyIPv4); |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 690 | EXPECT_TRUE(Connection::FixGatewayReachability( |
| 691 | &local, &peer, &gateway, trusted_ip)); |
Paul Stewart | 2aa5d7d | 2012-06-21 22:16:54 -0700 | [diff] [blame] | 692 | // Prefix should not have changed. |
| 693 | EXPECT_EQ(kIPv4MaxPrefix, local.prefix()); |
| 694 | // Peer address should be set to the gateway address. |
| 695 | EXPECT_TRUE(peer.Equals(gateway)); |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 696 | // Gateway should remain unchanged. |
| 697 | EXPECT_TRUE(gateway_backup.Equals(gateway)); |
Paul Stewart | 2aa5d7d | 2012-06-21 22:16:54 -0700 | [diff] [blame] | 698 | |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 699 | // If this is a peer-to-peer interface and the peer matches the gateway, |
| 700 | // we should succeed. |
Paul Stewart | 2aa5d7d | 2012-06-21 22:16:54 -0700 | [diff] [blame] | 701 | local.set_prefix(kPrefix); |
| 702 | ASSERT_TRUE(gateway.SetAddressFromString(kUnreachableGateway)); |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 703 | gateway_backup = gateway; |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 704 | ASSERT_TRUE(peer.SetAddressFromString(kUnreachableGateway)); |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 705 | EXPECT_TRUE(Connection::FixGatewayReachability( |
| 706 | &local, &peer, &gateway, trusted_ip)); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 707 | EXPECT_EQ(kPrefix, local.prefix()); |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 708 | EXPECT_TRUE(peer.Equals(gateway)); |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 709 | EXPECT_TRUE(gateway_backup.Equals(gateway)); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 710 | |
| 711 | // If there is a peer specified and it does not match the gateway (even |
| 712 | // if it was reachable via netmask), we should fail. |
| 713 | ASSERT_TRUE(gateway.SetAddressFromString(kReachableGateway)); |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 714 | EXPECT_FALSE(Connection::FixGatewayReachability( |
| 715 | &local, &peer, &gateway, trusted_ip)); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 716 | EXPECT_EQ(kPrefix, local.prefix()); |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 717 | EXPECT_FALSE(peer.Equals(gateway)); |
Paul Stewart | 73fcc3f | 2013-02-25 12:16:53 -0800 | [diff] [blame] | 718 | |
| 719 | // If this is a peer-to-peer interface and the peer matches the gateway, |
| 720 | // but it also matches the trusted IP address, the gateway and peer address |
| 721 | // should be modified to allow routing to work correctly. |
| 722 | ASSERT_TRUE(gateway.SetAddressFromString(kUnreachableGateway)); |
| 723 | ASSERT_TRUE(peer.SetAddressFromString(kUnreachableGateway)); |
| 724 | ASSERT_TRUE(trusted_ip.SetAddressFromString(kUnreachableGateway)); |
| 725 | EXPECT_TRUE(Connection::FixGatewayReachability( |
| 726 | &local, &peer, &gateway, trusted_ip)); |
| 727 | EXPECT_TRUE(peer.IsDefault()); |
| 728 | EXPECT_TRUE(gateway.IsDefault()); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 729 | } |
| 730 | |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 731 | TEST_F(ConnectionTest, Binders) { |
| 732 | EXPECT_TRUE(connection_->binders_.empty()); |
| 733 | DisconnectCallbackTarget target0; |
| 734 | DisconnectCallbackTarget target1; |
| 735 | DisconnectCallbackTarget target2; |
| 736 | DisconnectCallbackTarget target3; |
| 737 | Connection::Binder binder0("binder0", target0.callback()); |
| 738 | Connection::Binder binder1("binder1", target1.callback()); |
| 739 | Connection::Binder binder2("binder2", target2.callback()); |
| 740 | Connection::Binder binder3("binder3", target3.callback()); |
| 741 | |
| 742 | binder0.Attach(connection_); |
| 743 | binder1.Attach(connection_); |
| 744 | |
| 745 | EXPECT_CALL(target1, CallTarget()).Times(0); |
| 746 | binder1.Attach(connection_); |
| 747 | |
| 748 | binder3.Attach(connection_); |
| 749 | binder2.Attach(connection_); |
| 750 | |
| 751 | EXPECT_CALL(target3, CallTarget()).Times(0); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 752 | binder3.Attach(nullptr); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 753 | |
| 754 | ASSERT_EQ(3, connection_->binders_.size()); |
| 755 | EXPECT_TRUE(connection_->binders_.at(0) == &binder0); |
| 756 | EXPECT_TRUE(connection_->binders_.at(1) == &binder1); |
| 757 | EXPECT_TRUE(connection_->binders_.at(2) == &binder2); |
| 758 | |
| 759 | EXPECT_CALL(target0, CallTarget()).Times(1); |
| 760 | EXPECT_CALL(target1, CallTarget()).Times(1); |
| 761 | EXPECT_CALL(target2, CallTarget()).Times(1); |
| 762 | connection_->NotifyBindersOnDisconnect(); |
| 763 | EXPECT_TRUE(connection_->binders_.empty()); |
| 764 | |
| 765 | // Should be a no-op. |
| 766 | connection_->NotifyBindersOnDisconnect(); |
| 767 | } |
| 768 | |
| 769 | TEST_F(ConnectionTest, Binder) { |
| 770 | // No connection should be bound initially. |
| 771 | Connection::Binder *binder = &connection_->lower_binder_; |
| 772 | EXPECT_EQ(connection_->interface_name(), binder->name_); |
| 773 | EXPECT_FALSE(binder->client_disconnect_callback_.is_null()); |
| 774 | EXPECT_FALSE(binder->IsBound()); |
| 775 | |
| 776 | ConnectionRefPtr connection1 = GetNewConnection(); |
| 777 | EXPECT_TRUE(connection1->binders_.empty()); |
| 778 | |
| 779 | // Bind lower |connection1| and check if it's bound. |
| 780 | binder->Attach(connection1); |
| 781 | EXPECT_TRUE(binder->IsBound()); |
| 782 | EXPECT_EQ(connection1.get(), binder->connection().get()); |
| 783 | ASSERT_FALSE(connection1->binders_.empty()); |
| 784 | EXPECT_TRUE(binder == connection1->binders_.at(0)); |
| 785 | |
| 786 | // Unbind lower |connection1| and check if it's unbound. |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 787 | binder->Attach(nullptr); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 788 | EXPECT_FALSE(binder->IsBound()); |
| 789 | EXPECT_TRUE(connection1->binders_.empty()); |
| 790 | |
| 791 | ConnectionRefPtr connection2 = GetNewConnection(); |
| 792 | |
| 793 | // Bind lower |connection1| to upper |connection2| and destroy the upper |
| 794 | // |connection2|. Make sure lower |connection1| is unbound (i.e., the |
| 795 | // disconnect callback is deregistered). |
| 796 | connection2->lower_binder_.Attach(connection1); |
| 797 | EXPECT_FALSE(connection1->binders_.empty()); |
| 798 | AddDestructorExpectations(); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 799 | connection2 = nullptr; |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 800 | EXPECT_TRUE(connection1->binders_.empty()); |
| 801 | |
| 802 | // Bind lower |connection1| to upper |connection_| and destroy lower |
| 803 | // |connection1|. Make sure lower |connection1| is unbound from upper |
| 804 | // |connection_| and upper |connection_|'s registered disconnect callbacks are |
| 805 | // run. |
| 806 | binder->Attach(connection1); |
| 807 | DisconnectCallbackTarget target; |
| 808 | Connection::Binder test_binder("from_test", target.callback()); |
| 809 | test_binder.Attach(connection_); |
| 810 | EXPECT_CALL(target, CallTarget()).Times(1); |
| 811 | ASSERT_FALSE(connection_->binders_.empty()); |
| 812 | AddDestructorExpectations(); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 813 | connection1 = nullptr; |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 814 | EXPECT_FALSE(binder->IsBound()); |
| 815 | EXPECT_FALSE(test_binder.IsBound()); |
| 816 | EXPECT_TRUE(connection_->binders_.empty()); |
| 817 | |
| 818 | { |
| 819 | // Binding a connection to itself should be safe. |
| 820 | ConnectionRefPtr connection = GetNewConnection(); |
| 821 | |
| 822 | connection->lower_binder_.Attach(connection); |
| 823 | |
| 824 | EXPECT_FALSE(connection->binders_.empty()); |
| 825 | |
| 826 | DisconnectCallbackTarget target; |
| 827 | Connection::Binder binder("test", target.callback()); |
| 828 | binder.Attach(connection); |
| 829 | |
| 830 | AddDestructorExpectations(); |
| 831 | EXPECT_CALL(target, CallTarget()).Times(1); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 832 | connection = nullptr; |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 833 | } |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 834 | { |
| 835 | // Circular binding of multiple connections should be safe. |
| 836 | ConnectionRefPtr connection_a = GetNewConnection(); |
| 837 | ConnectionRefPtr connection_b = GetNewConnection(); |
| 838 | |
| 839 | connection_a->lower_binder_.Attach(connection_b); |
| 840 | connection_b->lower_binder_.Attach(connection_a); |
| 841 | |
| 842 | EXPECT_FALSE(connection_a->binders_.empty()); |
| 843 | EXPECT_FALSE(connection_b->binders_.empty()); |
| 844 | |
| 845 | DisconnectCallbackTarget target_a; |
| 846 | DisconnectCallbackTarget target_b; |
| 847 | Connection::Binder binder_a("test_a", target_a.callback()); |
| 848 | Connection::Binder binder_b("test_b", target_b.callback()); |
| 849 | binder_a.Attach(connection_a); |
| 850 | binder_b.Attach(connection_b); |
| 851 | |
| 852 | AddDestructorExpectations(); |
| 853 | EXPECT_CALL(target_a, CallTarget()).Times(1); |
| 854 | EXPECT_CALL(target_b, CallTarget()).Times(1); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 855 | connection_b = nullptr; |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 856 | |
| 857 | EXPECT_TRUE(connection_a->binders_.empty()); |
| 858 | |
| 859 | AddDestructorExpectations(); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 860 | connection_a = nullptr; |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 861 | } |
Darin Petkov | ef1f9fe | 2012-05-11 16:51:52 +0200 | [diff] [blame] | 862 | { |
| 863 | // Test the weak pointer to the bound Connection. This is not a case that |
| 864 | // should occur but the weak pointer should handle it gracefully. |
| 865 | DisconnectCallbackTarget target; |
| 866 | Connection::Binder binder("test_weak", target.callback()); |
| 867 | ConnectionRefPtr connection = GetNewConnection(); |
| 868 | binder.Attach(connection); |
| 869 | |
| 870 | // Make sure the connection doesn't notify the binder on destruction. |
| 871 | connection->binders_.clear(); |
| 872 | AddDestructorExpectations(); |
| 873 | EXPECT_CALL(target, CallTarget()).Times(0); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 874 | connection = nullptr; |
Darin Petkov | ef1f9fe | 2012-05-11 16:51:52 +0200 | [diff] [blame] | 875 | |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 876 | // Ensure no crash -- the weak pointer to connection should be nullptr. |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 877 | EXPECT_FALSE(binder.connection()); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 878 | binder.Attach(nullptr); |
Darin Petkov | ef1f9fe | 2012-05-11 16:51:52 +0200 | [diff] [blame] | 879 | } |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | TEST_F(ConnectionTest, OnRouteQueryResponse) { |
| 883 | Connection::Binder *binder = &connection_->lower_binder_; |
| 884 | ConnectionRefPtr connection = GetNewConnection(); |
| 885 | scoped_refptr<MockDevice> device(new StrictMock<MockDevice>( |
| 886 | &control_, |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 887 | nullptr, |
| 888 | nullptr, |
| 889 | nullptr, |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 890 | kTestDeviceName1, |
| 891 | string(), |
| 892 | kTestDeviceInterfaceIndex1)); |
| 893 | |
| 894 | // Make sure we unbind the old lower connection even if we can't lookup the |
| 895 | // lower connection device. |
| 896 | binder->Attach(connection); |
| 897 | scoped_refptr<MockDevice> null_device; |
| 898 | EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex1)) |
| 899 | .WillOnce(Return(null_device)); |
| 900 | connection_->OnRouteQueryResponse( |
| 901 | kTestDeviceInterfaceIndex1, RoutingTableEntry()); |
| 902 | EXPECT_FALSE(binder->IsBound()); |
| 903 | |
| 904 | // Check for graceful handling of a device with no connection. |
Paul Stewart | 2cb3fa7 | 2014-11-13 01:43:12 -0800 | [diff] [blame] | 905 | ConnectionRefPtr device_connection; |
| 906 | EXPECT_CALL(*device, connection()) |
| 907 | .WillRepeatedly(testing::ReturnRef(device_connection)); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 908 | EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex1)) |
| 909 | .WillOnce(Return(device)); |
| 910 | connection_->OnRouteQueryResponse( |
| 911 | kTestDeviceInterfaceIndex1, RoutingTableEntry()); |
| 912 | EXPECT_FALSE(binder->IsBound()); |
| 913 | |
mukesh agrawal | f407d59 | 2013-07-31 11:37:57 -0700 | [diff] [blame] | 914 | // Create a mock connection that will be used for binding. |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 915 | scoped_refptr<MockConnection> mock_connection( |
| 916 | new StrictMock<MockConnection>(device_info_.get())); |
| 917 | EXPECT_CALL(*device_info_.get(), |
| 918 | FlushAddresses(mock_connection->interface_index())); |
| 919 | const string kInterfaceName(kTestDeviceName0); |
| 920 | EXPECT_CALL(*mock_connection, interface_name()) |
| 921 | .WillRepeatedly(ReturnRef(kInterfaceName)); |
Paul Stewart | 2cb3fa7 | 2014-11-13 01:43:12 -0800 | [diff] [blame] | 922 | device_connection = mock_connection.get(); |
| 923 | EXPECT_CALL(*device, connection()) |
| 924 | .WillRepeatedly(testing::ReturnRef(device_connection)); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 925 | EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex1)) |
| 926 | .WillOnce(Return(device)); |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 927 | |
Ben Chan | a6bfe87 | 2012-09-26 09:48:34 -0700 | [diff] [blame] | 928 | // Check that the binding process completes, causing its upper |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 929 | // connection to create a gateway route. |
| 930 | EXPECT_CALL(*mock_connection, CreateGatewayRoute()) |
| 931 | .WillOnce(Return(true)); |
Paul Stewart | 8596f9f | 2013-03-14 07:58:26 -0700 | [diff] [blame] | 932 | |
| 933 | // Ensure that the Device is notified of the change to the connection. |
| 934 | EXPECT_CALL(*device, OnConnectionUpdated()).Times(1); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 935 | connection_->OnRouteQueryResponse( |
| 936 | kTestDeviceInterfaceIndex1, RoutingTableEntry()); |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 937 | |
| 938 | // Check that the upper connection is bound to the lower connection. |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 939 | EXPECT_TRUE(binder->IsBound()); |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 940 | EXPECT_EQ(mock_connection.get(), binder->connection().get()); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 941 | |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 942 | AddDestructorExpectations(); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 943 | connection = nullptr; |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 944 | } |
| 945 | |
Alex Deymo | fddc09a | 2013-07-03 18:41:31 -0700 | [diff] [blame] | 946 | TEST_F(ConnectionTest, GetCarrierConnection) { |
| 947 | EXPECT_EQ(connection_.get(), connection_->GetCarrierConnection().get()); |
| 948 | |
| 949 | ConnectionRefPtr connection1 = GetNewConnection(); |
| 950 | ConnectionRefPtr connection2 = GetNewConnection(); |
| 951 | ConnectionRefPtr connection3 = GetNewConnection(); |
| 952 | |
| 953 | connection_->lower_binder_.Attach(connection1); |
| 954 | EXPECT_EQ(connection1.get(), connection_->GetCarrierConnection().get()); |
| 955 | |
| 956 | connection1->lower_binder_.Attach(connection2); |
| 957 | EXPECT_EQ(connection2.get(), connection_->GetCarrierConnection().get()); |
| 958 | |
| 959 | connection2->lower_binder_.Attach(connection3); |
| 960 | EXPECT_EQ(connection3.get(), connection_->GetCarrierConnection().get()); |
| 961 | |
| 962 | // Create a cycle back to |connection1|. |
| 963 | connection3->lower_binder_.Attach(connection1); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 964 | EXPECT_EQ(nullptr, connection_->GetCarrierConnection().get()); |
Alex Deymo | fddc09a | 2013-07-03 18:41:31 -0700 | [diff] [blame] | 965 | |
| 966 | AddDestructorExpectations(); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 967 | connection3 = nullptr; |
Alex Deymo | fddc09a | 2013-07-03 18:41:31 -0700 | [diff] [blame] | 968 | |
| 969 | AddDestructorExpectations(); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 970 | connection2 = nullptr; |
Alex Deymo | fddc09a | 2013-07-03 18:41:31 -0700 | [diff] [blame] | 971 | |
| 972 | AddDestructorExpectations(); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 973 | connection1 = nullptr; |
Alex Deymo | fddc09a | 2013-07-03 18:41:31 -0700 | [diff] [blame] | 974 | } |
| 975 | |
Paul Stewart | 2cb3fa7 | 2014-11-13 01:43:12 -0800 | [diff] [blame] | 976 | TEST_F(ConnectionTest, GetSubnetName) { |
Paul Stewart | 182888e | 2014-11-13 01:43:12 -0800 | [diff] [blame] | 977 | EXPECT_EQ("", connection_->GetSubnetName()); |
Paul Stewart | 2cb3fa7 | 2014-11-13 01:43:12 -0800 | [diff] [blame] | 978 | IPAddress local("1.2.3.4"); |
| 979 | local.set_prefix(24); |
| 980 | SetLocal(local); |
| 981 | EXPECT_EQ("1.2.3.0/24", connection_->GetSubnetName()); |
| 982 | } |
| 983 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 984 | } // namespace shill |