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