summaryrefslogtreecommitdiffstats
blob: 3fad433f5f633d1e2c481dccf647a45e0484b6ff (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <memory>

#include <boost/bind.hpp>

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

#include <Swiften/EventLoop/DummyEventLoop.h>
#include <Swiften/Network/ChainedConnector.h>
#include <Swiften/Network/Connection.h>
#include <Swiften/Network/ConnectionFactory.h>
#include <Swiften/Network/DomainNameResolveError.h>
#include <Swiften/Network/DummyTimerFactory.h>
#include <Swiften/Network/HostAddressPort.h>
#include <Swiften/Network/StaticDomainNameResolver.h>

using namespace Swift;

class ChainedConnectorTest : public CppUnit::TestFixture {
        CPPUNIT_TEST_SUITE(ChainedConnectorTest);
        CPPUNIT_TEST(testConnect_FirstConnectorSucceeds);
        CPPUNIT_TEST(testConnect_SecondConnectorSucceeds);
        CPPUNIT_TEST(testConnect_NoConnectorSucceeds);
        CPPUNIT_TEST(testConnect_NoDNS);
        CPPUNIT_TEST(testStop);
        CPPUNIT_TEST_SUITE_END();

    public:
        void setUp() {
            error.reset();
            host = HostAddressPort(HostAddress("1.1.1.1"), 1234);
            eventLoop = new DummyEventLoop();
            resolver = new StaticDomainNameResolver(eventLoop);
            resolver->addXMPPClientService("foo.com", host);
            connectionFactory1 = new MockConnectionFactory(eventLoop, 1);
            connectionFactory2 = new MockConnectionFactory(eventLoop, 2);
            timerFactory = new DummyTimerFactory();
        }

        void tearDown() {
            delete timerFactory;
            delete connectionFactory2;
            delete connectionFactory1;
            delete resolver;
            delete eventLoop;
        }

        void testConnect_FirstConnectorSucceeds() {
            std::shared_ptr<ChainedConnector> testling(createConnector());
            connectionFactory1->connects = true;
            connectionFactory2->connects = false;

            testling->start();
            eventLoop->processEvents();

            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(connections.size()));
            CPPUNIT_ASSERT(connections[0]);
            CPPUNIT_ASSERT_EQUAL(1, std::dynamic_pointer_cast<MockConnection>(connections[0])->id);
            CPPUNIT_ASSERT(!std::dynamic_pointer_cast<DomainNameResolveError>(error));
        }

        void testConnect_SecondConnectorSucceeds() {
            std::shared_ptr<ChainedConnector> testling(createConnector());
            connectionFactory1->connects = false;
            connectionFactory2->connects = true;

            testling->start();
            eventLoop->processEvents();

            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(connections.size()));
            CPPUNIT_ASSERT(connections[0]);
            CPPUNIT_ASSERT_EQUAL(2, std::dynamic_pointer_cast<MockConnection>(connections[0])->id);
            CPPUNIT_ASSERT(!std::dynamic_pointer_cast<DomainNameResolveError>(error));
        }

        void testConnect_NoConnectorSucceeds() {
            std::shared_ptr<ChainedConnector> testling(createConnector());
            connectionFactory1->connects = false;
            connectionFactory2->connects = false;

            testling->start();
            eventLoop->processEvents();

            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(connections.size()));
            CPPUNIT_ASSERT(!connections[0]);
            CPPUNIT_ASSERT(!std::dynamic_pointer_cast<DomainNameResolveError>(error));
        }

        void testConnect_NoDNS() {
            /* Reset resolver so there's no record */
            delete resolver;
            resolver = new StaticDomainNameResolver(eventLoop);
            std::shared_ptr<ChainedConnector> testling(createConnector());
            connectionFactory1->connects = false;
            connectionFactory2->connects = false;

            testling->start();
            //testling->stop();
            eventLoop->processEvents();

            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(connections.size()));
            CPPUNIT_ASSERT(!connections[0]);
            CPPUNIT_ASSERT(std::dynamic_pointer_cast<DomainNameResolveError>(error));
        }

        void testStop() {
            std::shared_ptr<ChainedConnector> testling(createConnector());
            connectionFactory1->connects = true;
            connectionFactory2->connects = false;

            testling->start();
            testling->stop();
            eventLoop->processEvents();

            CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(connections.size()));
            CPPUNIT_ASSERT(!connections[0]);
        }

    private:
        std::shared_ptr<ChainedConnector> createConnector() {
            std::vector<ConnectionFactory*> factories;
            factories.push_back(connectionFactory1);
            factories.push_back(connectionFactory2);
            std::shared_ptr<ChainedConnector> connector = std::make_shared<ChainedConnector>("foo.com", -1, boost::optional<std::string>("_xmpp-client._tcp."), resolver, factories, timerFactory);
            connector->onConnectFinished.connect(boost::bind(&ChainedConnectorTest::handleConnectorFinished, this, _1, _2));
            return connector;
        }

        void handleConnectorFinished(std::shared_ptr<Connection> connection, std::shared_ptr<Error> resultError) {
            error = resultError;
            std::shared_ptr<MockConnection> c(std::dynamic_pointer_cast<MockConnection>(connection));
            if (connection) {
                assert(c);
            }
            connections.push_back(c);
        }

        struct MockConnection : public Connection {
            public:
                MockConnection(bool connects, int id, EventLoop* eventLoop) : connects(connects), id(id), eventLoop(eventLoop) {
                }

                void listen() { assert(false); }
                void connect(const HostAddressPort&) {
                    eventLoop->postEvent(boost::bind(boost::ref(onConnectFinished), !connects));
                }

                HostAddressPort getLocalAddress() const { return HostAddressPort(); }
                HostAddressPort getRemoteAddress() const { return HostAddressPort(); }
                void disconnect() { assert(false); }
                void write(const SafeByteArray&) { assert(false); }

                bool connects;
                int id;
                EventLoop* eventLoop;
        };

        struct MockConnectionFactory : public ConnectionFactory {
            MockConnectionFactory(EventLoop* eventLoop, int id) : eventLoop(eventLoop), connects(true), id(id) {
            }

            std::shared_ptr<Connection> createConnection() {
                return std::make_shared<MockConnection>(connects, id, eventLoop);
            }

            EventLoop* eventLoop;
            bool connects;
            int id;
        };

    private:
        HostAddressPort host;
        DummyEventLoop* eventLoop;
        StaticDomainNameResolver* resolver;
        MockConnectionFactory* connectionFactory1;
        MockConnectionFactory* connectionFactory2;
        DummyTimerFactory* timerFactory;
        std::vector< std::shared_ptr<MockConnection> > connections;
        std::shared_ptr<Error> error;
};

CPPUNIT_TEST_SUITE_REGISTRATION(ChainedConnectorTest);