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