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