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, |
| 60 | device_info_.get())), |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 61 | ipconfig_(new IPConfig(&control_, kTestDeviceName0)) {} |
| 62 | |
| 63 | virtual void SetUp() { |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 64 | ReplaceSingletons(connection_); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 65 | IPConfig::Properties properties; |
| 66 | properties.address = kIPAddress0; |
| 67 | properties.gateway = kGatewayAddress0; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 68 | properties.broadcast_address = kBroadcastAddress0; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 69 | properties.dns_servers.push_back(kNameServer0); |
| 70 | properties.dns_servers.push_back(kNameServer1); |
| 71 | properties.domain_search.push_back(kSearchDomain0); |
| 72 | properties.domain_search.push_back(kSearchDomain1); |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 73 | properties.address_family = IPAddress::kFamilyIPv4; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 74 | ipconfig_->UpdateProperties(properties, true); |
| 75 | } |
| 76 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 77 | virtual void TearDown() { |
| 78 | EXPECT_CALL(*device_info_, FlushAddresses(kTestDeviceInterfaceIndex0)); |
| 79 | } |
| 80 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 81 | void ReplaceSingletons(ConnectionRefPtr connection) { |
| 82 | connection->resolver_ = &resolver_; |
| 83 | connection->routing_table_ = &routing_table_; |
| 84 | connection->rtnl_handler_ = &rtnl_handler_; |
| 85 | } |
| 86 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 87 | protected: |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 88 | scoped_ptr<StrictMock<MockDeviceInfo> > device_info_; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 89 | ConnectionRefPtr connection_; |
| 90 | MockControl control_; |
| 91 | IPConfigRefPtr ipconfig_; |
| 92 | StrictMock<MockResolver> resolver_; |
| 93 | StrictMock<MockRoutingTable> routing_table_; |
| 94 | StrictMock<MockRTNLHandler> rtnl_handler_; |
| 95 | }; |
| 96 | |
| 97 | TEST_F(ConnectionTest, InitState) { |
| 98 | EXPECT_EQ(kTestDeviceInterfaceIndex0, connection_->interface_index_); |
| 99 | EXPECT_EQ(kTestDeviceName0, connection_->interface_name_); |
| 100 | EXPECT_FALSE(connection_->is_default()); |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 101 | EXPECT_FALSE(connection_->routing_request_count_); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | TEST_F(ConnectionTest, AddConfig) { |
| 105 | EXPECT_CALL(rtnl_handler_, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 106 | AddInterfaceAddress(kTestDeviceInterfaceIndex0, _, _)); |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 107 | EXPECT_CALL(routing_table_, |
| 108 | SetDefaultRoute(kTestDeviceInterfaceIndex0, |
| 109 | ipconfig_, |
| 110 | Connection::kNonDefaultMetricBase + |
| 111 | kTestDeviceInterfaceIndex0)); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 112 | connection_->UpdateFromIPConfig(ipconfig_); |
| 113 | |
| 114 | EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0, |
| 115 | Connection::kDefaultMetric)); |
| 116 | EXPECT_CALL(resolver_, SetDNSFromLists( |
| 117 | ipconfig_->properties().dns_servers, |
| 118 | ipconfig_->properties().domain_search)); |
| 119 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 120 | connection_->SetIsDefault(true); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 121 | EXPECT_TRUE(connection_->is_default()); |
| 122 | |
Paul Stewart | 7cfca04 | 2011-12-08 14:18:17 -0800 | [diff] [blame] | 123 | EXPECT_CALL(routing_table_, |
| 124 | SetDefaultMetric(kTestDeviceInterfaceIndex0, |
| 125 | Connection::kNonDefaultMetricBase + |
| 126 | kTestDeviceInterfaceIndex0)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 127 | connection_->SetIsDefault(false); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 128 | EXPECT_FALSE(connection_->is_default()); |
| 129 | } |
| 130 | |
| 131 | TEST_F(ConnectionTest, AddConfigReverse) { |
| 132 | EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0, |
| 133 | Connection::kDefaultMetric)); |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 134 | vector<string> empty_list; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 135 | EXPECT_CALL(resolver_, SetDNSFromLists(empty_list, empty_list)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 136 | connection_->SetIsDefault(true); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 137 | |
| 138 | EXPECT_CALL(rtnl_handler_, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 139 | AddInterfaceAddress(kTestDeviceInterfaceIndex0, _, _)); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 140 | EXPECT_CALL(routing_table_, SetDefaultRoute(kTestDeviceInterfaceIndex0, |
| 141 | ipconfig_, |
| 142 | Connection::kDefaultMetric)); |
| 143 | EXPECT_CALL(resolver_, SetDNSFromIPConfig(ipconfig_)); |
| 144 | |
| 145 | connection_->UpdateFromIPConfig(ipconfig_); |
| 146 | } |
| 147 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 148 | TEST_F(ConnectionTest, RouteRequest) { |
| 149 | { |
| 150 | ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex0, |
| 151 | kTestDeviceName0, |
| 152 | device_info_.get())); |
| 153 | ReplaceSingletons(connection); |
| 154 | scoped_refptr<MockDevice> device(new StrictMock<MockDevice>( |
| 155 | &control_, |
| 156 | reinterpret_cast<EventDispatcher *>(NULL), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame^] | 157 | reinterpret_cast<Metrics *>(NULL), |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 158 | reinterpret_cast<Manager *>(NULL), |
| 159 | kTestDeviceName0, |
| 160 | string(), |
| 161 | kTestDeviceInterfaceIndex0)); |
| 162 | EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0)) |
| 163 | .WillRepeatedly(Return(device)); |
| 164 | EXPECT_CALL(*device.get(), DisableReversePathFilter()).Times(1); |
| 165 | connection->RequestRouting(); |
| 166 | connection->RequestRouting(); |
| 167 | |
| 168 | // The first release should only decrement the reference counter. |
| 169 | connection->ReleaseRouting(); |
| 170 | |
| 171 | // Another release will re-enable reverse-path filter. |
| 172 | EXPECT_CALL(*device.get(), EnableReversePathFilter()); |
| 173 | EXPECT_CALL(routing_table_, FlushCache()); |
| 174 | connection->ReleaseRouting(); |
| 175 | |
| 176 | // The destructor will remove the routes and addresses. |
| 177 | EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex0)); |
| 178 | EXPECT_CALL(*device_info_.get(), |
| 179 | FlushAddresses(kTestDeviceInterfaceIndex0)); |
| 180 | } |
| 181 | } |
| 182 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 183 | TEST_F(ConnectionTest, Destructor) { |
| 184 | EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex1)); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 185 | EXPECT_CALL(*device_info_, FlushAddresses(kTestDeviceInterfaceIndex1)); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 186 | { |
| 187 | ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex1, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 188 | kTestDeviceName1, |
| 189 | device_info_.get())); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 190 | connection->resolver_ = &resolver_; |
| 191 | connection->routing_table_ = &routing_table_; |
| 192 | connection->rtnl_handler_ = &rtnl_handler_; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | } // namespace shill |