blob: 1c407aa6b317341883b5e3a9b09921eb8ace1757 [file] [log] [blame]
Darin Petkove02b3ca2011-05-31 16:00:44 -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
Chris Masone19e30402011-07-19 15:48:47 -07005#include "shill/ipconfig.h"
6
Eric Shienbrood3e20a232012-02-16 11:35:56 -05007#include <base/bind.h>
mukesh agrawal7aed61c2013-04-22 16:01:24 -07008#include <chromeos/dbus/service_constants.h>
9#include <gmock/gmock.h>
Darin Petkovf65e9282011-06-21 14:29:56 -070010#include <gtest/gtest.h>
Darin Petkovefb09c32011-06-07 20:24:17 -070011
mukesh agrawal7aed61c2013-04-22 16:01:24 -070012#include "shill/mock_adaptors.h"
Chris Masone19e30402011-07-19 15:48:47 -070013#include "shill/mock_control.h"
Chris Masone8a7b8be2011-07-22 12:43:37 -070014#include "shill/mock_store.h"
mukesh agrawal7aed61c2013-04-22 16:01:24 -070015#include "shill/static_ip_parameters.h"
Darin Petkove02b3ca2011-05-31 16:00:44 -070016
Eric Shienbrood3e20a232012-02-16 11:35:56 -050017using base::Bind;
18using base::Unretained;
Chris Masone8a7b8be2011-07-22 12:43:37 -070019using std::string;
20using testing::_;
mukesh agrawal7aed61c2013-04-22 16:01:24 -070021using testing::Mock;
Chris Masone8a7b8be2011-07-22 12:43:37 -070022using testing::Return;
23using testing::SaveArg;
24using testing::SetArgumentPointee;
25using testing::StrictMock;
Darin Petkovefb09c32011-06-07 20:24:17 -070026using testing::Test;
27
Darin Petkove02b3ca2011-05-31 16:00:44 -070028namespace shill {
Darin Petkove02b3ca2011-05-31 16:00:44 -070029
Darin Petkovf65e9282011-06-21 14:29:56 -070030namespace {
31const char kDeviceName[] = "testdevice";
32} // namespace {}
33
Darin Petkove02b3ca2011-05-31 16:00:44 -070034class IPConfigTest : public Test {
35 public:
Chris Masone19e30402011-07-19 15:48:47 -070036 IPConfigTest() : ipconfig_(new IPConfig(&control_, kDeviceName)) {}
mukesh agrawal1c1dd352013-05-08 15:58:34 -070037 void DropRef(const IPConfigRefPtr &/*ipconfig*/, bool /*config_success*/) {
38 ipconfig_ = NULL;
39 }
Darin Petkove02b3ca2011-05-31 16:00:44 -070040
41 protected:
mukesh agrawal7aed61c2013-04-22 16:01:24 -070042 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 Masone19e30402011-07-19 15:48:47 -070050 MockControl control_;
Darin Petkovefb09c32011-06-07 20:24:17 -070051 IPConfigRefPtr ipconfig_;
Darin Petkove02b3ca2011-05-31 16:00:44 -070052};
53
Darin Petkovf65e9282011-06-21 14:29:56 -070054TEST_F(IPConfigTest, DeviceName) {
55 EXPECT_EQ(kDeviceName, ipconfig_->device_name());
Darin Petkovefb09c32011-06-07 20:24:17 -070056}
57
Darin Petkov92c43902011-06-09 20:46:06 -070058TEST_F(IPConfigTest, RequestIP) {
59 EXPECT_FALSE(ipconfig_->RequestIP());
Darin Petkovefb09c32011-06-07 20:24:17 -070060}
61
Darin Petkov92c43902011-06-09 20:46:06 -070062TEST_F(IPConfigTest, RenewIP) {
63 EXPECT_FALSE(ipconfig_->RenewIP());
64}
65
66TEST_F(IPConfigTest, ReleaseIP) {
Paul Stewart217c61d2013-06-13 15:12:02 -070067 EXPECT_FALSE(ipconfig_->ReleaseIP(IPConfig::kReleaseReasonDisconnect));
Darin Petkovefb09c32011-06-07 20:24:17 -070068}
69
Chris Masone8a7b8be2011-07-22 12:43:37 -070070TEST_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 Masone34af2182011-08-22 11:59:36 -070078 ASSERT_TRUE(ipconfig_->Save(&storage, ""));
Chris Masone8a7b8be2011-07-22 12:43:37 -070079
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 Masone34af2182011-08-22 11:59:36 -070084 ASSERT_TRUE(ipconfig_->Load(&storage, ""));
Chris Masone8a7b8be2011-07-22 12:43:37 -070085}
86
Darin Petkovefb09c32011-06-07 20:24:17 -070087TEST_F(IPConfigTest, UpdateProperties) {
88 IPConfig::Properties properties;
89 properties.address = "1.2.3.4";
Paul Stewart48100b02012-03-19 07:53:52 -070090 properties.subnet_prefix = 24;
Darin Petkovefb09c32011-06-07 20:24:17 -070091 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 Chana0163122012-09-25 15:10:52 -070098 properties.blackhole_ipv6 = true;
Darin Petkovefb09c32011-06-07 20:24:17 -070099 properties.mtu = 700;
mukesh agrawal7aed61c2013-04-22 16:01:24 -0700100 UpdateProperties(properties, true);
Darin Petkovefb09c32011-06-07 20:24:17 -0700101 EXPECT_EQ("1.2.3.4", ipconfig_->properties().address);
Paul Stewart48100b02012-03-19 07:53:52 -0700102 EXPECT_EQ(24, ipconfig_->properties().subnet_prefix);
Darin Petkovefb09c32011-06-07 20:24:17 -0700103 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 Chana0163122012-09-25 15:10:52 -0700112 EXPECT_TRUE(ipconfig_->properties().blackhole_ipv6);
Darin Petkovefb09c32011-06-07 20:24:17 -0700113 EXPECT_EQ(700, ipconfig_->properties().mtu);
114}
115
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700116namespace {
117
Darin Petkovefb09c32011-06-07 20:24:17 -0700118class UpdateCallbackTest {
119 public:
Chris Masone2b105542011-06-22 10:58:09 -0700120 UpdateCallbackTest(const IPConfigRefPtr &ipconfig, bool success)
Darin Petkovefb09c32011-06-07 20:24:17 -0700121 : ipconfig_(ipconfig),
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700122 success_(success),
Darin Petkovefb09c32011-06-07 20:24:17 -0700123 called_(false) {}
124
Chris Masone2b105542011-06-22 10:58:09 -0700125 void Callback(const IPConfigRefPtr &ipconfig, bool success) {
Darin Petkovefb09c32011-06-07 20:24:17 -0700126 called_ = true;
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700127 EXPECT_EQ(ipconfig_.get(), ipconfig.get());
128 EXPECT_EQ(success_, success);
Darin Petkovefb09c32011-06-07 20:24:17 -0700129 }
130
131 bool called() const { return called_; }
132
133 private:
134 IPConfigRefPtr ipconfig_;
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700135 bool success_;
Darin Petkovefb09c32011-06-07 20:24:17 -0700136 bool called_;
137};
138
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700139} // namespace {}
140
Darin Petkovefb09c32011-06-07 20:24:17 -0700141TEST_F(IPConfigTest, UpdateCallback) {
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700142 for (int success = 0; success < 2; success++) {
143 UpdateCallbackTest callback_test(ipconfig_, success);
144 ASSERT_FALSE(callback_test.called());
145 ipconfig_->RegisterUpdateCallback(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500146 Bind(&UpdateCallbackTest::Callback, Unretained(&callback_test)));
mukesh agrawal7aed61c2013-04-22 16:01:24 -0700147 UpdateProperties(IPConfig::Properties(), success);
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700148 EXPECT_TRUE(callback_test.called());
149 }
Darin Petkove02b3ca2011-05-31 16:00:44 -0700150}
151
mukesh agrawal1c1dd352013-05-08 15:58:34 -0700152TEST_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 agrawal7aed61c2013-04-22 16:01:24 -0700160TEST_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 Petkove02b3ca2011-05-31 16:00:44 -0700181} // namespace shill