summaryrefslogtreecommitdiffstats
blob: ee4b913c5fc0daaa02db0aa5115980c56523bbf4 (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
99
100
/*
 * Copyright (c) 2010-2018 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <cstdint>

#include <boost/bind.hpp>

#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>

#include <Swiften/Elements/Message.h>
#include <Swiften/StreamManagement/StanzaAckResponder.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() {
            std::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() {
            std::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() {
            std::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() {
            std::shared_ptr<StanzaAckResponder> testling(createResponder());
            testling->handledStanzasCount = UINT32_MAX;
            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);