blob: 19922b3f3aaf54784c5214ba86788ce1577507d8 [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
Darin Petkovefb09c32011-06-07 20:24:17 -07007#include <base/callback_old.h>
Darin Petkovf65e9282011-06-21 14:29:56 -07008#include <gtest/gtest.h>
Darin Petkovefb09c32011-06-07 20:24:17 -07009
Chris Masone19e30402011-07-19 15:48:47 -070010#include "shill/mock_control.h"
Chris Masone8a7b8be2011-07-22 12:43:37 -070011#include "shill/mock_store.h"
Darin Petkove02b3ca2011-05-31 16:00:44 -070012
Chris Masone8a7b8be2011-07-22 12:43:37 -070013using std::string;
14using testing::_;
15using testing::Return;
16using testing::SaveArg;
17using testing::SetArgumentPointee;
18using testing::StrictMock;
Darin Petkovefb09c32011-06-07 20:24:17 -070019using testing::Test;
20
Darin Petkove02b3ca2011-05-31 16:00:44 -070021namespace shill {
Darin Petkove02b3ca2011-05-31 16:00:44 -070022
Darin Petkovf65e9282011-06-21 14:29:56 -070023namespace {
24const char kDeviceName[] = "testdevice";
25} // namespace {}
26
Darin Petkove02b3ca2011-05-31 16:00:44 -070027class IPConfigTest : public Test {
28 public:
Chris Masone19e30402011-07-19 15:48:47 -070029 IPConfigTest() : ipconfig_(new IPConfig(&control_, kDeviceName)) {}
Darin Petkove02b3ca2011-05-31 16:00:44 -070030
31 protected:
Chris Masone19e30402011-07-19 15:48:47 -070032 MockControl control_;
Darin Petkovefb09c32011-06-07 20:24:17 -070033 IPConfigRefPtr ipconfig_;
Darin Petkove02b3ca2011-05-31 16:00:44 -070034};
35
Darin Petkovf65e9282011-06-21 14:29:56 -070036TEST_F(IPConfigTest, DeviceName) {
37 EXPECT_EQ(kDeviceName, ipconfig_->device_name());
Darin Petkovefb09c32011-06-07 20:24:17 -070038}
39
Darin Petkov92c43902011-06-09 20:46:06 -070040TEST_F(IPConfigTest, RequestIP) {
41 EXPECT_FALSE(ipconfig_->RequestIP());
Darin Petkovefb09c32011-06-07 20:24:17 -070042}
43
Darin Petkov92c43902011-06-09 20:46:06 -070044TEST_F(IPConfigTest, RenewIP) {
45 EXPECT_FALSE(ipconfig_->RenewIP());
46}
47
48TEST_F(IPConfigTest, ReleaseIP) {
49 EXPECT_FALSE(ipconfig_->ReleaseIP());
Darin Petkovefb09c32011-06-07 20:24:17 -070050}
51
Chris Masone8a7b8be2011-07-22 12:43:37 -070052TEST_F(IPConfigTest, SaveLoad) {
53 StrictMock<MockStore> storage;
54 string id, key, value;
55 EXPECT_CALL(storage, SetString(_, _, _))
56 .WillOnce(DoAll(SaveArg<0>(&id),
57 SaveArg<1>(&key),
58 SaveArg<2>(&value),
59 Return(true)));
Chris Masone34af2182011-08-22 11:59:36 -070060 ASSERT_TRUE(ipconfig_->Save(&storage, ""));
Chris Masone8a7b8be2011-07-22 12:43:37 -070061
62 EXPECT_CALL(storage, ContainsGroup(id))
63 .WillOnce(Return(true));
64 EXPECT_CALL(storage, GetString(id, key, _))
65 .WillOnce(DoAll(SetArgumentPointee<2>(value), Return(true)));
Chris Masone34af2182011-08-22 11:59:36 -070066 ASSERT_TRUE(ipconfig_->Load(&storage, ""));
Chris Masone8a7b8be2011-07-22 12:43:37 -070067}
68
Darin Petkovefb09c32011-06-07 20:24:17 -070069TEST_F(IPConfigTest, UpdateProperties) {
70 IPConfig::Properties properties;
71 properties.address = "1.2.3.4";
72 properties.subnet_cidr = 24;
73 properties.broadcast_address = "11.22.33.44";
74 properties.gateway = "5.6.7.8";
75 properties.dns_servers.push_back("10.20.30.40");
76 properties.dns_servers.push_back("20.30.40.50");
77 properties.domain_name = "foo.org";
78 properties.domain_search.push_back("zoo.org");
79 properties.domain_search.push_back("zoo.com");
80 properties.mtu = 700;
Darin Petkovf9b0ca82011-06-20 12:10:23 -070081 ipconfig_->UpdateProperties(properties, true);
Darin Petkovefb09c32011-06-07 20:24:17 -070082 EXPECT_EQ("1.2.3.4", ipconfig_->properties().address);
83 EXPECT_EQ(24, ipconfig_->properties().subnet_cidr);
84 EXPECT_EQ("11.22.33.44", ipconfig_->properties().broadcast_address);
85 EXPECT_EQ("5.6.7.8", ipconfig_->properties().gateway);
86 ASSERT_EQ(2, ipconfig_->properties().dns_servers.size());
87 EXPECT_EQ("10.20.30.40", ipconfig_->properties().dns_servers[0]);
88 EXPECT_EQ("20.30.40.50", ipconfig_->properties().dns_servers[1]);
89 ASSERT_EQ(2, ipconfig_->properties().domain_search.size());
90 EXPECT_EQ("zoo.org", ipconfig_->properties().domain_search[0]);
91 EXPECT_EQ("zoo.com", ipconfig_->properties().domain_search[1]);
92 EXPECT_EQ("foo.org", ipconfig_->properties().domain_name);
93 EXPECT_EQ(700, ipconfig_->properties().mtu);
94}
95
Darin Petkovf9b0ca82011-06-20 12:10:23 -070096namespace {
97
Darin Petkovefb09c32011-06-07 20:24:17 -070098class UpdateCallbackTest {
99 public:
Chris Masone2b105542011-06-22 10:58:09 -0700100 UpdateCallbackTest(const IPConfigRefPtr &ipconfig, bool success)
Darin Petkovefb09c32011-06-07 20:24:17 -0700101 : ipconfig_(ipconfig),
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700102 success_(success),
Darin Petkovefb09c32011-06-07 20:24:17 -0700103 called_(false) {}
104
Chris Masone2b105542011-06-22 10:58:09 -0700105 void Callback(const IPConfigRefPtr &ipconfig, bool success) {
Darin Petkovefb09c32011-06-07 20:24:17 -0700106 called_ = true;
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700107 EXPECT_EQ(ipconfig_.get(), ipconfig.get());
108 EXPECT_EQ(success_, success);
Darin Petkovefb09c32011-06-07 20:24:17 -0700109 }
110
111 bool called() const { return called_; }
112
113 private:
114 IPConfigRefPtr ipconfig_;
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700115 bool success_;
Darin Petkovefb09c32011-06-07 20:24:17 -0700116 bool called_;
117};
118
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700119} // namespace {}
120
Darin Petkovefb09c32011-06-07 20:24:17 -0700121TEST_F(IPConfigTest, UpdateCallback) {
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700122 for (int success = 0; success < 2; success++) {
123 UpdateCallbackTest callback_test(ipconfig_, success);
124 ASSERT_FALSE(callback_test.called());
125 ipconfig_->RegisterUpdateCallback(
126 NewCallback(&callback_test, &UpdateCallbackTest::Callback));
127 ipconfig_->UpdateProperties(IPConfig::Properties(), success);
128 EXPECT_TRUE(callback_test.called());
129 }
Darin Petkove02b3ca2011-05-31 16:00:44 -0700130}
131
132} // namespace shill