blob: d43bd696b217357ca4bc35bb151009bd4b17d313 [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 Stewart3f68bb12012-03-15 13:33:10 -0700112 EXPECT_CALL(routing_table_,
113 ConfigureRoutes(kTestDeviceInterfaceIndex0,
114 ipconfig_,
115 Connection::kDefaultMetric));
Paul Stewartdd60e452011-08-08 11:38:36 -0700116 connection_->UpdateFromIPConfig(ipconfig_);
117
118 EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0,
119 Connection::kDefaultMetric));
120 EXPECT_CALL(resolver_, SetDNSFromLists(
121 ipconfig_->properties().dns_servers,
122 ipconfig_->properties().domain_search));
123
Paul Stewartc681fa02012-03-02 19:40:04 -0800124 scoped_refptr<MockDevice> device(new StrictMock<MockDevice>(
125 &control_,
126 reinterpret_cast<EventDispatcher *>(NULL),
127 reinterpret_cast<Metrics *>(NULL),
128 reinterpret_cast<Manager *>(NULL),
129 kTestDeviceName0,
130 string(),
131 kTestDeviceInterfaceIndex0));
132 EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0))
133 .WillOnce(Return(device));
134 EXPECT_CALL(*device.get(), RequestPortalDetection())
135 .WillOnce(Return(true));
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800136 connection_->SetIsDefault(true);
Paul Stewartdd60e452011-08-08 11:38:36 -0700137 EXPECT_TRUE(connection_->is_default());
138
Paul Stewart7cfca042011-12-08 14:18:17 -0800139 EXPECT_CALL(routing_table_,
140 SetDefaultMetric(kTestDeviceInterfaceIndex0,
141 Connection::kNonDefaultMetricBase +
142 kTestDeviceInterfaceIndex0));
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800143 connection_->SetIsDefault(false);
Paul Stewartdd60e452011-08-08 11:38:36 -0700144 EXPECT_FALSE(connection_->is_default());
145}
146
147TEST_F(ConnectionTest, AddConfigReverse) {
148 EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0,
149 Connection::kDefaultMetric));
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800150 vector<string> empty_list;
Paul Stewartdd60e452011-08-08 11:38:36 -0700151 EXPECT_CALL(resolver_, SetDNSFromLists(empty_list, empty_list));
Paul Stewartc681fa02012-03-02 19:40:04 -0800152 scoped_refptr<MockDevice> device(new StrictMock<MockDevice>(
153 &control_,
154 reinterpret_cast<EventDispatcher *>(NULL),
155 reinterpret_cast<Metrics *>(NULL),
156 reinterpret_cast<Manager *>(NULL),
157 kTestDeviceName0,
158 string(),
159 kTestDeviceInterfaceIndex0));
160 EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0))
161 .WillOnce(Return(device));
162 EXPECT_CALL(*device.get(), RequestPortalDetection())
163 .WillOnce(Return(true));
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800164 connection_->SetIsDefault(true);
Paul Stewartdd60e452011-08-08 11:38:36 -0700165
166 EXPECT_CALL(rtnl_handler_,
Paul Stewart9a908082011-08-31 12:18:48 -0700167 AddInterfaceAddress(kTestDeviceInterfaceIndex0, _, _));
Paul Stewartdd60e452011-08-08 11:38:36 -0700168 EXPECT_CALL(routing_table_, SetDefaultRoute(kTestDeviceInterfaceIndex0,
169 ipconfig_,
170 Connection::kDefaultMetric));
Paul Stewart3f68bb12012-03-15 13:33:10 -0700171 EXPECT_CALL(routing_table_,
172 ConfigureRoutes(kTestDeviceInterfaceIndex0,
173 ipconfig_,
174 Connection::kDefaultMetric));
Paul Stewartdd60e452011-08-08 11:38:36 -0700175 EXPECT_CALL(resolver_, SetDNSFromIPConfig(ipconfig_));
176
177 connection_->UpdateFromIPConfig(ipconfig_);
178}
179
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800180TEST_F(ConnectionTest, RouteRequest) {
Paul Stewartf748a362012-03-07 12:01:20 -0800181 ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex0,
182 kTestDeviceName0,
183 device_info_.get()));
184 ReplaceSingletons(connection);
185 scoped_refptr<MockDevice> device(new StrictMock<MockDevice>(
186 &control_,
187 reinterpret_cast<EventDispatcher *>(NULL),
188 reinterpret_cast<Metrics *>(NULL),
189 reinterpret_cast<Manager *>(NULL),
190 kTestDeviceName0,
191 string(),
192 kTestDeviceInterfaceIndex0));
193 EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0))
194 .WillRepeatedly(Return(device));
195 EXPECT_CALL(*device.get(), DisableReversePathFilter()).Times(1);
196 connection->RequestRouting();
197 connection->RequestRouting();
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800198
Paul Stewartf748a362012-03-07 12:01:20 -0800199 // The first release should only decrement the reference counter.
200 connection->ReleaseRouting();
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800201
Paul Stewartf748a362012-03-07 12:01:20 -0800202 // Another release will re-enable reverse-path filter.
203 EXPECT_CALL(*device.get(), EnableReversePathFilter());
204 EXPECT_CALL(routing_table_, FlushCache());
205 connection->ReleaseRouting();
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800206
Paul Stewartf748a362012-03-07 12:01:20 -0800207 // The destructor will remove the routes and addresses.
208 EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex0));
209 EXPECT_CALL(*device_info_.get(),
210 FlushAddresses(kTestDeviceInterfaceIndex0));
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800211}
212
Paul Stewartdd60e452011-08-08 11:38:36 -0700213TEST_F(ConnectionTest, Destructor) {
Thieu Lefb46caf2012-03-08 11:57:15 -0800214 EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex1));
Paul Stewart9a908082011-08-31 12:18:48 -0700215 EXPECT_CALL(*device_info_, FlushAddresses(kTestDeviceInterfaceIndex1));
Paul Stewartdd60e452011-08-08 11:38:36 -0700216 {
217 ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex1,
Paul Stewart9a908082011-08-31 12:18:48 -0700218 kTestDeviceName1,
219 device_info_.get()));
Paul Stewartdd60e452011-08-08 11:38:36 -0700220 connection->resolver_ = &resolver_;
221 connection->routing_table_ = &routing_table_;
222 connection->rtnl_handler_ = &rtnl_handler_;
223 }
224}
225
Paul Stewartf748a362012-03-07 12:01:20 -0800226MATCHER_P2(IsIPAddress, address, prefix, "") {
227 IPAddress match_address(address);
228 match_address.set_prefix(prefix);
229 return match_address.Equals(arg);
230}
231
232TEST_F(ConnectionTest, RequestHostRoute) {
233 ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex0,
234 kTestDeviceName0,
235 device_info_.get()));
236 ReplaceSingletons(connection);
237 IPAddress address(IPAddress::kFamilyIPv4);
238 ASSERT_TRUE(address.SetAddressFromString(kIPAddress0));
239 size_t prefix_len = address.GetLength() * 8;
240 EXPECT_CALL(routing_table_, RequestRouteToHost(
241 IsIPAddress(address, prefix_len), kTestDeviceInterfaceIndex0))
242 .WillOnce(Return(true));
243 EXPECT_TRUE(connection->RequestHostRoute(address));
244
245 // The destructor will remove the routes and addresses.
246 EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex0));
247 EXPECT_CALL(*device_info_.get(),
248 FlushAddresses(kTestDeviceInterfaceIndex0));
249}
250
Paul Stewartdd60e452011-08-08 11:38:36 -0700251} // namespace shill