mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 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 | 8a7b8be | 2011-07-22 12:43:37 -0700 | [diff] [blame] | 5 | #include "shill/dhcp_config.h" |
| 6 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 7 | #include <base/bind.h> |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 8 | #include <base/file_util.h> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 9 | #include <base/scoped_temp_dir.h> |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 10 | #include <base/stringprintf.h> |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 11 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | 8a7b8be | 2011-07-22 12:43:37 -0700 | [diff] [blame] | 12 | #include <gtest/gtest.h> |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 13 | |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 14 | #include "shill/dbus_adaptor.h" |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 15 | #include "shill/dhcp_provider.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 16 | #include "shill/event_dispatcher.h" |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 17 | #include "shill/mock_control.h" |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 18 | #include "shill/mock_dhcp_proxy.h" |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 19 | #include "shill/mock_glib.h" |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 20 | #include "shill/property_store_unittest.h" |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 21 | #include "shill/proxy_factory.h" |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 22 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 23 | using base::Bind; |
| 24 | using base::Unretained; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 25 | using std::string; |
| 26 | using std::vector; |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 27 | using testing::_; |
| 28 | using testing::Return; |
| 29 | using testing::SetArgumentPointee; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 30 | using testing::Test; |
| 31 | |
| 32 | namespace shill { |
| 33 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 34 | namespace { |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 35 | const char kDeviceName[] = "eth0"; |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 36 | const char kHostName[] = "hostname"; |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 37 | } // namespace {} |
| 38 | |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 39 | class DHCPConfigTest : public PropertyStoreTest { |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 40 | public: |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 41 | DHCPConfigTest() |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 42 | : proxy_(new MockDHCPProxy()), |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 43 | proxy_factory_(this), |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 44 | config_(new DHCPConfig(&control_, |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 45 | dispatcher(), |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 46 | DHCPProvider::GetInstance(), |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 47 | kDeviceName, |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 48 | kHostName, |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 49 | glib())) {} |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 50 | |
| 51 | virtual void SetUp() { |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 52 | config_->proxy_factory_ = &proxy_factory_; |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | virtual void TearDown() { |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 56 | config_->proxy_factory_ = NULL; |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 57 | } |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 58 | |
| 59 | protected: |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 60 | class TestProxyFactory : public ProxyFactory { |
| 61 | public: |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 62 | explicit TestProxyFactory(DHCPConfigTest *test) : test_(test) {} |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 63 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 64 | virtual DHCPProxyInterface *CreateDHCPProxy(const string &/*service*/) { |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 65 | return test_->proxy_.release(); |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | DHCPConfigTest *test_; |
| 70 | }; |
| 71 | |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 72 | scoped_ptr<MockDHCPProxy> proxy_; |
| 73 | TestProxyFactory proxy_factory_; |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 74 | MockControl control_; |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 75 | DHCPConfigRefPtr config_; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | TEST_F(DHCPConfigTest, GetIPv4AddressString) { |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 79 | EXPECT_EQ("255.255.255.255", config_->GetIPv4AddressString(0xffffffff)); |
| 80 | EXPECT_EQ("0.0.0.0", config_->GetIPv4AddressString(0)); |
| 81 | EXPECT_EQ("1.2.3.4", config_->GetIPv4AddressString(0x04030201)); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 84 | TEST_F(DHCPConfigTest, InitProxy) { |
| 85 | static const char kService[] = ":1.200"; |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 86 | EXPECT_TRUE(proxy_.get()); |
| 87 | EXPECT_FALSE(config_->proxy_.get()); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 88 | config_->InitProxy(kService); |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 89 | EXPECT_FALSE(proxy_.get()); |
| 90 | EXPECT_TRUE(config_->proxy_.get()); |
| 91 | |
| 92 | config_->InitProxy(kService); |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 95 | TEST_F(DHCPConfigTest, ParseConfiguration) { |
| 96 | DHCPConfig::Configuration conf; |
| 97 | conf[DHCPConfig::kConfigurationKeyIPAddress].writer().append_uint32( |
| 98 | 0x01020304); |
| 99 | conf[DHCPConfig::kConfigurationKeySubnetCIDR].writer().append_byte( |
| 100 | 16); |
| 101 | conf[DHCPConfig::kConfigurationKeyBroadcastAddress].writer().append_uint32( |
| 102 | 0x10203040); |
| 103 | { |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 104 | vector<unsigned int> routers; |
| 105 | routers.push_back(0x02040608); |
| 106 | routers.push_back(0x03050709); |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 107 | DBus::MessageIter writer = |
| 108 | conf[DHCPConfig::kConfigurationKeyRouters].writer(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 109 | writer << routers; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 110 | } |
| 111 | { |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 112 | vector<unsigned int> dns; |
| 113 | dns.push_back(0x09070503); |
| 114 | dns.push_back(0x08060402); |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 115 | DBus::MessageIter writer = conf[DHCPConfig::kConfigurationKeyDNS].writer(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 116 | writer << dns; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 117 | } |
| 118 | conf[DHCPConfig::kConfigurationKeyDomainName].writer().append_string( |
| 119 | "domain-name"); |
| 120 | { |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 121 | vector<string> search; |
| 122 | search.push_back("foo.com"); |
| 123 | search.push_back("bar.com"); |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 124 | DBus::MessageIter writer = |
| 125 | conf[DHCPConfig::kConfigurationKeyDomainSearch].writer(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 126 | writer << search; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 127 | } |
| 128 | conf[DHCPConfig::kConfigurationKeyMTU].writer().append_uint16(600); |
| 129 | conf["UnknownKey"] = DBus::Variant(); |
| 130 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 131 | IPConfig::Properties properties; |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 132 | ASSERT_TRUE(config_->ParseConfiguration(conf, &properties)); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 133 | EXPECT_EQ("4.3.2.1", properties.address); |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 134 | EXPECT_EQ(16, properties.subnet_prefix); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 135 | EXPECT_EQ("64.48.32.16", properties.broadcast_address); |
| 136 | EXPECT_EQ("8.6.4.2", properties.gateway); |
| 137 | ASSERT_EQ(2, properties.dns_servers.size()); |
| 138 | EXPECT_EQ("3.5.7.9", properties.dns_servers[0]); |
| 139 | EXPECT_EQ("2.4.6.8", properties.dns_servers[1]); |
| 140 | EXPECT_EQ("domain-name", properties.domain_name); |
| 141 | ASSERT_EQ(2, properties.domain_search.size()); |
| 142 | EXPECT_EQ("foo.com", properties.domain_search[0]); |
| 143 | EXPECT_EQ("bar.com", properties.domain_search[1]); |
| 144 | EXPECT_EQ(600, properties.mtu); |
| 145 | } |
| 146 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 147 | TEST_F(DHCPConfigTest, StartFail) { |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 148 | EXPECT_CALL(*glib(), SpawnAsync(_, _, _, _, _, _, _, _)) |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 149 | .WillOnce(Return(false)); |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 150 | EXPECT_CALL(*glib(), ChildWatchAdd(_, _, _)).Times(0); |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 151 | EXPECT_FALSE(config_->Start()); |
| 152 | EXPECT_EQ(0, config_->pid_); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 153 | } |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 154 | |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 155 | MATCHER_P(IsDHCPCDArgs, has_hostname, "") { |
| 156 | if (string(arg[0]) != "/sbin/dhcpcd" || |
| 157 | string(arg[1]) != "-B" || |
| 158 | string(arg[2]) != kDeviceName) { |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | if (has_hostname) { |
| 163 | if (string(arg[3]) != "-h" || |
| 164 | string(arg[4]) != kHostName || |
| 165 | arg[5] != NULL) { |
| 166 | return false; |
| 167 | } |
| 168 | } else { |
| 169 | if (arg[3] != NULL) { |
| 170 | return false; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return true; |
| 175 | } |
| 176 | |
| 177 | TEST_F(DHCPConfigTest, StartWithHostname) { |
| 178 | EXPECT_CALL(*glib(), SpawnAsync(_, IsDHCPCDArgs(true), _, _, _, _, _, _)) |
| 179 | .WillOnce(Return(false)); |
| 180 | EXPECT_FALSE(config_->Start()); |
| 181 | } |
| 182 | |
| 183 | TEST_F(DHCPConfigTest, StartWithoutHostname) { |
| 184 | DHCPConfigRefPtr config(new DHCPConfig(&control_, |
| 185 | dispatcher(), |
| 186 | DHCPProvider::GetInstance(), |
| 187 | kDeviceName, |
| 188 | "", |
| 189 | glib())); |
| 190 | |
| 191 | EXPECT_CALL(*glib(), SpawnAsync(_, IsDHCPCDArgs(false), _, _, _, _, _, _)) |
| 192 | .WillOnce(Return(false)); |
| 193 | EXPECT_FALSE(config->Start()); |
| 194 | } |
| 195 | |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 196 | namespace { |
| 197 | |
| 198 | class UpdateCallbackTest { |
| 199 | public: |
| 200 | UpdateCallbackTest(const string &message, |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 201 | const IPConfigRefPtr &ipconfig, |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 202 | bool success) |
| 203 | : message_(message), |
| 204 | ipconfig_(ipconfig), |
| 205 | success_(success), |
| 206 | called_(false) {} |
| 207 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 208 | void Callback(const IPConfigRefPtr &ipconfig, bool success) { |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 209 | called_ = true; |
| 210 | EXPECT_EQ(ipconfig_.get(), ipconfig.get()) << message_; |
| 211 | EXPECT_EQ(success_, success) << message_; |
| 212 | } |
| 213 | |
| 214 | bool called() const { return called_; } |
| 215 | |
| 216 | private: |
| 217 | const string message_; |
| 218 | IPConfigRefPtr ipconfig_; |
| 219 | bool success_; |
| 220 | bool called_; |
| 221 | }; |
| 222 | |
| 223 | } // namespace {} |
| 224 | |
| 225 | TEST_F(DHCPConfigTest, ProcessEventSignalFail) { |
| 226 | DHCPConfig::Configuration conf; |
| 227 | conf[DHCPConfig::kConfigurationKeyIPAddress].writer().append_uint32( |
| 228 | 0x01020304); |
| 229 | UpdateCallbackTest callback_test(DHCPConfig::kReasonFail, config_, false); |
| 230 | config_->RegisterUpdateCallback( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 231 | Bind(&UpdateCallbackTest::Callback, Unretained(&callback_test))); |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 232 | config_->ProcessEventSignal(DHCPConfig::kReasonFail, conf); |
| 233 | EXPECT_TRUE(callback_test.called()); |
| 234 | EXPECT_TRUE(config_->properties().address.empty()); |
| 235 | } |
| 236 | |
| 237 | TEST_F(DHCPConfigTest, ProcessEventSignalSuccess) { |
| 238 | static const char * const kReasons[] = { |
| 239 | DHCPConfig::kReasonBound, |
| 240 | DHCPConfig::kReasonRebind, |
| 241 | DHCPConfig::kReasonReboot, |
| 242 | DHCPConfig::kReasonRenew |
| 243 | }; |
| 244 | for (size_t r = 0; r < arraysize(kReasons); r++) { |
| 245 | DHCPConfig::Configuration conf; |
| 246 | string message = string(kReasons[r]) + " failed"; |
| 247 | conf[DHCPConfig::kConfigurationKeyIPAddress].writer().append_uint32(r); |
| 248 | UpdateCallbackTest callback_test(message, config_, true); |
| 249 | config_->RegisterUpdateCallback( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 250 | Bind(&UpdateCallbackTest::Callback, Unretained(&callback_test))); |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 251 | config_->ProcessEventSignal(kReasons[r], conf); |
| 252 | EXPECT_TRUE(callback_test.called()) << message; |
Paul Stewart | 8322440 | 2011-11-30 14:52:30 -0800 | [diff] [blame] | 253 | EXPECT_EQ(base::StringPrintf("%zu.0.0.0", r), config_->properties().address) |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 254 | << message; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | TEST_F(DHCPConfigTest, ProcessEventSignalUnknown) { |
| 259 | DHCPConfig::Configuration conf; |
| 260 | conf[DHCPConfig::kConfigurationKeyIPAddress].writer().append_uint32( |
| 261 | 0x01020304); |
| 262 | static const char kReasonUnknown[] = "UNKNOWN_REASON"; |
| 263 | UpdateCallbackTest callback_test(kReasonUnknown, config_, false); |
| 264 | config_->RegisterUpdateCallback( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 265 | Bind(&UpdateCallbackTest::Callback, Unretained(&callback_test))); |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 266 | config_->ProcessEventSignal(kReasonUnknown, conf); |
| 267 | EXPECT_FALSE(callback_test.called()); |
| 268 | EXPECT_TRUE(config_->properties().address.empty()); |
| 269 | } |
| 270 | |
| 271 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 272 | TEST_F(DHCPConfigTest, ReleaseIP) { |
| 273 | config_->pid_ = 1 << 18; // Ensure unknown positive PID. |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 274 | EXPECT_CALL(*proxy_, Release(kDeviceName)).Times(1); |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 275 | config_->proxy_.reset(proxy_.release()); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 276 | EXPECT_TRUE(config_->ReleaseIP()); |
| 277 | config_->pid_ = 0; |
| 278 | } |
| 279 | |
| 280 | TEST_F(DHCPConfigTest, RenewIP) { |
| 281 | config_->pid_ = 456; |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 282 | EXPECT_CALL(*proxy_, Rebind(kDeviceName)).Times(1); |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 283 | config_->proxy_.reset(proxy_.release()); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 284 | EXPECT_TRUE(config_->RenewIP()); |
| 285 | config_->pid_ = 0; |
| 286 | } |
| 287 | |
| 288 | TEST_F(DHCPConfigTest, RequestIP) { |
| 289 | config_->pid_ = 567; |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 290 | EXPECT_CALL(*proxy_, Rebind(kDeviceName)).Times(1); |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 291 | config_->proxy_.reset(proxy_.release()); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 292 | EXPECT_TRUE(config_->RenewIP()); |
| 293 | config_->pid_ = 0; |
| 294 | } |
| 295 | |
| 296 | TEST_F(DHCPConfigTest, Restart) { |
| 297 | const int kPID1 = 1 << 17; // Ensure unknown positive PID. |
| 298 | const int kPID2 = 987; |
| 299 | const unsigned int kTag1 = 11; |
| 300 | const unsigned int kTag2 = 22; |
| 301 | config_->pid_ = kPID1; |
| 302 | config_->child_watch_tag_ = kTag1; |
| 303 | DHCPProvider::GetInstance()->BindPID(kPID1, config_); |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 304 | EXPECT_CALL(*glib(), SourceRemove(kTag1)).WillOnce(Return(true)); |
| 305 | EXPECT_CALL(*glib(), SpawnClosePID(kPID1)).Times(1); |
| 306 | EXPECT_CALL(*glib(), SpawnAsync(_, _, _, _, _, _, _, _)) |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 307 | .WillOnce(DoAll(SetArgumentPointee<6>(kPID2), Return(true))); |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 308 | EXPECT_CALL(*glib(), ChildWatchAdd(kPID2, _, _)).WillOnce(Return(kTag2)); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 309 | EXPECT_TRUE(config_->Restart()); |
| 310 | EXPECT_EQ(kPID2, config_->pid_); |
| 311 | EXPECT_EQ(config_.get(), DHCPProvider::GetInstance()->GetConfig(kPID2).get()); |
| 312 | EXPECT_EQ(kTag2, config_->child_watch_tag_); |
| 313 | DHCPProvider::GetInstance()->UnbindPID(kPID2); |
| 314 | config_->pid_ = 0; |
| 315 | config_->child_watch_tag_ = 0; |
| 316 | } |
| 317 | |
| 318 | TEST_F(DHCPConfigTest, RestartNoClient) { |
| 319 | const int kPID = 777; |
| 320 | const unsigned int kTag = 66; |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 321 | EXPECT_CALL(*glib(), SourceRemove(_)).Times(0); |
| 322 | EXPECT_CALL(*glib(), SpawnClosePID(_)).Times(0); |
| 323 | EXPECT_CALL(*glib(), SpawnAsync(_, _, _, _, _, _, _, _)) |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 324 | .WillOnce(DoAll(SetArgumentPointee<6>(kPID), Return(true))); |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 325 | EXPECT_CALL(*glib(), ChildWatchAdd(kPID, _, _)).WillOnce(Return(kTag)); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 326 | EXPECT_TRUE(config_->Restart()); |
| 327 | EXPECT_EQ(kPID, config_->pid_); |
| 328 | EXPECT_EQ(config_.get(), DHCPProvider::GetInstance()->GetConfig(kPID).get()); |
| 329 | EXPECT_EQ(kTag, config_->child_watch_tag_); |
| 330 | DHCPProvider::GetInstance()->UnbindPID(kPID); |
| 331 | config_->pid_ = 0; |
| 332 | config_->child_watch_tag_ = 0; |
| 333 | } |
| 334 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 335 | TEST_F(DHCPConfigTest, StartSuccess) { |
| 336 | const int kPID = 123456; |
| 337 | const unsigned int kTag = 55; |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 338 | EXPECT_CALL(*glib(), SpawnAsync(_, _, _, _, _, _, _, _)) |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 339 | .WillOnce(DoAll(SetArgumentPointee<6>(kPID), Return(true))); |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 340 | EXPECT_CALL(*glib(), ChildWatchAdd(kPID, _, _)).WillOnce(Return(kTag)); |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 341 | EXPECT_TRUE(config_->Start()); |
| 342 | EXPECT_EQ(kPID, config_->pid_); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 343 | EXPECT_EQ(config_.get(), DHCPProvider::GetInstance()->GetConfig(kPID).get()); |
| 344 | EXPECT_EQ(kTag, config_->child_watch_tag_); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 345 | |
| 346 | ScopedTempDir temp_dir; |
| 347 | config_->root_ = temp_dir.path(); |
| 348 | FilePath varrun = temp_dir.path().Append("var/run"); |
| 349 | EXPECT_TRUE(file_util::CreateDirectory(varrun)); |
| 350 | FilePath pid_file = |
| 351 | varrun.Append(base::StringPrintf("dhcpcd-%s.pid", kDeviceName)); |
| 352 | FilePath lease_file = |
| 353 | varrun.Append(base::StringPrintf("dhcpcd-%s.lease", kDeviceName)); |
| 354 | EXPECT_EQ(0, file_util::WriteFile(pid_file, "", 0)); |
| 355 | EXPECT_EQ(0, file_util::WriteFile(lease_file, "", 0)); |
| 356 | ASSERT_TRUE(file_util::PathExists(pid_file)); |
| 357 | ASSERT_TRUE(file_util::PathExists(lease_file)); |
| 358 | |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 359 | EXPECT_CALL(*glib(), SpawnClosePID(kPID)).Times(1); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 360 | DHCPConfig::ChildWatchCallback(kPID, 0, config_.get()); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 361 | EXPECT_EQ(NULL, DHCPProvider::GetInstance()->GetConfig(kPID).get()); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 362 | EXPECT_FALSE(file_util::PathExists(pid_file)); |
| 363 | EXPECT_FALSE(file_util::PathExists(lease_file)); |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 366 | TEST_F(DHCPConfigTest, Stop) { |
| 367 | // Ensure no crashes. |
| 368 | const int kPID = 1 << 17; // Ensure unknown positive PID. |
| 369 | config_->Stop(); |
| 370 | config_->pid_ = kPID; |
| 371 | config_->Stop(); |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 372 | EXPECT_CALL(*glib(), SpawnClosePID(kPID)).Times(1); // Invoked by destructor. |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 373 | } |
| 374 | |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 375 | TEST_F(DHCPConfigTest, SetProperty) { |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 376 | ::DBus::Error error; |
| 377 | // Ensure that an attempt to write a R/O property returns InvalidArgs error. |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 378 | EXPECT_FALSE(DBusAdaptor::SetProperty(config_->mutable_store(), |
| 379 | flimflam::kAddressProperty, |
| 380 | PropertyStoreTest::kStringV, |
| 381 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 382 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 385 | } // namespace shill |