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