blob: 3111db7e32cd1f67e6f86767cfe76472d5350565 [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
8#include <vector>
9
10#include <base/memory/scoped_ptr.h>
Paul Stewartdd60e452011-08-08 11:38:36 -070011#include <gtest/gtest.h>
12#include <gmock/gmock.h>
13
14#include "shill/connection.h"
Paul Stewartdd60e452011-08-08 11:38:36 -070015#include "shill/ipconfig.h"
16#include "shill/mock_control.h"
Paul Stewart9a908082011-08-31 12:18:48 -070017#include "shill/mock_device_info.h"
Paul Stewartdd60e452011-08-08 11:38:36 -070018#include "shill/mock_resolver.h"
19#include "shill/mock_routing_table.h"
20#include "shill/mock_rtnl_handler.h"
21#include "shill/routing_table_entry.h"
22
Paul Stewart9a908082011-08-31 12:18:48 -070023using std::vector;
Paul Stewartdd60e452011-08-08 11:38:36 -070024using testing::_;
25using testing::NiceMock;
26using testing::Return;
27using testing::StrictMock;
28using testing::Test;
29
30namespace shill {
31
32namespace {
33const char kTestDeviceName0[] = "netdev0";
34const int kTestDeviceInterfaceIndex0 = 123;
35const char kTestDeviceName1[] = "netdev1";
36const int kTestDeviceInterfaceIndex1 = 321;
37const char kIPAddress0[] = "192.168.1.1";
38const char kGatewayAddress0[] = "192.168.1.254";
Paul Stewart9a908082011-08-31 12:18:48 -070039const char kBroadcastAddress0[] = "192.168.1.255";
Paul Stewartdd60e452011-08-08 11:38:36 -070040const char kNameServer0[] = "8.8.8.8";
41const char kNameServer1[] = "8.8.9.9";
42const char kSearchDomain0[] = "chromium.org";
43const char kSearchDomain1[] = "google.com";
44} // namespace {}
45
46class ConnectionTest : public Test {
47 public:
48 ConnectionTest()
Paul Stewart9a908082011-08-31 12:18:48 -070049 : device_info_(new StrictMock<MockDeviceInfo>(
50 &control_,
51 static_cast<EventDispatcher*>(NULL),
52 static_cast<Manager*>(NULL))),
53 connection_(new Connection(
54 kTestDeviceInterfaceIndex0,
55 kTestDeviceName0,
56 device_info_.get())),
Paul Stewartdd60e452011-08-08 11:38:36 -070057 ipconfig_(new IPConfig(&control_, kTestDeviceName0)) {}
58
59 virtual void SetUp() {
60 connection_->resolver_ = &resolver_;
61 connection_->routing_table_ = &routing_table_;
62 connection_->rtnl_handler_ = &rtnl_handler_;
63
64 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);
72 properties.address_family = IPAddress::kAddressFamilyIPv4;
73 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 Stewartdd60e452011-08-08 11:38:36 -070080 protected:
Paul Stewart9a908082011-08-31 12:18:48 -070081 scoped_ptr<StrictMock<MockDeviceInfo> > device_info_;
Paul Stewartdd60e452011-08-08 11:38:36 -070082 ConnectionRefPtr connection_;
83 MockControl control_;
84 IPConfigRefPtr ipconfig_;
85 StrictMock<MockResolver> resolver_;
86 StrictMock<MockRoutingTable> routing_table_;
87 StrictMock<MockRTNLHandler> rtnl_handler_;
88};
89
90TEST_F(ConnectionTest, InitState) {
91 EXPECT_EQ(kTestDeviceInterfaceIndex0, connection_->interface_index_);
92 EXPECT_EQ(kTestDeviceName0, connection_->interface_name_);
93 EXPECT_FALSE(connection_->is_default());
94}
95
96TEST_F(ConnectionTest, AddConfig) {
97 EXPECT_CALL(rtnl_handler_,
Paul Stewart9a908082011-08-31 12:18:48 -070098 AddInterfaceAddress(kTestDeviceInterfaceIndex0, _, _));
Paul Stewartdd60e452011-08-08 11:38:36 -070099 EXPECT_CALL(routing_table_, SetDefaultRoute(kTestDeviceInterfaceIndex0,
100 ipconfig_,
101 Connection::kNonDefaultMetric));
102 connection_->UpdateFromIPConfig(ipconfig_);
103
104 EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0,
105 Connection::kDefaultMetric));
106 EXPECT_CALL(resolver_, SetDNSFromLists(
107 ipconfig_->properties().dns_servers,
108 ipconfig_->properties().domain_search));
109
110 connection_->SetDefault(true);
111 EXPECT_TRUE(connection_->is_default());
112
113 EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0,
114 Connection::kNonDefaultMetric));
115 connection_->SetDefault(false);
116 EXPECT_FALSE(connection_->is_default());
117}
118
119TEST_F(ConnectionTest, AddConfigReverse) {
120 EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0,
121 Connection::kDefaultMetric));
Paul Stewart9a908082011-08-31 12:18:48 -0700122 vector<std::string> empty_list;
Paul Stewartdd60e452011-08-08 11:38:36 -0700123 EXPECT_CALL(resolver_, SetDNSFromLists(empty_list, empty_list));
124 connection_->SetDefault(true);
125
126 EXPECT_CALL(rtnl_handler_,
Paul Stewart9a908082011-08-31 12:18:48 -0700127 AddInterfaceAddress(kTestDeviceInterfaceIndex0, _, _));
Paul Stewartdd60e452011-08-08 11:38:36 -0700128 EXPECT_CALL(routing_table_, SetDefaultRoute(kTestDeviceInterfaceIndex0,
129 ipconfig_,
130 Connection::kDefaultMetric));
131 EXPECT_CALL(resolver_, SetDNSFromIPConfig(ipconfig_));
132
133 connection_->UpdateFromIPConfig(ipconfig_);
134}
135
136TEST_F(ConnectionTest, Destructor) {
137 EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex1));
Paul Stewart9a908082011-08-31 12:18:48 -0700138 EXPECT_CALL(*device_info_, FlushAddresses(kTestDeviceInterfaceIndex1));
Paul Stewartdd60e452011-08-08 11:38:36 -0700139 {
140 ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex1,
Paul Stewart9a908082011-08-31 12:18:48 -0700141 kTestDeviceName1,
142 device_info_.get()));
Paul Stewartdd60e452011-08-08 11:38:36 -0700143 connection->resolver_ = &resolver_;
144 connection->routing_table_ = &routing_table_;
145 connection->rtnl_handler_ = &rtnl_handler_;
146 }
147}
148
149} // namespace shill