blob: 05a51c6fb48e215936af1ec365779b108a7cb9b2 [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 Petkovf8046b82012-04-24 16:29:23 +020020#include "shill/mock_vpn.h"
Darin Petkov7476a262012-04-12 16:30:46 +020021#include "shill/mock_vpn_service.h"
Darin Petkovf8046b82012-04-24 16:29:23 +020022#include "shill/vpn.h"
Darin Petkov7476a262012-04-12 16:30:46 +020023
Darin Petkovf7ef50a2012-04-16 20:54:31 +020024using std::find;
Darin Petkov209e6292012-04-20 11:33:32 +020025using std::map;
Darin Petkovf7ef50a2012-04-16 20:54:31 +020026using std::string;
27using std::vector;
28using testing::_;
29using testing::ElementsAreArray;
Darin Petkov0e9735d2012-04-24 12:33:45 +020030using testing::NiceMock;
Darin Petkovf7ef50a2012-04-16 20:54:31 +020031using testing::Return;
32using testing::ReturnRef;
Darin Petkov209e6292012-04-20 11:33:32 +020033using testing::SetArgumentPointee;
Darin Petkov0e9735d2012-04-24 12:33:45 +020034using testing::StrictMock;
Darin Petkovf7ef50a2012-04-16 20:54:31 +020035
Darin Petkov7476a262012-04-12 16:30:46 +020036namespace shill {
37
Darin Petkov209e6292012-04-20 11:33:32 +020038class L2TPIPSecDriverTest : public testing::Test,
39 public RPCTaskDelegate {
Darin Petkov7476a262012-04-12 16:30:46 +020040 public:
41 L2TPIPSecDriverTest()
Darin Petkov0e9735d2012-04-24 12:33:45 +020042 : device_info_(&control_, &dispatcher_, &metrics_, &manager_),
43 manager_(&control_, &dispatcher_, &metrics_, &glib_),
Darin Petkovf8046b82012-04-24 16:29:23 +020044 driver_(new L2TPIPSecDriver(&control_, &dispatcher_, &metrics_,
45 &manager_, &device_info_, &glib_)),
Darin Petkov7476a262012-04-12 16:30:46 +020046 service_(new MockVPNService(&control_, &dispatcher_, &metrics_,
Darin Petkovf8046b82012-04-24 16:29:23 +020047 &manager_, driver_)),
48 device_(new MockVPN(&control_, &dispatcher_, &metrics_, &manager_,
49 kInterfaceName, kInterfaceIndex)) {
Darin Petkovf7ef50a2012-04-16 20:54:31 +020050 driver_->nss_ = &nss_;
51 }
Darin Petkov7476a262012-04-12 16:30:46 +020052
53 virtual ~L2TPIPSecDriverTest() {}
54
Darin Petkovf7ef50a2012-04-16 20:54:31 +020055 virtual void SetUp() {
56 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
57 }
58
59 virtual void TearDown() {
Darin Petkov209e6292012-04-20 11:33:32 +020060 driver_->child_watch_tag_ = 0;
61 driver_->pid_ = 0;
Darin Petkovf8046b82012-04-24 16:29:23 +020062 driver_->device_ = NULL;
Darin Petkov209e6292012-04-20 11:33:32 +020063 driver_->service_ = NULL;
Darin Petkovf7ef50a2012-04-16 20:54:31 +020064 ASSERT_TRUE(temp_dir_.Delete());
65 }
66
Darin Petkov7476a262012-04-12 16:30:46 +020067 protected:
Darin Petkovf8046b82012-04-24 16:29:23 +020068 static const char kInterfaceName[];
69 static const int kInterfaceIndex;
70
Darin Petkovf7ef50a2012-04-16 20:54:31 +020071 void SetArg(const string &arg, const string &value) {
Darin Petkov01c66042012-04-26 11:10:45 +020072 driver_->args()->SetString(arg, value);
Darin Petkovf7ef50a2012-04-16 20:54:31 +020073 }
74
Darin Petkovd4325392012-04-23 15:48:22 +020075 KeyValueStore *GetArgs() {
76 return driver_->args();
77 }
78
Darin Petkovb536a742012-04-26 11:31:28 +020079 bool FindKeyValueStorePropertyInStore(const PropertyStore &store,
80 const string &key,
81 KeyValueStore *value,
82 Error *error);
83
Darin Petkovf7ef50a2012-04-16 20:54:31 +020084 // Used to assert that a flag appears in the options.
85 void ExpectInFlags(const vector<string> &options, const string &flag,
86 const string &value);
87
Darin Petkov0e9735d2012-04-24 12:33:45 +020088 FilePath SetupPSKFile();
89
Darin Petkov209e6292012-04-20 11:33:32 +020090 // Inherited from RPCTaskDelegate.
91 virtual void GetLogin(string *user, string *password);
92 virtual void Notify(const string &reason, const map<string, string> &dict);
93
Darin Petkovf7ef50a2012-04-16 20:54:31 +020094 ScopedTempDir temp_dir_;
Darin Petkov7476a262012-04-12 16:30:46 +020095 NiceMockControl control_;
Darin Petkov0e9735d2012-04-24 12:33:45 +020096 NiceMock<MockDeviceInfo> device_info_;
Darin Petkov7476a262012-04-12 16:30:46 +020097 EventDispatcher dispatcher_;
98 MockMetrics metrics_;
99 MockGLib glib_;
100 MockManager manager_;
101 L2TPIPSecDriver *driver_; // Owned by |service_|.
102 scoped_refptr<MockVPNService> service_;
Darin Petkovf8046b82012-04-24 16:29:23 +0200103 scoped_refptr<MockVPN> device_;
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200104 MockNSS nss_;
Darin Petkov7476a262012-04-12 16:30:46 +0200105};
106
Darin Petkovf8046b82012-04-24 16:29:23 +0200107const char L2TPIPSecDriverTest::kInterfaceName[] = "ppp0";
108const int L2TPIPSecDriverTest::kInterfaceIndex = 123;
109
Darin Petkov209e6292012-04-20 11:33:32 +0200110void L2TPIPSecDriverTest::GetLogin(string */*user*/, string */*password*/) {}
111
112void L2TPIPSecDriverTest::Notify(
113 const string &/*reason*/, const map<string, string> &/*dict*/) {}
114
Darin Petkovb536a742012-04-26 11:31:28 +0200115bool L2TPIPSecDriverTest::FindKeyValueStorePropertyInStore(
116 const PropertyStore &store,
117 const string &key,
118 KeyValueStore *value,
119 Error *error) {
120 ReadablePropertyConstIterator<KeyValueStore> it =
121 store.GetKeyValueStorePropertiesIter();
122 for ( ; !it.AtEnd(); it.Advance()) {
123 if (it.Key() == key) {
124 *value = it.Value(error);
125 return error->IsSuccess();
126 }
127 }
128 error->Populate(Error::kNotFound);
129 return false;
130}
131
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200132void L2TPIPSecDriverTest::ExpectInFlags(
133 const vector<string> &options, const string &flag, const string &value) {
134 vector<string>::const_iterator it =
135 find(options.begin(), options.end(), flag);
136
137 EXPECT_TRUE(it != options.end());
138 if (it != options.end())
139 return; // Don't crash below.
140 it++;
141 EXPECT_TRUE(it != options.end());
142 if (it != options.end())
143 return; // Don't crash below.
144 EXPECT_EQ(value, *it);
145}
146
Darin Petkov0e9735d2012-04-24 12:33:45 +0200147FilePath L2TPIPSecDriverTest::SetupPSKFile() {
148 FilePath psk_file;
149 EXPECT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), &psk_file));
150 EXPECT_FALSE(psk_file.empty());
151 EXPECT_TRUE(file_util::PathExists(psk_file));
152 driver_->psk_file_ = psk_file;
153 return psk_file;
154}
155
Darin Petkov7476a262012-04-12 16:30:46 +0200156TEST_F(L2TPIPSecDriverTest, GetProviderType) {
157 EXPECT_EQ(flimflam::kProviderL2tpIpsec, driver_->GetProviderType());
158}
159
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200160TEST_F(L2TPIPSecDriverTest, Cleanup) {
Darin Petkov209e6292012-04-20 11:33:32 +0200161 driver_->Cleanup(Service::kStateIdle); // Ensure no crash.
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200162
Darin Petkov209e6292012-04-20 11:33:32 +0200163 const unsigned int kTag = 123;
164 driver_->child_watch_tag_ = kTag;
165 EXPECT_CALL(glib_, SourceRemove(kTag));
166 const int kPID = 123456;
167 driver_->pid_ = kPID;
168 EXPECT_CALL(glib_, SpawnClosePID(kPID));
Darin Petkova0e645e2012-04-25 11:38:59 +0200169 driver_->device_ = device_;
Darin Petkov209e6292012-04-20 11:33:32 +0200170 driver_->service_ = service_;
Darin Petkova0e645e2012-04-25 11:38:59 +0200171 EXPECT_CALL(*device_, OnDisconnected());
172 EXPECT_CALL(*device_, SetEnabled(false));
Darin Petkov209e6292012-04-20 11:33:32 +0200173 EXPECT_CALL(*service_, SetState(Service::kStateFailure));
174 driver_->rpc_task_.reset(new RPCTask(&control_, this));
Darin Petkov0e9735d2012-04-24 12:33:45 +0200175 FilePath psk_file = SetupPSKFile();
Darin Petkov209e6292012-04-20 11:33:32 +0200176 driver_->Cleanup(Service::kStateFailure);
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200177 EXPECT_FALSE(file_util::PathExists(psk_file));
178 EXPECT_TRUE(driver_->psk_file_.empty());
Darin Petkov209e6292012-04-20 11:33:32 +0200179 EXPECT_EQ(0, driver_->child_watch_tag_);
180 EXPECT_EQ(0, driver_->pid_);
181 EXPECT_FALSE(driver_->rpc_task_.get());
Darin Petkova0e645e2012-04-25 11:38:59 +0200182 EXPECT_FALSE(driver_->device_);
Darin Petkov209e6292012-04-20 11:33:32 +0200183 EXPECT_FALSE(driver_->service_);
184}
185
Darin Petkov0e9735d2012-04-24 12:33:45 +0200186TEST_F(L2TPIPSecDriverTest, DeletePSKFile) {
187 FilePath psk_file = SetupPSKFile();
188 driver_->DeletePSKFile();
189 EXPECT_FALSE(file_util::PathExists(psk_file));
190 EXPECT_TRUE(driver_->psk_file_.empty());
191}
192
Darin Petkov209e6292012-04-20 11:33:32 +0200193TEST_F(L2TPIPSecDriverTest, InitEnvironment) {
194 vector<string> env;
195 driver_->rpc_task_.reset(new RPCTask(&control_, this));
196 driver_->InitEnvironment(&env);
197 ASSERT_EQ(3, env.size());
198 EXPECT_EQ(string("CONNMAN_BUSNAME=") + RPCTaskMockAdaptor::kRpcConnId,
199 env[0]);
200 EXPECT_EQ(string("CONNMAN_INTERFACE=") + RPCTaskMockAdaptor::kRpcInterfaceId,
201 env[1]);
202 EXPECT_EQ(string("CONNMAN_PATH=") + RPCTaskMockAdaptor::kRpcId, env[2]);
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200203}
204
205TEST_F(L2TPIPSecDriverTest, InitOptionsNoHost) {
206 Error error;
207 vector<string> options;
Darin Petkov209e6292012-04-20 11:33:32 +0200208 EXPECT_FALSE(driver_->InitOptions(&options, &error));
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200209 EXPECT_EQ(Error::kInvalidArguments, error.type());
210 EXPECT_TRUE(options.empty());
211}
212
213TEST_F(L2TPIPSecDriverTest, InitOptions) {
214 static const char kHost[] = "192.168.2.254";
215 static const char kCaCertNSS[] = "{1234}";
216 static const char kPSK[] = "foobar";
217
218 SetArg(flimflam::kProviderHostProperty, kHost);
219 SetArg(flimflam::kL2tpIpsecCaCertNssProperty, kCaCertNSS);
220 SetArg(flimflam::kL2tpIpsecPskProperty, kPSK);
221
222 FilePath empty_cert;
223 EXPECT_CALL(nss_, GetDERCertfile(kCaCertNSS, _)).WillOnce(Return(empty_cert));
224
225 const FilePath temp_dir(temp_dir_.path());
226 EXPECT_CALL(manager_, run_path()).WillOnce(ReturnRef(temp_dir));
227
228 Error error;
229 vector<string> options;
Darin Petkov209e6292012-04-20 11:33:32 +0200230 EXPECT_TRUE(driver_->InitOptions(&options, &error));
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200231 EXPECT_TRUE(error.IsSuccess());
232
233 ExpectInFlags(options, "--remote_host", kHost);
234 ASSERT_FALSE(driver_->psk_file_.empty());
235 ExpectInFlags(options, "--psk_file", driver_->psk_file_.value());
236}
237
238TEST_F(L2TPIPSecDriverTest, InitPSKOptions) {
239 Error error;
240 vector<string> options;
241 static const char kPSK[] = "foobar";
242 const FilePath bad_dir("/non/existent/directory");
243 const FilePath temp_dir(temp_dir_.path());
244 EXPECT_CALL(manager_, run_path())
245 .WillOnce(ReturnRef(bad_dir))
246 .WillOnce(ReturnRef(temp_dir));
247
248 EXPECT_TRUE(driver_->InitPSKOptions(&options, &error));
249 EXPECT_TRUE(options.empty());
250 EXPECT_TRUE(error.IsSuccess());
251
252 SetArg(flimflam::kL2tpIpsecPskProperty, kPSK);
253
254 EXPECT_FALSE(driver_->InitPSKOptions(&options, &error));
255 EXPECT_TRUE(options.empty());
256 EXPECT_EQ(Error::kInternalError, error.type());
257 error.Reset();
258
259 EXPECT_TRUE(driver_->InitPSKOptions(&options, &error));
260 ASSERT_FALSE(driver_->psk_file_.empty());
261 ExpectInFlags(options, "--psk_file", driver_->psk_file_.value());
262 EXPECT_TRUE(error.IsSuccess());
263 string contents;
264 EXPECT_TRUE(
265 file_util::ReadFileToString(driver_->psk_file_, &contents));
266 EXPECT_EQ(kPSK, contents);
267 struct stat buf;
268 ASSERT_EQ(0, stat(driver_->psk_file_.value().c_str(), &buf));
269 EXPECT_EQ(S_IFREG | S_IRUSR | S_IWUSR, buf.st_mode);
270}
271
272TEST_F(L2TPIPSecDriverTest, InitNSSOptions) {
273 static const char kHost[] = "192.168.2.254";
274 static const char kCaCertNSS[] = "{1234}";
275 static const char kNSSCertfile[] = "/tmp/nss-cert";
276 FilePath empty_cert;
277 FilePath nss_cert(kNSSCertfile);
278 SetArg(flimflam::kProviderHostProperty, kHost);
279 SetArg(flimflam::kL2tpIpsecCaCertNssProperty, kCaCertNSS);
280 EXPECT_CALL(nss_,
281 GetDERCertfile(kCaCertNSS,
282 ElementsAreArray(kHost, arraysize(kHost) - 1)))
283 .WillOnce(Return(empty_cert))
284 .WillOnce(Return(nss_cert));
285
286 vector<string> options;
287 driver_->InitNSSOptions(&options);
288 EXPECT_TRUE(options.empty());
289 driver_->InitNSSOptions(&options);
290 ExpectInFlags(options, "--server_ca_file", kNSSCertfile);
291}
292
293TEST_F(L2TPIPSecDriverTest, AppendValueOption) {
294 static const char kOption[] = "--l2tpipsec-option";
295 static const char kProperty[] = "L2TPIPSec.SomeProperty";
296 static const char kValue[] = "some-property-value";
297 static const char kOption2[] = "--l2tpipsec-option2";
298 static const char kProperty2[] = "L2TPIPSec.SomeProperty2";
299 static const char kValue2[] = "some-property-value2";
300
301 vector<string> options;
302 EXPECT_FALSE(
303 driver_->AppendValueOption(
304 "L2TPIPSec.UnknownProperty", kOption, &options));
305 EXPECT_TRUE(options.empty());
306
307 SetArg(kProperty, "");
308 EXPECT_FALSE(driver_->AppendValueOption(kProperty, kOption, &options));
309 EXPECT_TRUE(options.empty());
310
311 SetArg(kProperty, kValue);
312 SetArg(kProperty2, kValue2);
313 EXPECT_TRUE(driver_->AppendValueOption(kProperty, kOption, &options));
314 EXPECT_TRUE(driver_->AppendValueOption(kProperty2, kOption2, &options));
315 EXPECT_EQ(4, options.size());
316 EXPECT_EQ(kOption, options[0]);
317 EXPECT_EQ(kValue, options[1]);
318 EXPECT_EQ(kOption2, options[2]);
319 EXPECT_EQ(kValue2, options[3]);
320}
321
322TEST_F(L2TPIPSecDriverTest, AppendFlag) {
323 static const char kTrueOption[] = "--l2tpipsec-option";
324 static const char kFalseOption[] = "--nol2tpipsec-option";
325 static const char kProperty[] = "L2TPIPSec.SomeProperty";
326 static const char kTrueOption2[] = "--l2tpipsec-option2";
327 static const char kFalseOption2[] = "--nol2tpipsec-option2";
328 static const char kProperty2[] = "L2TPIPSec.SomeProperty2";
329
330 vector<string> options;
331 EXPECT_FALSE(driver_->AppendFlag("L2TPIPSec.UnknownProperty",
332 kTrueOption, kFalseOption, &options));
333 EXPECT_TRUE(options.empty());
334
335 SetArg(kProperty, "");
336 EXPECT_FALSE(
337 driver_->AppendFlag(kProperty, kTrueOption, kFalseOption, &options));
338 EXPECT_TRUE(options.empty());
339
340 SetArg(kProperty, "true");
341 SetArg(kProperty2, "false");
342 EXPECT_TRUE(
343 driver_->AppendFlag(kProperty, kTrueOption, kFalseOption, &options));
344 EXPECT_TRUE(
345 driver_->AppendFlag(kProperty2, kTrueOption2, kFalseOption2, &options));
346 EXPECT_EQ(2, options.size());
347 EXPECT_EQ(kTrueOption, options[0]);
348 EXPECT_EQ(kFalseOption2, options[1]);
349}
350
Darin Petkov209e6292012-04-20 11:33:32 +0200351TEST_F(L2TPIPSecDriverTest, GetLogin) {
352 static const char kUser[] = "joesmith";
353 static const char kPassword[] = "random-password";
354 string user, password;
355 SetArg(flimflam::kL2tpIpsecUserProperty, kUser);
356 driver_->GetLogin(&user, &password);
357 EXPECT_TRUE(user.empty());
358 EXPECT_TRUE(password.empty());
359 SetArg(flimflam::kL2tpIpsecUserProperty, "");
360 SetArg(flimflam::kL2tpIpsecPasswordProperty, kPassword);
361 driver_->GetLogin(&user, &password);
362 EXPECT_TRUE(user.empty());
363 EXPECT_TRUE(password.empty());
364 SetArg(flimflam::kL2tpIpsecUserProperty, kUser);
365 driver_->GetLogin(&user, &password);
366 EXPECT_EQ(kUser, user);
367 EXPECT_EQ(kPassword, password);
368}
369
370TEST_F(L2TPIPSecDriverTest, OnL2TPIPSecVPNDied) {
371 const int kPID = 99999;
372 driver_->child_watch_tag_ = 333;
373 driver_->pid_ = kPID;
374 EXPECT_CALL(glib_, SpawnClosePID(kPID));
375 L2TPIPSecDriver::OnL2TPIPSecVPNDied(kPID, 2, driver_);
376 EXPECT_EQ(0, driver_->child_watch_tag_);
377 EXPECT_EQ(0, driver_->pid_);
378}
379
380namespace {
381MATCHER(CheckEnv, "") {
382 if (!arg || !arg[0] || !arg[1] || !arg[2] || arg[3]) {
383 return false;
384 }
385 return StartsWithASCII(arg[0], "CONNMAN_", true);
386}
387} // namespace
388
389TEST_F(L2TPIPSecDriverTest, SpawnL2TPIPSecVPN) {
390 Error error;
391 EXPECT_FALSE(driver_->SpawnL2TPIPSecVPN(&error));
392 EXPECT_TRUE(error.IsFailure());
393
394 static const char kHost[] = "192.168.2.254";
395 SetArg(flimflam::kProviderHostProperty, kHost);
396 driver_->rpc_task_.reset(new RPCTask(&control_, this));
397
398 const int kPID = 234678;
399 EXPECT_CALL(glib_,
400 SpawnAsyncWithPipesCWD(_, CheckEnv(), _, _, _, _, _, _, _, _))
401 .WillOnce(Return(false))
402 .WillOnce(DoAll(SetArgumentPointee<5>(kPID), Return(true)));
403 const int kTag = 6;
404 EXPECT_CALL(glib_, ChildWatchAdd(kPID, &driver_->OnL2TPIPSecVPNDied, driver_))
405 .WillOnce(Return(kTag));
406 error.Reset();
407 EXPECT_FALSE(driver_->SpawnL2TPIPSecVPN(&error));
408 EXPECT_EQ(Error::kInternalError, error.type());
409 error.Reset();
410 EXPECT_TRUE(driver_->SpawnL2TPIPSecVPN(&error));
411 EXPECT_TRUE(error.IsSuccess());
412 EXPECT_EQ(kPID, driver_->pid_);
413 EXPECT_EQ(kTag, driver_->child_watch_tag_);
414}
415
416TEST_F(L2TPIPSecDriverTest, Connect) {
417 EXPECT_CALL(*service_, SetState(Service::kStateConfiguring));
418 static const char kHost[] = "192.168.2.254";
419 SetArg(flimflam::kProviderHostProperty, kHost);
420 EXPECT_CALL(glib_, SpawnAsyncWithPipesCWD(_, _, _, _, _, _, _, _, _, _))
421 .WillOnce(Return(true));
422 EXPECT_CALL(glib_, ChildWatchAdd(_, _, _)).WillOnce(Return(1));
423 Error error;
424 driver_->Connect(service_, &error);
425 EXPECT_TRUE(error.IsSuccess());
426}
427
Darin Petkova0e645e2012-04-25 11:38:59 +0200428TEST_F(L2TPIPSecDriverTest, Disconnect) {
429 driver_->device_ = device_;
430 driver_->service_ = service_;
431 EXPECT_CALL(*device_, OnDisconnected());
432 EXPECT_CALL(*device_, SetEnabled(false));
433 EXPECT_CALL(*service_, SetState(Service::kStateIdle));
434 driver_->Disconnect();
435 EXPECT_FALSE(driver_->device_);
436 EXPECT_FALSE(driver_->service_);
437}
438
Darin Petkovd4325392012-04-23 15:48:22 +0200439TEST_F(L2TPIPSecDriverTest, InitPropertyStore) {
440 // Sanity test property store initialization.
441 PropertyStore store;
442 driver_->InitPropertyStore(&store);
443 const string kUser = "joe";
444 Error error;
445 EXPECT_TRUE(
446 store.SetStringProperty(flimflam::kL2tpIpsecUserProperty, kUser, &error));
447 EXPECT_TRUE(error.IsSuccess());
Darin Petkovb536a742012-04-26 11:31:28 +0200448 EXPECT_EQ(kUser,
449 GetArgs()->LookupString(flimflam::kL2tpIpsecUserProperty, ""));
450}
451
452TEST_F(L2TPIPSecDriverTest, GetProvider) {
453 PropertyStore store;
454 driver_->InitPropertyStore(&store);
455 {
456 Error error;
457 KeyValueStore props;
458 EXPECT_TRUE(
459 FindKeyValueStorePropertyInStore(
460 store, flimflam::kProviderProperty, &props, &error));
461 EXPECT_TRUE(props.LookupBool(flimflam::kPassphraseRequiredProperty, false));
462 EXPECT_TRUE(
463 props.LookupBool(flimflam::kL2tpIpsecPskRequiredProperty, false));
464 }
465 {
466 Error error;
467 KeyValueStore props;
468 SetArg(flimflam::kL2tpIpsecPasswordProperty, "random-password");
469 SetArg(flimflam::kL2tpIpsecPskProperty, "random-psk");
470 EXPECT_TRUE(
471 FindKeyValueStorePropertyInStore(
472 store, flimflam::kProviderProperty, &props, &error));
473 EXPECT_FALSE(props.LookupBool(flimflam::kPassphraseRequiredProperty, true));
474 EXPECT_FALSE(
475 props.LookupBool(flimflam::kL2tpIpsecPskRequiredProperty, true));
476 }
Darin Petkovd4325392012-04-23 15:48:22 +0200477}
478
Darin Petkov0e9735d2012-04-24 12:33:45 +0200479TEST_F(L2TPIPSecDriverTest, ParseIPConfiguration) {
480 map<string, string> config;
481 config["INTERNAL_IP4_ADDRESS"] = "4.5.6.7";
482 config["EXTERNAL_IP4_ADDRESS"] = "33.44.55.66";
483 config["GATEWAY_ADDRESS"] = "192.168.1.1";
484 config["DNS1"] = "1.1.1.1";
485 config["DNS2"] = "2.2.2.2";
486 config["INTERNAL_IFNAME"] = "ppp0";
487 config["LNS_ADDRESS"] = "99.88.77.66";
488 config["foo"] = "bar";
489 IPConfig::Properties props;
490 string interface_name;
491 L2TPIPSecDriver::ParseIPConfiguration(config, &props, &interface_name);
492 EXPECT_EQ(IPAddress::kFamilyIPv4, props.address_family);
493 EXPECT_EQ("4.5.6.7", props.address);
494 EXPECT_EQ("33.44.55.66", props.peer_address);
495 EXPECT_EQ("192.168.1.1", props.gateway);
496 EXPECT_EQ("99.88.77.66", props.trusted_ip);
497 ASSERT_EQ(2, props.dns_servers.size());
498 EXPECT_EQ("1.1.1.1", props.dns_servers[0]);
499 EXPECT_EQ("2.2.2.2", props.dns_servers[1]);
500 EXPECT_EQ("ppp0", interface_name);
501}
502
503namespace {
504MATCHER_P(IsIPAddress, address, "") {
505 IPAddress ip_address(IPAddress::kFamilyIPv4);
506 EXPECT_TRUE(ip_address.SetAddressFromString(address));
507 return ip_address.Equals(arg);
508}
509} // namespace
510
511TEST_F(L2TPIPSecDriverTest, Notify) {
512 map<string, string> config;
Darin Petkovf8046b82012-04-24 16:29:23 +0200513 config["INTERNAL_IFNAME"] = kInterfaceName;
Darin Petkovf8046b82012-04-24 16:29:23 +0200514 EXPECT_CALL(device_info_, GetIndex(kInterfaceName))
515 .WillOnce(Return(kInterfaceIndex));
Darin Petkovf8046b82012-04-24 16:29:23 +0200516 EXPECT_CALL(*device_, SetEnabled(true));
517 EXPECT_CALL(*device_, UpdateIPConfig(_));
518 driver_->device_ = device_;
Darin Petkov0e9735d2012-04-24 12:33:45 +0200519 FilePath psk_file = SetupPSKFile();
520 driver_->Notify("connect", config);
521 EXPECT_FALSE(file_util::PathExists(psk_file));
522 EXPECT_TRUE(driver_->psk_file_.empty());
523}
524
Darin Petkova0e645e2012-04-25 11:38:59 +0200525TEST_F(L2TPIPSecDriverTest, NotifyFail) {
526 map<string, string> dict;
527 driver_->device_ = device_;
528 EXPECT_CALL(*device_, OnDisconnected());
529 driver_->Notify("fail", dict);
530}
531
532TEST_F(L2TPIPSecDriverTest, VerifyPaths) {
533 // Ensure that the various path constants that the L2TP/IPSec driver uses
534 // actually exists in the build image. Due to build dependencies, they should
535 // already exist by the time we run unit tests.
536
537 // The L2TPIPSecDriver path constants are absolute. FilePath::Append asserts
538 // that its argument is not an absolute path, so we need to strip the leading
539 // separators. There's nothing built into FilePath to do so.
540 static const char *kPaths[] = {
541 L2TPIPSecDriver::kL2TPIPSecVPNPath,
542 L2TPIPSecDriver::kPPPDPlugin,
543 };
544 for (size_t i = 0; i < arraysize(kPaths); i++) {
545 string path(kPaths[i]);
546 TrimString(path, FilePath::kSeparators, &path);
547 EXPECT_TRUE(file_util::PathExists(FilePath(SYSROOT).Append(path)))
548 << kPaths[i];
549 }
550}
551
Darin Petkov7476a262012-04-12 16:30:46 +0200552} // namespace shill