summaryrefslogtreecommitdiffstats
blob: 71a7f94f58facd1c96fd6131b7791d3732fb30d5 (plain)
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
/*
 * Copyright (c) 2010 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <boost/bind.hpp>
#include <boost/numeric/conversion/cast.hpp>

#include <Swiften/StreamManagement/StanzaAckResponder.h>
#include <Swiften/Elements/Message.h>

using namespace Swift;

namespace Swift {

class StanzaAckResponderTest : public CppUnit::TestFixture {
		CPPUNIT_TEST_SUITE(StanzaAckResponderTest);
		CPPUNIT_TEST(testHandleAckRequestReceived_AcksStanza);
		CPPUNIT_TEST(testHandleAckRequestReceived_AcksMultipleStanzas);
		CPPUNIT_TEST(testHandleAckRequestReceived_MultipleAcks);
		CPPUNIT_TEST(testHandleAckRequestReceived_WrapAround);
		CPPUNIT_TEST_SUITE_END();

	public:
		void testHandleAckRequestReceived_AcksStanza() {
			boost::shared_ptr<StanzaAckResponder> testling(createResponder());
			testling->handleStanzaReceived();

			testling->handleAckRequestReceived();

			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(acks.size()));
			CPPUNIT_ASSERT_EQUAL(1U, acks[0]);
		}

		void testHandleAckRequestReceived_AcksMultipleStanzas() {
			boost::shared_ptr<StanzaAckResponder> testling(createResponder());
			testling->handleStanzaReceived();
			testling->handleStanzaReceived();

			testling->handleAckRequestReceived();

			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(acks.size()));
			CPPUNIT_ASSERT_EQUAL(2U, acks[0]);
		}

		void testHandleAckRequestReceived_MultipleAcks() {
			boost::shared_ptr<StanzaAckResponder> testling(createResponder());
			testling->handleStanzaReceived();
			testling->handleAckRequestReceived();

			testling->handleStanzaReceived();
			testling->handleAckRequestReceived();

			CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(acks.size()));
			CPPUNIT_ASSERT_EQUAL(1U, acks[0]);
			CPPUNIT_ASSERT_EQUAL(2U, acks[1]);
		}

		// Handle stanza ack count wrapping, as per the XEP
		void testHandleAckRequestReceived_WrapAround() {
			boost::shared_ptr<StanzaAckResponder> testling(createResponder());
			testling->handledStanzasCount = boost::numeric_cast<unsigned int>((1ULL<<32) - 1);
			testling->handleStanzaReceived();
			testling->handleStanzaReceived();

			testling->handleAckRequestReceived();

			CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(acks.size()));
			CPPUNIT_ASSERT_EQUAL(1U, acks[0]);
		}

	private:
		Message::ref createMessage(const std::string& id) {
			Message::ref result(new Message());
			result->setID(id);
			return result;
		}

		StanzaAckResponder* createResponder() {
			StanzaAckResponder* responder = new StanzaAckResponder();
			responder->onAck.connect(boost::bind(&StanzaAckResponderTest::handleAck, this, _1));
			return responder;
		}

		void handleAck(unsigned int h) {
			acks.push_back(h);
		}

	private:
		std::vector<unsigned int> acks;
};

}

CPPUNIT_TEST_SUITE_REGISTRATION(Swift::StanzaAckResponderTest);