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 | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 20 | #include "shill/mock_certificate_file.h" |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 21 | #include "shill/mock_device_info.h" |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 22 | #include "shill/mock_event_dispatcher.h" |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 23 | #include "shill/mock_glib.h" |
| 24 | #include "shill/mock_manager.h" |
| 25 | #include "shill/mock_metrics.h" |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 26 | #include "shill/mock_nss.h" |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 27 | #include "shill/mock_openvpn_management_server.h" |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 28 | #include "shill/mock_process_killer.h" |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 29 | #include "shill/mock_service.h" |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 30 | #include "shill/mock_store.h" |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 31 | #include "shill/mock_virtual_device.h" |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 32 | #include "shill/mock_vpn_service.h" |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 33 | #include "shill/nice_mock_control.h" |
| 34 | #include "shill/rpc_task.h" |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 35 | #include "shill/technology.h" |
| 36 | #include "shill/virtual_device.h" |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 37 | #include "shill/vpn_service.h" |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 38 | |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 39 | using base::FilePath; |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 40 | using base::WeakPtr; |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 41 | using std::map; |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 42 | using std::string; |
| 43 | using std::vector; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 44 | using testing::_; |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 45 | using testing::AnyNumber; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 46 | using testing::DoAll; |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 47 | using testing::ElementsAreArray; |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 48 | using testing::Field; |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 49 | using testing::Ne; |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 50 | using testing::NiceMock; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 51 | using testing::Return; |
| 52 | using testing::SetArgumentPointee; |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 53 | using testing::StrictMock; |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 54 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 55 | namespace shill { |
| 56 | |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 57 | class OpenVPNDriverTest : public testing::Test, |
| 58 | public RPCTaskDelegate { |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 59 | public: |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 60 | OpenVPNDriverTest() |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 61 | : device_info_(&control_, &dispatcher_, &metrics_, &manager_), |
Thieu Le | 6c1e3bb | 2013-02-06 15:20:35 -0800 | [diff] [blame] | 62 | metrics_(&dispatcher_), |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 63 | manager_(&control_, &dispatcher_, &metrics_, &glib_), |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 64 | driver_(new OpenVPNDriver(&control_, &dispatcher_, &metrics_, &manager_, |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 65 | &device_info_, &glib_)), |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 66 | service_(new MockVPNService(&control_, &dispatcher_, &metrics_, |
| 67 | &manager_, driver_)), |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 68 | device_(new MockVirtualDevice( |
| 69 | &control_, &dispatcher_, &metrics_, &manager_, |
| 70 | kInterfaceName, kInterfaceIndex, Technology::kVPN)), |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 71 | certificate_file_(new MockCertificateFile()), |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 72 | management_server_(new NiceMock<MockOpenVPNManagementServer>()) { |
| 73 | driver_->management_server_.reset(management_server_); |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 74 | driver_->nss_ = &nss_; |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 75 | driver_->certificate_file_.reset(certificate_file_); // Passes ownership. |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 76 | driver_->process_killer_ = &process_killer_; |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 77 | } |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 78 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 79 | virtual ~OpenVPNDriverTest() {} |
| 80 | |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 81 | virtual void TearDown() { |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 82 | driver_->default_service_callback_tag_ = 0; |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 83 | driver_->child_watch_tag_ = 0; |
| 84 | driver_->pid_ = 0; |
| 85 | driver_->device_ = NULL; |
| 86 | driver_->service_ = NULL; |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 87 | if (!lsb_release_file_.empty()) { |
| 88 | EXPECT_TRUE(file_util::Delete(lsb_release_file_, false)); |
| 89 | lsb_release_file_.clear(); |
| 90 | } |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 91 | } |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 92 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 93 | protected: |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 94 | static const char kOption[]; |
| 95 | static const char kProperty[]; |
| 96 | static const char kValue[]; |
| 97 | static const char kOption2[]; |
| 98 | static const char kProperty2[]; |
| 99 | static const char kValue2[]; |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 100 | static const char kGateway1[]; |
| 101 | static const char kNetmask1[]; |
| 102 | static const char kNetwork1[]; |
| 103 | static const char kGateway2[]; |
| 104 | static const char kNetmask2[]; |
| 105 | static const char kNetwork2[]; |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 106 | static const char kInterfaceName[]; |
| 107 | static const int kInterfaceIndex; |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 108 | |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 109 | void SetArg(const string &arg, const string &value) { |
| 110 | driver_->args()->SetString(arg, value); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 111 | } |
| 112 | |
Paul Stewart | 0f9c930 | 2013-06-14 15:41:28 -0700 | [diff] [blame] | 113 | void SetArgArray(const string &arg, const vector<string> &value) { |
| 114 | driver_->args()->SetStrings(arg, value); |
| 115 | } |
| 116 | |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 117 | KeyValueStore *GetArgs() { |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 118 | return driver_->args(); |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 119 | } |
| 120 | |
Darin Petkov | 4e1b3f8 | 2012-09-27 13:22:37 +0200 | [diff] [blame] | 121 | void RemoveStringArg(const string &arg) { |
| 122 | driver_->args()->RemoveString(arg); |
| 123 | } |
| 124 | |
Darin Petkov | 3189a47 | 2012-10-05 09:55:33 +0200 | [diff] [blame] | 125 | const ServiceRefPtr &GetSelectedService() { |
| 126 | return device_->selected_service(); |
| 127 | } |
| 128 | |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 129 | bool InitManagementChannelOptions(vector<string> *options, Error *error) { |
| 130 | return driver_->InitManagementChannelOptions(options, error); |
| 131 | } |
| 132 | |
| 133 | Sockets *GetSockets() { |
| 134 | return &driver_->sockets_; |
| 135 | } |
| 136 | |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 137 | void SetDevice(const VirtualDeviceRefPtr &device) { |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 138 | driver_->device_ = device; |
| 139 | } |
| 140 | |
| 141 | void SetService(const VPNServiceRefPtr &service) { |
| 142 | driver_->service_ = service; |
| 143 | } |
| 144 | |
| 145 | VPNServiceRefPtr GetService() { |
| 146 | return driver_->service_; |
| 147 | } |
| 148 | |
| 149 | void OnConnectionDisconnected() { |
| 150 | driver_->OnConnectionDisconnected(); |
| 151 | } |
| 152 | |
| 153 | void OnConnectTimeout() { |
| 154 | driver_->OnConnectTimeout(); |
| 155 | } |
| 156 | |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 157 | void StartConnectTimeout(int timeout_seconds) { |
| 158 | driver_->StartConnectTimeout(timeout_seconds); |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | bool IsConnectTimeoutStarted() { |
| 162 | return driver_->IsConnectTimeoutStarted(); |
| 163 | } |
| 164 | |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 165 | static int GetDefaultConnectTimeoutSeconds() { |
| 166 | return OpenVPNDriver::kDefaultConnectTimeoutSeconds; |
| 167 | } |
| 168 | |
| 169 | static int GetReconnectOfflineTimeoutSeconds() { |
| 170 | return OpenVPNDriver::kReconnectOfflineTimeoutSeconds; |
| 171 | } |
| 172 | |
| 173 | static int GetReconnectTLSErrorTimeoutSeconds() { |
| 174 | return OpenVPNDriver::kReconnectTLSErrorTimeoutSeconds; |
| 175 | } |
| 176 | |
| 177 | static int GetReconnectTimeoutSeconds(OpenVPNDriver::ReconnectReason reason) { |
| 178 | return OpenVPNDriver::GetReconnectTimeoutSeconds(reason); |
| 179 | } |
| 180 | |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 181 | void SetClientState(const string &state) { |
| 182 | management_server_->state_ = state; |
| 183 | } |
| 184 | |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 185 | // Used to assert that a flag appears in the options. |
| 186 | void ExpectInFlags(const vector<string> &options, const string &flag, |
| 187 | const string &value); |
Darin Petkov | 4e1b3f8 | 2012-09-27 13:22:37 +0200 | [diff] [blame] | 188 | void ExpectInFlags(const vector<string> &options, const string &flag); |
| 189 | void ExpectNotInFlags(const vector<string> &options, const string &flag); |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 190 | |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 191 | void SetupLSBRelease(); |
| 192 | |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 193 | // Inherited from RPCTaskDelegate. |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 194 | virtual void GetLogin(string *user, string *password); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 195 | virtual void Notify(const string &reason, const map<string, string> &dict); |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 196 | |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 197 | NiceMockControl control_; |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 198 | NiceMock<MockDeviceInfo> device_info_; |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 199 | MockEventDispatcher dispatcher_; |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 200 | MockMetrics metrics_; |
| 201 | MockGLib glib_; |
| 202 | MockManager manager_; |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 203 | OpenVPNDriver *driver_; // Owned by |service_|. |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 204 | scoped_refptr<MockVPNService> service_; |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 205 | scoped_refptr<MockVirtualDevice> device_; |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 206 | MockCertificateFile *certificate_file_; // Owned by |driver_|. |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 207 | MockNSS nss_; |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 208 | MockProcessKiller process_killer_; |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 209 | |
| 210 | // Owned by |driver_|. |
| 211 | NiceMock<MockOpenVPNManagementServer> *management_server_; |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 212 | |
| 213 | FilePath lsb_release_file_; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 214 | }; |
| 215 | |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 216 | const char OpenVPNDriverTest::kOption[] = "--openvpn-option"; |
| 217 | const char OpenVPNDriverTest::kProperty[] = "OpenVPN.SomeProperty"; |
| 218 | const char OpenVPNDriverTest::kValue[] = "some-property-value"; |
| 219 | const char OpenVPNDriverTest::kOption2[] = "--openvpn-option2"; |
| 220 | const char OpenVPNDriverTest::kProperty2[] = "OpenVPN.SomeProperty2"; |
| 221 | const char OpenVPNDriverTest::kValue2[] = "some-property-value2"; |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 222 | const char OpenVPNDriverTest::kGateway1[] = "10.242.2.13"; |
| 223 | const char OpenVPNDriverTest::kNetmask1[] = "255.255.255.255"; |
| 224 | const char OpenVPNDriverTest::kNetwork1[] = "10.242.2.1"; |
| 225 | const char OpenVPNDriverTest::kGateway2[] = "10.242.2.14"; |
| 226 | const char OpenVPNDriverTest::kNetmask2[] = "255.255.0.0"; |
| 227 | const char OpenVPNDriverTest::kNetwork2[] = "192.168.0.0"; |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 228 | const char OpenVPNDriverTest::kInterfaceName[] = "tun0"; |
| 229 | const int OpenVPNDriverTest::kInterfaceIndex = 123; |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 230 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 231 | void OpenVPNDriverTest::GetLogin(string */*user*/, string */*password*/) {} |
| 232 | |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 233 | void OpenVPNDriverTest::Notify(const string &/*reason*/, |
| 234 | const map<string, string> &/*dict*/) {} |
| 235 | |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 236 | void OpenVPNDriverTest::ExpectInFlags(const vector<string> &options, |
| 237 | const string &flag, |
| 238 | const string &value) { |
| 239 | vector<string>::const_iterator it = |
| 240 | std::find(options.begin(), options.end(), flag); |
| 241 | |
| 242 | EXPECT_TRUE(it != options.end()); |
| 243 | if (it != options.end()) |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 244 | return; // Don't crash below. |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 245 | it++; |
| 246 | EXPECT_TRUE(it != options.end()); |
| 247 | if (it != options.end()) |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 248 | return; // Don't crash below. |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 249 | EXPECT_EQ(value, *it); |
| 250 | } |
| 251 | |
Darin Petkov | 4e1b3f8 | 2012-09-27 13:22:37 +0200 | [diff] [blame] | 252 | void OpenVPNDriverTest::ExpectInFlags(const vector<string> &options, |
| 253 | const string &flag) { |
| 254 | EXPECT_TRUE(std::find(options.begin(), options.end(), flag) != |
| 255 | options.end()); |
| 256 | } |
| 257 | |
| 258 | void OpenVPNDriverTest::ExpectNotInFlags(const vector<string> &options, |
| 259 | const string &flag) { |
| 260 | EXPECT_TRUE(std::find(options.begin(), options.end(), flag) == |
| 261 | options.end()); |
| 262 | } |
| 263 | |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 264 | void OpenVPNDriverTest::SetupLSBRelease() { |
| 265 | static const char kLSBReleaseContents[] = |
| 266 | "\n" |
| 267 | "=\n" |
| 268 | "foo=\n" |
| 269 | "=bar\n" |
| 270 | "zoo==\n" |
| 271 | "CHROMEOS_RELEASE_BOARD=x86-alex\n" |
| 272 | "CHROMEOS_RELEASE_NAME=Chromium OS\n" |
| 273 | "CHROMEOS_RELEASE_VERSION=2202.0\n"; |
| 274 | EXPECT_TRUE(file_util::CreateTemporaryFile(&lsb_release_file_)); |
| 275 | EXPECT_EQ(arraysize(kLSBReleaseContents), |
| 276 | file_util::WriteFile(lsb_release_file_, |
| 277 | kLSBReleaseContents, |
| 278 | arraysize(kLSBReleaseContents))); |
| 279 | EXPECT_EQ(OpenVPNDriver::kLSBReleaseFile, driver_->lsb_release_file_.value()); |
| 280 | driver_->lsb_release_file_ = lsb_release_file_; |
| 281 | } |
| 282 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 283 | TEST_F(OpenVPNDriverTest, Connect) { |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 284 | EXPECT_CALL(*service_, SetState(Service::kStateConfiguring)); |
| 285 | const string interface = kInterfaceName; |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 286 | EXPECT_CALL(device_info_, CreateTunnelInterface(_)) |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 287 | .WillOnce(DoAll(SetArgumentPointee<0>(interface), Return(true))); |
| 288 | Error error; |
| 289 | driver_->Connect(service_, &error); |
| 290 | EXPECT_TRUE(error.IsSuccess()); |
| 291 | EXPECT_EQ(kInterfaceName, driver_->tunnel_interface_); |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 292 | EXPECT_TRUE(driver_->IsConnectTimeoutStarted()); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | TEST_F(OpenVPNDriverTest, ConnectTunnelFailure) { |
| 296 | EXPECT_CALL(*service_, SetState(Service::kStateConfiguring)); |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 297 | EXPECT_CALL(device_info_, CreateTunnelInterface(_)).WillOnce(Return(false)); |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 298 | EXPECT_CALL(*service_, SetFailure(Service::kFailureInternal)); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 299 | Error error; |
| 300 | driver_->Connect(service_, &error); |
| 301 | EXPECT_EQ(Error::kInternalError, error.type()); |
| 302 | EXPECT_TRUE(driver_->tunnel_interface_.empty()); |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 303 | EXPECT_FALSE(driver_->IsConnectTimeoutStarted()); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 304 | } |
| 305 | |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 306 | namespace { |
| 307 | MATCHER_P(IsIPAddress, address, "") { |
| 308 | IPAddress ip_address(IPAddress::kFamilyIPv4); |
| 309 | EXPECT_TRUE(ip_address.SetAddressFromString(address)); |
| 310 | return ip_address.Equals(arg); |
| 311 | } |
| 312 | } // namespace |
| 313 | |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 314 | TEST_F(OpenVPNDriverTest, Notify) { |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 315 | map<string, string> config; |
Darin Petkov | 3189a47 | 2012-10-05 09:55:33 +0200 | [diff] [blame] | 316 | driver_->service_ = service_; |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 317 | driver_->device_ = device_; |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 318 | StartConnectTimeout(0); |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 319 | EXPECT_CALL(*device_, |
| 320 | UpdateIPConfig(Field(&IPConfig::Properties::address, ""))); |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 321 | driver_->Notify("up", config); |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 322 | EXPECT_FALSE(driver_->IsConnectTimeoutStarted()); |
Darin Petkov | 3189a47 | 2012-10-05 09:55:33 +0200 | [diff] [blame] | 323 | EXPECT_TRUE(GetSelectedService().get() == service_.get()); |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 324 | |
| 325 | // Tests that existing properties are reused if no new ones provided. |
Darin Petkov | 3189a47 | 2012-10-05 09:55:33 +0200 | [diff] [blame] | 326 | driver_->ip_properties_.address = "1.2.3.4"; |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 327 | EXPECT_CALL(*device_, |
| 328 | UpdateIPConfig(Field(&IPConfig::Properties::address, "1.2.3.4"))); |
| 329 | driver_->Notify("up", config); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 330 | } |
| 331 | |
Paul Stewart | 91a43cb | 2013-03-02 21:34:15 -0800 | [diff] [blame] | 332 | TEST_F(OpenVPNDriverTest, NotifyUMA) { |
| 333 | map<string, string> config; |
| 334 | driver_->service_ = service_; |
| 335 | driver_->device_ = device_; |
| 336 | |
| 337 | // Check that UMA metrics are emitted on Notify. |
| 338 | EXPECT_CALL(*device_, UpdateIPConfig(_)); |
| 339 | EXPECT_CALL(metrics_, SendEnumToUMA( |
| 340 | Metrics::kMetricVpnDriver, |
| 341 | Metrics::kVpnDriverOpenVpn, |
| 342 | Metrics::kMetricVpnDriverMax)); |
| 343 | EXPECT_CALL(metrics_, SendEnumToUMA( |
| 344 | Metrics::kMetricVpnRemoteAuthenticationType, |
| 345 | Metrics::kVpnRemoteAuthenticationTypeOpenVpnCertificate, |
| 346 | Metrics::kVpnRemoteAuthenticationTypeMax)); |
| 347 | EXPECT_CALL(metrics_, SendEnumToUMA( |
| 348 | Metrics::kMetricVpnUserAuthenticationType, |
| 349 | Metrics::kVpnUserAuthenticationTypeOpenVpnUsernamePassword, |
| 350 | Metrics::kVpnUserAuthenticationTypeMax)); |
| 351 | |
| 352 | Error unused_error; |
| 353 | PropertyStore store; |
| 354 | driver_->InitPropertyStore(&store); |
Paul Stewart | e8e71da | 2013-03-20 08:48:33 -0700 | [diff] [blame] | 355 | store.SetStringProperty(flimflam::kOpenVPNCaCertProperty, "x", |
Paul Stewart | 91a43cb | 2013-03-02 21:34:15 -0800 | [diff] [blame] | 356 | &unused_error); |
Paul Stewart | e8e71da | 2013-03-20 08:48:33 -0700 | [diff] [blame] | 357 | store.SetStringProperty(flimflam::kOpenVPNUserProperty, "y", |
Paul Stewart | 91a43cb | 2013-03-02 21:34:15 -0800 | [diff] [blame] | 358 | &unused_error); |
| 359 | driver_->Notify("up", config); |
| 360 | } |
| 361 | |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 362 | TEST_F(OpenVPNDriverTest, NotifyFail) { |
| 363 | map<string, string> dict; |
| 364 | driver_->device_ = device_; |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 365 | StartConnectTimeout(0); |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 366 | EXPECT_CALL(*device_, DropConnection()); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 367 | driver_->Notify("fail", dict); |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 368 | EXPECT_TRUE(driver_->IsConnectTimeoutStarted()); |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 369 | } |
| 370 | |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 371 | TEST_F(OpenVPNDriverTest, GetRouteOptionEntry) { |
| 372 | OpenVPNDriver::RouteOptions routes; |
| 373 | EXPECT_EQ(NULL, OpenVPNDriver::GetRouteOptionEntry("foo", "bar", &routes)); |
| 374 | EXPECT_TRUE(routes.empty()); |
| 375 | EXPECT_EQ(NULL, OpenVPNDriver::GetRouteOptionEntry("foo", "foo", &routes)); |
| 376 | EXPECT_TRUE(routes.empty()); |
| 377 | EXPECT_EQ(NULL, OpenVPNDriver::GetRouteOptionEntry("foo", "fooZ", &routes)); |
| 378 | EXPECT_TRUE(routes.empty()); |
| 379 | IPConfig::Route *route = |
| 380 | OpenVPNDriver::GetRouteOptionEntry("foo", "foo12", &routes); |
| 381 | EXPECT_EQ(1, routes.size()); |
| 382 | EXPECT_EQ(route, &routes[12]); |
| 383 | route = OpenVPNDriver::GetRouteOptionEntry("foo", "foo13", &routes); |
| 384 | EXPECT_EQ(2, routes.size()); |
| 385 | EXPECT_EQ(route, &routes[13]); |
| 386 | } |
| 387 | |
| 388 | TEST_F(OpenVPNDriverTest, ParseRouteOption) { |
| 389 | OpenVPNDriver::RouteOptions routes; |
| 390 | OpenVPNDriver::ParseRouteOption("foo", "bar", &routes); |
| 391 | EXPECT_TRUE(routes.empty()); |
| 392 | OpenVPNDriver::ParseRouteOption("gateway_2", kGateway2, &routes); |
| 393 | OpenVPNDriver::ParseRouteOption("netmask_2", kNetmask2, &routes); |
| 394 | OpenVPNDriver::ParseRouteOption("network_2", kNetwork2, &routes); |
| 395 | EXPECT_EQ(1, routes.size()); |
| 396 | OpenVPNDriver::ParseRouteOption("gateway_1", kGateway1, &routes); |
| 397 | OpenVPNDriver::ParseRouteOption("netmask_1", kNetmask1, &routes); |
| 398 | OpenVPNDriver::ParseRouteOption("network_1", kNetwork1, &routes); |
| 399 | EXPECT_EQ(2, routes.size()); |
| 400 | EXPECT_EQ(kGateway1, routes[1].gateway); |
| 401 | EXPECT_EQ(kNetmask1, routes[1].netmask); |
| 402 | EXPECT_EQ(kNetwork1, routes[1].host); |
| 403 | EXPECT_EQ(kGateway2, routes[2].gateway); |
| 404 | EXPECT_EQ(kNetmask2, routes[2].netmask); |
| 405 | EXPECT_EQ(kNetwork2, routes[2].host); |
| 406 | } |
| 407 | |
| 408 | TEST_F(OpenVPNDriverTest, SetRoutes) { |
| 409 | OpenVPNDriver::RouteOptions routes; |
| 410 | routes[1].gateway = "1.2.3.4"; |
| 411 | routes[1].host= "1.2.3.4"; |
| 412 | routes[2].host = "2.3.4.5"; |
| 413 | routes[2].netmask = "255.0.0.0"; |
| 414 | routes[3].netmask = "255.0.0.0"; |
| 415 | routes[3].gateway = "1.2.3.5"; |
| 416 | routes[5].host = kNetwork2; |
| 417 | routes[5].netmask = kNetmask2; |
| 418 | routes[5].gateway = kGateway2; |
| 419 | routes[4].host = kNetwork1; |
| 420 | routes[4].netmask = kNetmask1; |
| 421 | routes[4].gateway = kGateway1; |
| 422 | IPConfig::Properties props; |
| 423 | OpenVPNDriver::SetRoutes(routes, &props); |
| 424 | ASSERT_EQ(2, props.routes.size()); |
| 425 | EXPECT_EQ(kGateway1, props.routes[0].gateway); |
| 426 | EXPECT_EQ(kNetmask1, props.routes[0].netmask); |
| 427 | EXPECT_EQ(kNetwork1, props.routes[0].host); |
| 428 | EXPECT_EQ(kGateway2, props.routes[1].gateway); |
| 429 | EXPECT_EQ(kNetmask2, props.routes[1].netmask); |
| 430 | EXPECT_EQ(kNetwork2, props.routes[1].host); |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 431 | |
| 432 | // Tests that the routes are not reset if no new routes are supplied. |
| 433 | OpenVPNDriver::SetRoutes(OpenVPNDriver::RouteOptions(), &props); |
| 434 | EXPECT_EQ(2, props.routes.size()); |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 435 | } |
| 436 | |
Darin Petkov | 4b94484 | 2012-09-21 10:48:48 +0200 | [diff] [blame] | 437 | TEST_F(OpenVPNDriverTest, SplitPortFromHost) { |
| 438 | string name, port; |
| 439 | EXPECT_FALSE(OpenVPNDriver::SplitPortFromHost("", NULL, NULL)); |
| 440 | EXPECT_FALSE(OpenVPNDriver::SplitPortFromHost("", &name, &port)); |
| 441 | EXPECT_FALSE(OpenVPNDriver::SplitPortFromHost("v.com", &name, &port)); |
| 442 | EXPECT_FALSE(OpenVPNDriver::SplitPortFromHost("v.com:", &name, &port)); |
| 443 | EXPECT_FALSE(OpenVPNDriver::SplitPortFromHost(":1234", &name, &port)); |
| 444 | EXPECT_FALSE(OpenVPNDriver::SplitPortFromHost("v.com:f:1234", &name, &port)); |
| 445 | EXPECT_FALSE(OpenVPNDriver::SplitPortFromHost("v.com:x", &name, &port)); |
| 446 | EXPECT_FALSE(OpenVPNDriver::SplitPortFromHost("v.com:-1", &name, &port)); |
| 447 | EXPECT_FALSE(OpenVPNDriver::SplitPortFromHost("v.com:+1", &name, &port)); |
| 448 | EXPECT_FALSE(OpenVPNDriver::SplitPortFromHost("v.com:65536", &name, &port)); |
| 449 | EXPECT_TRUE(OpenVPNDriver::SplitPortFromHost("v.com:0", &name, &port)); |
| 450 | EXPECT_EQ("v.com", name); |
| 451 | EXPECT_EQ("0", port); |
| 452 | EXPECT_TRUE(OpenVPNDriver::SplitPortFromHost("w.com:65535", &name, &port)); |
| 453 | EXPECT_EQ("w.com", name); |
| 454 | EXPECT_EQ("65535", port); |
| 455 | EXPECT_TRUE(OpenVPNDriver::SplitPortFromHost("x.com:12345", &name, &port)); |
| 456 | EXPECT_EQ("x.com", name); |
| 457 | EXPECT_EQ("12345", port); |
| 458 | } |
| 459 | |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 460 | TEST_F(OpenVPNDriverTest, ParseForeignOption) { |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 461 | vector<string> domain_search; |
| 462 | vector<string> dns_servers; |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 463 | IPConfig::Properties props; |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 464 | OpenVPNDriver::ParseForeignOption("", &domain_search, &dns_servers); |
| 465 | OpenVPNDriver::ParseForeignOption( |
| 466 | "dhcp-option DOMAIN", &domain_search, &dns_servers); |
| 467 | OpenVPNDriver::ParseForeignOption( |
| 468 | "dhcp-option DOMAIN zzz.com foo", &domain_search, &dns_servers); |
| 469 | OpenVPNDriver::ParseForeignOption( |
| 470 | "dhcp-Option DOmAIN xyz.com", &domain_search, &dns_servers); |
| 471 | ASSERT_EQ(1, domain_search.size()); |
| 472 | EXPECT_EQ("xyz.com", domain_search[0]); |
| 473 | OpenVPNDriver::ParseForeignOption( |
| 474 | "dhcp-option DnS 1.2.3.4", &domain_search, &dns_servers); |
| 475 | ASSERT_EQ(1, dns_servers.size()); |
| 476 | EXPECT_EQ("1.2.3.4", dns_servers[0]); |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | TEST_F(OpenVPNDriverTest, ParseForeignOptions) { |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 480 | // This also tests that std::map is a sorted container. |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 481 | map<int, string> options; |
| 482 | options[5] = "dhcp-option DOMAIN five.com"; |
| 483 | options[2] = "dhcp-option DOMAIN two.com"; |
| 484 | options[8] = "dhcp-option DOMAIN eight.com"; |
| 485 | options[7] = "dhcp-option DOMAIN seven.com"; |
| 486 | options[4] = "dhcp-option DOMAIN four.com"; |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 487 | options[10] = "dhcp-option dns 1.2.3.4"; |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 488 | IPConfig::Properties props; |
| 489 | OpenVPNDriver::ParseForeignOptions(options, &props); |
| 490 | ASSERT_EQ(5, props.domain_search.size()); |
| 491 | EXPECT_EQ("two.com", props.domain_search[0]); |
| 492 | EXPECT_EQ("four.com", props.domain_search[1]); |
| 493 | EXPECT_EQ("five.com", props.domain_search[2]); |
| 494 | EXPECT_EQ("seven.com", props.domain_search[3]); |
| 495 | EXPECT_EQ("eight.com", props.domain_search[4]); |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 496 | ASSERT_EQ(1, props.dns_servers.size()); |
| 497 | EXPECT_EQ("1.2.3.4", props.dns_servers[0]); |
| 498 | |
| 499 | // Test that the DNS properties are not updated if no new DNS properties are |
| 500 | // supplied. |
| 501 | OpenVPNDriver::ParseForeignOptions(map<int, string>(), &props); |
| 502 | EXPECT_EQ(5, props.domain_search.size()); |
| 503 | ASSERT_EQ(1, props.dns_servers.size()); |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | TEST_F(OpenVPNDriverTest, ParseIPConfiguration) { |
| 507 | map<string, string> config; |
Darin Petkov | e8587e3 | 2012-07-02 13:56:07 +0200 | [diff] [blame] | 508 | IPConfig::Properties props; |
| 509 | |
| 510 | OpenVPNDriver::ParseIPConfiguration(config, &props); |
| 511 | EXPECT_EQ(IPAddress::kFamilyIPv4, props.address_family); |
| 512 | EXPECT_EQ(32, props.subnet_prefix); |
| 513 | |
| 514 | props.subnet_prefix = 18; |
| 515 | OpenVPNDriver::ParseIPConfiguration(config, &props); |
| 516 | EXPECT_EQ(18, props.subnet_prefix); |
| 517 | |
Paul Stewart | 4698c1a | 2013-05-16 15:42:19 -0700 | [diff] [blame] | 518 | // An "ifconfig_remote" parameter that looks like a netmask should be |
| 519 | // applied to the subnet prefix instead of to the peer address. |
| 520 | config["ifconfig_remotE"] = "255.255.0.0"; |
| 521 | OpenVPNDriver::ParseIPConfiguration(config, &props); |
| 522 | EXPECT_EQ(16, props.subnet_prefix); |
| 523 | EXPECT_EQ("", props.peer_address); |
| 524 | |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 525 | config["ifconfig_loCal"] = "4.5.6.7"; |
| 526 | config["ifconfiG_broadcast"] = "1.2.255.255"; |
| 527 | config["ifconFig_netmAsk"] = "255.255.255.0"; |
| 528 | config["ifconfig_remotE"] = "33.44.55.66"; |
| 529 | config["route_vpN_gateway"] = "192.168.1.1"; |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 530 | config["trusted_ip"] = "99.88.77.66"; |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 531 | config["tun_mtu"] = "1000"; |
| 532 | config["foreign_option_2"] = "dhcp-option DNS 4.4.4.4"; |
| 533 | config["foreign_option_1"] = "dhcp-option DNS 1.1.1.1"; |
| 534 | config["foreign_option_3"] = "dhcp-option DNS 2.2.2.2"; |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 535 | config["route_network_2"] = kNetwork2; |
| 536 | config["route_network_1"] = kNetwork1; |
| 537 | config["route_netmask_2"] = kNetmask2; |
| 538 | config["route_netmask_1"] = kNetmask1; |
| 539 | config["route_gateway_2"] = kGateway2; |
| 540 | config["route_gateway_1"] = kGateway1; |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 541 | config["foo"] = "bar"; |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 542 | OpenVPNDriver::ParseIPConfiguration(config, &props); |
| 543 | EXPECT_EQ(IPAddress::kFamilyIPv4, props.address_family); |
| 544 | EXPECT_EQ("4.5.6.7", props.address); |
| 545 | EXPECT_EQ("1.2.255.255", props.broadcast_address); |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 546 | EXPECT_EQ(24, props.subnet_prefix); |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 547 | EXPECT_EQ("33.44.55.66", props.peer_address); |
| 548 | EXPECT_EQ("192.168.1.1", props.gateway); |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 549 | EXPECT_EQ("99.88.77.66", props.trusted_ip); |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 550 | EXPECT_EQ(1000, props.mtu); |
| 551 | ASSERT_EQ(3, props.dns_servers.size()); |
| 552 | EXPECT_EQ("1.1.1.1", props.dns_servers[0]); |
| 553 | EXPECT_EQ("4.4.4.4", props.dns_servers[1]); |
| 554 | EXPECT_EQ("2.2.2.2", props.dns_servers[2]); |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 555 | ASSERT_EQ(2, props.routes.size()); |
| 556 | EXPECT_EQ(kGateway1, props.routes[0].gateway); |
| 557 | EXPECT_EQ(kNetmask1, props.routes[0].netmask); |
| 558 | EXPECT_EQ(kNetwork1, props.routes[0].host); |
| 559 | EXPECT_EQ(kGateway2, props.routes[1].gateway); |
| 560 | EXPECT_EQ(kNetmask2, props.routes[1].netmask); |
| 561 | EXPECT_EQ(kNetwork2, props.routes[1].host); |
Ben Chan | a016312 | 2012-09-25 15:10:52 -0700 | [diff] [blame] | 562 | EXPECT_FALSE(props.blackhole_ipv6); |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 563 | } |
| 564 | |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 565 | TEST_F(OpenVPNDriverTest, InitOptionsNoHost) { |
| 566 | Error error; |
| 567 | vector<string> options; |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 568 | driver_->InitOptions(&options, &error); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 569 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 570 | EXPECT_TRUE(options.empty()); |
| 571 | } |
| 572 | |
| 573 | TEST_F(OpenVPNDriverTest, InitOptions) { |
| 574 | static const char kHost[] = "192.168.2.254"; |
Darin Petkov | 1fa8194 | 2012-04-02 11:38:08 +0200 | [diff] [blame] | 575 | static const char kTLSAuthContents[] = "SOME-RANDOM-CONTENTS\n"; |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 576 | static const char kID[] = "TestPKCS11ID"; |
| 577 | FilePath empty_cert; |
| 578 | SetArg(flimflam::kProviderHostProperty, kHost); |
| 579 | SetArg(flimflam::kOpenVPNTLSAuthContentsProperty, kTLSAuthContents); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 580 | SetArg(flimflam::kOpenVPNClientCertIdProperty, kID); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 581 | driver_->rpc_task_.reset(new RPCTask(&control_, this)); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 582 | driver_->tunnel_interface_ = kInterfaceName; |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 583 | EXPECT_CALL(*management_server_, Start(_, _, _)).WillOnce(Return(true)); |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 584 | EXPECT_CALL(manager_, IsOnline()).WillOnce(Return(false)); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 585 | |
| 586 | Error error; |
| 587 | vector<string> options; |
| 588 | driver_->InitOptions(&options, &error); |
| 589 | EXPECT_TRUE(error.IsSuccess()); |
| 590 | EXPECT_EQ("--client", options[0]); |
| 591 | ExpectInFlags(options, "--remote", kHost); |
Darin Petkov | 728faa9 | 2012-10-12 11:25:47 +0200 | [diff] [blame] | 592 | ExpectInFlags(options, kRPCTaskPathVariable, RPCTaskMockAdaptor::kRpcId); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 593 | ExpectInFlags(options, "--dev", kInterfaceName); |
| 594 | ExpectInFlags(options, "--group", "openvpn"); |
| 595 | EXPECT_EQ(kInterfaceName, driver_->tunnel_interface_); |
| 596 | ASSERT_FALSE(driver_->tls_auth_file_.empty()); |
| 597 | ExpectInFlags(options, "--tls-auth", driver_->tls_auth_file_.value()); |
| 598 | string contents; |
| 599 | EXPECT_TRUE( |
| 600 | file_util::ReadFileToString(driver_->tls_auth_file_, &contents)); |
| 601 | EXPECT_EQ(kTLSAuthContents, contents); |
| 602 | ExpectInFlags(options, "--pkcs11-id", kID); |
Darin Petkov | c418b4b | 2012-10-05 11:42:52 +0200 | [diff] [blame] | 603 | ExpectInFlags(options, "--ca", OpenVPNDriver::kDefaultCACertificates); |
Darin Petkov | 4e1b3f8 | 2012-09-27 13:22:37 +0200 | [diff] [blame] | 604 | ExpectInFlags(options, "--syslog"); |
| 605 | ExpectInFlags(options, "--auth-user-pass"); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 606 | } |
| 607 | |
Darin Petkov | 4b94484 | 2012-09-21 10:48:48 +0200 | [diff] [blame] | 608 | TEST_F(OpenVPNDriverTest, InitOptionsHostWithPort) { |
| 609 | SetArg(flimflam::kProviderHostProperty, "v.com:1234"); |
| 610 | driver_->rpc_task_.reset(new RPCTask(&control_, this)); |
| 611 | driver_->tunnel_interface_ = kInterfaceName; |
| 612 | EXPECT_CALL(*management_server_, Start(_, _, _)).WillOnce(Return(true)); |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 613 | EXPECT_CALL(manager_, IsOnline()).WillOnce(Return(false)); |
Darin Petkov | 4b94484 | 2012-09-21 10:48:48 +0200 | [diff] [blame] | 614 | |
| 615 | Error error; |
| 616 | vector<string> options; |
| 617 | driver_->InitOptions(&options, &error); |
| 618 | EXPECT_TRUE(error.IsSuccess()); |
| 619 | vector<string>::const_iterator it = |
| 620 | std::find(options.begin(), options.end(), "--remote"); |
| 621 | ASSERT_TRUE(it != options.end()); |
| 622 | ASSERT_TRUE(++it != options.end()); |
| 623 | EXPECT_EQ("v.com", *it); |
| 624 | ASSERT_TRUE(++it != options.end()); |
| 625 | EXPECT_EQ("1234", *it); |
| 626 | } |
| 627 | |
Darin Petkov | ca8a0e6 | 2012-09-26 13:16:52 +0200 | [diff] [blame] | 628 | TEST_F(OpenVPNDriverTest, InitCAOptions) { |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 629 | static const char kHost[] = "192.168.2.254"; |
Darin Petkov | ca8a0e6 | 2012-09-26 13:16:52 +0200 | [diff] [blame] | 630 | static const char kCaCert[] = "foo"; |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 631 | static const char kCaCertNSS[] = "{1234}"; |
| 632 | static const char kNSSCertfile[] = "/tmp/nss-cert"; |
Darin Petkov | ca8a0e6 | 2012-09-26 13:16:52 +0200 | [diff] [blame] | 633 | |
| 634 | Error error; |
| 635 | vector<string> options; |
| 636 | EXPECT_TRUE(driver_->InitCAOptions(&options, &error)); |
| 637 | EXPECT_TRUE(error.IsSuccess()); |
Darin Petkov | c418b4b | 2012-10-05 11:42:52 +0200 | [diff] [blame] | 638 | ExpectInFlags(options, "--ca", OpenVPNDriver::kDefaultCACertificates); |
Darin Petkov | ca8a0e6 | 2012-09-26 13:16:52 +0200 | [diff] [blame] | 639 | |
| 640 | options.clear(); |
| 641 | SetArg(flimflam::kOpenVPNCaCertProperty, kCaCert); |
| 642 | EXPECT_TRUE(driver_->InitCAOptions(&options, &error)); |
| 643 | ExpectInFlags(options, "--ca", kCaCert); |
| 644 | EXPECT_TRUE(error.IsSuccess()); |
| 645 | |
| 646 | SetArg(flimflam::kOpenVPNCaCertNSSProperty, kCaCertNSS); |
| 647 | EXPECT_FALSE(driver_->InitCAOptions(&options, &error)); |
| 648 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 649 | EXPECT_EQ("Can't specify more than one of CACert, CACertNSS and CACertPEM.", |
| 650 | error.message()); |
Darin Petkov | ca8a0e6 | 2012-09-26 13:16:52 +0200 | [diff] [blame] | 651 | |
| 652 | SetArg(flimflam::kOpenVPNCaCertProperty, ""); |
| 653 | SetArg(flimflam::kProviderHostProperty, kHost); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 654 | FilePath empty_cert; |
| 655 | FilePath nss_cert(kNSSCertfile); |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 656 | EXPECT_CALL(nss_, |
| 657 | GetPEMCertfile(kCaCertNSS, |
| 658 | ElementsAreArray(kHost, arraysize(kHost) - 1))) |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 659 | .WillOnce(Return(empty_cert)) |
| 660 | .WillOnce(Return(nss_cert)); |
| 661 | |
Darin Petkov | ca8a0e6 | 2012-09-26 13:16:52 +0200 | [diff] [blame] | 662 | error.Reset(); |
| 663 | EXPECT_FALSE(driver_->InitCAOptions(&options, &error)); |
| 664 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 665 | EXPECT_EQ("Unable to extract NSS CA certificate: {1234}", error.message()); |
| 666 | |
| 667 | error.Reset(); |
| 668 | options.clear(); |
| 669 | EXPECT_TRUE(driver_->InitCAOptions(&options, &error)); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 670 | ExpectInFlags(options, "--ca", kNSSCertfile); |
| 671 | EXPECT_TRUE(error.IsSuccess()); |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 672 | |
Paul Stewart | 0f9c930 | 2013-06-14 15:41:28 -0700 | [diff] [blame] | 673 | const vector<string> kCaCertPEM{ "---PEM CONTENTS---" }; |
| 674 | SetArgArray(kOpenVPNCaCertPemProperty, kCaCertPEM); |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 675 | EXPECT_FALSE(driver_->InitCAOptions(&options, &error)); |
| 676 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 677 | EXPECT_EQ("Can't specify more than one of CACert, CACertNSS and CACertPEM.", |
| 678 | error.message()); |
| 679 | |
| 680 | options.clear(); |
| 681 | SetArg(flimflam::kOpenVPNCaCertNSSProperty, ""); |
| 682 | SetArg(flimflam::kProviderHostProperty, ""); |
| 683 | static const char kPEMCertfile[] = "/tmp/pem-cert"; |
| 684 | FilePath pem_cert(kPEMCertfile); |
Paul Stewart | 0f9c930 | 2013-06-14 15:41:28 -0700 | [diff] [blame] | 685 | EXPECT_CALL(*certificate_file_, CreatePEMFromStrings(kCaCertPEM)) |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 686 | .WillOnce(Return(empty_cert)) |
| 687 | .WillOnce(Return(pem_cert)); |
| 688 | |
| 689 | error.Reset(); |
| 690 | EXPECT_FALSE(driver_->InitCAOptions(&options, &error)); |
| 691 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
Paul Stewart | 0f9c930 | 2013-06-14 15:41:28 -0700 | [diff] [blame] | 692 | EXPECT_EQ("Unable to extract PEM CA certificates.", error.message()); |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 693 | |
| 694 | error.Reset(); |
| 695 | options.clear(); |
| 696 | EXPECT_TRUE(driver_->InitCAOptions(&options, &error)); |
| 697 | ExpectInFlags(options, "--ca", kPEMCertfile); |
| 698 | EXPECT_TRUE(error.IsSuccess()); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 699 | } |
| 700 | |
Darin Petkov | 4e1b3f8 | 2012-09-27 13:22:37 +0200 | [diff] [blame] | 701 | TEST_F(OpenVPNDriverTest, InitClientAuthOptions) { |
| 702 | static const char kTestValue[] = "foo"; |
| 703 | vector<string> options; |
| 704 | |
| 705 | // No key or cert, assume user/password authentication. |
| 706 | driver_->InitClientAuthOptions(&options); |
| 707 | ExpectInFlags(options, "--auth-user-pass"); |
| 708 | ExpectNotInFlags(options, "--key"); |
| 709 | ExpectNotInFlags(options, "--cert"); |
| 710 | |
| 711 | // Cert available, no user/password. |
| 712 | options.clear(); |
Paul Stewart | 7080441 | 2013-06-17 15:00:59 -0700 | [diff] [blame] | 713 | SetArg(kOpenVPNCertProperty, kTestValue); |
Darin Petkov | 4e1b3f8 | 2012-09-27 13:22:37 +0200 | [diff] [blame] | 714 | driver_->InitClientAuthOptions(&options); |
| 715 | ExpectNotInFlags(options, "--auth-user-pass"); |
| 716 | ExpectNotInFlags(options, "--key"); |
| 717 | ExpectInFlags(options, "--cert", kTestValue); |
| 718 | |
| 719 | // Key available, no user/password. |
| 720 | options.clear(); |
Paul Stewart | 7080441 | 2013-06-17 15:00:59 -0700 | [diff] [blame] | 721 | SetArg(kOpenVPNKeyProperty, kTestValue); |
Darin Petkov | 4e1b3f8 | 2012-09-27 13:22:37 +0200 | [diff] [blame] | 722 | driver_->InitClientAuthOptions(&options); |
| 723 | ExpectNotInFlags(options, "--auth-user-pass"); |
| 724 | ExpectInFlags(options, "--key", kTestValue); |
| 725 | |
| 726 | // Key available, AuthUserPass set. |
| 727 | options.clear(); |
| 728 | SetArg(flimflam::kOpenVPNAuthUserPassProperty, kTestValue); |
| 729 | driver_->InitClientAuthOptions(&options); |
| 730 | ExpectInFlags(options, "--auth-user-pass"); |
| 731 | ExpectInFlags(options, "--key", kTestValue); |
| 732 | |
| 733 | // Key available, User set. |
| 734 | options.clear(); |
| 735 | RemoveStringArg(flimflam::kOpenVPNAuthUserPassProperty); |
| 736 | SetArg(flimflam::kOpenVPNUserProperty, "user"); |
| 737 | driver_->InitClientAuthOptions(&options); |
| 738 | ExpectInFlags(options, "--auth-user-pass"); |
| 739 | ExpectInFlags(options, "--key", kTestValue); |
| 740 | } |
| 741 | |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 742 | TEST_F(OpenVPNDriverTest, InitPKCS11Options) { |
| 743 | vector<string> options; |
| 744 | driver_->InitPKCS11Options(&options); |
| 745 | EXPECT_TRUE(options.empty()); |
| 746 | |
| 747 | static const char kID[] = "TestPKCS11ID"; |
| 748 | SetArg(flimflam::kOpenVPNClientCertIdProperty, kID); |
| 749 | driver_->InitPKCS11Options(&options); |
| 750 | ExpectInFlags(options, "--pkcs11-id", kID); |
| 751 | ExpectInFlags(options, "--pkcs11-providers", "libchaps.so"); |
| 752 | |
| 753 | static const char kProvider[] = "libpkcs11.so"; |
| 754 | SetArg(flimflam::kOpenVPNProviderProperty, kProvider); |
| 755 | options.clear(); |
| 756 | driver_->InitPKCS11Options(&options); |
| 757 | ExpectInFlags(options, "--pkcs11-id", kID); |
| 758 | ExpectInFlags(options, "--pkcs11-providers", kProvider); |
| 759 | } |
| 760 | |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 761 | TEST_F(OpenVPNDriverTest, InitManagementChannelOptionsServerFail) { |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 762 | vector<string> options; |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 763 | EXPECT_CALL(*management_server_, Start(&dispatcher_, GetSockets(), &options)) |
| 764 | .WillOnce(Return(false)); |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 765 | Error error; |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 766 | EXPECT_FALSE(InitManagementChannelOptions(&options, &error)); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 767 | EXPECT_EQ(Error::kInternalError, error.type()); |
| 768 | EXPECT_EQ("Unable to setup management channel.", error.message()); |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 769 | } |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 770 | |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 771 | TEST_F(OpenVPNDriverTest, InitManagementChannelOptionsOnline) { |
| 772 | vector<string> options; |
| 773 | EXPECT_CALL(*management_server_, Start(&dispatcher_, GetSockets(), &options)) |
| 774 | .WillOnce(Return(true)); |
| 775 | EXPECT_CALL(manager_, IsOnline()).WillOnce(Return(true)); |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 776 | EXPECT_CALL(*management_server_, ReleaseHold()); |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 777 | Error error; |
| 778 | EXPECT_TRUE(InitManagementChannelOptions(&options, &error)); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 779 | EXPECT_TRUE(error.IsSuccess()); |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 780 | } |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 781 | |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 782 | TEST_F(OpenVPNDriverTest, InitManagementChannelOptionsOffline) { |
| 783 | vector<string> options; |
| 784 | EXPECT_CALL(*management_server_, Start(&dispatcher_, GetSockets(), &options)) |
| 785 | .WillOnce(Return(true)); |
| 786 | EXPECT_CALL(manager_, IsOnline()).WillOnce(Return(false)); |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 787 | EXPECT_CALL(*management_server_, ReleaseHold()).Times(0); |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 788 | Error error; |
| 789 | EXPECT_TRUE(InitManagementChannelOptions(&options, &error)); |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 790 | EXPECT_TRUE(error.IsSuccess()); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 791 | } |
| 792 | |
Darin Petkov | 55771b7 | 2012-04-25 09:25:19 +0200 | [diff] [blame] | 793 | TEST_F(OpenVPNDriverTest, InitLoggingOptions) { |
| 794 | vector<string> options; |
| 795 | bool vpn_logging = SLOG_IS_ON(VPN, 0); |
| 796 | ScopeLogger::GetInstance()->EnableScopesByName("-vpn"); |
| 797 | driver_->InitLoggingOptions(&options); |
| 798 | ASSERT_EQ(1, options.size()); |
| 799 | EXPECT_EQ("--syslog", options[0]); |
| 800 | ScopeLogger::GetInstance()->EnableScopesByName("+vpn"); |
| 801 | options.clear(); |
| 802 | driver_->InitLoggingOptions(&options); |
| 803 | ExpectInFlags(options, "--verb", "3"); |
| 804 | ScopeLogger::GetInstance()->EnableScopesByName("-vpn"); |
| 805 | SetArg("OpenVPN.Verb", "2"); |
| 806 | options.clear(); |
| 807 | driver_->InitLoggingOptions(&options); |
| 808 | ExpectInFlags(options, "--verb", "2"); |
| 809 | ScopeLogger::GetInstance()->EnableScopesByName("+vpn"); |
| 810 | SetArg("OpenVPN.Verb", "1"); |
| 811 | options.clear(); |
| 812 | driver_->InitLoggingOptions(&options); |
| 813 | ExpectInFlags(options, "--verb", "1"); |
| 814 | if (!vpn_logging) { |
| 815 | ScopeLogger::GetInstance()->EnableScopesByName("-vpn"); |
| 816 | } |
| 817 | } |
| 818 | |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 819 | TEST_F(OpenVPNDriverTest, AppendValueOption) { |
| 820 | vector<string> options; |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 821 | EXPECT_FALSE( |
| 822 | driver_->AppendValueOption("OpenVPN.UnknownProperty", kOption, &options)); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 823 | EXPECT_TRUE(options.empty()); |
| 824 | |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 825 | SetArg(kProperty, ""); |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 826 | EXPECT_FALSE(driver_->AppendValueOption(kProperty, kOption, &options)); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 827 | EXPECT_TRUE(options.empty()); |
| 828 | |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 829 | SetArg(kProperty, kValue); |
| 830 | SetArg(kProperty2, kValue2); |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 831 | EXPECT_TRUE(driver_->AppendValueOption(kProperty, kOption, &options)); |
| 832 | EXPECT_TRUE(driver_->AppendValueOption(kProperty2, kOption2, &options)); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 833 | EXPECT_EQ(4, options.size()); |
| 834 | EXPECT_EQ(kOption, options[0]); |
| 835 | EXPECT_EQ(kValue, options[1]); |
| 836 | EXPECT_EQ(kOption2, options[2]); |
| 837 | EXPECT_EQ(kValue2, options[3]); |
| 838 | } |
| 839 | |
| 840 | TEST_F(OpenVPNDriverTest, AppendFlag) { |
| 841 | vector<string> options; |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 842 | EXPECT_FALSE( |
| 843 | driver_->AppendFlag("OpenVPN.UnknownProperty", kOption, &options)); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 844 | EXPECT_TRUE(options.empty()); |
| 845 | |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 846 | SetArg(kProperty, ""); |
| 847 | SetArg(kProperty2, kValue2); |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 848 | EXPECT_TRUE(driver_->AppendFlag(kProperty, kOption, &options)); |
| 849 | EXPECT_TRUE(driver_->AppendFlag(kProperty2, kOption2, &options)); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 850 | EXPECT_EQ(2, options.size()); |
| 851 | EXPECT_EQ(kOption, options[0]); |
| 852 | EXPECT_EQ(kOption2, options[1]); |
| 853 | } |
| 854 | |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 855 | TEST_F(OpenVPNDriverTest, ClaimInterface) { |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 856 | driver_->tunnel_interface_ = kInterfaceName; |
| 857 | EXPECT_FALSE(driver_->ClaimInterface(string(kInterfaceName) + "XXX", |
| 858 | kInterfaceIndex)); |
| 859 | EXPECT_FALSE(driver_->device_); |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 860 | |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 861 | static const char kHost[] = "192.168.2.254"; |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 862 | SetArg(flimflam::kProviderHostProperty, kHost); |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 863 | EXPECT_CALL(*management_server_, Start(_, _, _)).WillOnce(Return(true)); |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 864 | EXPECT_CALL(manager_, IsOnline()).WillOnce(Return(false)); |
Darin Petkov | 68710d7 | 2013-02-13 14:22:56 +0100 | [diff] [blame] | 865 | EXPECT_CALL(glib_, SpawnAsync(_, _, _, _, _, _, _, _)).WillOnce(Return(true)); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 866 | EXPECT_CALL(glib_, ChildWatchAdd(_, _, _)).WillOnce(Return(1)); |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 867 | const int kServiceCallbackTag = 1; |
| 868 | EXPECT_EQ(0, driver_->default_service_callback_tag_); |
| 869 | EXPECT_CALL(manager_, RegisterDefaultServiceCallback(_)) |
| 870 | .WillOnce(Return(kServiceCallbackTag)); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 871 | EXPECT_TRUE(driver_->ClaimInterface(kInterfaceName, kInterfaceIndex)); |
| 872 | ASSERT_TRUE(driver_->device_); |
| 873 | EXPECT_EQ(kInterfaceIndex, driver_->device_->interface_index()); |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 874 | EXPECT_EQ(kServiceCallbackTag, driver_->default_service_callback_tag_); |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 875 | } |
| 876 | |
Darin Petkov | aba8932 | 2013-03-11 14:48:22 +0100 | [diff] [blame] | 877 | TEST_F(OpenVPNDriverTest, IdleService) { |
| 878 | SetService(service_); |
Darin Petkov | aba8932 | 2013-03-11 14:48:22 +0100 | [diff] [blame] | 879 | EXPECT_CALL(*service_, SetState(Service::kStateIdle)); |
| 880 | driver_->IdleService(); |
Darin Petkov | aba8932 | 2013-03-11 14:48:22 +0100 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | TEST_F(OpenVPNDriverTest, FailService) { |
| 884 | static const char kErrorDetails[] = "Bad password."; |
| 885 | SetService(service_); |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 886 | EXPECT_CALL(*service_, SetFailure(Service::kFailureConnect)); |
| 887 | driver_->FailService(Service::kFailureConnect, kErrorDetails); |
Darin Petkov | aba8932 | 2013-03-11 14:48:22 +0100 | [diff] [blame] | 888 | EXPECT_EQ(kErrorDetails, service_->error_details()); |
| 889 | } |
| 890 | |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 891 | TEST_F(OpenVPNDriverTest, Cleanup) { |
Darin Petkov | aba8932 | 2013-03-11 14:48:22 +0100 | [diff] [blame] | 892 | // Ensure no crash. |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 893 | driver_->Cleanup(Service::kStateIdle, |
| 894 | Service::kFailureUnknown, |
| 895 | Service::kErrorDetailsNone); |
Darin Petkov | 1fa8194 | 2012-04-02 11:38:08 +0200 | [diff] [blame] | 896 | |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 897 | const unsigned int kChildTag = 123; |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 898 | const int kPID = 123456; |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 899 | const int kServiceCallbackTag = 5; |
Darin Petkov | aba8932 | 2013-03-11 14:48:22 +0100 | [diff] [blame] | 900 | static const char kErrorDetails[] = "Certificate revoked."; |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 901 | driver_->default_service_callback_tag_ = kServiceCallbackTag; |
| 902 | driver_->child_watch_tag_ = kChildTag; |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 903 | driver_->pid_ = kPID; |
| 904 | driver_->rpc_task_.reset(new RPCTask(&control_, this)); |
| 905 | driver_->tunnel_interface_ = kInterfaceName; |
| 906 | driver_->device_ = device_; |
| 907 | driver_->service_ = service_; |
Darin Petkov | 3189a47 | 2012-10-05 09:55:33 +0200 | [diff] [blame] | 908 | driver_->ip_properties_.address = "1.2.3.4"; |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 909 | StartConnectTimeout(0); |
Darin Petkov | 1fa8194 | 2012-04-02 11:38:08 +0200 | [diff] [blame] | 910 | FilePath tls_auth_file; |
| 911 | EXPECT_TRUE(file_util::CreateTemporaryFile(&tls_auth_file)); |
| 912 | EXPECT_FALSE(tls_auth_file.empty()); |
| 913 | EXPECT_TRUE(file_util::PathExists(tls_auth_file)); |
| 914 | driver_->tls_auth_file_ = tls_auth_file; |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 915 | // Stop will be called twice -- once by Cleanup and once by the destructor. |
| 916 | EXPECT_CALL(*management_server_, Stop()).Times(2); |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 917 | EXPECT_CALL(glib_, SourceRemove(kChildTag)); |
| 918 | EXPECT_CALL(manager_, DeregisterDefaultServiceCallback(kServiceCallbackTag)); |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 919 | EXPECT_CALL(process_killer_, Kill(kPID, _)); |
| 920 | EXPECT_CALL(device_info_, DeleteInterface(_)).Times(0); |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 921 | EXPECT_CALL(*device_, DropConnection()); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 922 | EXPECT_CALL(*device_, SetEnabled(false)); |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 923 | EXPECT_CALL(*service_, SetFailure(Service::kFailureInternal)); |
| 924 | driver_->Cleanup( |
| 925 | Service::kStateFailure, Service::kFailureInternal, kErrorDetails); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 926 | EXPECT_EQ(0, driver_->child_watch_tag_); |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 927 | EXPECT_EQ(0, driver_->default_service_callback_tag_); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 928 | EXPECT_EQ(0, driver_->pid_); |
| 929 | EXPECT_FALSE(driver_->rpc_task_.get()); |
| 930 | EXPECT_TRUE(driver_->tunnel_interface_.empty()); |
| 931 | EXPECT_FALSE(driver_->device_); |
| 932 | EXPECT_FALSE(driver_->service_); |
Darin Petkov | aba8932 | 2013-03-11 14:48:22 +0100 | [diff] [blame] | 933 | EXPECT_EQ(kErrorDetails, service_->error_details()); |
Darin Petkov | 1fa8194 | 2012-04-02 11:38:08 +0200 | [diff] [blame] | 934 | EXPECT_FALSE(file_util::PathExists(tls_auth_file)); |
| 935 | EXPECT_TRUE(driver_->tls_auth_file_.empty()); |
Darin Petkov | 3189a47 | 2012-10-05 09:55:33 +0200 | [diff] [blame] | 936 | EXPECT_TRUE(driver_->ip_properties_.address.empty()); |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 937 | EXPECT_FALSE(driver_->IsConnectTimeoutStarted()); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 938 | } |
| 939 | |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 940 | namespace { |
| 941 | MATCHER(CheckEnv, "") { |
| 942 | if (!arg || !arg[0] || !arg[1] || arg[2]) { |
| 943 | return false; |
| 944 | } |
| 945 | return (string(arg[0]) == "IV_PLAT=Chromium OS" && |
| 946 | string(arg[1]) == "IV_PLAT_REL=2202.0"); |
| 947 | } |
| 948 | } // namespace |
| 949 | |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 950 | TEST_F(OpenVPNDriverTest, SpawnOpenVPN) { |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 951 | SetupLSBRelease(); |
| 952 | |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 953 | EXPECT_FALSE(driver_->SpawnOpenVPN()); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 954 | |
| 955 | static const char kHost[] = "192.168.2.254"; |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 956 | SetArg(flimflam::kProviderHostProperty, kHost); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 957 | driver_->tunnel_interface_ = "tun0"; |
| 958 | driver_->rpc_task_.reset(new RPCTask(&control_, this)); |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 959 | EXPECT_CALL(*management_server_, Start(_, _, _)) |
| 960 | .Times(2) |
| 961 | .WillRepeatedly(Return(true)); |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 962 | EXPECT_CALL(manager_, IsOnline()).Times(2).WillRepeatedly(Return(false)); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 963 | |
| 964 | const int kPID = 234678; |
Darin Petkov | 68710d7 | 2013-02-13 14:22:56 +0100 | [diff] [blame] | 965 | EXPECT_CALL(glib_, SpawnAsync(_, _, CheckEnv(), _, _, _, _, _)) |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 966 | .WillOnce(Return(false)) |
Darin Petkov | 68710d7 | 2013-02-13 14:22:56 +0100 | [diff] [blame] | 967 | .WillOnce(DoAll(SetArgumentPointee<6>(kPID), Return(true))); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 968 | const int kTag = 6; |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 969 | EXPECT_CALL(glib_, ChildWatchAdd(kPID, &driver_->OnOpenVPNDied, driver_)) |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 970 | .WillOnce(Return(kTag)); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 971 | EXPECT_FALSE(driver_->SpawnOpenVPN()); |
| 972 | EXPECT_TRUE(driver_->SpawnOpenVPN()); |
| 973 | EXPECT_EQ(kPID, driver_->pid_); |
| 974 | EXPECT_EQ(kTag, driver_->child_watch_tag_); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | TEST_F(OpenVPNDriverTest, OnOpenVPNDied) { |
| 978 | const int kPID = 99999; |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 979 | driver_->device_ = device_; |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 980 | driver_->child_watch_tag_ = 333; |
| 981 | driver_->pid_ = kPID; |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 982 | EXPECT_CALL(*device_, DropConnection()); |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 983 | EXPECT_CALL(*device_, SetEnabled(false)); |
| 984 | EXPECT_CALL(process_killer_, Kill(_, _)).Times(0); |
| 985 | EXPECT_CALL(device_info_, DeleteInterface(kInterfaceIndex)); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 986 | OpenVPNDriver::OnOpenVPNDied(kPID, 2, driver_); |
| 987 | EXPECT_EQ(0, driver_->child_watch_tag_); |
| 988 | EXPECT_EQ(0, driver_->pid_); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 989 | } |
| 990 | |
Darin Petkov | 6aa2187 | 2012-03-09 16:10:19 +0100 | [diff] [blame] | 991 | TEST_F(OpenVPNDriverTest, Disconnect) { |
| 992 | driver_->device_ = device_; |
| 993 | driver_->service_ = service_; |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 994 | EXPECT_CALL(*device_, DropConnection()); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 995 | EXPECT_CALL(*device_, SetEnabled(false)); |
Darin Petkov | 6aa2187 | 2012-03-09 16:10:19 +0100 | [diff] [blame] | 996 | EXPECT_CALL(device_info_, DeleteInterface(kInterfaceIndex)); |
| 997 | EXPECT_CALL(*service_, SetState(Service::kStateIdle)); |
| 998 | driver_->Disconnect(); |
Darin Petkov | a0e645e | 2012-04-25 11:38:59 +0200 | [diff] [blame] | 999 | EXPECT_FALSE(driver_->device_); |
Darin Petkov | 6aa2187 | 2012-03-09 16:10:19 +0100 | [diff] [blame] | 1000 | EXPECT_FALSE(driver_->service_); |
| 1001 | } |
| 1002 | |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 1003 | TEST_F(OpenVPNDriverTest, OnConnectionDisconnected) { |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 1004 | EXPECT_CALL(*management_server_, Restart()); |
| 1005 | SetDevice(device_); |
| 1006 | SetService(service_); |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 1007 | EXPECT_CALL(*device_, DropConnection()); |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 1008 | EXPECT_CALL(*service_, SetState(Service::kStateAssociating)); |
| 1009 | OnConnectionDisconnected(); |
| 1010 | EXPECT_TRUE(IsConnectTimeoutStarted()); |
| 1011 | } |
| 1012 | |
| 1013 | TEST_F(OpenVPNDriverTest, OnConnectTimeout) { |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 1014 | StartConnectTimeout(0); |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 1015 | SetService(service_); |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 1016 | EXPECT_CALL(*service_, SetFailure(Service::kFailureConnect)); |
| 1017 | OnConnectTimeout(); |
| 1018 | EXPECT_FALSE(GetService()); |
| 1019 | EXPECT_FALSE(IsConnectTimeoutStarted()); |
| 1020 | } |
| 1021 | |
| 1022 | TEST_F(OpenVPNDriverTest, OnConnectTimeoutResolve) { |
| 1023 | StartConnectTimeout(0); |
| 1024 | SetService(service_); |
| 1025 | SetClientState(OpenVPNManagementServer::kStateResolve); |
| 1026 | EXPECT_CALL(*service_, SetFailure(Service::kFailureDNSLookup)); |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 1027 | OnConnectTimeout(); |
| 1028 | EXPECT_FALSE(GetService()); |
| 1029 | EXPECT_FALSE(IsConnectTimeoutStarted()); |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 1030 | } |
| 1031 | |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 1032 | TEST_F(OpenVPNDriverTest, OnReconnectingUnknown) { |
| 1033 | EXPECT_FALSE(IsConnectTimeoutStarted()); |
| 1034 | EXPECT_CALL(dispatcher_, |
| 1035 | PostDelayedTask(_, GetDefaultConnectTimeoutSeconds() * 1000)) |
| 1036 | .WillOnce(Return(true)); |
| 1037 | SetDevice(device_); |
| 1038 | SetService(service_); |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 1039 | EXPECT_CALL(*device_, DropConnection()); |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 1040 | EXPECT_CALL(*service_, SetState(Service::kStateAssociating)); |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 1041 | driver_->OnReconnecting(OpenVPNDriver::kReconnectReasonUnknown); |
| 1042 | EXPECT_TRUE(IsConnectTimeoutStarted()); |
| 1043 | } |
| 1044 | |
| 1045 | TEST_F(OpenVPNDriverTest, OnReconnectingTLSError) { |
| 1046 | EXPECT_CALL(dispatcher_, |
| 1047 | PostDelayedTask(_, GetReconnectOfflineTimeoutSeconds() * 1000)) |
| 1048 | .WillOnce(Return(true)); |
| 1049 | EXPECT_CALL(dispatcher_, |
| 1050 | PostDelayedTask(_, GetReconnectTLSErrorTimeoutSeconds() * 1000)) |
| 1051 | .WillOnce(Return(true)); |
| 1052 | |
| 1053 | driver_->OnReconnecting(OpenVPNDriver::kReconnectReasonOffline); |
| 1054 | EXPECT_TRUE(IsConnectTimeoutStarted()); |
| 1055 | |
| 1056 | // The scheduled timeout should not be affected for unknown reason. |
| 1057 | driver_->OnReconnecting(OpenVPNDriver::kReconnectReasonUnknown); |
| 1058 | EXPECT_TRUE(IsConnectTimeoutStarted()); |
| 1059 | |
| 1060 | // Reconnect on TLS error reschedules the timeout once. |
| 1061 | driver_->OnReconnecting(OpenVPNDriver::kReconnectReasonTLSError); |
| 1062 | EXPECT_TRUE(IsConnectTimeoutStarted()); |
| 1063 | driver_->OnReconnecting(OpenVPNDriver::kReconnectReasonTLSError); |
| 1064 | EXPECT_TRUE(IsConnectTimeoutStarted()); |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 1065 | } |
| 1066 | |
Paul Stewart | 291a473 | 2012-03-14 19:19:02 -0700 | [diff] [blame] | 1067 | TEST_F(OpenVPNDriverTest, VerifyPaths) { |
| 1068 | // Ensure that the various path constants that the OpenVPN driver uses |
Darin Petkov | a0e645e | 2012-04-25 11:38:59 +0200 | [diff] [blame] | 1069 | // actually exists in the build image. Due to build dependencies, they should |
| 1070 | // already exist by the time we run unit tests. |
Paul Stewart | 291a473 | 2012-03-14 19:19:02 -0700 | [diff] [blame] | 1071 | |
Darin Petkov | a0e645e | 2012-04-25 11:38:59 +0200 | [diff] [blame] | 1072 | // The OpenVPNDriver path constants are absolute. FilePath::Append asserts |
| 1073 | // that its argument is not an absolute path, so we need to strip the leading |
| 1074 | // separators. There's nothing built into FilePath to do so. |
| 1075 | static const char *kPaths[] = { |
| 1076 | OpenVPNDriver::kOpenVPNPath, |
| 1077 | OpenVPNDriver::kOpenVPNScript, |
| 1078 | }; |
| 1079 | for (size_t i = 0; i < arraysize(kPaths); i++) { |
| 1080 | string path(kPaths[i]); |
| 1081 | TrimString(path, FilePath::kSeparators, &path); |
| 1082 | EXPECT_TRUE(file_util::PathExists(FilePath(SYSROOT).Append(path))) |
| 1083 | << kPaths[i]; |
| 1084 | } |
Paul Stewart | 291a473 | 2012-03-14 19:19:02 -0700 | [diff] [blame] | 1085 | } |
| 1086 | |
Darin Petkov | d432539 | 2012-04-23 15:48:22 +0200 | [diff] [blame] | 1087 | TEST_F(OpenVPNDriverTest, InitPropertyStore) { |
| 1088 | // Sanity test property store initialization. |
| 1089 | PropertyStore store; |
| 1090 | driver_->InitPropertyStore(&store); |
| 1091 | const string kUser = "joe"; |
| 1092 | Error error; |
| 1093 | EXPECT_TRUE( |
| 1094 | store.SetStringProperty(flimflam::kOpenVPNUserProperty, kUser, &error)); |
| 1095 | EXPECT_TRUE(error.IsSuccess()); |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 1096 | EXPECT_EQ(kUser, GetArgs()->LookupString(flimflam::kOpenVPNUserProperty, "")); |
| 1097 | } |
| 1098 | |
| 1099 | TEST_F(OpenVPNDriverTest, GetProvider) { |
| 1100 | PropertyStore store; |
| 1101 | driver_->InitPropertyStore(&store); |
| 1102 | { |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 1103 | KeyValueStore props; |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 1104 | Error error; |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 1105 | EXPECT_TRUE( |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 1106 | store.GetKeyValueStoreProperty( |
| 1107 | flimflam::kProviderProperty, &props, &error)); |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 1108 | EXPECT_TRUE(props.LookupBool(flimflam::kPassphraseRequiredProperty, false)); |
| 1109 | } |
| 1110 | { |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 1111 | KeyValueStore props; |
| 1112 | SetArg(flimflam::kOpenVPNPasswordProperty, "random-password"); |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 1113 | Error error; |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 1114 | EXPECT_TRUE( |
Paul Stewart | e6e8e49 | 2013-01-17 11:00:50 -0800 | [diff] [blame] | 1115 | store.GetKeyValueStoreProperty( |
| 1116 | flimflam::kProviderProperty, &props, &error)); |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 1117 | EXPECT_FALSE(props.LookupBool(flimflam::kPassphraseRequiredProperty, true)); |
Darin Petkov | 0223655 | 2012-06-11 13:15:19 +0200 | [diff] [blame] | 1118 | EXPECT_FALSE(props.ContainsString(flimflam::kOpenVPNPasswordProperty)); |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 1119 | } |
Darin Petkov | d432539 | 2012-04-23 15:48:22 +0200 | [diff] [blame] | 1120 | } |
| 1121 | |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 1122 | TEST_F(OpenVPNDriverTest, ParseLSBRelease) { |
| 1123 | SetupLSBRelease(); |
| 1124 | map<string, string> lsb_release; |
| 1125 | EXPECT_TRUE(driver_->ParseLSBRelease(&lsb_release)); |
| 1126 | EXPECT_TRUE(ContainsKey(lsb_release, "foo") && lsb_release["foo"] == ""); |
| 1127 | EXPECT_EQ("=", lsb_release["zoo"]); |
| 1128 | EXPECT_EQ("Chromium OS", lsb_release[OpenVPNDriver::kChromeOSReleaseName]); |
| 1129 | EXPECT_EQ("2202.0", lsb_release[OpenVPNDriver::kChromeOSReleaseVersion]); |
| 1130 | driver_->lsb_release_file_ = FilePath("/non/existent/file"); |
| 1131 | EXPECT_FALSE(driver_->ParseLSBRelease(NULL)); |
| 1132 | } |
| 1133 | |
| 1134 | TEST_F(OpenVPNDriverTest, InitEnvironment) { |
| 1135 | vector<string> env; |
| 1136 | SetupLSBRelease(); |
| 1137 | driver_->InitEnvironment(&env); |
| 1138 | ASSERT_EQ(2, env.size()); |
| 1139 | EXPECT_EQ("IV_PLAT=Chromium OS", env[0]); |
| 1140 | EXPECT_EQ("IV_PLAT_REL=2202.0", env[1]); |
| 1141 | env.clear(); |
| 1142 | EXPECT_EQ(0, file_util::WriteFile(lsb_release_file_, "", 0)); |
| 1143 | driver_->InitEnvironment(&env); |
| 1144 | EXPECT_EQ(0, env.size()); |
| 1145 | } |
| 1146 | |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 1147 | TEST_F(OpenVPNDriverTest, DeleteInterface) { |
| 1148 | scoped_ptr<MockDeviceInfo> device_info( |
| 1149 | new MockDeviceInfo(&control_, &dispatcher_, &metrics_, &manager_)); |
| 1150 | EXPECT_CALL(*device_info, DeleteInterface(kInterfaceIndex)) |
| 1151 | .WillOnce(Return(true)); |
| 1152 | WeakPtr<DeviceInfo> weak = device_info->AsWeakPtr(); |
| 1153 | EXPECT_TRUE(weak); |
| 1154 | OpenVPNDriver::DeleteInterface(weak, kInterfaceIndex); |
| 1155 | device_info.reset(); |
| 1156 | EXPECT_FALSE(weak); |
| 1157 | // Expect no crash. |
| 1158 | OpenVPNDriver::DeleteInterface(weak, kInterfaceIndex); |
| 1159 | } |
| 1160 | |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 1161 | TEST_F(OpenVPNDriverTest, OnDefaultServiceChanged) { |
| 1162 | driver_->service_ = service_; |
| 1163 | |
| 1164 | ServiceRefPtr null_service; |
| 1165 | EXPECT_CALL(*management_server_, Hold()); |
| 1166 | driver_->OnDefaultServiceChanged(null_service); |
| 1167 | |
| 1168 | EXPECT_CALL(*management_server_, Hold()); |
| 1169 | driver_->OnDefaultServiceChanged(service_); |
| 1170 | |
| 1171 | scoped_refptr<MockService> mock_service( |
| 1172 | new MockService(&control_, &dispatcher_, &metrics_, &manager_)); |
| 1173 | |
| 1174 | EXPECT_CALL(*mock_service, IsConnected()).WillOnce(Return(false)); |
| 1175 | EXPECT_CALL(*management_server_, Hold()); |
| 1176 | driver_->OnDefaultServiceChanged(mock_service); |
| 1177 | |
| 1178 | EXPECT_CALL(*mock_service, IsConnected()).WillOnce(Return(true)); |
| 1179 | EXPECT_CALL(*management_server_, ReleaseHold()); |
| 1180 | driver_->OnDefaultServiceChanged(mock_service); |
| 1181 | } |
| 1182 | |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 1183 | TEST_F(OpenVPNDriverTest, GetReconnectTimeoutSeconds) { |
| 1184 | EXPECT_EQ(GetDefaultConnectTimeoutSeconds(), |
| 1185 | GetReconnectTimeoutSeconds(OpenVPNDriver::kReconnectReasonUnknown)); |
| 1186 | EXPECT_EQ(GetReconnectOfflineTimeoutSeconds(), |
| 1187 | GetReconnectTimeoutSeconds(OpenVPNDriver::kReconnectReasonOffline)); |
| 1188 | EXPECT_EQ(GetReconnectTLSErrorTimeoutSeconds(), |
| 1189 | GetReconnectTimeoutSeconds( |
| 1190 | OpenVPNDriver::kReconnectReasonTLSError)); |
| 1191 | } |
| 1192 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 1193 | } // namespace shill |