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 | |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 7 | #include <base/callback_old.h> |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 8 | #include <gtest/gtest.h> |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 9 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 10 | #include "shill/mock_control.h" |
Chris Masone | 8a7b8be | 2011-07-22 12:43:37 -0700 | [diff] [blame] | 11 | #include "shill/mock_store.h" |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 12 | |
Chris Masone | 8a7b8be | 2011-07-22 12:43:37 -0700 | [diff] [blame] | 13 | using std::string; |
| 14 | using testing::_; |
| 15 | using testing::Return; |
| 16 | using testing::SaveArg; |
| 17 | using testing::SetArgumentPointee; |
| 18 | using testing::StrictMock; |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 19 | using testing::Test; |
| 20 | |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 21 | namespace shill { |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 22 | |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 23 | namespace { |
| 24 | const char kDeviceName[] = "testdevice"; |
| 25 | } // namespace {} |
| 26 | |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 27 | class IPConfigTest : public Test { |
| 28 | public: |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 29 | IPConfigTest() : ipconfig_(new IPConfig(&control_, kDeviceName)) {} |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 30 | |
| 31 | protected: |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 32 | MockControl control_; |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 33 | IPConfigRefPtr ipconfig_; |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 34 | }; |
| 35 | |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 36 | TEST_F(IPConfigTest, DeviceName) { |
| 37 | EXPECT_EQ(kDeviceName, ipconfig_->device_name()); |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 40 | TEST_F(IPConfigTest, RequestIP) { |
| 41 | EXPECT_FALSE(ipconfig_->RequestIP()); |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 44 | TEST_F(IPConfigTest, RenewIP) { |
| 45 | EXPECT_FALSE(ipconfig_->RenewIP()); |
| 46 | } |
| 47 | |
| 48 | TEST_F(IPConfigTest, ReleaseIP) { |
| 49 | EXPECT_FALSE(ipconfig_->ReleaseIP()); |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Chris Masone | 8a7b8be | 2011-07-22 12:43:37 -0700 | [diff] [blame] | 52 | TEST_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 Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 60 | ASSERT_TRUE(ipconfig_->Save(&storage, "")); |
Chris Masone | 8a7b8be | 2011-07-22 12:43:37 -0700 | [diff] [blame] | 61 | |
| 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 Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 66 | ASSERT_TRUE(ipconfig_->Load(&storage, "")); |
Chris Masone | 8a7b8be | 2011-07-22 12:43:37 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 69 | TEST_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 Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 81 | ipconfig_->UpdateProperties(properties, true); |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 82 | 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 Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 96 | namespace { |
| 97 | |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 98 | class UpdateCallbackTest { |
| 99 | public: |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 100 | UpdateCallbackTest(const IPConfigRefPtr &ipconfig, bool success) |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 101 | : ipconfig_(ipconfig), |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 102 | success_(success), |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 103 | called_(false) {} |
| 104 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 105 | void Callback(const IPConfigRefPtr &ipconfig, bool success) { |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 106 | called_ = true; |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 107 | EXPECT_EQ(ipconfig_.get(), ipconfig.get()); |
| 108 | EXPECT_EQ(success_, success); |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | bool called() const { return called_; } |
| 112 | |
| 113 | private: |
| 114 | IPConfigRefPtr ipconfig_; |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 115 | bool success_; |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 116 | bool called_; |
| 117 | }; |
| 118 | |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 119 | } // namespace {} |
| 120 | |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 121 | TEST_F(IPConfigTest, UpdateCallback) { |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 122 | 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 Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | } // namespace shill |