From aa32913718442aecb7b22e891bd6f1a1ceca9d8c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Thu, 11 Jun 2009 22:22:04 +0200
Subject: Do not allow a request response before it is sent.


diff --git a/Swiften/Queries/Request.cpp b/Swiften/Queries/Request.cpp
index 609d67b..189bbaa 100644
--- a/Swiften/Queries/Request.cpp
+++ b/Swiften/Queries/Request.cpp
@@ -4,7 +4,7 @@
 
 namespace Swift {
 
-Request::Request(IQ::Type type, const JID& receiver, boost::shared_ptr<Payload> payload, IQRouter* router, AutoDeleteBehavior autoDeleteBehavior) : IQHandler(router), type_(type), receiver_(receiver), payload_(payload), autoDeleteBehavior_(autoDeleteBehavior) {
+Request::Request(IQ::Type type, const JID& receiver, boost::shared_ptr<Payload> payload, IQRouter* router, AutoDeleteBehavior autoDeleteBehavior) : IQHandler(router), type_(type), receiver_(receiver), payload_(payload), autoDeleteBehavior_(autoDeleteBehavior), sent_(false) {
 	id_ = getRouter()->getNewIQID();
 }
 
@@ -13,12 +13,13 @@ void Request::send() {
 	iq->setTo(receiver_);
 	iq->addPayload(payload_);
 	iq->setID(id_);
+	sent_ = true;
 	getRouter()->sendIQ(iq);
 }
 
 bool Request::handleIQ(boost::shared_ptr<IQ> iq) {
 	bool handled = false;
-	if (iq->getID() == id_) {
+	if (sent_ && iq->getID() == id_) {
 		if (iq->getType() == IQ::Result) {
 			handleResponse(iq->getPayloadOfSameType(payload_), boost::optional<Error>());
 		}
diff --git a/Swiften/Queries/Request.h b/Swiften/Queries/Request.h
index f084303..d2bede6 100644
--- a/Swiften/Queries/Request.h
+++ b/Swiften/Queries/Request.h
@@ -40,6 +40,7 @@ namespace Swift {
 			boost::shared_ptr<Payload> payload_;
 			AutoDeleteBehavior autoDeleteBehavior_;
 			String id_;
+			bool sent_;
 	};
 }
 
diff --git a/Swiften/Queries/UnitTest/RequestTest.cpp b/Swiften/Queries/UnitTest/RequestTest.cpp
index 31c0603..ea6dee6 100644
--- a/Swiften/Queries/UnitTest/RequestTest.cpp
+++ b/Swiften/Queries/UnitTest/RequestTest.cpp
@@ -18,6 +18,7 @@ class RequestTest : public CppUnit::TestFixture
 		CPPUNIT_TEST(testHandleIQ);
 		CPPUNIT_TEST(testHandleIQ_InvalidID);
 		CPPUNIT_TEST(testHandleIQ_Error);
+		CPPUNIT_TEST(testHandleIQ_BeforeSend);
 		CPPUNIT_TEST_SUITE_END();
 
 	public:
@@ -100,6 +101,16 @@ class RequestTest : public CppUnit::TestFixture
 			CPPUNIT_ASSERT_EQUAL(1, errorsReceived_);
 			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel_->iqs_.size()));
 		}
+
+		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"));
+
+			CPPUNIT_ASSERT_EQUAL(0, responsesReceived_);
+			CPPUNIT_ASSERT_EQUAL(0, errorsReceived_);
+			CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(channel_->iqs_.size()));
+		}
 	
 	private:
 		void handleResponse(boost::shared_ptr<Payload> p, const boost::optional<Error>& e) {
-- 
cgit v0.10.2-6-g49f6