Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 1 | // 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 Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 5 | #include <arpa/inet.h> |
| 6 | #include <linux/rtnetlink.h> |
| 7 | |
| 8 | #include <vector> |
| 9 | |
| 10 | #include <base/memory/scoped_ptr.h> |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 11 | #include <gtest/gtest.h> |
| 12 | #include <gmock/gmock.h> |
| 13 | |
| 14 | #include "shill/connection.h" |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 15 | #include "shill/ipconfig.h" |
| 16 | #include "shill/mock_control.h" |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 17 | #include "shill/mock_device_info.h" |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 18 | #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 Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 23 | using std::vector; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 24 | using testing::_; |
| 25 | using testing::NiceMock; |
| 26 | using testing::Return; |
| 27 | using testing::StrictMock; |
| 28 | using testing::Test; |
| 29 | |
| 30 | namespace shill { |
| 31 | |
| 32 | namespace { |
| 33 | const char kTestDeviceName0[] = "netdev0"; |
| 34 | const int kTestDeviceInterfaceIndex0 = 123; |
| 35 | const char kTestDeviceName1[] = "netdev1"; |
| 36 | const int kTestDeviceInterfaceIndex1 = 321; |
| 37 | const char kIPAddress0[] = "192.168.1.1"; |
| 38 | const char kGatewayAddress0[] = "192.168.1.254"; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 39 | const char kBroadcastAddress0[] = "192.168.1.255"; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 40 | const char kNameServer0[] = "8.8.8.8"; |
| 41 | const char kNameServer1[] = "8.8.9.9"; |
| 42 | const char kSearchDomain0[] = "chromium.org"; |
| 43 | const char kSearchDomain1[] = "google.com"; |
| 44 | } // namespace {} |
| 45 | |
| 46 | class ConnectionTest : public Test { |
| 47 | public: |
| 48 | ConnectionTest() |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 49 | : 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 Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 57 | 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 Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 67 | properties.broadcast_address = kBroadcastAddress0; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 68 | 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 Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 72 | properties.address_family = IPAddress::kFamilyIPv4; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 73 | ipconfig_->UpdateProperties(properties, true); |
| 74 | } |
| 75 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 76 | virtual void TearDown() { |
| 77 | EXPECT_CALL(*device_info_, FlushAddresses(kTestDeviceInterfaceIndex0)); |
| 78 | } |
| 79 | |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 80 | protected: |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 81 | scoped_ptr<StrictMock<MockDeviceInfo> > device_info_; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 82 | ConnectionRefPtr connection_; |
| 83 | MockControl control_; |
| 84 | IPConfigRefPtr ipconfig_; |
| 85 | StrictMock<MockResolver> resolver_; |
| 86 | StrictMock<MockRoutingTable> routing_table_; |
| 87 | StrictMock<MockRTNLHandler> rtnl_handler_; |
| 88 | }; |
| 89 | |
| 90 | TEST_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 | |
| 96 | TEST_F(ConnectionTest, AddConfig) { |
| 97 | EXPECT_CALL(rtnl_handler_, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 98 | AddInterfaceAddress(kTestDeviceInterfaceIndex0, _, _)); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 99 | 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 | |
| 119 | TEST_F(ConnectionTest, AddConfigReverse) { |
| 120 | EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0, |
| 121 | Connection::kDefaultMetric)); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 122 | vector<std::string> empty_list; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 123 | EXPECT_CALL(resolver_, SetDNSFromLists(empty_list, empty_list)); |
| 124 | connection_->SetDefault(true); |
| 125 | |
| 126 | EXPECT_CALL(rtnl_handler_, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 127 | AddInterfaceAddress(kTestDeviceInterfaceIndex0, _, _)); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 128 | EXPECT_CALL(routing_table_, SetDefaultRoute(kTestDeviceInterfaceIndex0, |
| 129 | ipconfig_, |
| 130 | Connection::kDefaultMetric)); |
| 131 | EXPECT_CALL(resolver_, SetDNSFromIPConfig(ipconfig_)); |
| 132 | |
| 133 | connection_->UpdateFromIPConfig(ipconfig_); |
| 134 | } |
| 135 | |
| 136 | TEST_F(ConnectionTest, Destructor) { |
| 137 | EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex1)); |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 138 | EXPECT_CALL(*device_info_, FlushAddresses(kTestDeviceInterfaceIndex1)); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 139 | { |
| 140 | ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex1, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 141 | kTestDeviceName1, |
| 142 | device_info_.get())); |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 143 | connection->resolver_ = &resolver_; |
| 144 | connection->routing_table_ = &routing_table_; |
| 145 | connection->rtnl_handler_ = &rtnl_handler_; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | } // namespace shill |