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" |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 20 | #include "shill/mock_minijail.h" |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 21 | #include "shill/property_store_unittest.h" |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 22 | #include "shill/proxy_factory.h" |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 23 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 24 | using base::Bind; |
| 25 | using base::Unretained; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 26 | using std::string; |
| 27 | using std::vector; |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 28 | using testing::_; |
| 29 | using testing::Return; |
| 30 | using testing::SetArgumentPointee; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 31 | using testing::Test; |
| 32 | |
| 33 | namespace shill { |
| 34 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 35 | namespace { |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 36 | const char kDeviceName[] = "eth0"; |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 37 | const char kHostName[] = "hostname"; |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 38 | const char kLeaseFileSuffix[] = "leasefilesuffix"; |
| 39 | const bool kArpGateway = true; |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 40 | } // namespace {} |
| 41 | |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 42 | class DHCPConfigTest : public PropertyStoreTest { |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 43 | public: |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 44 | DHCPConfigTest() |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 45 | : proxy_(new MockDHCPProxy()), |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 46 | proxy_factory_(this), |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 47 | minijail_(new MockMinijail()), |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 48 | config_(new DHCPConfig(&control_, |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 49 | dispatcher(), |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 50 | DHCPProvider::GetInstance(), |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 51 | kDeviceName, |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 52 | kHostName, |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 53 | kLeaseFileSuffix, |
| 54 | kArpGateway, |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 55 | glib())) {} |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 56 | |
| 57 | virtual void SetUp() { |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 58 | config_->proxy_factory_ = &proxy_factory_; |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 59 | config_->minijail_ = minijail_.get(); |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | virtual void TearDown() { |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 63 | config_->proxy_factory_ = NULL; |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 64 | config_->minijail_ = NULL; |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 65 | } |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 66 | |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 67 | DHCPConfigRefPtr CreateMockMinijailConfig(const string &hostname, |
| 68 | const string &lease_suffix, |
| 69 | bool arp_gateway); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 70 | DHCPConfigRefPtr CreateRunningConfig(const string &hostname, |
| 71 | const string &lease_suffix, |
| 72 | bool arp_gateway); |
| 73 | void StopRunningConfigAndExpect(DHCPConfigRefPtr config, |
| 74 | bool lease_file_exists); |
| 75 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 76 | protected: |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 77 | class TestProxyFactory : public ProxyFactory { |
| 78 | public: |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 79 | explicit TestProxyFactory(DHCPConfigTest *test) : test_(test) {} |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 80 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 81 | virtual DHCPProxyInterface *CreateDHCPProxy(const string &/*service*/) { |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 82 | return test_->proxy_.release(); |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | DHCPConfigTest *test_; |
| 87 | }; |
| 88 | |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 89 | static const int kPID; |
| 90 | static const unsigned int kTag; |
| 91 | |
| 92 | FilePath lease_file_; |
| 93 | FilePath pid_file_; |
| 94 | ScopedTempDir temp_dir_; |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 95 | scoped_ptr<MockDHCPProxy> proxy_; |
| 96 | TestProxyFactory proxy_factory_; |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 97 | MockControl control_; |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 98 | scoped_ptr<MockMinijail> minijail_; |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 99 | DHCPConfigRefPtr config_; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 100 | }; |
| 101 | |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 102 | const int DHCPConfigTest::kPID = 123456; |
| 103 | const unsigned int DHCPConfigTest::kTag = 77; |
| 104 | |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 105 | DHCPConfigRefPtr DHCPConfigTest::CreateMockMinijailConfig( |
| 106 | const string &hostname, |
| 107 | const string &lease_suffix, |
| 108 | bool arp_gateway) { |
| 109 | DHCPConfigRefPtr config(new DHCPConfig(&control_, |
| 110 | dispatcher(), |
| 111 | DHCPProvider::GetInstance(), |
| 112 | kDeviceName, |
| 113 | hostname, |
| 114 | lease_suffix, |
| 115 | arp_gateway, |
| 116 | glib())); |
| 117 | config->minijail_ = minijail_.get(); |
| 118 | EXPECT_CALL(*minijail_, RunAndDestroy(_, _, _)).WillOnce(Return(false)); |
| 119 | |
| 120 | return config; |
| 121 | } |
| 122 | |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 123 | DHCPConfigRefPtr DHCPConfigTest::CreateRunningConfig(const string &hostname, |
| 124 | const string &lease_suffix, |
| 125 | bool arp_gateway) { |
| 126 | DHCPConfigRefPtr config(new DHCPConfig(&control_, |
| 127 | dispatcher(), |
| 128 | DHCPProvider::GetInstance(), |
| 129 | kDeviceName, |
| 130 | hostname, |
| 131 | lease_suffix, |
| 132 | arp_gateway, |
| 133 | glib())); |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 134 | config->minijail_ = minijail_.get(); |
| 135 | EXPECT_CALL(*minijail_, RunAndDestroy(_, _, _)) |
| 136 | .WillOnce(DoAll(SetArgumentPointee<2>(kPID), Return(true))); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 137 | EXPECT_CALL(*glib(), ChildWatchAdd(kPID, _, _)).WillOnce(Return(kTag)); |
| 138 | EXPECT_TRUE(config->Start()); |
| 139 | EXPECT_EQ(kPID, config->pid_); |
| 140 | EXPECT_EQ(config.get(), DHCPProvider::GetInstance()->GetConfig(kPID).get()); |
| 141 | EXPECT_EQ(kTag, config->child_watch_tag_); |
| 142 | |
| 143 | EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 144 | config->root_ = temp_dir_.path(); |
Jorge Lucangeli Obes | 2f3169d | 2012-04-25 11:38:25 -0700 | [diff] [blame] | 145 | FilePath varrun = temp_dir_.path().Append("var/run/dhcpcd"); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 146 | EXPECT_TRUE(file_util::CreateDirectory(varrun)); |
| 147 | pid_file_ = varrun.Append(base::StringPrintf("dhcpcd-%s.pid", kDeviceName)); |
| 148 | FilePath varlib = temp_dir_.path().Append("var/lib/dhcpcd"); |
| 149 | EXPECT_TRUE(file_util::CreateDirectory(varlib)); |
| 150 | lease_file_ = |
| 151 | varlib.Append(base::StringPrintf("dhcpcd-%s.lease", kDeviceName)); |
| 152 | EXPECT_EQ(0, file_util::WriteFile(pid_file_, "", 0)); |
| 153 | EXPECT_EQ(0, file_util::WriteFile(lease_file_, "", 0)); |
| 154 | EXPECT_TRUE(file_util::PathExists(pid_file_)); |
| 155 | EXPECT_TRUE(file_util::PathExists(lease_file_)); |
| 156 | |
| 157 | return config; |
| 158 | } |
| 159 | |
| 160 | void DHCPConfigTest::StopRunningConfigAndExpect(DHCPConfigRefPtr config, |
| 161 | bool lease_file_exists) { |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 162 | DHCPConfig::ChildWatchCallback(kPID, 0, config.get()); |
| 163 | EXPECT_EQ(NULL, DHCPProvider::GetInstance()->GetConfig(kPID).get()); |
| 164 | |
| 165 | EXPECT_FALSE(file_util::PathExists(pid_file_)); |
| 166 | EXPECT_EQ(lease_file_exists, file_util::PathExists(lease_file_)); |
| 167 | } |
| 168 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 169 | TEST_F(DHCPConfigTest, GetIPv4AddressString) { |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 170 | EXPECT_EQ("255.255.255.255", config_->GetIPv4AddressString(0xffffffff)); |
| 171 | EXPECT_EQ("0.0.0.0", config_->GetIPv4AddressString(0)); |
| 172 | EXPECT_EQ("1.2.3.4", config_->GetIPv4AddressString(0x04030201)); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 175 | TEST_F(DHCPConfigTest, InitProxy) { |
| 176 | static const char kService[] = ":1.200"; |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 177 | EXPECT_TRUE(proxy_.get()); |
| 178 | EXPECT_FALSE(config_->proxy_.get()); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 179 | config_->InitProxy(kService); |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 180 | EXPECT_FALSE(proxy_.get()); |
| 181 | EXPECT_TRUE(config_->proxy_.get()); |
| 182 | |
| 183 | config_->InitProxy(kService); |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Paul Stewart | 65bcd08 | 2012-11-16 09:37:14 -0800 | [diff] [blame] | 186 | TEST_F(DHCPConfigTest, ParseClasslessStaticRoutes) { |
| 187 | const string kDefaultAddress = "0.0.0.0"; |
| 188 | const string kDefaultDestination = kDefaultAddress + "/0"; |
| 189 | const string kRouter0 = "10.0.0.254"; |
| 190 | const string kAddress1 = "192.168.1.0"; |
| 191 | const string kDestination1 = kAddress1 + "/24"; |
| 192 | // Last gateway missing, leaving an odd number of parameters. |
| 193 | const string kBrokenClasslessRoutes0 = kDefaultDestination + " " + kRouter0 + |
| 194 | " " + kDestination1; |
| 195 | IPConfig::Properties properties; |
| 196 | EXPECT_FALSE(DHCPConfig::ParseClasslessStaticRoutes(kBrokenClasslessRoutes0, |
| 197 | &properties)); |
| 198 | EXPECT_TRUE(properties.routes.empty()); |
| 199 | EXPECT_TRUE(properties.gateway.empty()); |
| 200 | |
| 201 | // Gateway argument for the second route is malformed, but we were able |
| 202 | // to salvage a default gateway. |
| 203 | const string kBrokenRouter1 = "10.0.0"; |
| 204 | const string kBrokenClasslessRoutes1 = kBrokenClasslessRoutes0 + " " + |
| 205 | kBrokenRouter1; |
| 206 | EXPECT_FALSE(DHCPConfig::ParseClasslessStaticRoutes(kBrokenClasslessRoutes1, |
| 207 | &properties)); |
| 208 | EXPECT_TRUE(properties.routes.empty()); |
| 209 | EXPECT_EQ(kRouter0, properties.gateway); |
| 210 | |
| 211 | const string kRouter1 = "10.0.0.253"; |
| 212 | const string kRouter2 = "10.0.0.252"; |
| 213 | const string kClasslessRoutes0 = kDefaultDestination + " " + kRouter2 + " " + |
| 214 | kDestination1 + " " + kRouter1; |
| 215 | EXPECT_TRUE(DHCPConfig::ParseClasslessStaticRoutes(kClasslessRoutes0, |
| 216 | &properties)); |
| 217 | // The old default route is preserved. |
| 218 | EXPECT_EQ(kRouter0, properties.gateway); |
| 219 | |
| 220 | // The two routes (including the one which would have otherwise been |
| 221 | // classified as a default route) are added to the routing table. |
| 222 | EXPECT_EQ(2, properties.routes.size()); |
| 223 | const IPConfig::Route &route0 = properties.routes[0]; |
| 224 | EXPECT_EQ(kDefaultAddress, route0.host); |
| 225 | EXPECT_EQ("0.0.0.0", route0.netmask); |
| 226 | EXPECT_EQ(kRouter2, route0.gateway); |
| 227 | |
| 228 | const IPConfig::Route &route1 = properties.routes[1]; |
| 229 | EXPECT_EQ(kAddress1, route1.host); |
| 230 | EXPECT_EQ("255.255.255.0", route1.netmask); |
| 231 | EXPECT_EQ(kRouter1, route1.gateway); |
| 232 | |
| 233 | // A malformed routing table should not affect the current table. |
| 234 | EXPECT_FALSE(DHCPConfig::ParseClasslessStaticRoutes(kBrokenClasslessRoutes1, |
| 235 | &properties)); |
| 236 | EXPECT_EQ(2, properties.routes.size()); |
| 237 | EXPECT_EQ(kRouter0, properties.gateway); |
| 238 | } |
| 239 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 240 | TEST_F(DHCPConfigTest, ParseConfiguration) { |
| 241 | DHCPConfig::Configuration conf; |
| 242 | conf[DHCPConfig::kConfigurationKeyIPAddress].writer().append_uint32( |
| 243 | 0x01020304); |
| 244 | conf[DHCPConfig::kConfigurationKeySubnetCIDR].writer().append_byte( |
| 245 | 16); |
| 246 | conf[DHCPConfig::kConfigurationKeyBroadcastAddress].writer().append_uint32( |
| 247 | 0x10203040); |
| 248 | { |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 249 | vector<unsigned int> routers; |
| 250 | routers.push_back(0x02040608); |
| 251 | routers.push_back(0x03050709); |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 252 | DBus::MessageIter writer = |
| 253 | conf[DHCPConfig::kConfigurationKeyRouters].writer(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 254 | writer << routers; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 255 | } |
| 256 | { |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 257 | vector<unsigned int> dns; |
| 258 | dns.push_back(0x09070503); |
| 259 | dns.push_back(0x08060402); |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 260 | DBus::MessageIter writer = conf[DHCPConfig::kConfigurationKeyDNS].writer(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 261 | writer << dns; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 262 | } |
| 263 | conf[DHCPConfig::kConfigurationKeyDomainName].writer().append_string( |
| 264 | "domain-name"); |
| 265 | { |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 266 | vector<string> search; |
| 267 | search.push_back("foo.com"); |
| 268 | search.push_back("bar.com"); |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 269 | DBus::MessageIter writer = |
| 270 | conf[DHCPConfig::kConfigurationKeyDomainSearch].writer(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 271 | writer << search; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 272 | } |
| 273 | conf[DHCPConfig::kConfigurationKeyMTU].writer().append_uint16(600); |
| 274 | conf["UnknownKey"] = DBus::Variant(); |
| 275 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 276 | IPConfig::Properties properties; |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 277 | ASSERT_TRUE(config_->ParseConfiguration(conf, &properties)); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 278 | EXPECT_EQ("4.3.2.1", properties.address); |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 279 | EXPECT_EQ(16, properties.subnet_prefix); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 280 | EXPECT_EQ("64.48.32.16", properties.broadcast_address); |
| 281 | EXPECT_EQ("8.6.4.2", properties.gateway); |
| 282 | ASSERT_EQ(2, properties.dns_servers.size()); |
| 283 | EXPECT_EQ("3.5.7.9", properties.dns_servers[0]); |
| 284 | EXPECT_EQ("2.4.6.8", properties.dns_servers[1]); |
| 285 | EXPECT_EQ("domain-name", properties.domain_name); |
| 286 | ASSERT_EQ(2, properties.domain_search.size()); |
| 287 | EXPECT_EQ("foo.com", properties.domain_search[0]); |
| 288 | EXPECT_EQ("bar.com", properties.domain_search[1]); |
| 289 | EXPECT_EQ(600, properties.mtu); |
| 290 | } |
| 291 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 292 | TEST_F(DHCPConfigTest, StartFail) { |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 293 | EXPECT_CALL(*minijail_, RunAndDestroy(_, _, _)).WillOnce(Return(false)); |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 294 | EXPECT_CALL(*glib(), ChildWatchAdd(_, _, _)).Times(0); |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 295 | EXPECT_FALSE(config_->Start()); |
| 296 | EXPECT_EQ(0, config_->pid_); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 297 | } |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 298 | |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 299 | MATCHER_P3(IsDHCPCDArgs, has_hostname, has_arp_gateway, has_lease_suffix, "") { |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 300 | if (string(arg[0]) != "/sbin/dhcpcd" || |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 301 | string(arg[1]) != "-B") { |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 302 | return false; |
| 303 | } |
| 304 | |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 305 | int end_offset = 2; |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 306 | if (has_hostname) { |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 307 | if (string(arg[end_offset]) != "-h" || |
| 308 | string(arg[end_offset + 1]) != kHostName) { |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 309 | return false; |
| 310 | } |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 311 | end_offset += 2; |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 314 | if (has_arp_gateway) { |
| 315 | if (string(arg[end_offset]) != "-R") |
| 316 | return false; |
| 317 | ++end_offset; |
| 318 | } |
| 319 | |
| 320 | string device_arg = has_lease_suffix ? |
| 321 | string(kDeviceName) + "=" + string(kLeaseFileSuffix) : kDeviceName; |
| 322 | return string(arg[end_offset]) == device_arg && arg[end_offset + 1] == NULL; |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | TEST_F(DHCPConfigTest, StartWithHostname) { |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 326 | EXPECT_CALL(*minijail_, RunAndDestroy(_, _, _)).WillOnce(Return(false)); |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 327 | EXPECT_FALSE(config_->Start()); |
| 328 | } |
| 329 | |
| 330 | TEST_F(DHCPConfigTest, StartWithoutHostname) { |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 331 | DHCPConfigRefPtr config = CreateMockMinijailConfig("", |
| 332 | kLeaseFileSuffix, |
| 333 | kArpGateway); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 334 | EXPECT_FALSE(config->Start()); |
| 335 | } |
| 336 | |
| 337 | TEST_F(DHCPConfigTest, StartWithoutArpGateway) { |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 338 | DHCPConfigRefPtr config = CreateMockMinijailConfig(kHostName, |
| 339 | kLeaseFileSuffix, |
| 340 | false); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 341 | EXPECT_FALSE(config->Start()); |
| 342 | } |
| 343 | |
| 344 | TEST_F(DHCPConfigTest, StartWithoutLeaseSuffix) { |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 345 | DHCPConfigRefPtr config = CreateMockMinijailConfig(kHostName, |
| 346 | kDeviceName, |
| 347 | kArpGateway); |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 348 | EXPECT_FALSE(config->Start()); |
| 349 | } |
| 350 | |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 351 | namespace { |
| 352 | |
| 353 | class UpdateCallbackTest { |
| 354 | public: |
| 355 | UpdateCallbackTest(const string &message, |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 356 | const IPConfigRefPtr &ipconfig, |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 357 | bool success) |
| 358 | : message_(message), |
| 359 | ipconfig_(ipconfig), |
| 360 | success_(success), |
| 361 | called_(false) {} |
| 362 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 363 | void Callback(const IPConfigRefPtr &ipconfig, bool success) { |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 364 | called_ = true; |
| 365 | EXPECT_EQ(ipconfig_.get(), ipconfig.get()) << message_; |
| 366 | EXPECT_EQ(success_, success) << message_; |
| 367 | } |
| 368 | |
| 369 | bool called() const { return called_; } |
| 370 | |
| 371 | private: |
| 372 | const string message_; |
| 373 | IPConfigRefPtr ipconfig_; |
| 374 | bool success_; |
| 375 | bool called_; |
| 376 | }; |
| 377 | |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 378 | void DoNothing() {} |
| 379 | |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 380 | } // namespace {} |
| 381 | |
| 382 | TEST_F(DHCPConfigTest, ProcessEventSignalFail) { |
| 383 | DHCPConfig::Configuration conf; |
| 384 | conf[DHCPConfig::kConfigurationKeyIPAddress].writer().append_uint32( |
| 385 | 0x01020304); |
| 386 | UpdateCallbackTest callback_test(DHCPConfig::kReasonFail, config_, false); |
| 387 | config_->RegisterUpdateCallback( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 388 | Bind(&UpdateCallbackTest::Callback, Unretained(&callback_test))); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 389 | config_->lease_acquisition_timeout_callback_.Reset(base::Bind(&DoNothing)); |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 390 | config_->ProcessEventSignal(DHCPConfig::kReasonFail, conf); |
| 391 | EXPECT_TRUE(callback_test.called()); |
| 392 | EXPECT_TRUE(config_->properties().address.empty()); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 393 | EXPECT_TRUE(config_->lease_acquisition_timeout_callback_.IsCancelled()); |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | TEST_F(DHCPConfigTest, ProcessEventSignalSuccess) { |
| 397 | static const char * const kReasons[] = { |
| 398 | DHCPConfig::kReasonBound, |
| 399 | DHCPConfig::kReasonRebind, |
| 400 | DHCPConfig::kReasonReboot, |
| 401 | DHCPConfig::kReasonRenew |
| 402 | }; |
| 403 | for (size_t r = 0; r < arraysize(kReasons); r++) { |
| 404 | DHCPConfig::Configuration conf; |
| 405 | string message = string(kReasons[r]) + " failed"; |
| 406 | conf[DHCPConfig::kConfigurationKeyIPAddress].writer().append_uint32(r); |
| 407 | UpdateCallbackTest callback_test(message, config_, true); |
| 408 | config_->RegisterUpdateCallback( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 409 | Bind(&UpdateCallbackTest::Callback, Unretained(&callback_test))); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 410 | config_->lease_acquisition_timeout_callback_.Reset(base::Bind(&DoNothing)); |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 411 | config_->ProcessEventSignal(kReasons[r], conf); |
| 412 | EXPECT_TRUE(callback_test.called()) << message; |
Paul Stewart | 8322440 | 2011-11-30 14:52:30 -0800 | [diff] [blame] | 413 | EXPECT_EQ(base::StringPrintf("%zu.0.0.0", r), config_->properties().address) |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 414 | << message; |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 415 | EXPECT_TRUE(config_->lease_acquisition_timeout_callback_.IsCancelled()); |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
| 419 | TEST_F(DHCPConfigTest, ProcessEventSignalUnknown) { |
| 420 | DHCPConfig::Configuration conf; |
| 421 | conf[DHCPConfig::kConfigurationKeyIPAddress].writer().append_uint32( |
| 422 | 0x01020304); |
| 423 | static const char kReasonUnknown[] = "UNKNOWN_REASON"; |
| 424 | UpdateCallbackTest callback_test(kReasonUnknown, config_, false); |
| 425 | config_->RegisterUpdateCallback( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 426 | Bind(&UpdateCallbackTest::Callback, Unretained(&callback_test))); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 427 | config_->lease_acquisition_timeout_callback_.Reset(base::Bind(&DoNothing)); |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 428 | config_->ProcessEventSignal(kReasonUnknown, conf); |
| 429 | EXPECT_FALSE(callback_test.called()); |
| 430 | EXPECT_TRUE(config_->properties().address.empty()); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 431 | EXPECT_FALSE(config_->lease_acquisition_timeout_callback_.IsCancelled()); |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 435 | TEST_F(DHCPConfigTest, ReleaseIP) { |
| 436 | config_->pid_ = 1 << 18; // Ensure unknown positive PID. |
Paul Stewart | a02ee49 | 2012-05-16 10:04:53 -0700 | [diff] [blame] | 437 | config_->arp_gateway_ = false; |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 438 | EXPECT_CALL(*proxy_, Release(kDeviceName)).Times(1); |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 439 | config_->proxy_.reset(proxy_.release()); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 440 | EXPECT_TRUE(config_->ReleaseIP()); |
| 441 | config_->pid_ = 0; |
| 442 | } |
| 443 | |
Paul Stewart | a02ee49 | 2012-05-16 10:04:53 -0700 | [diff] [blame] | 444 | TEST_F(DHCPConfigTest, ReleaseIPArpGW) { |
| 445 | config_->pid_ = 1 << 18; // Ensure unknown positive PID. |
| 446 | config_->arp_gateway_ = true; |
| 447 | EXPECT_CALL(*proxy_, Release(kDeviceName)).Times(0); |
| 448 | config_->proxy_.reset(proxy_.release()); |
| 449 | EXPECT_TRUE(config_->ReleaseIP()); |
| 450 | config_->pid_ = 0; |
| 451 | } |
| 452 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 453 | TEST_F(DHCPConfigTest, RenewIP) { |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 454 | EXPECT_TRUE(config_->lease_acquisition_timeout_callback_.IsCancelled()); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 455 | config_->pid_ = 456; |
Paul Stewart | c02344a | 2012-08-17 16:57:37 -0700 | [diff] [blame] | 456 | EXPECT_FALSE(config_->RenewIP()); // Expect no crash with NULL proxy. |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 457 | EXPECT_CALL(*proxy_, Rebind(kDeviceName)).Times(1); |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 458 | config_->proxy_.reset(proxy_.release()); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 459 | EXPECT_TRUE(config_->RenewIP()); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 460 | EXPECT_FALSE(config_->lease_acquisition_timeout_callback_.IsCancelled()); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 461 | config_->pid_ = 0; |
| 462 | } |
| 463 | |
| 464 | TEST_F(DHCPConfigTest, RequestIP) { |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 465 | EXPECT_TRUE(config_->lease_acquisition_timeout_callback_.IsCancelled()); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 466 | config_->pid_ = 567; |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 467 | EXPECT_CALL(*proxy_, Rebind(kDeviceName)).Times(1); |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 468 | config_->proxy_.reset(proxy_.release()); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 469 | EXPECT_TRUE(config_->RenewIP()); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 470 | EXPECT_FALSE(config_->lease_acquisition_timeout_callback_.IsCancelled()); |
| 471 | config_->pid_ = 0; |
| 472 | } |
| 473 | |
| 474 | TEST_F(DHCPConfigTest, RequestIPTimeout) { |
| 475 | UpdateCallbackTest callback_test(DHCPConfig::kReasonFail, config_, false); |
| 476 | config_->RegisterUpdateCallback( |
| 477 | Bind(&UpdateCallbackTest::Callback, Unretained(&callback_test))); |
| 478 | config_->lease_acquisition_timeout_seconds_ = 0; |
| 479 | config_->pid_ = 567; |
| 480 | EXPECT_CALL(*proxy_, Rebind(kDeviceName)).Times(1); |
| 481 | config_->proxy_.reset(proxy_.release()); |
| 482 | config_->RenewIP(); |
| 483 | config_->dispatcher_->DispatchPendingEvents(); |
| 484 | EXPECT_TRUE(callback_test.called()); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 485 | config_->pid_ = 0; |
| 486 | } |
| 487 | |
| 488 | TEST_F(DHCPConfigTest, Restart) { |
| 489 | const int kPID1 = 1 << 17; // Ensure unknown positive PID. |
| 490 | const int kPID2 = 987; |
| 491 | const unsigned int kTag1 = 11; |
| 492 | const unsigned int kTag2 = 22; |
| 493 | config_->pid_ = kPID1; |
| 494 | config_->child_watch_tag_ = kTag1; |
| 495 | DHCPProvider::GetInstance()->BindPID(kPID1, config_); |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 496 | EXPECT_CALL(*glib(), SourceRemove(kTag1)).WillOnce(Return(true)); |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 497 | EXPECT_CALL(*minijail_, RunAndDestroy(_, _, _)).WillOnce( |
| 498 | DoAll(SetArgumentPointee<2>(kPID2), Return(true))); |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 499 | EXPECT_CALL(*glib(), ChildWatchAdd(kPID2, _, _)).WillOnce(Return(kTag2)); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 500 | EXPECT_TRUE(config_->Restart()); |
| 501 | EXPECT_EQ(kPID2, config_->pid_); |
| 502 | EXPECT_EQ(config_.get(), DHCPProvider::GetInstance()->GetConfig(kPID2).get()); |
| 503 | EXPECT_EQ(kTag2, config_->child_watch_tag_); |
| 504 | DHCPProvider::GetInstance()->UnbindPID(kPID2); |
| 505 | config_->pid_ = 0; |
| 506 | config_->child_watch_tag_ = 0; |
| 507 | } |
| 508 | |
| 509 | TEST_F(DHCPConfigTest, RestartNoClient) { |
| 510 | const int kPID = 777; |
| 511 | const unsigned int kTag = 66; |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 512 | EXPECT_CALL(*glib(), SourceRemove(_)).Times(0); |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 513 | EXPECT_CALL(*minijail_, RunAndDestroy(_, _, _)).WillOnce( |
| 514 | DoAll(SetArgumentPointee<2>(kPID), Return(true))); |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 515 | EXPECT_CALL(*glib(), ChildWatchAdd(kPID, _, _)).WillOnce(Return(kTag)); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 516 | EXPECT_TRUE(config_->Restart()); |
| 517 | EXPECT_EQ(kPID, config_->pid_); |
| 518 | EXPECT_EQ(config_.get(), DHCPProvider::GetInstance()->GetConfig(kPID).get()); |
| 519 | EXPECT_EQ(kTag, config_->child_watch_tag_); |
| 520 | DHCPProvider::GetInstance()->UnbindPID(kPID); |
| 521 | config_->pid_ = 0; |
| 522 | config_->child_watch_tag_ = 0; |
| 523 | } |
| 524 | |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 525 | TEST_F(DHCPConfigTest, StartSuccessEphemeral) { |
| 526 | DHCPConfigRefPtr config = |
| 527 | CreateRunningConfig(kHostName, kDeviceName, kArpGateway); |
| 528 | StopRunningConfigAndExpect(config, false); |
| 529 | } |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 530 | |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 531 | TEST_F(DHCPConfigTest, StartSuccessPersistent) { |
| 532 | DHCPConfigRefPtr config = |
| 533 | CreateRunningConfig(kHostName, kLeaseFileSuffix, kArpGateway); |
| 534 | StopRunningConfigAndExpect(config, true); |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 535 | } |
| 536 | |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 537 | TEST_F(DHCPConfigTest, StartTimeout) { |
| 538 | UpdateCallbackTest callback_test(DHCPConfig::kReasonFail, config_, false); |
| 539 | config_->RegisterUpdateCallback( |
| 540 | Bind(&UpdateCallbackTest::Callback, Unretained(&callback_test))); |
| 541 | config_->lease_acquisition_timeout_seconds_ = 0; |
| 542 | config_->proxy_.reset(proxy_.release()); |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 543 | EXPECT_CALL(*minijail_, RunAndDestroy(_, _, _)).WillOnce(Return(true)); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 544 | config_->Start(); |
| 545 | config_->dispatcher_->DispatchPendingEvents(); |
| 546 | EXPECT_TRUE(callback_test.called()); |
| 547 | } |
| 548 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 549 | TEST_F(DHCPConfigTest, Stop) { |
| 550 | // Ensure no crashes. |
| 551 | const int kPID = 1 << 17; // Ensure unknown positive PID. |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 552 | config_->pid_ = kPID; |
| 553 | config_->Stop(); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 554 | EXPECT_TRUE(config_->lease_acquisition_timeout_callback_.IsCancelled()); |
| 555 | } |
| 556 | |
| 557 | TEST_F(DHCPConfigTest, StopDuringRequestIP) { |
| 558 | config_->pid_ = 567; |
| 559 | EXPECT_CALL(*proxy_, Rebind(kDeviceName)).Times(1); |
| 560 | config_->proxy_.reset(proxy_.release()); |
| 561 | EXPECT_TRUE(config_->RenewIP()); |
| 562 | EXPECT_FALSE(config_->lease_acquisition_timeout_callback_.IsCancelled()); |
| 563 | config_->pid_ = 0; // Keep Stop from killing a real process. |
| 564 | config_->Stop(); |
| 565 | EXPECT_TRUE(config_->lease_acquisition_timeout_callback_.IsCancelled()); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 566 | } |
| 567 | |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 568 | TEST_F(DHCPConfigTest, SetProperty) { |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 569 | ::DBus::Error error; |
| 570 | // 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] | 571 | EXPECT_FALSE(DBusAdaptor::SetProperty(config_->mutable_store(), |
| 572 | flimflam::kAddressProperty, |
| 573 | PropertyStoreTest::kStringV, |
| 574 | &error)); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 575 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 578 | } // namespace shill |