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::_; |
| 29 | using testing::NiceMock; |
| 30 | using testing::Return; |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame^] | 31 | using testing::ReturnRef; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 32 | using testing::StrictMock; |
| 33 | using testing::Test; |
| 34 | |
| 35 | namespace shill { |
| 36 | |
| 37 | namespace { |
| 38 | const char kTestDeviceName0[] = "netdev0"; |
| 39 | const int kTestDeviceInterfaceIndex0 = 123; |
| 40 | const char kTestDeviceName1[] = "netdev1"; |
| 41 | const int kTestDeviceInterfaceIndex1 = 321; |
| 42 | const char kIPAddress0[] = "192.168.1.1"; |
| 43 | const char kGatewayAddress0[] = "192.168.1.254"; |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 44 | const char kGatewayAddress1[] = "192.168.2.254"; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 45 | const char kBroadcastAddress0[] = "192.168.1.255"; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 46 | const char kNameServer0[] = "8.8.8.8"; |
| 47 | const char kNameServer1[] = "8.8.9.9"; |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 48 | const int32 kPrefix0 = 24; |
| 49 | const int32 kPrefix1 = 31; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 50 | const char kSearchDomain0[] = "chromium.org"; |
| 51 | const char kSearchDomain1[] = "google.com"; |
| 52 | } // namespace {} |
| 53 | |
| 54 | class ConnectionTest : public Test { |
| 55 | public: |
| 56 | ConnectionTest() |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 57 | : device_info_(new StrictMock<MockDeviceInfo>( |
| 58 | &control_, |
| 59 | static_cast<EventDispatcher*>(NULL), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 60 | static_cast<Metrics*>(NULL), |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 61 | static_cast<Manager*>(NULL))), |
| 62 | connection_(new Connection( |
| 63 | kTestDeviceInterfaceIndex0, |
| 64 | kTestDeviceName0, |
Paul Stewart | e00600e | 2012-03-16 07:08:00 -0700 | [diff] [blame] | 65 | Technology::kUnknown, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 66 | device_info_.get())), |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 67 | ipconfig_(new IPConfig(&control_, kTestDeviceName0)), |
| 68 | local_address_(IPAddress::kFamilyIPv4), |
| 69 | broadcast_address_(IPAddress::kFamilyIPv4), |
| 70 | gateway_address_(IPAddress::kFamilyIPv4), |
| 71 | default_address_(IPAddress::kFamilyIPv4) {} |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 72 | |
| 73 | virtual void SetUp() { |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 74 | ReplaceSingletons(connection_); |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 75 | properties_.address = kIPAddress0; |
| 76 | properties_.subnet_prefix = kPrefix0; |
| 77 | properties_.gateway = kGatewayAddress0; |
| 78 | properties_.broadcast_address = kBroadcastAddress0; |
| 79 | properties_.dns_servers.push_back(kNameServer0); |
| 80 | properties_.dns_servers.push_back(kNameServer1); |
| 81 | properties_.domain_search.push_back(kSearchDomain0); |
| 82 | properties_.domain_search.push_back(kSearchDomain1); |
| 83 | properties_.address_family = IPAddress::kFamilyIPv4; |
| 84 | UpdateProperties(); |
| 85 | EXPECT_TRUE(local_address_.SetAddressFromString(kIPAddress0)); |
| 86 | EXPECT_TRUE(broadcast_address_.SetAddressFromString(kBroadcastAddress0)); |
| 87 | EXPECT_TRUE(gateway_address_.SetAddressFromString(kGatewayAddress0)); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 90 | virtual void TearDown() { |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame^] | 91 | AddDestructorExpectations(); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 92 | connection_ = NULL; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 95 | void ReplaceSingletons(ConnectionRefPtr connection) { |
| 96 | connection->resolver_ = &resolver_; |
| 97 | connection->routing_table_ = &routing_table_; |
| 98 | connection->rtnl_handler_ = &rtnl_handler_; |
| 99 | } |
| 100 | |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 101 | void UpdateProperties() { |
| 102 | ipconfig_->UpdateProperties(properties_, true); |
| 103 | } |
| 104 | |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 105 | bool PinHostRoute(ConnectionRefPtr connection, |
| 106 | const IPConfig::Properties &properties) { |
| 107 | return connection->PinHostRoute(properties); |
| 108 | } |
| 109 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 110 | protected: |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame^] | 111 | class DisconnectCallbackTarget { |
| 112 | public: |
| 113 | DisconnectCallbackTarget() |
| 114 | : callback_(base::Bind(&DisconnectCallbackTarget::CallTarget, |
| 115 | base::Unretained(this))) {} |
| 116 | |
| 117 | MOCK_METHOD0(CallTarget, void()); |
| 118 | const base::Closure &callback() { return callback_; } |
| 119 | |
| 120 | private: |
| 121 | base::Closure callback_; |
| 122 | }; |
| 123 | |
| 124 | void AddDestructorExpectations() { |
| 125 | EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex0)); |
| 126 | EXPECT_CALL(routing_table_, FlushRoutesWithTag(kTestDeviceInterfaceIndex0)); |
| 127 | EXPECT_CALL(*device_info_.get(), |
| 128 | FlushAddresses(kTestDeviceInterfaceIndex0)); |
| 129 | } |
| 130 | |
| 131 | // Returns a new test connection object. The caller usually needs to call |
| 132 | // AddDestructorExpectations before destroying the object. |
| 133 | ConnectionRefPtr GetNewConnection() { |
| 134 | ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex0, |
| 135 | kTestDeviceName0, |
| 136 | Technology::kUnknown, |
| 137 | device_info_.get())); |
| 138 | ReplaceSingletons(connection); |
| 139 | return connection; |
| 140 | } |
| 141 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 142 | scoped_ptr<StrictMock<MockDeviceInfo> > device_info_; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 143 | ConnectionRefPtr connection_; |
| 144 | MockControl control_; |
| 145 | IPConfigRefPtr ipconfig_; |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 146 | IPConfig::Properties properties_; |
| 147 | IPAddress local_address_; |
| 148 | IPAddress broadcast_address_; |
| 149 | IPAddress gateway_address_; |
| 150 | IPAddress default_address_; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 151 | StrictMock<MockResolver> resolver_; |
| 152 | StrictMock<MockRoutingTable> routing_table_; |
| 153 | StrictMock<MockRTNLHandler> rtnl_handler_; |
| 154 | }; |
| 155 | |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame^] | 156 | namespace { |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 157 | |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 158 | MATCHER_P2(IsIPAddress, address, prefix, "") { |
| 159 | IPAddress match_address(address); |
| 160 | match_address.set_prefix(prefix); |
| 161 | return match_address.Equals(arg); |
| 162 | } |
| 163 | |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame^] | 164 | MATCHER(IsNonNullCallback, "") { |
| 165 | return !arg.is_null(); |
| 166 | } |
| 167 | |
| 168 | } // namespace |
| 169 | |
| 170 | TEST_F(ConnectionTest, InitState) { |
| 171 | EXPECT_EQ(kTestDeviceInterfaceIndex0, connection_->interface_index_); |
| 172 | EXPECT_EQ(kTestDeviceName0, connection_->interface_name_); |
| 173 | EXPECT_FALSE(connection_->is_default()); |
| 174 | EXPECT_FALSE(connection_->routing_request_count_); |
| 175 | } |
| 176 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 177 | TEST_F(ConnectionTest, AddConfig) { |
| 178 | EXPECT_CALL(rtnl_handler_, |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 179 | AddInterfaceAddress(kTestDeviceInterfaceIndex0, |
| 180 | IsIPAddress(local_address_, kPrefix0), |
| 181 | IsIPAddress(broadcast_address_, 0), |
| 182 | IsIPAddress(default_address_, 0))); |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 183 | EXPECT_CALL(routing_table_, |
| 184 | SetDefaultRoute(kTestDeviceInterfaceIndex0, |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 185 | IsIPAddress(gateway_address_, 0), |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 186 | Connection::kNonDefaultMetricBase + |
| 187 | kTestDeviceInterfaceIndex0)); |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 188 | EXPECT_CALL(routing_table_, |
| 189 | ConfigureRoutes(kTestDeviceInterfaceIndex0, |
| 190 | ipconfig_, |
| 191 | Connection::kDefaultMetric)); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 192 | connection_->UpdateFromIPConfig(ipconfig_); |
| 193 | |
| 194 | EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0, |
| 195 | Connection::kDefaultMetric)); |
| 196 | EXPECT_CALL(resolver_, SetDNSFromLists( |
| 197 | ipconfig_->properties().dns_servers, |
| 198 | ipconfig_->properties().domain_search)); |
| 199 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 200 | scoped_refptr<MockDevice> device(new StrictMock<MockDevice>( |
| 201 | &control_, |
| 202 | reinterpret_cast<EventDispatcher *>(NULL), |
| 203 | reinterpret_cast<Metrics *>(NULL), |
| 204 | reinterpret_cast<Manager *>(NULL), |
| 205 | kTestDeviceName0, |
| 206 | string(), |
| 207 | kTestDeviceInterfaceIndex0)); |
| 208 | EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0)) |
| 209 | .WillOnce(Return(device)); |
| 210 | EXPECT_CALL(*device.get(), RequestPortalDetection()) |
| 211 | .WillOnce(Return(true)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 212 | connection_->SetIsDefault(true); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 213 | EXPECT_TRUE(connection_->is_default()); |
| 214 | |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 215 | EXPECT_CALL(routing_table_, |
| 216 | SetDefaultMetric(kTestDeviceInterfaceIndex0, |
| 217 | Connection::kNonDefaultMetricBase + |
| 218 | kTestDeviceInterfaceIndex0)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 219 | connection_->SetIsDefault(false); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 220 | EXPECT_FALSE(connection_->is_default()); |
| 221 | } |
| 222 | |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 223 | TEST_F(ConnectionTest, AddConfigWithPeer) { |
| 224 | const string kPeerAddress("192.168.1.222"); |
| 225 | IPAddress peer_address(IPAddress::kFamilyIPv4); |
| 226 | EXPECT_TRUE(peer_address.SetAddressFromString(kPeerAddress)); |
| 227 | properties_.peer_address = kPeerAddress; |
| 228 | properties_.gateway = string(); |
| 229 | UpdateProperties(); |
| 230 | EXPECT_CALL(rtnl_handler_, |
| 231 | AddInterfaceAddress(kTestDeviceInterfaceIndex0, |
| 232 | IsIPAddress(local_address_, kPrefix0), |
| 233 | IsIPAddress(broadcast_address_, 0), |
| 234 | IsIPAddress(peer_address, 0))); |
| 235 | EXPECT_CALL(routing_table_, SetDefaultRoute(_, _, _)).Times(0); |
| 236 | EXPECT_CALL(routing_table_, |
| 237 | ConfigureRoutes(kTestDeviceInterfaceIndex0, |
| 238 | ipconfig_, |
| 239 | Connection::kDefaultMetric)); |
| 240 | connection_->UpdateFromIPConfig(ipconfig_); |
| 241 | } |
| 242 | |
| 243 | TEST_F(ConnectionTest, AddConfigWithBrokenNetmask) { |
| 244 | // Assign a prefix that makes the gateway unreachable. |
| 245 | properties_.subnet_prefix = kPrefix1; |
| 246 | UpdateProperties(); |
| 247 | |
| 248 | // Connection should override with a prefix which will allow the |
| 249 | // gateway to be reachable. |
| 250 | EXPECT_CALL(rtnl_handler_, |
| 251 | AddInterfaceAddress(kTestDeviceInterfaceIndex0, |
| 252 | IsIPAddress(local_address_, kPrefix0), |
| 253 | IsIPAddress(broadcast_address_, 0), |
| 254 | IsIPAddress(default_address_, 0))); |
| 255 | EXPECT_CALL(routing_table_, |
| 256 | SetDefaultRoute(kTestDeviceInterfaceIndex0, |
| 257 | IsIPAddress(gateway_address_, 0), |
| 258 | Connection::kNonDefaultMetricBase + |
| 259 | kTestDeviceInterfaceIndex0)); |
| 260 | EXPECT_CALL(routing_table_, |
| 261 | ConfigureRoutes(kTestDeviceInterfaceIndex0, |
| 262 | ipconfig_, |
| 263 | Connection::kDefaultMetric)); |
| 264 | connection_->UpdateFromIPConfig(ipconfig_); |
| 265 | |
| 266 | // Assign a gateway address that violates the minimum plausible prefix |
| 267 | // the Connection can assign. |
| 268 | properties_.gateway = kGatewayAddress1; |
| 269 | UpdateProperties(); |
| 270 | |
| 271 | // Connection cannot override this prefix, so it will revert to the |
| 272 | // configured prefix, expecting the default route to fail. |
| 273 | EXPECT_CALL(rtnl_handler_, |
| 274 | AddInterfaceAddress(kTestDeviceInterfaceIndex0, |
| 275 | IsIPAddress(local_address_, kPrefix1), |
| 276 | IsIPAddress(broadcast_address_, 0), |
| 277 | IsIPAddress(default_address_, 0))); |
| 278 | EXPECT_CALL(routing_table_, |
| 279 | SetDefaultRoute(kTestDeviceInterfaceIndex0, _, _)); |
| 280 | EXPECT_CALL(routing_table_, |
| 281 | ConfigureRoutes(kTestDeviceInterfaceIndex0, _, _)); |
| 282 | connection_->UpdateFromIPConfig(ipconfig_); |
| 283 | } |
| 284 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 285 | TEST_F(ConnectionTest, AddConfigReverse) { |
| 286 | EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0, |
| 287 | Connection::kDefaultMetric)); |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 288 | vector<string> empty_list; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 289 | EXPECT_CALL(resolver_, SetDNSFromLists(empty_list, empty_list)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 290 | scoped_refptr<MockDevice> device(new StrictMock<MockDevice>( |
| 291 | &control_, |
| 292 | reinterpret_cast<EventDispatcher *>(NULL), |
| 293 | reinterpret_cast<Metrics *>(NULL), |
| 294 | reinterpret_cast<Manager *>(NULL), |
| 295 | kTestDeviceName0, |
| 296 | string(), |
| 297 | kTestDeviceInterfaceIndex0)); |
| 298 | EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0)) |
| 299 | .WillOnce(Return(device)); |
| 300 | EXPECT_CALL(*device.get(), RequestPortalDetection()) |
| 301 | .WillOnce(Return(true)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 302 | connection_->SetIsDefault(true); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 303 | |
| 304 | EXPECT_CALL(rtnl_handler_, |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 305 | AddInterfaceAddress(kTestDeviceInterfaceIndex0, |
| 306 | IsIPAddress(local_address_, kPrefix0), |
| 307 | IsIPAddress(broadcast_address_, 0), |
| 308 | IsIPAddress(default_address_, 0))); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 309 | EXPECT_CALL(routing_table_, SetDefaultRoute(kTestDeviceInterfaceIndex0, |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 310 | IsIPAddress(gateway_address_, 0), |
| 311 | Connection::kDefaultMetric)); |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 312 | EXPECT_CALL(routing_table_, |
| 313 | ConfigureRoutes(kTestDeviceInterfaceIndex0, |
| 314 | ipconfig_, |
| 315 | Connection::kDefaultMetric)); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 316 | EXPECT_CALL(resolver_, SetDNSFromIPConfig(ipconfig_)); |
| 317 | |
| 318 | connection_->UpdateFromIPConfig(ipconfig_); |
| 319 | } |
| 320 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 321 | TEST_F(ConnectionTest, RouteRequest) { |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame^] | 322 | ConnectionRefPtr connection = GetNewConnection(); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 323 | scoped_refptr<MockDevice> device(new StrictMock<MockDevice>( |
| 324 | &control_, |
| 325 | reinterpret_cast<EventDispatcher *>(NULL), |
| 326 | reinterpret_cast<Metrics *>(NULL), |
| 327 | reinterpret_cast<Manager *>(NULL), |
| 328 | kTestDeviceName0, |
| 329 | string(), |
| 330 | kTestDeviceInterfaceIndex0)); |
| 331 | EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0)) |
| 332 | .WillRepeatedly(Return(device)); |
| 333 | EXPECT_CALL(*device.get(), DisableReversePathFilter()).Times(1); |
| 334 | connection->RequestRouting(); |
| 335 | connection->RequestRouting(); |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 336 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 337 | // The first release should only decrement the reference counter. |
| 338 | connection->ReleaseRouting(); |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 339 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 340 | // Another release will re-enable reverse-path filter. |
| 341 | EXPECT_CALL(*device.get(), EnableReversePathFilter()); |
| 342 | EXPECT_CALL(routing_table_, FlushCache()); |
| 343 | connection->ReleaseRouting(); |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 344 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 345 | // The destructor will remove the routes and addresses. |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame^] | 346 | AddDestructorExpectations(); |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 347 | } |
| 348 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 349 | TEST_F(ConnectionTest, Destructor) { |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame^] | 350 | ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex1, |
| 351 | kTestDeviceName1, |
| 352 | Technology::kUnknown, |
| 353 | device_info_.get())); |
| 354 | connection->resolver_ = &resolver_; |
| 355 | connection->routing_table_ = &routing_table_; |
| 356 | connection->rtnl_handler_ = &rtnl_handler_; |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 357 | EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex1)); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 358 | EXPECT_CALL(routing_table_, FlushRoutesWithTag(kTestDeviceInterfaceIndex1)); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 359 | EXPECT_CALL(*device_info_, FlushAddresses(kTestDeviceInterfaceIndex1)); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame^] | 360 | connection = NULL; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 363 | TEST_F(ConnectionTest, RequestHostRoute) { |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame^] | 364 | ConnectionRefPtr connection = GetNewConnection(); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 365 | IPAddress address(IPAddress::kFamilyIPv4); |
| 366 | ASSERT_TRUE(address.SetAddressFromString(kIPAddress0)); |
| 367 | size_t prefix_len = address.GetLength() * 8; |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame^] | 368 | EXPECT_CALL(routing_table_, |
| 369 | RequestRouteToHost(IsIPAddress(address, prefix_len), |
| 370 | -1, |
| 371 | kTestDeviceInterfaceIndex0, |
| 372 | IsNonNullCallback())) |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 373 | .WillOnce(Return(true)); |
| 374 | EXPECT_TRUE(connection->RequestHostRoute(address)); |
| 375 | |
| 376 | // The destructor will remove the routes and addresses. |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame^] | 377 | AddDestructorExpectations(); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | TEST_F(ConnectionTest, PinHostRoute) { |
| 381 | static const char kGateway[] = "10.242.2.13"; |
| 382 | static const char kNetwork[] = "10.242.2.1"; |
| 383 | |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame^] | 384 | ConnectionRefPtr connection = GetNewConnection(); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 385 | |
| 386 | IPConfig::Properties props; |
| 387 | props.address_family = IPAddress::kFamilyIPv4; |
| 388 | EXPECT_FALSE(PinHostRoute(connection, props)); |
| 389 | |
| 390 | props.gateway = kGateway; |
| 391 | EXPECT_FALSE(PinHostRoute(connection, props)); |
| 392 | |
| 393 | props.gateway.clear(); |
| 394 | props.trusted_ip = "xxx"; |
| 395 | EXPECT_FALSE(PinHostRoute(connection, props)); |
| 396 | |
| 397 | props.gateway = kGateway; |
| 398 | EXPECT_FALSE(PinHostRoute(connection, props)); |
| 399 | |
| 400 | props.trusted_ip = kNetwork; |
| 401 | IPAddress address(IPAddress::kFamilyIPv4); |
| 402 | ASSERT_TRUE(address.SetAddressFromString(kNetwork)); |
| 403 | size_t prefix_len = address.GetLength() * 8; |
| 404 | EXPECT_CALL(routing_table_, RequestRouteToHost( |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 405 | IsIPAddress(address, prefix_len), -1, kTestDeviceInterfaceIndex0, _)) |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 406 | .WillOnce(Return(false)); |
| 407 | EXPECT_FALSE(PinHostRoute(connection, props)); |
| 408 | |
| 409 | EXPECT_CALL(routing_table_, RequestRouteToHost( |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 410 | IsIPAddress(address, prefix_len), -1, kTestDeviceInterfaceIndex0, _)) |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 411 | .WillOnce(Return(true)); |
| 412 | EXPECT_TRUE(PinHostRoute(connection, props)); |
| 413 | |
| 414 | // The destructor will remove the routes and addresses. |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame^] | 415 | AddDestructorExpectations(); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 418 | TEST_F(ConnectionTest, FixGatewayReachability) { |
| 419 | static const char kLocal[] = "10.242.2.13"; |
| 420 | IPAddress local(IPAddress::kFamilyIPv4); |
| 421 | ASSERT_TRUE(local.SetAddressFromString(kLocal)); |
| 422 | const int kPrefix = 24; |
| 423 | local.set_prefix(kPrefix); |
| 424 | IPAddress gateway(IPAddress::kFamilyIPv4); |
| 425 | IPAddress peer(IPAddress::kFamilyIPv4); |
| 426 | |
| 427 | // Should fail because no gateway is set. |
| 428 | EXPECT_FALSE(Connection::FixGatewayReachability(&local, gateway, peer)); |
| 429 | EXPECT_EQ(kPrefix, local.prefix()); |
| 430 | |
| 431 | // Should succeed because with the given prefix, this gateway is reachable. |
| 432 | static const char kReachableGateway[] = "10.242.2.14"; |
| 433 | ASSERT_TRUE(gateway.SetAddressFromString(kReachableGateway)); |
| 434 | EXPECT_TRUE(Connection::FixGatewayReachability(&local, gateway, peer)); |
| 435 | // Prefix should remain unchanged. |
| 436 | EXPECT_EQ(kPrefix, local.prefix()); |
| 437 | |
| 438 | // Should succeed because we modified the prefix to match the gateway. |
| 439 | static const char kExpandableGateway[] = "10.242.3.14"; |
| 440 | ASSERT_TRUE(gateway.SetAddressFromString(kExpandableGateway)); |
| 441 | EXPECT_TRUE(Connection::FixGatewayReachability(&local, gateway, peer)); |
| 442 | // Prefix should have opened up by 1 bit. |
| 443 | EXPECT_EQ(kPrefix - 1, local.prefix()); |
| 444 | |
| 445 | // Should fail because we cannot plausibly expand the prefix past 8. |
| 446 | local.set_prefix(kPrefix); |
| 447 | static const char kUnreachableGateway[] = "11.242.2.14"; |
| 448 | ASSERT_TRUE(gateway.SetAddressFromString(kUnreachableGateway)); |
| 449 | EXPECT_FALSE(Connection::FixGatewayReachability(&local, gateway, peer)); |
| 450 | // Prefix should not have changed. |
| 451 | EXPECT_EQ(kPrefix, local.prefix()); |
| 452 | |
| 453 | // However, if this is a peer-to-peer interface and the peer matches |
| 454 | // the gateway, we should succeed. |
| 455 | ASSERT_TRUE(peer.SetAddressFromString(kUnreachableGateway)); |
| 456 | EXPECT_TRUE(Connection::FixGatewayReachability(&local, gateway, peer)); |
| 457 | EXPECT_EQ(kPrefix, local.prefix()); |
| 458 | |
| 459 | // If there is a peer specified and it does not match the gateway (even |
| 460 | // if it was reachable via netmask), we should fail. |
| 461 | ASSERT_TRUE(gateway.SetAddressFromString(kReachableGateway)); |
| 462 | EXPECT_FALSE(Connection::FixGatewayReachability(&local, gateway, peer)); |
| 463 | EXPECT_EQ(kPrefix, local.prefix()); |
| 464 | } |
| 465 | |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame^] | 466 | TEST_F(ConnectionTest, Binders) { |
| 467 | EXPECT_TRUE(connection_->binders_.empty()); |
| 468 | DisconnectCallbackTarget target0; |
| 469 | DisconnectCallbackTarget target1; |
| 470 | DisconnectCallbackTarget target2; |
| 471 | DisconnectCallbackTarget target3; |
| 472 | Connection::Binder binder0("binder0", target0.callback()); |
| 473 | Connection::Binder binder1("binder1", target1.callback()); |
| 474 | Connection::Binder binder2("binder2", target2.callback()); |
| 475 | Connection::Binder binder3("binder3", target3.callback()); |
| 476 | |
| 477 | binder0.Attach(connection_); |
| 478 | binder1.Attach(connection_); |
| 479 | |
| 480 | EXPECT_CALL(target1, CallTarget()).Times(0); |
| 481 | binder1.Attach(connection_); |
| 482 | |
| 483 | binder3.Attach(connection_); |
| 484 | binder2.Attach(connection_); |
| 485 | |
| 486 | EXPECT_CALL(target3, CallTarget()).Times(0); |
| 487 | binder3.Attach(NULL); |
| 488 | |
| 489 | ASSERT_EQ(3, connection_->binders_.size()); |
| 490 | EXPECT_TRUE(connection_->binders_.at(0) == &binder0); |
| 491 | EXPECT_TRUE(connection_->binders_.at(1) == &binder1); |
| 492 | EXPECT_TRUE(connection_->binders_.at(2) == &binder2); |
| 493 | |
| 494 | EXPECT_CALL(target0, CallTarget()).Times(1); |
| 495 | EXPECT_CALL(target1, CallTarget()).Times(1); |
| 496 | EXPECT_CALL(target2, CallTarget()).Times(1); |
| 497 | connection_->NotifyBindersOnDisconnect(); |
| 498 | EXPECT_TRUE(connection_->binders_.empty()); |
| 499 | |
| 500 | // Should be a no-op. |
| 501 | connection_->NotifyBindersOnDisconnect(); |
| 502 | } |
| 503 | |
| 504 | TEST_F(ConnectionTest, Binder) { |
| 505 | // No connection should be bound initially. |
| 506 | Connection::Binder *binder = &connection_->lower_binder_; |
| 507 | EXPECT_EQ(connection_->interface_name(), binder->name_); |
| 508 | EXPECT_FALSE(binder->client_disconnect_callback_.is_null()); |
| 509 | EXPECT_FALSE(binder->IsBound()); |
| 510 | |
| 511 | ConnectionRefPtr connection1 = GetNewConnection(); |
| 512 | EXPECT_TRUE(connection1->binders_.empty()); |
| 513 | |
| 514 | // Bind lower |connection1| and check if it's bound. |
| 515 | binder->Attach(connection1); |
| 516 | EXPECT_TRUE(binder->IsBound()); |
| 517 | EXPECT_EQ(connection1.get(), binder->connection().get()); |
| 518 | ASSERT_FALSE(connection1->binders_.empty()); |
| 519 | EXPECT_TRUE(binder == connection1->binders_.at(0)); |
| 520 | |
| 521 | // Unbind lower |connection1| and check if it's unbound. |
| 522 | binder->Attach(NULL); |
| 523 | EXPECT_FALSE(binder->IsBound()); |
| 524 | EXPECT_TRUE(connection1->binders_.empty()); |
| 525 | |
| 526 | ConnectionRefPtr connection2 = GetNewConnection(); |
| 527 | |
| 528 | // Bind lower |connection1| to upper |connection2| and destroy the upper |
| 529 | // |connection2|. Make sure lower |connection1| is unbound (i.e., the |
| 530 | // disconnect callback is deregistered). |
| 531 | connection2->lower_binder_.Attach(connection1); |
| 532 | EXPECT_FALSE(connection1->binders_.empty()); |
| 533 | AddDestructorExpectations(); |
| 534 | connection2 = NULL; |
| 535 | EXPECT_TRUE(connection1->binders_.empty()); |
| 536 | |
| 537 | // Bind lower |connection1| to upper |connection_| and destroy lower |
| 538 | // |connection1|. Make sure lower |connection1| is unbound from upper |
| 539 | // |connection_| and upper |connection_|'s registered disconnect callbacks are |
| 540 | // run. |
| 541 | binder->Attach(connection1); |
| 542 | DisconnectCallbackTarget target; |
| 543 | Connection::Binder test_binder("from_test", target.callback()); |
| 544 | test_binder.Attach(connection_); |
| 545 | EXPECT_CALL(target, CallTarget()).Times(1); |
| 546 | ASSERT_FALSE(connection_->binders_.empty()); |
| 547 | AddDestructorExpectations(); |
| 548 | connection1 = NULL; |
| 549 | EXPECT_FALSE(binder->IsBound()); |
| 550 | EXPECT_FALSE(test_binder.IsBound()); |
| 551 | EXPECT_TRUE(connection_->binders_.empty()); |
| 552 | |
| 553 | { |
| 554 | // Binding a connection to itself should be safe. |
| 555 | ConnectionRefPtr connection = GetNewConnection(); |
| 556 | |
| 557 | connection->lower_binder_.Attach(connection); |
| 558 | |
| 559 | EXPECT_FALSE(connection->binders_.empty()); |
| 560 | |
| 561 | DisconnectCallbackTarget target; |
| 562 | Connection::Binder binder("test", target.callback()); |
| 563 | binder.Attach(connection); |
| 564 | |
| 565 | AddDestructorExpectations(); |
| 566 | EXPECT_CALL(target, CallTarget()).Times(1); |
| 567 | connection = NULL; |
| 568 | } |
| 569 | |
| 570 | { |
| 571 | // Circular binding of multiple connections should be safe. |
| 572 | ConnectionRefPtr connection_a = GetNewConnection(); |
| 573 | ConnectionRefPtr connection_b = GetNewConnection(); |
| 574 | |
| 575 | connection_a->lower_binder_.Attach(connection_b); |
| 576 | connection_b->lower_binder_.Attach(connection_a); |
| 577 | |
| 578 | EXPECT_FALSE(connection_a->binders_.empty()); |
| 579 | EXPECT_FALSE(connection_b->binders_.empty()); |
| 580 | |
| 581 | DisconnectCallbackTarget target_a; |
| 582 | DisconnectCallbackTarget target_b; |
| 583 | Connection::Binder binder_a("test_a", target_a.callback()); |
| 584 | Connection::Binder binder_b("test_b", target_b.callback()); |
| 585 | binder_a.Attach(connection_a); |
| 586 | binder_b.Attach(connection_b); |
| 587 | |
| 588 | AddDestructorExpectations(); |
| 589 | EXPECT_CALL(target_a, CallTarget()).Times(1); |
| 590 | EXPECT_CALL(target_b, CallTarget()).Times(1); |
| 591 | connection_b = NULL; |
| 592 | |
| 593 | EXPECT_TRUE(connection_a->binders_.empty()); |
| 594 | |
| 595 | AddDestructorExpectations(); |
| 596 | connection_a = NULL; |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | TEST_F(ConnectionTest, OnRouteQueryResponse) { |
| 601 | Connection::Binder *binder = &connection_->lower_binder_; |
| 602 | ConnectionRefPtr connection = GetNewConnection(); |
| 603 | scoped_refptr<MockDevice> device(new StrictMock<MockDevice>( |
| 604 | &control_, |
| 605 | reinterpret_cast<EventDispatcher *>(NULL), |
| 606 | reinterpret_cast<Metrics *>(NULL), |
| 607 | reinterpret_cast<Manager *>(NULL), |
| 608 | kTestDeviceName1, |
| 609 | string(), |
| 610 | kTestDeviceInterfaceIndex1)); |
| 611 | |
| 612 | // Make sure we unbind the old lower connection even if we can't lookup the |
| 613 | // lower connection device. |
| 614 | binder->Attach(connection); |
| 615 | scoped_refptr<MockDevice> null_device; |
| 616 | EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex1)) |
| 617 | .WillOnce(Return(null_device)); |
| 618 | connection_->OnRouteQueryResponse( |
| 619 | kTestDeviceInterfaceIndex1, RoutingTableEntry()); |
| 620 | EXPECT_FALSE(binder->IsBound()); |
| 621 | |
| 622 | // Check for graceful handling of a device with no connection. |
| 623 | EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex1)) |
| 624 | .WillOnce(Return(device)); |
| 625 | connection_->OnRouteQueryResponse( |
| 626 | kTestDeviceInterfaceIndex1, RoutingTableEntry()); |
| 627 | EXPECT_FALSE(binder->IsBound()); |
| 628 | |
| 629 | // Check that the upper connection is bound to the lower connection. |
| 630 | device->connection_ = connection; |
| 631 | EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex1)) |
| 632 | .WillOnce(Return(device)); |
| 633 | connection_->OnRouteQueryResponse( |
| 634 | kTestDeviceInterfaceIndex1, RoutingTableEntry()); |
| 635 | EXPECT_TRUE(binder->IsBound()); |
| 636 | EXPECT_EQ(connection.get(), binder->connection().get()); |
| 637 | |
| 638 | device->connection_ = NULL; |
| 639 | AddDestructorExpectations(); |
| 640 | connection = NULL; |
| 641 | } |
| 642 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 643 | } // namespace shill |