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