blob: 491cb1f4b254362c153f55f9f8b16823bbf7c9fa [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 Stewartc1dec4d2011-12-08 15:25:28 -0800120 connection_->SetIsDefault(true);
Paul Stewartdd60e452011-08-08 11:38:36 -0700121 EXPECT_TRUE(connection_->is_default());
122
Paul Stewart7cfca042011-12-08 14:18:17 -0800123 EXPECT_CALL(routing_table_,
124 SetDefaultMetric(kTestDeviceInterfaceIndex0,
125 Connection::kNonDefaultMetricBase +
126 kTestDeviceInterfaceIndex0));
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800127 connection_->SetIsDefault(false);
Paul Stewartdd60e452011-08-08 11:38:36 -0700128 EXPECT_FALSE(connection_->is_default());
129}
130
131TEST_F(ConnectionTest, AddConfigReverse) {
132 EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0,
133 Connection::kDefaultMetric));
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800134 vector<string> empty_list;
Paul Stewartdd60e452011-08-08 11:38:36 -0700135 EXPECT_CALL(resolver_, SetDNSFromLists(empty_list, empty_list));
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800136 connection_->SetIsDefault(true);
Paul Stewartdd60e452011-08-08 11:38:36 -0700137
138 EXPECT_CALL(rtnl_handler_,
Paul Stewart9a908082011-08-31 12:18:48 -0700139 AddInterfaceAddress(kTestDeviceInterfaceIndex0, _, _));
Paul Stewartdd60e452011-08-08 11:38:36 -0700140 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 Stewartc8f4bef2011-12-13 09:45:51 -0800148TEST_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 Le3426c8f2012-01-11 17:35:11 -0800157 reinterpret_cast<Metrics *>(NULL),
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800158 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 Stewartdd60e452011-08-08 11:38:36 -0700183TEST_F(ConnectionTest, Destructor) {
184 EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex1));
Paul Stewart9a908082011-08-31 12:18:48 -0700185 EXPECT_CALL(*device_info_, FlushAddresses(kTestDeviceInterfaceIndex1));
Paul Stewartdd60e452011-08-08 11:38:36 -0700186 {
187 ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex1,
Paul Stewart9a908082011-08-31 12:18:48 -0700188 kTestDeviceName1,
189 device_info_.get()));
Paul Stewartdd60e452011-08-08 11:38:36 -0700190 connection->resolver_ = &resolver_;
191 connection->routing_table_ = &routing_table_;
192 connection->rtnl_handler_ = &rtnl_handler_;
193 }
194}
195
196} // namespace shill