Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "shill/l2tp_ipsec_driver.h" |
| 6 | |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 7 | #include <base/file_util.h> |
| 8 | #include <base/scoped_temp_dir.h> |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 9 | #include <base/string_util.h> |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 10 | #include <gtest/gtest.h> |
| 11 | |
| 12 | #include "shill/event_dispatcher.h" |
| 13 | #include "shill/nice_mock_control.h" |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 14 | #include "shill/mock_adaptors.h" |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 15 | #include "shill/mock_device_info.h" |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 16 | #include "shill/mock_glib.h" |
| 17 | #include "shill/mock_manager.h" |
| 18 | #include "shill/mock_metrics.h" |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 19 | #include "shill/mock_nss.h" |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 20 | #include "shill/mock_process_killer.h" |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 21 | #include "shill/mock_vpn.h" |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 22 | #include "shill/mock_vpn_service.h" |
Paul Stewart | 8e7e459 | 2012-04-29 09:47:48 -0700 | [diff] [blame] | 23 | #include "shill/property_store_inspector.h" |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 24 | #include "shill/vpn.h" |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 25 | |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 26 | using std::find; |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 27 | using std::map; |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 28 | using std::string; |
| 29 | using std::vector; |
| 30 | using testing::_; |
| 31 | using testing::ElementsAreArray; |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 32 | using testing::NiceMock; |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 33 | using testing::Return; |
| 34 | using testing::ReturnRef; |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 35 | using testing::SetArgumentPointee; |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 36 | using testing::StrictMock; |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 37 | |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 38 | namespace shill { |
| 39 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 40 | class L2TPIPSecDriverTest : public testing::Test, |
| 41 | public RPCTaskDelegate { |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 42 | public: |
| 43 | L2TPIPSecDriverTest() |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 44 | : device_info_(&control_, &dispatcher_, &metrics_, &manager_), |
| 45 | manager_(&control_, &dispatcher_, &metrics_, &glib_), |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 46 | driver_(new L2TPIPSecDriver(&control_, &dispatcher_, &metrics_, |
| 47 | &manager_, &device_info_, &glib_)), |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 48 | service_(new MockVPNService(&control_, &dispatcher_, &metrics_, |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 49 | &manager_, driver_)), |
| 50 | device_(new MockVPN(&control_, &dispatcher_, &metrics_, &manager_, |
| 51 | kInterfaceName, kInterfaceIndex)) { |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 52 | driver_->nss_ = &nss_; |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 53 | driver_->process_killer_ = &process_killer_; |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 54 | } |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 55 | |
| 56 | virtual ~L2TPIPSecDriverTest() {} |
| 57 | |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 58 | virtual void SetUp() { |
| 59 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 60 | } |
| 61 | |
| 62 | virtual void TearDown() { |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 63 | driver_->child_watch_tag_ = 0; |
| 64 | driver_->pid_ = 0; |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 65 | driver_->device_ = NULL; |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 66 | driver_->service_ = NULL; |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 67 | ASSERT_TRUE(temp_dir_.Delete()); |
| 68 | } |
| 69 | |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 70 | protected: |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 71 | static const char kInterfaceName[]; |
| 72 | static const int kInterfaceIndex; |
| 73 | |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 74 | void SetArg(const string &arg, const string &value) { |
Darin Petkov | 01c6604 | 2012-04-26 11:10:45 +0200 | [diff] [blame] | 75 | driver_->args()->SetString(arg, value); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 76 | } |
| 77 | |
Darin Petkov | d432539 | 2012-04-23 15:48:22 +0200 | [diff] [blame] | 78 | KeyValueStore *GetArgs() { |
| 79 | return driver_->args(); |
| 80 | } |
| 81 | |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 82 | // Used to assert that a flag appears in the options. |
| 83 | void ExpectInFlags(const vector<string> &options, const string &flag, |
| 84 | const string &value); |
| 85 | |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 86 | FilePath SetupPSKFile(); |
| 87 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 88 | // Inherited from RPCTaskDelegate. |
| 89 | virtual void GetLogin(string *user, string *password); |
| 90 | virtual void Notify(const string &reason, const map<string, string> &dict); |
| 91 | |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 92 | ScopedTempDir temp_dir_; |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 93 | NiceMockControl control_; |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 94 | NiceMock<MockDeviceInfo> device_info_; |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 95 | EventDispatcher dispatcher_; |
| 96 | MockMetrics metrics_; |
| 97 | MockGLib glib_; |
| 98 | MockManager manager_; |
| 99 | L2TPIPSecDriver *driver_; // Owned by |service_|. |
| 100 | scoped_refptr<MockVPNService> service_; |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 101 | scoped_refptr<MockVPN> device_; |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 102 | MockNSS nss_; |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 103 | MockProcessKiller process_killer_; |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 104 | }; |
| 105 | |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 106 | const char L2TPIPSecDriverTest::kInterfaceName[] = "ppp0"; |
| 107 | const int L2TPIPSecDriverTest::kInterfaceIndex = 123; |
| 108 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 109 | void L2TPIPSecDriverTest::GetLogin(string */*user*/, string */*password*/) {} |
| 110 | |
| 111 | void L2TPIPSecDriverTest::Notify( |
| 112 | const string &/*reason*/, const map<string, string> &/*dict*/) {} |
| 113 | |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 114 | void L2TPIPSecDriverTest::ExpectInFlags( |
| 115 | const vector<string> &options, const string &flag, const string &value) { |
| 116 | vector<string>::const_iterator it = |
| 117 | find(options.begin(), options.end(), flag); |
| 118 | |
| 119 | EXPECT_TRUE(it != options.end()); |
| 120 | if (it != options.end()) |
| 121 | return; // Don't crash below. |
| 122 | it++; |
| 123 | EXPECT_TRUE(it != options.end()); |
| 124 | if (it != options.end()) |
| 125 | return; // Don't crash below. |
| 126 | EXPECT_EQ(value, *it); |
| 127 | } |
| 128 | |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 129 | FilePath L2TPIPSecDriverTest::SetupPSKFile() { |
| 130 | FilePath psk_file; |
| 131 | EXPECT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), &psk_file)); |
| 132 | EXPECT_FALSE(psk_file.empty()); |
| 133 | EXPECT_TRUE(file_util::PathExists(psk_file)); |
| 134 | driver_->psk_file_ = psk_file; |
| 135 | return psk_file; |
| 136 | } |
| 137 | |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 138 | TEST_F(L2TPIPSecDriverTest, GetProviderType) { |
| 139 | EXPECT_EQ(flimflam::kProviderL2tpIpsec, driver_->GetProviderType()); |
| 140 | } |
| 141 | |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 142 | TEST_F(L2TPIPSecDriverTest, Cleanup) { |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 143 | driver_->Cleanup(Service::kStateIdle); // Ensure no crash. |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 144 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 145 | const unsigned int kTag = 123; |
| 146 | driver_->child_watch_tag_ = kTag; |
| 147 | EXPECT_CALL(glib_, SourceRemove(kTag)); |
| 148 | const int kPID = 123456; |
| 149 | driver_->pid_ = kPID; |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 150 | EXPECT_CALL(process_killer_, Kill(kPID, _)); |
Darin Petkov | a0e645e | 2012-04-25 11:38:59 +0200 | [diff] [blame] | 151 | driver_->device_ = device_; |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 152 | driver_->service_ = service_; |
Darin Petkov | a0e645e | 2012-04-25 11:38:59 +0200 | [diff] [blame] | 153 | EXPECT_CALL(*device_, OnDisconnected()); |
| 154 | EXPECT_CALL(*device_, SetEnabled(false)); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 155 | EXPECT_CALL(*service_, SetState(Service::kStateFailure)); |
| 156 | driver_->rpc_task_.reset(new RPCTask(&control_, this)); |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 157 | FilePath psk_file = SetupPSKFile(); |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 158 | driver_->StartConnectTimeout(); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 159 | driver_->Cleanup(Service::kStateFailure); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 160 | EXPECT_FALSE(file_util::PathExists(psk_file)); |
| 161 | EXPECT_TRUE(driver_->psk_file_.empty()); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 162 | EXPECT_EQ(0, driver_->child_watch_tag_); |
| 163 | EXPECT_EQ(0, driver_->pid_); |
| 164 | EXPECT_FALSE(driver_->rpc_task_.get()); |
Darin Petkov | a0e645e | 2012-04-25 11:38:59 +0200 | [diff] [blame] | 165 | EXPECT_FALSE(driver_->device_); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 166 | EXPECT_FALSE(driver_->service_); |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 167 | EXPECT_FALSE(driver_->IsConnectTimeoutStarted()); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 170 | TEST_F(L2TPIPSecDriverTest, DeletePSKFile) { |
| 171 | FilePath psk_file = SetupPSKFile(); |
| 172 | driver_->DeletePSKFile(); |
| 173 | EXPECT_FALSE(file_util::PathExists(psk_file)); |
| 174 | EXPECT_TRUE(driver_->psk_file_.empty()); |
| 175 | } |
| 176 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 177 | TEST_F(L2TPIPSecDriverTest, InitEnvironment) { |
| 178 | vector<string> env; |
| 179 | driver_->rpc_task_.reset(new RPCTask(&control_, this)); |
| 180 | driver_->InitEnvironment(&env); |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 181 | ASSERT_EQ(2, env.size()); |
| 182 | EXPECT_EQ( |
| 183 | string(kRPCTaskServiceVariable) + "=" + RPCTaskMockAdaptor::kRpcConnId, |
| 184 | env[0]); |
| 185 | EXPECT_EQ(string(kRPCTaskPathVariable) + "=" + RPCTaskMockAdaptor::kRpcId, |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 186 | env[1]); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | TEST_F(L2TPIPSecDriverTest, InitOptionsNoHost) { |
| 190 | Error error; |
| 191 | vector<string> options; |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 192 | EXPECT_FALSE(driver_->InitOptions(&options, &error)); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 193 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 194 | EXPECT_TRUE(options.empty()); |
| 195 | } |
| 196 | |
| 197 | TEST_F(L2TPIPSecDriverTest, InitOptions) { |
| 198 | static const char kHost[] = "192.168.2.254"; |
| 199 | static const char kCaCertNSS[] = "{1234}"; |
| 200 | static const char kPSK[] = "foobar"; |
| 201 | |
| 202 | SetArg(flimflam::kProviderHostProperty, kHost); |
| 203 | SetArg(flimflam::kL2tpIpsecCaCertNssProperty, kCaCertNSS); |
| 204 | SetArg(flimflam::kL2tpIpsecPskProperty, kPSK); |
| 205 | |
| 206 | FilePath empty_cert; |
| 207 | EXPECT_CALL(nss_, GetDERCertfile(kCaCertNSS, _)).WillOnce(Return(empty_cert)); |
| 208 | |
| 209 | const FilePath temp_dir(temp_dir_.path()); |
| 210 | EXPECT_CALL(manager_, run_path()).WillOnce(ReturnRef(temp_dir)); |
| 211 | |
| 212 | Error error; |
| 213 | vector<string> options; |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 214 | EXPECT_TRUE(driver_->InitOptions(&options, &error)); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 215 | EXPECT_TRUE(error.IsSuccess()); |
| 216 | |
| 217 | ExpectInFlags(options, "--remote_host", kHost); |
| 218 | ASSERT_FALSE(driver_->psk_file_.empty()); |
| 219 | ExpectInFlags(options, "--psk_file", driver_->psk_file_.value()); |
| 220 | } |
| 221 | |
| 222 | TEST_F(L2TPIPSecDriverTest, InitPSKOptions) { |
| 223 | Error error; |
| 224 | vector<string> options; |
| 225 | static const char kPSK[] = "foobar"; |
| 226 | const FilePath bad_dir("/non/existent/directory"); |
| 227 | const FilePath temp_dir(temp_dir_.path()); |
| 228 | EXPECT_CALL(manager_, run_path()) |
| 229 | .WillOnce(ReturnRef(bad_dir)) |
| 230 | .WillOnce(ReturnRef(temp_dir)); |
| 231 | |
| 232 | EXPECT_TRUE(driver_->InitPSKOptions(&options, &error)); |
| 233 | EXPECT_TRUE(options.empty()); |
| 234 | EXPECT_TRUE(error.IsSuccess()); |
| 235 | |
| 236 | SetArg(flimflam::kL2tpIpsecPskProperty, kPSK); |
| 237 | |
| 238 | EXPECT_FALSE(driver_->InitPSKOptions(&options, &error)); |
| 239 | EXPECT_TRUE(options.empty()); |
| 240 | EXPECT_EQ(Error::kInternalError, error.type()); |
| 241 | error.Reset(); |
| 242 | |
| 243 | EXPECT_TRUE(driver_->InitPSKOptions(&options, &error)); |
| 244 | ASSERT_FALSE(driver_->psk_file_.empty()); |
| 245 | ExpectInFlags(options, "--psk_file", driver_->psk_file_.value()); |
| 246 | EXPECT_TRUE(error.IsSuccess()); |
| 247 | string contents; |
| 248 | EXPECT_TRUE( |
| 249 | file_util::ReadFileToString(driver_->psk_file_, &contents)); |
| 250 | EXPECT_EQ(kPSK, contents); |
| 251 | struct stat buf; |
| 252 | ASSERT_EQ(0, stat(driver_->psk_file_.value().c_str(), &buf)); |
| 253 | EXPECT_EQ(S_IFREG | S_IRUSR | S_IWUSR, buf.st_mode); |
| 254 | } |
| 255 | |
| 256 | TEST_F(L2TPIPSecDriverTest, InitNSSOptions) { |
| 257 | static const char kHost[] = "192.168.2.254"; |
| 258 | static const char kCaCertNSS[] = "{1234}"; |
| 259 | static const char kNSSCertfile[] = "/tmp/nss-cert"; |
| 260 | FilePath empty_cert; |
| 261 | FilePath nss_cert(kNSSCertfile); |
| 262 | SetArg(flimflam::kProviderHostProperty, kHost); |
| 263 | SetArg(flimflam::kL2tpIpsecCaCertNssProperty, kCaCertNSS); |
| 264 | EXPECT_CALL(nss_, |
| 265 | GetDERCertfile(kCaCertNSS, |
| 266 | ElementsAreArray(kHost, arraysize(kHost) - 1))) |
| 267 | .WillOnce(Return(empty_cert)) |
| 268 | .WillOnce(Return(nss_cert)); |
| 269 | |
| 270 | vector<string> options; |
| 271 | driver_->InitNSSOptions(&options); |
| 272 | EXPECT_TRUE(options.empty()); |
| 273 | driver_->InitNSSOptions(&options); |
| 274 | ExpectInFlags(options, "--server_ca_file", kNSSCertfile); |
| 275 | } |
| 276 | |
| 277 | TEST_F(L2TPIPSecDriverTest, AppendValueOption) { |
| 278 | static const char kOption[] = "--l2tpipsec-option"; |
| 279 | static const char kProperty[] = "L2TPIPSec.SomeProperty"; |
| 280 | static const char kValue[] = "some-property-value"; |
| 281 | static const char kOption2[] = "--l2tpipsec-option2"; |
| 282 | static const char kProperty2[] = "L2TPIPSec.SomeProperty2"; |
| 283 | static const char kValue2[] = "some-property-value2"; |
| 284 | |
| 285 | vector<string> options; |
| 286 | EXPECT_FALSE( |
| 287 | driver_->AppendValueOption( |
| 288 | "L2TPIPSec.UnknownProperty", kOption, &options)); |
| 289 | EXPECT_TRUE(options.empty()); |
| 290 | |
| 291 | SetArg(kProperty, ""); |
| 292 | EXPECT_FALSE(driver_->AppendValueOption(kProperty, kOption, &options)); |
| 293 | EXPECT_TRUE(options.empty()); |
| 294 | |
| 295 | SetArg(kProperty, kValue); |
| 296 | SetArg(kProperty2, kValue2); |
| 297 | EXPECT_TRUE(driver_->AppendValueOption(kProperty, kOption, &options)); |
| 298 | EXPECT_TRUE(driver_->AppendValueOption(kProperty2, kOption2, &options)); |
| 299 | EXPECT_EQ(4, options.size()); |
| 300 | EXPECT_EQ(kOption, options[0]); |
| 301 | EXPECT_EQ(kValue, options[1]); |
| 302 | EXPECT_EQ(kOption2, options[2]); |
| 303 | EXPECT_EQ(kValue2, options[3]); |
| 304 | } |
| 305 | |
| 306 | TEST_F(L2TPIPSecDriverTest, AppendFlag) { |
| 307 | static const char kTrueOption[] = "--l2tpipsec-option"; |
| 308 | static const char kFalseOption[] = "--nol2tpipsec-option"; |
| 309 | static const char kProperty[] = "L2TPIPSec.SomeProperty"; |
| 310 | static const char kTrueOption2[] = "--l2tpipsec-option2"; |
| 311 | static const char kFalseOption2[] = "--nol2tpipsec-option2"; |
| 312 | static const char kProperty2[] = "L2TPIPSec.SomeProperty2"; |
| 313 | |
| 314 | vector<string> options; |
| 315 | EXPECT_FALSE(driver_->AppendFlag("L2TPIPSec.UnknownProperty", |
| 316 | kTrueOption, kFalseOption, &options)); |
| 317 | EXPECT_TRUE(options.empty()); |
| 318 | |
| 319 | SetArg(kProperty, ""); |
| 320 | EXPECT_FALSE( |
| 321 | driver_->AppendFlag(kProperty, kTrueOption, kFalseOption, &options)); |
| 322 | EXPECT_TRUE(options.empty()); |
| 323 | |
| 324 | SetArg(kProperty, "true"); |
| 325 | SetArg(kProperty2, "false"); |
| 326 | EXPECT_TRUE( |
| 327 | driver_->AppendFlag(kProperty, kTrueOption, kFalseOption, &options)); |
| 328 | EXPECT_TRUE( |
| 329 | driver_->AppendFlag(kProperty2, kTrueOption2, kFalseOption2, &options)); |
| 330 | EXPECT_EQ(2, options.size()); |
| 331 | EXPECT_EQ(kTrueOption, options[0]); |
| 332 | EXPECT_EQ(kFalseOption2, options[1]); |
| 333 | } |
| 334 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 335 | TEST_F(L2TPIPSecDriverTest, GetLogin) { |
| 336 | static const char kUser[] = "joesmith"; |
| 337 | static const char kPassword[] = "random-password"; |
| 338 | string user, password; |
| 339 | SetArg(flimflam::kL2tpIpsecUserProperty, kUser); |
| 340 | driver_->GetLogin(&user, &password); |
| 341 | EXPECT_TRUE(user.empty()); |
| 342 | EXPECT_TRUE(password.empty()); |
| 343 | SetArg(flimflam::kL2tpIpsecUserProperty, ""); |
| 344 | SetArg(flimflam::kL2tpIpsecPasswordProperty, kPassword); |
| 345 | driver_->GetLogin(&user, &password); |
| 346 | EXPECT_TRUE(user.empty()); |
| 347 | EXPECT_TRUE(password.empty()); |
| 348 | SetArg(flimflam::kL2tpIpsecUserProperty, kUser); |
| 349 | driver_->GetLogin(&user, &password); |
| 350 | EXPECT_EQ(kUser, user); |
| 351 | EXPECT_EQ(kPassword, password); |
| 352 | } |
| 353 | |
| 354 | TEST_F(L2TPIPSecDriverTest, OnL2TPIPSecVPNDied) { |
| 355 | const int kPID = 99999; |
| 356 | driver_->child_watch_tag_ = 333; |
| 357 | driver_->pid_ = kPID; |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 358 | EXPECT_CALL(process_killer_, Kill(_, _)).Times(0); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 359 | L2TPIPSecDriver::OnL2TPIPSecVPNDied(kPID, 2, driver_); |
| 360 | EXPECT_EQ(0, driver_->child_watch_tag_); |
| 361 | EXPECT_EQ(0, driver_->pid_); |
| 362 | } |
| 363 | |
| 364 | namespace { |
| 365 | MATCHER(CheckEnv, "") { |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 366 | if (!arg || !arg[0] || !arg[1] || arg[2]) { |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 367 | return false; |
| 368 | } |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 369 | return StartsWithASCII(arg[0], "SHILL_TASK_", true); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 370 | } |
| 371 | } // namespace |
| 372 | |
| 373 | TEST_F(L2TPIPSecDriverTest, SpawnL2TPIPSecVPN) { |
| 374 | Error error; |
| 375 | EXPECT_FALSE(driver_->SpawnL2TPIPSecVPN(&error)); |
| 376 | EXPECT_TRUE(error.IsFailure()); |
| 377 | |
| 378 | static const char kHost[] = "192.168.2.254"; |
| 379 | SetArg(flimflam::kProviderHostProperty, kHost); |
| 380 | driver_->rpc_task_.reset(new RPCTask(&control_, this)); |
| 381 | |
| 382 | const int kPID = 234678; |
| 383 | EXPECT_CALL(glib_, |
| 384 | SpawnAsyncWithPipesCWD(_, CheckEnv(), _, _, _, _, _, _, _, _)) |
| 385 | .WillOnce(Return(false)) |
| 386 | .WillOnce(DoAll(SetArgumentPointee<5>(kPID), Return(true))); |
| 387 | const int kTag = 6; |
| 388 | EXPECT_CALL(glib_, ChildWatchAdd(kPID, &driver_->OnL2TPIPSecVPNDied, driver_)) |
| 389 | .WillOnce(Return(kTag)); |
| 390 | error.Reset(); |
| 391 | EXPECT_FALSE(driver_->SpawnL2TPIPSecVPN(&error)); |
| 392 | EXPECT_EQ(Error::kInternalError, error.type()); |
| 393 | error.Reset(); |
| 394 | EXPECT_TRUE(driver_->SpawnL2TPIPSecVPN(&error)); |
| 395 | EXPECT_TRUE(error.IsSuccess()); |
| 396 | EXPECT_EQ(kPID, driver_->pid_); |
| 397 | EXPECT_EQ(kTag, driver_->child_watch_tag_); |
| 398 | } |
| 399 | |
| 400 | TEST_F(L2TPIPSecDriverTest, Connect) { |
| 401 | EXPECT_CALL(*service_, SetState(Service::kStateConfiguring)); |
| 402 | static const char kHost[] = "192.168.2.254"; |
| 403 | SetArg(flimflam::kProviderHostProperty, kHost); |
| 404 | EXPECT_CALL(glib_, SpawnAsyncWithPipesCWD(_, _, _, _, _, _, _, _, _, _)) |
| 405 | .WillOnce(Return(true)); |
| 406 | EXPECT_CALL(glib_, ChildWatchAdd(_, _, _)).WillOnce(Return(1)); |
| 407 | Error error; |
| 408 | driver_->Connect(service_, &error); |
| 409 | EXPECT_TRUE(error.IsSuccess()); |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 410 | EXPECT_TRUE(driver_->IsConnectTimeoutStarted()); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 411 | } |
| 412 | |
Darin Petkov | a0e645e | 2012-04-25 11:38:59 +0200 | [diff] [blame] | 413 | TEST_F(L2TPIPSecDriverTest, Disconnect) { |
| 414 | driver_->device_ = device_; |
| 415 | driver_->service_ = service_; |
| 416 | EXPECT_CALL(*device_, OnDisconnected()); |
| 417 | EXPECT_CALL(*device_, SetEnabled(false)); |
| 418 | EXPECT_CALL(*service_, SetState(Service::kStateIdle)); |
| 419 | driver_->Disconnect(); |
| 420 | EXPECT_FALSE(driver_->device_); |
| 421 | EXPECT_FALSE(driver_->service_); |
| 422 | } |
| 423 | |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 424 | TEST_F(L2TPIPSecDriverTest, OnConnectionDisconnected) { |
| 425 | driver_->service_ = service_; |
| 426 | EXPECT_CALL(*service_, SetState(Service::kStateFailure)); |
| 427 | driver_->OnConnectionDisconnected(); |
| 428 | EXPECT_FALSE(driver_->service_); |
| 429 | } |
| 430 | |
Darin Petkov | d432539 | 2012-04-23 15:48:22 +0200 | [diff] [blame] | 431 | TEST_F(L2TPIPSecDriverTest, InitPropertyStore) { |
| 432 | // Sanity test property store initialization. |
| 433 | PropertyStore store; |
| 434 | driver_->InitPropertyStore(&store); |
| 435 | const string kUser = "joe"; |
| 436 | Error error; |
| 437 | EXPECT_TRUE( |
| 438 | store.SetStringProperty(flimflam::kL2tpIpsecUserProperty, kUser, &error)); |
| 439 | EXPECT_TRUE(error.IsSuccess()); |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 440 | EXPECT_EQ(kUser, |
| 441 | GetArgs()->LookupString(flimflam::kL2tpIpsecUserProperty, "")); |
| 442 | } |
| 443 | |
| 444 | TEST_F(L2TPIPSecDriverTest, GetProvider) { |
| 445 | PropertyStore store; |
| 446 | driver_->InitPropertyStore(&store); |
Paul Stewart | 8e7e459 | 2012-04-29 09:47:48 -0700 | [diff] [blame] | 447 | PropertyStoreInspector inspector(&store); |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 448 | { |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 449 | KeyValueStore props; |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 450 | EXPECT_TRUE( |
| 451 | inspector.GetKeyValueStoreProperty( |
| 452 | flimflam::kProviderProperty, &props)); |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 453 | EXPECT_TRUE(props.LookupBool(flimflam::kPassphraseRequiredProperty, false)); |
| 454 | EXPECT_TRUE( |
| 455 | props.LookupBool(flimflam::kL2tpIpsecPskRequiredProperty, false)); |
| 456 | } |
| 457 | { |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 458 | KeyValueStore props; |
| 459 | SetArg(flimflam::kL2tpIpsecPasswordProperty, "random-password"); |
| 460 | SetArg(flimflam::kL2tpIpsecPskProperty, "random-psk"); |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 461 | EXPECT_TRUE( |
| 462 | inspector.GetKeyValueStoreProperty( |
| 463 | flimflam::kProviderProperty, &props)); |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 464 | EXPECT_FALSE(props.LookupBool(flimflam::kPassphraseRequiredProperty, true)); |
| 465 | EXPECT_FALSE( |
| 466 | props.LookupBool(flimflam::kL2tpIpsecPskRequiredProperty, true)); |
Darin Petkov | 0223655 | 2012-06-11 13:15:19 +0200 | [diff] [blame] | 467 | EXPECT_FALSE(props.ContainsString(flimflam::kL2tpIpsecPasswordProperty)); |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 468 | } |
Darin Petkov | d432539 | 2012-04-23 15:48:22 +0200 | [diff] [blame] | 469 | } |
| 470 | |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 471 | TEST_F(L2TPIPSecDriverTest, ParseIPConfiguration) { |
| 472 | map<string, string> config; |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 473 | config[kL2TPIPSecInternalIP4Address] = "4.5.6.7"; |
| 474 | config[kL2TPIPSecExternalIP4Address] = "33.44.55.66"; |
| 475 | config[kL2TPIPSecGatewayAddress] = "192.168.1.1"; |
| 476 | config[kL2TPIPSecDNS1] = "1.1.1.1"; |
| 477 | config[kL2TPIPSecDNS2] = "2.2.2.2"; |
| 478 | config[kL2TPIPSecInterfaceName] = "ppp0"; |
| 479 | config[kL2TPIPSecLNSAddress] = "99.88.77.66"; |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 480 | config["foo"] = "bar"; |
| 481 | IPConfig::Properties props; |
| 482 | string interface_name; |
| 483 | L2TPIPSecDriver::ParseIPConfiguration(config, &props, &interface_name); |
| 484 | EXPECT_EQ(IPAddress::kFamilyIPv4, props.address_family); |
| 485 | EXPECT_EQ("4.5.6.7", props.address); |
| 486 | EXPECT_EQ("33.44.55.66", props.peer_address); |
| 487 | EXPECT_EQ("192.168.1.1", props.gateway); |
| 488 | EXPECT_EQ("99.88.77.66", props.trusted_ip); |
| 489 | ASSERT_EQ(2, props.dns_servers.size()); |
| 490 | EXPECT_EQ("1.1.1.1", props.dns_servers[0]); |
| 491 | EXPECT_EQ("2.2.2.2", props.dns_servers[1]); |
| 492 | EXPECT_EQ("ppp0", interface_name); |
Ben Chan | a016312 | 2012-09-25 15:10:52 -0700 | [diff] [blame] | 493 | EXPECT_TRUE(props.blackhole_ipv6); |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | namespace { |
| 497 | MATCHER_P(IsIPAddress, address, "") { |
| 498 | IPAddress ip_address(IPAddress::kFamilyIPv4); |
| 499 | EXPECT_TRUE(ip_address.SetAddressFromString(address)); |
| 500 | return ip_address.Equals(arg); |
| 501 | } |
| 502 | } // namespace |
| 503 | |
| 504 | TEST_F(L2TPIPSecDriverTest, Notify) { |
| 505 | map<string, string> config; |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 506 | config[kL2TPIPSecInterfaceName] = kInterfaceName; |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 507 | EXPECT_CALL(device_info_, GetIndex(kInterfaceName)) |
| 508 | .WillOnce(Return(kInterfaceIndex)); |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 509 | EXPECT_CALL(*device_, SetEnabled(true)); |
| 510 | EXPECT_CALL(*device_, UpdateIPConfig(_)); |
| 511 | driver_->device_ = device_; |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 512 | FilePath psk_file = SetupPSKFile(); |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 513 | driver_->StartConnectTimeout(); |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 514 | driver_->Notify(kL2TPIPSecReasonConnect, config); |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 515 | EXPECT_FALSE(file_util::PathExists(psk_file)); |
| 516 | EXPECT_TRUE(driver_->psk_file_.empty()); |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 517 | EXPECT_FALSE(driver_->IsConnectTimeoutStarted()); |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 518 | } |
| 519 | |
Darin Petkov | a0e645e | 2012-04-25 11:38:59 +0200 | [diff] [blame] | 520 | TEST_F(L2TPIPSecDriverTest, NotifyFail) { |
| 521 | map<string, string> dict; |
| 522 | driver_->device_ = device_; |
| 523 | EXPECT_CALL(*device_, OnDisconnected()); |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 524 | driver_->StartConnectTimeout(); |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 525 | driver_->Notify(kL2TPIPSecReasonDisconnect, dict); |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 526 | EXPECT_TRUE(driver_->IsConnectTimeoutStarted()); |
Darin Petkov | a0e645e | 2012-04-25 11:38:59 +0200 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | TEST_F(L2TPIPSecDriverTest, VerifyPaths) { |
| 530 | // Ensure that the various path constants that the L2TP/IPSec driver uses |
| 531 | // actually exists in the build image. Due to build dependencies, they should |
| 532 | // already exist by the time we run unit tests. |
| 533 | |
| 534 | // The L2TPIPSecDriver path constants are absolute. FilePath::Append asserts |
| 535 | // that its argument is not an absolute path, so we need to strip the leading |
| 536 | // separators. There's nothing built into FilePath to do so. |
| 537 | static const char *kPaths[] = { |
| 538 | L2TPIPSecDriver::kL2TPIPSecVPNPath, |
| 539 | L2TPIPSecDriver::kPPPDPlugin, |
| 540 | }; |
| 541 | for (size_t i = 0; i < arraysize(kPaths); i++) { |
| 542 | string path(kPaths[i]); |
| 543 | TrimString(path, FilePath::kSeparators, &path); |
| 544 | EXPECT_TRUE(file_util::PathExists(FilePath(SYSROOT).Append(path))) |
| 545 | << kPaths[i]; |
| 546 | } |
| 547 | } |
| 548 | |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 549 | } // namespace shill |