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" |
| 17 | #include "shill/mock_control.h" |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 18 | #include "shill/mock_device.h" |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 19 | #include "shill/mock_device_info.h" |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 20 | #include "shill/mock_resolver.h" |
| 21 | #include "shill/mock_routing_table.h" |
| 22 | #include "shill/mock_rtnl_handler.h" |
| 23 | #include "shill/routing_table_entry.h" |
| 24 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 25 | using std::string; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 26 | using std::vector; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 27 | using testing::_; |
| 28 | using testing::NiceMock; |
| 29 | using testing::Return; |
| 30 | using testing::StrictMock; |
| 31 | using testing::Test; |
| 32 | |
| 33 | namespace shill { |
| 34 | |
| 35 | namespace { |
| 36 | const char kTestDeviceName0[] = "netdev0"; |
| 37 | const int kTestDeviceInterfaceIndex0 = 123; |
| 38 | const char kTestDeviceName1[] = "netdev1"; |
| 39 | const int kTestDeviceInterfaceIndex1 = 321; |
| 40 | const char kIPAddress0[] = "192.168.1.1"; |
| 41 | const char kGatewayAddress0[] = "192.168.1.254"; |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 42 | const char kGatewayAddress1[] = "192.168.2.254"; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 43 | const char kBroadcastAddress0[] = "192.168.1.255"; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 44 | const char kNameServer0[] = "8.8.8.8"; |
| 45 | const char kNameServer1[] = "8.8.9.9"; |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 46 | const int32 kPrefix0 = 24; |
| 47 | const int32 kPrefix1 = 31; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 48 | const char kSearchDomain0[] = "chromium.org"; |
| 49 | const char kSearchDomain1[] = "google.com"; |
| 50 | } // namespace {} |
| 51 | |
| 52 | class ConnectionTest : public Test { |
| 53 | public: |
| 54 | ConnectionTest() |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 55 | : device_info_(new StrictMock<MockDeviceInfo>( |
| 56 | &control_, |
| 57 | static_cast<EventDispatcher*>(NULL), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 58 | static_cast<Metrics*>(NULL), |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 59 | static_cast<Manager*>(NULL))), |
| 60 | connection_(new Connection( |
| 61 | kTestDeviceInterfaceIndex0, |
| 62 | kTestDeviceName0, |
Paul Stewart | e00600e | 2012-03-16 07:08:00 -0700 | [diff] [blame] | 63 | Technology::kUnknown, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 64 | device_info_.get())), |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 65 | ipconfig_(new IPConfig(&control_, kTestDeviceName0)), |
| 66 | local_address_(IPAddress::kFamilyIPv4), |
| 67 | broadcast_address_(IPAddress::kFamilyIPv4), |
| 68 | gateway_address_(IPAddress::kFamilyIPv4), |
| 69 | default_address_(IPAddress::kFamilyIPv4) {} |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 70 | |
| 71 | virtual void SetUp() { |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 72 | ReplaceSingletons(connection_); |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 73 | properties_.address = kIPAddress0; |
| 74 | properties_.subnet_prefix = kPrefix0; |
| 75 | properties_.gateway = kGatewayAddress0; |
| 76 | properties_.broadcast_address = kBroadcastAddress0; |
| 77 | properties_.dns_servers.push_back(kNameServer0); |
| 78 | properties_.dns_servers.push_back(kNameServer1); |
| 79 | properties_.domain_search.push_back(kSearchDomain0); |
| 80 | properties_.domain_search.push_back(kSearchDomain1); |
| 81 | properties_.address_family = IPAddress::kFamilyIPv4; |
| 82 | UpdateProperties(); |
| 83 | EXPECT_TRUE(local_address_.SetAddressFromString(kIPAddress0)); |
| 84 | EXPECT_TRUE(broadcast_address_.SetAddressFromString(kBroadcastAddress0)); |
| 85 | EXPECT_TRUE(gateway_address_.SetAddressFromString(kGatewayAddress0)); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 88 | virtual void TearDown() { |
| 89 | EXPECT_CALL(*device_info_, FlushAddresses(kTestDeviceInterfaceIndex0)); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 90 | EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex0)); |
| 91 | EXPECT_CALL(routing_table_, FlushRoutesWithTag(kTestDeviceInterfaceIndex0)); |
| 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: |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 111 | scoped_ptr<StrictMock<MockDeviceInfo> > device_info_; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 112 | ConnectionRefPtr connection_; |
| 113 | MockControl control_; |
| 114 | IPConfigRefPtr ipconfig_; |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 115 | IPConfig::Properties properties_; |
| 116 | IPAddress local_address_; |
| 117 | IPAddress broadcast_address_; |
| 118 | IPAddress gateway_address_; |
| 119 | IPAddress default_address_; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 120 | StrictMock<MockResolver> resolver_; |
| 121 | StrictMock<MockRoutingTable> routing_table_; |
| 122 | StrictMock<MockRTNLHandler> rtnl_handler_; |
| 123 | }; |
| 124 | |
| 125 | TEST_F(ConnectionTest, InitState) { |
| 126 | EXPECT_EQ(kTestDeviceInterfaceIndex0, connection_->interface_index_); |
| 127 | EXPECT_EQ(kTestDeviceName0, connection_->interface_name_); |
| 128 | EXPECT_FALSE(connection_->is_default()); |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 129 | EXPECT_FALSE(connection_->routing_request_count_); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 132 | MATCHER_P2(IsIPAddress, address, prefix, "") { |
| 133 | IPAddress match_address(address); |
| 134 | match_address.set_prefix(prefix); |
| 135 | return match_address.Equals(arg); |
| 136 | } |
| 137 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 138 | TEST_F(ConnectionTest, AddConfig) { |
| 139 | EXPECT_CALL(rtnl_handler_, |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 140 | AddInterfaceAddress(kTestDeviceInterfaceIndex0, |
| 141 | IsIPAddress(local_address_, kPrefix0), |
| 142 | IsIPAddress(broadcast_address_, 0), |
| 143 | IsIPAddress(default_address_, 0))); |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 144 | EXPECT_CALL(routing_table_, |
| 145 | SetDefaultRoute(kTestDeviceInterfaceIndex0, |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 146 | IsIPAddress(gateway_address_, 0), |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 147 | Connection::kNonDefaultMetricBase + |
| 148 | kTestDeviceInterfaceIndex0)); |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 149 | EXPECT_CALL(routing_table_, |
| 150 | ConfigureRoutes(kTestDeviceInterfaceIndex0, |
| 151 | ipconfig_, |
| 152 | Connection::kDefaultMetric)); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 153 | connection_->UpdateFromIPConfig(ipconfig_); |
| 154 | |
| 155 | EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0, |
| 156 | Connection::kDefaultMetric)); |
| 157 | EXPECT_CALL(resolver_, SetDNSFromLists( |
| 158 | ipconfig_->properties().dns_servers, |
| 159 | ipconfig_->properties().domain_search)); |
| 160 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 161 | scoped_refptr<MockDevice> device(new StrictMock<MockDevice>( |
| 162 | &control_, |
| 163 | reinterpret_cast<EventDispatcher *>(NULL), |
| 164 | reinterpret_cast<Metrics *>(NULL), |
| 165 | reinterpret_cast<Manager *>(NULL), |
| 166 | kTestDeviceName0, |
| 167 | string(), |
| 168 | kTestDeviceInterfaceIndex0)); |
| 169 | EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0)) |
| 170 | .WillOnce(Return(device)); |
| 171 | EXPECT_CALL(*device.get(), RequestPortalDetection()) |
| 172 | .WillOnce(Return(true)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 173 | connection_->SetIsDefault(true); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 174 | EXPECT_TRUE(connection_->is_default()); |
| 175 | |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 176 | EXPECT_CALL(routing_table_, |
| 177 | SetDefaultMetric(kTestDeviceInterfaceIndex0, |
| 178 | Connection::kNonDefaultMetricBase + |
| 179 | kTestDeviceInterfaceIndex0)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 180 | connection_->SetIsDefault(false); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 181 | EXPECT_FALSE(connection_->is_default()); |
| 182 | } |
| 183 | |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 184 | TEST_F(ConnectionTest, AddConfigWithPeer) { |
| 185 | const string kPeerAddress("192.168.1.222"); |
| 186 | IPAddress peer_address(IPAddress::kFamilyIPv4); |
| 187 | EXPECT_TRUE(peer_address.SetAddressFromString(kPeerAddress)); |
| 188 | properties_.peer_address = kPeerAddress; |
| 189 | properties_.gateway = string(); |
| 190 | UpdateProperties(); |
| 191 | EXPECT_CALL(rtnl_handler_, |
| 192 | AddInterfaceAddress(kTestDeviceInterfaceIndex0, |
| 193 | IsIPAddress(local_address_, kPrefix0), |
| 194 | IsIPAddress(broadcast_address_, 0), |
| 195 | IsIPAddress(peer_address, 0))); |
| 196 | EXPECT_CALL(routing_table_, SetDefaultRoute(_, _, _)).Times(0); |
| 197 | EXPECT_CALL(routing_table_, |
| 198 | ConfigureRoutes(kTestDeviceInterfaceIndex0, |
| 199 | ipconfig_, |
| 200 | Connection::kDefaultMetric)); |
| 201 | connection_->UpdateFromIPConfig(ipconfig_); |
| 202 | } |
| 203 | |
| 204 | TEST_F(ConnectionTest, AddConfigWithBrokenNetmask) { |
| 205 | // Assign a prefix that makes the gateway unreachable. |
| 206 | properties_.subnet_prefix = kPrefix1; |
| 207 | UpdateProperties(); |
| 208 | |
| 209 | // Connection should override with a prefix which will allow the |
| 210 | // gateway to be reachable. |
| 211 | EXPECT_CALL(rtnl_handler_, |
| 212 | AddInterfaceAddress(kTestDeviceInterfaceIndex0, |
| 213 | IsIPAddress(local_address_, kPrefix0), |
| 214 | IsIPAddress(broadcast_address_, 0), |
| 215 | IsIPAddress(default_address_, 0))); |
| 216 | EXPECT_CALL(routing_table_, |
| 217 | SetDefaultRoute(kTestDeviceInterfaceIndex0, |
| 218 | IsIPAddress(gateway_address_, 0), |
| 219 | Connection::kNonDefaultMetricBase + |
| 220 | kTestDeviceInterfaceIndex0)); |
| 221 | EXPECT_CALL(routing_table_, |
| 222 | ConfigureRoutes(kTestDeviceInterfaceIndex0, |
| 223 | ipconfig_, |
| 224 | Connection::kDefaultMetric)); |
| 225 | connection_->UpdateFromIPConfig(ipconfig_); |
| 226 | |
| 227 | // Assign a gateway address that violates the minimum plausible prefix |
| 228 | // the Connection can assign. |
| 229 | properties_.gateway = kGatewayAddress1; |
| 230 | UpdateProperties(); |
| 231 | |
| 232 | // Connection cannot override this prefix, so it will revert to the |
| 233 | // configured prefix, expecting the default route to fail. |
| 234 | EXPECT_CALL(rtnl_handler_, |
| 235 | AddInterfaceAddress(kTestDeviceInterfaceIndex0, |
| 236 | IsIPAddress(local_address_, kPrefix1), |
| 237 | IsIPAddress(broadcast_address_, 0), |
| 238 | IsIPAddress(default_address_, 0))); |
| 239 | EXPECT_CALL(routing_table_, |
| 240 | SetDefaultRoute(kTestDeviceInterfaceIndex0, _, _)); |
| 241 | EXPECT_CALL(routing_table_, |
| 242 | ConfigureRoutes(kTestDeviceInterfaceIndex0, _, _)); |
| 243 | connection_->UpdateFromIPConfig(ipconfig_); |
| 244 | } |
| 245 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 246 | TEST_F(ConnectionTest, AddConfigReverse) { |
| 247 | EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0, |
| 248 | Connection::kDefaultMetric)); |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 249 | vector<string> empty_list; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 250 | EXPECT_CALL(resolver_, SetDNSFromLists(empty_list, empty_list)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 251 | scoped_refptr<MockDevice> device(new StrictMock<MockDevice>( |
| 252 | &control_, |
| 253 | reinterpret_cast<EventDispatcher *>(NULL), |
| 254 | reinterpret_cast<Metrics *>(NULL), |
| 255 | reinterpret_cast<Manager *>(NULL), |
| 256 | kTestDeviceName0, |
| 257 | string(), |
| 258 | kTestDeviceInterfaceIndex0)); |
| 259 | EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0)) |
| 260 | .WillOnce(Return(device)); |
| 261 | EXPECT_CALL(*device.get(), RequestPortalDetection()) |
| 262 | .WillOnce(Return(true)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 263 | connection_->SetIsDefault(true); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 264 | |
| 265 | EXPECT_CALL(rtnl_handler_, |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 266 | AddInterfaceAddress(kTestDeviceInterfaceIndex0, |
| 267 | IsIPAddress(local_address_, kPrefix0), |
| 268 | IsIPAddress(broadcast_address_, 0), |
| 269 | IsIPAddress(default_address_, 0))); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 270 | EXPECT_CALL(routing_table_, SetDefaultRoute(kTestDeviceInterfaceIndex0, |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 271 | IsIPAddress(gateway_address_, 0), |
| 272 | Connection::kDefaultMetric)); |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 273 | EXPECT_CALL(routing_table_, |
| 274 | ConfigureRoutes(kTestDeviceInterfaceIndex0, |
| 275 | ipconfig_, |
| 276 | Connection::kDefaultMetric)); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 277 | EXPECT_CALL(resolver_, SetDNSFromIPConfig(ipconfig_)); |
| 278 | |
| 279 | connection_->UpdateFromIPConfig(ipconfig_); |
| 280 | } |
| 281 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 282 | TEST_F(ConnectionTest, RouteRequest) { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 283 | ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex0, |
| 284 | kTestDeviceName0, |
Paul Stewart | e00600e | 2012-03-16 07:08:00 -0700 | [diff] [blame] | 285 | Technology::kUnknown, |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 286 | device_info_.get())); |
| 287 | ReplaceSingletons(connection); |
| 288 | scoped_refptr<MockDevice> device(new StrictMock<MockDevice>( |
| 289 | &control_, |
| 290 | reinterpret_cast<EventDispatcher *>(NULL), |
| 291 | reinterpret_cast<Metrics *>(NULL), |
| 292 | reinterpret_cast<Manager *>(NULL), |
| 293 | kTestDeviceName0, |
| 294 | string(), |
| 295 | kTestDeviceInterfaceIndex0)); |
| 296 | EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0)) |
| 297 | .WillRepeatedly(Return(device)); |
| 298 | EXPECT_CALL(*device.get(), DisableReversePathFilter()).Times(1); |
| 299 | connection->RequestRouting(); |
| 300 | connection->RequestRouting(); |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 301 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 302 | // The first release should only decrement the reference counter. |
| 303 | connection->ReleaseRouting(); |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 304 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 305 | // Another release will re-enable reverse-path filter. |
| 306 | EXPECT_CALL(*device.get(), EnableReversePathFilter()); |
| 307 | EXPECT_CALL(routing_table_, FlushCache()); |
| 308 | connection->ReleaseRouting(); |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 309 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 310 | // The destructor will remove the routes and addresses. |
| 311 | EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex0)); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 312 | EXPECT_CALL(routing_table_, FlushRoutesWithTag(kTestDeviceInterfaceIndex0)); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 313 | EXPECT_CALL(*device_info_.get(), |
| 314 | FlushAddresses(kTestDeviceInterfaceIndex0)); |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 315 | } |
| 316 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 317 | TEST_F(ConnectionTest, Destructor) { |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 318 | EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex1)); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 319 | EXPECT_CALL(routing_table_, FlushRoutesWithTag(kTestDeviceInterfaceIndex1)); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 320 | EXPECT_CALL(*device_info_, FlushAddresses(kTestDeviceInterfaceIndex1)); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 321 | { |
| 322 | ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex1, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 323 | kTestDeviceName1, |
Paul Stewart | e00600e | 2012-03-16 07:08:00 -0700 | [diff] [blame] | 324 | Technology::kUnknown, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 325 | device_info_.get())); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 326 | connection->resolver_ = &resolver_; |
| 327 | connection->routing_table_ = &routing_table_; |
| 328 | connection->rtnl_handler_ = &rtnl_handler_; |
| 329 | } |
| 330 | } |
| 331 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 332 | TEST_F(ConnectionTest, RequestHostRoute) { |
| 333 | ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex0, |
| 334 | kTestDeviceName0, |
Paul Stewart | e00600e | 2012-03-16 07:08:00 -0700 | [diff] [blame] | 335 | Technology::kUnknown, |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 336 | device_info_.get())); |
| 337 | ReplaceSingletons(connection); |
| 338 | IPAddress address(IPAddress::kFamilyIPv4); |
| 339 | ASSERT_TRUE(address.SetAddressFromString(kIPAddress0)); |
| 340 | size_t prefix_len = address.GetLength() * 8; |
| 341 | EXPECT_CALL(routing_table_, RequestRouteToHost( |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 342 | IsIPAddress(address, prefix_len), -1, kTestDeviceInterfaceIndex0)) |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 343 | .WillOnce(Return(true)); |
| 344 | EXPECT_TRUE(connection->RequestHostRoute(address)); |
| 345 | |
| 346 | // The destructor will remove the routes and addresses. |
| 347 | EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex0)); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 348 | EXPECT_CALL(routing_table_, FlushRoutesWithTag(kTestDeviceInterfaceIndex0)); |
| 349 | EXPECT_CALL(*device_info_.get(), |
| 350 | FlushAddresses(kTestDeviceInterfaceIndex0)); |
| 351 | } |
| 352 | |
| 353 | TEST_F(ConnectionTest, PinHostRoute) { |
| 354 | static const char kGateway[] = "10.242.2.13"; |
| 355 | static const char kNetwork[] = "10.242.2.1"; |
| 356 | |
| 357 | ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex0, |
| 358 | kTestDeviceName0, |
| 359 | Technology::kUnknown, |
| 360 | device_info_.get())); |
| 361 | ReplaceSingletons(connection); |
| 362 | |
| 363 | IPConfig::Properties props; |
| 364 | props.address_family = IPAddress::kFamilyIPv4; |
| 365 | EXPECT_FALSE(PinHostRoute(connection, props)); |
| 366 | |
| 367 | props.gateway = kGateway; |
| 368 | EXPECT_FALSE(PinHostRoute(connection, props)); |
| 369 | |
| 370 | props.gateway.clear(); |
| 371 | props.trusted_ip = "xxx"; |
| 372 | EXPECT_FALSE(PinHostRoute(connection, props)); |
| 373 | |
| 374 | props.gateway = kGateway; |
| 375 | EXPECT_FALSE(PinHostRoute(connection, props)); |
| 376 | |
| 377 | props.trusted_ip = kNetwork; |
| 378 | IPAddress address(IPAddress::kFamilyIPv4); |
| 379 | ASSERT_TRUE(address.SetAddressFromString(kNetwork)); |
| 380 | size_t prefix_len = address.GetLength() * 8; |
| 381 | EXPECT_CALL(routing_table_, RequestRouteToHost( |
| 382 | IsIPAddress(address, prefix_len), -1, kTestDeviceInterfaceIndex0)) |
| 383 | .WillOnce(Return(false)); |
| 384 | EXPECT_FALSE(PinHostRoute(connection, props)); |
| 385 | |
| 386 | EXPECT_CALL(routing_table_, RequestRouteToHost( |
| 387 | IsIPAddress(address, prefix_len), -1, kTestDeviceInterfaceIndex0)) |
| 388 | .WillOnce(Return(true)); |
| 389 | EXPECT_TRUE(PinHostRoute(connection, props)); |
| 390 | |
| 391 | // The destructor will remove the routes and addresses. |
| 392 | EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex0)); |
| 393 | EXPECT_CALL(routing_table_, FlushRoutesWithTag(kTestDeviceInterfaceIndex0)); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 394 | EXPECT_CALL(*device_info_.get(), |
| 395 | FlushAddresses(kTestDeviceInterfaceIndex0)); |
| 396 | } |
| 397 | |
Paul Stewart | 53a3038 | 2012-04-26 09:06:59 -0700 | [diff] [blame^] | 398 | TEST_F(ConnectionTest, FixGatewayReachability) { |
| 399 | static const char kLocal[] = "10.242.2.13"; |
| 400 | IPAddress local(IPAddress::kFamilyIPv4); |
| 401 | ASSERT_TRUE(local.SetAddressFromString(kLocal)); |
| 402 | const int kPrefix = 24; |
| 403 | local.set_prefix(kPrefix); |
| 404 | IPAddress gateway(IPAddress::kFamilyIPv4); |
| 405 | IPAddress peer(IPAddress::kFamilyIPv4); |
| 406 | |
| 407 | // Should fail because no gateway is set. |
| 408 | EXPECT_FALSE(Connection::FixGatewayReachability(&local, gateway, peer)); |
| 409 | EXPECT_EQ(kPrefix, local.prefix()); |
| 410 | |
| 411 | // Should succeed because with the given prefix, this gateway is reachable. |
| 412 | static const char kReachableGateway[] = "10.242.2.14"; |
| 413 | ASSERT_TRUE(gateway.SetAddressFromString(kReachableGateway)); |
| 414 | EXPECT_TRUE(Connection::FixGatewayReachability(&local, gateway, peer)); |
| 415 | // Prefix should remain unchanged. |
| 416 | EXPECT_EQ(kPrefix, local.prefix()); |
| 417 | |
| 418 | // Should succeed because we modified the prefix to match the gateway. |
| 419 | static const char kExpandableGateway[] = "10.242.3.14"; |
| 420 | ASSERT_TRUE(gateway.SetAddressFromString(kExpandableGateway)); |
| 421 | EXPECT_TRUE(Connection::FixGatewayReachability(&local, gateway, peer)); |
| 422 | // Prefix should have opened up by 1 bit. |
| 423 | EXPECT_EQ(kPrefix - 1, local.prefix()); |
| 424 | |
| 425 | // Should fail because we cannot plausibly expand the prefix past 8. |
| 426 | local.set_prefix(kPrefix); |
| 427 | static const char kUnreachableGateway[] = "11.242.2.14"; |
| 428 | ASSERT_TRUE(gateway.SetAddressFromString(kUnreachableGateway)); |
| 429 | EXPECT_FALSE(Connection::FixGatewayReachability(&local, gateway, peer)); |
| 430 | // Prefix should not have changed. |
| 431 | EXPECT_EQ(kPrefix, local.prefix()); |
| 432 | |
| 433 | // However, if this is a peer-to-peer interface and the peer matches |
| 434 | // the gateway, we should succeed. |
| 435 | ASSERT_TRUE(peer.SetAddressFromString(kUnreachableGateway)); |
| 436 | EXPECT_TRUE(Connection::FixGatewayReachability(&local, gateway, peer)); |
| 437 | EXPECT_EQ(kPrefix, local.prefix()); |
| 438 | |
| 439 | // If there is a peer specified and it does not match the gateway (even |
| 440 | // if it was reachable via netmask), we should fail. |
| 441 | ASSERT_TRUE(gateway.SetAddressFromString(kReachableGateway)); |
| 442 | EXPECT_FALSE(Connection::FixGatewayReachability(&local, gateway, peer)); |
| 443 | EXPECT_EQ(kPrefix, local.prefix()); |
| 444 | } |
| 445 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 446 | } // namespace shill |