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