blob: e5b0cbaf4fd2121eb14e704d4eea2fef9c316bac [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);
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 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 Stewart7cfca042011-12-08 14:18:17 -080099 EXPECT_CALL(routing_table_,
100 SetDefaultRoute(kTestDeviceInterfaceIndex0,
101 ipconfig_,
102 Connection::kNonDefaultMetricBase +
103 kTestDeviceInterfaceIndex0));
Paul Stewartdd60e452011-08-08 11:38:36 -0700104 connection_->UpdateFromIPConfig(ipconfig_);
105
106 EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0,
107 Connection::kDefaultMetric));
108 EXPECT_CALL(resolver_, SetDNSFromLists(
109 ipconfig_->properties().dns_servers,
110 ipconfig_->properties().domain_search));
111
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800112 connection_->SetIsDefault(true);
Paul Stewartdd60e452011-08-08 11:38:36 -0700113 EXPECT_TRUE(connection_->is_default());
114
Paul Stewart7cfca042011-12-08 14:18:17 -0800115 EXPECT_CALL(routing_table_,
116 SetDefaultMetric(kTestDeviceInterfaceIndex0,
117 Connection::kNonDefaultMetricBase +
118 kTestDeviceInterfaceIndex0));
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800119 connection_->SetIsDefault(false);
Paul Stewartdd60e452011-08-08 11:38:36 -0700120 EXPECT_FALSE(connection_->is_default());
121}
122
123TEST_F(ConnectionTest, AddConfigReverse) {
124 EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0,
125 Connection::kDefaultMetric));
Paul Stewart9a908082011-08-31 12:18:48 -0700126 vector<std::string> empty_list;
Paul Stewartdd60e452011-08-08 11:38:36 -0700127 EXPECT_CALL(resolver_, SetDNSFromLists(empty_list, empty_list));
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800128 connection_->SetIsDefault(true);
Paul Stewartdd60e452011-08-08 11:38:36 -0700129
130 EXPECT_CALL(rtnl_handler_,
Paul Stewart9a908082011-08-31 12:18:48 -0700131 AddInterfaceAddress(kTestDeviceInterfaceIndex0, _, _));
Paul Stewartdd60e452011-08-08 11:38:36 -0700132 EXPECT_CALL(routing_table_, SetDefaultRoute(kTestDeviceInterfaceIndex0,
133 ipconfig_,
134 Connection::kDefaultMetric));
135 EXPECT_CALL(resolver_, SetDNSFromIPConfig(ipconfig_));
136
137 connection_->UpdateFromIPConfig(ipconfig_);
138}
139
140TEST_F(ConnectionTest, Destructor) {
141 EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex1));
Paul Stewart9a908082011-08-31 12:18:48 -0700142 EXPECT_CALL(*device_info_, FlushAddresses(kTestDeviceInterfaceIndex1));
Paul Stewartdd60e452011-08-08 11:38:36 -0700143 {
144 ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex1,
Paul Stewart9a908082011-08-31 12:18:48 -0700145 kTestDeviceName1,
146 device_info_.get()));
Paul Stewartdd60e452011-08-08 11:38:36 -0700147 connection->resolver_ = &resolver_;
148 connection->routing_table_ = &routing_table_;
149 connection->rtnl_handler_ = &rtnl_handler_;
150 }
151}
152
153} // namespace shill