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