blob: bd72f765bbe857c8e33bb27d3628f11b99fceddc [file] [log] [blame]
Darin Petkov7476a262012-04-12 16:30:46 +02001// 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 Petkovf7ef50a2012-04-16 20:54:31 +02007#include <base/file_util.h>
8#include <base/scoped_temp_dir.h>
Darin Petkov209e6292012-04-20 11:33:32 +02009#include <base/string_util.h>
Darin Petkov7476a262012-04-12 16:30:46 +020010#include <gtest/gtest.h>
11
12#include "shill/event_dispatcher.h"
13#include "shill/nice_mock_control.h"
Darin Petkov209e6292012-04-20 11:33:32 +020014#include "shill/mock_adaptors.h"
Darin Petkov0e9735d2012-04-24 12:33:45 +020015#include "shill/mock_device_info.h"
Darin Petkov7476a262012-04-12 16:30:46 +020016#include "shill/mock_glib.h"
17#include "shill/mock_manager.h"
18#include "shill/mock_metrics.h"
Darin Petkovf7ef50a2012-04-16 20:54:31 +020019#include "shill/mock_nss.h"
Darin Petkov5a850472012-06-06 15:44:24 +020020#include "shill/mock_process_killer.h"
Darin Petkovf8046b82012-04-24 16:29:23 +020021#include "shill/mock_vpn.h"
Darin Petkov7476a262012-04-12 16:30:46 +020022#include "shill/mock_vpn_service.h"
Paul Stewart8e7e4592012-04-29 09:47:48 -070023#include "shill/property_store_inspector.h"
Darin Petkovf8046b82012-04-24 16:29:23 +020024#include "shill/vpn.h"
Darin Petkov7476a262012-04-12 16:30:46 +020025
Darin Petkovf7ef50a2012-04-16 20:54:31 +020026using std::find;
Darin Petkov209e6292012-04-20 11:33:32 +020027using std::map;
Darin Petkovf7ef50a2012-04-16 20:54:31 +020028using std::string;
29using std::vector;
30using testing::_;
31using testing::ElementsAreArray;
Darin Petkov0e9735d2012-04-24 12:33:45 +020032using testing::NiceMock;
Darin Petkovf7ef50a2012-04-16 20:54:31 +020033using testing::Return;
34using testing::ReturnRef;
Darin Petkov209e6292012-04-20 11:33:32 +020035using testing::SetArgumentPointee;
Darin Petkov0e9735d2012-04-24 12:33:45 +020036using testing::StrictMock;
Darin Petkovf7ef50a2012-04-16 20:54:31 +020037
Darin Petkov7476a262012-04-12 16:30:46 +020038namespace shill {
39
Darin Petkov209e6292012-04-20 11:33:32 +020040class L2TPIPSecDriverTest : public testing::Test,
41 public RPCTaskDelegate {
Darin Petkov7476a262012-04-12 16:30:46 +020042 public:
43 L2TPIPSecDriverTest()
Darin Petkov0e9735d2012-04-24 12:33:45 +020044 : device_info_(&control_, &dispatcher_, &metrics_, &manager_),
45 manager_(&control_, &dispatcher_, &metrics_, &glib_),
Darin Petkovf8046b82012-04-24 16:29:23 +020046 driver_(new L2TPIPSecDriver(&control_, &dispatcher_, &metrics_,
47 &manager_, &device_info_, &glib_)),
Darin Petkov7476a262012-04-12 16:30:46 +020048 service_(new MockVPNService(&control_, &dispatcher_, &metrics_,
Darin Petkovf8046b82012-04-24 16:29:23 +020049 &manager_, driver_)),
50 device_(new MockVPN(&control_, &dispatcher_, &metrics_, &manager_,
51 kInterfaceName, kInterfaceIndex)) {
Darin Petkovf7ef50a2012-04-16 20:54:31 +020052 driver_->nss_ = &nss_;
Darin Petkov5a850472012-06-06 15:44:24 +020053 driver_->process_killer_ = &process_killer_;
Darin Petkovf7ef50a2012-04-16 20:54:31 +020054 }
Darin Petkov7476a262012-04-12 16:30:46 +020055
56 virtual ~L2TPIPSecDriverTest() {}
57
Darin Petkovf7ef50a2012-04-16 20:54:31 +020058 virtual void SetUp() {
59 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
60 }
61
62 virtual void TearDown() {
Darin Petkov209e6292012-04-20 11:33:32 +020063 driver_->child_watch_tag_ = 0;
64 driver_->pid_ = 0;
Darin Petkovf8046b82012-04-24 16:29:23 +020065 driver_->device_ = NULL;
Darin Petkov209e6292012-04-20 11:33:32 +020066 driver_->service_ = NULL;
Darin Petkovf7ef50a2012-04-16 20:54:31 +020067 ASSERT_TRUE(temp_dir_.Delete());
68 }
69
Darin Petkov7476a262012-04-12 16:30:46 +020070 protected:
Darin Petkovf8046b82012-04-24 16:29:23 +020071 static const char kInterfaceName[];
72 static const int kInterfaceIndex;
73
Darin Petkovf7ef50a2012-04-16 20:54:31 +020074 void SetArg(const string &arg, const string &value) {
Darin Petkov01c66042012-04-26 11:10:45 +020075 driver_->args()->SetString(arg, value);
Darin Petkovf7ef50a2012-04-16 20:54:31 +020076 }
77
Darin Petkovd4325392012-04-23 15:48:22 +020078 KeyValueStore *GetArgs() {
79 return driver_->args();
80 }
81
Darin Petkovf7ef50a2012-04-16 20:54:31 +020082 // 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 Petkov0e9735d2012-04-24 12:33:45 +020086 FilePath SetupPSKFile();
87
Darin Petkov209e6292012-04-20 11:33:32 +020088 // 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 Petkovf7ef50a2012-04-16 20:54:31 +020092 ScopedTempDir temp_dir_;
Darin Petkov7476a262012-04-12 16:30:46 +020093 NiceMockControl control_;
Darin Petkov0e9735d2012-04-24 12:33:45 +020094 NiceMock<MockDeviceInfo> device_info_;
Darin Petkov7476a262012-04-12 16:30:46 +020095 EventDispatcher dispatcher_;
96 MockMetrics metrics_;
97 MockGLib glib_;
98 MockManager manager_;
99 L2TPIPSecDriver *driver_; // Owned by |service_|.
100 scoped_refptr<MockVPNService> service_;
Darin Petkovf8046b82012-04-24 16:29:23 +0200101 scoped_refptr<MockVPN> device_;
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200102 MockNSS nss_;
Darin Petkov5a850472012-06-06 15:44:24 +0200103 MockProcessKiller process_killer_;
Darin Petkov7476a262012-04-12 16:30:46 +0200104};
105
Darin Petkovf8046b82012-04-24 16:29:23 +0200106const char L2TPIPSecDriverTest::kInterfaceName[] = "ppp0";
107const int L2TPIPSecDriverTest::kInterfaceIndex = 123;
108
Darin Petkov209e6292012-04-20 11:33:32 +0200109void L2TPIPSecDriverTest::GetLogin(string */*user*/, string */*password*/) {}
110
111void L2TPIPSecDriverTest::Notify(
112 const string &/*reason*/, const map<string, string> &/*dict*/) {}
113
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200114void 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 Petkov0e9735d2012-04-24 12:33:45 +0200129FilePath 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 Petkov7476a262012-04-12 16:30:46 +0200138TEST_F(L2TPIPSecDriverTest, GetProviderType) {
139 EXPECT_EQ(flimflam::kProviderL2tpIpsec, driver_->GetProviderType());
140}
141
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200142TEST_F(L2TPIPSecDriverTest, Cleanup) {
Darin Petkov209e6292012-04-20 11:33:32 +0200143 driver_->Cleanup(Service::kStateIdle); // Ensure no crash.
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200144
Darin Petkov209e6292012-04-20 11:33:32 +0200145 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 Petkov5a850472012-06-06 15:44:24 +0200150 EXPECT_CALL(process_killer_, Kill(kPID, _));
Darin Petkova0e645e2012-04-25 11:38:59 +0200151 driver_->device_ = device_;
Darin Petkov209e6292012-04-20 11:33:32 +0200152 driver_->service_ = service_;
Darin Petkova0e645e2012-04-25 11:38:59 +0200153 EXPECT_CALL(*device_, OnDisconnected());
154 EXPECT_CALL(*device_, SetEnabled(false));
Darin Petkov209e6292012-04-20 11:33:32 +0200155 EXPECT_CALL(*service_, SetState(Service::kStateFailure));
156 driver_->rpc_task_.reset(new RPCTask(&control_, this));
Darin Petkov0e9735d2012-04-24 12:33:45 +0200157 FilePath psk_file = SetupPSKFile();
Darin Petkov602303f2012-06-06 12:15:59 +0200158 driver_->StartConnectTimeout();
Darin Petkov209e6292012-04-20 11:33:32 +0200159 driver_->Cleanup(Service::kStateFailure);
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200160 EXPECT_FALSE(file_util::PathExists(psk_file));
161 EXPECT_TRUE(driver_->psk_file_.empty());
Darin Petkov209e6292012-04-20 11:33:32 +0200162 EXPECT_EQ(0, driver_->child_watch_tag_);
163 EXPECT_EQ(0, driver_->pid_);
164 EXPECT_FALSE(driver_->rpc_task_.get());
Darin Petkova0e645e2012-04-25 11:38:59 +0200165 EXPECT_FALSE(driver_->device_);
Darin Petkov209e6292012-04-20 11:33:32 +0200166 EXPECT_FALSE(driver_->service_);
Darin Petkov602303f2012-06-06 12:15:59 +0200167 EXPECT_FALSE(driver_->IsConnectTimeoutStarted());
Darin Petkov209e6292012-04-20 11:33:32 +0200168}
169
Darin Petkov0e9735d2012-04-24 12:33:45 +0200170TEST_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 Petkov209e6292012-04-20 11:33:32 +0200177TEST_F(L2TPIPSecDriverTest, InitEnvironment) {
178 vector<string> env;
179 driver_->rpc_task_.reset(new RPCTask(&control_, this));
180 driver_->InitEnvironment(&env);
181 ASSERT_EQ(3, env.size());
182 EXPECT_EQ(string("CONNMAN_BUSNAME=") + RPCTaskMockAdaptor::kRpcConnId,
183 env[0]);
184 EXPECT_EQ(string("CONNMAN_INTERFACE=") + RPCTaskMockAdaptor::kRpcInterfaceId,
185 env[1]);
186 EXPECT_EQ(string("CONNMAN_PATH=") + RPCTaskMockAdaptor::kRpcId, env[2]);
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200187}
188
189TEST_F(L2TPIPSecDriverTest, InitOptionsNoHost) {
190 Error error;
191 vector<string> options;
Darin Petkov209e6292012-04-20 11:33:32 +0200192 EXPECT_FALSE(driver_->InitOptions(&options, &error));
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200193 EXPECT_EQ(Error::kInvalidArguments, error.type());
194 EXPECT_TRUE(options.empty());
195}
196
197TEST_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 Petkov209e6292012-04-20 11:33:32 +0200214 EXPECT_TRUE(driver_->InitOptions(&options, &error));
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200215 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
222TEST_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
256TEST_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
277TEST_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
306TEST_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 Petkov209e6292012-04-20 11:33:32 +0200335TEST_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
354TEST_F(L2TPIPSecDriverTest, OnL2TPIPSecVPNDied) {
355 const int kPID = 99999;
356 driver_->child_watch_tag_ = 333;
357 driver_->pid_ = kPID;
Darin Petkov5a850472012-06-06 15:44:24 +0200358 EXPECT_CALL(process_killer_, Kill(_, _)).Times(0);
Darin Petkov209e6292012-04-20 11:33:32 +0200359 L2TPIPSecDriver::OnL2TPIPSecVPNDied(kPID, 2, driver_);
360 EXPECT_EQ(0, driver_->child_watch_tag_);
361 EXPECT_EQ(0, driver_->pid_);
362}
363
364namespace {
365MATCHER(CheckEnv, "") {
366 if (!arg || !arg[0] || !arg[1] || !arg[2] || arg[3]) {
367 return false;
368 }
369 return StartsWithASCII(arg[0], "CONNMAN_", true);
370}
371} // namespace
372
373TEST_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
400TEST_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 Petkov602303f2012-06-06 12:15:59 +0200410 EXPECT_TRUE(driver_->IsConnectTimeoutStarted());
Darin Petkov209e6292012-04-20 11:33:32 +0200411}
412
Darin Petkova0e645e2012-04-25 11:38:59 +0200413TEST_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 Petkov5eb05422012-05-11 15:45:25 +0200424TEST_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 Petkovd4325392012-04-23 15:48:22 +0200431TEST_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 Petkovb536a742012-04-26 11:31:28 +0200440 EXPECT_EQ(kUser,
441 GetArgs()->LookupString(flimflam::kL2tpIpsecUserProperty, ""));
442}
443
444TEST_F(L2TPIPSecDriverTest, GetProvider) {
445 PropertyStore store;
446 driver_->InitPropertyStore(&store);
Paul Stewart8e7e4592012-04-29 09:47:48 -0700447 PropertyStoreInspector inspector(&store);
Darin Petkovb536a742012-04-26 11:31:28 +0200448 {
Darin Petkovb536a742012-04-26 11:31:28 +0200449 KeyValueStore props;
Darin Petkov4682aa82012-05-31 16:24:11 +0200450 EXPECT_TRUE(
451 inspector.GetKeyValueStoreProperty(
452 flimflam::kProviderProperty, &props));
Darin Petkovb536a742012-04-26 11:31:28 +0200453 EXPECT_TRUE(props.LookupBool(flimflam::kPassphraseRequiredProperty, false));
454 EXPECT_TRUE(
455 props.LookupBool(flimflam::kL2tpIpsecPskRequiredProperty, false));
456 }
457 {
Darin Petkovb536a742012-04-26 11:31:28 +0200458 KeyValueStore props;
459 SetArg(flimflam::kL2tpIpsecPasswordProperty, "random-password");
460 SetArg(flimflam::kL2tpIpsecPskProperty, "random-psk");
Darin Petkov4682aa82012-05-31 16:24:11 +0200461 EXPECT_TRUE(
462 inspector.GetKeyValueStoreProperty(
463 flimflam::kProviderProperty, &props));
Darin Petkovb536a742012-04-26 11:31:28 +0200464 EXPECT_FALSE(props.LookupBool(flimflam::kPassphraseRequiredProperty, true));
465 EXPECT_FALSE(
466 props.LookupBool(flimflam::kL2tpIpsecPskRequiredProperty, true));
Darin Petkov02236552012-06-11 13:15:19 +0200467 EXPECT_FALSE(props.ContainsString(flimflam::kL2tpIpsecPasswordProperty));
Darin Petkovb536a742012-04-26 11:31:28 +0200468 }
Darin Petkovd4325392012-04-23 15:48:22 +0200469}
470
Darin Petkov0e9735d2012-04-24 12:33:45 +0200471TEST_F(L2TPIPSecDriverTest, ParseIPConfiguration) {
472 map<string, string> config;
473 config["INTERNAL_IP4_ADDRESS"] = "4.5.6.7";
474 config["EXTERNAL_IP4_ADDRESS"] = "33.44.55.66";
475 config["GATEWAY_ADDRESS"] = "192.168.1.1";
476 config["DNS1"] = "1.1.1.1";
477 config["DNS2"] = "2.2.2.2";
478 config["INTERNAL_IFNAME"] = "ppp0";
479 config["LNS_ADDRESS"] = "99.88.77.66";
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);
493}
494
495namespace {
496MATCHER_P(IsIPAddress, address, "") {
497 IPAddress ip_address(IPAddress::kFamilyIPv4);
498 EXPECT_TRUE(ip_address.SetAddressFromString(address));
499 return ip_address.Equals(arg);
500}
501} // namespace
502
503TEST_F(L2TPIPSecDriverTest, Notify) {
504 map<string, string> config;
Darin Petkovf8046b82012-04-24 16:29:23 +0200505 config["INTERNAL_IFNAME"] = kInterfaceName;
Darin Petkovf8046b82012-04-24 16:29:23 +0200506 EXPECT_CALL(device_info_, GetIndex(kInterfaceName))
507 .WillOnce(Return(kInterfaceIndex));
Darin Petkovf8046b82012-04-24 16:29:23 +0200508 EXPECT_CALL(*device_, SetEnabled(true));
509 EXPECT_CALL(*device_, UpdateIPConfig(_));
510 driver_->device_ = device_;
Darin Petkov0e9735d2012-04-24 12:33:45 +0200511 FilePath psk_file = SetupPSKFile();
Darin Petkov602303f2012-06-06 12:15:59 +0200512 driver_->StartConnectTimeout();
Darin Petkov0e9735d2012-04-24 12:33:45 +0200513 driver_->Notify("connect", config);
514 EXPECT_FALSE(file_util::PathExists(psk_file));
515 EXPECT_TRUE(driver_->psk_file_.empty());
Darin Petkov602303f2012-06-06 12:15:59 +0200516 EXPECT_FALSE(driver_->IsConnectTimeoutStarted());
Darin Petkov0e9735d2012-04-24 12:33:45 +0200517}
518
Darin Petkova0e645e2012-04-25 11:38:59 +0200519TEST_F(L2TPIPSecDriverTest, NotifyFail) {
520 map<string, string> dict;
521 driver_->device_ = device_;
522 EXPECT_CALL(*device_, OnDisconnected());
Darin Petkov602303f2012-06-06 12:15:59 +0200523 driver_->StartConnectTimeout();
Darin Petkova0e645e2012-04-25 11:38:59 +0200524 driver_->Notify("fail", dict);
Darin Petkov602303f2012-06-06 12:15:59 +0200525 EXPECT_TRUE(driver_->IsConnectTimeoutStarted());
Darin Petkova0e645e2012-04-25 11:38:59 +0200526}
527
528TEST_F(L2TPIPSecDriverTest, VerifyPaths) {
529 // Ensure that the various path constants that the L2TP/IPSec driver uses
530 // actually exists in the build image. Due to build dependencies, they should
531 // already exist by the time we run unit tests.
532
533 // The L2TPIPSecDriver path constants are absolute. FilePath::Append asserts
534 // that its argument is not an absolute path, so we need to strip the leading
535 // separators. There's nothing built into FilePath to do so.
536 static const char *kPaths[] = {
537 L2TPIPSecDriver::kL2TPIPSecVPNPath,
538 L2TPIPSecDriver::kPPPDPlugin,
539 };
540 for (size_t i = 0; i < arraysize(kPaths); i++) {
541 string path(kPaths[i]);
542 TrimString(path, FilePath::kSeparators, &path);
543 EXPECT_TRUE(file_util::PathExists(FilePath(SYSROOT).Append(path)))
544 << kPaths[i];
545 }
546}
547
Darin Petkov7476a262012-04-12 16:30:46 +0200548} // namespace shill