Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "shill/openvpn_driver.h" |
| 6 | |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
Paul Stewart | 291a473 | 2012-03-14 19:19:02 -0700 | [diff] [blame] | 9 | #include <base/file_path.h> |
| 10 | #include <base/file_util.h> |
| 11 | #include <base/string_util.h> |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 12 | #include <chromeos/dbus/service_constants.h> |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 13 | #include <gtest/gtest.h> |
| 14 | |
Paul Stewart | ebd3856 | 2012-03-23 13:06:40 -0700 | [diff] [blame] | 15 | #include "shill/dbus_adaptor.h" |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 16 | #include "shill/error.h" |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 17 | #include "shill/ipconfig.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 18 | #include "shill/logging.h" |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 19 | #include "shill/mock_adaptors.h" |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 20 | #include "shill/mock_device_info.h" |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 21 | #include "shill/mock_glib.h" |
| 22 | #include "shill/mock_manager.h" |
| 23 | #include "shill/mock_metrics.h" |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 24 | #include "shill/mock_nss.h" |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 25 | #include "shill/mock_openvpn_management_server.h" |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 26 | #include "shill/mock_process_killer.h" |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 27 | #include "shill/mock_service.h" |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 28 | #include "shill/mock_store.h" |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 29 | #include "shill/mock_vpn.h" |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 30 | #include "shill/mock_vpn_service.h" |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 31 | #include "shill/nice_mock_control.h" |
| 32 | #include "shill/rpc_task.h" |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 33 | #include "shill/vpn.h" |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 34 | #include "shill/vpn_service.h" |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 35 | |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 36 | using base::WeakPtr; |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 37 | using std::map; |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 38 | using std::string; |
| 39 | using std::vector; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 40 | using testing::_; |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 41 | using testing::AnyNumber; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 42 | using testing::DoAll; |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 43 | using testing::ElementsAreArray; |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 44 | using testing::Field; |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 45 | using testing::Ne; |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 46 | using testing::NiceMock; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 47 | using testing::Return; |
| 48 | using testing::SetArgumentPointee; |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 49 | using testing::StrictMock; |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 50 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 51 | namespace shill { |
| 52 | |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 53 | class OpenVPNDriverTest : public testing::Test, |
| 54 | public RPCTaskDelegate { |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 55 | public: |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 56 | OpenVPNDriverTest() |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 57 | : device_info_(&control_, &dispatcher_, &metrics_, &manager_), |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 58 | manager_(&control_, &dispatcher_, &metrics_, &glib_), |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 59 | driver_(new OpenVPNDriver(&control_, &dispatcher_, &metrics_, &manager_, |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 60 | &device_info_, &glib_)), |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 61 | service_(new MockVPNService(&control_, &dispatcher_, &metrics_, |
| 62 | &manager_, driver_)), |
| 63 | device_(new MockVPN(&control_, &dispatcher_, &metrics_, &manager_, |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 64 | kInterfaceName, kInterfaceIndex)), |
| 65 | management_server_(new NiceMock<MockOpenVPNManagementServer>()) { |
| 66 | driver_->management_server_.reset(management_server_); |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 67 | driver_->nss_ = &nss_; |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 68 | driver_->process_killer_ = &process_killer_; |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 69 | } |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 70 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 71 | virtual ~OpenVPNDriverTest() {} |
| 72 | |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 73 | virtual void TearDown() { |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 74 | driver_->default_service_callback_tag_ = 0; |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 75 | driver_->child_watch_tag_ = 0; |
| 76 | driver_->pid_ = 0; |
| 77 | driver_->device_ = NULL; |
| 78 | driver_->service_ = NULL; |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 79 | if (!lsb_release_file_.empty()) { |
| 80 | EXPECT_TRUE(file_util::Delete(lsb_release_file_, false)); |
| 81 | lsb_release_file_.clear(); |
| 82 | } |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 83 | } |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 84 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 85 | protected: |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 86 | 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 Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 92 | 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 Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 98 | static const char kInterfaceName[]; |
| 99 | static const int kInterfaceIndex; |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 100 | |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 101 | void SetArg(const string &arg, const string &value) { |
| 102 | driver_->args()->SetString(arg, value); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 105 | KeyValueStore *GetArgs() { |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 106 | return driver_->args(); |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Darin Petkov | 4e1b3f8 | 2012-09-27 13:22:37 +0200 | [diff] [blame] | 109 | void RemoveStringArg(const string &arg) { |
| 110 | driver_->args()->RemoveString(arg); |
| 111 | } |
| 112 | |
Darin Petkov | 3189a47 | 2012-10-05 09:55:33 +0200 | [diff] [blame] | 113 | const ServiceRefPtr &GetSelectedService() { |
| 114 | return device_->selected_service(); |
| 115 | } |
| 116 | |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 117 | // 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 Petkov | 4e1b3f8 | 2012-09-27 13:22:37 +0200 | [diff] [blame] | 120 | void ExpectInFlags(const vector<string> &options, const string &flag); |
| 121 | void ExpectNotInFlags(const vector<string> &options, const string &flag); |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 122 | |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 123 | void SetupLSBRelease(); |
| 124 | |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 125 | // Inherited from RPCTaskDelegate. |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 126 | virtual void GetLogin(string *user, string *password); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 127 | virtual void Notify(const string &reason, const map<string, string> &dict); |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 128 | |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 129 | NiceMockControl control_; |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 130 | NiceMock<MockDeviceInfo> device_info_; |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 131 | EventDispatcher dispatcher_; |
| 132 | MockMetrics metrics_; |
| 133 | MockGLib glib_; |
| 134 | MockManager manager_; |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 135 | OpenVPNDriver *driver_; // Owned by |service_|. |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 136 | scoped_refptr<MockVPNService> service_; |
| 137 | scoped_refptr<MockVPN> device_; |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 138 | MockNSS nss_; |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 139 | MockProcessKiller process_killer_; |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 140 | |
| 141 | // Owned by |driver_|. |
| 142 | NiceMock<MockOpenVPNManagementServer> *management_server_; |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 143 | |
| 144 | FilePath lsb_release_file_; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 145 | }; |
| 146 | |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 147 | const char OpenVPNDriverTest::kOption[] = "--openvpn-option"; |
| 148 | const char OpenVPNDriverTest::kProperty[] = "OpenVPN.SomeProperty"; |
| 149 | const char OpenVPNDriverTest::kValue[] = "some-property-value"; |
| 150 | const char OpenVPNDriverTest::kOption2[] = "--openvpn-option2"; |
| 151 | const char OpenVPNDriverTest::kProperty2[] = "OpenVPN.SomeProperty2"; |
| 152 | const char OpenVPNDriverTest::kValue2[] = "some-property-value2"; |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 153 | const char OpenVPNDriverTest::kGateway1[] = "10.242.2.13"; |
| 154 | const char OpenVPNDriverTest::kNetmask1[] = "255.255.255.255"; |
| 155 | const char OpenVPNDriverTest::kNetwork1[] = "10.242.2.1"; |
| 156 | const char OpenVPNDriverTest::kGateway2[] = "10.242.2.14"; |
| 157 | const char OpenVPNDriverTest::kNetmask2[] = "255.255.0.0"; |
| 158 | const char OpenVPNDriverTest::kNetwork2[] = "192.168.0.0"; |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 159 | const char OpenVPNDriverTest::kInterfaceName[] = "tun0"; |
| 160 | const int OpenVPNDriverTest::kInterfaceIndex = 123; |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 161 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 162 | void OpenVPNDriverTest::GetLogin(string */*user*/, string */*password*/) {} |
| 163 | |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 164 | void OpenVPNDriverTest::Notify(const string &/*reason*/, |
| 165 | const map<string, string> &/*dict*/) {} |
| 166 | |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 167 | void 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 Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 175 | return; // Don't crash below. |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 176 | it++; |
| 177 | EXPECT_TRUE(it != options.end()); |
| 178 | if (it != options.end()) |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 179 | return; // Don't crash below. |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 180 | EXPECT_EQ(value, *it); |
| 181 | } |
| 182 | |
Darin Petkov | 4e1b3f8 | 2012-09-27 13:22:37 +0200 | [diff] [blame] | 183 | void 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 | |
| 189 | void 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 Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 195 | void 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 Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 214 | TEST_F(OpenVPNDriverTest, Connect) { |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 215 | EXPECT_CALL(*service_, SetState(Service::kStateConfiguring)); |
| 216 | const string interface = kInterfaceName; |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 217 | EXPECT_CALL(device_info_, CreateTunnelInterface(_)) |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 218 | .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 Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 223 | EXPECT_TRUE(driver_->IsConnectTimeoutStarted()); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | TEST_F(OpenVPNDriverTest, ConnectTunnelFailure) { |
| 227 | EXPECT_CALL(*service_, SetState(Service::kStateConfiguring)); |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 228 | EXPECT_CALL(device_info_, CreateTunnelInterface(_)).WillOnce(Return(false)); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 229 | 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 Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 234 | EXPECT_FALSE(driver_->IsConnectTimeoutStarted()); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 235 | } |
| 236 | |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 237 | namespace { |
| 238 | MATCHER_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 Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 245 | TEST_F(OpenVPNDriverTest, Notify) { |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 246 | map<string, string> config; |
Darin Petkov | 3189a47 | 2012-10-05 09:55:33 +0200 | [diff] [blame] | 247 | driver_->service_ = service_; |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 248 | driver_->device_ = device_; |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 249 | driver_->StartConnectTimeout(); |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 250 | EXPECT_CALL(*device_, |
| 251 | UpdateIPConfig(Field(&IPConfig::Properties::address, ""))); |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 252 | driver_->Notify("up", config); |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 253 | EXPECT_FALSE(driver_->IsConnectTimeoutStarted()); |
Darin Petkov | 3189a47 | 2012-10-05 09:55:33 +0200 | [diff] [blame] | 254 | EXPECT_TRUE(GetSelectedService().get() == service_.get()); |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 255 | |
| 256 | // Tests that existing properties are reused if no new ones provided. |
Darin Petkov | 3189a47 | 2012-10-05 09:55:33 +0200 | [diff] [blame] | 257 | driver_->ip_properties_.address = "1.2.3.4"; |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 258 | EXPECT_CALL(*device_, |
| 259 | UpdateIPConfig(Field(&IPConfig::Properties::address, "1.2.3.4"))); |
| 260 | driver_->Notify("up", config); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | TEST_F(OpenVPNDriverTest, NotifyFail) { |
| 264 | map<string, string> dict; |
| 265 | driver_->device_ = device_; |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 266 | driver_->StartConnectTimeout(); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 267 | EXPECT_CALL(*device_, OnDisconnected()); |
| 268 | driver_->Notify("fail", dict); |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 269 | EXPECT_TRUE(driver_->IsConnectTimeoutStarted()); |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 270 | } |
| 271 | |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 272 | TEST_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 | |
| 289 | TEST_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 | |
| 309 | TEST_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 Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 332 | |
| 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 Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 336 | } |
| 337 | |
Darin Petkov | 4b94484 | 2012-09-21 10:48:48 +0200 | [diff] [blame] | 338 | TEST_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 Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 361 | TEST_F(OpenVPNDriverTest, ParseForeignOption) { |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 362 | vector<string> domain_search; |
| 363 | vector<string> dns_servers; |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 364 | IPConfig::Properties props; |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 365 | 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 Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | TEST_F(OpenVPNDriverTest, ParseForeignOptions) { |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 381 | // This also tests that std::map is a sorted container. |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 382 | 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 Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 388 | options[10] = "dhcp-option dns 1.2.3.4"; |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 389 | 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 Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 397 | 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 Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | TEST_F(OpenVPNDriverTest, ParseIPConfiguration) { |
| 408 | map<string, string> config; |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 409 | 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 Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 419 | 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 Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 424 | config["trusted_ip"] = "99.88.77.66"; |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 425 | 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 Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 429 | 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 Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 435 | config["foo"] = "bar"; |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 436 | 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 Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 440 | EXPECT_EQ(24, props.subnet_prefix); |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 441 | EXPECT_EQ("33.44.55.66", props.peer_address); |
| 442 | EXPECT_EQ("192.168.1.1", props.gateway); |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 443 | EXPECT_EQ("99.88.77.66", props.trusted_ip); |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 444 | 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 Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 449 | 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 Chan | a016312 | 2012-09-25 15:10:52 -0700 | [diff] [blame] | 456 | EXPECT_FALSE(props.blackhole_ipv6); |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 457 | } |
| 458 | |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 459 | TEST_F(OpenVPNDriverTest, InitOptionsNoHost) { |
| 460 | Error error; |
| 461 | vector<string> options; |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 462 | driver_->InitOptions(&options, &error); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 463 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 464 | EXPECT_TRUE(options.empty()); |
| 465 | } |
| 466 | |
| 467 | TEST_F(OpenVPNDriverTest, InitOptions) { |
| 468 | static const char kHost[] = "192.168.2.254"; |
Darin Petkov | 1fa8194 | 2012-04-02 11:38:08 +0200 | [diff] [blame] | 469 | static const char kTLSAuthContents[] = "SOME-RANDOM-CONTENTS\n"; |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 470 | static const char kID[] = "TestPKCS11ID"; |
| 471 | FilePath empty_cert; |
| 472 | SetArg(flimflam::kProviderHostProperty, kHost); |
| 473 | SetArg(flimflam::kOpenVPNTLSAuthContentsProperty, kTLSAuthContents); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 474 | SetArg(flimflam::kOpenVPNClientCertIdProperty, kID); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 475 | driver_->rpc_task_.reset(new RPCTask(&control_, this)); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 476 | driver_->tunnel_interface_ = kInterfaceName; |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 477 | EXPECT_CALL(*management_server_, Start(_, _, _)).WillOnce(Return(true)); |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 478 | ServiceRefPtr null_service; |
| 479 | EXPECT_CALL(manager_, GetDefaultService()).WillOnce(Return(null_service)); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 480 | |
| 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 Petkov | 728faa9 | 2012-10-12 11:25:47 +0200 | [diff] [blame] | 487 | ExpectInFlags(options, kRPCTaskPathVariable, RPCTaskMockAdaptor::kRpcId); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 488 | 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 Petkov | c418b4b | 2012-10-05 11:42:52 +0200 | [diff] [blame] | 498 | ExpectInFlags(options, "--ca", OpenVPNDriver::kDefaultCACertificates); |
Darin Petkov | 4e1b3f8 | 2012-09-27 13:22:37 +0200 | [diff] [blame] | 499 | ExpectInFlags(options, "--syslog"); |
| 500 | ExpectInFlags(options, "--auth-user-pass"); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 501 | } |
| 502 | |
Darin Petkov | 4b94484 | 2012-09-21 10:48:48 +0200 | [diff] [blame] | 503 | TEST_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 Petkov | ca8a0e6 | 2012-09-26 13:16:52 +0200 | [diff] [blame] | 524 | TEST_F(OpenVPNDriverTest, InitCAOptions) { |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 525 | static const char kHost[] = "192.168.2.254"; |
Darin Petkov | ca8a0e6 | 2012-09-26 13:16:52 +0200 | [diff] [blame] | 526 | static const char kCaCert[] = "foo"; |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 527 | static const char kCaCertNSS[] = "{1234}"; |
| 528 | static const char kNSSCertfile[] = "/tmp/nss-cert"; |
Darin Petkov | ca8a0e6 | 2012-09-26 13:16:52 +0200 | [diff] [blame] | 529 | |
| 530 | Error error; |
| 531 | vector<string> options; |
| 532 | EXPECT_TRUE(driver_->InitCAOptions(&options, &error)); |
| 533 | EXPECT_TRUE(error.IsSuccess()); |
Darin Petkov | c418b4b | 2012-10-05 11:42:52 +0200 | [diff] [blame] | 534 | ExpectInFlags(options, "--ca", OpenVPNDriver::kDefaultCACertificates); |
Darin Petkov | ca8a0e6 | 2012-09-26 13:16:52 +0200 | [diff] [blame] | 535 | |
| 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 Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 549 | FilePath empty_cert; |
| 550 | FilePath nss_cert(kNSSCertfile); |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 551 | EXPECT_CALL(nss_, |
| 552 | GetPEMCertfile(kCaCertNSS, |
| 553 | ElementsAreArray(kHost, arraysize(kHost) - 1))) |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 554 | .WillOnce(Return(empty_cert)) |
| 555 | .WillOnce(Return(nss_cert)); |
| 556 | |
Darin Petkov | ca8a0e6 | 2012-09-26 13:16:52 +0200 | [diff] [blame] | 557 | 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 Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 565 | ExpectInFlags(options, "--ca", kNSSCertfile); |
| 566 | EXPECT_TRUE(error.IsSuccess()); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 567 | } |
| 568 | |
Darin Petkov | 4e1b3f8 | 2012-09-27 13:22:37 +0200 | [diff] [blame] | 569 | TEST_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 Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 610 | TEST_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 | |
| 629 | TEST_F(OpenVPNDriverTest, InitManagementChannelOptions) { |
| 630 | vector<string> options; |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 631 | Error error; |
| 632 | |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 633 | EXPECT_CALL(*management_server_, |
| 634 | Start(&dispatcher_, &driver_->sockets_, &options)) |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 635 | .Times(4) |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 636 | .WillOnce(Return(false)) |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 637 | .WillRepeatedly(Return(true)); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 638 | |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 639 | // Management server fails to start. |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 640 | 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 Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 644 | // 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 Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 650 | error.Reset(); |
| 651 | EXPECT_TRUE(driver_->InitManagementChannelOptions(&options, &error)); |
| 652 | EXPECT_TRUE(error.IsSuccess()); |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 653 | |
| 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 Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 667 | } |
| 668 | |
Darin Petkov | 55771b7 | 2012-04-25 09:25:19 +0200 | [diff] [blame] | 669 | TEST_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 Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 695 | TEST_F(OpenVPNDriverTest, AppendValueOption) { |
| 696 | vector<string> options; |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 697 | EXPECT_FALSE( |
| 698 | driver_->AppendValueOption("OpenVPN.UnknownProperty", kOption, &options)); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 699 | EXPECT_TRUE(options.empty()); |
| 700 | |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 701 | SetArg(kProperty, ""); |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 702 | EXPECT_FALSE(driver_->AppendValueOption(kProperty, kOption, &options)); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 703 | EXPECT_TRUE(options.empty()); |
| 704 | |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 705 | SetArg(kProperty, kValue); |
| 706 | SetArg(kProperty2, kValue2); |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 707 | EXPECT_TRUE(driver_->AppendValueOption(kProperty, kOption, &options)); |
| 708 | EXPECT_TRUE(driver_->AppendValueOption(kProperty2, kOption2, &options)); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 709 | 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 | |
| 716 | TEST_F(OpenVPNDriverTest, AppendFlag) { |
| 717 | vector<string> options; |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 718 | EXPECT_FALSE( |
| 719 | driver_->AppendFlag("OpenVPN.UnknownProperty", kOption, &options)); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 720 | EXPECT_TRUE(options.empty()); |
| 721 | |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 722 | SetArg(kProperty, ""); |
| 723 | SetArg(kProperty2, kValue2); |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 724 | EXPECT_TRUE(driver_->AppendFlag(kProperty, kOption, &options)); |
| 725 | EXPECT_TRUE(driver_->AppendFlag(kProperty2, kOption2, &options)); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 726 | EXPECT_EQ(2, options.size()); |
| 727 | EXPECT_EQ(kOption, options[0]); |
| 728 | EXPECT_EQ(kOption2, options[1]); |
| 729 | } |
| 730 | |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 731 | TEST_F(OpenVPNDriverTest, ClaimInterface) { |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 732 | driver_->tunnel_interface_ = kInterfaceName; |
| 733 | EXPECT_FALSE(driver_->ClaimInterface(string(kInterfaceName) + "XXX", |
| 734 | kInterfaceIndex)); |
| 735 | EXPECT_FALSE(driver_->device_); |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 736 | |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 737 | static const char kHost[] = "192.168.2.254"; |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 738 | SetArg(flimflam::kProviderHostProperty, kHost); |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 739 | EXPECT_CALL(*management_server_, Start(_, _, _)).WillOnce(Return(true)); |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 740 | ServiceRefPtr null_service; |
| 741 | EXPECT_CALL(manager_, GetDefaultService()).WillOnce(Return(null_service)); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 742 | EXPECT_CALL(glib_, SpawnAsyncWithPipesCWD(_, _, _, _, _, _, _, _, _, _)) |
| 743 | .WillOnce(Return(true)); |
| 744 | EXPECT_CALL(glib_, ChildWatchAdd(_, _, _)).WillOnce(Return(1)); |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 745 | const int kServiceCallbackTag = 1; |
| 746 | EXPECT_EQ(0, driver_->default_service_callback_tag_); |
| 747 | EXPECT_CALL(manager_, RegisterDefaultServiceCallback(_)) |
| 748 | .WillOnce(Return(kServiceCallbackTag)); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 749 | EXPECT_TRUE(driver_->ClaimInterface(kInterfaceName, kInterfaceIndex)); |
| 750 | ASSERT_TRUE(driver_->device_); |
| 751 | EXPECT_EQ(kInterfaceIndex, driver_->device_->interface_index()); |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 752 | EXPECT_EQ(kServiceCallbackTag, driver_->default_service_callback_tag_); |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 753 | } |
| 754 | |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 755 | TEST_F(OpenVPNDriverTest, Cleanup) { |
Darin Petkov | 1fa8194 | 2012-04-02 11:38:08 +0200 | [diff] [blame] | 756 | driver_->Cleanup(Service::kStateIdle); // Ensure no crash. |
| 757 | |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 758 | const unsigned int kChildTag = 123; |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 759 | const int kPID = 123456; |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 760 | const int kServiceCallbackTag = 5; |
| 761 | driver_->default_service_callback_tag_ = kServiceCallbackTag; |
| 762 | driver_->child_watch_tag_ = kChildTag; |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 763 | 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 Petkov | 3189a47 | 2012-10-05 09:55:33 +0200 | [diff] [blame] | 768 | driver_->ip_properties_.address = "1.2.3.4"; |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 769 | driver_->StartConnectTimeout(); |
Darin Petkov | 1fa8194 | 2012-04-02 11:38:08 +0200 | [diff] [blame] | 770 | 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 Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 775 | // Stop will be called twice -- once by Cleanup and once by the destructor. |
| 776 | EXPECT_CALL(*management_server_, Stop()).Times(2); |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 777 | EXPECT_CALL(glib_, SourceRemove(kChildTag)); |
| 778 | EXPECT_CALL(manager_, DeregisterDefaultServiceCallback(kServiceCallbackTag)); |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 779 | EXPECT_CALL(process_killer_, Kill(kPID, _)); |
| 780 | EXPECT_CALL(device_info_, DeleteInterface(_)).Times(0); |
Darin Petkov | 029d353 | 2012-04-18 14:38:04 +0200 | [diff] [blame] | 781 | EXPECT_CALL(*device_, OnDisconnected()); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 782 | EXPECT_CALL(*device_, SetEnabled(false)); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 783 | EXPECT_CALL(*service_, SetState(Service::kStateFailure)); |
Darin Petkov | 3f9131c | 2012-03-20 11:37:32 +0100 | [diff] [blame] | 784 | driver_->Cleanup(Service::kStateFailure); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 785 | EXPECT_EQ(0, driver_->child_watch_tag_); |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 786 | EXPECT_EQ(0, driver_->default_service_callback_tag_); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 787 | 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 Petkov | 1fa8194 | 2012-04-02 11:38:08 +0200 | [diff] [blame] | 792 | EXPECT_FALSE(file_util::PathExists(tls_auth_file)); |
| 793 | EXPECT_TRUE(driver_->tls_auth_file_.empty()); |
Darin Petkov | 3189a47 | 2012-10-05 09:55:33 +0200 | [diff] [blame] | 794 | EXPECT_TRUE(driver_->ip_properties_.address.empty()); |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 795 | EXPECT_FALSE(driver_->IsConnectTimeoutStarted()); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 796 | } |
| 797 | |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 798 | namespace { |
| 799 | MATCHER(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 Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 808 | TEST_F(OpenVPNDriverTest, SpawnOpenVPN) { |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 809 | SetupLSBRelease(); |
| 810 | |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 811 | EXPECT_FALSE(driver_->SpawnOpenVPN()); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 812 | |
| 813 | static const char kHost[] = "192.168.2.254"; |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 814 | SetArg(flimflam::kProviderHostProperty, kHost); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 815 | driver_->tunnel_interface_ = "tun0"; |
| 816 | driver_->rpc_task_.reset(new RPCTask(&control_, this)); |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 817 | EXPECT_CALL(*management_server_, Start(_, _, _)) |
| 818 | .Times(2) |
| 819 | .WillRepeatedly(Return(true)); |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 820 | ServiceRefPtr null_service; |
| 821 | EXPECT_CALL(manager_, GetDefaultService()) |
| 822 | .Times(2) |
| 823 | .WillRepeatedly(Return(null_service)); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 824 | |
| 825 | const int kPID = 234678; |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 826 | EXPECT_CALL(glib_, |
| 827 | SpawnAsyncWithPipesCWD(_, CheckEnv(), _, _, _, _, _, _, _, _)) |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 828 | .WillOnce(Return(false)) |
| 829 | .WillOnce(DoAll(SetArgumentPointee<5>(kPID), Return(true))); |
| 830 | const int kTag = 6; |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 831 | EXPECT_CALL(glib_, ChildWatchAdd(kPID, &driver_->OnOpenVPNDied, driver_)) |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 832 | .WillOnce(Return(kTag)); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 833 | EXPECT_FALSE(driver_->SpawnOpenVPN()); |
| 834 | EXPECT_TRUE(driver_->SpawnOpenVPN()); |
| 835 | EXPECT_EQ(kPID, driver_->pid_); |
| 836 | EXPECT_EQ(kTag, driver_->child_watch_tag_); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | TEST_F(OpenVPNDriverTest, OnOpenVPNDied) { |
| 840 | const int kPID = 99999; |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 841 | driver_->device_ = device_; |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 842 | driver_->child_watch_tag_ = 333; |
| 843 | driver_->pid_ = kPID; |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 844 | 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 Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 848 | OpenVPNDriver::OnOpenVPNDied(kPID, 2, driver_); |
| 849 | EXPECT_EQ(0, driver_->child_watch_tag_); |
| 850 | EXPECT_EQ(0, driver_->pid_); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 851 | } |
| 852 | |
Darin Petkov | 6aa2187 | 2012-03-09 16:10:19 +0100 | [diff] [blame] | 853 | TEST_F(OpenVPNDriverTest, Disconnect) { |
| 854 | driver_->device_ = device_; |
| 855 | driver_->service_ = service_; |
Darin Petkov | 029d353 | 2012-04-18 14:38:04 +0200 | [diff] [blame] | 856 | EXPECT_CALL(*device_, OnDisconnected()); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 857 | EXPECT_CALL(*device_, SetEnabled(false)); |
Darin Petkov | 6aa2187 | 2012-03-09 16:10:19 +0100 | [diff] [blame] | 858 | EXPECT_CALL(device_info_, DeleteInterface(kInterfaceIndex)); |
| 859 | EXPECT_CALL(*service_, SetState(Service::kStateIdle)); |
| 860 | driver_->Disconnect(); |
Darin Petkov | a0e645e | 2012-04-25 11:38:59 +0200 | [diff] [blame] | 861 | EXPECT_FALSE(driver_->device_); |
Darin Petkov | 6aa2187 | 2012-03-09 16:10:19 +0100 | [diff] [blame] | 862 | EXPECT_FALSE(driver_->service_); |
| 863 | } |
| 864 | |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 865 | TEST_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 Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 872 | TEST_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 Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 879 | EXPECT_TRUE(driver_->IsConnectTimeoutStarted()); |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 880 | } |
| 881 | |
Paul Stewart | 291a473 | 2012-03-14 19:19:02 -0700 | [diff] [blame] | 882 | TEST_F(OpenVPNDriverTest, VerifyPaths) { |
| 883 | // Ensure that the various path constants that the OpenVPN driver uses |
Darin Petkov | a0e645e | 2012-04-25 11:38:59 +0200 | [diff] [blame] | 884 | // actually exists in the build image. Due to build dependencies, they should |
| 885 | // already exist by the time we run unit tests. |
Paul Stewart | 291a473 | 2012-03-14 19:19:02 -0700 | [diff] [blame] | 886 | |
Darin Petkov | a0e645e | 2012-04-25 11:38:59 +0200 | [diff] [blame] | 887 | // 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 Stewart | 291a473 | 2012-03-14 19:19:02 -0700 | [diff] [blame] | 900 | } |
| 901 | |
Darin Petkov | d432539 | 2012-04-23 15:48:22 +0200 | [diff] [blame] | 902 | TEST_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 Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 911 | EXPECT_EQ(kUser, GetArgs()->LookupString(flimflam::kOpenVPNUserProperty, "")); |
| 912 | } |
| 913 | |
| 914 | TEST_F(OpenVPNDriverTest, GetProvider) { |
| 915 | PropertyStore store; |
| 916 | driver_->InitPropertyStore(&store); |
| 917 | { |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 918 | KeyValueStore props; |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 919 | Error error; |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 920 | EXPECT_TRUE( |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 921 | store.GetKeyValueStoreProperty( |
| 922 | flimflam::kProviderProperty, &props, &error)); |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 923 | EXPECT_TRUE(props.LookupBool(flimflam::kPassphraseRequiredProperty, false)); |
| 924 | } |
| 925 | { |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 926 | KeyValueStore props; |
| 927 | SetArg(flimflam::kOpenVPNPasswordProperty, "random-password"); |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 928 | Error error; |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 929 | EXPECT_TRUE( |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 930 | store.GetKeyValueStoreProperty( |
| 931 | flimflam::kProviderProperty, &props, &error)); |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 932 | EXPECT_FALSE(props.LookupBool(flimflam::kPassphraseRequiredProperty, true)); |
Darin Petkov | 0223655 | 2012-06-11 13:15:19 +0200 | [diff] [blame] | 933 | EXPECT_FALSE(props.ContainsString(flimflam::kOpenVPNPasswordProperty)); |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 934 | } |
Darin Petkov | d432539 | 2012-04-23 15:48:22 +0200 | [diff] [blame] | 935 | } |
| 936 | |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 937 | TEST_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 | |
| 949 | TEST_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 Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 962 | TEST_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 Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 976 | TEST_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 Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 998 | } // namespace shill |