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 | |
| 5 | #include <gtest/gtest.h> |
| 6 | #include <gmock/gmock.h> |
| 7 | |
| 8 | #include "shill/connection.h" |
| 9 | #include "shill/ip_address.h" |
| 10 | #include "shill/ipconfig.h" |
| 11 | #include "shill/mock_control.h" |
| 12 | #include "shill/mock_resolver.h" |
| 13 | #include "shill/mock_routing_table.h" |
| 14 | #include "shill/mock_rtnl_handler.h" |
| 15 | #include "shill/routing_table_entry.h" |
| 16 | |
| 17 | using testing::_; |
| 18 | using testing::NiceMock; |
| 19 | using testing::Return; |
| 20 | using testing::StrictMock; |
| 21 | using testing::Test; |
| 22 | |
| 23 | namespace shill { |
| 24 | |
| 25 | namespace { |
| 26 | const char kTestDeviceName0[] = "netdev0"; |
| 27 | const int kTestDeviceInterfaceIndex0 = 123; |
| 28 | const char kTestDeviceName1[] = "netdev1"; |
| 29 | const int kTestDeviceInterfaceIndex1 = 321; |
| 30 | const char kIPAddress0[] = "192.168.1.1"; |
| 31 | const char kGatewayAddress0[] = "192.168.1.254"; |
| 32 | const char kNameServer0[] = "8.8.8.8"; |
| 33 | const char kNameServer1[] = "8.8.9.9"; |
| 34 | const char kSearchDomain0[] = "chromium.org"; |
| 35 | const char kSearchDomain1[] = "google.com"; |
| 36 | } // namespace {} |
| 37 | |
| 38 | class ConnectionTest : public Test { |
| 39 | public: |
| 40 | ConnectionTest() |
| 41 | : connection_( |
| 42 | new Connection(kTestDeviceInterfaceIndex0, kTestDeviceName0)), |
| 43 | ipconfig_(new IPConfig(&control_, kTestDeviceName0)) {} |
| 44 | |
| 45 | virtual void SetUp() { |
| 46 | connection_->resolver_ = &resolver_; |
| 47 | connection_->routing_table_ = &routing_table_; |
| 48 | connection_->rtnl_handler_ = &rtnl_handler_; |
| 49 | |
| 50 | IPConfig::Properties properties; |
| 51 | properties.address = kIPAddress0; |
| 52 | properties.gateway = kGatewayAddress0; |
| 53 | properties.dns_servers.push_back(kNameServer0); |
| 54 | properties.dns_servers.push_back(kNameServer1); |
| 55 | properties.domain_search.push_back(kSearchDomain0); |
| 56 | properties.domain_search.push_back(kSearchDomain1); |
| 57 | properties.address_family = IPAddress::kAddressFamilyIPv4; |
| 58 | ipconfig_->UpdateProperties(properties, true); |
| 59 | } |
| 60 | |
| 61 | protected: |
| 62 | ConnectionRefPtr connection_; |
| 63 | MockControl control_; |
| 64 | IPConfigRefPtr ipconfig_; |
| 65 | StrictMock<MockResolver> resolver_; |
| 66 | StrictMock<MockRoutingTable> routing_table_; |
| 67 | StrictMock<MockRTNLHandler> rtnl_handler_; |
| 68 | }; |
| 69 | |
| 70 | TEST_F(ConnectionTest, InitState) { |
| 71 | EXPECT_EQ(kTestDeviceInterfaceIndex0, connection_->interface_index_); |
| 72 | EXPECT_EQ(kTestDeviceName0, connection_->interface_name_); |
| 73 | EXPECT_FALSE(connection_->is_default()); |
| 74 | } |
| 75 | |
| 76 | TEST_F(ConnectionTest, AddConfig) { |
| 77 | EXPECT_CALL(rtnl_handler_, |
| 78 | AddInterfaceAddress(kTestDeviceInterfaceIndex0, _)); |
| 79 | EXPECT_CALL(routing_table_, SetDefaultRoute(kTestDeviceInterfaceIndex0, |
| 80 | ipconfig_, |
| 81 | Connection::kNonDefaultMetric)); |
| 82 | connection_->UpdateFromIPConfig(ipconfig_); |
| 83 | |
| 84 | EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0, |
| 85 | Connection::kDefaultMetric)); |
| 86 | EXPECT_CALL(resolver_, SetDNSFromLists( |
| 87 | ipconfig_->properties().dns_servers, |
| 88 | ipconfig_->properties().domain_search)); |
| 89 | |
| 90 | connection_->SetDefault(true); |
| 91 | EXPECT_TRUE(connection_->is_default()); |
| 92 | |
| 93 | EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0, |
| 94 | Connection::kNonDefaultMetric)); |
| 95 | connection_->SetDefault(false); |
| 96 | EXPECT_FALSE(connection_->is_default()); |
| 97 | } |
| 98 | |
| 99 | TEST_F(ConnectionTest, AddConfigReverse) { |
| 100 | EXPECT_CALL(routing_table_, SetDefaultMetric(kTestDeviceInterfaceIndex0, |
| 101 | Connection::kDefaultMetric)); |
| 102 | std::vector<std::string> empty_list; |
| 103 | EXPECT_CALL(resolver_, SetDNSFromLists(empty_list, empty_list)); |
| 104 | connection_->SetDefault(true); |
| 105 | |
| 106 | EXPECT_CALL(rtnl_handler_, |
| 107 | AddInterfaceAddress(kTestDeviceInterfaceIndex0, _)); |
| 108 | EXPECT_CALL(routing_table_, SetDefaultRoute(kTestDeviceInterfaceIndex0, |
| 109 | ipconfig_, |
| 110 | Connection::kDefaultMetric)); |
| 111 | EXPECT_CALL(resolver_, SetDNSFromIPConfig(ipconfig_)); |
| 112 | |
| 113 | connection_->UpdateFromIPConfig(ipconfig_); |
| 114 | } |
| 115 | |
| 116 | TEST_F(ConnectionTest, Destructor) { |
| 117 | EXPECT_CALL(routing_table_, FlushRoutes(kTestDeviceInterfaceIndex1)); |
| 118 | { |
| 119 | ConnectionRefPtr connection(new Connection(kTestDeviceInterfaceIndex1, |
| 120 | kTestDeviceName1)); |
| 121 | connection->resolver_ = &resolver_; |
| 122 | connection->routing_table_ = &routing_table_; |
| 123 | connection->rtnl_handler_ = &rtnl_handler_; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | } // namespace shill |