diff options
author | Remko Tronçon <git@el-tramo.be> | 2011-08-28 09:19:46 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2011-08-28 09:19:46 (GMT) |
commit | 5f1cb0d768265347bc80862c33f5967f07759b10 (patch) | |
tree | 979eceddcd0f5e6a651180b91d0908e1e1e319b7 /Swiften/Queries/UnitTest/RequestTest.cpp | |
parent | 8dbae452004e7bc3c3a6e855f365b9bb348e8f2d (diff) | |
download | swift-contrib-5f1cb0d768265347bc80862c33f5967f07759b10.zip swift-contrib-5f1cb0d768265347bc80862c33f5967f07759b10.tar.bz2 |
Check sender on incoming IQ responses.
Release-Notes: Fixed a bug whereby the sender of an iq wasn't being checked before matching it to a request.
Diffstat (limited to 'Swiften/Queries/UnitTest/RequestTest.cpp')
-rw-r--r-- | Swiften/Queries/UnitTest/RequestTest.cpp | 95 |
1 files changed, 84 insertions, 11 deletions
diff --git a/Swiften/Queries/UnitTest/RequestTest.cpp b/Swiften/Queries/UnitTest/RequestTest.cpp index 46eb6fd..b3925dd 100644 --- a/Swiften/Queries/UnitTest/RequestTest.cpp +++ b/Swiften/Queries/UnitTest/RequestTest.cpp @@ -25,18 +25,23 @@ class RequestTest : public CppUnit::TestFixture { CPPUNIT_TEST(testHandleIQ); CPPUNIT_TEST(testHandleIQ_InvalidID); CPPUNIT_TEST(testHandleIQ_Error); CPPUNIT_TEST(testHandleIQ_ErrorWithoutPayload); CPPUNIT_TEST(testHandleIQ_BeforeSend); CPPUNIT_TEST(testHandleIQ_DifferentPayload); CPPUNIT_TEST(testHandleIQ_RawXMLPayload); CPPUNIT_TEST(testHandleIQ_GetWithSameID); CPPUNIT_TEST(testHandleIQ_SetWithSameID); + CPPUNIT_TEST(testHandleIQ_IncorrectSender); + CPPUNIT_TEST(testHandleIQ_IncorrectSenderForServerQuery); + CPPUNIT_TEST(testHandleIQ_ServerRespondsWithDomain); + CPPUNIT_TEST(testHandleIQ_ServerRespondsWithBareJID); + CPPUNIT_TEST(testHandleIQ_ServerRespondsWithoutFrom); CPPUNIT_TEST_SUITE_END(); public: class MyPayload : public Payload { public: MyPayload(const std::string& s = "") : text_(s) {} std::string text_; }; @@ -92,132 +97,198 @@ class RequestTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel_->iqs_.size())); CPPUNIT_ASSERT_EQUAL(IQ::Get, channel_->iqs_[0]->getType()); } void testHandleIQ() { MyRequest testling(IQ::Get, JID("foo@bar.com/baz"), payload_, router_); testling.onResponse.connect(boost::bind(&RequestTest::handleResponse, this, _1, _2)); testling.send(); - channel_->onIQReceived(createResponse("test-id")); + channel_->onIQReceived(createResponse(JID("foo@bar.com/baz"),"test-id")); CPPUNIT_ASSERT_EQUAL(1, responsesReceived_); CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(receivedErrors.size())); CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel_->iqs_.size())); } // FIXME: Doesn't test that it didn't handle the payload void testHandleIQ_InvalidID() { MyRequest testling(IQ::Get, JID("foo@bar.com/baz"), payload_, router_); testling.onResponse.connect(boost::bind(&RequestTest::handleResponse, this, _1, _2)); testling.send(); - channel_->onIQReceived(createResponse("different-id")); + channel_->onIQReceived(createResponse(JID("foo@bar.com/baz"),"different-id")); CPPUNIT_ASSERT_EQUAL(0, responsesReceived_); CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(receivedErrors.size())); CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel_->iqs_.size())); } void testHandleIQ_Error() { MyRequest testling(IQ::Get, JID("foo@bar.com/baz"), payload_, router_); testling.onResponse.connect(boost::bind(&RequestTest::handleResponse, this, _1, _2)); testling.send(); - boost::shared_ptr<IQ> error = createError("test-id"); + boost::shared_ptr<IQ> error = createError(JID("foo@bar.com/baz"),"test-id"); boost::shared_ptr<Payload> errorPayload = boost::shared_ptr<ErrorPayload>(new ErrorPayload(ErrorPayload::InternalServerError)); error->addPayload(errorPayload); channel_->onIQReceived(error); CPPUNIT_ASSERT_EQUAL(0, responsesReceived_); CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(receivedErrors.size())); CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel_->iqs_.size())); CPPUNIT_ASSERT_EQUAL(ErrorPayload::InternalServerError, receivedErrors[0].getCondition()); } void testHandleIQ_ErrorWithoutPayload() { MyRequest testling(IQ::Get, JID("foo@bar.com/baz"), payload_, router_); testling.onResponse.connect(boost::bind(&RequestTest::handleResponse, this, _1, _2)); testling.send(); - channel_->onIQReceived(createError("test-id")); + channel_->onIQReceived(createError(JID("foo@bar.com/baz"),"test-id")); CPPUNIT_ASSERT_EQUAL(0, responsesReceived_); CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(receivedErrors.size())); CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel_->iqs_.size())); CPPUNIT_ASSERT_EQUAL(ErrorPayload::UndefinedCondition, receivedErrors[0].getCondition()); } void testHandleIQ_BeforeSend() { MyRequest testling(IQ::Get, JID("foo@bar.com/baz"), payload_, router_); testling.onResponse.connect(boost::bind(&RequestTest::handleResponse, this, _1, _2)); - channel_->onIQReceived(createResponse("test-id")); + channel_->onIQReceived(createResponse(JID("foo@bar.com/baz"),"test-id")); CPPUNIT_ASSERT_EQUAL(0, responsesReceived_); CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(receivedErrors.size())); CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(channel_->iqs_.size())); } void testHandleIQ_DifferentPayload() { MyRequest testling(IQ::Get, JID("foo@bar.com/baz"), payload_, router_); testling.onResponse.connect(boost::bind(&RequestTest::handleDifferentResponse, this, _1, _2)); testling.send(); responsePayload_ = boost::make_shared<MyOtherPayload>(); - channel_->onIQReceived(createResponse("test-id")); + channel_->onIQReceived(createResponse(JID("foo@bar.com/baz"),"test-id")); CPPUNIT_ASSERT_EQUAL(1, responsesReceived_); CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(receivedErrors.size())); CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel_->iqs_.size())); } void testHandleIQ_RawXMLPayload() { payload_ = boost::make_shared<RawXMLPayload>("<bla/>"); MyRequest testling(IQ::Get, JID("foo@bar.com/baz"), payload_, router_); testling.onResponse.connect(boost::bind(&RequestTest::handleRawXMLResponse, this, _1, _2)); testling.send(); responsePayload_ = boost::make_shared<MyOtherPayload>(); - channel_->onIQReceived(createResponse("test-id")); + channel_->onIQReceived(createResponse(JID("foo@bar.com/baz"),"test-id")); CPPUNIT_ASSERT_EQUAL(1, responsesReceived_); CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(receivedErrors.size())); CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel_->iqs_.size())); } void testHandleIQ_GetWithSameID() { MyRequest testling(IQ::Get, JID("foo@bar.com/baz"), payload_, router_); testling.onResponse.connect(boost::bind(&RequestTest::handleResponse, this, _1, _2)); testling.send(); - boost::shared_ptr<IQ> response = createResponse("test-id"); + boost::shared_ptr<IQ> response = createResponse(JID("foo@bar.com/baz"),"test-id"); response->setType(IQ::Get); channel_->onIQReceived(response); CPPUNIT_ASSERT_EQUAL(0, responsesReceived_); CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(receivedErrors.size())); CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(channel_->iqs_.size())); } void testHandleIQ_SetWithSameID() { MyRequest testling(IQ::Get, JID("foo@bar.com/baz"), payload_, router_); testling.onResponse.connect(boost::bind(&RequestTest::handleResponse, this, _1, _2)); testling.send(); - boost::shared_ptr<IQ> response = createResponse("test-id"); + boost::shared_ptr<IQ> response = createResponse(JID("foo@bar.com/baz"), "test-id"); response->setType(IQ::Set); channel_->onIQReceived(response); CPPUNIT_ASSERT_EQUAL(0, responsesReceived_); CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(receivedErrors.size())); CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(channel_->iqs_.size())); } + void testHandleIQ_IncorrectSender() { + MyRequest testling(IQ::Get, JID("foo@bar.com/baz"), payload_, router_); + router_->setJID("alice@wonderland.lit/TeaParty"); + testling.onResponse.connect(boost::bind(&RequestTest::handleResponse, this, _1, _2)); + testling.send(); + + channel_->onIQReceived(createResponse(JID("anotherfoo@bar.com/baz"), "test-id")); + + CPPUNIT_ASSERT_EQUAL(0, responsesReceived_); + CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(receivedErrors.size())); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel_->iqs_.size())); + } + + void testHandleIQ_IncorrectSenderForServerQuery() { + MyRequest testling(IQ::Get, JID(), payload_, router_); + router_->setJID("alice@wonderland.lit/TeaParty"); + testling.onResponse.connect(boost::bind(&RequestTest::handleResponse, this, _1, _2)); + testling.send(); + + channel_->onIQReceived(createResponse(JID("foo@bar.com/baz"), "test-id")); + + CPPUNIT_ASSERT_EQUAL(0, responsesReceived_); + CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(receivedErrors.size())); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel_->iqs_.size())); + } + + void testHandleIQ_ServerRespondsWithDomain() { + MyRequest testling(IQ::Get, JID(), payload_, router_); + router_->setJID("alice@wonderland.lit/TeaParty"); + testling.onResponse.connect(boost::bind(&RequestTest::handleResponse, this, _1, _2)); + testling.send(); + + channel_->onIQReceived(createResponse(JID("wonderland.lit"),"test-id")); + + CPPUNIT_ASSERT_EQUAL(0, responsesReceived_); + CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(receivedErrors.size())); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel_->iqs_.size())); + } + + void testHandleIQ_ServerRespondsWithBareJID() { + MyRequest testling(IQ::Get, JID(), payload_, router_); + router_->setJID("alice@wonderland.lit/TeaParty"); + testling.onResponse.connect(boost::bind(&RequestTest::handleResponse, this, _1, _2)); + testling.send(); + + channel_->onIQReceived(createResponse(JID("alice@wonderland.lit"),"test-id")); + + CPPUNIT_ASSERT_EQUAL(1, responsesReceived_); + CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(receivedErrors.size())); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel_->iqs_.size())); + } + + void testHandleIQ_ServerRespondsWithoutFrom() { + MyRequest testling(IQ::Get, JID(), payload_, router_); + router_->setJID("alice@wonderland.lit/TeaParty"); + testling.onResponse.connect(boost::bind(&RequestTest::handleResponse, this, _1, _2)); + testling.send(); + + channel_->onIQReceived(createResponse(JID(),"test-id")); + + CPPUNIT_ASSERT_EQUAL(1, responsesReceived_); + CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(receivedErrors.size())); + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel_->iqs_.size())); + } + + private: void handleResponse(boost::shared_ptr<Payload> p, ErrorPayload::ref e) { if (e) { receivedErrors.push_back(*e); } else { boost::shared_ptr<MyPayload> payload(boost::dynamic_pointer_cast<MyPayload>(p)); CPPUNIT_ASSERT(payload); @@ -233,27 +304,29 @@ class RequestTest : public CppUnit::TestFixture { } void handleRawXMLResponse(boost::shared_ptr<Payload> p, ErrorPayload::ref e) { CPPUNIT_ASSERT(!e); CPPUNIT_ASSERT(p); CPPUNIT_ASSERT(boost::dynamic_pointer_cast<MyOtherPayload>(p)); ++responsesReceived_; } - boost::shared_ptr<IQ> createResponse(const std::string& id) { + boost::shared_ptr<IQ> createResponse(const JID& from, const std::string& id) { boost::shared_ptr<IQ> iq(new IQ(IQ::Result)); + iq->setFrom(from); iq->addPayload(responsePayload_); iq->setID(id); return iq; } - boost::shared_ptr<IQ> createError(const std::string& id) { + boost::shared_ptr<IQ> createError(const JID& from, const std::string& id) { boost::shared_ptr<IQ> iq(new IQ(IQ::Error)); + iq->setFrom(from); iq->setID(id); return iq; } private: IQRouter* router_; DummyIQChannel* channel_; boost::shared_ptr<Payload> payload_; boost::shared_ptr<Payload> responsePayload_; |