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