Darin Petkov | 1c11520 | 2012-03-22 15:35:47 +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_management_server.h" |
| 6 | |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 7 | #include <netinet/in.h> |
| 8 | |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 9 | #include <chromeos/dbus/service_constants.h> |
Darin Petkov | 1c11520 | 2012-03-22 15:35:47 +0100 | [diff] [blame] | 10 | #include <gtest/gtest.h> |
| 11 | |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 12 | #include "shill/glib.h" |
Darin Petkov | 1c11520 | 2012-03-22 15:35:47 +0100 | [diff] [blame] | 13 | #include "shill/key_value_store.h" |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 14 | #include "shill/mock_event_dispatcher.h" |
Darin Petkov | 1c11520 | 2012-03-22 15:35:47 +0100 | [diff] [blame] | 15 | #include "shill/mock_openvpn_driver.h" |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 16 | #include "shill/mock_sockets.h" |
| 17 | |
Darin Petkov | 3273da7 | 2013-02-13 11:50:25 +0100 | [diff] [blame] | 18 | using base::Bind; |
| 19 | using base::Unretained; |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 20 | using std::string; |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 21 | using std::vector; |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 22 | using testing::_; |
Darin Petkov | e08084d | 2012-06-11 13:19:35 +0200 | [diff] [blame] | 23 | using testing::Assign; |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 24 | using testing::InSequence; |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 25 | using testing::Return; |
| 26 | using testing::ReturnNew; |
Darin Petkov | 1c11520 | 2012-03-22 15:35:47 +0100 | [diff] [blame] | 27 | |
| 28 | namespace shill { |
| 29 | |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 30 | namespace { |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 31 | MATCHER_P(VoidStringEq, value, "") { |
| 32 | return value == reinterpret_cast<const char *>(arg); |
| 33 | } |
| 34 | } // namespace {} |
| 35 | |
Darin Petkov | 1c11520 | 2012-03-22 15:35:47 +0100 | [diff] [blame] | 36 | class OpenVPNManagementServerTest : public testing::Test { |
| 37 | public: |
| 38 | OpenVPNManagementServerTest() |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 39 | : server_(&driver_, &glib_) {} |
Darin Petkov | 1c11520 | 2012-03-22 15:35:47 +0100 | [diff] [blame] | 40 | |
| 41 | virtual ~OpenVPNManagementServerTest() {} |
| 42 | |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 43 | protected: |
| 44 | static const int kConnectedSocket; |
| 45 | |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 46 | void SetSockets() { server_.sockets_ = &sockets_; } |
| 47 | void SetDispatcher() { server_.dispatcher_ = &dispatcher_; } |
Darin Petkov | ffd3306 | 2012-06-11 19:23:42 +0200 | [diff] [blame] | 48 | void ExpectNotStarted() { EXPECT_FALSE(server_.IsStarted()); } |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 49 | |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 50 | void SetConnectedSocket() { |
| 51 | server_.connected_socket_ = kConnectedSocket; |
| 52 | SetSockets(); |
| 53 | } |
| 54 | |
| 55 | void ExpectSend(const string &value) { |
| 56 | EXPECT_CALL(sockets_, |
| 57 | Send(kConnectedSocket, VoidStringEq(value), value.size(), 0)) |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 58 | .WillOnce(Return(value.size())); |
| 59 | } |
| 60 | |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 61 | void ExpectStaticChallengeResponse() { |
| 62 | driver_.args()->SetString(flimflam::kOpenVPNUserProperty, "jojo"); |
| 63 | driver_.args()->SetString(flimflam::kOpenVPNPasswordProperty, "yoyo"); |
| 64 | driver_.args()->SetString(flimflam::kOpenVPNOTPProperty, "123456"); |
| 65 | SetConnectedSocket(); |
| 66 | ExpectSend("username \"Auth\" jojo\n"); |
| 67 | ExpectSend("password \"Auth\" \"SCRV1:eW95bw==:MTIzNDU2\"\n"); |
| 68 | } |
| 69 | |
Darin Petkov | daaa553 | 2012-07-24 15:37:55 +0200 | [diff] [blame] | 70 | void ExpectAuthenticationResponse() { |
| 71 | driver_.args()->SetString(flimflam::kOpenVPNUserProperty, "jojo"); |
| 72 | driver_.args()->SetString(flimflam::kOpenVPNPasswordProperty, "yoyo"); |
| 73 | SetConnectedSocket(); |
| 74 | ExpectSend("username \"Auth\" jojo\n"); |
| 75 | ExpectSend("password \"Auth\" \"yoyo\"\n"); |
| 76 | } |
| 77 | |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 78 | void ExpectPINResponse() { |
| 79 | driver_.args()->SetString(flimflam::kOpenVPNPinProperty, "987654"); |
| 80 | SetConnectedSocket(); |
| 81 | ExpectSend("password \"User-Specific TPM Token FOO\" \"987654\"\n"); |
| 82 | } |
| 83 | |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 84 | void ExpectHoldRelease() { |
| 85 | SetConnectedSocket(); |
| 86 | ExpectSend("hold release\n"); |
| 87 | } |
| 88 | |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 89 | void ExpectRestart() { |
| 90 | SetConnectedSocket(); |
| 91 | ExpectSend("signal SIGUSR1\n"); |
| 92 | } |
| 93 | |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 94 | InputData CreateInputDataFromString(const string &str) { |
| 95 | InputData data( |
| 96 | reinterpret_cast<unsigned char *>(const_cast<char *>(str.data())), |
| 97 | str.size()); |
| 98 | return data; |
| 99 | } |
| 100 | |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 101 | void SendSignal(const string &signal) { |
| 102 | server_.SendSignal(signal); |
| 103 | } |
| 104 | |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 105 | void OnInput(InputData *data) { |
| 106 | server_.OnInput(data); |
| 107 | } |
| 108 | |
| 109 | void ProcessMessage(const string &message) { |
| 110 | server_.ProcessMessage(message); |
| 111 | } |
| 112 | |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 113 | bool ProcessSuccessMessage(const string &message) { |
| 114 | return server_.ProcessSuccessMessage(message); |
| 115 | } |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 116 | |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 117 | bool ProcessStateMessage(const string &message) { |
| 118 | return server_.ProcessStateMessage(message); |
| 119 | } |
| 120 | |
Darin Petkov | 16e7032 | 2013-03-07 15:54:23 +0100 | [diff] [blame] | 121 | bool ProcessAuthTokenMessage(const string &message) { |
| 122 | return server_.ProcessAuthTokenMessage(message); |
| 123 | } |
| 124 | |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 125 | bool GetHoldWaiting() { return server_.hold_waiting_; } |
| 126 | |
Darin Petkov | aba8932 | 2013-03-11 14:48:22 +0100 | [diff] [blame] | 127 | static string ParseSubstring( |
| 128 | const string &message, const string &start, const string &end) { |
| 129 | return OpenVPNManagementServer::ParseSubstring(message, start, end); |
| 130 | } |
| 131 | |
| 132 | static string ParsePasswordTag(const string &message) { |
| 133 | return OpenVPNManagementServer::ParsePasswordTag(message); |
| 134 | } |
| 135 | |
| 136 | static string ParsePasswordFailedReason(const string &message) { |
| 137 | return OpenVPNManagementServer::ParsePasswordFailedReason(message); |
| 138 | } |
| 139 | |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 140 | void SetClientState(const string &state) { |
| 141 | server_.state_ = state; |
| 142 | } |
| 143 | |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 144 | GLib glib_; |
Darin Petkov | 1c11520 | 2012-03-22 15:35:47 +0100 | [diff] [blame] | 145 | MockOpenVPNDriver driver_; |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 146 | MockSockets sockets_; |
| 147 | MockEventDispatcher dispatcher_; |
mukesh agrawal | 7efde5b | 2013-05-08 11:36:58 -0700 | [diff] [blame^] | 148 | OpenVPNManagementServer server_; // Destroy before anything it references. |
Darin Petkov | 1c11520 | 2012-03-22 15:35:47 +0100 | [diff] [blame] | 149 | }; |
| 150 | |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 151 | // static |
| 152 | const int OpenVPNManagementServerTest::kConnectedSocket = 555; |
| 153 | |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 154 | TEST_F(OpenVPNManagementServerTest, StartStarted) { |
| 155 | SetSockets(); |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 156 | EXPECT_TRUE(server_.Start(NULL, NULL, NULL)); |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | TEST_F(OpenVPNManagementServerTest, StartSocketFail) { |
| 160 | EXPECT_CALL(sockets_, Socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) |
| 161 | .WillOnce(Return(-1)); |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 162 | EXPECT_FALSE(server_.Start(NULL, &sockets_, NULL)); |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 163 | ExpectNotStarted(); |
| 164 | } |
| 165 | |
| 166 | TEST_F(OpenVPNManagementServerTest, StartGetSockNameFail) { |
| 167 | const int kSocket = 123; |
| 168 | EXPECT_CALL(sockets_, Socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) |
| 169 | .WillOnce(Return(kSocket)); |
| 170 | EXPECT_CALL(sockets_, Bind(kSocket, _, _)).WillOnce(Return(0)); |
| 171 | EXPECT_CALL(sockets_, Listen(kSocket, 1)).WillOnce(Return(0)); |
| 172 | EXPECT_CALL(sockets_, GetSockName(kSocket, _, _)).WillOnce(Return(-1)); |
| 173 | EXPECT_CALL(sockets_, Close(kSocket)).WillOnce(Return(0)); |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 174 | EXPECT_FALSE(server_.Start(NULL, &sockets_, NULL)); |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 175 | ExpectNotStarted(); |
| 176 | } |
| 177 | |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 178 | TEST_F(OpenVPNManagementServerTest, Start) { |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 179 | const int kSocket = 123; |
| 180 | EXPECT_CALL(sockets_, Socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) |
| 181 | .WillOnce(Return(kSocket)); |
| 182 | EXPECT_CALL(sockets_, Bind(kSocket, _, _)).WillOnce(Return(0)); |
| 183 | EXPECT_CALL(sockets_, Listen(kSocket, 1)).WillOnce(Return(0)); |
| 184 | EXPECT_CALL(sockets_, GetSockName(kSocket, _, _)).WillOnce(Return(0)); |
| 185 | EXPECT_CALL(dispatcher_, |
Darin Petkov | 3273da7 | 2013-02-13 11:50:25 +0100 | [diff] [blame] | 186 | CreateReadyHandler(kSocket, IOHandler::kModeInput, _)) |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 187 | .WillOnce(ReturnNew<IOHandler>()); |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 188 | vector<string> options; |
| 189 | EXPECT_TRUE(server_.Start(&dispatcher_, &sockets_, &options)); |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 190 | EXPECT_EQ(&sockets_, server_.sockets_); |
| 191 | EXPECT_EQ(kSocket, server_.socket_); |
| 192 | EXPECT_TRUE(server_.ready_handler_.get()); |
| 193 | EXPECT_EQ(&dispatcher_, server_.dispatcher_); |
Darin Petkov | 4646302 | 2012-03-29 14:57:32 +0200 | [diff] [blame] | 194 | EXPECT_FALSE(options.empty()); |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | TEST_F(OpenVPNManagementServerTest, Stop) { |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 198 | EXPECT_TRUE(server_.state().empty()); |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 199 | SetSockets(); |
| 200 | server_.input_handler_.reset(new IOHandler()); |
| 201 | const int kConnectedSocket = 234; |
| 202 | server_.connected_socket_ = kConnectedSocket; |
| 203 | EXPECT_CALL(sockets_, Close(kConnectedSocket)).WillOnce(Return(0)); |
| 204 | SetDispatcher(); |
| 205 | server_.ready_handler_.reset(new IOHandler()); |
| 206 | const int kSocket = 345; |
| 207 | server_.socket_ = kSocket; |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 208 | SetClientState(OpenVPNManagementServer::kStateReconnecting); |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 209 | EXPECT_CALL(sockets_, Close(kSocket)).WillOnce(Return(0)); |
| 210 | server_.Stop(); |
| 211 | EXPECT_FALSE(server_.input_handler_.get()); |
| 212 | EXPECT_EQ(-1, server_.connected_socket_); |
| 213 | EXPECT_FALSE(server_.dispatcher_); |
| 214 | EXPECT_FALSE(server_.ready_handler_.get()); |
| 215 | EXPECT_EQ(-1, server_.socket_); |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 216 | EXPECT_TRUE(server_.state().empty()); |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 217 | ExpectNotStarted(); |
| 218 | } |
| 219 | |
| 220 | TEST_F(OpenVPNManagementServerTest, OnReadyAcceptFail) { |
| 221 | const int kSocket = 333; |
| 222 | SetSockets(); |
| 223 | EXPECT_CALL(sockets_, Accept(kSocket, NULL, NULL)).WillOnce(Return(-1)); |
| 224 | server_.OnReady(kSocket); |
| 225 | EXPECT_EQ(-1, server_.connected_socket_); |
| 226 | } |
| 227 | |
| 228 | TEST_F(OpenVPNManagementServerTest, OnReady) { |
| 229 | const int kSocket = 111; |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 230 | SetConnectedSocket(); |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 231 | SetDispatcher(); |
| 232 | EXPECT_CALL(sockets_, Accept(kSocket, NULL, NULL)) |
| 233 | .WillOnce(Return(kConnectedSocket)); |
| 234 | server_.ready_handler_.reset(new IOHandler()); |
Darin Petkov | 3273da7 | 2013-02-13 11:50:25 +0100 | [diff] [blame] | 235 | EXPECT_CALL(dispatcher_, CreateInputHandler(kConnectedSocket, _, _)) |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 236 | .WillOnce(ReturnNew<IOHandler>()); |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 237 | ExpectSend("state on\n"); |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 238 | server_.OnReady(kSocket); |
| 239 | EXPECT_EQ(kConnectedSocket, server_.connected_socket_); |
| 240 | EXPECT_FALSE(server_.ready_handler_.get()); |
| 241 | EXPECT_TRUE(server_.input_handler_.get()); |
| 242 | } |
| 243 | |
| 244 | TEST_F(OpenVPNManagementServerTest, OnInput) { |
| 245 | { |
| 246 | string s; |
| 247 | InputData data = CreateInputDataFromString(s); |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 248 | OnInput(&data); |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 249 | } |
| 250 | { |
| 251 | string s = "foo\n" |
| 252 | ">INFO:...\n" |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 253 | ">PASSWORD:Need 'Auth' SC:user/password/otp\n" |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 254 | ">PASSWORD:Need 'User-Specific TPM Token FOO' ...\n" |
Darin Petkov | 0440b9b | 2012-04-17 16:11:56 +0200 | [diff] [blame] | 255 | ">PASSWORD:Verification Failed: .\n" |
Darin Petkov | 16e7032 | 2013-03-07 15:54:23 +0100 | [diff] [blame] | 256 | ">PASSWORD:Auth-Token:ToKeN==\n" |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 257 | ">STATE:123,RECONNECTING,detail,...,...\n" |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 258 | ">HOLD:Waiting for hold release\n" |
| 259 | "SUCCESS: Hold released."; |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 260 | InputData data = CreateInputDataFromString(s); |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 261 | ExpectStaticChallengeResponse(); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 262 | ExpectPINResponse(); |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 263 | EXPECT_CALL(driver_, FailService(Service::kFailureConnect, |
| 264 | Service::kErrorDetailsNone)); |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 265 | EXPECT_CALL(driver_, OnReconnecting(_)); |
| 266 | EXPECT_FALSE(GetHoldWaiting()); |
| 267 | OnInput(&data); |
| 268 | EXPECT_TRUE(GetHoldWaiting()); |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
Darin Petkov | e08084d | 2012-06-11 13:19:35 +0200 | [diff] [blame] | 272 | TEST_F(OpenVPNManagementServerTest, OnInputStop) { |
| 273 | string s = |
| 274 | ">PASSWORD:Verification Failed: .\n" |
| 275 | ">STATE:123,RECONNECTING,detail,...,..."; |
| 276 | InputData data = CreateInputDataFromString(s); |
| 277 | SetSockets(); |
| 278 | // Stops the server after the first message is processed. |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 279 | EXPECT_CALL(driver_, FailService(Service::kFailureConnect, |
| 280 | Service::kErrorDetailsNone)) |
Darin Petkov | e08084d | 2012-06-11 13:19:35 +0200 | [diff] [blame] | 281 | .WillOnce(Assign(&server_.sockets_, reinterpret_cast<Sockets *>(NULL))); |
| 282 | // The second message should not be processed. |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 283 | EXPECT_CALL(driver_, OnReconnecting(_)).Times(0); |
| 284 | OnInput(&data); |
Darin Petkov | e08084d | 2012-06-11 13:19:35 +0200 | [diff] [blame] | 285 | } |
| 286 | |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 287 | TEST_F(OpenVPNManagementServerTest, ProcessMessage) { |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 288 | ProcessMessage("foo"); |
| 289 | ProcessMessage(">INFO:"); |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 290 | |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 291 | EXPECT_CALL(driver_, OnReconnecting(_)); |
| 292 | ProcessMessage(">STATE:123,RECONNECTING,detail,...,..."); |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 293 | } |
| 294 | |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 295 | TEST_F(OpenVPNManagementServerTest, ProcessSuccessMessage) { |
| 296 | EXPECT_FALSE(ProcessSuccessMessage("foo")); |
| 297 | EXPECT_TRUE(ProcessSuccessMessage("SUCCESS: foo")); |
| 298 | } |
| 299 | |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 300 | TEST_F(OpenVPNManagementServerTest, ProcessInfoMessage) { |
| 301 | EXPECT_FALSE(server_.ProcessInfoMessage("foo")); |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 302 | EXPECT_TRUE(server_.ProcessInfoMessage(">INFO:foo")); |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | TEST_F(OpenVPNManagementServerTest, ProcessStateMessage) { |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 306 | EXPECT_TRUE(server_.state().empty()); |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 307 | EXPECT_FALSE(ProcessStateMessage("foo")); |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 308 | EXPECT_TRUE(server_.state().empty()); |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 309 | EXPECT_TRUE(ProcessStateMessage(">STATE:123,WAIT,detail,...,...")); |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 310 | EXPECT_EQ("WAIT", server_.state()); |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 311 | { |
| 312 | InSequence seq; |
| 313 | EXPECT_CALL(driver_, |
| 314 | OnReconnecting(OpenVPNDriver::kReconnectReasonUnknown)); |
| 315 | EXPECT_CALL(driver_, |
| 316 | OnReconnecting(OpenVPNDriver::kReconnectReasonTLSError)); |
| 317 | } |
| 318 | EXPECT_TRUE(ProcessStateMessage(">STATE:123,RECONNECTING,detail,...,...")); |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 319 | EXPECT_EQ(OpenVPNManagementServer::kStateReconnecting, server_.state()); |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 320 | EXPECT_TRUE(ProcessStateMessage(">STATE:123,RECONNECTING,tls-error,...,...")); |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 321 | } |
| 322 | |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 323 | TEST_F(OpenVPNManagementServerTest, ProcessNeedPasswordMessageAuthSC) { |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 324 | ExpectStaticChallengeResponse(); |
| 325 | EXPECT_TRUE( |
| 326 | server_.ProcessNeedPasswordMessage( |
| 327 | ">PASSWORD:Need 'Auth' SC:user/password/otp")); |
| 328 | EXPECT_FALSE(driver_.args()->ContainsString(flimflam::kOpenVPNOTPProperty)); |
| 329 | } |
| 330 | |
Darin Petkov | daaa553 | 2012-07-24 15:37:55 +0200 | [diff] [blame] | 331 | TEST_F(OpenVPNManagementServerTest, ProcessNeedPasswordMessageAuth) { |
| 332 | ExpectAuthenticationResponse(); |
| 333 | EXPECT_TRUE( |
| 334 | server_.ProcessNeedPasswordMessage( |
| 335 | ">PASSWORD:Need 'Auth' username/password")); |
| 336 | } |
| 337 | |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 338 | TEST_F(OpenVPNManagementServerTest, ProcessNeedPasswordMessageTPMToken) { |
| 339 | ExpectPINResponse(); |
| 340 | EXPECT_TRUE( |
| 341 | server_.ProcessNeedPasswordMessage( |
| 342 | ">PASSWORD:Need 'User-Specific TPM Token FOO' ...")); |
| 343 | } |
| 344 | |
| 345 | TEST_F(OpenVPNManagementServerTest, ProcessNeedPasswordMessageUnknown) { |
| 346 | EXPECT_FALSE(server_.ProcessNeedPasswordMessage("foo")); |
| 347 | } |
| 348 | |
Darin Petkov | aba8932 | 2013-03-11 14:48:22 +0100 | [diff] [blame] | 349 | TEST_F(OpenVPNManagementServerTest, ParseSubstring) { |
| 350 | EXPECT_EQ("", ParseSubstring("", "'", "'")); |
| 351 | EXPECT_EQ("", ParseSubstring(" ", "'", "'")); |
| 352 | EXPECT_EQ("", ParseSubstring("'", "'", "'")); |
| 353 | EXPECT_EQ("", ParseSubstring("''", "'", "'")); |
| 354 | EXPECT_EQ("", ParseSubstring("] [", "[", "]")); |
| 355 | EXPECT_EQ("", ParseSubstring("[]", "[", "]")); |
| 356 | EXPECT_EQ("bar", ParseSubstring("foo['bar']zoo", "['", "']")); |
| 357 | EXPECT_EQ("bar", ParseSubstring("foo['bar']", "['", "']")); |
| 358 | EXPECT_EQ("bar", ParseSubstring("['bar']zoo", "['", "']")); |
| 359 | EXPECT_EQ("bar", ParseSubstring("['bar']['zoo']", "['", "']")); |
| 360 | } |
| 361 | |
| 362 | TEST_F(OpenVPNManagementServerTest, ParsePasswordTag) { |
| 363 | EXPECT_EQ("", ParsePasswordTag("")); |
| 364 | EXPECT_EQ("Auth", |
| 365 | ParsePasswordTag( |
| 366 | ">PASSWORD:Verification Failed: 'Auth' " |
| 367 | "['REVOKED: client certificate has been revoked']")); |
| 368 | } |
| 369 | |
| 370 | TEST_F(OpenVPNManagementServerTest, ParsePasswordFailedReason) { |
| 371 | EXPECT_EQ("", ParsePasswordFailedReason("")); |
| 372 | EXPECT_EQ("REVOKED: client certificate has been revoked", |
| 373 | ParsePasswordFailedReason( |
| 374 | ">PASSWORD:Verification Failed: 'Auth' " |
| 375 | "['REVOKED: client certificate has been revoked']")); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 376 | } |
| 377 | |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 378 | TEST_F(OpenVPNManagementServerTest, PerformStaticChallengeNoCreds) { |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 379 | EXPECT_CALL(driver_, FailService(Service::kFailureInternal, |
| 380 | Service::kErrorDetailsNone)).Times(3); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 381 | server_.PerformStaticChallenge("Auth"); |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 382 | driver_.args()->SetString(flimflam::kOpenVPNUserProperty, "jojo"); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 383 | server_.PerformStaticChallenge("Auth"); |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 384 | driver_.args()->SetString(flimflam::kOpenVPNPasswordProperty, "yoyo"); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 385 | server_.PerformStaticChallenge("Auth"); |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | TEST_F(OpenVPNManagementServerTest, PerformStaticChallenge) { |
| 389 | ExpectStaticChallengeResponse(); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 390 | server_.PerformStaticChallenge("Auth"); |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 391 | EXPECT_FALSE(driver_.args()->ContainsString(flimflam::kOpenVPNOTPProperty)); |
| 392 | } |
| 393 | |
Darin Petkov | daaa553 | 2012-07-24 15:37:55 +0200 | [diff] [blame] | 394 | TEST_F(OpenVPNManagementServerTest, PerformAuthenticationNoCreds) { |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 395 | EXPECT_CALL(driver_, FailService(Service::kFailureInternal, |
| 396 | Service::kErrorDetailsNone)).Times(2); |
Darin Petkov | daaa553 | 2012-07-24 15:37:55 +0200 | [diff] [blame] | 397 | server_.PerformAuthentication("Auth"); |
| 398 | driver_.args()->SetString(flimflam::kOpenVPNUserProperty, "jojo"); |
| 399 | server_.PerformAuthentication("Auth"); |
| 400 | } |
| 401 | |
| 402 | TEST_F(OpenVPNManagementServerTest, PerformAuthentication) { |
| 403 | ExpectAuthenticationResponse(); |
| 404 | server_.PerformAuthentication("Auth"); |
| 405 | } |
| 406 | |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 407 | TEST_F(OpenVPNManagementServerTest, ProcessHoldMessage) { |
| 408 | EXPECT_FALSE(server_.hold_release_); |
| 409 | EXPECT_FALSE(server_.hold_waiting_); |
| 410 | |
| 411 | EXPECT_FALSE(server_.ProcessHoldMessage("foo")); |
| 412 | |
| 413 | EXPECT_TRUE(server_.ProcessHoldMessage(">HOLD:Waiting for hold release")); |
| 414 | EXPECT_FALSE(server_.hold_release_); |
| 415 | EXPECT_TRUE(server_.hold_waiting_); |
| 416 | |
| 417 | ExpectHoldRelease(); |
| 418 | server_.hold_release_ = true; |
| 419 | server_.hold_waiting_ = false; |
| 420 | EXPECT_TRUE(server_.ProcessHoldMessage(">HOLD:Waiting for hold release")); |
| 421 | EXPECT_TRUE(server_.hold_release_); |
| 422 | EXPECT_FALSE(server_.hold_waiting_); |
| 423 | } |
| 424 | |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 425 | TEST_F(OpenVPNManagementServerTest, SupplyTPMTokenNoPIN) { |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 426 | EXPECT_CALL(driver_, FailService(Service::kFailureInternal, |
| 427 | Service::kErrorDetailsNone)); |
Darin Petkov | e0d5dd1 | 2012-04-04 16:10:48 +0200 | [diff] [blame] | 428 | server_.SupplyTPMToken("User-Specific TPM Token FOO"); |
| 429 | } |
| 430 | |
| 431 | TEST_F(OpenVPNManagementServerTest, SupplyTPMToken) { |
| 432 | ExpectPINResponse(); |
| 433 | server_.SupplyTPMToken("User-Specific TPM Token FOO"); |
| 434 | } |
| 435 | |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 436 | TEST_F(OpenVPNManagementServerTest, Send) { |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 437 | const char kMessage[] = "foo\n"; |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 438 | SetConnectedSocket(); |
| 439 | ExpectSend(kMessage); |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 440 | server_.Send(kMessage); |
| 441 | } |
| 442 | |
| 443 | TEST_F(OpenVPNManagementServerTest, SendState) { |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 444 | SetConnectedSocket(); |
| 445 | ExpectSend("state off\n"); |
Darin Petkov | 271fe52 | 2012-03-27 13:47:29 +0200 | [diff] [blame] | 446 | server_.SendState("off"); |
Darin Petkov | 1c11520 | 2012-03-22 15:35:47 +0100 | [diff] [blame] | 447 | } |
| 448 | |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 449 | TEST_F(OpenVPNManagementServerTest, SendUsername) { |
| 450 | SetConnectedSocket(); |
| 451 | ExpectSend("username \"Auth\" joesmith\n"); |
| 452 | server_.SendUsername("Auth", "joesmith"); |
| 453 | } |
| 454 | |
| 455 | TEST_F(OpenVPNManagementServerTest, SendPassword) { |
| 456 | SetConnectedSocket(); |
Darin Petkov | daaa553 | 2012-07-24 15:37:55 +0200 | [diff] [blame] | 457 | ExpectSend("password \"Auth\" \"foo\\\"bar\"\n"); |
| 458 | server_.SendPassword("Auth", "foo\"bar"); |
Darin Petkov | 683942b | 2012-03-27 18:00:04 +0200 | [diff] [blame] | 459 | } |
| 460 | |
Darin Petkov | 0440b9b | 2012-04-17 16:11:56 +0200 | [diff] [blame] | 461 | TEST_F(OpenVPNManagementServerTest, ProcessFailedPasswordMessage) { |
| 462 | EXPECT_FALSE(server_.ProcessFailedPasswordMessage("foo")); |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 463 | EXPECT_CALL(driver_, FailService(Service::kFailureConnect, |
| 464 | Service::kErrorDetailsNone)).Times(3); |
| 465 | EXPECT_CALL(driver_, FailService(Service::kFailureConnect, "Revoked.")); |
Darin Petkov | 0440b9b | 2012-04-17 16:11:56 +0200 | [diff] [blame] | 466 | EXPECT_TRUE( |
| 467 | server_.ProcessFailedPasswordMessage(">PASSWORD:Verification Failed: .")); |
Darin Petkov | aba8932 | 2013-03-11 14:48:22 +0100 | [diff] [blame] | 468 | EXPECT_TRUE( |
| 469 | server_.ProcessFailedPasswordMessage( |
| 470 | ">PASSWORD:Verification Failed: 'Private Key' ['Reason']")); |
| 471 | EXPECT_TRUE( |
| 472 | server_.ProcessFailedPasswordMessage( |
| 473 | ">PASSWORD:Verification Failed: 'Auth'")); |
| 474 | EXPECT_TRUE( |
| 475 | server_.ProcessFailedPasswordMessage( |
| 476 | ">PASSWORD:Verification Failed: 'Auth' ['Revoked.']")); |
Darin Petkov | 0440b9b | 2012-04-17 16:11:56 +0200 | [diff] [blame] | 477 | } |
| 478 | |
Darin Petkov | 16e7032 | 2013-03-07 15:54:23 +0100 | [diff] [blame] | 479 | TEST_F(OpenVPNManagementServerTest, ProcessAuthTokenMessage) { |
| 480 | EXPECT_FALSE(ProcessAuthTokenMessage("foo")); |
| 481 | EXPECT_TRUE(ProcessAuthTokenMessage(">PASSWORD:Auth-Token:ToKeN==")); |
| 482 | } |
| 483 | |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 484 | TEST_F(OpenVPNManagementServerTest, SendSignal) { |
| 485 | SetConnectedSocket(); |
| 486 | ExpectSend("signal SIGUSR2\n"); |
| 487 | SendSignal("SIGUSR2"); |
| 488 | } |
| 489 | |
| 490 | TEST_F(OpenVPNManagementServerTest, Restart) { |
| 491 | ExpectRestart(); |
| 492 | server_.Restart(); |
| 493 | } |
| 494 | |
Darin Petkov | a5e07ef | 2012-07-09 14:27:57 +0200 | [diff] [blame] | 495 | TEST_F(OpenVPNManagementServerTest, SendHoldRelease) { |
| 496 | ExpectHoldRelease(); |
| 497 | server_.SendHoldRelease(); |
| 498 | } |
| 499 | |
| 500 | TEST_F(OpenVPNManagementServerTest, Hold) { |
| 501 | EXPECT_FALSE(server_.hold_release_); |
| 502 | EXPECT_FALSE(server_.hold_waiting_); |
| 503 | |
| 504 | server_.ReleaseHold(); |
| 505 | EXPECT_TRUE(server_.hold_release_); |
| 506 | EXPECT_FALSE(server_.hold_waiting_); |
| 507 | |
| 508 | server_.Hold(); |
| 509 | EXPECT_FALSE(server_.hold_release_); |
| 510 | EXPECT_FALSE(server_.hold_waiting_); |
| 511 | |
| 512 | server_.hold_waiting_ = true; |
| 513 | ExpectHoldRelease(); |
| 514 | server_.ReleaseHold(); |
| 515 | EXPECT_TRUE(server_.hold_release_); |
| 516 | EXPECT_FALSE(server_.hold_waiting_); |
| 517 | } |
| 518 | |
Darin Petkov | daaa553 | 2012-07-24 15:37:55 +0200 | [diff] [blame] | 519 | TEST_F(OpenVPNManagementServerTest, EscapeToQuote) { |
| 520 | EXPECT_EQ("", OpenVPNManagementServer::EscapeToQuote("")); |
| 521 | EXPECT_EQ("foo './", OpenVPNManagementServer::EscapeToQuote("foo './")); |
| 522 | EXPECT_EQ("\\\\", OpenVPNManagementServer::EscapeToQuote("\\")); |
| 523 | EXPECT_EQ("\\\"", OpenVPNManagementServer::EscapeToQuote("\"")); |
| 524 | EXPECT_EQ("\\\\\\\"foo\\\\bar\\\"", |
| 525 | OpenVPNManagementServer::EscapeToQuote("\\\"foo\\bar\"")); |
| 526 | } |
| 527 | |
Darin Petkov | 1c11520 | 2012-03-22 15:35:47 +0100 | [diff] [blame] | 528 | } // namespace shill |