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; |
| 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 | e78ec54 | 2012-06-08 18:28:50 -0700 | [diff] [blame] | 212 | EXPECT_CALL(routing_table_, FlushCache()) |
| 213 | .WillOnce(Return(true)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 214 | connection_->SetIsDefault(true); |
Paul Stewart | e78ec54 | 2012-06-08 18:28:50 -0700 | [diff] [blame] | 215 | Mock::VerifyAndClearExpectations(&routing_table_); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 216 | EXPECT_TRUE(connection_->is_default()); |
| 217 | |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 218 | EXPECT_CALL(routing_table_, |
| 219 | SetDefaultMetric(kTestDeviceInterfaceIndex0, |
| 220 | Connection::kNonDefaultMetricBase + |
| 221 | kTestDeviceInterfaceIndex0)); |
Paul Stewart | e78ec54 | 2012-06-08 18:28:50 -0700 | [diff] [blame] | 222 | EXPECT_CALL(routing_table_, FlushCache()) |
| 223 | .WillOnce(Return(true)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 224 | connection_->SetIsDefault(false); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 225 | EXPECT_FALSE(connection_->is_default()); |
| 226 | } |
| 227 | |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 228 | TEST_F(ConnectionTest, AddConfigWithPeer) { |
| 229 | const string kPeerAddress("192.168.1.222"); |
| 230 | IPAddress peer_address(IPAddress::kFamilyIPv4); |
| 231 | EXPECT_TRUE(peer_address.SetAddressFromString(kPeerAddress)); |
| 232 | properties_.peer_address = kPeerAddress; |
| 233 | properties_.gateway = string(); |
| 234 | UpdateProperties(); |
| 235 | EXPECT_CALL(rtnl_handler_, |
| 236 | AddInterfaceAddress(kTestDeviceInterfaceIndex0, |
| 237 | IsIPAddress(local_address_, kPrefix0), |
| 238 | IsIPAddress(broadcast_address_, 0), |
| 239 | IsIPAddress(peer_address, 0))); |
| 240 | EXPECT_CALL(routing_table_, SetDefaultRoute(_, _, _)).Times(0); |
| 241 | EXPECT_CALL(routing_table_, |
| 242 | ConfigureRoutes(kTestDeviceInterfaceIndex0, |
| 243 | ipconfig_, |
| 244 | Connection::kDefaultMetric)); |
| 245 | connection_->UpdateFromIPConfig(ipconfig_); |
| 246 | } |
| 247 | |
| 248 | TEST_F(ConnectionTest, AddConfigWithBrokenNetmask) { |
| 249 | // Assign a prefix that makes the gateway unreachable. |
| 250 | properties_.subnet_prefix = kPrefix1; |
| 251 | UpdateProperties(); |
| 252 | |
| 253 | // Connection should override with a prefix which will allow the |
| 254 | // gateway to be reachable. |
| 255 | EXPECT_CALL(rtnl_handler_, |
| 256 | AddInterfaceAddress(kTestDeviceInterfaceIndex0, |
| 257 | IsIPAddress(local_address_, kPrefix0), |
| 258 | IsIPAddress(broadcast_address_, 0), |
| 259 | IsIPAddress(default_address_, 0))); |
| 260 | EXPECT_CALL(routing_table_, |
| 261 | SetDefaultRoute(kTestDeviceInterfaceIndex0, |
| 262 | IsIPAddress(gateway_address_, 0), |
| 263 | Connection::kNonDefaultMetricBase + |
| 264 | kTestDeviceInterfaceIndex0)); |
| 265 | EXPECT_CALL(routing_table_, |
| 266 | ConfigureRoutes(kTestDeviceInterfaceIndex0, |
| 267 | ipconfig_, |
| 268 | Connection::kDefaultMetric)); |
| 269 | connection_->UpdateFromIPConfig(ipconfig_); |
| 270 | |
| 271 | // Assign a gateway address that violates the minimum plausible prefix |
| 272 | // the Connection can assign. |
| 273 | properties_.gateway = kGatewayAddress1; |
| 274 | UpdateProperties(); |
| 275 | |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 276 | IPAddress gateway_address1(IPAddress::kFamilyIPv4); |
| 277 | EXPECT_TRUE(gateway_address1.SetAddressFromString(kGatewayAddress1)); |
| 278 | // Connection cannot override this prefix, so it will switch to a |
| 279 | // model where the peer address is set to the value of the gateway |
| 280 | // address. |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 281 | EXPECT_CALL(rtnl_handler_, |
| 282 | AddInterfaceAddress(kTestDeviceInterfaceIndex0, |
| 283 | IsIPAddress(local_address_, kPrefix1), |
| 284 | IsIPAddress(broadcast_address_, 0), |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 285 | IsIPAddress(gateway_address1, 0))); |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 286 | EXPECT_CALL(routing_table_, |
| 287 | SetDefaultRoute(kTestDeviceInterfaceIndex0, _, _)); |
| 288 | EXPECT_CALL(routing_table_, |
| 289 | ConfigureRoutes(kTestDeviceInterfaceIndex0, _, _)); |
| 290 | connection_->UpdateFromIPConfig(ipconfig_); |
| 291 | } |
| 292 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 293 | TEST_F(ConnectionTest, AddConfigReverse) { |
| 294 | EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0, |
| 295 | Connection::kDefaultMetric)); |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 296 | vector<string> empty_list; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 297 | EXPECT_CALL(resolver_, SetDNSFromLists(empty_list, empty_list)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 298 | scoped_refptr<MockDevice> device(new StrictMock<MockDevice>( |
| 299 | &control_, |
| 300 | reinterpret_cast<EventDispatcher *>(NULL), |
| 301 | reinterpret_cast<Metrics *>(NULL), |
| 302 | reinterpret_cast<Manager *>(NULL), |
| 303 | kTestDeviceName0, |
| 304 | string(), |
| 305 | kTestDeviceInterfaceIndex0)); |
| 306 | EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0)) |
| 307 | .WillOnce(Return(device)); |
| 308 | EXPECT_CALL(*device.get(), RequestPortalDetection()) |
| 309 | .WillOnce(Return(true)); |
Paul Stewart | e78ec54 | 2012-06-08 18:28:50 -0700 | [diff] [blame] | 310 | EXPECT_CALL(routing_table_, FlushCache()) |
| 311 | .WillOnce(Return(true)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 312 | connection_->SetIsDefault(true); |
Paul Stewart | e78ec54 | 2012-06-08 18:28:50 -0700 | [diff] [blame] | 313 | Mock::VerifyAndClearExpectations(&routing_table_); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 314 | |
| 315 | EXPECT_CALL(rtnl_handler_, |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 316 | AddInterfaceAddress(kTestDeviceInterfaceIndex0, |
| 317 | IsIPAddress(local_address_, kPrefix0), |
| 318 | IsIPAddress(broadcast_address_, 0), |
| 319 | IsIPAddress(default_address_, 0))); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 320 | EXPECT_CALL(routing_table_, SetDefaultRoute(kTestDeviceInterfaceIndex0, |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 321 | IsIPAddress(gateway_address_, 0), |
| 322 | Connection::kDefaultMetric)); |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 323 | EXPECT_CALL(routing_table_, |
| 324 | ConfigureRoutes(kTestDeviceInterfaceIndex0, |
| 325 | ipconfig_, |
| 326 | Connection::kDefaultMetric)); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 327 | EXPECT_CALL(resolver_, SetDNSFromIPConfig(ipconfig_)); |
| 328 | |
| 329 | connection_->UpdateFromIPConfig(ipconfig_); |
| 330 | } |
| 331 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 332 | TEST_F(ConnectionTest, RouteRequest) { |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 333 | ConnectionRefPtr connection = GetNewConnection(); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 334 | scoped_refptr<MockDevice> device(new StrictMock<MockDevice>( |
| 335 | &control_, |
| 336 | reinterpret_cast<EventDispatcher *>(NULL), |
| 337 | reinterpret_cast<Metrics *>(NULL), |
| 338 | reinterpret_cast<Manager *>(NULL), |
| 339 | kTestDeviceName0, |
| 340 | string(), |
| 341 | kTestDeviceInterfaceIndex0)); |
| 342 | EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0)) |
| 343 | .WillRepeatedly(Return(device)); |
| 344 | EXPECT_CALL(*device.get(), DisableReversePathFilter()).Times(1); |
| 345 | connection->RequestRouting(); |
| 346 | connection->RequestRouting(); |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 347 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 348 | // The first release should only decrement the reference counter. |
| 349 | connection->ReleaseRouting(); |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 350 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 351 | // Another release will re-enable reverse-path filter. |
| 352 | EXPECT_CALL(*device.get(), EnableReversePathFilter()); |
| 353 | EXPECT_CALL(routing_table_, FlushCache()); |
| 354 | connection->ReleaseRouting(); |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 355 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 356 | // The destructor will remove the routes and addresses. |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 357 | AddDestructorExpectations(); |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 360 | TEST_F(ConnectionTest, Destructor) { |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 361 | ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex1, |
| 362 | kTestDeviceName1, |
| 363 | Technology::kUnknown, |
| 364 | device_info_.get())); |
| 365 | connection->resolver_ = &resolver_; |
| 366 | connection->routing_table_ = &routing_table_; |
| 367 | connection->rtnl_handler_ = &rtnl_handler_; |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 368 | EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex1)); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 369 | EXPECT_CALL(routing_table_, FlushRoutesWithTag(kTestDeviceInterfaceIndex1)); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 370 | EXPECT_CALL(*device_info_, FlushAddresses(kTestDeviceInterfaceIndex1)); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 371 | connection = NULL; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 374 | TEST_F(ConnectionTest, RequestHostRoute) { |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 375 | ConnectionRefPtr connection = GetNewConnection(); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 376 | IPAddress address(IPAddress::kFamilyIPv4); |
| 377 | ASSERT_TRUE(address.SetAddressFromString(kIPAddress0)); |
| 378 | size_t prefix_len = address.GetLength() * 8; |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 379 | EXPECT_CALL(routing_table_, |
| 380 | RequestRouteToHost(IsIPAddress(address, prefix_len), |
| 381 | -1, |
| 382 | kTestDeviceInterfaceIndex0, |
| 383 | IsNonNullCallback())) |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 384 | .WillOnce(Return(true)); |
| 385 | EXPECT_TRUE(connection->RequestHostRoute(address)); |
| 386 | |
| 387 | // The destructor will remove the routes and addresses. |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 388 | AddDestructorExpectations(); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | TEST_F(ConnectionTest, PinHostRoute) { |
| 392 | static const char kGateway[] = "10.242.2.13"; |
| 393 | static const char kNetwork[] = "10.242.2.1"; |
| 394 | |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 395 | ConnectionRefPtr connection = GetNewConnection(); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 396 | |
| 397 | IPConfig::Properties props; |
| 398 | props.address_family = IPAddress::kFamilyIPv4; |
| 399 | EXPECT_FALSE(PinHostRoute(connection, props)); |
| 400 | |
| 401 | props.gateway = kGateway; |
| 402 | EXPECT_FALSE(PinHostRoute(connection, props)); |
| 403 | |
| 404 | props.gateway.clear(); |
| 405 | props.trusted_ip = "xxx"; |
| 406 | EXPECT_FALSE(PinHostRoute(connection, props)); |
| 407 | |
| 408 | props.gateway = kGateway; |
| 409 | EXPECT_FALSE(PinHostRoute(connection, props)); |
| 410 | |
| 411 | props.trusted_ip = kNetwork; |
| 412 | IPAddress address(IPAddress::kFamilyIPv4); |
| 413 | ASSERT_TRUE(address.SetAddressFromString(kNetwork)); |
| 414 | size_t prefix_len = address.GetLength() * 8; |
| 415 | EXPECT_CALL(routing_table_, RequestRouteToHost( |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 416 | IsIPAddress(address, prefix_len), -1, kTestDeviceInterfaceIndex0, _)) |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 417 | .WillOnce(Return(false)); |
| 418 | EXPECT_FALSE(PinHostRoute(connection, props)); |
| 419 | |
| 420 | EXPECT_CALL(routing_table_, RequestRouteToHost( |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 421 | IsIPAddress(address, prefix_len), -1, kTestDeviceInterfaceIndex0, _)) |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 422 | .WillOnce(Return(true)); |
| 423 | EXPECT_TRUE(PinHostRoute(connection, props)); |
| 424 | |
| 425 | // The destructor will remove the routes and addresses. |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 426 | AddDestructorExpectations(); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 427 | } |
| 428 | |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 429 | TEST_F(ConnectionTest, FixGatewayReachability) { |
| 430 | static const char kLocal[] = "10.242.2.13"; |
| 431 | IPAddress local(IPAddress::kFamilyIPv4); |
| 432 | ASSERT_TRUE(local.SetAddressFromString(kLocal)); |
| 433 | const int kPrefix = 24; |
| 434 | local.set_prefix(kPrefix); |
| 435 | IPAddress gateway(IPAddress::kFamilyIPv4); |
| 436 | IPAddress peer(IPAddress::kFamilyIPv4); |
| 437 | |
| 438 | // Should fail because no gateway is set. |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 439 | EXPECT_FALSE(Connection::FixGatewayReachability(&local, &peer, gateway)); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 440 | EXPECT_EQ(kPrefix, local.prefix()); |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 441 | EXPECT_FALSE(peer.IsValid()); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 442 | |
| 443 | // Should succeed because with the given prefix, this gateway is reachable. |
| 444 | static const char kReachableGateway[] = "10.242.2.14"; |
| 445 | ASSERT_TRUE(gateway.SetAddressFromString(kReachableGateway)); |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 446 | peer = IPAddress(IPAddress::kFamilyIPv4); |
| 447 | EXPECT_TRUE(Connection::FixGatewayReachability(&local, &peer, gateway)); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 448 | // Prefix should remain unchanged. |
| 449 | EXPECT_EQ(kPrefix, local.prefix()); |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 450 | // Peer should remain unchanged. |
| 451 | EXPECT_FALSE(peer.IsValid()); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 452 | |
| 453 | // Should succeed because we modified the prefix to match the gateway. |
| 454 | static const char kExpandableGateway[] = "10.242.3.14"; |
| 455 | ASSERT_TRUE(gateway.SetAddressFromString(kExpandableGateway)); |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 456 | peer = IPAddress(IPAddress::kFamilyIPv4); |
| 457 | EXPECT_TRUE(Connection::FixGatewayReachability(&local, &peer, gateway)); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 458 | // Prefix should have opened up by 1 bit. |
| 459 | EXPECT_EQ(kPrefix - 1, local.prefix()); |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 460 | // Peer should remain unchanged. |
| 461 | EXPECT_FALSE(peer.IsValid()); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 462 | |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 463 | // Should change models to assuming peer-to-pear because we cannot |
| 464 | // plausibly expand the prefix past 8. |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 465 | local.set_prefix(kPrefix); |
| 466 | static const char kUnreachableGateway[] = "11.242.2.14"; |
| 467 | ASSERT_TRUE(gateway.SetAddressFromString(kUnreachableGateway)); |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 468 | peer = IPAddress(IPAddress::kFamilyIPv4); |
| 469 | EXPECT_TRUE(Connection::FixGatewayReachability(&local, &peer, gateway)); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 470 | // Prefix should not have changed. |
| 471 | EXPECT_EQ(kPrefix, local.prefix()); |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 472 | // Peer address should be set to the gateway address. |
| 473 | EXPECT_TRUE(peer.Equals(gateway)); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 474 | |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 475 | // If this is a peer-to-peer interface and the peer matches the gateway, |
| 476 | // we should succeed. |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 477 | ASSERT_TRUE(peer.SetAddressFromString(kUnreachableGateway)); |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 478 | EXPECT_TRUE(Connection::FixGatewayReachability(&local, &peer, gateway)); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 479 | EXPECT_EQ(kPrefix, local.prefix()); |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 480 | EXPECT_TRUE(peer.Equals(gateway)); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 481 | |
| 482 | // If there is a peer specified and it does not match the gateway (even |
| 483 | // if it was reachable via netmask), we should fail. |
| 484 | ASSERT_TRUE(gateway.SetAddressFromString(kReachableGateway)); |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 485 | EXPECT_FALSE(Connection::FixGatewayReachability(&local, &peer, gateway)); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 486 | EXPECT_EQ(kPrefix, local.prefix()); |
Paul Stewart | 4925829 | 2012-05-26 06:37:14 -0700 | [diff] [blame] | 487 | EXPECT_FALSE(peer.Equals(gateway)); |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 490 | TEST_F(ConnectionTest, Binders) { |
| 491 | EXPECT_TRUE(connection_->binders_.empty()); |
| 492 | DisconnectCallbackTarget target0; |
| 493 | DisconnectCallbackTarget target1; |
| 494 | DisconnectCallbackTarget target2; |
| 495 | DisconnectCallbackTarget target3; |
| 496 | Connection::Binder binder0("binder0", target0.callback()); |
| 497 | Connection::Binder binder1("binder1", target1.callback()); |
| 498 | Connection::Binder binder2("binder2", target2.callback()); |
| 499 | Connection::Binder binder3("binder3", target3.callback()); |
| 500 | |
| 501 | binder0.Attach(connection_); |
| 502 | binder1.Attach(connection_); |
| 503 | |
| 504 | EXPECT_CALL(target1, CallTarget()).Times(0); |
| 505 | binder1.Attach(connection_); |
| 506 | |
| 507 | binder3.Attach(connection_); |
| 508 | binder2.Attach(connection_); |
| 509 | |
| 510 | EXPECT_CALL(target3, CallTarget()).Times(0); |
| 511 | binder3.Attach(NULL); |
| 512 | |
| 513 | ASSERT_EQ(3, connection_->binders_.size()); |
| 514 | EXPECT_TRUE(connection_->binders_.at(0) == &binder0); |
| 515 | EXPECT_TRUE(connection_->binders_.at(1) == &binder1); |
| 516 | EXPECT_TRUE(connection_->binders_.at(2) == &binder2); |
| 517 | |
| 518 | EXPECT_CALL(target0, CallTarget()).Times(1); |
| 519 | EXPECT_CALL(target1, CallTarget()).Times(1); |
| 520 | EXPECT_CALL(target2, CallTarget()).Times(1); |
| 521 | connection_->NotifyBindersOnDisconnect(); |
| 522 | EXPECT_TRUE(connection_->binders_.empty()); |
| 523 | |
| 524 | // Should be a no-op. |
| 525 | connection_->NotifyBindersOnDisconnect(); |
| 526 | } |
| 527 | |
| 528 | TEST_F(ConnectionTest, Binder) { |
| 529 | // No connection should be bound initially. |
| 530 | Connection::Binder *binder = &connection_->lower_binder_; |
| 531 | EXPECT_EQ(connection_->interface_name(), binder->name_); |
| 532 | EXPECT_FALSE(binder->client_disconnect_callback_.is_null()); |
| 533 | EXPECT_FALSE(binder->IsBound()); |
| 534 | |
| 535 | ConnectionRefPtr connection1 = GetNewConnection(); |
| 536 | EXPECT_TRUE(connection1->binders_.empty()); |
| 537 | |
| 538 | // Bind lower |connection1| and check if it's bound. |
| 539 | binder->Attach(connection1); |
| 540 | EXPECT_TRUE(binder->IsBound()); |
| 541 | EXPECT_EQ(connection1.get(), binder->connection().get()); |
| 542 | ASSERT_FALSE(connection1->binders_.empty()); |
| 543 | EXPECT_TRUE(binder == connection1->binders_.at(0)); |
| 544 | |
| 545 | // Unbind lower |connection1| and check if it's unbound. |
| 546 | binder->Attach(NULL); |
| 547 | EXPECT_FALSE(binder->IsBound()); |
| 548 | EXPECT_TRUE(connection1->binders_.empty()); |
| 549 | |
| 550 | ConnectionRefPtr connection2 = GetNewConnection(); |
| 551 | |
| 552 | // Bind lower |connection1| to upper |connection2| and destroy the upper |
| 553 | // |connection2|. Make sure lower |connection1| is unbound (i.e., the |
| 554 | // disconnect callback is deregistered). |
| 555 | connection2->lower_binder_.Attach(connection1); |
| 556 | EXPECT_FALSE(connection1->binders_.empty()); |
| 557 | AddDestructorExpectations(); |
| 558 | connection2 = NULL; |
| 559 | EXPECT_TRUE(connection1->binders_.empty()); |
| 560 | |
| 561 | // Bind lower |connection1| to upper |connection_| and destroy lower |
| 562 | // |connection1|. Make sure lower |connection1| is unbound from upper |
| 563 | // |connection_| and upper |connection_|'s registered disconnect callbacks are |
| 564 | // run. |
| 565 | binder->Attach(connection1); |
| 566 | DisconnectCallbackTarget target; |
| 567 | Connection::Binder test_binder("from_test", target.callback()); |
| 568 | test_binder.Attach(connection_); |
| 569 | EXPECT_CALL(target, CallTarget()).Times(1); |
| 570 | ASSERT_FALSE(connection_->binders_.empty()); |
| 571 | AddDestructorExpectations(); |
| 572 | connection1 = NULL; |
| 573 | EXPECT_FALSE(binder->IsBound()); |
| 574 | EXPECT_FALSE(test_binder.IsBound()); |
| 575 | EXPECT_TRUE(connection_->binders_.empty()); |
| 576 | |
| 577 | { |
| 578 | // Binding a connection to itself should be safe. |
| 579 | ConnectionRefPtr connection = GetNewConnection(); |
| 580 | |
| 581 | connection->lower_binder_.Attach(connection); |
| 582 | |
| 583 | EXPECT_FALSE(connection->binders_.empty()); |
| 584 | |
| 585 | DisconnectCallbackTarget target; |
| 586 | Connection::Binder binder("test", target.callback()); |
| 587 | binder.Attach(connection); |
| 588 | |
| 589 | AddDestructorExpectations(); |
| 590 | EXPECT_CALL(target, CallTarget()).Times(1); |
| 591 | connection = NULL; |
| 592 | } |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 593 | { |
| 594 | // Circular binding of multiple connections should be safe. |
| 595 | ConnectionRefPtr connection_a = GetNewConnection(); |
| 596 | ConnectionRefPtr connection_b = GetNewConnection(); |
| 597 | |
| 598 | connection_a->lower_binder_.Attach(connection_b); |
| 599 | connection_b->lower_binder_.Attach(connection_a); |
| 600 | |
| 601 | EXPECT_FALSE(connection_a->binders_.empty()); |
| 602 | EXPECT_FALSE(connection_b->binders_.empty()); |
| 603 | |
| 604 | DisconnectCallbackTarget target_a; |
| 605 | DisconnectCallbackTarget target_b; |
| 606 | Connection::Binder binder_a("test_a", target_a.callback()); |
| 607 | Connection::Binder binder_b("test_b", target_b.callback()); |
| 608 | binder_a.Attach(connection_a); |
| 609 | binder_b.Attach(connection_b); |
| 610 | |
| 611 | AddDestructorExpectations(); |
| 612 | EXPECT_CALL(target_a, CallTarget()).Times(1); |
| 613 | EXPECT_CALL(target_b, CallTarget()).Times(1); |
| 614 | connection_b = NULL; |
| 615 | |
| 616 | EXPECT_TRUE(connection_a->binders_.empty()); |
| 617 | |
| 618 | AddDestructorExpectations(); |
| 619 | connection_a = NULL; |
| 620 | } |
Darin Petkov | ef1f9fe | 2012-05-11 16:51:52 +0200 | [diff] [blame] | 621 | { |
| 622 | // Test the weak pointer to the bound Connection. This is not a case that |
| 623 | // should occur but the weak pointer should handle it gracefully. |
| 624 | DisconnectCallbackTarget target; |
| 625 | Connection::Binder binder("test_weak", target.callback()); |
| 626 | ConnectionRefPtr connection = GetNewConnection(); |
| 627 | binder.Attach(connection); |
| 628 | |
| 629 | // Make sure the connection doesn't notify the binder on destruction. |
| 630 | connection->binders_.clear(); |
| 631 | AddDestructorExpectations(); |
| 632 | EXPECT_CALL(target, CallTarget()).Times(0); |
| 633 | connection = NULL; |
| 634 | |
| 635 | // Ensure no crash -- the weak pointer to connection should be NULL. |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 636 | EXPECT_FALSE(binder.connection()); |
Darin Petkov | ef1f9fe | 2012-05-11 16:51:52 +0200 | [diff] [blame] | 637 | binder.Attach(NULL); |
| 638 | } |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | TEST_F(ConnectionTest, OnRouteQueryResponse) { |
| 642 | Connection::Binder *binder = &connection_->lower_binder_; |
| 643 | ConnectionRefPtr connection = GetNewConnection(); |
| 644 | scoped_refptr<MockDevice> device(new StrictMock<MockDevice>( |
| 645 | &control_, |
| 646 | reinterpret_cast<EventDispatcher *>(NULL), |
| 647 | reinterpret_cast<Metrics *>(NULL), |
| 648 | reinterpret_cast<Manager *>(NULL), |
| 649 | kTestDeviceName1, |
| 650 | string(), |
| 651 | kTestDeviceInterfaceIndex1)); |
| 652 | |
| 653 | // Make sure we unbind the old lower connection even if we can't lookup the |
| 654 | // lower connection device. |
| 655 | binder->Attach(connection); |
| 656 | scoped_refptr<MockDevice> null_device; |
| 657 | EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex1)) |
| 658 | .WillOnce(Return(null_device)); |
| 659 | connection_->OnRouteQueryResponse( |
| 660 | kTestDeviceInterfaceIndex1, RoutingTableEntry()); |
| 661 | EXPECT_FALSE(binder->IsBound()); |
| 662 | |
| 663 | // Check for graceful handling of a device with no connection. |
| 664 | EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex1)) |
| 665 | .WillOnce(Return(device)); |
| 666 | connection_->OnRouteQueryResponse( |
| 667 | kTestDeviceInterfaceIndex1, RoutingTableEntry()); |
| 668 | EXPECT_FALSE(binder->IsBound()); |
| 669 | |
| 670 | // Check that the upper connection is bound to the lower connection. |
| 671 | device->connection_ = connection; |
| 672 | EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex1)) |
| 673 | .WillOnce(Return(device)); |
| 674 | connection_->OnRouteQueryResponse( |
| 675 | kTestDeviceInterfaceIndex1, RoutingTableEntry()); |
| 676 | EXPECT_TRUE(binder->IsBound()); |
| 677 | EXPECT_EQ(connection.get(), binder->connection().get()); |
| 678 | |
| 679 | device->connection_ = NULL; |
| 680 | AddDestructorExpectations(); |
| 681 | connection = NULL; |
| 682 | } |
| 683 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 684 | } // namespace shill |