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" |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 18 | #include "shill/mock_adaptors.h" |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 19 | #include "shill/mock_device_info.h" |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 20 | #include "shill/mock_glib.h" |
| 21 | #include "shill/mock_manager.h" |
| 22 | #include "shill/mock_metrics.h" |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 23 | #include "shill/mock_nss.h" |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 24 | #include "shill/mock_openvpn_management_server.h" |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 25 | #include "shill/mock_store.h" |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 26 | #include "shill/mock_vpn.h" |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 27 | #include "shill/mock_vpn_service.h" |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 28 | #include "shill/nice_mock_control.h" |
Paul Stewart | 8e7e459 | 2012-04-29 09:47:48 -0700 | [diff] [blame] | 29 | #include "shill/property_store_inspector.h" |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 30 | #include "shill/rpc_task.h" |
Darin Petkov | 55771b7 | 2012-04-25 09:25:19 +0200 | [diff] [blame] | 31 | #include "shill/scope_logger.h" |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 32 | #include "shill/vpn.h" |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 33 | #include "shill/vpn_service.h" |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 34 | |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 35 | using std::map; |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 36 | using std::string; |
| 37 | using std::vector; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 38 | using testing::_; |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 39 | using testing::AnyNumber; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 40 | using testing::DoAll; |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 41 | using testing::ElementsAreArray; |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 42 | using testing::Ne; |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 43 | using testing::NiceMock; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 44 | using testing::Return; |
| 45 | using testing::SetArgumentPointee; |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 46 | using testing::StrictMock; |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 47 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 48 | namespace shill { |
| 49 | |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 50 | class OpenVPNDriverTest : public testing::Test, |
| 51 | public RPCTaskDelegate { |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 52 | public: |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 53 | OpenVPNDriverTest() |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 54 | : device_info_(&control_, &dispatcher_, &metrics_, &manager_), |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 55 | manager_(&control_, &dispatcher_, &metrics_, &glib_), |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 56 | driver_(new OpenVPNDriver(&control_, &dispatcher_, &metrics_, &manager_, |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 57 | &device_info_, &glib_)), |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 58 | service_(new MockVPNService(&control_, &dispatcher_, &metrics_, |
| 59 | &manager_, driver_)), |
| 60 | device_(new MockVPN(&control_, &dispatcher_, &metrics_, &manager_, |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 61 | kInterfaceName, kInterfaceIndex)), |
| 62 | management_server_(new NiceMock<MockOpenVPNManagementServer>()) { |
| 63 | driver_->management_server_.reset(management_server_); |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 64 | driver_->nss_ = &nss_; |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 65 | } |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 66 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 67 | virtual ~OpenVPNDriverTest() {} |
| 68 | |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 69 | virtual void TearDown() { |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 70 | driver_->child_watch_tag_ = 0; |
| 71 | driver_->pid_ = 0; |
| 72 | driver_->device_ = NULL; |
| 73 | driver_->service_ = NULL; |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 74 | if (!lsb_release_file_.empty()) { |
| 75 | EXPECT_TRUE(file_util::Delete(lsb_release_file_, false)); |
| 76 | lsb_release_file_.clear(); |
| 77 | } |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 78 | } |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 79 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 80 | protected: |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 81 | static const char kOption[]; |
| 82 | static const char kProperty[]; |
| 83 | static const char kValue[]; |
| 84 | static const char kOption2[]; |
| 85 | static const char kProperty2[]; |
| 86 | static const char kValue2[]; |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 87 | static const char kGateway1[]; |
| 88 | static const char kNetmask1[]; |
| 89 | static const char kNetwork1[]; |
| 90 | static const char kGateway2[]; |
| 91 | static const char kNetmask2[]; |
| 92 | static const char kNetwork2[]; |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 93 | static const char kInterfaceName[]; |
| 94 | static const int kInterfaceIndex; |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 95 | |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 96 | void SetArg(const string &arg, const string &value) { |
| 97 | driver_->args()->SetString(arg, value); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 100 | KeyValueStore *GetArgs() { |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 101 | return driver_->args(); |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 104 | // Used to assert that a flag appears in the options. |
| 105 | void ExpectInFlags(const vector<string> &options, const string &flag, |
| 106 | const string &value); |
| 107 | |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 108 | void SetupLSBRelease(); |
| 109 | |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 110 | // Inherited from RPCTaskDelegate. |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 111 | virtual void GetLogin(string *user, string *password); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 112 | virtual void Notify(const string &reason, const map<string, string> &dict); |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 113 | |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 114 | NiceMockControl control_; |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 115 | NiceMock<MockDeviceInfo> device_info_; |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 116 | EventDispatcher dispatcher_; |
| 117 | MockMetrics metrics_; |
| 118 | MockGLib glib_; |
| 119 | MockManager manager_; |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 120 | OpenVPNDriver *driver_; // Owned by |service_|. |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 121 | scoped_refptr<MockVPNService> service_; |
| 122 | scoped_refptr<MockVPN> device_; |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 123 | MockNSS nss_; |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 124 | |
| 125 | // Owned by |driver_|. |
| 126 | NiceMock<MockOpenVPNManagementServer> *management_server_; |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 127 | |
| 128 | FilePath lsb_release_file_; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 129 | }; |
| 130 | |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 131 | const char OpenVPNDriverTest::kOption[] = "--openvpn-option"; |
| 132 | const char OpenVPNDriverTest::kProperty[] = "OpenVPN.SomeProperty"; |
| 133 | const char OpenVPNDriverTest::kValue[] = "some-property-value"; |
| 134 | const char OpenVPNDriverTest::kOption2[] = "--openvpn-option2"; |
| 135 | const char OpenVPNDriverTest::kProperty2[] = "OpenVPN.SomeProperty2"; |
| 136 | const char OpenVPNDriverTest::kValue2[] = "some-property-value2"; |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 137 | const char OpenVPNDriverTest::kGateway1[] = "10.242.2.13"; |
| 138 | const char OpenVPNDriverTest::kNetmask1[] = "255.255.255.255"; |
| 139 | const char OpenVPNDriverTest::kNetwork1[] = "10.242.2.1"; |
| 140 | const char OpenVPNDriverTest::kGateway2[] = "10.242.2.14"; |
| 141 | const char OpenVPNDriverTest::kNetmask2[] = "255.255.0.0"; |
| 142 | const char OpenVPNDriverTest::kNetwork2[] = "192.168.0.0"; |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 143 | const char OpenVPNDriverTest::kInterfaceName[] = "tun0"; |
| 144 | const int OpenVPNDriverTest::kInterfaceIndex = 123; |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 145 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 146 | void OpenVPNDriverTest::GetLogin(string */*user*/, string */*password*/) {} |
| 147 | |
Darin Petkov | a9b1fed | 2012-02-29 11:49:05 +0100 | [diff] [blame] | 148 | void OpenVPNDriverTest::Notify(const string &/*reason*/, |
| 149 | const map<string, string> &/*dict*/) {} |
| 150 | |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 151 | void OpenVPNDriverTest::ExpectInFlags(const vector<string> &options, |
| 152 | const string &flag, |
| 153 | const string &value) { |
| 154 | vector<string>::const_iterator it = |
| 155 | std::find(options.begin(), options.end(), flag); |
| 156 | |
| 157 | EXPECT_TRUE(it != options.end()); |
| 158 | if (it != options.end()) |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 159 | return; // Don't crash below. |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 160 | it++; |
| 161 | EXPECT_TRUE(it != options.end()); |
| 162 | if (it != options.end()) |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 163 | return; // Don't crash below. |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 164 | EXPECT_EQ(value, *it); |
| 165 | } |
| 166 | |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 167 | void OpenVPNDriverTest::SetupLSBRelease() { |
| 168 | static const char kLSBReleaseContents[] = |
| 169 | "\n" |
| 170 | "=\n" |
| 171 | "foo=\n" |
| 172 | "=bar\n" |
| 173 | "zoo==\n" |
| 174 | "CHROMEOS_RELEASE_BOARD=x86-alex\n" |
| 175 | "CHROMEOS_RELEASE_NAME=Chromium OS\n" |
| 176 | "CHROMEOS_RELEASE_VERSION=2202.0\n"; |
| 177 | EXPECT_TRUE(file_util::CreateTemporaryFile(&lsb_release_file_)); |
| 178 | EXPECT_EQ(arraysize(kLSBReleaseContents), |
| 179 | file_util::WriteFile(lsb_release_file_, |
| 180 | kLSBReleaseContents, |
| 181 | arraysize(kLSBReleaseContents))); |
| 182 | EXPECT_EQ(OpenVPNDriver::kLSBReleaseFile, driver_->lsb_release_file_.value()); |
| 183 | driver_->lsb_release_file_ = lsb_release_file_; |
| 184 | } |
| 185 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 186 | TEST_F(OpenVPNDriverTest, Connect) { |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 187 | EXPECT_CALL(*service_, SetState(Service::kStateConfiguring)); |
| 188 | const string interface = kInterfaceName; |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 189 | EXPECT_CALL(device_info_, CreateTunnelInterface(_)) |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 190 | .WillOnce(DoAll(SetArgumentPointee<0>(interface), Return(true))); |
| 191 | Error error; |
| 192 | driver_->Connect(service_, &error); |
| 193 | EXPECT_TRUE(error.IsSuccess()); |
| 194 | EXPECT_EQ(kInterfaceName, driver_->tunnel_interface_); |
| 195 | } |
| 196 | |
| 197 | TEST_F(OpenVPNDriverTest, ConnectTunnelFailure) { |
| 198 | EXPECT_CALL(*service_, SetState(Service::kStateConfiguring)); |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 199 | EXPECT_CALL(device_info_, CreateTunnelInterface(_)).WillOnce(Return(false)); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 200 | EXPECT_CALL(*service_, SetState(Service::kStateFailure)); |
| 201 | Error error; |
| 202 | driver_->Connect(service_, &error); |
| 203 | EXPECT_EQ(Error::kInternalError, error.type()); |
| 204 | EXPECT_TRUE(driver_->tunnel_interface_.empty()); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 205 | } |
| 206 | |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 207 | namespace { |
| 208 | MATCHER_P(IsIPAddress, address, "") { |
| 209 | IPAddress ip_address(IPAddress::kFamilyIPv4); |
| 210 | EXPECT_TRUE(ip_address.SetAddressFromString(address)); |
| 211 | return ip_address.Equals(arg); |
| 212 | } |
| 213 | } // namespace |
| 214 | |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 215 | TEST_F(OpenVPNDriverTest, Notify) { |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 216 | map<string, string> config; |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 217 | driver_->device_ = device_; |
| 218 | EXPECT_CALL(*device_, UpdateIPConfig(_)); |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 219 | driver_->Notify("up", config); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | TEST_F(OpenVPNDriverTest, NotifyFail) { |
| 223 | map<string, string> dict; |
| 224 | driver_->device_ = device_; |
| 225 | EXPECT_CALL(*device_, OnDisconnected()); |
| 226 | driver_->Notify("fail", dict); |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 227 | } |
| 228 | |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 229 | TEST_F(OpenVPNDriverTest, GetRouteOptionEntry) { |
| 230 | OpenVPNDriver::RouteOptions routes; |
| 231 | EXPECT_EQ(NULL, OpenVPNDriver::GetRouteOptionEntry("foo", "bar", &routes)); |
| 232 | EXPECT_TRUE(routes.empty()); |
| 233 | EXPECT_EQ(NULL, OpenVPNDriver::GetRouteOptionEntry("foo", "foo", &routes)); |
| 234 | EXPECT_TRUE(routes.empty()); |
| 235 | EXPECT_EQ(NULL, OpenVPNDriver::GetRouteOptionEntry("foo", "fooZ", &routes)); |
| 236 | EXPECT_TRUE(routes.empty()); |
| 237 | IPConfig::Route *route = |
| 238 | OpenVPNDriver::GetRouteOptionEntry("foo", "foo12", &routes); |
| 239 | EXPECT_EQ(1, routes.size()); |
| 240 | EXPECT_EQ(route, &routes[12]); |
| 241 | route = OpenVPNDriver::GetRouteOptionEntry("foo", "foo13", &routes); |
| 242 | EXPECT_EQ(2, routes.size()); |
| 243 | EXPECT_EQ(route, &routes[13]); |
| 244 | } |
| 245 | |
| 246 | TEST_F(OpenVPNDriverTest, ParseRouteOption) { |
| 247 | OpenVPNDriver::RouteOptions routes; |
| 248 | OpenVPNDriver::ParseRouteOption("foo", "bar", &routes); |
| 249 | EXPECT_TRUE(routes.empty()); |
| 250 | OpenVPNDriver::ParseRouteOption("gateway_2", kGateway2, &routes); |
| 251 | OpenVPNDriver::ParseRouteOption("netmask_2", kNetmask2, &routes); |
| 252 | OpenVPNDriver::ParseRouteOption("network_2", kNetwork2, &routes); |
| 253 | EXPECT_EQ(1, routes.size()); |
| 254 | OpenVPNDriver::ParseRouteOption("gateway_1", kGateway1, &routes); |
| 255 | OpenVPNDriver::ParseRouteOption("netmask_1", kNetmask1, &routes); |
| 256 | OpenVPNDriver::ParseRouteOption("network_1", kNetwork1, &routes); |
| 257 | EXPECT_EQ(2, routes.size()); |
| 258 | EXPECT_EQ(kGateway1, routes[1].gateway); |
| 259 | EXPECT_EQ(kNetmask1, routes[1].netmask); |
| 260 | EXPECT_EQ(kNetwork1, routes[1].host); |
| 261 | EXPECT_EQ(kGateway2, routes[2].gateway); |
| 262 | EXPECT_EQ(kNetmask2, routes[2].netmask); |
| 263 | EXPECT_EQ(kNetwork2, routes[2].host); |
| 264 | } |
| 265 | |
| 266 | TEST_F(OpenVPNDriverTest, SetRoutes) { |
| 267 | OpenVPNDriver::RouteOptions routes; |
| 268 | routes[1].gateway = "1.2.3.4"; |
| 269 | routes[1].host= "1.2.3.4"; |
| 270 | routes[2].host = "2.3.4.5"; |
| 271 | routes[2].netmask = "255.0.0.0"; |
| 272 | routes[3].netmask = "255.0.0.0"; |
| 273 | routes[3].gateway = "1.2.3.5"; |
| 274 | routes[5].host = kNetwork2; |
| 275 | routes[5].netmask = kNetmask2; |
| 276 | routes[5].gateway = kGateway2; |
| 277 | routes[4].host = kNetwork1; |
| 278 | routes[4].netmask = kNetmask1; |
| 279 | routes[4].gateway = kGateway1; |
| 280 | IPConfig::Properties props; |
| 281 | OpenVPNDriver::SetRoutes(routes, &props); |
| 282 | ASSERT_EQ(2, props.routes.size()); |
| 283 | EXPECT_EQ(kGateway1, props.routes[0].gateway); |
| 284 | EXPECT_EQ(kNetmask1, props.routes[0].netmask); |
| 285 | EXPECT_EQ(kNetwork1, props.routes[0].host); |
| 286 | EXPECT_EQ(kGateway2, props.routes[1].gateway); |
| 287 | EXPECT_EQ(kNetmask2, props.routes[1].netmask); |
| 288 | EXPECT_EQ(kNetwork2, props.routes[1].host); |
| 289 | } |
| 290 | |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 291 | TEST_F(OpenVPNDriverTest, ParseForeignOption) { |
| 292 | IPConfig::Properties props; |
| 293 | OpenVPNDriver::ParseForeignOption("", &props); |
| 294 | OpenVPNDriver::ParseForeignOption("dhcp-option DOMAIN", &props); |
| 295 | OpenVPNDriver::ParseForeignOption("dhcp-option DOMAIN zzz.com foo", &props); |
| 296 | OpenVPNDriver::ParseForeignOption("dhcp-Option DOmAIN xyz.com", &props); |
| 297 | ASSERT_EQ(1, props.domain_search.size()); |
| 298 | EXPECT_EQ("xyz.com", props.domain_search[0]); |
| 299 | OpenVPNDriver::ParseForeignOption("dhcp-option DnS 1.2.3.4", &props); |
| 300 | ASSERT_EQ(1, props.dns_servers.size()); |
| 301 | EXPECT_EQ("1.2.3.4", props.dns_servers[0]); |
| 302 | } |
| 303 | |
| 304 | TEST_F(OpenVPNDriverTest, ParseForeignOptions) { |
| 305 | // Basically test that std::map is a sorted container. |
| 306 | map<int, string> options; |
| 307 | options[5] = "dhcp-option DOMAIN five.com"; |
| 308 | options[2] = "dhcp-option DOMAIN two.com"; |
| 309 | options[8] = "dhcp-option DOMAIN eight.com"; |
| 310 | options[7] = "dhcp-option DOMAIN seven.com"; |
| 311 | options[4] = "dhcp-option DOMAIN four.com"; |
| 312 | IPConfig::Properties props; |
| 313 | OpenVPNDriver::ParseForeignOptions(options, &props); |
| 314 | ASSERT_EQ(5, props.domain_search.size()); |
| 315 | EXPECT_EQ("two.com", props.domain_search[0]); |
| 316 | EXPECT_EQ("four.com", props.domain_search[1]); |
| 317 | EXPECT_EQ("five.com", props.domain_search[2]); |
| 318 | EXPECT_EQ("seven.com", props.domain_search[3]); |
| 319 | EXPECT_EQ("eight.com", props.domain_search[4]); |
| 320 | } |
| 321 | |
| 322 | TEST_F(OpenVPNDriverTest, ParseIPConfiguration) { |
| 323 | map<string, string> config; |
| 324 | config["ifconfig_loCal"] = "4.5.6.7"; |
| 325 | config["ifconfiG_broadcast"] = "1.2.255.255"; |
| 326 | config["ifconFig_netmAsk"] = "255.255.255.0"; |
| 327 | config["ifconfig_remotE"] = "33.44.55.66"; |
| 328 | config["route_vpN_gateway"] = "192.168.1.1"; |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 329 | config["trusted_ip"] = "99.88.77.66"; |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 330 | config["tun_mtu"] = "1000"; |
| 331 | config["foreign_option_2"] = "dhcp-option DNS 4.4.4.4"; |
| 332 | config["foreign_option_1"] = "dhcp-option DNS 1.1.1.1"; |
| 333 | config["foreign_option_3"] = "dhcp-option DNS 2.2.2.2"; |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 334 | config["route_network_2"] = kNetwork2; |
| 335 | config["route_network_1"] = kNetwork1; |
| 336 | config["route_netmask_2"] = kNetmask2; |
| 337 | config["route_netmask_1"] = kNetmask1; |
| 338 | config["route_gateway_2"] = kGateway2; |
| 339 | config["route_gateway_1"] = kGateway1; |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 340 | config["foo"] = "bar"; |
| 341 | IPConfig::Properties props; |
| 342 | OpenVPNDriver::ParseIPConfiguration(config, &props); |
| 343 | EXPECT_EQ(IPAddress::kFamilyIPv4, props.address_family); |
| 344 | EXPECT_EQ("4.5.6.7", props.address); |
| 345 | EXPECT_EQ("1.2.255.255", props.broadcast_address); |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 346 | EXPECT_EQ(24, props.subnet_prefix); |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 347 | EXPECT_EQ("33.44.55.66", props.peer_address); |
| 348 | EXPECT_EQ("192.168.1.1", props.gateway); |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 349 | EXPECT_EQ("99.88.77.66", props.trusted_ip); |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 350 | EXPECT_EQ(1000, props.mtu); |
| 351 | ASSERT_EQ(3, props.dns_servers.size()); |
| 352 | EXPECT_EQ("1.1.1.1", props.dns_servers[0]); |
| 353 | EXPECT_EQ("4.4.4.4", props.dns_servers[1]); |
| 354 | EXPECT_EQ("2.2.2.2", props.dns_servers[2]); |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 355 | ASSERT_EQ(2, props.routes.size()); |
| 356 | EXPECT_EQ(kGateway1, props.routes[0].gateway); |
| 357 | EXPECT_EQ(kNetmask1, props.routes[0].netmask); |
| 358 | EXPECT_EQ(kNetwork1, props.routes[0].host); |
| 359 | EXPECT_EQ(kGateway2, props.routes[1].gateway); |
| 360 | EXPECT_EQ(kNetmask2, props.routes[1].netmask); |
| 361 | EXPECT_EQ(kNetwork2, props.routes[1].host); |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 362 | } |
| 363 | |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 364 | TEST_F(OpenVPNDriverTest, InitOptionsNoHost) { |
| 365 | Error error; |
| 366 | vector<string> options; |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 367 | driver_->InitOptions(&options, &error); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 368 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 369 | EXPECT_TRUE(options.empty()); |
| 370 | } |
| 371 | |
| 372 | TEST_F(OpenVPNDriverTest, InitOptions) { |
| 373 | static const char kHost[] = "192.168.2.254"; |
Darin Petkov | 1fa8194 | 2012-04-02 11:38:08 +0200 | [diff] [blame] | 374 | static const char kTLSAuthContents[] = "SOME-RANDOM-CONTENTS\n"; |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 375 | static const char kCaCertNSS[] = "{1234}"; |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 376 | static const char kID[] = "TestPKCS11ID"; |
| 377 | FilePath empty_cert; |
| 378 | SetArg(flimflam::kProviderHostProperty, kHost); |
| 379 | SetArg(flimflam::kOpenVPNTLSAuthContentsProperty, kTLSAuthContents); |
| 380 | SetArg(flimflam::kOpenVPNCaCertNSSProperty, kCaCertNSS); |
| 381 | SetArg(flimflam::kOpenVPNClientCertIdProperty, kID); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 382 | driver_->rpc_task_.reset(new RPCTask(&control_, this)); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 383 | driver_->tunnel_interface_ = kInterfaceName; |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 384 | EXPECT_CALL(*management_server_, Start(_, _, _)).WillOnce(Return(true)); |
| 385 | EXPECT_CALL(nss_, GetPEMCertfile(kCaCertNSS, _)).WillOnce(Return(empty_cert)); |
| 386 | |
| 387 | Error error; |
| 388 | vector<string> options; |
| 389 | driver_->InitOptions(&options, &error); |
| 390 | EXPECT_TRUE(error.IsSuccess()); |
| 391 | EXPECT_EQ("--client", options[0]); |
| 392 | ExpectInFlags(options, "--remote", kHost); |
| 393 | ExpectInFlags(options, "CONNMAN_PATH", RPCTaskMockAdaptor::kRpcId); |
| 394 | ExpectInFlags(options, "--dev", kInterfaceName); |
| 395 | ExpectInFlags(options, "--group", "openvpn"); |
| 396 | EXPECT_EQ(kInterfaceName, driver_->tunnel_interface_); |
| 397 | ASSERT_FALSE(driver_->tls_auth_file_.empty()); |
| 398 | ExpectInFlags(options, "--tls-auth", driver_->tls_auth_file_.value()); |
| 399 | string contents; |
| 400 | EXPECT_TRUE( |
| 401 | file_util::ReadFileToString(driver_->tls_auth_file_, &contents)); |
| 402 | EXPECT_EQ(kTLSAuthContents, contents); |
| 403 | ExpectInFlags(options, "--pkcs11-id", kID); |
Darin Petkov | 55771b7 | 2012-04-25 09:25:19 +0200 | [diff] [blame] | 404 | EXPECT_TRUE(std::find(options.begin(), options.end(), "--syslog") != |
| 405 | options.end()); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | TEST_F(OpenVPNDriverTest, InitNSSOptions) { |
| 409 | static const char kHost[] = "192.168.2.254"; |
| 410 | static const char kCaCertNSS[] = "{1234}"; |
| 411 | static const char kNSSCertfile[] = "/tmp/nss-cert"; |
| 412 | FilePath empty_cert; |
| 413 | FilePath nss_cert(kNSSCertfile); |
| 414 | SetArg(flimflam::kProviderHostProperty, kHost); |
| 415 | SetArg(flimflam::kOpenVPNCaCertNSSProperty, kCaCertNSS); |
Darin Petkov | 3c5e4dc | 2012-04-02 14:44:27 +0200 | [diff] [blame] | 416 | EXPECT_CALL(nss_, |
| 417 | GetPEMCertfile(kCaCertNSS, |
| 418 | ElementsAreArray(kHost, arraysize(kHost) - 1))) |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 419 | .WillOnce(Return(empty_cert)) |
| 420 | .WillOnce(Return(nss_cert)); |
| 421 | |
| 422 | Error error; |
| 423 | vector<string> options; |
| 424 | EXPECT_TRUE(driver_->InitNSSOptions(&options, &error)); |
| 425 | EXPECT_TRUE(error.IsSuccess()); |
| 426 | EXPECT_TRUE(options.empty()); |
| 427 | EXPECT_TRUE(driver_->InitNSSOptions(&options, &error)); |
| 428 | ExpectInFlags(options, "--ca", kNSSCertfile); |
| 429 | EXPECT_TRUE(error.IsSuccess()); |
| 430 | |
| 431 | SetArg(flimflam::kOpenVPNCaCertProperty, "foo"); |
| 432 | options.clear(); |
| 433 | EXPECT_FALSE(driver_->InitNSSOptions(&options, &error)); |
| 434 | EXPECT_EQ(Error::kInvalidArguments, error.type()); |
| 435 | EXPECT_EQ("Can't specify both CACert and CACertNSS.", error.message()); |
| 436 | } |
| 437 | |
| 438 | TEST_F(OpenVPNDriverTest, InitPKCS11Options) { |
| 439 | vector<string> options; |
| 440 | driver_->InitPKCS11Options(&options); |
| 441 | EXPECT_TRUE(options.empty()); |
| 442 | |
| 443 | static const char kID[] = "TestPKCS11ID"; |
| 444 | SetArg(flimflam::kOpenVPNClientCertIdProperty, kID); |
| 445 | driver_->InitPKCS11Options(&options); |
| 446 | ExpectInFlags(options, "--pkcs11-id", kID); |
| 447 | ExpectInFlags(options, "--pkcs11-providers", "libchaps.so"); |
| 448 | |
| 449 | static const char kProvider[] = "libpkcs11.so"; |
| 450 | SetArg(flimflam::kOpenVPNProviderProperty, kProvider); |
| 451 | options.clear(); |
| 452 | driver_->InitPKCS11Options(&options); |
| 453 | ExpectInFlags(options, "--pkcs11-id", kID); |
| 454 | ExpectInFlags(options, "--pkcs11-providers", kProvider); |
| 455 | } |
| 456 | |
| 457 | TEST_F(OpenVPNDriverTest, InitManagementChannelOptions) { |
| 458 | vector<string> options; |
| 459 | EXPECT_CALL(*management_server_, |
| 460 | Start(&dispatcher_, &driver_->sockets_, &options)) |
| 461 | .WillOnce(Return(false)) |
| 462 | .WillOnce(Return(true)); |
| 463 | |
| 464 | Error error; |
| 465 | EXPECT_FALSE(driver_->InitManagementChannelOptions(&options, &error)); |
| 466 | EXPECT_EQ(Error::kInternalError, error.type()); |
| 467 | EXPECT_EQ("Unable to setup management channel.", error.message()); |
| 468 | |
| 469 | error.Reset(); |
| 470 | EXPECT_TRUE(driver_->InitManagementChannelOptions(&options, &error)); |
| 471 | EXPECT_TRUE(error.IsSuccess()); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 472 | } |
| 473 | |
Darin Petkov | 55771b7 | 2012-04-25 09:25:19 +0200 | [diff] [blame] | 474 | TEST_F(OpenVPNDriverTest, InitLoggingOptions) { |
| 475 | vector<string> options; |
| 476 | bool vpn_logging = SLOG_IS_ON(VPN, 0); |
| 477 | ScopeLogger::GetInstance()->EnableScopesByName("-vpn"); |
| 478 | driver_->InitLoggingOptions(&options); |
| 479 | ASSERT_EQ(1, options.size()); |
| 480 | EXPECT_EQ("--syslog", options[0]); |
| 481 | ScopeLogger::GetInstance()->EnableScopesByName("+vpn"); |
| 482 | options.clear(); |
| 483 | driver_->InitLoggingOptions(&options); |
| 484 | ExpectInFlags(options, "--verb", "3"); |
| 485 | ScopeLogger::GetInstance()->EnableScopesByName("-vpn"); |
| 486 | SetArg("OpenVPN.Verb", "2"); |
| 487 | options.clear(); |
| 488 | driver_->InitLoggingOptions(&options); |
| 489 | ExpectInFlags(options, "--verb", "2"); |
| 490 | ScopeLogger::GetInstance()->EnableScopesByName("+vpn"); |
| 491 | SetArg("OpenVPN.Verb", "1"); |
| 492 | options.clear(); |
| 493 | driver_->InitLoggingOptions(&options); |
| 494 | ExpectInFlags(options, "--verb", "1"); |
| 495 | if (!vpn_logging) { |
| 496 | ScopeLogger::GetInstance()->EnableScopesByName("-vpn"); |
| 497 | } |
| 498 | } |
| 499 | |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 500 | TEST_F(OpenVPNDriverTest, AppendValueOption) { |
| 501 | vector<string> options; |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 502 | EXPECT_FALSE( |
| 503 | driver_->AppendValueOption("OpenVPN.UnknownProperty", kOption, &options)); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 504 | EXPECT_TRUE(options.empty()); |
| 505 | |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 506 | SetArg(kProperty, ""); |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 507 | EXPECT_FALSE(driver_->AppendValueOption(kProperty, kOption, &options)); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 508 | EXPECT_TRUE(options.empty()); |
| 509 | |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 510 | SetArg(kProperty, kValue); |
| 511 | SetArg(kProperty2, kValue2); |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 512 | EXPECT_TRUE(driver_->AppendValueOption(kProperty, kOption, &options)); |
| 513 | EXPECT_TRUE(driver_->AppendValueOption(kProperty2, kOption2, &options)); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 514 | EXPECT_EQ(4, options.size()); |
| 515 | EXPECT_EQ(kOption, options[0]); |
| 516 | EXPECT_EQ(kValue, options[1]); |
| 517 | EXPECT_EQ(kOption2, options[2]); |
| 518 | EXPECT_EQ(kValue2, options[3]); |
| 519 | } |
| 520 | |
| 521 | TEST_F(OpenVPNDriverTest, AppendFlag) { |
| 522 | vector<string> options; |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 523 | EXPECT_FALSE( |
| 524 | driver_->AppendFlag("OpenVPN.UnknownProperty", kOption, &options)); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 525 | EXPECT_TRUE(options.empty()); |
| 526 | |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 527 | SetArg(kProperty, ""); |
| 528 | SetArg(kProperty2, kValue2); |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 529 | EXPECT_TRUE(driver_->AppendFlag(kProperty, kOption, &options)); |
| 530 | EXPECT_TRUE(driver_->AppendFlag(kProperty2, kOption2, &options)); |
Darin Petkov | fe6a937 | 2012-02-28 16:25:06 +0100 | [diff] [blame] | 531 | EXPECT_EQ(2, options.size()); |
| 532 | EXPECT_EQ(kOption, options[0]); |
| 533 | EXPECT_EQ(kOption2, options[1]); |
| 534 | } |
| 535 | |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 536 | TEST_F(OpenVPNDriverTest, ClaimInterface) { |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 537 | driver_->tunnel_interface_ = kInterfaceName; |
| 538 | EXPECT_FALSE(driver_->ClaimInterface(string(kInterfaceName) + "XXX", |
| 539 | kInterfaceIndex)); |
| 540 | EXPECT_FALSE(driver_->device_); |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 541 | |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 542 | static const char kHost[] = "192.168.2.254"; |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 543 | SetArg(flimflam::kProviderHostProperty, kHost); |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 544 | EXPECT_CALL(*management_server_, Start(_, _, _)).WillOnce(Return(true)); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 545 | EXPECT_CALL(glib_, SpawnAsyncWithPipesCWD(_, _, _, _, _, _, _, _, _, _)) |
| 546 | .WillOnce(Return(true)); |
| 547 | EXPECT_CALL(glib_, ChildWatchAdd(_, _, _)).WillOnce(Return(1)); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 548 | EXPECT_TRUE(driver_->ClaimInterface(kInterfaceName, kInterfaceIndex)); |
| 549 | ASSERT_TRUE(driver_->device_); |
| 550 | EXPECT_EQ(kInterfaceIndex, driver_->device_->interface_index()); |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 551 | } |
| 552 | |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 553 | TEST_F(OpenVPNDriverTest, Cleanup) { |
Darin Petkov | 1fa8194 | 2012-04-02 11:38:08 +0200 | [diff] [blame] | 554 | driver_->Cleanup(Service::kStateIdle); // Ensure no crash. |
| 555 | |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 556 | const unsigned int kTag = 123; |
| 557 | const int kPID = 123456; |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 558 | driver_->child_watch_tag_ = kTag; |
| 559 | driver_->pid_ = kPID; |
| 560 | driver_->rpc_task_.reset(new RPCTask(&control_, this)); |
| 561 | driver_->tunnel_interface_ = kInterfaceName; |
| 562 | driver_->device_ = device_; |
| 563 | driver_->service_ = service_; |
Darin Petkov | 1fa8194 | 2012-04-02 11:38:08 +0200 | [diff] [blame] | 564 | FilePath tls_auth_file; |
| 565 | EXPECT_TRUE(file_util::CreateTemporaryFile(&tls_auth_file)); |
| 566 | EXPECT_FALSE(tls_auth_file.empty()); |
| 567 | EXPECT_TRUE(file_util::PathExists(tls_auth_file)); |
| 568 | driver_->tls_auth_file_ = tls_auth_file; |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 569 | // Stop will be called twice -- once by Cleanup and once by the destructor. |
| 570 | EXPECT_CALL(*management_server_, Stop()).Times(2); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 571 | EXPECT_CALL(glib_, SourceRemove(kTag)); |
| 572 | EXPECT_CALL(glib_, SpawnClosePID(kPID)); |
Darin Petkov | 029d353 | 2012-04-18 14:38:04 +0200 | [diff] [blame] | 573 | EXPECT_CALL(*device_, OnDisconnected()); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 574 | EXPECT_CALL(*device_, SetEnabled(false)); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 575 | EXPECT_CALL(device_info_, DeleteInterface(kInterfaceIndex)); |
| 576 | EXPECT_CALL(*service_, SetState(Service::kStateFailure)); |
Darin Petkov | 3f9131c | 2012-03-20 11:37:32 +0100 | [diff] [blame] | 577 | driver_->Cleanup(Service::kStateFailure); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 578 | EXPECT_EQ(0, driver_->child_watch_tag_); |
| 579 | EXPECT_EQ(0, driver_->pid_); |
| 580 | EXPECT_FALSE(driver_->rpc_task_.get()); |
| 581 | EXPECT_TRUE(driver_->tunnel_interface_.empty()); |
| 582 | EXPECT_FALSE(driver_->device_); |
| 583 | EXPECT_FALSE(driver_->service_); |
Darin Petkov | 1fa8194 | 2012-04-02 11:38:08 +0200 | [diff] [blame] | 584 | EXPECT_FALSE(file_util::PathExists(tls_auth_file)); |
| 585 | EXPECT_TRUE(driver_->tls_auth_file_.empty()); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 586 | } |
| 587 | |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 588 | namespace { |
| 589 | MATCHER(CheckEnv, "") { |
| 590 | if (!arg || !arg[0] || !arg[1] || arg[2]) { |
| 591 | return false; |
| 592 | } |
| 593 | return (string(arg[0]) == "IV_PLAT=Chromium OS" && |
| 594 | string(arg[1]) == "IV_PLAT_REL=2202.0"); |
| 595 | } |
| 596 | } // namespace |
| 597 | |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 598 | TEST_F(OpenVPNDriverTest, SpawnOpenVPN) { |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 599 | SetupLSBRelease(); |
| 600 | |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 601 | EXPECT_FALSE(driver_->SpawnOpenVPN()); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 602 | |
| 603 | static const char kHost[] = "192.168.2.254"; |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 604 | SetArg(flimflam::kProviderHostProperty, kHost); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 605 | driver_->tunnel_interface_ = "tun0"; |
| 606 | driver_->rpc_task_.reset(new RPCTask(&control_, this)); |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 607 | EXPECT_CALL(*management_server_, Start(_, _, _)) |
| 608 | .Times(2) |
| 609 | .WillRepeatedly(Return(true)); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 610 | |
| 611 | const int kPID = 234678; |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 612 | EXPECT_CALL(glib_, |
| 613 | SpawnAsyncWithPipesCWD(_, CheckEnv(), _, _, _, _, _, _, _, _)) |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 614 | .WillOnce(Return(false)) |
| 615 | .WillOnce(DoAll(SetArgumentPointee<5>(kPID), Return(true))); |
| 616 | const int kTag = 6; |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 617 | EXPECT_CALL(glib_, ChildWatchAdd(kPID, &driver_->OnOpenVPNDied, driver_)) |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 618 | .WillOnce(Return(kTag)); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 619 | EXPECT_FALSE(driver_->SpawnOpenVPN()); |
| 620 | EXPECT_TRUE(driver_->SpawnOpenVPN()); |
| 621 | EXPECT_EQ(kPID, driver_->pid_); |
| 622 | EXPECT_EQ(kTag, driver_->child_watch_tag_); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | TEST_F(OpenVPNDriverTest, OnOpenVPNDied) { |
| 626 | const int kPID = 99999; |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 627 | driver_->child_watch_tag_ = 333; |
| 628 | driver_->pid_ = kPID; |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 629 | EXPECT_CALL(glib_, SpawnClosePID(kPID)); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 630 | OpenVPNDriver::OnOpenVPNDied(kPID, 2, driver_); |
| 631 | EXPECT_EQ(0, driver_->child_watch_tag_); |
| 632 | EXPECT_EQ(0, driver_->pid_); |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 633 | } |
| 634 | |
Darin Petkov | 6aa2187 | 2012-03-09 16:10:19 +0100 | [diff] [blame] | 635 | TEST_F(OpenVPNDriverTest, Disconnect) { |
| 636 | driver_->device_ = device_; |
| 637 | driver_->service_ = service_; |
Darin Petkov | 029d353 | 2012-04-18 14:38:04 +0200 | [diff] [blame] | 638 | EXPECT_CALL(*device_, OnDisconnected()); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 639 | EXPECT_CALL(*device_, SetEnabled(false)); |
Darin Petkov | 6aa2187 | 2012-03-09 16:10:19 +0100 | [diff] [blame] | 640 | EXPECT_CALL(device_info_, DeleteInterface(kInterfaceIndex)); |
| 641 | EXPECT_CALL(*service_, SetState(Service::kStateIdle)); |
| 642 | driver_->Disconnect(); |
Darin Petkov | a0e645e | 2012-04-25 11:38:59 +0200 | [diff] [blame] | 643 | EXPECT_FALSE(driver_->device_); |
Darin Petkov | 6aa2187 | 2012-03-09 16:10:19 +0100 | [diff] [blame] | 644 | EXPECT_FALSE(driver_->service_); |
| 645 | } |
| 646 | |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 647 | TEST_F(OpenVPNDriverTest, OnConnectionDisconnected) { |
| 648 | driver_->service_ = service_; |
| 649 | EXPECT_CALL(*service_, SetState(Service::kStateFailure)); |
| 650 | driver_->OnConnectionDisconnected(); |
| 651 | EXPECT_FALSE(driver_->service_); |
| 652 | } |
| 653 | |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 654 | TEST_F(OpenVPNDriverTest, OnReconnecting) { |
| 655 | driver_->OnReconnecting(); // Expect no crash. |
| 656 | driver_->device_ = device_; |
| 657 | driver_->service_ = service_; |
| 658 | EXPECT_CALL(*device_, OnDisconnected()); |
| 659 | EXPECT_CALL(*service_, SetState(Service::kStateAssociating)); |
| 660 | driver_->OnReconnecting(); |
| 661 | } |
| 662 | |
Paul Stewart | 291a473 | 2012-03-14 19:19:02 -0700 | [diff] [blame] | 663 | TEST_F(OpenVPNDriverTest, VerifyPaths) { |
| 664 | // Ensure that the various path constants that the OpenVPN driver uses |
Darin Petkov | a0e645e | 2012-04-25 11:38:59 +0200 | [diff] [blame] | 665 | // actually exists in the build image. Due to build dependencies, they should |
| 666 | // already exist by the time we run unit tests. |
Paul Stewart | 291a473 | 2012-03-14 19:19:02 -0700 | [diff] [blame] | 667 | |
Darin Petkov | a0e645e | 2012-04-25 11:38:59 +0200 | [diff] [blame] | 668 | // The OpenVPNDriver path constants are absolute. FilePath::Append asserts |
| 669 | // that its argument is not an absolute path, so we need to strip the leading |
| 670 | // separators. There's nothing built into FilePath to do so. |
| 671 | static const char *kPaths[] = { |
| 672 | OpenVPNDriver::kOpenVPNPath, |
| 673 | OpenVPNDriver::kOpenVPNScript, |
| 674 | }; |
| 675 | for (size_t i = 0; i < arraysize(kPaths); i++) { |
| 676 | string path(kPaths[i]); |
| 677 | TrimString(path, FilePath::kSeparators, &path); |
| 678 | EXPECT_TRUE(file_util::PathExists(FilePath(SYSROOT).Append(path))) |
| 679 | << kPaths[i]; |
| 680 | } |
Paul Stewart | 291a473 | 2012-03-14 19:19:02 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Darin Petkov | d432539 | 2012-04-23 15:48:22 +0200 | [diff] [blame] | 683 | TEST_F(OpenVPNDriverTest, InitPropertyStore) { |
| 684 | // Sanity test property store initialization. |
| 685 | PropertyStore store; |
| 686 | driver_->InitPropertyStore(&store); |
| 687 | const string kUser = "joe"; |
| 688 | Error error; |
| 689 | EXPECT_TRUE( |
| 690 | store.SetStringProperty(flimflam::kOpenVPNUserProperty, kUser, &error)); |
| 691 | EXPECT_TRUE(error.IsSuccess()); |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 692 | EXPECT_EQ(kUser, GetArgs()->LookupString(flimflam::kOpenVPNUserProperty, "")); |
| 693 | } |
| 694 | |
| 695 | TEST_F(OpenVPNDriverTest, GetProvider) { |
| 696 | PropertyStore store; |
| 697 | driver_->InitPropertyStore(&store); |
Paul Stewart | 8e7e459 | 2012-04-29 09:47:48 -0700 | [diff] [blame] | 698 | PropertyStoreInspector inspector(&store); |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 699 | { |
| 700 | Error error; |
| 701 | KeyValueStore props; |
| 702 | EXPECT_TRUE( |
Paul Stewart | 8e7e459 | 2012-04-29 09:47:48 -0700 | [diff] [blame] | 703 | inspector.GetKeyValueStoreProperty( |
| 704 | flimflam::kProviderProperty, &props, &error)); |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 705 | EXPECT_TRUE(props.LookupBool(flimflam::kPassphraseRequiredProperty, false)); |
| 706 | } |
| 707 | { |
| 708 | Error error; |
| 709 | KeyValueStore props; |
| 710 | SetArg(flimflam::kOpenVPNPasswordProperty, "random-password"); |
| 711 | EXPECT_TRUE( |
Paul Stewart | 8e7e459 | 2012-04-29 09:47:48 -0700 | [diff] [blame] | 712 | inspector.GetKeyValueStoreProperty( |
| 713 | flimflam::kProviderProperty, &props, &error)); |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 714 | EXPECT_FALSE(props.LookupBool(flimflam::kPassphraseRequiredProperty, true)); |
| 715 | } |
Darin Petkov | d432539 | 2012-04-23 15:48:22 +0200 | [diff] [blame] | 716 | } |
| 717 | |
Darin Petkov | 1a462de | 2012-05-02 11:10:48 +0200 | [diff] [blame] | 718 | TEST_F(OpenVPNDriverTest, ParseLSBRelease) { |
| 719 | SetupLSBRelease(); |
| 720 | map<string, string> lsb_release; |
| 721 | EXPECT_TRUE(driver_->ParseLSBRelease(&lsb_release)); |
| 722 | EXPECT_TRUE(ContainsKey(lsb_release, "foo") && lsb_release["foo"] == ""); |
| 723 | EXPECT_EQ("=", lsb_release["zoo"]); |
| 724 | EXPECT_EQ("Chromium OS", lsb_release[OpenVPNDriver::kChromeOSReleaseName]); |
| 725 | EXPECT_EQ("2202.0", lsb_release[OpenVPNDriver::kChromeOSReleaseVersion]); |
| 726 | driver_->lsb_release_file_ = FilePath("/non/existent/file"); |
| 727 | EXPECT_FALSE(driver_->ParseLSBRelease(NULL)); |
| 728 | } |
| 729 | |
| 730 | TEST_F(OpenVPNDriverTest, InitEnvironment) { |
| 731 | vector<string> env; |
| 732 | SetupLSBRelease(); |
| 733 | driver_->InitEnvironment(&env); |
| 734 | ASSERT_EQ(2, env.size()); |
| 735 | EXPECT_EQ("IV_PLAT=Chromium OS", env[0]); |
| 736 | EXPECT_EQ("IV_PLAT_REL=2202.0", env[1]); |
| 737 | env.clear(); |
| 738 | EXPECT_EQ(0, file_util::WriteFile(lsb_release_file_, "", 0)); |
| 739 | driver_->InitEnvironment(&env); |
| 740 | EXPECT_EQ(0, env.size()); |
| 741 | } |
| 742 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 743 | } // namespace shill |