OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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. |
| 4 |
| 5 #include "remoting/protocol/fake_authenticator.h" |
| 6 |
| 7 #include "base/message_loop.h" |
| 8 #include "base/string_number_conversions.h" |
| 9 #include "net/socket/stream_socket.h" |
| 10 #include "remoting/base/constants.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" |
| 13 |
| 14 namespace remoting { |
| 15 namespace protocol { |
| 16 |
| 17 FakeChannelAuthenticator::FakeChannelAuthenticator(bool accept, bool async) |
| 18 : accept_(accept), |
| 19 async_(async), |
| 20 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 21 } |
| 22 |
| 23 FakeChannelAuthenticator::~FakeChannelAuthenticator() { |
| 24 } |
| 25 |
| 26 void FakeChannelAuthenticator::SecureAndAuthenticate( |
| 27 net::StreamSocket* socket, const DoneCallback& done_callback) { |
| 28 net::Error error; |
| 29 |
| 30 if (accept_) { |
| 31 error = net::OK; |
| 32 } else { |
| 33 error = net::ERR_FAILED; |
| 34 delete socket; |
| 35 socket = NULL; |
| 36 } |
| 37 |
| 38 if (async_) { |
| 39 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 40 &FakeChannelAuthenticator::CallCallback, weak_factory_.GetWeakPtr(), |
| 41 done_callback, error, socket)); |
| 42 } else { |
| 43 done_callback.Run(error, socket); |
| 44 } |
| 45 } |
| 46 |
| 47 void FakeChannelAuthenticator::CallCallback( |
| 48 const DoneCallback& done_callback, |
| 49 net::Error error, |
| 50 net::StreamSocket* socket) { |
| 51 done_callback.Run(error, socket); |
| 52 } |
| 53 |
| 54 FakeAuthenticator::FakeAuthenticator( |
| 55 Type type, int round_trips, Action action, bool async) |
| 56 : type_(type), |
| 57 round_trips_(round_trips), |
| 58 action_(action), |
| 59 async_(async), |
| 60 messages_(0) { |
| 61 } |
| 62 |
| 63 FakeAuthenticator::~FakeAuthenticator() { |
| 64 } |
| 65 |
| 66 Authenticator::State FakeAuthenticator::state() const{ |
| 67 EXPECT_LE(messages_, round_trips_ * 2); |
| 68 if (messages_ >= round_trips_ * 2) { |
| 69 if (action_ == REJECT) { |
| 70 return REJECTED; |
| 71 } else { |
| 72 return ACCEPTED; |
| 73 } |
| 74 } |
| 75 |
| 76 // Don't send the last message if this is a host that wants to |
| 77 // reject a connection. |
| 78 if (messages_ == round_trips_ * 2 - 1 && |
| 79 type_ == HOST && action_ == REJECT) { |
| 80 return REJECTED; |
| 81 } |
| 82 |
| 83 // We are not done yet. process next message. |
| 84 if ((messages_ % 2 == 0 && type_ == CLIENT) || |
| 85 (messages_ % 2 == 1 && type_ == HOST)) { |
| 86 return MESSAGE_READY; |
| 87 } else { |
| 88 return WAITING_MESSAGE; |
| 89 } |
| 90 } |
| 91 |
| 92 void FakeAuthenticator::ProcessMessage(const buzz::XmlElement* message) { |
| 93 EXPECT_EQ(WAITING_MESSAGE, state()); |
| 94 std::string id = |
| 95 message->TextNamed(buzz::QName(kChromotingXmlNamespace, "id")); |
| 96 EXPECT_EQ(id, base::IntToString(messages_)); |
| 97 ++messages_; |
| 98 } |
| 99 |
| 100 buzz::XmlElement* FakeAuthenticator::GetNextMessage() { |
| 101 EXPECT_EQ(MESSAGE_READY, state()); |
| 102 |
| 103 buzz::XmlElement* result = new buzz::XmlElement( |
| 104 buzz::QName(kChromotingXmlNamespace, "authentication")); |
| 105 buzz::XmlElement* id = new buzz::XmlElement( |
| 106 buzz::QName(kChromotingXmlNamespace, "id")); |
| 107 id->AddText(base::IntToString(messages_)); |
| 108 result->AddElement(id); |
| 109 |
| 110 ++messages_; |
| 111 return result; |
| 112 } |
| 113 |
| 114 ChannelAuthenticator* |
| 115 FakeAuthenticator::CreateChannelAuthenticator() const { |
| 116 EXPECT_EQ(ACCEPTED, state()); |
| 117 return new FakeChannelAuthenticator(action_ != REJECT_CHANNEL, async_); |
| 118 } |
| 119 |
| 120 FakeHostAuthenticatorFactory::FakeHostAuthenticatorFactory( |
| 121 int round_trips, FakeAuthenticator::Action action, bool async) |
| 122 : round_trips_(round_trips), |
| 123 action_(action), async_(async) { |
| 124 } |
| 125 |
| 126 FakeHostAuthenticatorFactory::~FakeHostAuthenticatorFactory() { |
| 127 } |
| 128 |
| 129 Authenticator* FakeHostAuthenticatorFactory::CreateAuthenticator( |
| 130 const std::string& remote_jid, |
| 131 const buzz::XmlElement* first_message) { |
| 132 return new FakeAuthenticator(FakeAuthenticator::HOST, round_trips_, |
| 133 action_, async_); |
| 134 } |
| 135 |
| 136 } // namespace protocol |
| 137 } // namespace remoting |
OLD | NEW |