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

#include <typeinfo>

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

#include <Swiften/Disco/DiscoInfoResponder.h>
#include <Swiften/Queries/DummyIQChannel.h>
#include <Swiften/Queries/IQRouter.h>

using namespace Swift;

class DiscoInfoResponderTest : public CppUnit::TestFixture {
        CPPUNIT_TEST_SUITE(DiscoInfoResponderTest);
        CPPUNIT_TEST(testHandleRequest_GetToplevelInfo);
        CPPUNIT_TEST(testHandleRequest_GetNodeInfo);
        CPPUNIT_TEST(testHandleRequest_GetInvalidNodeInfo);
        CPPUNIT_TEST_SUITE_END();

    public:
        void setUp() {
            channel_ = new DummyIQChannel();
            router_ = new IQRouter(channel_);
        }

        void tearDown() {
            delete router_;
            delete channel_;
        }

        void testHandleRequest_GetToplevelInfo() {
            DiscoInfoResponder testling(router_);
            testling.start();
            DiscoInfo discoInfo;
            discoInfo.addFeature("foo");
            testling.setDiscoInfo(discoInfo);

            std::shared_ptr<DiscoInfo> query(new DiscoInfo());
            channel_->onIQReceived(IQ::createRequest(IQ::Get, JID("foo@bar.com"), "id-1", query));

            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel_->iqs_.size()));
            std::shared_ptr<DiscoInfo> payload(channel_->iqs_[0]->getPayload<DiscoInfo>());
            CPPUNIT_ASSERT(payload);
            CPPUNIT_ASSERT_EQUAL(std::string(""), payload->getNode());
            CPPUNIT_ASSERT(payload->hasFeature("foo"));

            testling.stop();
        }

        void testHandleRequest_GetNodeInfo() {
            DiscoInfoResponder testling(router_);
            testling.start();
            DiscoInfo discoInfo;
            discoInfo.addFeature("foo");
            testling.setDiscoInfo(discoInfo);
            DiscoInfo discoInfoBar;
            discoInfoBar.addFeature("bar");
            testling.setDiscoInfo("bar-node", discoInfoBar);

            std::shared_ptr<DiscoInfo> query(new DiscoInfo());
            query->setNode("bar-node");
            channel_->onIQReceived(IQ::createRequest(IQ::Get, JID("foo@bar.com"), "id-1", query));

            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel_->iqs_.size()));
            std::shared_ptr<DiscoInfo> payload(channel_->iqs_[0]->getPayload<DiscoInfo>());
            CPPUNIT_ASSERT(payload);
            CPPUNIT_ASSERT_EQUAL(std::string("bar-node"), payload->getNode());
            CPPUNIT_ASSERT(payload->hasFeature("bar"));

            testling.stop();
        }

        void testHandleRequest_GetInvalidNodeInfo() {
            DiscoInfoResponder testling(router_);
            std::shared_ptr<DiscoInfo> query(new DiscoInfo());
            query->setNode("bar-node");
            channel_->onIQReceived(IQ::createRequest(IQ::Get, JID("foo@bar.com"), "id-1", query));
            testling.start();

            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel_->iqs_.size()));
            std::shared_ptr<ErrorPayload> payload(channel_->iqs_[0]->getPayload<ErrorPayload>());
            CPPUNIT_ASSERT(payload);

            testling.stop();
        }

    private:
        IQRouter* router_;
        DummyIQChannel* channel_;
};

CPPUNIT_TEST_SUITE_REGISTRATION(DiscoInfoResponderTest);