1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
// Copyright 2004 Google Inc. All Rights Reserved
// Author: David Bau
#include <string>
#include <sstream>
#include <iostream>
#include "talk/base/common.h"
#include "talk/base/gunit.h"
#include "talk/xmllite/xmlelement.h"
#include "talk/xmpp/constants.h"
#include "talk/xmpp/util_unittest.h"
#include "talk/xmpp/saslplainmechanism.h"
#include "talk/xmpp/plainsaslhandler.h"
#include "talk/xmpp/xmppengine.h"
using buzz::Jid;
using buzz::QName;
using buzz::XmlElement;
using buzz::XmppEngine;
using buzz::XmppIqCookie;
using buzz::XmppIqHandler;
using buzz::XmppTestHandler;
using buzz::QN_ID;
using buzz::QN_IQ;
using buzz::QN_TYPE;
using buzz::QN_ROSTER_QUERY;
using buzz::XMPP_RETURN_OK;
using buzz::XMPP_RETURN_BADARGUMENT;
// XmppEngineTestIqHandler
// This class grabs the response to an IQ stanza and stores it in a string.
class XmppEngineTestIqHandler : public XmppIqHandler {
public:
virtual void IqResponse(XmppIqCookie, const XmlElement * stanza) {
ss_ << stanza->Str();
}
std::string IqResponseActivity() {
std::string result = ss_.str();
ss_.str("");
return result;
}
private:
std::stringstream ss_;
};
class XmppEngineTest : public testing::Test {
public:
XmppEngine* engine() { return engine_.get(); }
XmppTestHandler* handler() { return handler_.get(); }
virtual void SetUp() {
engine_.reset(XmppEngine::Create());
handler_.reset(new XmppTestHandler(engine_.get()));
Jid jid("david@my-server");
talk_base::InsecureCryptStringImpl pass;
pass.password() = "david";
engine_->SetSessionHandler(handler_.get());
engine_->SetOutputHandler(handler_.get());
engine_->AddStanzaHandler(handler_.get());
engine_->SetUser(jid);
engine_->SetSaslHandler(
new buzz::PlainSaslHandler(jid, talk_base::CryptString(pass), true));
}
virtual void TearDown() {
handler_.reset();
engine_.reset();
}
void RunLogin();
private:
talk_base::scoped_ptr<XmppEngine> engine_;
talk_base::scoped_ptr<XmppTestHandler> handler_;
};
void XmppEngineTest::RunLogin() {
// Connect
EXPECT_EQ(XmppEngine::STATE_START, engine()->GetState());
engine()->Connect();
EXPECT_EQ(XmppEngine::STATE_OPENING, engine()->GetState());
EXPECT_EQ("[OPENING]", handler_->SessionActivity());
EXPECT_EQ("<stream:stream to=\"my-server\" xml:lang=\"*\" version=\"1.0\" "
"xmlns:stream=\"http://etherx.jabber.org/streams\" "
"xmlns=\"jabber:client\">\r\n", handler_->OutputActivity());
std::string input =
"<stream:stream id=\"a5f2d8c9\" version=\"1.0\" "
"xmlns:stream=\"http://etherx.jabber.org/streams\" "
"xmlns=\"jabber:client\">";
engine()->HandleInput(input.c_str(), input.length());
input =
"<stream:features>"
"<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'>"
"<required/>"
"</starttls>"
"<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>"
"<mechanism>DIGEST-MD5</mechanism>"
"<mechanism>PLAIN</mechanism>"
"</mechanisms>"
"</stream:features>";
engine()->HandleInput(input.c_str(), input.length());
EXPECT_EQ("<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"/>",
handler_->OutputActivity());
EXPECT_EQ("", handler_->SessionActivity());
EXPECT_EQ("", handler_->StanzaActivity());
input = "<proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>";
engine()->HandleInput(input.c_str(), input.length());
EXPECT_EQ("[START-TLS my-server]"
"<stream:stream to=\"my-server\" xml:lang=\"*\" "
"version=\"1.0\" xmlns:stream=\"http://etherx.jabber.org/streams\" "
"xmlns=\"jabber:client\">\r\n", handler_->OutputActivity());
EXPECT_EQ("", handler_->SessionActivity());
EXPECT_EQ("", handler_->StanzaActivity());
input = "<stream:stream id=\"01234567\" version=\"1.0\" "
"xmlns:stream=\"http://etherx.jabber.org/streams\" "
"xmlns=\"jabber:client\">";
engine()->HandleInput(input.c_str(), input.length());
input =
"<stream:features>"
"<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>"
"<mechanism>DIGEST-MD5</mechanism>"
"<mechanism>PLAIN</mechanism>"
"</mechanisms>"
"</stream:features>";
engine()->HandleInput(input.c_str(), input.length());
EXPECT_EQ("<auth xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\" "
"mechanism=\"PLAIN\" "
"auth:allow-non-google-login=\"true\" "
"auth:client-uses-full-bind-result=\"true\" "
"xmlns:auth=\"http://www.google.com/talk/protocol/auth\""
">AGRhdmlkAGRhdmlk</auth>",
handler_->OutputActivity());
EXPECT_EQ("", handler_->SessionActivity());
EXPECT_EQ("", handler_->StanzaActivity());
input = "<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>";
engine()->HandleInput(input.c_str(), input.length());
EXPECT_EQ("<stream:stream to=\"my-server\" xml:lang=\"*\" version=\"1.0\" "
"xmlns:stream=\"http://etherx.jabber.org/streams\" "
"xmlns=\"jabber:client\">\r\n", handler_->OutputActivity());
EXPECT_EQ("", handler_->SessionActivity());
EXPECT_EQ("", handler_->StanzaActivity());
input = "<stream:stream id=\"01234567\" version=\"1.0\" "
"xmlns:stream=\"http://etherx.jabber.org/streams\" "
"xmlns=\"jabber:client\">";
engine()->HandleInput(input.c_str(), input.length());
input = "<stream:features>"
"<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>"
"<session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>"
"</stream:features>";
engine()->HandleInput(input.c_str(), input.length());
EXPECT_EQ("<iq type=\"set\" id=\"0\">"
"<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"/></iq>",
handler_->OutputActivity());
EXPECT_EQ("", handler_->SessionActivity());
EXPECT_EQ("", handler_->StanzaActivity());
input = "<iq type='result' id='0'>"
"<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><jid>"
"david@my-server/test</jid></bind></iq>";
engine()->HandleInput(input.c_str(), input.length());
EXPECT_EQ("<iq type=\"set\" id=\"1\">"
"<session xmlns=\"urn:ietf:params:xml:ns:xmpp-session\"/></iq>",
handler_->OutputActivity());
EXPECT_EQ("", handler_->SessionActivity());
EXPECT_EQ("", handler_->StanzaActivity());
input = "<iq type='result' id='1'/>";
engine()->HandleInput(input.c_str(), input.length());
EXPECT_EQ("[OPEN]", handler_->SessionActivity());
EXPECT_EQ("", handler_->StanzaActivity());
EXPECT_EQ(Jid("david@my-server/test"), engine()->FullJid());
}
// TestSuccessfulLogin()
// This function simply tests to see if a login works. This includes
// encryption and authentication
TEST_F(XmppEngineTest, TestSuccessfulLoginAndDisconnect) {
RunLogin();
engine()->Disconnect();
EXPECT_EQ("</stream:stream>[CLOSED]", handler()->OutputActivity());
EXPECT_EQ("[CLOSED]", handler()->SessionActivity());
EXPECT_EQ("", handler()->StanzaActivity());
}
TEST_F(XmppEngineTest, TestSuccessfulLoginAndConnectionClosed) {
RunLogin();
engine()->ConnectionClosed(0);
EXPECT_EQ("[CLOSED]", handler()->OutputActivity());
EXPECT_EQ("[CLOSED][ERROR-CONNECTION-CLOSED]", handler()->SessionActivity());
EXPECT_EQ("", handler()->StanzaActivity());
}
// TestNotXmpp()
// This tests the error case when connecting to a non XMPP service
TEST_F(XmppEngineTest, TestNotXmpp) {
// Connect
engine()->Connect();
EXPECT_EQ("<stream:stream to=\"my-server\" xml:lang=\"*\" version=\"1.0\" "
"xmlns:stream=\"http://etherx.jabber.org/streams\" "
"xmlns=\"jabber:client\">\r\n", handler()->OutputActivity());
// Send garbage response (courtesy of apache)
std::string input = "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">";
engine()->HandleInput(input.c_str(), input.length());
EXPECT_EQ("[CLOSED]", handler()->OutputActivity());
EXPECT_EQ("[OPENING][CLOSED][ERROR-XML]", handler()->SessionActivity());
EXPECT_EQ("", handler()->StanzaActivity());
}
// TestPassthrough()
// This tests that arbitrary stanzas can be passed to the server through
// the engine.
TEST_F(XmppEngineTest, TestPassthrough) {
// Queue up an app stanza
XmlElement application_stanza(QName("test", "app-stanza"));
application_stanza.AddText("this-is-a-test");
engine()->SendStanza(&application_stanza);
// Do the whole login handshake
RunLogin();
EXPECT_EQ("<test:app-stanza xmlns:test=\"test\">this-is-a-test"
"</test:app-stanza>", handler()->OutputActivity());
// do another stanza
XmlElement roster_get(QN_IQ);
roster_get.AddAttr(QN_TYPE, "get");
roster_get.AddAttr(QN_ID, engine()->NextId());
roster_get.AddElement(new XmlElement(QN_ROSTER_QUERY, true));
engine()->SendStanza(&roster_get);
EXPECT_EQ("<iq type=\"get\" id=\"2\"><query xmlns=\"jabber:iq:roster\"/>"
"</iq>", handler()->OutputActivity());
// now say the server ends the stream
engine()->HandleInput("</stream:stream>", 16);
EXPECT_EQ("[CLOSED][ERROR-DOCUMENT-CLOSED]", handler()->SessionActivity());
EXPECT_EQ("[CLOSED]", handler()->OutputActivity());
EXPECT_EQ("", handler()->StanzaActivity());
}
// TestIqCallback()
// This tests the routing of Iq stanzas and responses.
TEST_F(XmppEngineTest, TestIqCallback) {
XmppEngineTestIqHandler iq_response;
XmppIqCookie cookie;
// Do the whole login handshake
RunLogin();
// Build an iq request
XmlElement roster_get(QN_IQ);
roster_get.AddAttr(QN_TYPE, "get");
roster_get.AddAttr(QN_ID, engine()->NextId());
roster_get.AddElement(new XmlElement(QN_ROSTER_QUERY, true));
engine()->SendIq(&roster_get, &iq_response, &cookie);
EXPECT_EQ("<iq type=\"get\" id=\"2\"><query xmlns=\"jabber:iq:roster\"/>"
"</iq>", handler()->OutputActivity());
EXPECT_EQ("", handler()->SessionActivity());
EXPECT_EQ("", handler()->StanzaActivity());
EXPECT_EQ("", iq_response.IqResponseActivity());
// now say the server responds to the iq
std::string input = "<iq type='result' id='2'>"
"<query xmlns='jabber:iq:roster'><item>foo</item>"
"</query></iq>";
engine()->HandleInput(input.c_str(), input.length());
EXPECT_EQ("", handler()->OutputActivity());
EXPECT_EQ("", handler()->SessionActivity());
EXPECT_EQ("", handler()->StanzaActivity());
EXPECT_EQ("<cli:iq type=\"result\" id=\"2\" xmlns:cli=\"jabber:client\">"
"<query xmlns=\"jabber:iq:roster\"><item>foo</item></query>"
"</cli:iq>", iq_response.IqResponseActivity());
EXPECT_EQ(XMPP_RETURN_BADARGUMENT, engine()->RemoveIqHandler(cookie, NULL));
// Do it again with another id to test cancel
roster_get.SetAttr(QN_ID, engine()->NextId());
engine()->SendIq(&roster_get, &iq_response, &cookie);
EXPECT_EQ("<iq type=\"get\" id=\"3\"><query xmlns=\"jabber:iq:roster\"/>"
"</iq>", handler()->OutputActivity());
EXPECT_EQ("", handler()->SessionActivity());
EXPECT_EQ("", handler()->StanzaActivity());
EXPECT_EQ("", iq_response.IqResponseActivity());
// cancel the handler this time
EXPECT_EQ(XMPP_RETURN_OK, engine()->RemoveIqHandler(cookie, NULL));
// now say the server responds to the iq: the iq handler should not get it.
input = "<iq type='result' id='3'><query xmlns='jabber:iq:roster'><item>bar"
"</item></query></iq>";
engine()->HandleInput(input.c_str(), input.length());
EXPECT_EQ("<cli:iq type=\"result\" id=\"3\" xmlns:cli=\"jabber:client\">"
"<query xmlns=\"jabber:iq:roster\"><item>bar</item></query>"
"</cli:iq>", handler()->StanzaActivity());
EXPECT_EQ("", iq_response.IqResponseActivity());
EXPECT_EQ("", handler()->OutputActivity());
EXPECT_EQ("", handler()->SessionActivity());
}
|