| 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 "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
| 9 #include "net/base/io_buffer.h" |
| 9 #include "net/socket/stream_socket.h" | 10 #include "net/socket/stream_socket.h" |
| 10 #include "remoting/base/constants.h" | 11 #include "remoting/base/constants.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" | 13 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" |
| 13 | 14 |
| 14 namespace remoting { | 15 namespace remoting { |
| 15 namespace protocol { | 16 namespace protocol { |
| 16 | 17 |
| 17 FakeChannelAuthenticator::FakeChannelAuthenticator(bool accept, bool async) | 18 FakeChannelAuthenticator::FakeChannelAuthenticator(bool accept, bool async) |
| 18 : accept_(accept), | 19 : result_(accept ? net::OK : net::ERR_FAILED), |
| 19 async_(async), | 20 async_(async), |
| 21 did_read_bytes_(false), |
| 22 did_write_bytes_(false), |
| 20 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 23 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 21 } | 24 } |
| 22 | 25 |
| 23 FakeChannelAuthenticator::~FakeChannelAuthenticator() { | 26 FakeChannelAuthenticator::~FakeChannelAuthenticator() { |
| 24 } | 27 } |
| 25 | 28 |
| 26 void FakeChannelAuthenticator::SecureAndAuthenticate( | 29 void FakeChannelAuthenticator::SecureAndAuthenticate( |
| 27 scoped_ptr<net::StreamSocket> socket, | 30 scoped_ptr<net::StreamSocket> socket, |
| 28 const DoneCallback& done_callback) { | 31 const DoneCallback& done_callback) { |
| 29 net::Error error; | 32 socket_ = socket.Pass(); |
| 30 | |
| 31 if (accept_) { | |
| 32 error = net::OK; | |
| 33 } else { | |
| 34 error = net::ERR_FAILED; | |
| 35 socket.reset(); | |
| 36 } | |
| 37 | 33 |
| 38 if (async_) { | 34 if (async_) { |
| 39 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | 35 done_callback_ = done_callback; |
| 40 &FakeChannelAuthenticator::CallCallback, weak_factory_.GetWeakPtr(), | 36 |
| 41 done_callback, error, base::Passed(&socket))); | 37 scoped_refptr<net::IOBuffer> write_buf = new net::IOBuffer(1); |
| 38 write_buf->data()[0] = 0; |
| 39 int result = socket_->Write( |
| 40 write_buf, 1, base::Bind(&FakeChannelAuthenticator::OnAuthBytesWritten, |
| 41 weak_factory_.GetWeakPtr())); |
| 42 if (result != net::ERR_IO_PENDING) { |
| 43 // This will not call the callback because |did_read_bytes_| is |
| 44 // still set to false. |
| 45 OnAuthBytesWritten(result); |
| 46 } |
| 47 |
| 48 scoped_refptr<net::IOBuffer> read_buf = new net::IOBuffer(1); |
| 49 result = socket_->Read( |
| 50 read_buf, 1, base::Bind(&FakeChannelAuthenticator::OnAuthBytesRead, |
| 51 weak_factory_.GetWeakPtr())); |
| 52 if (result != net::ERR_IO_PENDING) |
| 53 OnAuthBytesRead(result); |
| 42 } else { | 54 } else { |
| 43 done_callback.Run(error, socket.Pass()); | 55 if (result_ != net::OK) |
| 56 socket_.reset(); |
| 57 done_callback.Run(result_, socket_.Pass()); |
| 44 } | 58 } |
| 45 } | 59 } |
| 46 | 60 |
| 47 void FakeChannelAuthenticator::CallCallback( | 61 void FakeChannelAuthenticator::OnAuthBytesWritten(int result) { |
| 48 const DoneCallback& done_callback, | 62 EXPECT_EQ(1, result); |
| 49 net::Error error, | 63 EXPECT_FALSE(did_write_bytes_); |
| 50 scoped_ptr<net::StreamSocket> socket) { | 64 did_write_bytes_ = true; |
| 51 done_callback.Run(error, socket.Pass()); | 65 if (did_read_bytes_) |
| 66 done_callback_.Run(result_, socket_.Pass()); |
| 67 } |
| 68 |
| 69 void FakeChannelAuthenticator::OnAuthBytesRead(int result) { |
| 70 EXPECT_EQ(1, result); |
| 71 EXPECT_FALSE(did_read_bytes_); |
| 72 did_read_bytes_ = true; |
| 73 if (did_write_bytes_) |
| 74 done_callback_.Run(result_, socket_.Pass()); |
| 52 } | 75 } |
| 53 | 76 |
| 54 FakeAuthenticator::FakeAuthenticator( | 77 FakeAuthenticator::FakeAuthenticator( |
| 55 Type type, int round_trips, Action action, bool async) | 78 Type type, int round_trips, Action action, bool async) |
| 56 : type_(type), | 79 : type_(type), |
| 57 round_trips_(round_trips), | 80 round_trips_(round_trips), |
| 58 action_(action), | 81 action_(action), |
| 59 async_(async), | 82 async_(async), |
| 60 messages_(0) { | 83 messages_(0) { |
| 61 } | 84 } |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 | 157 |
| 135 scoped_ptr<Authenticator> FakeHostAuthenticatorFactory::CreateAuthenticator( | 158 scoped_ptr<Authenticator> FakeHostAuthenticatorFactory::CreateAuthenticator( |
| 136 const std::string& remote_jid, | 159 const std::string& remote_jid, |
| 137 const buzz::XmlElement* first_message) { | 160 const buzz::XmlElement* first_message) { |
| 138 return scoped_ptr<Authenticator>(new FakeAuthenticator( | 161 return scoped_ptr<Authenticator>(new FakeAuthenticator( |
| 139 FakeAuthenticator::HOST, round_trips_, action_, async_)); | 162 FakeAuthenticator::HOST, round_trips_, action_, async_)); |
| 140 } | 163 } |
| 141 | 164 |
| 142 } // namespace protocol | 165 } // namespace protocol |
| 143 } // namespace remoting | 166 } // namespace remoting |
| OLD | NEW |