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 | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 42 | const char kBroadcastAddress0[] = "192.168.1.255"; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 43 | const char kNameServer0[] = "8.8.8.8"; |
| 44 | const char kNameServer1[] = "8.8.9.9"; |
| 45 | const char kSearchDomain0[] = "chromium.org"; |
| 46 | const char kSearchDomain1[] = "google.com"; |
| 47 | } // namespace {} |
| 48 | |
| 49 | class ConnectionTest : public Test { |
| 50 | public: |
| 51 | ConnectionTest() |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 52 | : device_info_(new StrictMock<MockDeviceInfo>( |
| 53 | &control_, |
| 54 | static_cast<EventDispatcher*>(NULL), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 55 | static_cast<Metrics*>(NULL), |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 56 | static_cast<Manager*>(NULL))), |
| 57 | connection_(new Connection( |
| 58 | kTestDeviceInterfaceIndex0, |
| 59 | kTestDeviceName0, |
Paul Stewart | e00600e | 2012-03-16 07:08:00 -0700 | [diff] [blame] | 60 | Technology::kUnknown, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 61 | device_info_.get())), |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 62 | ipconfig_(new IPConfig(&control_, kTestDeviceName0)) {} |
| 63 | |
| 64 | virtual void SetUp() { |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 65 | ReplaceSingletons(connection_); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 66 | IPConfig::Properties properties; |
| 67 | properties.address = kIPAddress0; |
| 68 | properties.gateway = kGatewayAddress0; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 69 | properties.broadcast_address = kBroadcastAddress0; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 70 | properties.dns_servers.push_back(kNameServer0); |
| 71 | properties.dns_servers.push_back(kNameServer1); |
| 72 | properties.domain_search.push_back(kSearchDomain0); |
| 73 | properties.domain_search.push_back(kSearchDomain1); |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 74 | properties.address_family = IPAddress::kFamilyIPv4; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 75 | ipconfig_->UpdateProperties(properties, true); |
| 76 | } |
| 77 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 78 | virtual void TearDown() { |
| 79 | EXPECT_CALL(*device_info_, FlushAddresses(kTestDeviceInterfaceIndex0)); |
| 80 | } |
| 81 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 82 | void ReplaceSingletons(ConnectionRefPtr connection) { |
| 83 | connection->resolver_ = &resolver_; |
| 84 | connection->routing_table_ = &routing_table_; |
| 85 | connection->rtnl_handler_ = &rtnl_handler_; |
| 86 | } |
| 87 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 88 | protected: |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 89 | scoped_ptr<StrictMock<MockDeviceInfo> > device_info_; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 90 | ConnectionRefPtr connection_; |
| 91 | MockControl control_; |
| 92 | IPConfigRefPtr ipconfig_; |
| 93 | StrictMock<MockResolver> resolver_; |
| 94 | StrictMock<MockRoutingTable> routing_table_; |
| 95 | StrictMock<MockRTNLHandler> rtnl_handler_; |
| 96 | }; |
| 97 | |
| 98 | TEST_F(ConnectionTest, InitState) { |
| 99 | EXPECT_EQ(kTestDeviceInterfaceIndex0, connection_->interface_index_); |
| 100 | EXPECT_EQ(kTestDeviceName0, connection_->interface_name_); |
| 101 | EXPECT_FALSE(connection_->is_default()); |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 102 | EXPECT_FALSE(connection_->routing_request_count_); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | TEST_F(ConnectionTest, AddConfig) { |
| 106 | EXPECT_CALL(rtnl_handler_, |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 107 | AddInterfaceAddress(kTestDeviceInterfaceIndex0, _, _, _)); |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 108 | EXPECT_CALL(routing_table_, |
| 109 | SetDefaultRoute(kTestDeviceInterfaceIndex0, |
| 110 | ipconfig_, |
| 111 | Connection::kNonDefaultMetricBase + |
| 112 | kTestDeviceInterfaceIndex0)); |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 113 | EXPECT_CALL(routing_table_, |
| 114 | ConfigureRoutes(kTestDeviceInterfaceIndex0, |
| 115 | ipconfig_, |
| 116 | Connection::kDefaultMetric)); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 117 | connection_->UpdateFromIPConfig(ipconfig_); |
| 118 | |
| 119 | EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0, |
| 120 | Connection::kDefaultMetric)); |
| 121 | EXPECT_CALL(resolver_, SetDNSFromLists( |
| 122 | ipconfig_->properties().dns_servers, |
| 123 | ipconfig_->properties().domain_search)); |
| 124 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 125 | scoped_refptr<MockDevice> device(new StrictMock<MockDevice>( |
| 126 | &control_, |
| 127 | reinterpret_cast<EventDispatcher *>(NULL), |
| 128 | reinterpret_cast<Metrics *>(NULL), |
| 129 | reinterpret_cast<Manager *>(NULL), |
| 130 | kTestDeviceName0, |
| 131 | string(), |
| 132 | kTestDeviceInterfaceIndex0)); |
| 133 | EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0)) |
| 134 | .WillOnce(Return(device)); |
| 135 | EXPECT_CALL(*device.get(), RequestPortalDetection()) |
| 136 | .WillOnce(Return(true)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 137 | connection_->SetIsDefault(true); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 138 | EXPECT_TRUE(connection_->is_default()); |
| 139 | |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 140 | EXPECT_CALL(routing_table_, |
| 141 | SetDefaultMetric(kTestDeviceInterfaceIndex0, |
| 142 | Connection::kNonDefaultMetricBase + |
| 143 | kTestDeviceInterfaceIndex0)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 144 | connection_->SetIsDefault(false); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 145 | EXPECT_FALSE(connection_->is_default()); |
| 146 | } |
| 147 | |
| 148 | TEST_F(ConnectionTest, AddConfigReverse) { |
| 149 | EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0, |
| 150 | Connection::kDefaultMetric)); |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 151 | vector<string> empty_list; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 152 | EXPECT_CALL(resolver_, SetDNSFromLists(empty_list, empty_list)); |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 153 | scoped_refptr<MockDevice> device(new StrictMock<MockDevice>( |
| 154 | &control_, |
| 155 | reinterpret_cast<EventDispatcher *>(NULL), |
| 156 | reinterpret_cast<Metrics *>(NULL), |
| 157 | reinterpret_cast<Manager *>(NULL), |
| 158 | kTestDeviceName0, |
| 159 | string(), |
| 160 | kTestDeviceInterfaceIndex0)); |
| 161 | EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0)) |
| 162 | .WillOnce(Return(device)); |
| 163 | EXPECT_CALL(*device.get(), RequestPortalDetection()) |
| 164 | .WillOnce(Return(true)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 165 | connection_->SetIsDefault(true); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 166 | |
| 167 | EXPECT_CALL(rtnl_handler_, |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 168 | AddInterfaceAddress(kTestDeviceInterfaceIndex0, _, _, _)); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 169 | EXPECT_CALL(routing_table_, SetDefaultRoute(kTestDeviceInterfaceIndex0, |
| 170 | ipconfig_, |
| 171 | Connection::kDefaultMetric)); |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 172 | EXPECT_CALL(routing_table_, |
| 173 | ConfigureRoutes(kTestDeviceInterfaceIndex0, |
| 174 | ipconfig_, |
| 175 | Connection::kDefaultMetric)); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 176 | EXPECT_CALL(resolver_, SetDNSFromIPConfig(ipconfig_)); |
| 177 | |
| 178 | connection_->UpdateFromIPConfig(ipconfig_); |
| 179 | } |
| 180 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 181 | TEST_F(ConnectionTest, RouteRequest) { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 182 | ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex0, |
| 183 | kTestDeviceName0, |
Paul Stewart | e00600e | 2012-03-16 07:08:00 -0700 | [diff] [blame] | 184 | Technology::kUnknown, |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 185 | device_info_.get())); |
| 186 | ReplaceSingletons(connection); |
| 187 | scoped_refptr<MockDevice> device(new StrictMock<MockDevice>( |
| 188 | &control_, |
| 189 | reinterpret_cast<EventDispatcher *>(NULL), |
| 190 | reinterpret_cast<Metrics *>(NULL), |
| 191 | reinterpret_cast<Manager *>(NULL), |
| 192 | kTestDeviceName0, |
| 193 | string(), |
| 194 | kTestDeviceInterfaceIndex0)); |
| 195 | EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0)) |
| 196 | .WillRepeatedly(Return(device)); |
| 197 | EXPECT_CALL(*device.get(), DisableReversePathFilter()).Times(1); |
| 198 | connection->RequestRouting(); |
| 199 | connection->RequestRouting(); |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 200 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 201 | // The first release should only decrement the reference counter. |
| 202 | connection->ReleaseRouting(); |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 203 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 204 | // Another release will re-enable reverse-path filter. |
| 205 | EXPECT_CALL(*device.get(), EnableReversePathFilter()); |
| 206 | EXPECT_CALL(routing_table_, FlushCache()); |
| 207 | connection->ReleaseRouting(); |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 208 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 209 | // The destructor will remove the routes and addresses. |
| 210 | EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex0)); |
| 211 | EXPECT_CALL(*device_info_.get(), |
| 212 | FlushAddresses(kTestDeviceInterfaceIndex0)); |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 215 | TEST_F(ConnectionTest, Destructor) { |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 216 | EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex1)); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 217 | EXPECT_CALL(*device_info_, FlushAddresses(kTestDeviceInterfaceIndex1)); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 218 | { |
| 219 | ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex1, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 220 | kTestDeviceName1, |
Paul Stewart | e00600e | 2012-03-16 07:08:00 -0700 | [diff] [blame] | 221 | Technology::kUnknown, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 222 | device_info_.get())); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 223 | connection->resolver_ = &resolver_; |
| 224 | connection->routing_table_ = &routing_table_; |
| 225 | connection->rtnl_handler_ = &rtnl_handler_; |
| 226 | } |
| 227 | } |
| 228 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 229 | MATCHER_P2(IsIPAddress, address, prefix, "") { |
| 230 | IPAddress match_address(address); |
| 231 | match_address.set_prefix(prefix); |
| 232 | return match_address.Equals(arg); |
| 233 | } |
| 234 | |
| 235 | TEST_F(ConnectionTest, RequestHostRoute) { |
| 236 | ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex0, |
| 237 | kTestDeviceName0, |
Paul Stewart | e00600e | 2012-03-16 07:08:00 -0700 | [diff] [blame] | 238 | Technology::kUnknown, |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 239 | device_info_.get())); |
| 240 | ReplaceSingletons(connection); |
| 241 | IPAddress address(IPAddress::kFamilyIPv4); |
| 242 | ASSERT_TRUE(address.SetAddressFromString(kIPAddress0)); |
| 243 | size_t prefix_len = address.GetLength() * 8; |
| 244 | EXPECT_CALL(routing_table_, RequestRouteToHost( |
Paul Stewart | 536820d | 2012-03-19 16:05:59 -0700 | [diff] [blame] | 245 | IsIPAddress(address, prefix_len), -1)) |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 246 | .WillOnce(Return(true)); |
| 247 | EXPECT_TRUE(connection->RequestHostRoute(address)); |
| 248 | |
| 249 | // The destructor will remove the routes and addresses. |
| 250 | EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex0)); |
| 251 | EXPECT_CALL(*device_info_.get(), |
| 252 | FlushAddresses(kTestDeviceInterfaceIndex0)); |
| 253 | } |
| 254 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 255 | } // namespace shill |