OLD | NEW |
(Empty) | |
| 1 // Copyright 2004 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 <iostream> |
| 6 #include <memory> |
| 7 #include <sstream> |
| 8 #include <string> |
| 9 |
| 10 #include "third_party/libjingle_xmpp/xmllite/xmlelement.h" |
| 11 #include "third_party/libjingle_xmpp/xmpp/constants.h" |
| 12 #include "third_party/libjingle_xmpp/xmpp/plainsaslhandler.h" |
| 13 #include "third_party/libjingle_xmpp/xmpp/saslplainmechanism.h" |
| 14 #include "third_party/libjingle_xmpp/xmpp/util_unittest.h" |
| 15 #include "third_party/libjingle_xmpp/xmpp/xmppengine.h" |
| 16 #include "webrtc/base/common.h" |
| 17 #include "webrtc/base/cryptstring.h" |
| 18 #include "webrtc/base/gunit.h" |
| 19 #include "webrtc/typedefs.h" |
| 20 |
| 21 using buzz::Jid; |
| 22 using buzz::QName; |
| 23 using buzz::XmlElement; |
| 24 using buzz::XmppEngine; |
| 25 using buzz::XmppTestHandler; |
| 26 |
| 27 enum XlttStage { |
| 28 XLTT_STAGE_CONNECT = 0, |
| 29 XLTT_STAGE_STREAMSTART, |
| 30 XLTT_STAGE_TLS_FEATURES, |
| 31 XLTT_STAGE_TLS_PROCEED, |
| 32 XLTT_STAGE_ENCRYPTED_START, |
| 33 XLTT_STAGE_AUTH_FEATURES, |
| 34 XLTT_STAGE_AUTH_SUCCESS, |
| 35 XLTT_STAGE_AUTHENTICATED_START, |
| 36 XLTT_STAGE_BIND_FEATURES, |
| 37 XLTT_STAGE_BIND_SUCCESS, |
| 38 XLTT_STAGE_SESSION_SUCCESS, |
| 39 }; |
| 40 |
| 41 class XmppLoginTaskTest : public testing::Test { |
| 42 public: |
| 43 XmppEngine* engine() { return engine_.get(); } |
| 44 XmppTestHandler* handler() { return handler_.get(); } |
| 45 virtual void SetUp() { |
| 46 engine_.reset(XmppEngine::Create()); |
| 47 handler_.reset(new XmppTestHandler(engine_.get())); |
| 48 |
| 49 Jid jid("david@my-server"); |
| 50 rtc::InsecureCryptStringImpl pass; |
| 51 pass.password() = "david"; |
| 52 engine_->SetSessionHandler(handler_.get()); |
| 53 engine_->SetOutputHandler(handler_.get()); |
| 54 engine_->AddStanzaHandler(handler_.get()); |
| 55 engine_->SetUser(jid); |
| 56 engine_->SetSaslHandler( |
| 57 new buzz::PlainSaslHandler(jid, rtc::CryptString(pass), true)); |
| 58 } |
| 59 virtual void TearDown() { |
| 60 handler_.reset(); |
| 61 engine_.reset(); |
| 62 } |
| 63 void RunPartialLogin(XlttStage startstage, XlttStage endstage); |
| 64 void SetTlsOptions(buzz::TlsOptions option); |
| 65 |
| 66 private: |
| 67 std::unique_ptr<XmppEngine> engine_; |
| 68 std::unique_ptr<XmppTestHandler> handler_; |
| 69 }; |
| 70 |
| 71 void XmppLoginTaskTest::SetTlsOptions(buzz::TlsOptions option) { |
| 72 engine_->SetTls(option); |
| 73 } |
| 74 void XmppLoginTaskTest::RunPartialLogin(XlttStage startstage, |
| 75 XlttStage endstage) { |
| 76 std::string input; |
| 77 |
| 78 switch (startstage) { |
| 79 case XLTT_STAGE_CONNECT: { |
| 80 engine_->Connect(); |
| 81 XmlElement appStanza(QName("test", "app-stanza")); |
| 82 appStanza.AddText("this-is-a-test"); |
| 83 engine_->SendStanza(&appStanza); |
| 84 |
| 85 EXPECT_EQ("<stream:stream to=\"my-server\" xml:lang=\"*\" " |
| 86 "version=\"1.0\" xmlns:stream=\"http://etherx.jabber.org/streams\" " |
| 87 "xmlns=\"jabber:client\">\r\n", handler_->OutputActivity()); |
| 88 EXPECT_EQ("[OPENING]", handler_->SessionActivity()); |
| 89 EXPECT_EQ("", handler_->StanzaActivity()); |
| 90 if (endstage == XLTT_STAGE_CONNECT) |
| 91 return; |
| 92 FALLTHROUGH(); |
| 93 } |
| 94 |
| 95 case XLTT_STAGE_STREAMSTART: { |
| 96 input = "<stream:stream id=\"a5f2d8c9\" version=\"1.0\" " |
| 97 "xmlns:stream=\"http://etherx.jabber.org/streams\" " |
| 98 "xmlns=\"jabber:client\">"; |
| 99 engine_->HandleInput(input.c_str(), input.length()); |
| 100 EXPECT_EQ("", handler_->StanzaActivity()); |
| 101 EXPECT_EQ("", handler_->SessionActivity()); |
| 102 EXPECT_EQ("", handler_->OutputActivity()); |
| 103 if (endstage == XLTT_STAGE_STREAMSTART) |
| 104 return; |
| 105 FALLTHROUGH(); |
| 106 } |
| 107 |
| 108 case XLTT_STAGE_TLS_FEATURES: { |
| 109 input = "<stream:features>" |
| 110 "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>" |
| 111 "</stream:features>"; |
| 112 engine_->HandleInput(input.c_str(), input.length()); |
| 113 EXPECT_EQ("<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"/>", |
| 114 handler_->OutputActivity()); |
| 115 EXPECT_EQ("", handler_->StanzaActivity()); |
| 116 EXPECT_EQ("", handler_->SessionActivity()); |
| 117 if (endstage == XLTT_STAGE_TLS_FEATURES) |
| 118 return; |
| 119 FALLTHROUGH(); |
| 120 } |
| 121 |
| 122 case XLTT_STAGE_TLS_PROCEED: { |
| 123 input = std::string("<proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>"); |
| 124 engine_->HandleInput(input.c_str(), input.length()); |
| 125 EXPECT_EQ("[START-TLS my-server]" |
| 126 "<stream:stream to=\"my-server\" xml:lang=\"*\" " |
| 127 "version=\"1.0\" xmlns:stream=\"http://etherx.jabber.org/streams\" " |
| 128 "xmlns=\"jabber:client\">\r\n", handler_->OutputActivity()); |
| 129 EXPECT_EQ("", handler_->StanzaActivity()); |
| 130 EXPECT_EQ("", handler_->SessionActivity()); |
| 131 if (endstage == XLTT_STAGE_TLS_PROCEED) |
| 132 return; |
| 133 FALLTHROUGH(); |
| 134 } |
| 135 |
| 136 case XLTT_STAGE_ENCRYPTED_START: { |
| 137 input = std::string("<stream:stream id=\"01234567\" version=\"1.0\" " |
| 138 "xmlns:stream=\"http://etherx.jabber.org/streams\" " |
| 139 "xmlns=\"jabber:client\">"); |
| 140 engine_->HandleInput(input.c_str(), input.length()); |
| 141 EXPECT_EQ("", handler_->StanzaActivity()); |
| 142 EXPECT_EQ("", handler_->SessionActivity()); |
| 143 EXPECT_EQ("", handler_->OutputActivity()); |
| 144 if (endstage == XLTT_STAGE_ENCRYPTED_START) |
| 145 return; |
| 146 FALLTHROUGH(); |
| 147 } |
| 148 |
| 149 case XLTT_STAGE_AUTH_FEATURES: { |
| 150 input = "<stream:features>" |
| 151 "<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>" |
| 152 "<mechanism>DIGEST-MD5</mechanism>" |
| 153 "<mechanism>PLAIN</mechanism>" |
| 154 "</mechanisms>" |
| 155 "</stream:features>"; |
| 156 engine_->HandleInput(input.c_str(), input.length()); |
| 157 EXPECT_EQ("<auth xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\" " |
| 158 "mechanism=\"PLAIN\" " |
| 159 "auth:allow-non-google-login=\"true\" " |
| 160 "auth:client-uses-full-bind-result=\"true\" " |
| 161 "xmlns:auth=\"http://www.google.com/talk/protocol/auth\"" |
| 162 ">AGRhdmlkAGRhdmlk</auth>", |
| 163 handler_->OutputActivity()); |
| 164 EXPECT_EQ("", handler_->StanzaActivity()); |
| 165 EXPECT_EQ("", handler_->SessionActivity()); |
| 166 if (endstage == XLTT_STAGE_AUTH_FEATURES) |
| 167 return; |
| 168 FALLTHROUGH(); |
| 169 } |
| 170 |
| 171 case XLTT_STAGE_AUTH_SUCCESS: { |
| 172 input = "<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>"; |
| 173 engine_->HandleInput(input.c_str(), input.length()); |
| 174 EXPECT_EQ("<stream:stream to=\"my-server\" xml:lang=\"*\" " |
| 175 "version=\"1.0\" xmlns:stream=\"http://etherx.jabber.org/streams\" " |
| 176 "xmlns=\"jabber:client\">\r\n", handler_->OutputActivity()); |
| 177 EXPECT_EQ("", handler_->StanzaActivity()); |
| 178 EXPECT_EQ("", handler_->SessionActivity()); |
| 179 if (endstage == XLTT_STAGE_AUTH_SUCCESS) |
| 180 return; |
| 181 FALLTHROUGH(); |
| 182 } |
| 183 |
| 184 case XLTT_STAGE_AUTHENTICATED_START: { |
| 185 input = std::string("<stream:stream id=\"01234567\" version=\"1.0\" " |
| 186 "xmlns:stream=\"http://etherx.jabber.org/streams\" " |
| 187 "xmlns=\"jabber:client\">"); |
| 188 engine_->HandleInput(input.c_str(), input.length()); |
| 189 EXPECT_EQ("", handler_->StanzaActivity()); |
| 190 EXPECT_EQ("", handler_->SessionActivity()); |
| 191 EXPECT_EQ("", handler_->OutputActivity()); |
| 192 if (endstage == XLTT_STAGE_AUTHENTICATED_START) |
| 193 return; |
| 194 FALLTHROUGH(); |
| 195 } |
| 196 |
| 197 case XLTT_STAGE_BIND_FEATURES: { |
| 198 input = "<stream:features>" |
| 199 "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>" |
| 200 "<session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>" |
| 201 "</stream:features>"; |
| 202 engine_->HandleInput(input.c_str(), input.length()); |
| 203 EXPECT_EQ("<iq type=\"set\" id=\"0\">" |
| 204 "<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"/></iq>", |
| 205 handler_->OutputActivity()); |
| 206 EXPECT_EQ("", handler_->StanzaActivity()); |
| 207 EXPECT_EQ("", handler_->SessionActivity()); |
| 208 if (endstage == XLTT_STAGE_BIND_FEATURES) |
| 209 return; |
| 210 FALLTHROUGH(); |
| 211 } |
| 212 |
| 213 case XLTT_STAGE_BIND_SUCCESS: { |
| 214 input = "<iq type='result' id='0'>" |
| 215 "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>" |
| 216 "<jid>david@my-server/test</jid></bind></iq>"; |
| 217 engine_->HandleInput(input.c_str(), input.length()); |
| 218 EXPECT_EQ("<iq type=\"set\" id=\"1\">" |
| 219 "<session xmlns=\"urn:ietf:params:xml:ns:xmpp-session\"/></iq>", |
| 220 handler_->OutputActivity()); |
| 221 EXPECT_EQ("", handler_->StanzaActivity()); |
| 222 EXPECT_EQ("", handler_->SessionActivity()); |
| 223 if (endstage == XLTT_STAGE_BIND_SUCCESS) |
| 224 return; |
| 225 FALLTHROUGH(); |
| 226 } |
| 227 |
| 228 case XLTT_STAGE_SESSION_SUCCESS: { |
| 229 input = "<iq type='result' id='1'/>"; |
| 230 engine_->HandleInput(input.c_str(), input.length()); |
| 231 EXPECT_EQ("<test:app-stanza xmlns:test=\"test\">this-is-a-test" |
| 232 "</test:app-stanza>", handler_->OutputActivity()); |
| 233 EXPECT_EQ("[OPEN]", handler_->SessionActivity()); |
| 234 EXPECT_EQ("", handler_->StanzaActivity()); |
| 235 if (endstage == XLTT_STAGE_SESSION_SUCCESS) |
| 236 return; |
| 237 FALLTHROUGH(); |
| 238 } |
| 239 default: |
| 240 break; |
| 241 } |
| 242 } |
| 243 |
| 244 TEST_F(XmppLoginTaskTest, TestUtf8Good) { |
| 245 RunPartialLogin(XLTT_STAGE_CONNECT, XLTT_STAGE_CONNECT); |
| 246 |
| 247 std::string input = "<?xml version='1.0' encoding='UTF-8'?>" |
| 248 "<stream:stream id=\"a5f2d8c9\" version=\"1.0\" " |
| 249 "xmlns:stream=\"http://etherx.jabber.org/streams\" " |
| 250 "xmlns=\"jabber:client\">"; |
| 251 engine()->HandleInput(input.c_str(), input.length()); |
| 252 EXPECT_EQ("", handler()->OutputActivity()); |
| 253 EXPECT_EQ("", handler()->SessionActivity()); |
| 254 EXPECT_EQ("", handler()->StanzaActivity()); |
| 255 } |
| 256 |
| 257 TEST_F(XmppLoginTaskTest, TestNonUtf8Bad) { |
| 258 RunPartialLogin(XLTT_STAGE_CONNECT, XLTT_STAGE_CONNECT); |
| 259 |
| 260 std::string input = "<?xml version='1.0' encoding='ISO-8859-1'?>" |
| 261 "<stream:stream id=\"a5f2d8c9\" version=\"1.0\" " |
| 262 "xmlns:stream=\"http://etherx.jabber.org/streams\" " |
| 263 "xmlns=\"jabber:client\">"; |
| 264 engine()->HandleInput(input.c_str(), input.length()); |
| 265 EXPECT_EQ("[CLOSED]", handler()->OutputActivity()); |
| 266 EXPECT_EQ("[CLOSED][ERROR-XML]", handler()->SessionActivity()); |
| 267 EXPECT_EQ("", handler()->StanzaActivity()); |
| 268 } |
| 269 |
| 270 TEST_F(XmppLoginTaskTest, TestNoFeatures) { |
| 271 RunPartialLogin(XLTT_STAGE_CONNECT, XLTT_STAGE_STREAMSTART); |
| 272 |
| 273 std::string input = "<iq type='get' id='1'/>"; |
| 274 engine()->HandleInput(input.c_str(), input.length()); |
| 275 |
| 276 EXPECT_EQ("[CLOSED]", handler()->OutputActivity()); |
| 277 EXPECT_EQ("[CLOSED][ERROR-VERSION]", handler()->SessionActivity()); |
| 278 EXPECT_EQ("", handler()->StanzaActivity()); |
| 279 } |
| 280 |
| 281 TEST_F(XmppLoginTaskTest, TestTlsRequiredNotPresent) { |
| 282 RunPartialLogin(XLTT_STAGE_CONNECT, XLTT_STAGE_STREAMSTART); |
| 283 |
| 284 std::string input = "<stream:features>" |
| 285 "<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>" |
| 286 "<mechanism>DIGEST-MD5</mechanism>" |
| 287 "<mechanism>PLAIN</mechanism>" |
| 288 "</mechanisms>" |
| 289 "</stream:features>"; |
| 290 engine()->HandleInput(input.c_str(), input.length()); |
| 291 |
| 292 EXPECT_EQ("[CLOSED]", handler()->OutputActivity()); |
| 293 EXPECT_EQ("[CLOSED][ERROR-TLS]", handler()->SessionActivity()); |
| 294 EXPECT_EQ("", handler()->StanzaActivity()); |
| 295 } |
| 296 |
| 297 TEST_F(XmppLoginTaskTest, TestTlsRequeiredAndPresent) { |
| 298 RunPartialLogin(XLTT_STAGE_CONNECT, XLTT_STAGE_STREAMSTART); |
| 299 |
| 300 std::string input = "<stream:features>" |
| 301 "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'>" |
| 302 "<required/>" |
| 303 "</starttls>" |
| 304 "<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>" |
| 305 "<mechanism>X-GOOGLE-TOKEN</mechanism>" |
| 306 "<mechanism>PLAIN</mechanism>" |
| 307 "<mechanism>X-OAUTH2</mechanism>" |
| 308 "</mechanisms>" |
| 309 "</stream:features>"; |
| 310 engine()->HandleInput(input.c_str(), input.length()); |
| 311 |
| 312 EXPECT_EQ("<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"/>", |
| 313 handler()->OutputActivity()); |
| 314 EXPECT_EQ("", handler()->SessionActivity()); |
| 315 EXPECT_EQ("", handler()->StanzaActivity()); |
| 316 } |
| 317 |
| 318 TEST_F(XmppLoginTaskTest, TestTlsEnabledNotPresent) { |
| 319 SetTlsOptions(buzz::TLS_ENABLED); |
| 320 RunPartialLogin(XLTT_STAGE_CONNECT, XLTT_STAGE_STREAMSTART); |
| 321 |
| 322 std::string input = "<stream:features>" |
| 323 "<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>" |
| 324 "<mechanism>DIGEST-MD5</mechanism>" |
| 325 "<mechanism>PLAIN</mechanism>" |
| 326 "</mechanisms>" |
| 327 "</stream:features>"; |
| 328 engine()->HandleInput(input.c_str(), input.length()); |
| 329 |
| 330 EXPECT_EQ("<auth xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\" " |
| 331 "mechanism=\"PLAIN\" auth:allow-non-google-login=\"true\" " |
| 332 "auth:client-uses-full-bind-result=\"true\" " |
| 333 "xmlns:auth=\"http://www.google.com/talk/protocol/auth\"" |
| 334 ">AGRhdmlkAGRhdmlk</auth>", handler()->OutputActivity()); |
| 335 EXPECT_EQ("", handler()->SessionActivity()); |
| 336 EXPECT_EQ("", handler()->StanzaActivity()); |
| 337 } |
| 338 |
| 339 TEST_F(XmppLoginTaskTest, TestTlsEnabledAndPresent) { |
| 340 SetTlsOptions(buzz::TLS_ENABLED); |
| 341 RunPartialLogin(XLTT_STAGE_CONNECT, XLTT_STAGE_STREAMSTART); |
| 342 |
| 343 std::string input = "<stream:features>" |
| 344 "<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>" |
| 345 "<mechanism>X-GOOGLE-TOKEN</mechanism>" |
| 346 "<mechanism>PLAIN</mechanism>" |
| 347 "<mechanism>X-OAUTH2</mechanism>" |
| 348 "</mechanisms>" |
| 349 "</stream:features>"; |
| 350 engine()->HandleInput(input.c_str(), input.length()); |
| 351 |
| 352 EXPECT_EQ("<auth xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\" " |
| 353 "mechanism=\"PLAIN\" auth:allow-non-google-login=\"true\" " |
| 354 "auth:client-uses-full-bind-result=\"true\" " |
| 355 "xmlns:auth=\"http://www.google.com/talk/protocol/auth\"" |
| 356 ">AGRhdmlkAGRhdmlk</auth>", handler()->OutputActivity()); |
| 357 EXPECT_EQ("", handler()->SessionActivity()); |
| 358 EXPECT_EQ("", handler()->StanzaActivity()); |
| 359 } |
| 360 |
| 361 TEST_F(XmppLoginTaskTest, TestTlsDisabledNotPresent) { |
| 362 SetTlsOptions(buzz::TLS_DISABLED); |
| 363 RunPartialLogin(XLTT_STAGE_CONNECT, XLTT_STAGE_STREAMSTART); |
| 364 |
| 365 std::string input = "<stream:features>" |
| 366 "<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>" |
| 367 "<mechanism>DIGEST-MD5</mechanism>" |
| 368 "<mechanism>PLAIN</mechanism>" |
| 369 "</mechanisms>" |
| 370 "</stream:features>"; |
| 371 engine()->HandleInput(input.c_str(), input.length()); |
| 372 |
| 373 EXPECT_EQ("<auth xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\" " |
| 374 "mechanism=\"PLAIN\" auth:allow-non-google-login=\"true\" " |
| 375 "auth:client-uses-full-bind-result=\"true\" " |
| 376 "xmlns:auth=\"http://www.google.com/talk/protocol/auth\"" |
| 377 ">AGRhdmlkAGRhdmlk</auth>", handler()->OutputActivity()); |
| 378 EXPECT_EQ("", handler()->SessionActivity()); |
| 379 EXPECT_EQ("", handler()->StanzaActivity()); |
| 380 } |
| 381 |
| 382 TEST_F(XmppLoginTaskTest, TestTlsDisabledAndPresent) { |
| 383 SetTlsOptions(buzz::TLS_DISABLED); |
| 384 RunPartialLogin(XLTT_STAGE_CONNECT, XLTT_STAGE_STREAMSTART); |
| 385 |
| 386 std::string input = "<stream:features>" |
| 387 "<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>" |
| 388 "<mechanism>X-GOOGLE-TOKEN</mechanism>" |
| 389 "<mechanism>PLAIN</mechanism>" |
| 390 "<mechanism>X-OAUTH2</mechanism>" |
| 391 "</mechanisms>" |
| 392 "</stream:features>"; |
| 393 engine()->HandleInput(input.c_str(), input.length()); |
| 394 |
| 395 EXPECT_EQ("<auth xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\" " |
| 396 "mechanism=\"PLAIN\" auth:allow-non-google-login=\"true\" " |
| 397 "auth:client-uses-full-bind-result=\"true\" " |
| 398 "xmlns:auth=\"http://www.google.com/talk/protocol/auth\"" |
| 399 ">AGRhdmlkAGRhdmlk</auth>", handler()->OutputActivity()); |
| 400 EXPECT_EQ("", handler()->SessionActivity()); |
| 401 EXPECT_EQ("", handler()->StanzaActivity()); |
| 402 } |
| 403 |
| 404 TEST_F(XmppLoginTaskTest, TestTlsFailure) { |
| 405 RunPartialLogin(XLTT_STAGE_CONNECT, XLTT_STAGE_TLS_FEATURES); |
| 406 |
| 407 std::string input = "<failure xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>"; |
| 408 engine()->HandleInput(input.c_str(), input.length()); |
| 409 |
| 410 EXPECT_EQ("[CLOSED]", handler()->OutputActivity()); |
| 411 EXPECT_EQ("[CLOSED][ERROR-TLS]", handler()->SessionActivity()); |
| 412 EXPECT_EQ("", handler()->StanzaActivity()); |
| 413 } |
| 414 |
| 415 TEST_F(XmppLoginTaskTest, TestTlsBadStream) { |
| 416 RunPartialLogin(XLTT_STAGE_CONNECT, XLTT_STAGE_TLS_PROCEED); |
| 417 |
| 418 std::string input = "<wrongtag>"; |
| 419 engine()->HandleInput(input.c_str(), input.length()); |
| 420 |
| 421 EXPECT_EQ("[CLOSED]", handler()->OutputActivity()); |
| 422 EXPECT_EQ("[CLOSED][ERROR-VERSION]", handler()->SessionActivity()); |
| 423 EXPECT_EQ("", handler()->StanzaActivity()); |
| 424 } |
| 425 |
| 426 TEST_F(XmppLoginTaskTest, TestMissingSaslPlain) { |
| 427 RunPartialLogin(XLTT_STAGE_CONNECT, XLTT_STAGE_ENCRYPTED_START); |
| 428 |
| 429 std::string input = "<stream:features>" |
| 430 "<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>" |
| 431 "<mechanism>DIGEST-MD5</mechanism>" |
| 432 "</mechanisms>" |
| 433 "</stream:features>"; |
| 434 engine()->HandleInput(input.c_str(), input.length()); |
| 435 |
| 436 EXPECT_EQ("[CLOSED]", handler()->OutputActivity()); |
| 437 EXPECT_EQ("[CLOSED][ERROR-AUTH]", handler()->SessionActivity()); |
| 438 EXPECT_EQ("", handler()->StanzaActivity()); |
| 439 } |
| 440 |
| 441 TEST_F(XmppLoginTaskTest, TestWrongPassword) { |
| 442 RunPartialLogin(XLTT_STAGE_CONNECT, XLTT_STAGE_AUTH_FEATURES); |
| 443 |
| 444 std::string input = "<failure xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>"; |
| 445 engine()->HandleInput(input.c_str(), input.length()); |
| 446 |
| 447 EXPECT_EQ("[CLOSED]", handler()->OutputActivity()); |
| 448 EXPECT_EQ("[CLOSED][ERROR-UNAUTHORIZED]", handler()->SessionActivity()); |
| 449 EXPECT_EQ("", handler()->StanzaActivity()); |
| 450 } |
| 451 |
| 452 TEST_F(XmppLoginTaskTest, TestAuthBadStream) { |
| 453 RunPartialLogin(XLTT_STAGE_CONNECT, XLTT_STAGE_AUTH_SUCCESS); |
| 454 |
| 455 std::string input = "<wrongtag>"; |
| 456 engine()->HandleInput(input.c_str(), input.length()); |
| 457 |
| 458 EXPECT_EQ("[CLOSED]", handler()->OutputActivity()); |
| 459 EXPECT_EQ("[CLOSED][ERROR-VERSION]", handler()->SessionActivity()); |
| 460 EXPECT_EQ("", handler()->StanzaActivity()); |
| 461 } |
| 462 |
| 463 TEST_F(XmppLoginTaskTest, TestMissingBindFeature) { |
| 464 RunPartialLogin(XLTT_STAGE_CONNECT, XLTT_STAGE_AUTHENTICATED_START); |
| 465 |
| 466 std::string input = "<stream:features>" |
| 467 "<session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>" |
| 468 "</stream:features>"; |
| 469 engine()->HandleInput(input.c_str(), input.length()); |
| 470 |
| 471 EXPECT_EQ("[CLOSED]", handler()->OutputActivity()); |
| 472 EXPECT_EQ("[CLOSED][ERROR-BIND]", handler()->SessionActivity()); |
| 473 } |
| 474 |
| 475 TEST_F(XmppLoginTaskTest, TestMissingSessionFeature) { |
| 476 RunPartialLogin(XLTT_STAGE_CONNECT, XLTT_STAGE_AUTHENTICATED_START); |
| 477 |
| 478 std::string input = "<stream:features>" |
| 479 "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>" |
| 480 "</stream:features>"; |
| 481 engine()->HandleInput(input.c_str(), input.length()); |
| 482 |
| 483 EXPECT_EQ("[CLOSED]", handler()->OutputActivity()); |
| 484 EXPECT_EQ("[CLOSED][ERROR-BIND]", handler()->SessionActivity()); |
| 485 EXPECT_EQ("", handler()->StanzaActivity()); |
| 486 } |
| 487 |
| 488 /* TODO: Handle this case properly inside XmppLoginTask. |
| 489 TEST_F(XmppLoginTaskTest, TestBindFailure1) { |
| 490 // check wrong JID |
| 491 RunPartialLogin(XLTT_STAGE_CONNECT, XLTT_STAGE_BIND_FEATURES); |
| 492 |
| 493 std::string input = "<iq type='result' id='0'>" |
| 494 "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>" |
| 495 "<jid>davey@my-server/test</jid></bind></iq>"; |
| 496 engine()->HandleInput(input.c_str(), input.length()); |
| 497 |
| 498 EXPECT_EQ("[CLOSED]", handler()->OutputActivity()); |
| 499 EXPECT_EQ("[CLOSED][ERROR-BIND]", handler()->SessionActivity()); |
| 500 EXPECT_EQ("", handler()->StanzaActivity()); |
| 501 } |
| 502 */ |
| 503 |
| 504 TEST_F(XmppLoginTaskTest, TestBindFailure2) { |
| 505 // check missing JID |
| 506 RunPartialLogin(XLTT_STAGE_CONNECT, XLTT_STAGE_BIND_FEATURES); |
| 507 |
| 508 std::string input = "<iq type='result' id='0'>" |
| 509 "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/></iq>"; |
| 510 engine()->HandleInput(input.c_str(), input.length()); |
| 511 |
| 512 EXPECT_EQ("[CLOSED]", handler()->OutputActivity()); |
| 513 EXPECT_EQ("[CLOSED][ERROR-BIND]", handler()->SessionActivity()); |
| 514 EXPECT_EQ("", handler()->StanzaActivity()); |
| 515 } |
| 516 |
| 517 TEST_F(XmppLoginTaskTest, TestBindFailure3) { |
| 518 // check plain failure |
| 519 RunPartialLogin(XLTT_STAGE_CONNECT, XLTT_STAGE_BIND_FEATURES); |
| 520 |
| 521 std::string input = "<iq type='error' id='0'/>"; |
| 522 engine()->HandleInput(input.c_str(), input.length()); |
| 523 |
| 524 EXPECT_EQ("[CLOSED]", handler()->OutputActivity()); |
| 525 EXPECT_EQ("[CLOSED][ERROR-BIND]", handler()->SessionActivity()); |
| 526 EXPECT_EQ("", handler()->StanzaActivity()); |
| 527 } |
| 528 |
| 529 TEST_F(XmppLoginTaskTest, TestBindFailure4) { |
| 530 // check wrong id to ignore |
| 531 RunPartialLogin(XLTT_STAGE_CONNECT, XLTT_STAGE_BIND_FEATURES); |
| 532 |
| 533 std::string input = "<iq type='error' id='1'/>"; |
| 534 engine()->HandleInput(input.c_str(), input.length()); |
| 535 |
| 536 // continue after an ignored iq |
| 537 RunPartialLogin(XLTT_STAGE_BIND_SUCCESS, XLTT_STAGE_SESSION_SUCCESS); |
| 538 } |
| 539 |
| 540 TEST_F(XmppLoginTaskTest, TestSessionFailurePlain1) { |
| 541 RunPartialLogin(XLTT_STAGE_CONNECT, XLTT_STAGE_BIND_SUCCESS); |
| 542 |
| 543 std::string input = "<iq type='error' id='1'/>"; |
| 544 engine()->HandleInput(input.c_str(), input.length()); |
| 545 |
| 546 EXPECT_EQ("[CLOSED]", handler()->OutputActivity()); |
| 547 EXPECT_EQ("[CLOSED][ERROR-BIND]", handler()->SessionActivity()); |
| 548 } |
| 549 |
| 550 TEST_F(XmppLoginTaskTest, TestSessionFailurePlain2) { |
| 551 RunPartialLogin(XLTT_STAGE_CONNECT, XLTT_STAGE_BIND_SUCCESS); |
| 552 |
| 553 // check reverse iq to ignore |
| 554 // TODO: consider queueing or passing through? |
| 555 std::string input = "<iq type='get' id='1'/>"; |
| 556 engine()->HandleInput(input.c_str(), input.length()); |
| 557 |
| 558 EXPECT_EQ("", handler()->OutputActivity()); |
| 559 EXPECT_EQ("", handler()->SessionActivity()); |
| 560 |
| 561 // continue after an ignored iq |
| 562 RunPartialLogin(XLTT_STAGE_SESSION_SUCCESS, XLTT_STAGE_SESSION_SUCCESS); |
| 563 } |
| 564 |
| 565 TEST_F(XmppLoginTaskTest, TestBadXml) { |
| 566 int errorKind = 0; |
| 567 for (XlttStage stage = XLTT_STAGE_CONNECT; |
| 568 stage <= XLTT_STAGE_SESSION_SUCCESS; |
| 569 stage = static_cast<XlttStage>(stage + 1)) { |
| 570 RunPartialLogin(XLTT_STAGE_CONNECT, stage); |
| 571 |
| 572 std::string input; |
| 573 switch (errorKind++ % 5) { |
| 574 case 0: input = "&syntax;"; break; |
| 575 case 1: input = "<nons:iq/>"; break; |
| 576 case 2: input = "<iq a='b' a='dupe'/>"; break; |
| 577 case 3: input = "<>"; break; |
| 578 case 4: input = "<iq a='<wrong>'>"; break; |
| 579 } |
| 580 |
| 581 engine()->HandleInput(input.c_str(), input.length()); |
| 582 |
| 583 EXPECT_EQ("[CLOSED]", handler()->OutputActivity()); |
| 584 EXPECT_EQ("[CLOSED][ERROR-XML]", handler()->SessionActivity()); |
| 585 |
| 586 TearDown(); |
| 587 SetUp(); |
| 588 } |
| 589 } |
| 590 |
| 591 TEST_F(XmppLoginTaskTest, TestStreamError) { |
| 592 for (XlttStage stage = XLTT_STAGE_CONNECT; |
| 593 stage <= XLTT_STAGE_SESSION_SUCCESS; |
| 594 stage = static_cast<XlttStage>(stage + 1)) { |
| 595 switch (stage) { |
| 596 case XLTT_STAGE_CONNECT: |
| 597 case XLTT_STAGE_TLS_PROCEED: |
| 598 case XLTT_STAGE_AUTH_SUCCESS: |
| 599 continue; |
| 600 default: |
| 601 break; |
| 602 } |
| 603 |
| 604 RunPartialLogin(XLTT_STAGE_CONNECT, stage); |
| 605 |
| 606 std::string input = "<stream:error>" |
| 607 "<xml-not-well-formed xmlns='urn:ietf:params:xml:ns:xmpp-streams'/>" |
| 608 "<text xml:lang='en' xmlns='urn:ietf:params:xml:ns:xmpp-streams'>" |
| 609 "Some special application diagnostic information!" |
| 610 "</text>" |
| 611 "<escape-your-data xmlns='application-ns'/>" |
| 612 "</stream:error>"; |
| 613 |
| 614 engine()->HandleInput(input.c_str(), input.length()); |
| 615 |
| 616 EXPECT_EQ("[CLOSED]", handler()->OutputActivity()); |
| 617 EXPECT_EQ("[CLOSED][ERROR-STREAM]", handler()->SessionActivity()); |
| 618 |
| 619 EXPECT_EQ("<str:error xmlns:str=\"http://etherx.jabber.org/streams\">" |
| 620 "<xml-not-well-formed xmlns=\"urn:ietf:params:xml:ns:xmpp-streams\"/>" |
| 621 "<text xml:lang=\"en\" xmlns=\"urn:ietf:params:xml:ns:xmpp-streams\">" |
| 622 "Some special application diagnostic information!" |
| 623 "</text>" |
| 624 "<escape-your-data xmlns=\"application-ns\"/>" |
| 625 "</str:error>", engine()->GetStreamError()->Str()); |
| 626 |
| 627 TearDown(); |
| 628 SetUp(); |
| 629 } |
| 630 } |
| 631 |
OLD | NEW |