| 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/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "net/base/io_buffer.h" | 9 #include "net/base/io_buffer.h" |
| 10 #include "net/base/net_errors.h" |
| 10 #include "net/socket/stream_socket.h" | 11 #include "net/socket/stream_socket.h" |
| 11 #include "remoting/base/constants.h" | 12 #include "remoting/base/constants.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" | 14 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h" |
| 14 | 15 |
| 15 namespace remoting { | 16 namespace remoting { |
| 16 namespace protocol { | 17 namespace protocol { |
| 17 | 18 |
| 18 FakeChannelAuthenticator::FakeChannelAuthenticator(bool accept, bool async) | 19 FakeChannelAuthenticator::FakeChannelAuthenticator(bool accept, bool async) |
| 19 : result_(accept ? net::OK : net::ERR_FAILED), | 20 : result_(accept ? net::OK : net::ERR_FAILED), |
| 20 async_(async), | 21 async_(async), |
| 21 did_read_bytes_(false), | 22 did_read_bytes_(false), |
| 22 did_write_bytes_(false), | 23 did_write_bytes_(false), |
| 23 weak_factory_(this) { | 24 weak_factory_(this) { |
| 24 } | 25 } |
| 25 | 26 |
| 26 FakeChannelAuthenticator::~FakeChannelAuthenticator() { | 27 FakeChannelAuthenticator::~FakeChannelAuthenticator() { |
| 27 } | 28 } |
| 28 | 29 |
| 29 void FakeChannelAuthenticator::SecureAndAuthenticate( | 30 void FakeChannelAuthenticator::SecureAndAuthenticate( |
| 30 scoped_ptr<net::StreamSocket> socket, | 31 scoped_ptr<net::StreamSocket> socket, |
| 31 const DoneCallback& done_callback) { | 32 const DoneCallback& done_callback) { |
| 32 socket_ = socket.Pass(); | 33 socket_ = socket.Pass(); |
| 33 | 34 |
| 34 if (async_) { | 35 if (async_) { |
| 35 done_callback_ = done_callback; | 36 done_callback_ = done_callback; |
| 36 | 37 |
| 37 scoped_refptr<net::IOBuffer> write_buf = new net::IOBuffer(1); | 38 if (result_ != net::OK) { |
| 38 write_buf->data()[0] = 0; | 39 // Don't write anything if we are going to reject auth to make test |
| 39 int result = | 40 // ordering deterministic. |
| 40 socket_->Write(write_buf.get(), | 41 did_write_bytes_ = true; |
| 41 1, | 42 } else { |
| 42 base::Bind(&FakeChannelAuthenticator::OnAuthBytesWritten, | 43 scoped_refptr<net::IOBuffer> write_buf = new net::IOBuffer(1); |
| 43 weak_factory_.GetWeakPtr())); | 44 write_buf->data()[0] = 0; |
| 44 if (result != net::ERR_IO_PENDING) { | 45 int result = socket_->Write( |
| 45 // This will not call the callback because |did_read_bytes_| is | 46 write_buf.get(), 1, |
| 46 // still set to false. | 47 base::Bind(&FakeChannelAuthenticator::OnAuthBytesWritten, |
| 47 OnAuthBytesWritten(result); | 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); |
| 53 } |
| 48 } | 54 } |
| 49 | 55 |
| 50 scoped_refptr<net::IOBuffer> read_buf = new net::IOBuffer(1); | 56 scoped_refptr<net::IOBuffer> read_buf = new net::IOBuffer(1); |
| 51 result = | 57 int result = |
| 52 socket_->Read(read_buf.get(), | 58 socket_->Read(read_buf.get(), 1, |
| 53 1, | |
| 54 base::Bind(&FakeChannelAuthenticator::OnAuthBytesRead, | 59 base::Bind(&FakeChannelAuthenticator::OnAuthBytesRead, |
| 55 weak_factory_.GetWeakPtr())); | 60 weak_factory_.GetWeakPtr())); |
| 56 if (result != net::ERR_IO_PENDING) | 61 if (result != net::ERR_IO_PENDING) |
| 57 OnAuthBytesRead(result); | 62 OnAuthBytesRead(result); |
| 58 } else { | 63 } else { |
| 59 if (result_ != net::OK) | 64 CallDoneCallback(); |
| 60 socket_.reset(); | |
| 61 done_callback.Run(result_, socket_.Pass()); | |
| 62 } | 65 } |
| 63 } | 66 } |
| 64 | 67 |
| 65 void FakeChannelAuthenticator::OnAuthBytesWritten(int result) { | 68 void FakeChannelAuthenticator::OnAuthBytesWritten(int result) { |
| 66 EXPECT_EQ(1, result); | 69 EXPECT_EQ(1, result); |
| 67 EXPECT_FALSE(did_write_bytes_); | 70 EXPECT_FALSE(did_write_bytes_); |
| 68 did_write_bytes_ = true; | 71 did_write_bytes_ = true; |
| 69 if (did_read_bytes_) | 72 if (did_read_bytes_) |
| 70 done_callback_.Run(result_, socket_.Pass()); | 73 CallDoneCallback(); |
| 71 } | 74 } |
| 72 | 75 |
| 73 void FakeChannelAuthenticator::OnAuthBytesRead(int result) { | 76 void FakeChannelAuthenticator::OnAuthBytesRead(int result) { |
| 74 EXPECT_EQ(1, result); | 77 EXPECT_EQ(1, result); |
| 75 EXPECT_FALSE(did_read_bytes_); | 78 EXPECT_FALSE(did_read_bytes_); |
| 76 did_read_bytes_ = true; | 79 did_read_bytes_ = true; |
| 77 if (did_write_bytes_) | 80 if (did_write_bytes_) |
| 78 done_callback_.Run(result_, socket_.Pass()); | 81 CallDoneCallback(); |
| 82 } |
| 83 |
| 84 void FakeChannelAuthenticator::CallDoneCallback() { |
| 85 DoneCallback callback = done_callback_; |
| 86 done_callback_.Reset(); |
| 87 if (result_ != net::OK) |
| 88 socket_.reset(); |
| 89 callback.Run(result_, socket_.Pass()); |
| 79 } | 90 } |
| 80 | 91 |
| 81 FakeAuthenticator::FakeAuthenticator( | 92 FakeAuthenticator::FakeAuthenticator( |
| 82 Type type, int round_trips, Action action, bool async) | 93 Type type, int round_trips, Action action, bool async) |
| 83 : type_(type), | 94 : type_(type), |
| 84 round_trips_(round_trips), | 95 round_trips_(round_trips), |
| 85 action_(action), | 96 action_(action), |
| 86 async_(async), | 97 async_(async), |
| 87 messages_(0), | 98 messages_(0), |
| 88 messages_till_started_(0) { | 99 messages_till_started_(0) { |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 FakeAuthenticator* authenticator = new FakeAuthenticator( | 190 FakeAuthenticator* authenticator = new FakeAuthenticator( |
| 180 FakeAuthenticator::HOST, round_trips_, action_, async_); | 191 FakeAuthenticator::HOST, round_trips_, action_, async_); |
| 181 authenticator->set_messages_till_started(messages_till_started_); | 192 authenticator->set_messages_till_started(messages_till_started_); |
| 182 | 193 |
| 183 scoped_ptr<Authenticator> result(authenticator); | 194 scoped_ptr<Authenticator> result(authenticator); |
| 184 return result.Pass(); | 195 return result.Pass(); |
| 185 } | 196 } |
| 186 | 197 |
| 187 } // namespace protocol | 198 } // namespace protocol |
| 188 } // namespace remoting | 199 } // namespace remoting |
| OLD | NEW |