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 <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 if (did_write_bytes_) | 82 if (did_write_bytes_) |
83 CallDoneCallback(); | 83 CallDoneCallback(); |
84 } | 84 } |
85 | 85 |
86 void FakeChannelAuthenticator::CallDoneCallback() { | 86 void FakeChannelAuthenticator::CallDoneCallback() { |
87 if (result_ != net::OK) | 87 if (result_ != net::OK) |
88 socket_.reset(); | 88 socket_.reset(); |
89 base::ResetAndReturn(&done_callback_).Run(result_, std::move(socket_)); | 89 base::ResetAndReturn(&done_callback_).Run(result_, std::move(socket_)); |
90 } | 90 } |
91 | 91 |
| 92 FakeAuthenticator::Config::Config() {} |
| 93 FakeAuthenticator::Config::Config(Action action) : action(action) {} |
| 94 FakeAuthenticator::Config::Config(int round_trips, Action action, bool async) |
| 95 : round_trips(round_trips), action(action), async(async) {} |
| 96 |
92 FakeAuthenticator::FakeAuthenticator(Type type, | 97 FakeAuthenticator::FakeAuthenticator(Type type, |
93 int round_trips, | 98 FakeAuthenticator::Config config, |
94 Action action, | 99 const std::string& local_id, |
95 bool async) | 100 const std::string& remote_id) |
96 : type_(type), round_trips_(round_trips), action_(action), async_(async) {} | 101 : type_(type), config_(config), local_id_(local_id), remote_id_(remote_id) { |
| 102 EXPECT_TRUE((!local_id_.empty() && !remote_id_.empty()) || |
| 103 config.round_trips == 0); |
| 104 } |
| 105 |
| 106 FakeAuthenticator::FakeAuthenticator(Action action) |
| 107 : FakeAuthenticator(CLIENT, |
| 108 FakeAuthenticator::Config(0, action, true), |
| 109 std::string(), |
| 110 std::string()) {} |
97 | 111 |
98 FakeAuthenticator::~FakeAuthenticator() {} | 112 FakeAuthenticator::~FakeAuthenticator() {} |
99 | 113 |
100 void FakeAuthenticator::set_messages_till_started(int messages) { | 114 void FakeAuthenticator::set_messages_till_started(int messages) { |
101 messages_till_started_ = messages; | 115 messages_till_started_ = messages; |
102 } | 116 } |
103 | 117 |
104 void FakeAuthenticator::Resume() { | 118 void FakeAuthenticator::Resume() { |
105 base::ResetAndReturn(&resume_closure_).Run(); | 119 base::ResetAndReturn(&resume_closure_).Run(); |
106 } | 120 } |
107 | 121 |
108 Authenticator::State FakeAuthenticator::state() const { | 122 Authenticator::State FakeAuthenticator::state() const { |
109 EXPECT_LE(messages_, round_trips_ * 2); | 123 EXPECT_LE(messages_, config_.round_trips * 2); |
110 | 124 |
111 if (messages_ == pause_message_index_ && !resume_closure_.is_null()) | 125 if (messages_ == pause_message_index_ && !resume_closure_.is_null()) |
112 return PROCESSING_MESSAGE; | 126 return PROCESSING_MESSAGE; |
113 | 127 |
114 if (messages_ >= round_trips_ * 2) { | 128 if (messages_ >= config_.round_trips * 2) { |
115 if (action_ == REJECT) { | 129 if (config_.action == REJECT) { |
116 return REJECTED; | 130 return REJECTED; |
117 } else { | 131 } else { |
118 return ACCEPTED; | 132 return ACCEPTED; |
119 } | 133 } |
120 } | 134 } |
121 | 135 |
122 // Don't send the last message if this is a host that wants to | 136 // Don't send the last message if this is a host that wants to |
123 // reject a connection. | 137 // reject a connection. |
124 if (messages_ == round_trips_ * 2 - 1 && | 138 if (messages_ == config_.round_trips * 2 - 1 && type_ == HOST && |
125 type_ == HOST && action_ == REJECT) { | 139 config_.action == REJECT) { |
126 return REJECTED; | 140 return REJECTED; |
127 } | 141 } |
128 | 142 |
129 // We are not done yet. process next message. | 143 // We are not done yet. process next message. |
130 if ((messages_ % 2 == 0 && type_ == CLIENT) || | 144 if ((messages_ % 2 == 0 && type_ == CLIENT) || |
131 (messages_ % 2 == 1 && type_ == HOST)) { | 145 (messages_ % 2 == 1 && type_ == HOST)) { |
132 return MESSAGE_READY; | 146 return MESSAGE_READY; |
133 } else { | 147 } else { |
134 return WAITING_MESSAGE; | 148 return WAITING_MESSAGE; |
135 } | 149 } |
136 } | 150 } |
137 | 151 |
138 bool FakeAuthenticator::started() const { | 152 bool FakeAuthenticator::started() const { |
139 return messages_ > messages_till_started_; | 153 return messages_ > messages_till_started_; |
140 } | 154 } |
141 | 155 |
142 Authenticator::RejectionReason FakeAuthenticator::rejection_reason() const { | 156 Authenticator::RejectionReason FakeAuthenticator::rejection_reason() const { |
143 EXPECT_EQ(REJECTED, state()); | 157 EXPECT_EQ(REJECTED, state()); |
144 return INVALID_CREDENTIALS; | 158 return INVALID_CREDENTIALS; |
145 } | 159 } |
146 | 160 |
147 void FakeAuthenticator::ProcessMessage(const buzz::XmlElement* message, | 161 void FakeAuthenticator::ProcessMessage(const buzz::XmlElement* message, |
148 const base::Closure& resume_callback) { | 162 const base::Closure& resume_callback) { |
149 EXPECT_EQ(WAITING_MESSAGE, state()); | 163 EXPECT_EQ(WAITING_MESSAGE, state()); |
150 std::string id = | 164 std::string id = |
151 message->TextNamed(buzz::QName(kChromotingXmlNamespace, "id")); | 165 message->TextNamed(buzz::QName(kChromotingXmlNamespace, "id")); |
152 EXPECT_EQ(id, base::IntToString(messages_)); | 166 EXPECT_EQ(id, base::IntToString(messages_)); |
153 | 167 |
154 // On the client receive the key in the last message. | 168 // On the client receive the key in the last message. |
155 if (type_ == CLIENT && messages_ == round_trips_ * 2 - 1) { | 169 if (type_ == CLIENT && messages_ == config_.round_trips * 2 - 1) { |
156 std::string key_base64 = | 170 std::string key_base64 = |
157 message->TextNamed(buzz::QName(kChromotingXmlNamespace, "key")); | 171 message->TextNamed(buzz::QName(kChromotingXmlNamespace, "key")); |
158 EXPECT_TRUE(!key_base64.empty()); | 172 EXPECT_TRUE(!key_base64.empty()); |
159 EXPECT_TRUE(base::Base64Decode(key_base64, &auth_key_)); | 173 EXPECT_TRUE(base::Base64Decode(key_base64, &auth_key_)); |
160 } | 174 } |
161 | 175 |
| 176 // Receive peer's id. |
| 177 if (messages_ < 2) { |
| 178 EXPECT_EQ(remote_id_, message->Attr(buzz::QName("", "id"))); |
| 179 } |
| 180 |
162 ++messages_; | 181 ++messages_; |
163 if (messages_ == pause_message_index_) { | 182 if (messages_ == pause_message_index_) { |
164 resume_closure_ = resume_callback; | 183 resume_closure_ = resume_callback; |
165 return; | 184 return; |
166 } | 185 } |
167 resume_callback.Run(); | 186 resume_callback.Run(); |
168 } | 187 } |
169 | 188 |
170 std::unique_ptr<buzz::XmlElement> FakeAuthenticator::GetNextMessage() { | 189 std::unique_ptr<buzz::XmlElement> FakeAuthenticator::GetNextMessage() { |
171 EXPECT_EQ(MESSAGE_READY, state()); | 190 EXPECT_EQ(MESSAGE_READY, state()); |
172 | 191 |
173 std::unique_ptr<buzz::XmlElement> result(new buzz::XmlElement( | 192 std::unique_ptr<buzz::XmlElement> result(new buzz::XmlElement( |
174 buzz::QName(kChromotingXmlNamespace, "authentication"))); | 193 buzz::QName(kChromotingXmlNamespace, "authentication"))); |
175 buzz::XmlElement* id = new buzz::XmlElement( | 194 buzz::XmlElement* id = new buzz::XmlElement( |
176 buzz::QName(kChromotingXmlNamespace, "id")); | 195 buzz::QName(kChromotingXmlNamespace, "id")); |
177 id->AddText(base::IntToString(messages_)); | 196 id->AddText(base::IntToString(messages_)); |
178 result->AddElement(id); | 197 result->AddElement(id); |
179 | 198 |
| 199 // Send local id in the first outgoing message. |
| 200 if (messages_ < 2) { |
| 201 result->AddAttr(buzz::QName("", "id"), local_id_); |
| 202 } |
| 203 |
180 // Add authentication key in the last message sent from host to client. | 204 // Add authentication key in the last message sent from host to client. |
181 if (type_ == HOST && messages_ == round_trips_ * 2 - 1) { | 205 if (type_ == HOST && messages_ == config_.round_trips * 2 - 1) { |
182 auth_key_ = base::RandBytesAsString(16); | 206 auth_key_ = base::RandBytesAsString(16); |
183 buzz::XmlElement* key = new buzz::XmlElement( | 207 buzz::XmlElement* key = new buzz::XmlElement( |
184 buzz::QName(kChromotingXmlNamespace, "key")); | 208 buzz::QName(kChromotingXmlNamespace, "key")); |
185 std::string key_base64; | 209 std::string key_base64; |
186 base::Base64Encode(auth_key_, &key_base64); | 210 base::Base64Encode(auth_key_, &key_base64); |
187 key->AddText(key_base64); | 211 key->AddText(key_base64); |
188 result->AddElement(key); | 212 result->AddElement(key); |
189 } | 213 } |
190 | 214 |
191 ++messages_; | 215 ++messages_; |
192 return result; | 216 return result; |
193 } | 217 } |
194 | 218 |
195 const std::string& FakeAuthenticator::GetAuthKey() const { | 219 const std::string& FakeAuthenticator::GetAuthKey() const { |
196 EXPECT_EQ(ACCEPTED, state()); | 220 EXPECT_EQ(ACCEPTED, state()); |
197 DCHECK(!auth_key_.empty()); | 221 DCHECK(!auth_key_.empty()); |
198 return auth_key_; | 222 return auth_key_; |
199 } | 223 } |
200 | 224 |
201 std::unique_ptr<ChannelAuthenticator> | 225 std::unique_ptr<ChannelAuthenticator> |
202 FakeAuthenticator::CreateChannelAuthenticator() const { | 226 FakeAuthenticator::CreateChannelAuthenticator() const { |
203 EXPECT_EQ(ACCEPTED, state()); | 227 EXPECT_EQ(ACCEPTED, state()); |
204 return base::MakeUnique<FakeChannelAuthenticator>(action_ != REJECT_CHANNEL, | 228 return base::MakeUnique<FakeChannelAuthenticator>( |
205 async_); | 229 config_.action != REJECT_CHANNEL, config_.async); |
206 } | 230 } |
207 | 231 |
208 FakeHostAuthenticatorFactory::FakeHostAuthenticatorFactory( | 232 FakeHostAuthenticatorFactory::FakeHostAuthenticatorFactory( |
209 int round_trips, | |
210 int messages_till_started, | 233 int messages_till_started, |
211 FakeAuthenticator::Action action, | 234 FakeAuthenticator::Config config) |
212 bool async) | 235 : messages_till_started_(messages_till_started), config_(config) {} |
213 : round_trips_(round_trips), | |
214 messages_till_started_(messages_till_started), | |
215 action_(action), | |
216 async_(async) {} | |
217 FakeHostAuthenticatorFactory::~FakeHostAuthenticatorFactory() {} | 236 FakeHostAuthenticatorFactory::~FakeHostAuthenticatorFactory() {} |
218 | 237 |
219 std::unique_ptr<Authenticator> | 238 std::unique_ptr<Authenticator> |
220 FakeHostAuthenticatorFactory::CreateAuthenticator( | 239 FakeHostAuthenticatorFactory::CreateAuthenticator( |
221 const std::string& local_jid, | 240 const std::string& local_jid, |
222 const std::string& remote_jid) { | 241 const std::string& remote_jid) { |
223 std::unique_ptr<FakeAuthenticator> authenticator(new FakeAuthenticator( | 242 std::unique_ptr<FakeAuthenticator> authenticator(new FakeAuthenticator( |
224 FakeAuthenticator::HOST, round_trips_, action_, async_)); | 243 FakeAuthenticator::HOST, config_, local_jid, remote_jid)); |
225 authenticator->set_messages_till_started(messages_till_started_); | 244 authenticator->set_messages_till_started(messages_till_started_); |
226 return std::move(authenticator); | 245 return std::move(authenticator); |
227 } | 246 } |
228 | 247 |
229 } // namespace protocol | 248 } // namespace protocol |
230 } // namespace remoting | 249 } // namespace remoting |
OLD | NEW |