Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -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 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 5 | #include "shill/ipconfig.h" |
| 6 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 7 | #include <base/bind.h> |
mukesh agrawal | 7aed61c | 2013-04-22 16:01:24 -0700 | [diff] [blame] | 8 | #include <chromeos/dbus/service_constants.h> |
| 9 | #include <gmock/gmock.h> |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 10 | #include <gtest/gtest.h> |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 11 | |
mukesh agrawal | 7aed61c | 2013-04-22 16:01:24 -0700 | [diff] [blame] | 12 | #include "shill/mock_adaptors.h" |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 13 | #include "shill/mock_control.h" |
Chris Masone | 8a7b8be | 2011-07-22 12:43:37 -0700 | [diff] [blame] | 14 | #include "shill/mock_store.h" |
mukesh agrawal | 7aed61c | 2013-04-22 16:01:24 -0700 | [diff] [blame] | 15 | #include "shill/static_ip_parameters.h" |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 16 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 17 | using base::Bind; |
| 18 | using base::Unretained; |
Chris Masone | 8a7b8be | 2011-07-22 12:43:37 -0700 | [diff] [blame] | 19 | using std::string; |
| 20 | using testing::_; |
mukesh agrawal | 7aed61c | 2013-04-22 16:01:24 -0700 | [diff] [blame] | 21 | using testing::Mock; |
Chris Masone | 8a7b8be | 2011-07-22 12:43:37 -0700 | [diff] [blame] | 22 | using testing::Return; |
| 23 | using testing::SaveArg; |
| 24 | using testing::SetArgumentPointee; |
| 25 | using testing::StrictMock; |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 26 | using testing::Test; |
| 27 | |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 28 | namespace shill { |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 29 | |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 30 | namespace { |
| 31 | const char kDeviceName[] = "testdevice"; |
| 32 | } // namespace {} |
| 33 | |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 34 | class IPConfigTest : public Test { |
| 35 | public: |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 36 | IPConfigTest() : ipconfig_(new IPConfig(&control_, kDeviceName)) {} |
mukesh agrawal | 1c1dd35 | 2013-05-08 15:58:34 -0700 | [diff] [blame] | 37 | void DropRef(const IPConfigRefPtr &/*ipconfig*/, bool /*config_success*/) { |
| 38 | ipconfig_ = NULL; |
| 39 | } |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 40 | |
| 41 | protected: |
mukesh agrawal | 7aed61c | 2013-04-22 16:01:24 -0700 | [diff] [blame] | 42 | IPConfigMockAdaptor *GetAdaptor() { |
| 43 | return dynamic_cast<IPConfigMockAdaptor *>(ipconfig_->adaptor_.get()); |
| 44 | } |
| 45 | |
| 46 | void UpdateProperties(const IPConfig::Properties &properties, bool success) { |
| 47 | ipconfig_->UpdateProperties(properties, success); |
| 48 | } |
| 49 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 50 | MockControl control_; |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 51 | IPConfigRefPtr ipconfig_; |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 54 | TEST_F(IPConfigTest, DeviceName) { |
| 55 | EXPECT_EQ(kDeviceName, ipconfig_->device_name()); |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 58 | TEST_F(IPConfigTest, RequestIP) { |
| 59 | EXPECT_FALSE(ipconfig_->RequestIP()); |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 62 | TEST_F(IPConfigTest, RenewIP) { |
| 63 | EXPECT_FALSE(ipconfig_->RenewIP()); |
| 64 | } |
| 65 | |
| 66 | TEST_F(IPConfigTest, ReleaseIP) { |
Paul Stewart | 217c61d | 2013-06-13 15:12:02 -0700 | [diff] [blame] | 67 | EXPECT_FALSE(ipconfig_->ReleaseIP(IPConfig::kReleaseReasonDisconnect)); |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Chris Masone | 8a7b8be | 2011-07-22 12:43:37 -0700 | [diff] [blame] | 70 | TEST_F(IPConfigTest, SaveLoad) { |
| 71 | StrictMock<MockStore> storage; |
| 72 | string id, key, value; |
| 73 | EXPECT_CALL(storage, SetString(_, _, _)) |
| 74 | .WillOnce(DoAll(SaveArg<0>(&id), |
| 75 | SaveArg<1>(&key), |
| 76 | SaveArg<2>(&value), |
| 77 | Return(true))); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 78 | ASSERT_TRUE(ipconfig_->Save(&storage, "")); |
Chris Masone | 8a7b8be | 2011-07-22 12:43:37 -0700 | [diff] [blame] | 79 | |
| 80 | EXPECT_CALL(storage, ContainsGroup(id)) |
| 81 | .WillOnce(Return(true)); |
| 82 | EXPECT_CALL(storage, GetString(id, key, _)) |
| 83 | .WillOnce(DoAll(SetArgumentPointee<2>(value), Return(true))); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 84 | ASSERT_TRUE(ipconfig_->Load(&storage, "")); |
Chris Masone | 8a7b8be | 2011-07-22 12:43:37 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 87 | TEST_F(IPConfigTest, UpdateProperties) { |
| 88 | IPConfig::Properties properties; |
| 89 | properties.address = "1.2.3.4"; |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 90 | properties.subnet_prefix = 24; |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 91 | properties.broadcast_address = "11.22.33.44"; |
| 92 | properties.gateway = "5.6.7.8"; |
| 93 | properties.dns_servers.push_back("10.20.30.40"); |
| 94 | properties.dns_servers.push_back("20.30.40.50"); |
| 95 | properties.domain_name = "foo.org"; |
| 96 | properties.domain_search.push_back("zoo.org"); |
| 97 | properties.domain_search.push_back("zoo.com"); |
Ben Chan | a016312 | 2012-09-25 15:10:52 -0700 | [diff] [blame] | 98 | properties.blackhole_ipv6 = true; |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 99 | properties.mtu = 700; |
mukesh agrawal | 7aed61c | 2013-04-22 16:01:24 -0700 | [diff] [blame] | 100 | UpdateProperties(properties, true); |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 101 | EXPECT_EQ("1.2.3.4", ipconfig_->properties().address); |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 102 | EXPECT_EQ(24, ipconfig_->properties().subnet_prefix); |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 103 | EXPECT_EQ("11.22.33.44", ipconfig_->properties().broadcast_address); |
| 104 | EXPECT_EQ("5.6.7.8", ipconfig_->properties().gateway); |
| 105 | ASSERT_EQ(2, ipconfig_->properties().dns_servers.size()); |
| 106 | EXPECT_EQ("10.20.30.40", ipconfig_->properties().dns_servers[0]); |
| 107 | EXPECT_EQ("20.30.40.50", ipconfig_->properties().dns_servers[1]); |
| 108 | ASSERT_EQ(2, ipconfig_->properties().domain_search.size()); |
| 109 | EXPECT_EQ("zoo.org", ipconfig_->properties().domain_search[0]); |
| 110 | EXPECT_EQ("zoo.com", ipconfig_->properties().domain_search[1]); |
| 111 | EXPECT_EQ("foo.org", ipconfig_->properties().domain_name); |
Ben Chan | a016312 | 2012-09-25 15:10:52 -0700 | [diff] [blame] | 112 | EXPECT_TRUE(ipconfig_->properties().blackhole_ipv6); |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 113 | EXPECT_EQ(700, ipconfig_->properties().mtu); |
| 114 | } |
| 115 | |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 116 | namespace { |
| 117 | |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 118 | class UpdateCallbackTest { |
| 119 | public: |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 120 | UpdateCallbackTest(const IPConfigRefPtr &ipconfig, bool success) |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 121 | : ipconfig_(ipconfig), |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 122 | success_(success), |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 123 | called_(false) {} |
| 124 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 125 | void Callback(const IPConfigRefPtr &ipconfig, bool success) { |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 126 | called_ = true; |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 127 | EXPECT_EQ(ipconfig_.get(), ipconfig.get()); |
| 128 | EXPECT_EQ(success_, success); |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | bool called() const { return called_; } |
| 132 | |
| 133 | private: |
| 134 | IPConfigRefPtr ipconfig_; |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 135 | bool success_; |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 136 | bool called_; |
| 137 | }; |
| 138 | |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 139 | } // namespace {} |
| 140 | |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 141 | TEST_F(IPConfigTest, UpdateCallback) { |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 142 | for (int success = 0; success < 2; success++) { |
| 143 | UpdateCallbackTest callback_test(ipconfig_, success); |
| 144 | ASSERT_FALSE(callback_test.called()); |
| 145 | ipconfig_->RegisterUpdateCallback( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 146 | Bind(&UpdateCallbackTest::Callback, Unretained(&callback_test))); |
mukesh agrawal | 7aed61c | 2013-04-22 16:01:24 -0700 | [diff] [blame] | 147 | UpdateProperties(IPConfig::Properties(), success); |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 148 | EXPECT_TRUE(callback_test.called()); |
| 149 | } |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 150 | } |
| 151 | |
mukesh agrawal | 1c1dd35 | 2013-05-08 15:58:34 -0700 | [diff] [blame] | 152 | TEST_F(IPConfigTest, UpdatePropertiesWithDropRef) { |
| 153 | // The UpdateCallback should be able to drop a reference to the |
| 154 | // IPConfig object without crashing. |
| 155 | ipconfig_->RegisterUpdateCallback( |
| 156 | Bind(&IPConfigTest::DropRef, Unretained(this))); |
| 157 | UpdateProperties(IPConfig::Properties(), true); |
| 158 | } |
| 159 | |
mukesh agrawal | 7aed61c | 2013-04-22 16:01:24 -0700 | [diff] [blame] | 160 | TEST_F(IPConfigTest, PropertyChanges) { |
| 161 | IPConfigMockAdaptor *adaptor = GetAdaptor(); |
| 162 | |
| 163 | StaticIPParameters static_ip_params; |
| 164 | EXPECT_CALL(*adaptor, EmitStringChanged(flimflam::kAddressProperty, _)); |
| 165 | EXPECT_CALL(*adaptor, EmitStringsChanged(flimflam::kNameServersProperty, _)); |
| 166 | ipconfig_->ApplyStaticIPParameters(&static_ip_params); |
| 167 | Mock::VerifyAndClearExpectations(adaptor); |
| 168 | |
| 169 | IPConfig::Properties ip_properties; |
| 170 | EXPECT_CALL(*adaptor, EmitStringChanged(flimflam::kAddressProperty, _)); |
| 171 | EXPECT_CALL(*adaptor, EmitStringsChanged(flimflam::kNameServersProperty, _)); |
| 172 | UpdateProperties(ip_properties, true); |
| 173 | Mock::VerifyAndClearExpectations(adaptor); |
| 174 | |
| 175 | EXPECT_CALL(*adaptor, EmitStringChanged(flimflam::kAddressProperty, _)); |
| 176 | EXPECT_CALL(*adaptor, EmitStringsChanged(flimflam::kNameServersProperty, _)); |
| 177 | UpdateProperties(ip_properties, false); |
| 178 | Mock::VerifyAndClearExpectations(adaptor); |
| 179 | } |
| 180 | |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 181 | } // namespace shill |