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 | |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 10 | #include <base/bind.h> |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 11 | #include <base/file_util.h> |
Paul Stewart | 5ad1606 | 2013-02-21 18:10:48 -0800 | [diff] [blame] | 12 | #include <base/files/scoped_temp_dir.h> |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 13 | #include <base/strings/stringprintf.h> |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 14 | #include <chromeos/dbus/service_constants.h> |
Utkarsh Sanghi | 83bd64b | 2014-07-29 16:01:43 -0700 | [diff] [blame] | 15 | #include <chromeos/minijail/mock_minijail.h> |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 16 | |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 17 | #include "shill/dbus_adaptor.h" |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 18 | #include "shill/dhcp_provider.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 19 | #include "shill/event_dispatcher.h" |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 20 | #include "shill/mock_control.h" |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 21 | #include "shill/mock_dhcp_proxy.h" |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 22 | #include "shill/mock_glib.h" |
mukesh agrawal | 6c6655d | 2012-12-06 14:49:50 -0800 | [diff] [blame] | 23 | #include "shill/mock_log.h" |
Paul Stewart | 3bdf1ab | 2014-07-17 19:22:26 -0700 | [diff] [blame] | 24 | #include "shill/mock_metrics.h" |
Ben Chan | a55469d | 2014-01-27 16:35:29 -0800 | [diff] [blame] | 25 | #include "shill/mock_proxy_factory.h" |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 26 | #include "shill/property_store_unittest.h" |
Ben Chan | a55469d | 2014-01-27 16:35:29 -0800 | [diff] [blame] | 27 | #include "shill/testing.h" |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 28 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 29 | using base::Bind; |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 30 | using base::FilePath; |
Paul Stewart | 5ad1606 | 2013-02-21 18:10:48 -0800 | [diff] [blame] | 31 | using base::ScopedTempDir; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 32 | using base::Unretained; |
Utkarsh Sanghi | 83bd64b | 2014-07-29 16:01:43 -0700 | [diff] [blame] | 33 | using chromeos::MockMinijail; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 34 | using std::string; |
| 35 | using std::vector; |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 36 | using testing::_; |
mukesh agrawal | 1835e77 | 2013-01-15 18:35:03 -0800 | [diff] [blame] | 37 | using testing::AnyNumber; |
| 38 | using testing::ContainsRegex; |
Paul Stewart | 475c072 | 2014-04-18 08:52:00 -0700 | [diff] [blame] | 39 | using testing::InvokeWithoutArgs; |
Paul Stewart | 21f7b34 | 2013-11-19 10:15:58 -0800 | [diff] [blame] | 40 | using testing::Mock; |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 41 | using testing::Return; |
| 42 | using testing::SetArgumentPointee; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 43 | using testing::Test; |
| 44 | |
| 45 | namespace shill { |
| 46 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 47 | namespace { |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 48 | const char kDeviceName[] = "eth0"; |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 49 | const char kHostName[] = "hostname"; |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 50 | const char kLeaseFileSuffix[] = "leasefilesuffix"; |
| 51 | const bool kArpGateway = true; |
Paul Stewart | 75a68b9 | 2013-10-24 10:50:27 -0700 | [diff] [blame] | 52 | const bool kHasHostname = true; |
| 53 | const bool kHasLeaseSuffix = true; |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 54 | } // namespace |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 55 | |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 56 | class DHCPConfigTest : public PropertyStoreTest { |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 57 | public: |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 58 | DHCPConfigTest() |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 59 | : proxy_(new MockDHCPProxy()), |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 60 | minijail_(new MockMinijail()), |
Paul Stewart | 3bdf1ab | 2014-07-17 19:22:26 -0700 | [diff] [blame] | 61 | metrics_(dispatcher()), |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 62 | config_(new DHCPConfig(&control_, |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 63 | dispatcher(), |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 64 | DHCPProvider::GetInstance(), |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 65 | kDeviceName, |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 66 | kHostName, |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 67 | kLeaseFileSuffix, |
| 68 | kArpGateway, |
Paul Stewart | 3bdf1ab | 2014-07-17 19:22:26 -0700 | [diff] [blame] | 69 | glib(), |
| 70 | &metrics_)) {} |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 71 | |
| 72 | virtual void SetUp() { |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 73 | config_->proxy_factory_ = &proxy_factory_; |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 74 | config_->minijail_ = minijail_.get(); |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | virtual void TearDown() { |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 78 | config_->proxy_factory_ = NULL; |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 79 | config_->minijail_ = NULL; |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 80 | } |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 81 | |
Paul Stewart | 475c072 | 2014-04-18 08:52:00 -0700 | [diff] [blame] | 82 | void StopInstance() { |
| 83 | config_->Stop("In test"); |
| 84 | } |
| 85 | |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 86 | DHCPConfigRefPtr CreateMockMinijailConfig(const string &hostname, |
Paul Stewart | 75a68b9 | 2013-10-24 10:50:27 -0700 | [diff] [blame] | 87 | const string &lease_suffix, |
Paul Stewart | b108318 | 2014-06-25 03:04:53 -0700 | [diff] [blame] | 88 | bool arp_gateway); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 89 | DHCPConfigRefPtr CreateRunningConfig(const string &hostname, |
| 90 | const string &lease_suffix, |
Paul Stewart | b108318 | 2014-06-25 03:04:53 -0700 | [diff] [blame] | 91 | bool arp_gateway); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 92 | void StopRunningConfigAndExpect(DHCPConfigRefPtr config, |
| 93 | bool lease_file_exists); |
| 94 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 95 | protected: |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 96 | static const int kPID; |
| 97 | static const unsigned int kTag; |
| 98 | |
| 99 | FilePath lease_file_; |
| 100 | FilePath pid_file_; |
| 101 | ScopedTempDir temp_dir_; |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 102 | scoped_ptr<MockDHCPProxy> proxy_; |
Ben Chan | a55469d | 2014-01-27 16:35:29 -0800 | [diff] [blame] | 103 | MockProxyFactory proxy_factory_; |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 104 | MockControl control_; |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 105 | scoped_ptr<MockMinijail> minijail_; |
Paul Stewart | 3bdf1ab | 2014-07-17 19:22:26 -0700 | [diff] [blame] | 106 | MockMetrics metrics_; |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 107 | DHCPConfigRefPtr config_; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 110 | const int DHCPConfigTest::kPID = 123456; |
| 111 | const unsigned int DHCPConfigTest::kTag = 77; |
| 112 | |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 113 | DHCPConfigRefPtr DHCPConfigTest::CreateMockMinijailConfig( |
Paul Stewart | 75a68b9 | 2013-10-24 10:50:27 -0700 | [diff] [blame] | 114 | const string &hostname, |
| 115 | const string &lease_suffix, |
Paul Stewart | b108318 | 2014-06-25 03:04:53 -0700 | [diff] [blame] | 116 | bool arp_gateway) { |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 117 | DHCPConfigRefPtr config(new DHCPConfig(&control_, |
| 118 | dispatcher(), |
| 119 | DHCPProvider::GetInstance(), |
| 120 | kDeviceName, |
| 121 | hostname, |
| 122 | lease_suffix, |
| 123 | arp_gateway, |
Paul Stewart | 3bdf1ab | 2014-07-17 19:22:26 -0700 | [diff] [blame] | 124 | glib(), |
| 125 | &metrics_)); |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 126 | config->minijail_ = minijail_.get(); |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 127 | |
| 128 | return config; |
| 129 | } |
| 130 | |
Paul Stewart | b108318 | 2014-06-25 03:04:53 -0700 | [diff] [blame] | 131 | DHCPConfigRefPtr DHCPConfigTest::CreateRunningConfig(const string &hostname, |
| 132 | const string &lease_suffix, |
| 133 | bool arp_gateway) { |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 134 | DHCPConfigRefPtr config(new DHCPConfig(&control_, |
| 135 | dispatcher(), |
| 136 | DHCPProvider::GetInstance(), |
| 137 | kDeviceName, |
| 138 | hostname, |
| 139 | lease_suffix, |
| 140 | arp_gateway, |
Paul Stewart | 3bdf1ab | 2014-07-17 19:22:26 -0700 | [diff] [blame] | 141 | glib(), |
| 142 | &metrics_)); |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 143 | config->minijail_ = minijail_.get(); |
| 144 | EXPECT_CALL(*minijail_, RunAndDestroy(_, _, _)) |
| 145 | .WillOnce(DoAll(SetArgumentPointee<2>(kPID), Return(true))); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 146 | EXPECT_CALL(*glib(), ChildWatchAdd(kPID, _, _)).WillOnce(Return(kTag)); |
| 147 | EXPECT_TRUE(config->Start()); |
| 148 | EXPECT_EQ(kPID, config->pid_); |
| 149 | EXPECT_EQ(config.get(), DHCPProvider::GetInstance()->GetConfig(kPID).get()); |
| 150 | EXPECT_EQ(kTag, config->child_watch_tag_); |
| 151 | |
| 152 | EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 153 | config->root_ = temp_dir_.path(); |
Jorge Lucangeli Obes | 2f3169d | 2012-04-25 11:38:25 -0700 | [diff] [blame] | 154 | FilePath varrun = temp_dir_.path().Append("var/run/dhcpcd"); |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 155 | EXPECT_TRUE(base::CreateDirectory(varrun)); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 156 | pid_file_ = varrun.Append(base::StringPrintf("dhcpcd-%s.pid", kDeviceName)); |
| 157 | FilePath varlib = temp_dir_.path().Append("var/lib/dhcpcd"); |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 158 | EXPECT_TRUE(base::CreateDirectory(varlib)); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 159 | lease_file_ = |
| 160 | varlib.Append(base::StringPrintf("dhcpcd-%s.lease", kDeviceName)); |
Ben Chan | 6fbf64f | 2014-05-21 18:07:01 -0700 | [diff] [blame] | 161 | EXPECT_EQ(0, base::WriteFile(pid_file_, "", 0)); |
| 162 | EXPECT_EQ(0, base::WriteFile(lease_file_, "", 0)); |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 163 | EXPECT_TRUE(base::PathExists(pid_file_)); |
| 164 | EXPECT_TRUE(base::PathExists(lease_file_)); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 165 | |
| 166 | return config; |
| 167 | } |
| 168 | |
| 169 | void DHCPConfigTest::StopRunningConfigAndExpect(DHCPConfigRefPtr config, |
| 170 | bool lease_file_exists) { |
mukesh agrawal | 6c6655d | 2012-12-06 14:49:50 -0800 | [diff] [blame] | 171 | ScopedMockLog log; |
| 172 | // We use a non-zero exit status so that we get the log message. |
| 173 | EXPECT_CALL(log, Log(_, _, ::testing::EndsWith("status 10"))); |
| 174 | DHCPConfig::ChildWatchCallback(kPID, 10, config.get()); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 175 | EXPECT_EQ(NULL, DHCPProvider::GetInstance()->GetConfig(kPID).get()); |
| 176 | |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 177 | EXPECT_FALSE(base::PathExists(pid_file_)); |
| 178 | EXPECT_EQ(lease_file_exists, base::PathExists(lease_file_)); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 181 | TEST_F(DHCPConfigTest, GetIPv4AddressString) { |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 182 | EXPECT_EQ("255.255.255.255", config_->GetIPv4AddressString(0xffffffff)); |
| 183 | EXPECT_EQ("0.0.0.0", config_->GetIPv4AddressString(0)); |
| 184 | EXPECT_EQ("1.2.3.4", config_->GetIPv4AddressString(0x04030201)); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 187 | TEST_F(DHCPConfigTest, InitProxy) { |
| 188 | static const char kService[] = ":1.200"; |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 189 | EXPECT_TRUE(proxy_.get()); |
| 190 | EXPECT_FALSE(config_->proxy_.get()); |
Ben Chan | a55469d | 2014-01-27 16:35:29 -0800 | [diff] [blame] | 191 | EXPECT_CALL(proxy_factory_, CreateDHCPProxy(kService)) |
| 192 | .WillOnce(ReturnAndReleasePointee(&proxy_)); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 193 | config_->InitProxy(kService); |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 194 | EXPECT_FALSE(proxy_.get()); |
| 195 | EXPECT_TRUE(config_->proxy_.get()); |
| 196 | |
| 197 | config_->InitProxy(kService); |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Paul Stewart | 65bcd08 | 2012-11-16 09:37:14 -0800 | [diff] [blame] | 200 | TEST_F(DHCPConfigTest, ParseClasslessStaticRoutes) { |
| 201 | const string kDefaultAddress = "0.0.0.0"; |
| 202 | const string kDefaultDestination = kDefaultAddress + "/0"; |
| 203 | const string kRouter0 = "10.0.0.254"; |
| 204 | const string kAddress1 = "192.168.1.0"; |
| 205 | const string kDestination1 = kAddress1 + "/24"; |
| 206 | // Last gateway missing, leaving an odd number of parameters. |
| 207 | const string kBrokenClasslessRoutes0 = kDefaultDestination + " " + kRouter0 + |
| 208 | " " + kDestination1; |
| 209 | IPConfig::Properties properties; |
| 210 | EXPECT_FALSE(DHCPConfig::ParseClasslessStaticRoutes(kBrokenClasslessRoutes0, |
| 211 | &properties)); |
| 212 | EXPECT_TRUE(properties.routes.empty()); |
| 213 | EXPECT_TRUE(properties.gateway.empty()); |
| 214 | |
| 215 | // Gateway argument for the second route is malformed, but we were able |
| 216 | // to salvage a default gateway. |
| 217 | const string kBrokenRouter1 = "10.0.0"; |
| 218 | const string kBrokenClasslessRoutes1 = kBrokenClasslessRoutes0 + " " + |
| 219 | kBrokenRouter1; |
| 220 | EXPECT_FALSE(DHCPConfig::ParseClasslessStaticRoutes(kBrokenClasslessRoutes1, |
| 221 | &properties)); |
| 222 | EXPECT_TRUE(properties.routes.empty()); |
| 223 | EXPECT_EQ(kRouter0, properties.gateway); |
| 224 | |
| 225 | const string kRouter1 = "10.0.0.253"; |
| 226 | const string kRouter2 = "10.0.0.252"; |
| 227 | const string kClasslessRoutes0 = kDefaultDestination + " " + kRouter2 + " " + |
| 228 | kDestination1 + " " + kRouter1; |
| 229 | EXPECT_TRUE(DHCPConfig::ParseClasslessStaticRoutes(kClasslessRoutes0, |
| 230 | &properties)); |
| 231 | // The old default route is preserved. |
| 232 | EXPECT_EQ(kRouter0, properties.gateway); |
| 233 | |
| 234 | // The two routes (including the one which would have otherwise been |
| 235 | // classified as a default route) are added to the routing table. |
| 236 | EXPECT_EQ(2, properties.routes.size()); |
| 237 | const IPConfig::Route &route0 = properties.routes[0]; |
| 238 | EXPECT_EQ(kDefaultAddress, route0.host); |
| 239 | EXPECT_EQ("0.0.0.0", route0.netmask); |
| 240 | EXPECT_EQ(kRouter2, route0.gateway); |
| 241 | |
| 242 | const IPConfig::Route &route1 = properties.routes[1]; |
| 243 | EXPECT_EQ(kAddress1, route1.host); |
| 244 | EXPECT_EQ("255.255.255.0", route1.netmask); |
| 245 | EXPECT_EQ(kRouter1, route1.gateway); |
| 246 | |
| 247 | // A malformed routing table should not affect the current table. |
| 248 | EXPECT_FALSE(DHCPConfig::ParseClasslessStaticRoutes(kBrokenClasslessRoutes1, |
| 249 | &properties)); |
| 250 | EXPECT_EQ(2, properties.routes.size()); |
| 251 | EXPECT_EQ(kRouter0, properties.gateway); |
| 252 | } |
| 253 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 254 | TEST_F(DHCPConfigTest, ParseConfiguration) { |
| 255 | DHCPConfig::Configuration conf; |
| 256 | conf[DHCPConfig::kConfigurationKeyIPAddress].writer().append_uint32( |
| 257 | 0x01020304); |
| 258 | conf[DHCPConfig::kConfigurationKeySubnetCIDR].writer().append_byte( |
| 259 | 16); |
| 260 | conf[DHCPConfig::kConfigurationKeyBroadcastAddress].writer().append_uint32( |
| 261 | 0x10203040); |
| 262 | { |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 263 | vector<unsigned int> routers; |
| 264 | routers.push_back(0x02040608); |
| 265 | routers.push_back(0x03050709); |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 266 | DBus::MessageIter writer = |
| 267 | conf[DHCPConfig::kConfigurationKeyRouters].writer(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 268 | writer << routers; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 269 | } |
| 270 | { |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 271 | vector<unsigned int> dns; |
| 272 | dns.push_back(0x09070503); |
| 273 | dns.push_back(0x08060402); |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 274 | DBus::MessageIter writer = conf[DHCPConfig::kConfigurationKeyDNS].writer(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 275 | writer << dns; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 276 | } |
| 277 | conf[DHCPConfig::kConfigurationKeyDomainName].writer().append_string( |
| 278 | "domain-name"); |
| 279 | { |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 280 | vector<string> search; |
| 281 | search.push_back("foo.com"); |
| 282 | search.push_back("bar.com"); |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 283 | DBus::MessageIter writer = |
| 284 | conf[DHCPConfig::kConfigurationKeyDomainSearch].writer(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 285 | writer << search; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 286 | } |
| 287 | conf[DHCPConfig::kConfigurationKeyMTU].writer().append_uint16(600); |
| 288 | conf["UnknownKey"] = DBus::Variant(); |
| 289 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 290 | IPConfig::Properties properties; |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 291 | ASSERT_TRUE(config_->ParseConfiguration(conf, &properties)); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 292 | EXPECT_EQ("4.3.2.1", properties.address); |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 293 | EXPECT_EQ(16, properties.subnet_prefix); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 294 | EXPECT_EQ("64.48.32.16", properties.broadcast_address); |
| 295 | EXPECT_EQ("8.6.4.2", properties.gateway); |
| 296 | ASSERT_EQ(2, properties.dns_servers.size()); |
| 297 | EXPECT_EQ("3.5.7.9", properties.dns_servers[0]); |
| 298 | EXPECT_EQ("2.4.6.8", properties.dns_servers[1]); |
| 299 | EXPECT_EQ("domain-name", properties.domain_name); |
| 300 | ASSERT_EQ(2, properties.domain_search.size()); |
| 301 | EXPECT_EQ("foo.com", properties.domain_search[0]); |
| 302 | EXPECT_EQ("bar.com", properties.domain_search[1]); |
| 303 | EXPECT_EQ(600, properties.mtu); |
| 304 | } |
| 305 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 306 | TEST_F(DHCPConfigTest, StartFail) { |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 307 | EXPECT_CALL(*minijail_, RunAndDestroy(_, _, _)).WillOnce(Return(false)); |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 308 | EXPECT_CALL(*glib(), ChildWatchAdd(_, _, _)).Times(0); |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 309 | EXPECT_FALSE(config_->Start()); |
| 310 | EXPECT_EQ(0, config_->pid_); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 311 | } |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 312 | |
Paul Stewart | b108318 | 2014-06-25 03:04:53 -0700 | [diff] [blame] | 313 | MATCHER_P3(IsDHCPCDArgs, has_hostname, has_arp_gateway, has_lease_suffix, "") { |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 314 | if (string(arg[0]) != "/sbin/dhcpcd" || |
Paul Stewart | 75a68b9 | 2013-10-24 10:50:27 -0700 | [diff] [blame] | 315 | string(arg[1]) != "-B" || |
| 316 | string(arg[2]) != "-q") { |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 317 | return false; |
| 318 | } |
| 319 | |
Paul Stewart | 75a68b9 | 2013-10-24 10:50:27 -0700 | [diff] [blame] | 320 | int end_offset = 3; |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 321 | if (has_hostname) { |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 322 | if (string(arg[end_offset]) != "-h" || |
| 323 | string(arg[end_offset + 1]) != kHostName) { |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 324 | return false; |
| 325 | } |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 326 | end_offset += 2; |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 329 | if (has_arp_gateway) { |
Paul Stewart | 75a68b9 | 2013-10-24 10:50:27 -0700 | [diff] [blame] | 330 | if (string(arg[end_offset]) != "-R" || |
| 331 | string(arg[end_offset + 1]) != "-U") { |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 332 | return false; |
Paul Stewart | 75a68b9 | 2013-10-24 10:50:27 -0700 | [diff] [blame] | 333 | } |
| 334 | end_offset += 2; |
| 335 | } |
| 336 | |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 337 | string device_arg = has_lease_suffix ? |
| 338 | string(kDeviceName) + "=" + string(kLeaseFileSuffix) : kDeviceName; |
| 339 | return string(arg[end_offset]) == device_arg && arg[end_offset + 1] == NULL; |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | TEST_F(DHCPConfigTest, StartWithHostname) { |
Paul Stewart | 75a68b9 | 2013-10-24 10:50:27 -0700 | [diff] [blame] | 343 | EXPECT_CALL(*minijail_, RunAndDestroy(_, IsDHCPCDArgs(kHasHostname, |
| 344 | kArpGateway, |
Paul Stewart | b108318 | 2014-06-25 03:04:53 -0700 | [diff] [blame] | 345 | kHasLeaseSuffix), _)) |
Paul Stewart | 75a68b9 | 2013-10-24 10:50:27 -0700 | [diff] [blame] | 346 | .WillOnce(Return(false)); |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 347 | EXPECT_FALSE(config_->Start()); |
| 348 | } |
| 349 | |
| 350 | TEST_F(DHCPConfigTest, StartWithoutHostname) { |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 351 | DHCPConfigRefPtr config = CreateMockMinijailConfig("", |
| 352 | kLeaseFileSuffix, |
Paul Stewart | b108318 | 2014-06-25 03:04:53 -0700 | [diff] [blame] | 353 | kArpGateway); |
Paul Stewart | 75a68b9 | 2013-10-24 10:50:27 -0700 | [diff] [blame] | 354 | EXPECT_CALL(*minijail_, RunAndDestroy(_, IsDHCPCDArgs(!kHasHostname, |
| 355 | kArpGateway, |
Paul Stewart | b108318 | 2014-06-25 03:04:53 -0700 | [diff] [blame] | 356 | kHasLeaseSuffix), _)) |
Paul Stewart | 75a68b9 | 2013-10-24 10:50:27 -0700 | [diff] [blame] | 357 | .WillOnce(Return(false)); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 358 | EXPECT_FALSE(config->Start()); |
| 359 | } |
| 360 | |
| 361 | TEST_F(DHCPConfigTest, StartWithoutArpGateway) { |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 362 | DHCPConfigRefPtr config = CreateMockMinijailConfig(kHostName, |
| 363 | kLeaseFileSuffix, |
Paul Stewart | b108318 | 2014-06-25 03:04:53 -0700 | [diff] [blame] | 364 | !kArpGateway); |
Paul Stewart | 75a68b9 | 2013-10-24 10:50:27 -0700 | [diff] [blame] | 365 | EXPECT_CALL(*minijail_, RunAndDestroy(_, IsDHCPCDArgs(kHasHostname, |
| 366 | !kArpGateway, |
Paul Stewart | b108318 | 2014-06-25 03:04:53 -0700 | [diff] [blame] | 367 | kHasLeaseSuffix), _)) |
Paul Stewart | 75a68b9 | 2013-10-24 10:50:27 -0700 | [diff] [blame] | 368 | .WillOnce(Return(false)); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 369 | EXPECT_FALSE(config->Start()); |
| 370 | } |
| 371 | |
| 372 | TEST_F(DHCPConfigTest, StartWithoutLeaseSuffix) { |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 373 | DHCPConfigRefPtr config = CreateMockMinijailConfig(kHostName, |
| 374 | kDeviceName, |
Paul Stewart | b108318 | 2014-06-25 03:04:53 -0700 | [diff] [blame] | 375 | kArpGateway); |
Paul Stewart | 75a68b9 | 2013-10-24 10:50:27 -0700 | [diff] [blame] | 376 | EXPECT_CALL(*minijail_, RunAndDestroy(_, IsDHCPCDArgs(kHasHostname, |
| 377 | kArpGateway, |
Paul Stewart | b108318 | 2014-06-25 03:04:53 -0700 | [diff] [blame] | 378 | !kHasLeaseSuffix), _)) |
Paul Stewart | 75a68b9 | 2013-10-24 10:50:27 -0700 | [diff] [blame] | 379 | .WillOnce(Return(false)); |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 380 | EXPECT_FALSE(config->Start()); |
| 381 | } |
| 382 | |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 383 | namespace { |
| 384 | |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 385 | class DHCPConfigCallbackTest : public DHCPConfigTest { |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 386 | public: |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 387 | virtual void SetUp() { |
| 388 | DHCPConfigTest::SetUp(); |
| 389 | config_->RegisterUpdateCallback( |
| 390 | Bind(&DHCPConfigCallbackTest::SuccessCallback, Unretained(this))); |
| 391 | config_->RegisterFailureCallback( |
| 392 | Bind(&DHCPConfigCallbackTest::FailureCallback, Unretained(this))); |
| 393 | ip_config_ = config_; |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 396 | MOCK_METHOD1(SuccessCallback, void(const IPConfigRefPtr &ipconfig)); |
| 397 | MOCK_METHOD1(FailureCallback, void(const IPConfigRefPtr &ipconfig)); |
| 398 | |
| 399 | // The mock methods above take IPConfigRefPtr because this is the type |
| 400 | // that the registered callbacks take. This conversion of the DHCP |
| 401 | // config ref pointer eases our work in setting up expectations. |
| 402 | const IPConfigRefPtr &ConfigRef() { return ip_config_; } |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 403 | |
| 404 | private: |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 405 | IPConfigRefPtr ip_config_; |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 406 | }; |
| 407 | |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 408 | void DoNothing() {} |
| 409 | |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 410 | } // namespace |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 411 | |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 412 | TEST_F(DHCPConfigCallbackTest, ProcessEventSignalFail) { |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 413 | DHCPConfig::Configuration conf; |
| 414 | conf[DHCPConfig::kConfigurationKeyIPAddress].writer().append_uint32( |
| 415 | 0x01020304); |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 416 | EXPECT_CALL(*this, SuccessCallback(_)).Times(0); |
| 417 | EXPECT_CALL(*this, FailureCallback(ConfigRef())); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 418 | config_->lease_acquisition_timeout_callback_.Reset(base::Bind(&DoNothing)); |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 419 | config_->lease_expiration_callback_.Reset(base::Bind(&DoNothing)); |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 420 | config_->ProcessEventSignal(DHCPConfig::kReasonFail, conf); |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 421 | Mock::VerifyAndClearExpectations(this); |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 422 | EXPECT_TRUE(config_->properties().address.empty()); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 423 | EXPECT_TRUE(config_->lease_acquisition_timeout_callback_.IsCancelled()); |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 424 | EXPECT_TRUE(config_->lease_expiration_callback_.IsCancelled()); |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 427 | TEST_F(DHCPConfigCallbackTest, ProcessEventSignalSuccess) { |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 428 | for (const auto &reason : { DHCPConfig::kReasonBound, |
| 429 | DHCPConfig::kReasonRebind, |
| 430 | DHCPConfig::kReasonReboot, |
| 431 | DHCPConfig::kReasonRenew }) { |
| 432 | int address_octet = 0; |
| 433 | for (const auto lease_time_given : { false, true }) { |
| 434 | DHCPConfig::Configuration conf; |
| 435 | conf[DHCPConfig::kConfigurationKeyIPAddress].writer().append_uint32( |
| 436 | ++address_octet); |
| 437 | if (lease_time_given) { |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 438 | const uint32_t kLeaseTime = 1; |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 439 | conf[DHCPConfig::kConfigurationKeyLeaseTime].writer().append_uint32( |
| 440 | kLeaseTime); |
| 441 | config_->lease_expiration_callback_.Cancel(); |
| 442 | } else { |
| 443 | config_->lease_expiration_callback_.Reset(base::Bind(&DoNothing)); |
| 444 | } |
| 445 | config_->lease_acquisition_timeout_callback_.Reset( |
| 446 | base::Bind(&DoNothing)); |
| 447 | EXPECT_CALL(*this, SuccessCallback(ConfigRef())); |
| 448 | EXPECT_CALL(*this, FailureCallback(_)).Times(0); |
| 449 | config_->ProcessEventSignal(reason, conf); |
| 450 | string failure_message = string(reason) + " failed with lease time " + |
| 451 | (lease_time_given ? "given" : "not given"); |
| 452 | EXPECT_TRUE(Mock::VerifyAndClearExpectations(this)) << failure_message; |
| 453 | EXPECT_EQ(base::StringPrintf("%d.0.0.0", address_octet), |
| 454 | config_->properties().address) << failure_message; |
| 455 | EXPECT_TRUE(config_->lease_acquisition_timeout_callback_.IsCancelled()) |
| 456 | << failure_message; |
| 457 | // With no lease time given, the expiration callback will be cancelled. |
| 458 | // With a lease time given, the expiration callback should be started. |
| 459 | EXPECT_EQ(!lease_time_given, |
| 460 | config_->lease_expiration_callback_.IsCancelled()) |
| 461 | << failure_message; |
| 462 | } |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | |
Paul Stewart | 475c072 | 2014-04-18 08:52:00 -0700 | [diff] [blame] | 466 | TEST_F(DHCPConfigCallbackTest, StoppedDuringFailureCallback) { |
| 467 | DHCPConfig::Configuration conf; |
| 468 | conf[DHCPConfig::kConfigurationKeyIPAddress].writer().append_uint32( |
| 469 | 0x01020304); |
| 470 | // Stop the DHCP config while it is calling the failure callback. We |
| 471 | // need to ensure that no callbacks are left running inadvertently as |
| 472 | // a result. |
| 473 | EXPECT_CALL(*this, FailureCallback(ConfigRef())) |
| 474 | .WillOnce(InvokeWithoutArgs(this, &DHCPConfigTest::StopInstance)); |
| 475 | config_->ProcessEventSignal(DHCPConfig::kReasonFail, conf); |
| 476 | EXPECT_TRUE(Mock::VerifyAndClearExpectations(this)); |
| 477 | EXPECT_TRUE(config_->lease_acquisition_timeout_callback_.IsCancelled()); |
| 478 | EXPECT_TRUE(config_->lease_expiration_callback_.IsCancelled()); |
| 479 | } |
| 480 | |
| 481 | TEST_F(DHCPConfigCallbackTest, StoppedDuringSuccessCallback) { |
| 482 | DHCPConfig::Configuration conf; |
| 483 | conf[DHCPConfig::kConfigurationKeyIPAddress].writer().append_uint32( |
| 484 | 0x01020304); |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 485 | const uint32_t kLeaseTime = 1; |
Paul Stewart | 475c072 | 2014-04-18 08:52:00 -0700 | [diff] [blame] | 486 | conf[DHCPConfig::kConfigurationKeyLeaseTime].writer().append_uint32( |
| 487 | kLeaseTime); |
| 488 | // Stop the DHCP config while it is calling the success callback. This |
| 489 | // can happen if the device has a static IP configuration and releases |
| 490 | // the lease after accepting other network parameters from the DHCP |
| 491 | // IPConfig properties. We need to ensure that no callbacks are left |
| 492 | // running inadvertently as a result. |
| 493 | EXPECT_CALL(*this, SuccessCallback(ConfigRef())) |
| 494 | .WillOnce(InvokeWithoutArgs(this, &DHCPConfigTest::StopInstance)); |
| 495 | config_->ProcessEventSignal(DHCPConfig::kReasonBound, conf); |
| 496 | EXPECT_TRUE(Mock::VerifyAndClearExpectations(this)); |
| 497 | EXPECT_TRUE(config_->lease_acquisition_timeout_callback_.IsCancelled()); |
| 498 | EXPECT_TRUE(config_->lease_expiration_callback_.IsCancelled()); |
| 499 | } |
| 500 | |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 501 | TEST_F(DHCPConfigCallbackTest, ProcessEventSignalUnknown) { |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 502 | DHCPConfig::Configuration conf; |
| 503 | conf[DHCPConfig::kConfigurationKeyIPAddress].writer().append_uint32( |
| 504 | 0x01020304); |
| 505 | static const char kReasonUnknown[] = "UNKNOWN_REASON"; |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 506 | EXPECT_CALL(*this, SuccessCallback(_)).Times(0); |
| 507 | EXPECT_CALL(*this, FailureCallback(_)).Times(0); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 508 | config_->lease_acquisition_timeout_callback_.Reset(base::Bind(&DoNothing)); |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 509 | config_->ProcessEventSignal(kReasonUnknown, conf); |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 510 | Mock::VerifyAndClearExpectations(this); |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 511 | EXPECT_TRUE(config_->properties().address.empty()); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 512 | EXPECT_FALSE(config_->lease_acquisition_timeout_callback_.IsCancelled()); |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 515 | TEST_F(DHCPConfigCallbackTest, ProcessEventSignalGatewayArp) { |
Paul Stewart | 94571f1 | 2013-09-10 17:26:07 -0700 | [diff] [blame] | 516 | DHCPConfig::Configuration conf; |
| 517 | conf[DHCPConfig::kConfigurationKeyIPAddress].writer().append_uint32( |
| 518 | 0x01020304); |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 519 | EXPECT_CALL(*this, SuccessCallback(ConfigRef())); |
| 520 | EXPECT_CALL(*this, FailureCallback(_)).Times(0); |
Paul Stewart | 94571f1 | 2013-09-10 17:26:07 -0700 | [diff] [blame] | 521 | EXPECT_CALL(*minijail_, RunAndDestroy(_, _, _)).WillOnce(Return(true)); |
| 522 | config_->Start(); |
| 523 | config_->ProcessEventSignal(DHCPConfig::kReasonGatewayArp, conf); |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 524 | Mock::VerifyAndClearExpectations(this); |
Paul Stewart | 94571f1 | 2013-09-10 17:26:07 -0700 | [diff] [blame] | 525 | EXPECT_EQ("4.3.2.1", config_->properties().address); |
| 526 | EXPECT_FALSE(config_->lease_acquisition_timeout_callback_.IsCancelled()); |
| 527 | EXPECT_TRUE(config_->is_gateway_arp_active_); |
| 528 | |
| 529 | // If the timeout gets called, we shouldn't lose the lease since GatewayArp |
| 530 | // is active. |
| 531 | ScopedMockLog log; |
| 532 | EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber()); |
| 533 | EXPECT_CALL(log, Log(_, _, ::testing::EndsWith( |
| 534 | "Continuing to use our previous lease, due to gateway-ARP."))); |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 535 | EXPECT_CALL(*this, SuccessCallback(_)).Times(0); |
| 536 | EXPECT_CALL(*this, FailureCallback(_)).Times(0); |
Paul Stewart | 94571f1 | 2013-09-10 17:26:07 -0700 | [diff] [blame] | 537 | config_->lease_acquisition_timeout_callback_.callback().Run(); |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 538 | Mock::VerifyAndClearExpectations(this); |
Paul Stewart | 94571f1 | 2013-09-10 17:26:07 -0700 | [diff] [blame] | 539 | EXPECT_TRUE(config_->is_gateway_arp_active_); |
| 540 | |
| 541 | // An official reply from a DHCP server should reset our GatewayArp state. |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 542 | EXPECT_CALL(*this, SuccessCallback(ConfigRef())); |
| 543 | EXPECT_CALL(*this, FailureCallback(_)).Times(0); |
Paul Stewart | 94571f1 | 2013-09-10 17:26:07 -0700 | [diff] [blame] | 544 | config_->ProcessEventSignal(DHCPConfig::kReasonRenew, conf); |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 545 | Mock::VerifyAndClearExpectations(this); |
Paul Stewart | 94571f1 | 2013-09-10 17:26:07 -0700 | [diff] [blame] | 546 | EXPECT_FALSE(config_->is_gateway_arp_active_); |
| 547 | } |
| 548 | |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 549 | TEST_F(DHCPConfigCallbackTest, ProcessEventSignalGatewayArpNak) { |
Paul Stewart | 94571f1 | 2013-09-10 17:26:07 -0700 | [diff] [blame] | 550 | DHCPConfig::Configuration conf; |
| 551 | conf[DHCPConfig::kConfigurationKeyIPAddress].writer().append_uint32( |
| 552 | 0x01020304); |
Paul Stewart | 94571f1 | 2013-09-10 17:26:07 -0700 | [diff] [blame] | 553 | EXPECT_CALL(*minijail_, RunAndDestroy(_, _, _)).WillOnce(Return(true)); |
| 554 | config_->Start(); |
| 555 | config_->ProcessEventSignal(DHCPConfig::kReasonGatewayArp, conf); |
| 556 | EXPECT_TRUE(config_->is_gateway_arp_active_); |
| 557 | |
| 558 | // Sending a NAK should clear is_gateway_arp_active_. |
| 559 | config_->ProcessEventSignal(DHCPConfig::kReasonNak, conf); |
| 560 | EXPECT_FALSE(config_->is_gateway_arp_active_); |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 561 | Mock::VerifyAndClearExpectations(this); |
Paul Stewart | 94571f1 | 2013-09-10 17:26:07 -0700 | [diff] [blame] | 562 | |
| 563 | // If the timeout gets called, we should lose the lease since GatewayArp |
| 564 | // is not active any more. |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 565 | EXPECT_CALL(*this, SuccessCallback(_)).Times(0); |
| 566 | EXPECT_CALL(*this, FailureCallback(ConfigRef())); |
Paul Stewart | 94571f1 | 2013-09-10 17:26:07 -0700 | [diff] [blame] | 567 | config_->lease_acquisition_timeout_callback_.callback().Run(); |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 568 | Mock::VerifyAndClearExpectations(this); |
Paul Stewart | 94571f1 | 2013-09-10 17:26:07 -0700 | [diff] [blame] | 569 | } |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 570 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 571 | TEST_F(DHCPConfigTest, ReleaseIP) { |
| 572 | config_->pid_ = 1 << 18; // Ensure unknown positive PID. |
Paul Stewart | a02ee49 | 2012-05-16 10:04:53 -0700 | [diff] [blame] | 573 | config_->arp_gateway_ = false; |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 574 | EXPECT_CALL(*proxy_, Release(kDeviceName)).Times(1); |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 575 | config_->proxy_.reset(proxy_.release()); |
Paul Stewart | 217c61d | 2013-06-13 15:12:02 -0700 | [diff] [blame] | 576 | EXPECT_TRUE(config_->ReleaseIP(IPConfig::kReleaseReasonDisconnect)); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 577 | config_->pid_ = 0; |
| 578 | } |
| 579 | |
Paul Stewart | a02ee49 | 2012-05-16 10:04:53 -0700 | [diff] [blame] | 580 | TEST_F(DHCPConfigTest, ReleaseIPArpGW) { |
| 581 | config_->pid_ = 1 << 18; // Ensure unknown positive PID. |
| 582 | config_->arp_gateway_ = true; |
| 583 | EXPECT_CALL(*proxy_, Release(kDeviceName)).Times(0); |
| 584 | config_->proxy_.reset(proxy_.release()); |
Paul Stewart | 217c61d | 2013-06-13 15:12:02 -0700 | [diff] [blame] | 585 | EXPECT_TRUE(config_->ReleaseIP(IPConfig::kReleaseReasonDisconnect)); |
| 586 | config_->pid_ = 0; |
| 587 | } |
| 588 | |
| 589 | TEST_F(DHCPConfigTest, ReleaseIPStaticIPWithLease) { |
| 590 | config_->pid_ = 1 << 18; // Ensure unknown positive PID. |
| 591 | config_->arp_gateway_ = true; |
| 592 | config_->is_lease_active_ = true; |
| 593 | EXPECT_CALL(*proxy_, Release(kDeviceName)); |
| 594 | config_->proxy_.reset(proxy_.release()); |
| 595 | EXPECT_TRUE(config_->ReleaseIP(IPConfig::kReleaseReasonStaticIP)); |
| 596 | EXPECT_EQ(NULL, config_->proxy_.get()); |
| 597 | config_->pid_ = 0; |
| 598 | } |
| 599 | |
| 600 | TEST_F(DHCPConfigTest, ReleaseIPStaticIPWithoutLease) { |
| 601 | config_->pid_ = 1 << 18; // Ensure unknown positive PID. |
| 602 | config_->arp_gateway_ = true; |
| 603 | config_->is_lease_active_ = false; |
| 604 | EXPECT_CALL(*proxy_, Release(kDeviceName)).Times(0); |
| 605 | MockDHCPProxy *proxy_pointer = proxy_.get(); |
| 606 | config_->proxy_.reset(proxy_.release()); |
| 607 | EXPECT_TRUE(config_->ReleaseIP(IPConfig::kReleaseReasonStaticIP)); |
| 608 | // Expect that proxy has not been released. |
| 609 | EXPECT_EQ(proxy_pointer, config_->proxy_.get()); |
Paul Stewart | a02ee49 | 2012-05-16 10:04:53 -0700 | [diff] [blame] | 610 | config_->pid_ = 0; |
| 611 | } |
| 612 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 613 | TEST_F(DHCPConfigTest, RenewIP) { |
Paul Stewart | 21f7b34 | 2013-11-19 10:15:58 -0800 | [diff] [blame] | 614 | EXPECT_CALL(*minijail_, RunAndDestroy(_, _, _)).WillOnce(Return(false)); |
| 615 | config_->pid_ = 0; |
| 616 | EXPECT_FALSE(config_->RenewIP()); // Expect a call to Start() if pid_ is 0. |
| 617 | Mock::VerifyAndClearExpectations(minijail_.get()); |
| 618 | EXPECT_CALL(*minijail_, RunAndDestroy(_, _, _)).Times(0); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 619 | EXPECT_TRUE(config_->lease_acquisition_timeout_callback_.IsCancelled()); |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 620 | config_->lease_expiration_callback_.Reset(base::Bind(&DoNothing)); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 621 | config_->pid_ = 456; |
Paul Stewart | c02344a | 2012-08-17 16:57:37 -0700 | [diff] [blame] | 622 | EXPECT_FALSE(config_->RenewIP()); // Expect no crash with NULL proxy. |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 623 | EXPECT_CALL(*proxy_, Rebind(kDeviceName)).Times(1); |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 624 | config_->proxy_.reset(proxy_.release()); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 625 | EXPECT_TRUE(config_->RenewIP()); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 626 | EXPECT_FALSE(config_->lease_acquisition_timeout_callback_.IsCancelled()); |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 627 | EXPECT_TRUE(config_->lease_expiration_callback_.IsCancelled()); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 628 | config_->pid_ = 0; |
| 629 | } |
| 630 | |
| 631 | TEST_F(DHCPConfigTest, RequestIP) { |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 632 | EXPECT_TRUE(config_->lease_acquisition_timeout_callback_.IsCancelled()); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 633 | config_->pid_ = 567; |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 634 | EXPECT_CALL(*proxy_, Rebind(kDeviceName)).Times(1); |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 635 | config_->proxy_.reset(proxy_.release()); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 636 | EXPECT_TRUE(config_->RenewIP()); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 637 | EXPECT_FALSE(config_->lease_acquisition_timeout_callback_.IsCancelled()); |
| 638 | config_->pid_ = 0; |
| 639 | } |
| 640 | |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 641 | TEST_F(DHCPConfigCallbackTest, RequestIPTimeout) { |
| 642 | EXPECT_CALL(*this, SuccessCallback(_)).Times(0); |
| 643 | EXPECT_CALL(*this, FailureCallback(ConfigRef())); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 644 | config_->lease_acquisition_timeout_seconds_ = 0; |
| 645 | config_->pid_ = 567; |
| 646 | EXPECT_CALL(*proxy_, Rebind(kDeviceName)).Times(1); |
| 647 | config_->proxy_.reset(proxy_.release()); |
| 648 | config_->RenewIP(); |
| 649 | config_->dispatcher_->DispatchPendingEvents(); |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 650 | Mock::VerifyAndClearExpectations(this); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 651 | config_->pid_ = 0; |
| 652 | } |
| 653 | |
| 654 | TEST_F(DHCPConfigTest, Restart) { |
| 655 | const int kPID1 = 1 << 17; // Ensure unknown positive PID. |
| 656 | const int kPID2 = 987; |
| 657 | const unsigned int kTag1 = 11; |
| 658 | const unsigned int kTag2 = 22; |
| 659 | config_->pid_ = kPID1; |
| 660 | config_->child_watch_tag_ = kTag1; |
| 661 | DHCPProvider::GetInstance()->BindPID(kPID1, config_); |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 662 | EXPECT_CALL(*glib(), SourceRemove(kTag1)).WillOnce(Return(true)); |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 663 | EXPECT_CALL(*minijail_, RunAndDestroy(_, _, _)).WillOnce( |
| 664 | DoAll(SetArgumentPointee<2>(kPID2), Return(true))); |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 665 | EXPECT_CALL(*glib(), ChildWatchAdd(kPID2, _, _)).WillOnce(Return(kTag2)); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 666 | EXPECT_TRUE(config_->Restart()); |
| 667 | EXPECT_EQ(kPID2, config_->pid_); |
| 668 | EXPECT_EQ(config_.get(), DHCPProvider::GetInstance()->GetConfig(kPID2).get()); |
| 669 | EXPECT_EQ(kTag2, config_->child_watch_tag_); |
| 670 | DHCPProvider::GetInstance()->UnbindPID(kPID2); |
| 671 | config_->pid_ = 0; |
| 672 | config_->child_watch_tag_ = 0; |
| 673 | } |
| 674 | |
| 675 | TEST_F(DHCPConfigTest, RestartNoClient) { |
| 676 | const int kPID = 777; |
| 677 | const unsigned int kTag = 66; |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 678 | EXPECT_CALL(*glib(), SourceRemove(_)).Times(0); |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 679 | EXPECT_CALL(*minijail_, RunAndDestroy(_, _, _)).WillOnce( |
| 680 | DoAll(SetArgumentPointee<2>(kPID), Return(true))); |
Chris Masone | 2176a88 | 2011-09-14 22:29:15 -0700 | [diff] [blame] | 681 | EXPECT_CALL(*glib(), ChildWatchAdd(kPID, _, _)).WillOnce(Return(kTag)); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 682 | EXPECT_TRUE(config_->Restart()); |
| 683 | EXPECT_EQ(kPID, config_->pid_); |
| 684 | EXPECT_EQ(config_.get(), DHCPProvider::GetInstance()->GetConfig(kPID).get()); |
| 685 | EXPECT_EQ(kTag, config_->child_watch_tag_); |
| 686 | DHCPProvider::GetInstance()->UnbindPID(kPID); |
| 687 | config_->pid_ = 0; |
| 688 | config_->child_watch_tag_ = 0; |
| 689 | } |
| 690 | |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 691 | TEST_F(DHCPConfigTest, StartSuccessEphemeral) { |
| 692 | DHCPConfigRefPtr config = |
Paul Stewart | b108318 | 2014-06-25 03:04:53 -0700 | [diff] [blame] | 693 | CreateRunningConfig(kHostName, kDeviceName, kArpGateway); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 694 | StopRunningConfigAndExpect(config, false); |
| 695 | } |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 696 | |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 697 | TEST_F(DHCPConfigTest, StartSuccessPersistent) { |
| 698 | DHCPConfigRefPtr config = |
Paul Stewart | b108318 | 2014-06-25 03:04:53 -0700 | [diff] [blame] | 699 | CreateRunningConfig(kHostName, kLeaseFileSuffix, kArpGateway); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 700 | StopRunningConfigAndExpect(config, true); |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 701 | } |
| 702 | |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 703 | TEST_F(DHCPConfigCallbackTest, StartTimeout) { |
| 704 | EXPECT_CALL(*this, SuccessCallback(_)).Times(0); |
| 705 | EXPECT_CALL(*this, FailureCallback(ConfigRef())); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 706 | config_->lease_acquisition_timeout_seconds_ = 0; |
| 707 | config_->proxy_.reset(proxy_.release()); |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 708 | EXPECT_CALL(*minijail_, RunAndDestroy(_, _, _)).WillOnce(Return(true)); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 709 | config_->Start(); |
| 710 | config_->dispatcher_->DispatchPendingEvents(); |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 711 | Mock::VerifyAndClearExpectations(this); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 714 | TEST_F(DHCPConfigTest, Stop) { |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 715 | const int kPID = 1 << 17; // Ensure unknown positive PID. |
mukesh agrawal | 1835e77 | 2013-01-15 18:35:03 -0800 | [diff] [blame] | 716 | ScopedMockLog log; |
| 717 | EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber()); |
| 718 | EXPECT_CALL(log, Log(_, _, ContainsRegex( |
Darin Petkov | 3fe1766 | 2013-02-04 14:19:08 +0100 | [diff] [blame] | 719 | base::StringPrintf("Stopping.+%s", __func__)))); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 720 | config_->pid_ = kPID; |
Darin Petkov | 3fe1766 | 2013-02-04 14:19:08 +0100 | [diff] [blame] | 721 | DHCPProvider::GetInstance()->BindPID(kPID, config_); |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 722 | config_->lease_acquisition_timeout_callback_.Reset(base::Bind(&DoNothing)); |
| 723 | config_->lease_expiration_callback_.Reset(base::Bind(&DoNothing)); |
mukesh agrawal | 1835e77 | 2013-01-15 18:35:03 -0800 | [diff] [blame] | 724 | config_->Stop(__func__); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 725 | EXPECT_TRUE(config_->lease_acquisition_timeout_callback_.IsCancelled()); |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 726 | EXPECT_TRUE(config_->lease_expiration_callback_.IsCancelled()); |
Darin Petkov | 3fe1766 | 2013-02-04 14:19:08 +0100 | [diff] [blame] | 727 | EXPECT_FALSE(DHCPProvider::GetInstance()->GetConfig(kPID)); |
| 728 | EXPECT_FALSE(config_->pid_); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | TEST_F(DHCPConfigTest, StopDuringRequestIP) { |
| 732 | config_->pid_ = 567; |
| 733 | EXPECT_CALL(*proxy_, Rebind(kDeviceName)).Times(1); |
| 734 | config_->proxy_.reset(proxy_.release()); |
| 735 | EXPECT_TRUE(config_->RenewIP()); |
| 736 | EXPECT_FALSE(config_->lease_acquisition_timeout_callback_.IsCancelled()); |
| 737 | config_->pid_ = 0; // Keep Stop from killing a real process. |
mukesh agrawal | 1835e77 | 2013-01-15 18:35:03 -0800 | [diff] [blame] | 738 | config_->Stop(__func__); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 739 | EXPECT_TRUE(config_->lease_acquisition_timeout_callback_.IsCancelled()); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 740 | } |
| 741 | |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 742 | TEST_F(DHCPConfigTest, SetProperty) { |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 743 | ::DBus::Error error; |
| 744 | // 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] | 745 | EXPECT_FALSE(DBusAdaptor::SetProperty(config_->mutable_store(), |
Ben Chan | 0295e0f | 2013-09-20 13:47:29 -0700 | [diff] [blame] | 746 | kAddressProperty, |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 747 | PropertyStoreTest::kStringV, |
| 748 | &error)); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 749 | ASSERT_TRUE(error.is_set()); // name() may be invalid otherwise |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 750 | EXPECT_EQ(invalid_args(), error.name()); |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 751 | } |
| 752 | |
Paul Stewart | 3bdf1ab | 2014-07-17 19:22:26 -0700 | [diff] [blame] | 753 | TEST_F(DHCPConfigTest, ProcessStatusChangeSingal) { |
| 754 | EXPECT_CALL(metrics_, NotifyDhcpClientStatus( |
| 755 | Metrics::kDhcpClientStatusBound)); |
| 756 | config_->ProcessStatusChangeSignal(DHCPConfig::kStatusBound); |
| 757 | } |
| 758 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 759 | } // namespace shill |