blob: 690c7a705e5178d16373f05b76083ea8ca0e8f2b [file] [log] [blame]
Thieu Le3426c8f2012-01-11 17:35:11 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewartdd60e452011-08-08 11:38:36 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Paul Stewart9a908082011-08-31 12:18:48 -07005#include <arpa/inet.h>
6#include <linux/rtnetlink.h>
7
Paul Stewartc8f4bef2011-12-13 09:45:51 -08008#include <string>
Paul Stewart9a908082011-08-31 12:18:48 -07009#include <vector>
10
11#include <base/memory/scoped_ptr.h>
Paul Stewartdd60e452011-08-08 11:38:36 -070012#include <gtest/gtest.h>
13#include <gmock/gmock.h>
14
15#include "shill/connection.h"
Paul Stewartdd60e452011-08-08 11:38:36 -070016#include "shill/ipconfig.h"
17#include "shill/mock_control.h"
Paul Stewartc8f4bef2011-12-13 09:45:51 -080018#include "shill/mock_device.h"
Paul Stewart9a908082011-08-31 12:18:48 -070019#include "shill/mock_device_info.h"
Paul Stewartdd60e452011-08-08 11:38:36 -070020#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 Stewartc8f4bef2011-12-13 09:45:51 -080025using std::string;
Paul Stewart9a908082011-08-31 12:18:48 -070026using std::vector;
Paul Stewartdd60e452011-08-08 11:38:36 -070027using testing::_;
28using testing::NiceMock;
29using testing::Return;
30using testing::StrictMock;
31using testing::Test;
32
33namespace shill {
34
35namespace {
36const char kTestDeviceName0[] = "netdev0";
37const int kTestDeviceInterfaceIndex0 = 123;
38const char kTestDeviceName1[] = "netdev1";
39const int kTestDeviceInterfaceIndex1 = 321;
40const char kIPAddress0[] = "192.168.1.1";
41const char kGatewayAddress0[] = "192.168.1.254";
Paul Stewart9a908082011-08-31 12:18:48 -070042const char kBroadcastAddress0[] = "192.168.1.255";
Paul Stewartdd60e452011-08-08 11:38:36 -070043const char kNameServer0[] = "8.8.8.8";
44const char kNameServer1[] = "8.8.9.9";
45const char kSearchDomain0[] = "chromium.org";
46const char kSearchDomain1[] = "google.com";
47} // namespace {}
48
49class ConnectionTest : public Test {
50 public:
51 ConnectionTest()
Paul Stewart9a908082011-08-31 12:18:48 -070052 : device_info_(new StrictMock<MockDeviceInfo>(
53 &control_,
54 static_cast<EventDispatcher*>(NULL),
Thieu Le3426c8f2012-01-11 17:35:11 -080055 static_cast<Metrics*>(NULL),
Paul Stewart9a908082011-08-31 12:18:48 -070056 static_cast<Manager*>(NULL))),
57 connection_(new Connection(
58 kTestDeviceInterfaceIndex0,
59 kTestDeviceName0,
60 device_info_.get())),
Paul Stewartdd60e452011-08-08 11:38:36 -070061 ipconfig_(new IPConfig(&control_, kTestDeviceName0)) {}
62
63 virtual void SetUp() {
Paul Stewartc8f4bef2011-12-13 09:45:51 -080064 ReplaceSingletons(connection_);
Paul Stewartdd60e452011-08-08 11:38:36 -070065 IPConfig::Properties properties;
66 properties.address = kIPAddress0;
67 properties.gateway = kGatewayAddress0;
Paul Stewart9a908082011-08-31 12:18:48 -070068 properties.broadcast_address = kBroadcastAddress0;
Paul Stewartdd60e452011-08-08 11:38:36 -070069 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 Stewart7355ce12011-09-02 10:47:01 -070073 properties.address_family = IPAddress::kFamilyIPv4;
Paul Stewartdd60e452011-08-08 11:38:36 -070074 ipconfig_->UpdateProperties(properties, true);
75 }
76
Paul Stewart9a908082011-08-31 12:18:48 -070077 virtual void TearDown() {
78 EXPECT_CALL(*device_info_, FlushAddresses(kTestDeviceInterfaceIndex0));
79 }
80
Paul Stewartc8f4bef2011-12-13 09:45:51 -080081 void ReplaceSingletons(ConnectionRefPtr connection) {
82 connection->resolver_ = &resolver_;
83 connection->routing_table_ = &routing_table_;
84 connection->rtnl_handler_ = &rtnl_handler_;
85 }
86
Paul Stewartdd60e452011-08-08 11:38:36 -070087 protected:
Paul Stewart9a908082011-08-31 12:18:48 -070088 scoped_ptr<StrictMock<MockDeviceInfo> > device_info_;
Paul Stewartdd60e452011-08-08 11:38:36 -070089 ConnectionRefPtr connection_;
90 MockControl control_;
91 IPConfigRefPtr ipconfig_;
92 StrictMock<MockResolver> resolver_;
93 StrictMock<MockRoutingTable> routing_table_;
94 StrictMock<MockRTNLHandler> rtnl_handler_;
95};
96
97TEST_F(ConnectionTest, InitState) {
98 EXPECT_EQ(kTestDeviceInterfaceIndex0, connection_->interface_index_);
99 EXPECT_EQ(kTestDeviceName0, connection_->interface_name_);
100 EXPECT_FALSE(connection_->is_default());
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800101 EXPECT_FALSE(connection_->routing_request_count_);
Paul Stewartdd60e452011-08-08 11:38:36 -0700102}
103
104TEST_F(ConnectionTest, AddConfig) {
105 EXPECT_CALL(rtnl_handler_,
Paul Stewart9a908082011-08-31 12:18:48 -0700106 AddInterfaceAddress(kTestDeviceInterfaceIndex0, _, _));
Paul Stewart7cfca042011-12-08 14:18:17 -0800107 EXPECT_CALL(routing_table_,
108 SetDefaultRoute(kTestDeviceInterfaceIndex0,
109 ipconfig_,
110 Connection::kNonDefaultMetricBase +
111 kTestDeviceInterfaceIndex0));
Paul Stewartdd60e452011-08-08 11:38:36 -0700112 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 Stewartc681fa02012-03-02 19:40:04 -0800120 scoped_refptr<MockDevice> device(new StrictMock<MockDevice>(
121 &control_,
122 reinterpret_cast<EventDispatcher *>(NULL),
123 reinterpret_cast<Metrics *>(NULL),
124 reinterpret_cast<Manager *>(NULL),
125 kTestDeviceName0,
126 string(),
127 kTestDeviceInterfaceIndex0));
128 EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0))
129 .WillOnce(Return(device));
130 EXPECT_CALL(*device.get(), RequestPortalDetection())
131 .WillOnce(Return(true));
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800132 connection_->SetIsDefault(true);
Paul Stewartdd60e452011-08-08 11:38:36 -0700133 EXPECT_TRUE(connection_->is_default());
134
Paul Stewart7cfca042011-12-08 14:18:17 -0800135 EXPECT_CALL(routing_table_,
136 SetDefaultMetric(kTestDeviceInterfaceIndex0,
137 Connection::kNonDefaultMetricBase +
138 kTestDeviceInterfaceIndex0));
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800139 connection_->SetIsDefault(false);
Paul Stewartdd60e452011-08-08 11:38:36 -0700140 EXPECT_FALSE(connection_->is_default());
141}
142
143TEST_F(ConnectionTest, AddConfigReverse) {
144 EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0,
145 Connection::kDefaultMetric));
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800146 vector<string> empty_list;
Paul Stewartdd60e452011-08-08 11:38:36 -0700147 EXPECT_CALL(resolver_, SetDNSFromLists(empty_list, empty_list));
Paul Stewartc681fa02012-03-02 19:40:04 -0800148 scoped_refptr<MockDevice> device(new StrictMock<MockDevice>(
149 &control_,
150 reinterpret_cast<EventDispatcher *>(NULL),
151 reinterpret_cast<Metrics *>(NULL),
152 reinterpret_cast<Manager *>(NULL),
153 kTestDeviceName0,
154 string(),
155 kTestDeviceInterfaceIndex0));
156 EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0))
157 .WillOnce(Return(device));
158 EXPECT_CALL(*device.get(), RequestPortalDetection())
159 .WillOnce(Return(true));
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800160 connection_->SetIsDefault(true);
Paul Stewartdd60e452011-08-08 11:38:36 -0700161
162 EXPECT_CALL(rtnl_handler_,
Paul Stewart9a908082011-08-31 12:18:48 -0700163 AddInterfaceAddress(kTestDeviceInterfaceIndex0, _, _));
Paul Stewartdd60e452011-08-08 11:38:36 -0700164 EXPECT_CALL(routing_table_, SetDefaultRoute(kTestDeviceInterfaceIndex0,
165 ipconfig_,
166 Connection::kDefaultMetric));
167 EXPECT_CALL(resolver_, SetDNSFromIPConfig(ipconfig_));
168
169 connection_->UpdateFromIPConfig(ipconfig_);
170}
171
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800172TEST_F(ConnectionTest, RouteRequest) {
Paul Stewartf748a362012-03-07 12:01:20 -0800173 ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex0,
174 kTestDeviceName0,
175 device_info_.get()));
176 ReplaceSingletons(connection);
177 scoped_refptr<MockDevice> device(new StrictMock<MockDevice>(
178 &control_,
179 reinterpret_cast<EventDispatcher *>(NULL),
180 reinterpret_cast<Metrics *>(NULL),
181 reinterpret_cast<Manager *>(NULL),
182 kTestDeviceName0,
183 string(),
184 kTestDeviceInterfaceIndex0));
185 EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0))
186 .WillRepeatedly(Return(device));
187 EXPECT_CALL(*device.get(), DisableReversePathFilter()).Times(1);
188 connection->RequestRouting();
189 connection->RequestRouting();
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800190
Paul Stewartf748a362012-03-07 12:01:20 -0800191 // The first release should only decrement the reference counter.
192 connection->ReleaseRouting();
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800193
Paul Stewartf748a362012-03-07 12:01:20 -0800194 // Another release will re-enable reverse-path filter.
195 EXPECT_CALL(*device.get(), EnableReversePathFilter());
196 EXPECT_CALL(routing_table_, FlushCache());
197 connection->ReleaseRouting();
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800198
Paul Stewartf748a362012-03-07 12:01:20 -0800199 // The destructor will remove the routes and addresses.
200 EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex0));
201 EXPECT_CALL(*device_info_.get(),
202 FlushAddresses(kTestDeviceInterfaceIndex0));
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800203}
204
Paul Stewartdd60e452011-08-08 11:38:36 -0700205TEST_F(ConnectionTest, Destructor) {
Thieu Lefb46caf2012-03-08 11:57:15 -0800206 EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex1));
Paul Stewart9a908082011-08-31 12:18:48 -0700207 EXPECT_CALL(*device_info_, FlushAddresses(kTestDeviceInterfaceIndex1));
Paul Stewartdd60e452011-08-08 11:38:36 -0700208 {
209 ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex1,
Paul Stewart9a908082011-08-31 12:18:48 -0700210 kTestDeviceName1,
211 device_info_.get()));
Paul Stewartdd60e452011-08-08 11:38:36 -0700212 connection->resolver_ = &resolver_;
213 connection->routing_table_ = &routing_table_;
214 connection->rtnl_handler_ = &rtnl_handler_;
215 }
216}
217
Paul Stewartf748a362012-03-07 12:01:20 -0800218MATCHER_P2(IsIPAddress, address, prefix, "") {
219 IPAddress match_address(address);
220 match_address.set_prefix(prefix);
221 return match_address.Equals(arg);
222}
223
224TEST_F(ConnectionTest, RequestHostRoute) {
225 ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex0,
226 kTestDeviceName0,
227 device_info_.get()));
228 ReplaceSingletons(connection);
229 IPAddress address(IPAddress::kFamilyIPv4);
230 ASSERT_TRUE(address.SetAddressFromString(kIPAddress0));
231 size_t prefix_len = address.GetLength() * 8;
232 EXPECT_CALL(routing_table_, RequestRouteToHost(
233 IsIPAddress(address, prefix_len), kTestDeviceInterfaceIndex0))
234 .WillOnce(Return(true));
235 EXPECT_TRUE(connection->RequestHostRoute(address));
236
237 // The destructor will remove the routes and addresses.
238 EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex0));
239 EXPECT_CALL(*device_info_.get(),
240 FlushAddresses(kTestDeviceInterfaceIndex0));
241}
242
Paul Stewartdd60e452011-08-08 11:38:36 -0700243} // namespace shill