1 // Copyright (c) 2012 The Chromium 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.
5 #include "remoting/protocol/authenticator_test_base.h"
7 #include "base/base64.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/path_service.h"
11 #include "base/test/test_timeouts.h"
12 #include "base/timer/timer.h"
13 #include "net/base/net_errors.h"
14 #include "net/base/test_data_directory.h"
15 #include "remoting/base/rsa_key_pair.h"
16 #include "remoting/protocol/authenticator.h"
17 #include "remoting/protocol/channel_authenticator.h"
18 #include "remoting/protocol/fake_session.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h"
23 using testing::SaveArg
;
30 ACTION_P(QuitThreadOnCounter
, counter
) {
32 EXPECT_GE(*counter
, 0);
34 base::MessageLoop::current()->Quit();
39 AuthenticatorTestBase::MockChannelDoneCallback::MockChannelDoneCallback() {}
41 AuthenticatorTestBase::MockChannelDoneCallback::~MockChannelDoneCallback() {}
43 AuthenticatorTestBase::AuthenticatorTestBase() {}
45 AuthenticatorTestBase::~AuthenticatorTestBase() {}
47 void AuthenticatorTestBase::SetUp() {
48 base::FilePath
certs_dir(net::GetTestCertsDirectory());
50 base::FilePath cert_path
= certs_dir
.AppendASCII("unittest.selfsigned.der");
51 ASSERT_TRUE(base::ReadFileToString(cert_path
, &host_cert_
));
53 base::FilePath key_path
= certs_dir
.AppendASCII("unittest.key.bin");
54 std::string key_string
;
55 ASSERT_TRUE(base::ReadFileToString(key_path
, &key_string
));
56 std::string key_base64
;
57 base::Base64Encode(key_string
, &key_base64
);
58 key_pair_
= RsaKeyPair::FromString(key_base64
);
59 ASSERT_TRUE(key_pair_
.get());
60 host_public_key_
= key_pair_
->GetPublicKey();
63 void AuthenticatorTestBase::RunAuthExchange() {
64 ContinueAuthExchangeWith(client_
.get(),
70 void AuthenticatorTestBase::RunHostInitiatedAuthExchange() {
71 ContinueAuthExchangeWith(host_
.get(),
78 // This function sends a message from the sender and receiver and recursively
79 // calls itself to the send the next message from the receiver to the sender
80 // untils the authentication completes.
81 void AuthenticatorTestBase::ContinueAuthExchangeWith(Authenticator
* sender
,
82 Authenticator
* receiver
,
84 bool receiver_started
) {
85 scoped_ptr
<buzz::XmlElement
> message
;
86 ASSERT_NE(Authenticator::WAITING_MESSAGE
, sender
->state());
87 if (sender
->state() == Authenticator::ACCEPTED
||
88 sender
->state() == Authenticator::REJECTED
)
91 // Verify that once the started flag for either party is set to true,
92 // it should always stay true.
93 if (receiver_started
) {
94 ASSERT_TRUE(receiver
->started());
98 ASSERT_TRUE(sender
->started());
101 ASSERT_EQ(Authenticator::MESSAGE_READY
, sender
->state());
102 message
= sender
->GetNextMessage();
103 ASSERT_TRUE(message
.get());
104 ASSERT_NE(Authenticator::MESSAGE_READY
, sender
->state());
106 ASSERT_EQ(Authenticator::WAITING_MESSAGE
, receiver
->state());
107 receiver
->ProcessMessage(message
.get(), base::Bind(
108 &AuthenticatorTestBase::ContinueAuthExchangeWith
,
109 base::Unretained(receiver
), base::Unretained(sender
),
110 receiver
->started(), sender
->started()));
113 void AuthenticatorTestBase::RunChannelAuth(bool expected_fail
) {
114 client_fake_socket_
.reset(new FakeSocket());
115 host_fake_socket_
.reset(new FakeSocket());
116 client_fake_socket_
->PairWith(host_fake_socket_
.get());
118 client_auth_
->SecureAndAuthenticate(
119 client_fake_socket_
.PassAs
<net::StreamSocket
>(),
120 base::Bind(&AuthenticatorTestBase::OnClientConnected
,
121 base::Unretained(this)));
123 host_auth_
->SecureAndAuthenticate(
124 host_fake_socket_
.PassAs
<net::StreamSocket
>(),
125 base::Bind(&AuthenticatorTestBase::OnHostConnected
,
126 base::Unretained(this)));
128 // Expect two callbacks to be called - the client callback and the host
130 int callback_counter
= 2;
132 EXPECT_CALL(client_callback_
, OnDone(net::OK
))
133 .WillOnce(QuitThreadOnCounter(&callback_counter
));
135 EXPECT_CALL(host_callback_
, OnDone(net::ERR_FAILED
))
136 .WillOnce(QuitThreadOnCounter(&callback_counter
));
138 EXPECT_CALL(host_callback_
, OnDone(net::OK
))
139 .WillOnce(QuitThreadOnCounter(&callback_counter
));
142 // Ensure that .Run() does not run unbounded if the callbacks are never
144 base::Timer
shutdown_timer(false, false);
145 shutdown_timer
.Start(FROM_HERE
,
146 TestTimeouts::action_timeout(),
147 base::MessageLoop::QuitClosure());
149 shutdown_timer
.Stop();
151 testing::Mock::VerifyAndClearExpectations(&client_callback_
);
152 testing::Mock::VerifyAndClearExpectations(&host_callback_
);
154 if (!expected_fail
) {
155 ASSERT_TRUE(client_socket_
.get() != NULL
);
156 ASSERT_TRUE(host_socket_
.get() != NULL
);
160 void AuthenticatorTestBase::OnHostConnected(
162 scoped_ptr
<net::StreamSocket
> socket
) {
163 host_callback_
.OnDone(error
);
164 host_socket_
= socket
.Pass();
167 void AuthenticatorTestBase::OnClientConnected(
169 scoped_ptr
<net::StreamSocket
> socket
) {
170 client_callback_
.OnDone(error
);
171 client_socket_
= socket
.Pass();
174 } // namespace protocol
175 } // namespace remoting