OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/protocol/fake_authenticator.h" | 5 #include "remoting/protocol/fake_authenticator.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
| 11 #include "base/memory/ptr_util.h" |
11 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
12 #include "base/rand_util.h" | 13 #include "base/rand_util.h" |
13 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
14 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
15 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
16 #include "remoting/base/constants.h" | 17 #include "remoting/base/constants.h" |
17 #include "remoting/protocol/p2p_stream_socket.h" | 18 #include "remoting/protocol/p2p_stream_socket.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" | 20 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" |
20 | 21 |
21 namespace remoting { | 22 namespace remoting { |
22 namespace protocol { | 23 namespace protocol { |
23 | 24 |
24 FakeChannelAuthenticator::FakeChannelAuthenticator(bool accept, bool async) | 25 FakeChannelAuthenticator::FakeChannelAuthenticator(bool accept, bool async) |
25 : result_(accept ? net::OK : net::ERR_FAILED), | 26 : result_(accept ? net::OK : net::ERR_FAILED), |
26 async_(async), | 27 async_(async), |
27 weak_factory_(this) {} | 28 weak_factory_(this) {} |
28 | 29 |
29 FakeChannelAuthenticator::~FakeChannelAuthenticator() {} | 30 FakeChannelAuthenticator::~FakeChannelAuthenticator() {} |
30 | 31 |
31 void FakeChannelAuthenticator::SecureAndAuthenticate( | 32 void FakeChannelAuthenticator::SecureAndAuthenticate( |
32 scoped_ptr<P2PStreamSocket> socket, | 33 std::unique_ptr<P2PStreamSocket> socket, |
33 const DoneCallback& done_callback) { | 34 const DoneCallback& done_callback) { |
34 socket_ = std::move(socket); | 35 socket_ = std::move(socket); |
35 | 36 |
36 done_callback_ = done_callback; | 37 done_callback_ = done_callback; |
37 | 38 |
38 if (async_) { | 39 if (async_) { |
39 if (result_ != net::OK) { | 40 if (result_ != net::OK) { |
40 // Don't write anything if we are going to reject auth to make test | 41 // Don't write anything if we are going to reject auth to make test |
41 // ordering deterministic. | 42 // ordering deterministic. |
42 did_write_bytes_ = true; | 43 did_write_bytes_ = true; |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 std::string key_base64 = | 148 std::string key_base64 = |
148 message->TextNamed(buzz::QName(kChromotingXmlNamespace, "key")); | 149 message->TextNamed(buzz::QName(kChromotingXmlNamespace, "key")); |
149 EXPECT_TRUE(!key_base64.empty()); | 150 EXPECT_TRUE(!key_base64.empty()); |
150 EXPECT_TRUE(base::Base64Decode(key_base64, &auth_key_)); | 151 EXPECT_TRUE(base::Base64Decode(key_base64, &auth_key_)); |
151 } | 152 } |
152 | 153 |
153 ++messages_; | 154 ++messages_; |
154 resume_callback.Run(); | 155 resume_callback.Run(); |
155 } | 156 } |
156 | 157 |
157 scoped_ptr<buzz::XmlElement> FakeAuthenticator::GetNextMessage() { | 158 std::unique_ptr<buzz::XmlElement> FakeAuthenticator::GetNextMessage() { |
158 EXPECT_EQ(MESSAGE_READY, state()); | 159 EXPECT_EQ(MESSAGE_READY, state()); |
159 | 160 |
160 scoped_ptr<buzz::XmlElement> result(new buzz::XmlElement( | 161 std::unique_ptr<buzz::XmlElement> result(new buzz::XmlElement( |
161 buzz::QName(kChromotingXmlNamespace, "authentication"))); | 162 buzz::QName(kChromotingXmlNamespace, "authentication"))); |
162 buzz::XmlElement* id = new buzz::XmlElement( | 163 buzz::XmlElement* id = new buzz::XmlElement( |
163 buzz::QName(kChromotingXmlNamespace, "id")); | 164 buzz::QName(kChromotingXmlNamespace, "id")); |
164 id->AddText(base::IntToString(messages_)); | 165 id->AddText(base::IntToString(messages_)); |
165 result->AddElement(id); | 166 result->AddElement(id); |
166 | 167 |
167 // Add authentication key in the last message sent from host to client. | 168 // Add authentication key in the last message sent from host to client. |
168 if (type_ == HOST && messages_ == round_trips_ * 2 - 1) { | 169 if (type_ == HOST && messages_ == round_trips_ * 2 - 1) { |
169 auth_key_ = base::RandBytesAsString(16); | 170 auth_key_ = base::RandBytesAsString(16); |
170 buzz::XmlElement* key = new buzz::XmlElement( | 171 buzz::XmlElement* key = new buzz::XmlElement( |
171 buzz::QName(kChromotingXmlNamespace, "key")); | 172 buzz::QName(kChromotingXmlNamespace, "key")); |
172 std::string key_base64; | 173 std::string key_base64; |
173 base::Base64Encode(auth_key_, &key_base64); | 174 base::Base64Encode(auth_key_, &key_base64); |
174 key->AddText(key_base64); | 175 key->AddText(key_base64); |
175 result->AddElement(key); | 176 result->AddElement(key); |
176 } | 177 } |
177 | 178 |
178 ++messages_; | 179 ++messages_; |
179 return result; | 180 return result; |
180 } | 181 } |
181 | 182 |
182 const std::string& FakeAuthenticator::GetAuthKey() const { | 183 const std::string& FakeAuthenticator::GetAuthKey() const { |
183 EXPECT_EQ(ACCEPTED, state()); | 184 EXPECT_EQ(ACCEPTED, state()); |
184 DCHECK(!auth_key_.empty()); | 185 DCHECK(!auth_key_.empty()); |
185 return auth_key_; | 186 return auth_key_; |
186 } | 187 } |
187 | 188 |
188 scoped_ptr<ChannelAuthenticator> | 189 std::unique_ptr<ChannelAuthenticator> |
189 FakeAuthenticator::CreateChannelAuthenticator() const { | 190 FakeAuthenticator::CreateChannelAuthenticator() const { |
190 EXPECT_EQ(ACCEPTED, state()); | 191 EXPECT_EQ(ACCEPTED, state()); |
191 return make_scoped_ptr( | 192 return base::WrapUnique( |
192 new FakeChannelAuthenticator(action_ != REJECT_CHANNEL, async_)); | 193 new FakeChannelAuthenticator(action_ != REJECT_CHANNEL, async_)); |
193 } | 194 } |
194 | 195 |
195 FakeHostAuthenticatorFactory::FakeHostAuthenticatorFactory( | 196 FakeHostAuthenticatorFactory::FakeHostAuthenticatorFactory( |
196 int round_trips, | 197 int round_trips, |
197 int messages_till_started, | 198 int messages_till_started, |
198 FakeAuthenticator::Action action, | 199 FakeAuthenticator::Action action, |
199 bool async) | 200 bool async) |
200 : round_trips_(round_trips), | 201 : round_trips_(round_trips), |
201 messages_till_started_(messages_till_started), | 202 messages_till_started_(messages_till_started), |
202 action_(action), | 203 action_(action), |
203 async_(async) {} | 204 async_(async) {} |
204 FakeHostAuthenticatorFactory::~FakeHostAuthenticatorFactory() {} | 205 FakeHostAuthenticatorFactory::~FakeHostAuthenticatorFactory() {} |
205 | 206 |
206 scoped_ptr<Authenticator> FakeHostAuthenticatorFactory::CreateAuthenticator( | 207 std::unique_ptr<Authenticator> |
| 208 FakeHostAuthenticatorFactory::CreateAuthenticator( |
207 const std::string& local_jid, | 209 const std::string& local_jid, |
208 const std::string& remote_jid) { | 210 const std::string& remote_jid) { |
209 scoped_ptr<FakeAuthenticator> authenticator(new FakeAuthenticator( | 211 std::unique_ptr<FakeAuthenticator> authenticator(new FakeAuthenticator( |
210 FakeAuthenticator::HOST, round_trips_, action_, async_)); | 212 FakeAuthenticator::HOST, round_trips_, action_, async_)); |
211 authenticator->set_messages_till_started(messages_till_started_); | 213 authenticator->set_messages_till_started(messages_till_started_); |
212 return std::move(authenticator); | 214 return std::move(authenticator); |
213 } | 215 } |
214 | 216 |
215 } // namespace protocol | 217 } // namespace protocol |
216 } // namespace remoting | 218 } // namespace remoting |
OLD | NEW |