blob: 283b069d80918fcc61c2d3750093e738cabd6900 [file] [log] [blame]
Paul Stewartdd60e452011-08-08 11:38:36 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// 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),
55 static_cast<Manager*>(NULL))),
56 connection_(new Connection(
57 kTestDeviceInterfaceIndex0,
58 kTestDeviceName0,
59 device_info_.get())),
Paul Stewartdd60e452011-08-08 11:38:36 -070060 ipconfig_(new IPConfig(&control_, kTestDeviceName0)) {}
61
62 virtual void SetUp() {
Paul Stewartc8f4bef2011-12-13 09:45:51 -080063 ReplaceSingletons(connection_);
Paul Stewartdd60e452011-08-08 11:38:36 -070064 IPConfig::Properties properties;
65 properties.address = kIPAddress0;
66 properties.gateway = kGatewayAddress0;
Paul Stewart9a908082011-08-31 12:18:48 -070067 properties.broadcast_address = kBroadcastAddress0;
Paul Stewartdd60e452011-08-08 11:38:36 -070068 properties.dns_servers.push_back(kNameServer0);
69 properties.dns_servers.push_back(kNameServer1);
70 properties.domain_search.push_back(kSearchDomain0);
71 properties.domain_search.push_back(kSearchDomain1);
Paul Stewart7355ce12011-09-02 10:47:01 -070072 properties.address_family = IPAddress::kFamilyIPv4;
Paul Stewartdd60e452011-08-08 11:38:36 -070073 ipconfig_->UpdateProperties(properties, true);
74 }
75
Paul Stewart9a908082011-08-31 12:18:48 -070076 virtual void TearDown() {
77 EXPECT_CALL(*device_info_, FlushAddresses(kTestDeviceInterfaceIndex0));
78 }
79
Paul Stewartc8f4bef2011-12-13 09:45:51 -080080 void ReplaceSingletons(ConnectionRefPtr connection) {
81 connection->resolver_ = &resolver_;
82 connection->routing_table_ = &routing_table_;
83 connection->rtnl_handler_ = &rtnl_handler_;
84 }
85
Paul Stewartdd60e452011-08-08 11:38:36 -070086 protected:
Paul Stewart9a908082011-08-31 12:18:48 -070087 scoped_ptr<StrictMock<MockDeviceInfo> > device_info_;
Paul Stewartdd60e452011-08-08 11:38:36 -070088 ConnectionRefPtr connection_;
89 MockControl control_;
90 IPConfigRefPtr ipconfig_;
91 StrictMock<MockResolver> resolver_;
92 StrictMock<MockRoutingTable> routing_table_;
93 StrictMock<MockRTNLHandler> rtnl_handler_;
94};
95
96TEST_F(ConnectionTest, InitState) {
97 EXPECT_EQ(kTestDeviceInterfaceIndex0, connection_->interface_index_);
98 EXPECT_EQ(kTestDeviceName0, connection_->interface_name_);
99 EXPECT_FALSE(connection_->is_default());
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800100 EXPECT_FALSE(connection_->routing_request_count_);
Paul Stewartdd60e452011-08-08 11:38:36 -0700101}
102
103TEST_F(ConnectionTest, AddConfig) {
104 EXPECT_CALL(rtnl_handler_,
Paul Stewart9a908082011-08-31 12:18:48 -0700105 AddInterfaceAddress(kTestDeviceInterfaceIndex0, _, _));
Paul Stewart7cfca042011-12-08 14:18:17 -0800106 EXPECT_CALL(routing_table_,
107 SetDefaultRoute(kTestDeviceInterfaceIndex0,
108 ipconfig_,
109 Connection::kNonDefaultMetricBase +
110 kTestDeviceInterfaceIndex0));
Paul Stewartdd60e452011-08-08 11:38:36 -0700111 connection_->UpdateFromIPConfig(ipconfig_);
112
113 EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0,
114 Connection::kDefaultMetric));
115 EXPECT_CALL(resolver_, SetDNSFromLists(
116 ipconfig_->properties().dns_servers,
117 ipconfig_->properties().domain_search));
118
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800119 connection_->SetIsDefault(true);
Paul Stewartdd60e452011-08-08 11:38:36 -0700120 EXPECT_TRUE(connection_->is_default());
121
Paul Stewart7cfca042011-12-08 14:18:17 -0800122 EXPECT_CALL(routing_table_,
123 SetDefaultMetric(kTestDeviceInterfaceIndex0,
124 Connection::kNonDefaultMetricBase +
125 kTestDeviceInterfaceIndex0));
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800126 connection_->SetIsDefault(false);
Paul Stewartdd60e452011-08-08 11:38:36 -0700127 EXPECT_FALSE(connection_->is_default());
128}
129
130TEST_F(ConnectionTest, AddConfigReverse) {
131 EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0,
132 Connection::kDefaultMetric));
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800133 vector<string> empty_list;
Paul Stewartdd60e452011-08-08 11:38:36 -0700134 EXPECT_CALL(resolver_, SetDNSFromLists(empty_list, empty_list));
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800135 connection_->SetIsDefault(true);
Paul Stewartdd60e452011-08-08 11:38:36 -0700136
137 EXPECT_CALL(rtnl_handler_,
Paul Stewart9a908082011-08-31 12:18:48 -0700138 AddInterfaceAddress(kTestDeviceInterfaceIndex0, _, _));
Paul Stewartdd60e452011-08-08 11:38:36 -0700139 EXPECT_CALL(routing_table_, SetDefaultRoute(kTestDeviceInterfaceIndex0,
140 ipconfig_,
141 Connection::kDefaultMetric));
142 EXPECT_CALL(resolver_, SetDNSFromIPConfig(ipconfig_));
143
144 connection_->UpdateFromIPConfig(ipconfig_);
145}
146
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800147TEST_F(ConnectionTest, RouteRequest) {
148 {
149 ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex0,
150 kTestDeviceName0,
151 device_info_.get()));
152 ReplaceSingletons(connection);
153 scoped_refptr<MockDevice> device(new StrictMock<MockDevice>(
154 &control_,
155 reinterpret_cast<EventDispatcher *>(NULL),
156 reinterpret_cast<Manager *>(NULL),
157 kTestDeviceName0,
158 string(),
159 kTestDeviceInterfaceIndex0));
160 EXPECT_CALL(*device_info_, GetDevice(kTestDeviceInterfaceIndex0))
161 .WillRepeatedly(Return(device));
162 EXPECT_CALL(*device.get(), DisableReversePathFilter()).Times(1);
163 connection->RequestRouting();
164 connection->RequestRouting();
165
166 // The first release should only decrement the reference counter.
167 connection->ReleaseRouting();
168
169 // Another release will re-enable reverse-path filter.
170 EXPECT_CALL(*device.get(), EnableReversePathFilter());
171 EXPECT_CALL(routing_table_, FlushCache());
172 connection->ReleaseRouting();
173
174 // The destructor will remove the routes and addresses.
175 EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex0));
176 EXPECT_CALL(*device_info_.get(),
177 FlushAddresses(kTestDeviceInterfaceIndex0));
178 }
179}
180
Paul Stewartdd60e452011-08-08 11:38:36 -0700181TEST_F(ConnectionTest, Destructor) {
182 EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex1));
Paul Stewart9a908082011-08-31 12:18:48 -0700183 EXPECT_CALL(*device_info_, FlushAddresses(kTestDeviceInterfaceIndex1));
Paul Stewartdd60e452011-08-08 11:38:36 -0700184 {
185 ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex1,
Paul Stewart9a908082011-08-31 12:18:48 -0700186 kTestDeviceName1,
187 device_info_.get()));
Paul Stewartdd60e452011-08-08 11:38:36 -0700188 connection->resolver_ = &resolver_;
189 connection->routing_table_ = &routing_table_;
190 connection->rtnl_handler_ = &rtnl_handler_;
191 }
192}
193
194} // namespace shill