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/fake_authenticator.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "net/base/io_buffer.h"
10 #include "net/base/net_errors.h"
11 #include "net/socket/stream_socket.h"
12 #include "remoting/base/constants.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h"
19 FakeChannelAuthenticator::FakeChannelAuthenticator(bool accept
, bool async
)
20 : result_(accept
? net::OK
: net::ERR_FAILED
),
22 did_read_bytes_(false),
23 did_write_bytes_(false),
27 FakeChannelAuthenticator::~FakeChannelAuthenticator() {
30 void FakeChannelAuthenticator::SecureAndAuthenticate(
31 scoped_ptr
<net::StreamSocket
> socket
,
32 const DoneCallback
& done_callback
) {
33 socket_
= socket
.Pass();
36 done_callback_
= done_callback
;
38 if (result_
!= net::OK
) {
39 // Don't write anything if we are going to reject auth to make test
40 // ordering deterministic.
41 did_write_bytes_
= true;
43 scoped_refptr
<net::IOBuffer
> write_buf
= new net::IOBuffer(1);
44 write_buf
->data()[0] = 0;
45 int result
= socket_
->Write(
47 base::Bind(&FakeChannelAuthenticator::OnAuthBytesWritten
,
48 weak_factory_
.GetWeakPtr()));
49 if (result
!= net::ERR_IO_PENDING
) {
50 // This will not call the callback because |did_read_bytes_| is
51 // still set to false.
52 OnAuthBytesWritten(result
);
56 scoped_refptr
<net::IOBuffer
> read_buf
= new net::IOBuffer(1);
58 socket_
->Read(read_buf
.get(), 1,
59 base::Bind(&FakeChannelAuthenticator::OnAuthBytesRead
,
60 weak_factory_
.GetWeakPtr()));
61 if (result
!= net::ERR_IO_PENDING
)
62 OnAuthBytesRead(result
);
68 void FakeChannelAuthenticator::OnAuthBytesWritten(int result
) {
70 EXPECT_FALSE(did_write_bytes_
);
71 did_write_bytes_
= true;
76 void FakeChannelAuthenticator::OnAuthBytesRead(int result
) {
78 EXPECT_FALSE(did_read_bytes_
);
79 did_read_bytes_
= true;
84 void FakeChannelAuthenticator::CallDoneCallback() {
85 DoneCallback callback
= done_callback_
;
86 done_callback_
.Reset();
87 if (result_
!= net::OK
)
89 callback
.Run(result_
, socket_
.Pass());
92 FakeAuthenticator::FakeAuthenticator(
93 Type type
, int round_trips
, Action action
, bool async
)
95 round_trips_(round_trips
),
99 messages_till_started_(0) {
102 FakeAuthenticator::~FakeAuthenticator() {
105 void FakeAuthenticator::set_messages_till_started(int messages
) {
106 messages_till_started_
= messages
;
109 Authenticator::State
FakeAuthenticator::state() const {
110 EXPECT_LE(messages_
, round_trips_
* 2);
111 if (messages_
>= round_trips_
* 2) {
112 if (action_
== REJECT
) {
119 // Don't send the last message if this is a host that wants to
120 // reject a connection.
121 if (messages_
== round_trips_
* 2 - 1 &&
122 type_
== HOST
&& action_
== REJECT
) {
126 // We are not done yet. process next message.
127 if ((messages_
% 2 == 0 && type_
== CLIENT
) ||
128 (messages_
% 2 == 1 && type_
== HOST
)) {
129 return MESSAGE_READY
;
131 return WAITING_MESSAGE
;
135 bool FakeAuthenticator::started() const {
136 return messages_
> messages_till_started_
;
139 Authenticator::RejectionReason
FakeAuthenticator::rejection_reason() const {
140 EXPECT_EQ(REJECTED
, state());
141 return INVALID_CREDENTIALS
;
144 void FakeAuthenticator::ProcessMessage(const buzz::XmlElement
* message
,
145 const base::Closure
& resume_callback
) {
146 EXPECT_EQ(WAITING_MESSAGE
, state());
148 message
->TextNamed(buzz::QName(kChromotingXmlNamespace
, "id"));
149 EXPECT_EQ(id
, base::IntToString(messages_
));
151 resume_callback
.Run();
154 scoped_ptr
<buzz::XmlElement
> FakeAuthenticator::GetNextMessage() {
155 EXPECT_EQ(MESSAGE_READY
, state());
157 scoped_ptr
<buzz::XmlElement
> result(new buzz::XmlElement(
158 buzz::QName(kChromotingXmlNamespace
, "authentication")));
159 buzz::XmlElement
* id
= new buzz::XmlElement(
160 buzz::QName(kChromotingXmlNamespace
, "id"));
161 id
->AddText(base::IntToString(messages_
));
162 result
->AddElement(id
);
165 return result
.Pass();
168 scoped_ptr
<ChannelAuthenticator
>
169 FakeAuthenticator::CreateChannelAuthenticator() const {
170 EXPECT_EQ(ACCEPTED
, state());
171 return make_scoped_ptr(
172 new FakeChannelAuthenticator(action_
!= REJECT_CHANNEL
, async_
));
175 FakeHostAuthenticatorFactory::FakeHostAuthenticatorFactory(
176 int round_trips
, int messages_till_started
,
177 FakeAuthenticator::Action action
, bool async
)
178 : round_trips_(round_trips
),
179 messages_till_started_(messages_till_started
),
180 action_(action
), async_(async
) {
183 FakeHostAuthenticatorFactory::~FakeHostAuthenticatorFactory() {
186 scoped_ptr
<Authenticator
> FakeHostAuthenticatorFactory::CreateAuthenticator(
187 const std::string
& local_jid
,
188 const std::string
& remote_jid
,
189 const buzz::XmlElement
* first_message
) {
190 FakeAuthenticator
* authenticator
= new FakeAuthenticator(
191 FakeAuthenticator::HOST
, round_trips_
, action_
, async_
);
192 authenticator
->set_messages_till_started(messages_till_started_
);
194 scoped_ptr
<Authenticator
> result(authenticator
);
195 return result
.Pass();
198 } // namespace protocol
199 } // namespace remoting