Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / remoting / protocol / fake_authenticator.cc
blobb4b9493b75c05796e4d0f7af0dc6e974d3a5c859
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/callback_helpers.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "net/base/io_buffer.h"
11 #include "net/base/net_errors.h"
12 #include "remoting/base/constants.h"
13 #include "remoting/protocol/p2p_stream_socket.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h"
17 namespace remoting {
18 namespace protocol {
20 FakeChannelAuthenticator::FakeChannelAuthenticator(bool accept, bool async)
21 : result_(accept ? net::OK : net::ERR_FAILED),
22 async_(async),
23 did_read_bytes_(false),
24 did_write_bytes_(false),
25 weak_factory_(this) {
28 FakeChannelAuthenticator::~FakeChannelAuthenticator() {
31 void FakeChannelAuthenticator::SecureAndAuthenticate(
32 scoped_ptr<P2PStreamSocket> socket,
33 const DoneCallback& done_callback) {
34 socket_ = socket.Pass();
36 if (async_) {
37 done_callback_ = done_callback;
39 if (result_ != net::OK) {
40 // Don't write anything if we are going to reject auth to make test
41 // ordering deterministic.
42 did_write_bytes_ = true;
43 } else {
44 scoped_refptr<net::IOBuffer> write_buf = new net::IOBuffer(1);
45 write_buf->data()[0] = 0;
46 int result = socket_->Write(
47 write_buf.get(), 1,
48 base::Bind(&FakeChannelAuthenticator::OnAuthBytesWritten,
49 weak_factory_.GetWeakPtr()));
50 if (result != net::ERR_IO_PENDING) {
51 // This will not call the callback because |did_read_bytes_| is
52 // still set to false.
53 OnAuthBytesWritten(result);
57 scoped_refptr<net::IOBuffer> read_buf = new net::IOBuffer(1);
58 int result =
59 socket_->Read(read_buf.get(), 1,
60 base::Bind(&FakeChannelAuthenticator::OnAuthBytesRead,
61 weak_factory_.GetWeakPtr()));
62 if (result != net::ERR_IO_PENDING)
63 OnAuthBytesRead(result);
64 } else {
65 CallDoneCallback();
69 void FakeChannelAuthenticator::OnAuthBytesWritten(int result) {
70 EXPECT_EQ(1, result);
71 EXPECT_FALSE(did_write_bytes_);
72 did_write_bytes_ = true;
73 if (did_read_bytes_)
74 CallDoneCallback();
77 void FakeChannelAuthenticator::OnAuthBytesRead(int result) {
78 EXPECT_EQ(1, result);
79 EXPECT_FALSE(did_read_bytes_);
80 did_read_bytes_ = true;
81 if (did_write_bytes_)
82 CallDoneCallback();
85 void FakeChannelAuthenticator::CallDoneCallback() {
86 if (result_ != net::OK)
87 socket_.reset();
88 base::ResetAndReturn(&done_callback_).Run(result_, socket_.Pass());
91 FakeAuthenticator::FakeAuthenticator(
92 Type type, int round_trips, Action action, bool async)
93 : type_(type),
94 round_trips_(round_trips),
95 action_(action),
96 async_(async),
97 messages_(0),
98 messages_till_started_(0) {
101 FakeAuthenticator::~FakeAuthenticator() {
104 void FakeAuthenticator::set_messages_till_started(int messages) {
105 messages_till_started_ = messages;
108 Authenticator::State FakeAuthenticator::state() const {
109 EXPECT_LE(messages_, round_trips_ * 2);
110 if (messages_ >= round_trips_ * 2) {
111 if (action_ == REJECT) {
112 return REJECTED;
113 } else {
114 return ACCEPTED;
118 // Don't send the last message if this is a host that wants to
119 // reject a connection.
120 if (messages_ == round_trips_ * 2 - 1 &&
121 type_ == HOST && action_ == REJECT) {
122 return REJECTED;
125 // We are not done yet. process next message.
126 if ((messages_ % 2 == 0 && type_ == CLIENT) ||
127 (messages_ % 2 == 1 && type_ == HOST)) {
128 return MESSAGE_READY;
129 } else {
130 return WAITING_MESSAGE;
134 bool FakeAuthenticator::started() const {
135 return messages_ > messages_till_started_;
138 Authenticator::RejectionReason FakeAuthenticator::rejection_reason() const {
139 EXPECT_EQ(REJECTED, state());
140 return INVALID_CREDENTIALS;
143 void FakeAuthenticator::ProcessMessage(const buzz::XmlElement* message,
144 const base::Closure& resume_callback) {
145 EXPECT_EQ(WAITING_MESSAGE, state());
146 std::string id =
147 message->TextNamed(buzz::QName(kChromotingXmlNamespace, "id"));
148 EXPECT_EQ(id, base::IntToString(messages_));
149 ++messages_;
150 resume_callback.Run();
153 scoped_ptr<buzz::XmlElement> FakeAuthenticator::GetNextMessage() {
154 EXPECT_EQ(MESSAGE_READY, state());
156 scoped_ptr<buzz::XmlElement> result(new buzz::XmlElement(
157 buzz::QName(kChromotingXmlNamespace, "authentication")));
158 buzz::XmlElement* id = new buzz::XmlElement(
159 buzz::QName(kChromotingXmlNamespace, "id"));
160 id->AddText(base::IntToString(messages_));
161 result->AddElement(id);
163 ++messages_;
164 return result.Pass();
167 scoped_ptr<ChannelAuthenticator>
168 FakeAuthenticator::CreateChannelAuthenticator() const {
169 EXPECT_EQ(ACCEPTED, state());
170 return make_scoped_ptr(
171 new FakeChannelAuthenticator(action_ != REJECT_CHANNEL, async_));
174 FakeHostAuthenticatorFactory::FakeHostAuthenticatorFactory(
175 int round_trips, int messages_till_started,
176 FakeAuthenticator::Action action, bool async)
177 : round_trips_(round_trips),
178 messages_till_started_(messages_till_started),
179 action_(action), async_(async) {
182 FakeHostAuthenticatorFactory::~FakeHostAuthenticatorFactory() {
185 scoped_ptr<Authenticator> FakeHostAuthenticatorFactory::CreateAuthenticator(
186 const std::string& local_jid,
187 const std::string& remote_jid,
188 const buzz::XmlElement* first_message) {
189 FakeAuthenticator* authenticator = new FakeAuthenticator(
190 FakeAuthenticator::HOST, round_trips_, action_, async_);
191 authenticator->set_messages_till_started(messages_till_started_);
193 scoped_ptr<Authenticator> result(authenticator);
194 return result.Pass();
197 } // namespace protocol
198 } // namespace remoting