blob: 16e0f78fa5bc291d8b471ede67e0328fe0e25cbc [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) {
173 {
174 ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex0,
175 kTestDeviceName0,
176 device_info_.get()));
177 ReplaceSingletons(connection);
178 scoped_refptr<MockDevice> device(new StrictMock<MockDevice>(
179 &control_,
180 reinterpret_cast<EventDispatcher *>(NULL),
Thieu Le3426c8f2012-01-11 17:35:11 -0800181 reinterpret_cast<Metrics *>(NULL),
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800182 reinterpret_cast<Manager *>(NULL),
183 kTestDeviceName0,
184 string(),
185 kTestDeviceInterfaceIndex0));
186 EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0))
187 .WillRepeatedly(Return(device));
188 EXPECT_CALL(*device.get(), DisableReversePathFilter()).Times(1);
189 connection->RequestRouting();
190 connection->RequestRouting();
191
192 // The first release should only decrement the reference counter.
193 connection->ReleaseRouting();
194
195 // Another release will re-enable reverse-path filter.
196 EXPECT_CALL(*device.get(), EnableReversePathFilter());
197 EXPECT_CALL(routing_table_, FlushCache());
198 connection->ReleaseRouting();
199
200 // The destructor will remove the routes and addresses.
Thieu Lecaef8932012-02-28 16:06:59 -0800201 EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex0, true));
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800202 EXPECT_CALL(*device_info_.get(),
203 FlushAddresses(kTestDeviceInterfaceIndex0));
204 }
205}
206
Paul Stewartdd60e452011-08-08 11:38:36 -0700207TEST_F(ConnectionTest, Destructor) {
Thieu Lecaef8932012-02-28 16:06:59 -0800208 EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex1, true));
Paul Stewart9a908082011-08-31 12:18:48 -0700209 EXPECT_CALL(*device_info_, FlushAddresses(kTestDeviceInterfaceIndex1));
Paul Stewartdd60e452011-08-08 11:38:36 -0700210 {
211 ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex1,
Paul Stewart9a908082011-08-31 12:18:48 -0700212 kTestDeviceName1,
213 device_info_.get()));
Paul Stewartdd60e452011-08-08 11:38:36 -0700214 connection->resolver_ = &resolver_;
215 connection->routing_table_ = &routing_table_;
216 connection->rtnl_handler_ = &rtnl_handler_;
217 }
218}
219
220} // namespace shill