blob: 8e84d5ec23626140b7bd34797fb1b92ab126b8f2 [file] [log] [blame]
Darin Petkov33af05c2012-02-28 10:10:30 +01001// 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/openvpn_driver.h"
6
Darin Petkova9b1fed2012-02-29 11:49:05 +01007#include <algorithm>
8
Paul Stewart291a4732012-03-14 19:19:02 -07009#include <base/file_path.h>
10#include <base/file_util.h>
11#include <base/string_util.h>
Darin Petkovfe6a9372012-02-28 16:25:06 +010012#include <chromeos/dbus/service_constants.h>
Darin Petkov33af05c2012-02-28 10:10:30 +010013#include <gtest/gtest.h>
14
Paul Stewartebd38562012-03-23 13:06:40 -070015#include "shill/dbus_adaptor.h"
Darin Petkov33af05c2012-02-28 10:10:30 +010016#include "shill/error.h"
Darin Petkov14c29ec2012-03-02 11:34:19 +010017#include "shill/ipconfig.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070018#include "shill/logging.h"
Darin Petkova9b1fed2012-02-29 11:49:05 +010019#include "shill/mock_adaptors.h"
Paul Stewartca6abd42012-03-01 15:45:29 -080020#include "shill/mock_device_info.h"
Darin Petkovf20994f2012-03-05 16:12:19 +010021#include "shill/mock_glib.h"
22#include "shill/mock_manager.h"
23#include "shill/mock_metrics.h"
Darin Petkov3c5e4dc2012-04-02 14:44:27 +020024#include "shill/mock_nss.h"
Darin Petkov46463022012-03-29 14:57:32 +020025#include "shill/mock_openvpn_management_server.h"
Darin Petkov5a850472012-06-06 15:44:24 +020026#include "shill/mock_process_killer.h"
Darin Petkova5e07ef2012-07-09 14:27:57 +020027#include "shill/mock_service.h"
Darin Petkovf3c71d72012-03-21 12:32:15 +010028#include "shill/mock_store.h"
Darin Petkov36a3ace2012-03-06 17:22:14 +010029#include "shill/mock_vpn.h"
Darin Petkov79d74c92012-03-07 17:20:32 +010030#include "shill/mock_vpn_service.h"
Darin Petkova9b1fed2012-02-29 11:49:05 +010031#include "shill/nice_mock_control.h"
32#include "shill/rpc_task.h"
Darin Petkovf20994f2012-03-05 16:12:19 +010033#include "shill/vpn.h"
Darin Petkov79d74c92012-03-07 17:20:32 +010034#include "shill/vpn_service.h"
Darin Petkov33af05c2012-02-28 10:10:30 +010035
Darin Petkov5a850472012-06-06 15:44:24 +020036using base::WeakPtr;
Darin Petkova9b1fed2012-02-29 11:49:05 +010037using std::map;
Darin Petkovfe6a9372012-02-28 16:25:06 +010038using std::string;
39using std::vector;
Paul Stewartca6abd42012-03-01 15:45:29 -080040using testing::_;
Darin Petkovf3c71d72012-03-21 12:32:15 +010041using testing::AnyNumber;
Paul Stewartca6abd42012-03-01 15:45:29 -080042using testing::DoAll;
Darin Petkov3c5e4dc2012-04-02 14:44:27 +020043using testing::ElementsAreArray;
Darin Petkove8587e32012-07-02 13:56:07 +020044using testing::Field;
Darin Petkovf3c71d72012-03-21 12:32:15 +010045using testing::Ne;
Paul Stewartce4ec192012-03-14 12:53:46 -070046using testing::NiceMock;
Paul Stewartca6abd42012-03-01 15:45:29 -080047using testing::Return;
48using testing::SetArgumentPointee;
Paul Stewartce4ec192012-03-14 12:53:46 -070049using testing::StrictMock;
Darin Petkovfe6a9372012-02-28 16:25:06 +010050
Darin Petkov33af05c2012-02-28 10:10:30 +010051namespace shill {
52
Darin Petkova9b1fed2012-02-29 11:49:05 +010053class OpenVPNDriverTest : public testing::Test,
54 public RPCTaskDelegate {
Darin Petkov33af05c2012-02-28 10:10:30 +010055 public:
Darin Petkovfe6a9372012-02-28 16:25:06 +010056 OpenVPNDriverTest()
Darin Petkov0e9735d2012-04-24 12:33:45 +020057 : device_info_(&control_, &dispatcher_, &metrics_, &manager_),
Darin Petkovf20994f2012-03-05 16:12:19 +010058 manager_(&control_, &dispatcher_, &metrics_, &glib_),
Darin Petkov79d74c92012-03-07 17:20:32 +010059 driver_(new OpenVPNDriver(&control_, &dispatcher_, &metrics_, &manager_,
Paul Stewart451aa7f2012-04-11 19:07:58 -070060 &device_info_, &glib_)),
Darin Petkov79d74c92012-03-07 17:20:32 +010061 service_(new MockVPNService(&control_, &dispatcher_, &metrics_,
62 &manager_, driver_)),
63 device_(new MockVPN(&control_, &dispatcher_, &metrics_, &manager_,
Darin Petkov46463022012-03-29 14:57:32 +020064 kInterfaceName, kInterfaceIndex)),
65 management_server_(new NiceMock<MockOpenVPNManagementServer>()) {
66 driver_->management_server_.reset(management_server_);
Darin Petkov3c5e4dc2012-04-02 14:44:27 +020067 driver_->nss_ = &nss_;
Darin Petkov5a850472012-06-06 15:44:24 +020068 driver_->process_killer_ = &process_killer_;
Darin Petkov46463022012-03-29 14:57:32 +020069 }
Darin Petkovfe6a9372012-02-28 16:25:06 +010070
Darin Petkov33af05c2012-02-28 10:10:30 +010071 virtual ~OpenVPNDriverTest() {}
72
Darin Petkov36a3ace2012-03-06 17:22:14 +010073 virtual void TearDown() {
Darin Petkova5e07ef2012-07-09 14:27:57 +020074 driver_->default_service_callback_tag_ = 0;
Darin Petkov79d74c92012-03-07 17:20:32 +010075 driver_->child_watch_tag_ = 0;
76 driver_->pid_ = 0;
77 driver_->device_ = NULL;
78 driver_->service_ = NULL;
Darin Petkov1a462de2012-05-02 11:10:48 +020079 if (!lsb_release_file_.empty()) {
80 EXPECT_TRUE(file_util::Delete(lsb_release_file_, false));
81 lsb_release_file_.clear();
82 }
Darin Petkov36a3ace2012-03-06 17:22:14 +010083 }
Darin Petkova9b1fed2012-02-29 11:49:05 +010084
Darin Petkov33af05c2012-02-28 10:10:30 +010085 protected:
Darin Petkovfe6a9372012-02-28 16:25:06 +010086 static const char kOption[];
87 static const char kProperty[];
88 static const char kValue[];
89 static const char kOption2[];
90 static const char kProperty2[];
91 static const char kValue2[];
Darin Petkov60596742012-03-05 12:17:17 +010092 static const char kGateway1[];
93 static const char kNetmask1[];
94 static const char kNetwork1[];
95 static const char kGateway2[];
96 static const char kNetmask2[];
97 static const char kNetwork2[];
Darin Petkov79d74c92012-03-07 17:20:32 +010098 static const char kInterfaceName[];
99 static const int kInterfaceIndex;
Darin Petkovfe6a9372012-02-28 16:25:06 +0100100
Darin Petkovb451d6e2012-04-23 11:56:41 +0200101 void SetArg(const string &arg, const string &value) {
102 driver_->args()->SetString(arg, value);
Darin Petkovfe6a9372012-02-28 16:25:06 +0100103 }
104
Darin Petkovf3c71d72012-03-21 12:32:15 +0100105 KeyValueStore *GetArgs() {
Darin Petkovb451d6e2012-04-23 11:56:41 +0200106 return driver_->args();
Darin Petkovf3c71d72012-03-21 12:32:15 +0100107 }
108
Darin Petkov4e1b3f82012-09-27 13:22:37 +0200109 void RemoveStringArg(const string &arg) {
110 driver_->args()->RemoveString(arg);
111 }
112
Darin Petkov3189a472012-10-05 09:55:33 +0200113 const ServiceRefPtr &GetSelectedService() {
114 return device_->selected_service();
115 }
116
Paul Stewartca6abd42012-03-01 15:45:29 -0800117 // Used to assert that a flag appears in the options.
118 void ExpectInFlags(const vector<string> &options, const string &flag,
119 const string &value);
Darin Petkov4e1b3f82012-09-27 13:22:37 +0200120 void ExpectInFlags(const vector<string> &options, const string &flag);
121 void ExpectNotInFlags(const vector<string> &options, const string &flag);
Paul Stewartca6abd42012-03-01 15:45:29 -0800122
Darin Petkov1a462de2012-05-02 11:10:48 +0200123 void SetupLSBRelease();
124
Darin Petkov36a3ace2012-03-06 17:22:14 +0100125 // Inherited from RPCTaskDelegate.
Darin Petkov209e6292012-04-20 11:33:32 +0200126 virtual void GetLogin(string *user, string *password);
Darin Petkov36a3ace2012-03-06 17:22:14 +0100127 virtual void Notify(const string &reason, const map<string, string> &dict);
Paul Stewartca6abd42012-03-01 15:45:29 -0800128
Darin Petkova9b1fed2012-02-29 11:49:05 +0100129 NiceMockControl control_;
Darin Petkov0e9735d2012-04-24 12:33:45 +0200130 NiceMock<MockDeviceInfo> device_info_;
Darin Petkovf20994f2012-03-05 16:12:19 +0100131 EventDispatcher dispatcher_;
132 MockMetrics metrics_;
133 MockGLib glib_;
134 MockManager manager_;
Darin Petkov46463022012-03-29 14:57:32 +0200135 OpenVPNDriver *driver_; // Owned by |service_|.
Darin Petkov79d74c92012-03-07 17:20:32 +0100136 scoped_refptr<MockVPNService> service_;
137 scoped_refptr<MockVPN> device_;
Darin Petkov3c5e4dc2012-04-02 14:44:27 +0200138 MockNSS nss_;
Darin Petkov5a850472012-06-06 15:44:24 +0200139 MockProcessKiller process_killer_;
Darin Petkov46463022012-03-29 14:57:32 +0200140
141 // Owned by |driver_|.
142 NiceMock<MockOpenVPNManagementServer> *management_server_;
Darin Petkov1a462de2012-05-02 11:10:48 +0200143
144 FilePath lsb_release_file_;
Darin Petkov33af05c2012-02-28 10:10:30 +0100145};
146
Darin Petkovfe6a9372012-02-28 16:25:06 +0100147const char OpenVPNDriverTest::kOption[] = "--openvpn-option";
148const char OpenVPNDriverTest::kProperty[] = "OpenVPN.SomeProperty";
149const char OpenVPNDriverTest::kValue[] = "some-property-value";
150const char OpenVPNDriverTest::kOption2[] = "--openvpn-option2";
151const char OpenVPNDriverTest::kProperty2[] = "OpenVPN.SomeProperty2";
152const char OpenVPNDriverTest::kValue2[] = "some-property-value2";
Darin Petkov60596742012-03-05 12:17:17 +0100153const char OpenVPNDriverTest::kGateway1[] = "10.242.2.13";
154const char OpenVPNDriverTest::kNetmask1[] = "255.255.255.255";
155const char OpenVPNDriverTest::kNetwork1[] = "10.242.2.1";
156const char OpenVPNDriverTest::kGateway2[] = "10.242.2.14";
157const char OpenVPNDriverTest::kNetmask2[] = "255.255.0.0";
158const char OpenVPNDriverTest::kNetwork2[] = "192.168.0.0";
Darin Petkov79d74c92012-03-07 17:20:32 +0100159const char OpenVPNDriverTest::kInterfaceName[] = "tun0";
160const int OpenVPNDriverTest::kInterfaceIndex = 123;
Darin Petkovfe6a9372012-02-28 16:25:06 +0100161
Darin Petkov209e6292012-04-20 11:33:32 +0200162void OpenVPNDriverTest::GetLogin(string */*user*/, string */*password*/) {}
163
Darin Petkova9b1fed2012-02-29 11:49:05 +0100164void OpenVPNDriverTest::Notify(const string &/*reason*/,
165 const map<string, string> &/*dict*/) {}
166
Paul Stewartca6abd42012-03-01 15:45:29 -0800167void OpenVPNDriverTest::ExpectInFlags(const vector<string> &options,
168 const string &flag,
169 const string &value) {
170 vector<string>::const_iterator it =
171 std::find(options.begin(), options.end(), flag);
172
173 EXPECT_TRUE(it != options.end());
174 if (it != options.end())
Darin Petkov36a3ace2012-03-06 17:22:14 +0100175 return; // Don't crash below.
Paul Stewartca6abd42012-03-01 15:45:29 -0800176 it++;
177 EXPECT_TRUE(it != options.end());
178 if (it != options.end())
Darin Petkov36a3ace2012-03-06 17:22:14 +0100179 return; // Don't crash below.
Paul Stewartca6abd42012-03-01 15:45:29 -0800180 EXPECT_EQ(value, *it);
181}
182
Darin Petkov4e1b3f82012-09-27 13:22:37 +0200183void OpenVPNDriverTest::ExpectInFlags(const vector<string> &options,
184 const string &flag) {
185 EXPECT_TRUE(std::find(options.begin(), options.end(), flag) !=
186 options.end());
187}
188
189void OpenVPNDriverTest::ExpectNotInFlags(const vector<string> &options,
190 const string &flag) {
191 EXPECT_TRUE(std::find(options.begin(), options.end(), flag) ==
192 options.end());
193}
194
Darin Petkov1a462de2012-05-02 11:10:48 +0200195void OpenVPNDriverTest::SetupLSBRelease() {
196 static const char kLSBReleaseContents[] =
197 "\n"
198 "=\n"
199 "foo=\n"
200 "=bar\n"
201 "zoo==\n"
202 "CHROMEOS_RELEASE_BOARD=x86-alex\n"
203 "CHROMEOS_RELEASE_NAME=Chromium OS\n"
204 "CHROMEOS_RELEASE_VERSION=2202.0\n";
205 EXPECT_TRUE(file_util::CreateTemporaryFile(&lsb_release_file_));
206 EXPECT_EQ(arraysize(kLSBReleaseContents),
207 file_util::WriteFile(lsb_release_file_,
208 kLSBReleaseContents,
209 arraysize(kLSBReleaseContents)));
210 EXPECT_EQ(OpenVPNDriver::kLSBReleaseFile, driver_->lsb_release_file_.value());
211 driver_->lsb_release_file_ = lsb_release_file_;
212}
213
Darin Petkov33af05c2012-02-28 10:10:30 +0100214TEST_F(OpenVPNDriverTest, Connect) {
Darin Petkov79d74c92012-03-07 17:20:32 +0100215 EXPECT_CALL(*service_, SetState(Service::kStateConfiguring));
216 const string interface = kInterfaceName;
Darin Petkovf20994f2012-03-05 16:12:19 +0100217 EXPECT_CALL(device_info_, CreateTunnelInterface(_))
Darin Petkov79d74c92012-03-07 17:20:32 +0100218 .WillOnce(DoAll(SetArgumentPointee<0>(interface), Return(true)));
219 Error error;
220 driver_->Connect(service_, &error);
221 EXPECT_TRUE(error.IsSuccess());
222 EXPECT_EQ(kInterfaceName, driver_->tunnel_interface_);
Darin Petkov602303f2012-06-06 12:15:59 +0200223 EXPECT_TRUE(driver_->IsConnectTimeoutStarted());
Darin Petkov79d74c92012-03-07 17:20:32 +0100224}
225
226TEST_F(OpenVPNDriverTest, ConnectTunnelFailure) {
227 EXPECT_CALL(*service_, SetState(Service::kStateConfiguring));
Darin Petkov0e9735d2012-04-24 12:33:45 +0200228 EXPECT_CALL(device_info_, CreateTunnelInterface(_)).WillOnce(Return(false));
Darin Petkov79d74c92012-03-07 17:20:32 +0100229 EXPECT_CALL(*service_, SetState(Service::kStateFailure));
230 Error error;
231 driver_->Connect(service_, &error);
232 EXPECT_EQ(Error::kInternalError, error.type());
233 EXPECT_TRUE(driver_->tunnel_interface_.empty());
Darin Petkov602303f2012-06-06 12:15:59 +0200234 EXPECT_FALSE(driver_->IsConnectTimeoutStarted());
Darin Petkov33af05c2012-02-28 10:10:30 +0100235}
236
Darin Petkov0e9735d2012-04-24 12:33:45 +0200237namespace {
238MATCHER_P(IsIPAddress, address, "") {
239 IPAddress ip_address(IPAddress::kFamilyIPv4);
240 EXPECT_TRUE(ip_address.SetAddressFromString(address));
241 return ip_address.Equals(arg);
242}
243} // namespace
244
Darin Petkov14c29ec2012-03-02 11:34:19 +0100245TEST_F(OpenVPNDriverTest, Notify) {
Darin Petkov0e9735d2012-04-24 12:33:45 +0200246 map<string, string> config;
Darin Petkov3189a472012-10-05 09:55:33 +0200247 driver_->service_ = service_;
Darin Petkov79d74c92012-03-07 17:20:32 +0100248 driver_->device_ = device_;
Darin Petkov602303f2012-06-06 12:15:59 +0200249 driver_->StartConnectTimeout();
Darin Petkove8587e32012-07-02 13:56:07 +0200250 EXPECT_CALL(*device_,
251 UpdateIPConfig(Field(&IPConfig::Properties::address, "")));
Darin Petkov0e9735d2012-04-24 12:33:45 +0200252 driver_->Notify("up", config);
Darin Petkov602303f2012-06-06 12:15:59 +0200253 EXPECT_FALSE(driver_->IsConnectTimeoutStarted());
Darin Petkov3189a472012-10-05 09:55:33 +0200254 EXPECT_TRUE(GetSelectedService().get() == service_.get());
Darin Petkove8587e32012-07-02 13:56:07 +0200255
256 // Tests that existing properties are reused if no new ones provided.
Darin Petkov3189a472012-10-05 09:55:33 +0200257 driver_->ip_properties_.address = "1.2.3.4";
Darin Petkove8587e32012-07-02 13:56:07 +0200258 EXPECT_CALL(*device_,
259 UpdateIPConfig(Field(&IPConfig::Properties::address, "1.2.3.4")));
260 driver_->Notify("up", config);
Darin Petkov79d74c92012-03-07 17:20:32 +0100261}
262
263TEST_F(OpenVPNDriverTest, NotifyFail) {
264 map<string, string> dict;
265 driver_->device_ = device_;
Darin Petkov602303f2012-06-06 12:15:59 +0200266 driver_->StartConnectTimeout();
Darin Petkov79d74c92012-03-07 17:20:32 +0100267 EXPECT_CALL(*device_, OnDisconnected());
268 driver_->Notify("fail", dict);
Darin Petkov602303f2012-06-06 12:15:59 +0200269 EXPECT_TRUE(driver_->IsConnectTimeoutStarted());
Darin Petkov14c29ec2012-03-02 11:34:19 +0100270}
271
Darin Petkov60596742012-03-05 12:17:17 +0100272TEST_F(OpenVPNDriverTest, GetRouteOptionEntry) {
273 OpenVPNDriver::RouteOptions routes;
274 EXPECT_EQ(NULL, OpenVPNDriver::GetRouteOptionEntry("foo", "bar", &routes));
275 EXPECT_TRUE(routes.empty());
276 EXPECT_EQ(NULL, OpenVPNDriver::GetRouteOptionEntry("foo", "foo", &routes));
277 EXPECT_TRUE(routes.empty());
278 EXPECT_EQ(NULL, OpenVPNDriver::GetRouteOptionEntry("foo", "fooZ", &routes));
279 EXPECT_TRUE(routes.empty());
280 IPConfig::Route *route =
281 OpenVPNDriver::GetRouteOptionEntry("foo", "foo12", &routes);
282 EXPECT_EQ(1, routes.size());
283 EXPECT_EQ(route, &routes[12]);
284 route = OpenVPNDriver::GetRouteOptionEntry("foo", "foo13", &routes);
285 EXPECT_EQ(2, routes.size());
286 EXPECT_EQ(route, &routes[13]);
287}
288
289TEST_F(OpenVPNDriverTest, ParseRouteOption) {
290 OpenVPNDriver::RouteOptions routes;
291 OpenVPNDriver::ParseRouteOption("foo", "bar", &routes);
292 EXPECT_TRUE(routes.empty());
293 OpenVPNDriver::ParseRouteOption("gateway_2", kGateway2, &routes);
294 OpenVPNDriver::ParseRouteOption("netmask_2", kNetmask2, &routes);
295 OpenVPNDriver::ParseRouteOption("network_2", kNetwork2, &routes);
296 EXPECT_EQ(1, routes.size());
297 OpenVPNDriver::ParseRouteOption("gateway_1", kGateway1, &routes);
298 OpenVPNDriver::ParseRouteOption("netmask_1", kNetmask1, &routes);
299 OpenVPNDriver::ParseRouteOption("network_1", kNetwork1, &routes);
300 EXPECT_EQ(2, routes.size());
301 EXPECT_EQ(kGateway1, routes[1].gateway);
302 EXPECT_EQ(kNetmask1, routes[1].netmask);
303 EXPECT_EQ(kNetwork1, routes[1].host);
304 EXPECT_EQ(kGateway2, routes[2].gateway);
305 EXPECT_EQ(kNetmask2, routes[2].netmask);
306 EXPECT_EQ(kNetwork2, routes[2].host);
307}
308
309TEST_F(OpenVPNDriverTest, SetRoutes) {
310 OpenVPNDriver::RouteOptions routes;
311 routes[1].gateway = "1.2.3.4";
312 routes[1].host= "1.2.3.4";
313 routes[2].host = "2.3.4.5";
314 routes[2].netmask = "255.0.0.0";
315 routes[3].netmask = "255.0.0.0";
316 routes[3].gateway = "1.2.3.5";
317 routes[5].host = kNetwork2;
318 routes[5].netmask = kNetmask2;
319 routes[5].gateway = kGateway2;
320 routes[4].host = kNetwork1;
321 routes[4].netmask = kNetmask1;
322 routes[4].gateway = kGateway1;
323 IPConfig::Properties props;
324 OpenVPNDriver::SetRoutes(routes, &props);
325 ASSERT_EQ(2, props.routes.size());
326 EXPECT_EQ(kGateway1, props.routes[0].gateway);
327 EXPECT_EQ(kNetmask1, props.routes[0].netmask);
328 EXPECT_EQ(kNetwork1, props.routes[0].host);
329 EXPECT_EQ(kGateway2, props.routes[1].gateway);
330 EXPECT_EQ(kNetmask2, props.routes[1].netmask);
331 EXPECT_EQ(kNetwork2, props.routes[1].host);
Darin Petkove8587e32012-07-02 13:56:07 +0200332
333 // Tests that the routes are not reset if no new routes are supplied.
334 OpenVPNDriver::SetRoutes(OpenVPNDriver::RouteOptions(), &props);
335 EXPECT_EQ(2, props.routes.size());
Darin Petkov60596742012-03-05 12:17:17 +0100336}
337
Darin Petkov4b944842012-09-21 10:48:48 +0200338TEST_F(OpenVPNDriverTest, SplitPortFromHost) {
339 string name, port;
340 EXPECT_FALSE(OpenVPNDriver::SplitPortFromHost("", NULL, NULL));
341 EXPECT_FALSE(OpenVPNDriver::SplitPortFromHost("", &name, &port));
342 EXPECT_FALSE(OpenVPNDriver::SplitPortFromHost("v.com", &name, &port));
343 EXPECT_FALSE(OpenVPNDriver::SplitPortFromHost("v.com:", &name, &port));
344 EXPECT_FALSE(OpenVPNDriver::SplitPortFromHost(":1234", &name, &port));
345 EXPECT_FALSE(OpenVPNDriver::SplitPortFromHost("v.com:f:1234", &name, &port));
346 EXPECT_FALSE(OpenVPNDriver::SplitPortFromHost("v.com:x", &name, &port));
347 EXPECT_FALSE(OpenVPNDriver::SplitPortFromHost("v.com:-1", &name, &port));
348 EXPECT_FALSE(OpenVPNDriver::SplitPortFromHost("v.com:+1", &name, &port));
349 EXPECT_FALSE(OpenVPNDriver::SplitPortFromHost("v.com:65536", &name, &port));
350 EXPECT_TRUE(OpenVPNDriver::SplitPortFromHost("v.com:0", &name, &port));
351 EXPECT_EQ("v.com", name);
352 EXPECT_EQ("0", port);
353 EXPECT_TRUE(OpenVPNDriver::SplitPortFromHost("w.com:65535", &name, &port));
354 EXPECT_EQ("w.com", name);
355 EXPECT_EQ("65535", port);
356 EXPECT_TRUE(OpenVPNDriver::SplitPortFromHost("x.com:12345", &name, &port));
357 EXPECT_EQ("x.com", name);
358 EXPECT_EQ("12345", port);
359}
360
Darin Petkov14c29ec2012-03-02 11:34:19 +0100361TEST_F(OpenVPNDriverTest, ParseForeignOption) {
Darin Petkove8587e32012-07-02 13:56:07 +0200362 vector<string> domain_search;
363 vector<string> dns_servers;
Darin Petkov14c29ec2012-03-02 11:34:19 +0100364 IPConfig::Properties props;
Darin Petkove8587e32012-07-02 13:56:07 +0200365 OpenVPNDriver::ParseForeignOption("", &domain_search, &dns_servers);
366 OpenVPNDriver::ParseForeignOption(
367 "dhcp-option DOMAIN", &domain_search, &dns_servers);
368 OpenVPNDriver::ParseForeignOption(
369 "dhcp-option DOMAIN zzz.com foo", &domain_search, &dns_servers);
370 OpenVPNDriver::ParseForeignOption(
371 "dhcp-Option DOmAIN xyz.com", &domain_search, &dns_servers);
372 ASSERT_EQ(1, domain_search.size());
373 EXPECT_EQ("xyz.com", domain_search[0]);
374 OpenVPNDriver::ParseForeignOption(
375 "dhcp-option DnS 1.2.3.4", &domain_search, &dns_servers);
376 ASSERT_EQ(1, dns_servers.size());
377 EXPECT_EQ("1.2.3.4", dns_servers[0]);
Darin Petkov14c29ec2012-03-02 11:34:19 +0100378}
379
380TEST_F(OpenVPNDriverTest, ParseForeignOptions) {
Darin Petkove8587e32012-07-02 13:56:07 +0200381 // This also tests that std::map is a sorted container.
Darin Petkov14c29ec2012-03-02 11:34:19 +0100382 map<int, string> options;
383 options[5] = "dhcp-option DOMAIN five.com";
384 options[2] = "dhcp-option DOMAIN two.com";
385 options[8] = "dhcp-option DOMAIN eight.com";
386 options[7] = "dhcp-option DOMAIN seven.com";
387 options[4] = "dhcp-option DOMAIN four.com";
Darin Petkove8587e32012-07-02 13:56:07 +0200388 options[10] = "dhcp-option dns 1.2.3.4";
Darin Petkov14c29ec2012-03-02 11:34:19 +0100389 IPConfig::Properties props;
390 OpenVPNDriver::ParseForeignOptions(options, &props);
391 ASSERT_EQ(5, props.domain_search.size());
392 EXPECT_EQ("two.com", props.domain_search[0]);
393 EXPECT_EQ("four.com", props.domain_search[1]);
394 EXPECT_EQ("five.com", props.domain_search[2]);
395 EXPECT_EQ("seven.com", props.domain_search[3]);
396 EXPECT_EQ("eight.com", props.domain_search[4]);
Darin Petkove8587e32012-07-02 13:56:07 +0200397 ASSERT_EQ(1, props.dns_servers.size());
398 EXPECT_EQ("1.2.3.4", props.dns_servers[0]);
399
400 // Test that the DNS properties are not updated if no new DNS properties are
401 // supplied.
402 OpenVPNDriver::ParseForeignOptions(map<int, string>(), &props);
403 EXPECT_EQ(5, props.domain_search.size());
404 ASSERT_EQ(1, props.dns_servers.size());
Darin Petkov14c29ec2012-03-02 11:34:19 +0100405}
406
407TEST_F(OpenVPNDriverTest, ParseIPConfiguration) {
408 map<string, string> config;
Darin Petkove8587e32012-07-02 13:56:07 +0200409 IPConfig::Properties props;
410
411 OpenVPNDriver::ParseIPConfiguration(config, &props);
412 EXPECT_EQ(IPAddress::kFamilyIPv4, props.address_family);
413 EXPECT_EQ(32, props.subnet_prefix);
414
415 props.subnet_prefix = 18;
416 OpenVPNDriver::ParseIPConfiguration(config, &props);
417 EXPECT_EQ(18, props.subnet_prefix);
418
Darin Petkov14c29ec2012-03-02 11:34:19 +0100419 config["ifconfig_loCal"] = "4.5.6.7";
420 config["ifconfiG_broadcast"] = "1.2.255.255";
421 config["ifconFig_netmAsk"] = "255.255.255.0";
422 config["ifconfig_remotE"] = "33.44.55.66";
423 config["route_vpN_gateway"] = "192.168.1.1";
Paul Stewartce4ec192012-03-14 12:53:46 -0700424 config["trusted_ip"] = "99.88.77.66";
Darin Petkov14c29ec2012-03-02 11:34:19 +0100425 config["tun_mtu"] = "1000";
426 config["foreign_option_2"] = "dhcp-option DNS 4.4.4.4";
427 config["foreign_option_1"] = "dhcp-option DNS 1.1.1.1";
428 config["foreign_option_3"] = "dhcp-option DNS 2.2.2.2";
Darin Petkov60596742012-03-05 12:17:17 +0100429 config["route_network_2"] = kNetwork2;
430 config["route_network_1"] = kNetwork1;
431 config["route_netmask_2"] = kNetmask2;
432 config["route_netmask_1"] = kNetmask1;
433 config["route_gateway_2"] = kGateway2;
434 config["route_gateway_1"] = kGateway1;
Darin Petkov14c29ec2012-03-02 11:34:19 +0100435 config["foo"] = "bar";
Darin Petkov14c29ec2012-03-02 11:34:19 +0100436 OpenVPNDriver::ParseIPConfiguration(config, &props);
437 EXPECT_EQ(IPAddress::kFamilyIPv4, props.address_family);
438 EXPECT_EQ("4.5.6.7", props.address);
439 EXPECT_EQ("1.2.255.255", props.broadcast_address);
Paul Stewart48100b02012-03-19 07:53:52 -0700440 EXPECT_EQ(24, props.subnet_prefix);
Darin Petkov14c29ec2012-03-02 11:34:19 +0100441 EXPECT_EQ("33.44.55.66", props.peer_address);
442 EXPECT_EQ("192.168.1.1", props.gateway);
Paul Stewartce4ec192012-03-14 12:53:46 -0700443 EXPECT_EQ("99.88.77.66", props.trusted_ip);
Darin Petkov14c29ec2012-03-02 11:34:19 +0100444 EXPECT_EQ(1000, props.mtu);
445 ASSERT_EQ(3, props.dns_servers.size());
446 EXPECT_EQ("1.1.1.1", props.dns_servers[0]);
447 EXPECT_EQ("4.4.4.4", props.dns_servers[1]);
448 EXPECT_EQ("2.2.2.2", props.dns_servers[2]);
Darin Petkov60596742012-03-05 12:17:17 +0100449 ASSERT_EQ(2, props.routes.size());
450 EXPECT_EQ(kGateway1, props.routes[0].gateway);
451 EXPECT_EQ(kNetmask1, props.routes[0].netmask);
452 EXPECT_EQ(kNetwork1, props.routes[0].host);
453 EXPECT_EQ(kGateway2, props.routes[1].gateway);
454 EXPECT_EQ(kNetmask2, props.routes[1].netmask);
455 EXPECT_EQ(kNetwork2, props.routes[1].host);
Ben Chana0163122012-09-25 15:10:52 -0700456 EXPECT_FALSE(props.blackhole_ipv6);
Darin Petkov14c29ec2012-03-02 11:34:19 +0100457}
458
Darin Petkovfe6a9372012-02-28 16:25:06 +0100459TEST_F(OpenVPNDriverTest, InitOptionsNoHost) {
460 Error error;
461 vector<string> options;
Darin Petkov79d74c92012-03-07 17:20:32 +0100462 driver_->InitOptions(&options, &error);
Darin Petkovfe6a9372012-02-28 16:25:06 +0100463 EXPECT_EQ(Error::kInvalidArguments, error.type());
464 EXPECT_TRUE(options.empty());
465}
466
467TEST_F(OpenVPNDriverTest, InitOptions) {
468 static const char kHost[] = "192.168.2.254";
Darin Petkov1fa81942012-04-02 11:38:08 +0200469 static const char kTLSAuthContents[] = "SOME-RANDOM-CONTENTS\n";
Darin Petkove0d5dd12012-04-04 16:10:48 +0200470 static const char kID[] = "TestPKCS11ID";
471 FilePath empty_cert;
472 SetArg(flimflam::kProviderHostProperty, kHost);
473 SetArg(flimflam::kOpenVPNTLSAuthContentsProperty, kTLSAuthContents);
Darin Petkove0d5dd12012-04-04 16:10:48 +0200474 SetArg(flimflam::kOpenVPNClientCertIdProperty, kID);
Darin Petkov79d74c92012-03-07 17:20:32 +0100475 driver_->rpc_task_.reset(new RPCTask(&control_, this));
Darin Petkov79d74c92012-03-07 17:20:32 +0100476 driver_->tunnel_interface_ = kInterfaceName;
Darin Petkove0d5dd12012-04-04 16:10:48 +0200477 EXPECT_CALL(*management_server_, Start(_, _, _)).WillOnce(Return(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +0200478 ServiceRefPtr null_service;
479 EXPECT_CALL(manager_, GetDefaultService()).WillOnce(Return(null_service));
Darin Petkove0d5dd12012-04-04 16:10:48 +0200480
481 Error error;
482 vector<string> options;
483 driver_->InitOptions(&options, &error);
484 EXPECT_TRUE(error.IsSuccess());
485 EXPECT_EQ("--client", options[0]);
486 ExpectInFlags(options, "--remote", kHost);
Darin Petkov728faa92012-10-12 11:25:47 +0200487 ExpectInFlags(options, kRPCTaskPathVariable, RPCTaskMockAdaptor::kRpcId);
Darin Petkove0d5dd12012-04-04 16:10:48 +0200488 ExpectInFlags(options, "--dev", kInterfaceName);
489 ExpectInFlags(options, "--group", "openvpn");
490 EXPECT_EQ(kInterfaceName, driver_->tunnel_interface_);
491 ASSERT_FALSE(driver_->tls_auth_file_.empty());
492 ExpectInFlags(options, "--tls-auth", driver_->tls_auth_file_.value());
493 string contents;
494 EXPECT_TRUE(
495 file_util::ReadFileToString(driver_->tls_auth_file_, &contents));
496 EXPECT_EQ(kTLSAuthContents, contents);
497 ExpectInFlags(options, "--pkcs11-id", kID);
Darin Petkovc418b4b2012-10-05 11:42:52 +0200498 ExpectInFlags(options, "--ca", OpenVPNDriver::kDefaultCACertificates);
Darin Petkov4e1b3f82012-09-27 13:22:37 +0200499 ExpectInFlags(options, "--syslog");
500 ExpectInFlags(options, "--auth-user-pass");
Darin Petkove0d5dd12012-04-04 16:10:48 +0200501}
502
Darin Petkov4b944842012-09-21 10:48:48 +0200503TEST_F(OpenVPNDriverTest, InitOptionsHostWithPort) {
504 SetArg(flimflam::kProviderHostProperty, "v.com:1234");
505 driver_->rpc_task_.reset(new RPCTask(&control_, this));
506 driver_->tunnel_interface_ = kInterfaceName;
507 EXPECT_CALL(*management_server_, Start(_, _, _)).WillOnce(Return(true));
508 ServiceRefPtr null_service;
509 EXPECT_CALL(manager_, GetDefaultService()).WillOnce(Return(null_service));
510
511 Error error;
512 vector<string> options;
513 driver_->InitOptions(&options, &error);
514 EXPECT_TRUE(error.IsSuccess());
515 vector<string>::const_iterator it =
516 std::find(options.begin(), options.end(), "--remote");
517 ASSERT_TRUE(it != options.end());
518 ASSERT_TRUE(++it != options.end());
519 EXPECT_EQ("v.com", *it);
520 ASSERT_TRUE(++it != options.end());
521 EXPECT_EQ("1234", *it);
522}
523
Darin Petkovca8a0e62012-09-26 13:16:52 +0200524TEST_F(OpenVPNDriverTest, InitCAOptions) {
Darin Petkove0d5dd12012-04-04 16:10:48 +0200525 static const char kHost[] = "192.168.2.254";
Darin Petkovca8a0e62012-09-26 13:16:52 +0200526 static const char kCaCert[] = "foo";
Darin Petkove0d5dd12012-04-04 16:10:48 +0200527 static const char kCaCertNSS[] = "{1234}";
528 static const char kNSSCertfile[] = "/tmp/nss-cert";
Darin Petkovca8a0e62012-09-26 13:16:52 +0200529
530 Error error;
531 vector<string> options;
532 EXPECT_TRUE(driver_->InitCAOptions(&options, &error));
533 EXPECT_TRUE(error.IsSuccess());
Darin Petkovc418b4b2012-10-05 11:42:52 +0200534 ExpectInFlags(options, "--ca", OpenVPNDriver::kDefaultCACertificates);
Darin Petkovca8a0e62012-09-26 13:16:52 +0200535
536 options.clear();
537 SetArg(flimflam::kOpenVPNCaCertProperty, kCaCert);
538 EXPECT_TRUE(driver_->InitCAOptions(&options, &error));
539 ExpectInFlags(options, "--ca", kCaCert);
540 EXPECT_TRUE(error.IsSuccess());
541
542 SetArg(flimflam::kOpenVPNCaCertNSSProperty, kCaCertNSS);
543 EXPECT_FALSE(driver_->InitCAOptions(&options, &error));
544 EXPECT_EQ(Error::kInvalidArguments, error.type());
545 EXPECT_EQ("Can't specify both CACert and CACertNSS.", error.message());
546
547 SetArg(flimflam::kOpenVPNCaCertProperty, "");
548 SetArg(flimflam::kProviderHostProperty, kHost);
Darin Petkove0d5dd12012-04-04 16:10:48 +0200549 FilePath empty_cert;
550 FilePath nss_cert(kNSSCertfile);
Darin Petkov3c5e4dc2012-04-02 14:44:27 +0200551 EXPECT_CALL(nss_,
552 GetPEMCertfile(kCaCertNSS,
553 ElementsAreArray(kHost, arraysize(kHost) - 1)))
Darin Petkove0d5dd12012-04-04 16:10:48 +0200554 .WillOnce(Return(empty_cert))
555 .WillOnce(Return(nss_cert));
556
Darin Petkovca8a0e62012-09-26 13:16:52 +0200557 error.Reset();
558 EXPECT_FALSE(driver_->InitCAOptions(&options, &error));
559 EXPECT_EQ(Error::kInvalidArguments, error.type());
560 EXPECT_EQ("Unable to extract NSS CA certificate: {1234}", error.message());
561
562 error.Reset();
563 options.clear();
564 EXPECT_TRUE(driver_->InitCAOptions(&options, &error));
Darin Petkove0d5dd12012-04-04 16:10:48 +0200565 ExpectInFlags(options, "--ca", kNSSCertfile);
566 EXPECT_TRUE(error.IsSuccess());
Darin Petkove0d5dd12012-04-04 16:10:48 +0200567}
568
Darin Petkov4e1b3f82012-09-27 13:22:37 +0200569TEST_F(OpenVPNDriverTest, InitClientAuthOptions) {
570 static const char kTestValue[] = "foo";
571 vector<string> options;
572
573 // No key or cert, assume user/password authentication.
574 driver_->InitClientAuthOptions(&options);
575 ExpectInFlags(options, "--auth-user-pass");
576 ExpectNotInFlags(options, "--key");
577 ExpectNotInFlags(options, "--cert");
578
579 // Cert available, no user/password.
580 options.clear();
581 SetArg(OpenVPNDriver::kOpenVPNCertProperty, kTestValue);
582 driver_->InitClientAuthOptions(&options);
583 ExpectNotInFlags(options, "--auth-user-pass");
584 ExpectNotInFlags(options, "--key");
585 ExpectInFlags(options, "--cert", kTestValue);
586
587 // Key available, no user/password.
588 options.clear();
589 SetArg(OpenVPNDriver::kOpenVPNKeyProperty, kTestValue);
590 driver_->InitClientAuthOptions(&options);
591 ExpectNotInFlags(options, "--auth-user-pass");
592 ExpectInFlags(options, "--key", kTestValue);
593
594 // Key available, AuthUserPass set.
595 options.clear();
596 SetArg(flimflam::kOpenVPNAuthUserPassProperty, kTestValue);
597 driver_->InitClientAuthOptions(&options);
598 ExpectInFlags(options, "--auth-user-pass");
599 ExpectInFlags(options, "--key", kTestValue);
600
601 // Key available, User set.
602 options.clear();
603 RemoveStringArg(flimflam::kOpenVPNAuthUserPassProperty);
604 SetArg(flimflam::kOpenVPNUserProperty, "user");
605 driver_->InitClientAuthOptions(&options);
606 ExpectInFlags(options, "--auth-user-pass");
607 ExpectInFlags(options, "--key", kTestValue);
608}
609
Darin Petkove0d5dd12012-04-04 16:10:48 +0200610TEST_F(OpenVPNDriverTest, InitPKCS11Options) {
611 vector<string> options;
612 driver_->InitPKCS11Options(&options);
613 EXPECT_TRUE(options.empty());
614
615 static const char kID[] = "TestPKCS11ID";
616 SetArg(flimflam::kOpenVPNClientCertIdProperty, kID);
617 driver_->InitPKCS11Options(&options);
618 ExpectInFlags(options, "--pkcs11-id", kID);
619 ExpectInFlags(options, "--pkcs11-providers", "libchaps.so");
620
621 static const char kProvider[] = "libpkcs11.so";
622 SetArg(flimflam::kOpenVPNProviderProperty, kProvider);
623 options.clear();
624 driver_->InitPKCS11Options(&options);
625 ExpectInFlags(options, "--pkcs11-id", kID);
626 ExpectInFlags(options, "--pkcs11-providers", kProvider);
627}
628
629TEST_F(OpenVPNDriverTest, InitManagementChannelOptions) {
630 vector<string> options;
Darin Petkova5e07ef2012-07-09 14:27:57 +0200631 Error error;
632
Darin Petkove0d5dd12012-04-04 16:10:48 +0200633 EXPECT_CALL(*management_server_,
634 Start(&dispatcher_, &driver_->sockets_, &options))
Darin Petkova5e07ef2012-07-09 14:27:57 +0200635 .Times(4)
Darin Petkove0d5dd12012-04-04 16:10:48 +0200636 .WillOnce(Return(false))
Darin Petkova5e07ef2012-07-09 14:27:57 +0200637 .WillRepeatedly(Return(true));
Darin Petkove0d5dd12012-04-04 16:10:48 +0200638
Darin Petkova5e07ef2012-07-09 14:27:57 +0200639 // Management server fails to start.
Darin Petkove0d5dd12012-04-04 16:10:48 +0200640 EXPECT_FALSE(driver_->InitManagementChannelOptions(&options, &error));
641 EXPECT_EQ(Error::kInternalError, error.type());
642 EXPECT_EQ("Unable to setup management channel.", error.message());
643
Darin Petkova5e07ef2012-07-09 14:27:57 +0200644 // Start with a connected default service.
645 scoped_refptr<MockService> mock_service(
646 new MockService(&control_, &dispatcher_, &metrics_, &manager_));
647 EXPECT_CALL(manager_, GetDefaultService()).WillOnce(Return(mock_service));
648 EXPECT_CALL(*mock_service, IsConnected()).WillOnce(Return(true));
649 EXPECT_CALL(*management_server_, ReleaseHold());
Darin Petkove0d5dd12012-04-04 16:10:48 +0200650 error.Reset();
651 EXPECT_TRUE(driver_->InitManagementChannelOptions(&options, &error));
652 EXPECT_TRUE(error.IsSuccess());
Darin Petkova5e07ef2012-07-09 14:27:57 +0200653
654 // Start with a disconnected default service.
655 EXPECT_CALL(manager_, GetDefaultService()).WillOnce(Return(mock_service));
656 EXPECT_CALL(*mock_service, IsConnected()).WillOnce(Return(false));
657 EXPECT_CALL(*management_server_, ReleaseHold()).Times(0);
658 EXPECT_TRUE(driver_->InitManagementChannelOptions(&options, &error));
659 EXPECT_TRUE(error.IsSuccess());
660
661 // Start with no default service.
662 ServiceRefPtr null_service;
663 EXPECT_CALL(manager_, GetDefaultService()).WillOnce(Return(null_service));
664 EXPECT_CALL(*management_server_, ReleaseHold()).Times(0);
665 EXPECT_TRUE(driver_->InitManagementChannelOptions(&options, &error));
666 EXPECT_TRUE(error.IsSuccess());
Darin Petkovfe6a9372012-02-28 16:25:06 +0100667}
668
Darin Petkov55771b72012-04-25 09:25:19 +0200669TEST_F(OpenVPNDriverTest, InitLoggingOptions) {
670 vector<string> options;
671 bool vpn_logging = SLOG_IS_ON(VPN, 0);
672 ScopeLogger::GetInstance()->EnableScopesByName("-vpn");
673 driver_->InitLoggingOptions(&options);
674 ASSERT_EQ(1, options.size());
675 EXPECT_EQ("--syslog", options[0]);
676 ScopeLogger::GetInstance()->EnableScopesByName("+vpn");
677 options.clear();
678 driver_->InitLoggingOptions(&options);
679 ExpectInFlags(options, "--verb", "3");
680 ScopeLogger::GetInstance()->EnableScopesByName("-vpn");
681 SetArg("OpenVPN.Verb", "2");
682 options.clear();
683 driver_->InitLoggingOptions(&options);
684 ExpectInFlags(options, "--verb", "2");
685 ScopeLogger::GetInstance()->EnableScopesByName("+vpn");
686 SetArg("OpenVPN.Verb", "1");
687 options.clear();
688 driver_->InitLoggingOptions(&options);
689 ExpectInFlags(options, "--verb", "1");
690 if (!vpn_logging) {
691 ScopeLogger::GetInstance()->EnableScopesByName("-vpn");
692 }
693}
694
Darin Petkovfe6a9372012-02-28 16:25:06 +0100695TEST_F(OpenVPNDriverTest, AppendValueOption) {
696 vector<string> options;
Darin Petkov46463022012-03-29 14:57:32 +0200697 EXPECT_FALSE(
698 driver_->AppendValueOption("OpenVPN.UnknownProperty", kOption, &options));
Darin Petkovfe6a9372012-02-28 16:25:06 +0100699 EXPECT_TRUE(options.empty());
700
Darin Petkove0d5dd12012-04-04 16:10:48 +0200701 SetArg(kProperty, "");
Darin Petkov46463022012-03-29 14:57:32 +0200702 EXPECT_FALSE(driver_->AppendValueOption(kProperty, kOption, &options));
Darin Petkovfe6a9372012-02-28 16:25:06 +0100703 EXPECT_TRUE(options.empty());
704
Darin Petkove0d5dd12012-04-04 16:10:48 +0200705 SetArg(kProperty, kValue);
706 SetArg(kProperty2, kValue2);
Darin Petkov46463022012-03-29 14:57:32 +0200707 EXPECT_TRUE(driver_->AppendValueOption(kProperty, kOption, &options));
708 EXPECT_TRUE(driver_->AppendValueOption(kProperty2, kOption2, &options));
Darin Petkovfe6a9372012-02-28 16:25:06 +0100709 EXPECT_EQ(4, options.size());
710 EXPECT_EQ(kOption, options[0]);
711 EXPECT_EQ(kValue, options[1]);
712 EXPECT_EQ(kOption2, options[2]);
713 EXPECT_EQ(kValue2, options[3]);
714}
715
716TEST_F(OpenVPNDriverTest, AppendFlag) {
717 vector<string> options;
Darin Petkov46463022012-03-29 14:57:32 +0200718 EXPECT_FALSE(
719 driver_->AppendFlag("OpenVPN.UnknownProperty", kOption, &options));
Darin Petkovfe6a9372012-02-28 16:25:06 +0100720 EXPECT_TRUE(options.empty());
721
Darin Petkove0d5dd12012-04-04 16:10:48 +0200722 SetArg(kProperty, "");
723 SetArg(kProperty2, kValue2);
Darin Petkov46463022012-03-29 14:57:32 +0200724 EXPECT_TRUE(driver_->AppendFlag(kProperty, kOption, &options));
725 EXPECT_TRUE(driver_->AppendFlag(kProperty2, kOption2, &options));
Darin Petkovfe6a9372012-02-28 16:25:06 +0100726 EXPECT_EQ(2, options.size());
727 EXPECT_EQ(kOption, options[0]);
728 EXPECT_EQ(kOption2, options[1]);
729}
730
Paul Stewartca6abd42012-03-01 15:45:29 -0800731TEST_F(OpenVPNDriverTest, ClaimInterface) {
Darin Petkov79d74c92012-03-07 17:20:32 +0100732 driver_->tunnel_interface_ = kInterfaceName;
733 EXPECT_FALSE(driver_->ClaimInterface(string(kInterfaceName) + "XXX",
734 kInterfaceIndex));
735 EXPECT_FALSE(driver_->device_);
Paul Stewartca6abd42012-03-01 15:45:29 -0800736
Darin Petkov36a3ace2012-03-06 17:22:14 +0100737 static const char kHost[] = "192.168.2.254";
Darin Petkove0d5dd12012-04-04 16:10:48 +0200738 SetArg(flimflam::kProviderHostProperty, kHost);
Darin Petkov46463022012-03-29 14:57:32 +0200739 EXPECT_CALL(*management_server_, Start(_, _, _)).WillOnce(Return(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +0200740 ServiceRefPtr null_service;
741 EXPECT_CALL(manager_, GetDefaultService()).WillOnce(Return(null_service));
Darin Petkov36a3ace2012-03-06 17:22:14 +0100742 EXPECT_CALL(glib_, SpawnAsyncWithPipesCWD(_, _, _, _, _, _, _, _, _, _))
743 .WillOnce(Return(true));
744 EXPECT_CALL(glib_, ChildWatchAdd(_, _, _)).WillOnce(Return(1));
Darin Petkova5e07ef2012-07-09 14:27:57 +0200745 const int kServiceCallbackTag = 1;
746 EXPECT_EQ(0, driver_->default_service_callback_tag_);
747 EXPECT_CALL(manager_, RegisterDefaultServiceCallback(_))
748 .WillOnce(Return(kServiceCallbackTag));
Darin Petkov79d74c92012-03-07 17:20:32 +0100749 EXPECT_TRUE(driver_->ClaimInterface(kInterfaceName, kInterfaceIndex));
750 ASSERT_TRUE(driver_->device_);
751 EXPECT_EQ(kInterfaceIndex, driver_->device_->interface_index());
Darin Petkova5e07ef2012-07-09 14:27:57 +0200752 EXPECT_EQ(kServiceCallbackTag, driver_->default_service_callback_tag_);
Paul Stewartca6abd42012-03-01 15:45:29 -0800753}
754
Darin Petkov36a3ace2012-03-06 17:22:14 +0100755TEST_F(OpenVPNDriverTest, Cleanup) {
Darin Petkov1fa81942012-04-02 11:38:08 +0200756 driver_->Cleanup(Service::kStateIdle); // Ensure no crash.
757
Darin Petkova5e07ef2012-07-09 14:27:57 +0200758 const unsigned int kChildTag = 123;
Darin Petkov36a3ace2012-03-06 17:22:14 +0100759 const int kPID = 123456;
Darin Petkova5e07ef2012-07-09 14:27:57 +0200760 const int kServiceCallbackTag = 5;
761 driver_->default_service_callback_tag_ = kServiceCallbackTag;
762 driver_->child_watch_tag_ = kChildTag;
Darin Petkov79d74c92012-03-07 17:20:32 +0100763 driver_->pid_ = kPID;
764 driver_->rpc_task_.reset(new RPCTask(&control_, this));
765 driver_->tunnel_interface_ = kInterfaceName;
766 driver_->device_ = device_;
767 driver_->service_ = service_;
Darin Petkov3189a472012-10-05 09:55:33 +0200768 driver_->ip_properties_.address = "1.2.3.4";
Darin Petkov602303f2012-06-06 12:15:59 +0200769 driver_->StartConnectTimeout();
Darin Petkov1fa81942012-04-02 11:38:08 +0200770 FilePath tls_auth_file;
771 EXPECT_TRUE(file_util::CreateTemporaryFile(&tls_auth_file));
772 EXPECT_FALSE(tls_auth_file.empty());
773 EXPECT_TRUE(file_util::PathExists(tls_auth_file));
774 driver_->tls_auth_file_ = tls_auth_file;
Darin Petkov46463022012-03-29 14:57:32 +0200775 // Stop will be called twice -- once by Cleanup and once by the destructor.
776 EXPECT_CALL(*management_server_, Stop()).Times(2);
Darin Petkova5e07ef2012-07-09 14:27:57 +0200777 EXPECT_CALL(glib_, SourceRemove(kChildTag));
778 EXPECT_CALL(manager_, DeregisterDefaultServiceCallback(kServiceCallbackTag));
Darin Petkov5a850472012-06-06 15:44:24 +0200779 EXPECT_CALL(process_killer_, Kill(kPID, _));
780 EXPECT_CALL(device_info_, DeleteInterface(_)).Times(0);
Darin Petkov029d3532012-04-18 14:38:04 +0200781 EXPECT_CALL(*device_, OnDisconnected());
Eric Shienbrood9a245532012-03-07 14:20:39 -0500782 EXPECT_CALL(*device_, SetEnabled(false));
Darin Petkov79d74c92012-03-07 17:20:32 +0100783 EXPECT_CALL(*service_, SetState(Service::kStateFailure));
Darin Petkov3f9131c2012-03-20 11:37:32 +0100784 driver_->Cleanup(Service::kStateFailure);
Darin Petkov79d74c92012-03-07 17:20:32 +0100785 EXPECT_EQ(0, driver_->child_watch_tag_);
Darin Petkova5e07ef2012-07-09 14:27:57 +0200786 EXPECT_EQ(0, driver_->default_service_callback_tag_);
Darin Petkov79d74c92012-03-07 17:20:32 +0100787 EXPECT_EQ(0, driver_->pid_);
788 EXPECT_FALSE(driver_->rpc_task_.get());
789 EXPECT_TRUE(driver_->tunnel_interface_.empty());
790 EXPECT_FALSE(driver_->device_);
791 EXPECT_FALSE(driver_->service_);
Darin Petkov1fa81942012-04-02 11:38:08 +0200792 EXPECT_FALSE(file_util::PathExists(tls_auth_file));
793 EXPECT_TRUE(driver_->tls_auth_file_.empty());
Darin Petkov3189a472012-10-05 09:55:33 +0200794 EXPECT_TRUE(driver_->ip_properties_.address.empty());
Darin Petkov602303f2012-06-06 12:15:59 +0200795 EXPECT_FALSE(driver_->IsConnectTimeoutStarted());
Darin Petkov36a3ace2012-03-06 17:22:14 +0100796}
797
Darin Petkov1a462de2012-05-02 11:10:48 +0200798namespace {
799MATCHER(CheckEnv, "") {
800 if (!arg || !arg[0] || !arg[1] || arg[2]) {
801 return false;
802 }
803 return (string(arg[0]) == "IV_PLAT=Chromium OS" &&
804 string(arg[1]) == "IV_PLAT_REL=2202.0");
805}
806} // namespace
807
Darin Petkov36a3ace2012-03-06 17:22:14 +0100808TEST_F(OpenVPNDriverTest, SpawnOpenVPN) {
Darin Petkov1a462de2012-05-02 11:10:48 +0200809 SetupLSBRelease();
810
Darin Petkov79d74c92012-03-07 17:20:32 +0100811 EXPECT_FALSE(driver_->SpawnOpenVPN());
Darin Petkov36a3ace2012-03-06 17:22:14 +0100812
813 static const char kHost[] = "192.168.2.254";
Darin Petkove0d5dd12012-04-04 16:10:48 +0200814 SetArg(flimflam::kProviderHostProperty, kHost);
Darin Petkov79d74c92012-03-07 17:20:32 +0100815 driver_->tunnel_interface_ = "tun0";
816 driver_->rpc_task_.reset(new RPCTask(&control_, this));
Darin Petkov46463022012-03-29 14:57:32 +0200817 EXPECT_CALL(*management_server_, Start(_, _, _))
818 .Times(2)
819 .WillRepeatedly(Return(true));
Darin Petkova5e07ef2012-07-09 14:27:57 +0200820 ServiceRefPtr null_service;
821 EXPECT_CALL(manager_, GetDefaultService())
822 .Times(2)
823 .WillRepeatedly(Return(null_service));
Darin Petkov36a3ace2012-03-06 17:22:14 +0100824
825 const int kPID = 234678;
Darin Petkov1a462de2012-05-02 11:10:48 +0200826 EXPECT_CALL(glib_,
827 SpawnAsyncWithPipesCWD(_, CheckEnv(), _, _, _, _, _, _, _, _))
Darin Petkov36a3ace2012-03-06 17:22:14 +0100828 .WillOnce(Return(false))
829 .WillOnce(DoAll(SetArgumentPointee<5>(kPID), Return(true)));
830 const int kTag = 6;
Darin Petkov79d74c92012-03-07 17:20:32 +0100831 EXPECT_CALL(glib_, ChildWatchAdd(kPID, &driver_->OnOpenVPNDied, driver_))
Darin Petkov36a3ace2012-03-06 17:22:14 +0100832 .WillOnce(Return(kTag));
Darin Petkov79d74c92012-03-07 17:20:32 +0100833 EXPECT_FALSE(driver_->SpawnOpenVPN());
834 EXPECT_TRUE(driver_->SpawnOpenVPN());
835 EXPECT_EQ(kPID, driver_->pid_);
836 EXPECT_EQ(kTag, driver_->child_watch_tag_);
Darin Petkov36a3ace2012-03-06 17:22:14 +0100837}
838
839TEST_F(OpenVPNDriverTest, OnOpenVPNDied) {
840 const int kPID = 99999;
Darin Petkov5a850472012-06-06 15:44:24 +0200841 driver_->device_ = device_;
Darin Petkov79d74c92012-03-07 17:20:32 +0100842 driver_->child_watch_tag_ = 333;
843 driver_->pid_ = kPID;
Darin Petkov5a850472012-06-06 15:44:24 +0200844 EXPECT_CALL(*device_, OnDisconnected());
845 EXPECT_CALL(*device_, SetEnabled(false));
846 EXPECT_CALL(process_killer_, Kill(_, _)).Times(0);
847 EXPECT_CALL(device_info_, DeleteInterface(kInterfaceIndex));
Darin Petkov79d74c92012-03-07 17:20:32 +0100848 OpenVPNDriver::OnOpenVPNDied(kPID, 2, driver_);
849 EXPECT_EQ(0, driver_->child_watch_tag_);
850 EXPECT_EQ(0, driver_->pid_);
Darin Petkov36a3ace2012-03-06 17:22:14 +0100851}
852
Darin Petkov6aa21872012-03-09 16:10:19 +0100853TEST_F(OpenVPNDriverTest, Disconnect) {
854 driver_->device_ = device_;
855 driver_->service_ = service_;
Darin Petkov029d3532012-04-18 14:38:04 +0200856 EXPECT_CALL(*device_, OnDisconnected());
Eric Shienbrood9a245532012-03-07 14:20:39 -0500857 EXPECT_CALL(*device_, SetEnabled(false));
Darin Petkov6aa21872012-03-09 16:10:19 +0100858 EXPECT_CALL(device_info_, DeleteInterface(kInterfaceIndex));
859 EXPECT_CALL(*service_, SetState(Service::kStateIdle));
860 driver_->Disconnect();
Darin Petkova0e645e2012-04-25 11:38:59 +0200861 EXPECT_FALSE(driver_->device_);
Darin Petkov6aa21872012-03-09 16:10:19 +0100862 EXPECT_FALSE(driver_->service_);
863}
864
Darin Petkov5eb05422012-05-11 15:45:25 +0200865TEST_F(OpenVPNDriverTest, OnConnectionDisconnected) {
866 driver_->service_ = service_;
867 EXPECT_CALL(*service_, SetState(Service::kStateFailure));
868 driver_->OnConnectionDisconnected();
869 EXPECT_FALSE(driver_->service_);
870}
871
Darin Petkov271fe522012-03-27 13:47:29 +0200872TEST_F(OpenVPNDriverTest, OnReconnecting) {
873 driver_->OnReconnecting(); // Expect no crash.
874 driver_->device_ = device_;
875 driver_->service_ = service_;
876 EXPECT_CALL(*device_, OnDisconnected());
877 EXPECT_CALL(*service_, SetState(Service::kStateAssociating));
878 driver_->OnReconnecting();
Darin Petkov602303f2012-06-06 12:15:59 +0200879 EXPECT_TRUE(driver_->IsConnectTimeoutStarted());
Darin Petkov271fe522012-03-27 13:47:29 +0200880}
881
Paul Stewart291a4732012-03-14 19:19:02 -0700882TEST_F(OpenVPNDriverTest, VerifyPaths) {
883 // Ensure that the various path constants that the OpenVPN driver uses
Darin Petkova0e645e2012-04-25 11:38:59 +0200884 // actually exists in the build image. Due to build dependencies, they should
885 // already exist by the time we run unit tests.
Paul Stewart291a4732012-03-14 19:19:02 -0700886
Darin Petkova0e645e2012-04-25 11:38:59 +0200887 // The OpenVPNDriver path constants are absolute. FilePath::Append asserts
888 // that its argument is not an absolute path, so we need to strip the leading
889 // separators. There's nothing built into FilePath to do so.
890 static const char *kPaths[] = {
891 OpenVPNDriver::kOpenVPNPath,
892 OpenVPNDriver::kOpenVPNScript,
893 };
894 for (size_t i = 0; i < arraysize(kPaths); i++) {
895 string path(kPaths[i]);
896 TrimString(path, FilePath::kSeparators, &path);
897 EXPECT_TRUE(file_util::PathExists(FilePath(SYSROOT).Append(path)))
898 << kPaths[i];
899 }
Paul Stewart291a4732012-03-14 19:19:02 -0700900}
901
Darin Petkovd4325392012-04-23 15:48:22 +0200902TEST_F(OpenVPNDriverTest, InitPropertyStore) {
903 // Sanity test property store initialization.
904 PropertyStore store;
905 driver_->InitPropertyStore(&store);
906 const string kUser = "joe";
907 Error error;
908 EXPECT_TRUE(
909 store.SetStringProperty(flimflam::kOpenVPNUserProperty, kUser, &error));
910 EXPECT_TRUE(error.IsSuccess());
Darin Petkovb536a742012-04-26 11:31:28 +0200911 EXPECT_EQ(kUser, GetArgs()->LookupString(flimflam::kOpenVPNUserProperty, ""));
912}
913
914TEST_F(OpenVPNDriverTest, GetProvider) {
915 PropertyStore store;
916 driver_->InitPropertyStore(&store);
917 {
Darin Petkovb536a742012-04-26 11:31:28 +0200918 KeyValueStore props;
Paul Stewarte6e8e492013-01-17 11:00:50 -0800919 Error error;
Darin Petkovb536a742012-04-26 11:31:28 +0200920 EXPECT_TRUE(
Paul Stewarte6e8e492013-01-17 11:00:50 -0800921 store.GetKeyValueStoreProperty(
922 flimflam::kProviderProperty, &props, &error));
Darin Petkovb536a742012-04-26 11:31:28 +0200923 EXPECT_TRUE(props.LookupBool(flimflam::kPassphraseRequiredProperty, false));
924 }
925 {
Darin Petkovb536a742012-04-26 11:31:28 +0200926 KeyValueStore props;
927 SetArg(flimflam::kOpenVPNPasswordProperty, "random-password");
Paul Stewarte6e8e492013-01-17 11:00:50 -0800928 Error error;
Darin Petkovb536a742012-04-26 11:31:28 +0200929 EXPECT_TRUE(
Paul Stewarte6e8e492013-01-17 11:00:50 -0800930 store.GetKeyValueStoreProperty(
931 flimflam::kProviderProperty, &props, &error));
Darin Petkovb536a742012-04-26 11:31:28 +0200932 EXPECT_FALSE(props.LookupBool(flimflam::kPassphraseRequiredProperty, true));
Darin Petkov02236552012-06-11 13:15:19 +0200933 EXPECT_FALSE(props.ContainsString(flimflam::kOpenVPNPasswordProperty));
Darin Petkovb536a742012-04-26 11:31:28 +0200934 }
Darin Petkovd4325392012-04-23 15:48:22 +0200935}
936
Darin Petkov1a462de2012-05-02 11:10:48 +0200937TEST_F(OpenVPNDriverTest, ParseLSBRelease) {
938 SetupLSBRelease();
939 map<string, string> lsb_release;
940 EXPECT_TRUE(driver_->ParseLSBRelease(&lsb_release));
941 EXPECT_TRUE(ContainsKey(lsb_release, "foo") && lsb_release["foo"] == "");
942 EXPECT_EQ("=", lsb_release["zoo"]);
943 EXPECT_EQ("Chromium OS", lsb_release[OpenVPNDriver::kChromeOSReleaseName]);
944 EXPECT_EQ("2202.0", lsb_release[OpenVPNDriver::kChromeOSReleaseVersion]);
945 driver_->lsb_release_file_ = FilePath("/non/existent/file");
946 EXPECT_FALSE(driver_->ParseLSBRelease(NULL));
947}
948
949TEST_F(OpenVPNDriverTest, InitEnvironment) {
950 vector<string> env;
951 SetupLSBRelease();
952 driver_->InitEnvironment(&env);
953 ASSERT_EQ(2, env.size());
954 EXPECT_EQ("IV_PLAT=Chromium OS", env[0]);
955 EXPECT_EQ("IV_PLAT_REL=2202.0", env[1]);
956 env.clear();
957 EXPECT_EQ(0, file_util::WriteFile(lsb_release_file_, "", 0));
958 driver_->InitEnvironment(&env);
959 EXPECT_EQ(0, env.size());
960}
961
Darin Petkov5a850472012-06-06 15:44:24 +0200962TEST_F(OpenVPNDriverTest, DeleteInterface) {
963 scoped_ptr<MockDeviceInfo> device_info(
964 new MockDeviceInfo(&control_, &dispatcher_, &metrics_, &manager_));
965 EXPECT_CALL(*device_info, DeleteInterface(kInterfaceIndex))
966 .WillOnce(Return(true));
967 WeakPtr<DeviceInfo> weak = device_info->AsWeakPtr();
968 EXPECT_TRUE(weak);
969 OpenVPNDriver::DeleteInterface(weak, kInterfaceIndex);
970 device_info.reset();
971 EXPECT_FALSE(weak);
972 // Expect no crash.
973 OpenVPNDriver::DeleteInterface(weak, kInterfaceIndex);
974}
975
Darin Petkova5e07ef2012-07-09 14:27:57 +0200976TEST_F(OpenVPNDriverTest, OnDefaultServiceChanged) {
977 driver_->service_ = service_;
978
979 ServiceRefPtr null_service;
980 EXPECT_CALL(*management_server_, Hold());
981 driver_->OnDefaultServiceChanged(null_service);
982
983 EXPECT_CALL(*management_server_, Hold());
984 driver_->OnDefaultServiceChanged(service_);
985
986 scoped_refptr<MockService> mock_service(
987 new MockService(&control_, &dispatcher_, &metrics_, &manager_));
988
989 EXPECT_CALL(*mock_service, IsConnected()).WillOnce(Return(false));
990 EXPECT_CALL(*management_server_, Hold());
991 driver_->OnDefaultServiceChanged(mock_service);
992
993 EXPECT_CALL(*mock_service, IsConnected()).WillOnce(Return(true));
994 EXPECT_CALL(*management_server_, ReleaseHold());
995 driver_->OnDefaultServiceChanged(mock_service);
996}
997
Darin Petkov33af05c2012-02-28 10:10:30 +0100998} // namespace shill