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

#include <memory>
#include <vector>

#include <boost/bind.hpp>

#include <QA/Checker/IO.h>

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

#include <Swiften/Base/ByteArray.h>
#include <Swiften/Base/Concat.h>
#include <Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.h>
#include <Swiften/Parser/PlatformXMLParserFactory.h>
#include <Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.h>
#include <Swiften/StreamStack/LowLayer.h>
#include <Swiften/StreamStack/StreamLayer.h>
#include <Swiften/StreamStack/StreamStack.h>
#include <Swiften/StreamStack/XMPPLayer.h>

using namespace Swift;

class StreamStackTest : public CppUnit::TestFixture {
        CPPUNIT_TEST_SUITE(StreamStackTest);
        CPPUNIT_TEST(testWriteData_NoIntermediateStreamStack);
        CPPUNIT_TEST(testWriteData_OneIntermediateStream);
        CPPUNIT_TEST(testWriteData_TwoIntermediateStreamStack);
        CPPUNIT_TEST(testReadData_NoIntermediateStreamStack);
        CPPUNIT_TEST(testReadData_OneIntermediateStream);
        CPPUNIT_TEST(testReadData_TwoIntermediateStreamStack);
        CPPUNIT_TEST(testAddLayer_ExistingOnWriteDataSlot);
        CPPUNIT_TEST_SUITE_END();

    public:
        void setUp() {
            testling_ = std::make_unique<StreamStack>(std::make_unique<XMPPLayer>(&parserFactories_, &serializers_, &xmlParserFactory_, ClientStreamType), std::make_unique<TestLowLayer>());
            physicalStream_ = testling_->getLayer<TestLowLayer>();
            xmppStream_ = testling_->getLayer<XMPPLayer>();
            elementsReceived_ = 0;
            dataWriteReceived_ = 0;
        }

        void tearDown() {
        }

        void testWriteData_NoIntermediateStreamStack() {

            xmppStream_->writeData("foo");

            CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), physicalStream_->data_.size());
            CPPUNIT_ASSERT_EQUAL(createSafeByteArray("foo"), physicalStream_->data_[0]);
        }

        void testWriteData_OneIntermediateStream() {
            std::unique_ptr<MyStreamLayer> xStream(new MyStreamLayer("X"));
            testling_->addLayer(std::move(xStream));

            xmppStream_->writeData("foo");

            CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), physicalStream_->data_.size());
            CPPUNIT_ASSERT_EQUAL(createSafeByteArray("Xfoo"), physicalStream_->data_[0]);
        }

        void testWriteData_TwoIntermediateStreamStack() {
            std::unique_ptr<MyStreamLayer> xStream(new MyStreamLayer("X"));
            std::unique_ptr<MyStreamLayer> yStream(new MyStreamLayer("Y"));
            testling_->addLayer(std::move(xStream));
            testling_->addLayer(std::move(yStream));

            xmppStream_->writeData("foo");

            CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), physicalStream_->data_.size());
            CPPUNIT_ASSERT_EQUAL(createSafeByteArray("XYfoo"), physicalStream_->data_[0]);
        }

        void testReadData_NoIntermediateStreamStack() {
            xmppStream_->onElement.connect(boost::bind(&StreamStackTest::handleElement, this, _1));

            physicalStream_->onDataRead(createSafeByteArray("<stream:stream xmlns:stream='http://etherx.jabber.org/streams'><presence/>"));

            CPPUNIT_ASSERT_EQUAL(1, elementsReceived_);
        }

        void testReadData_OneIntermediateStream() {
            xmppStream_->onElement.connect(boost::bind(&StreamStackTest::handleElement, this, _1));
            std::unique_ptr<MyStreamLayer> xStream(new MyStreamLayer("<"));
            testling_->addLayer(std::move(xStream));

            physicalStream_->onDataRead(createSafeByteArray("stream:stream xmlns:stream='http://etherx.jabber.org/streams'><presence/>"));

            CPPUNIT_ASSERT_EQUAL(1, elementsReceived_);
        }

        void testReadData_TwoIntermediateStreamStack() {
            xmppStream_->onElement.connect(boost::bind(&StreamStackTest::handleElement, this, _1));
            std::unique_ptr<MyStreamLayer> xStream(new MyStreamLayer("s"));
            std::unique_ptr<MyStreamLayer> yStream(new MyStreamLayer("<"));
            testling_->addLayer(std::move(xStream));
            testling_->addLayer(std::move(yStream));

            physicalStream_->onDataRead(createSafeByteArray("tream:stream xmlns:stream='http://etherx.jabber.org/streams'><presence/>"));

            CPPUNIT_ASSERT_EQUAL(1, elementsReceived_);
        }

        void testAddLayer_ExistingOnWriteDataSlot() {
            xmppStream_->onWriteData.connect(boost::bind(&StreamStackTest::handleWriteData, this, _1));
            std::unique_ptr<MyStreamLayer> xStream(new MyStreamLayer("X"));
            testling_->addLayer(std::move(xStream));

            xmppStream_->writeData("foo");

            CPPUNIT_ASSERT_EQUAL(1, dataWriteReceived_);
        }

        void handleElement(std::shared_ptr<ToplevelElement>) {
            ++elementsReceived_;
        }

        void handleWriteData(const SafeByteArray&) {
            ++dataWriteReceived_;
        }

    private:
        class MyStreamLayer : public StreamLayer {
            public:
                MyStreamLayer(const std::string& prepend) : prepend_(prepend) {
                }

                virtual void writeData(const SafeByteArray& data) {
                    writeDataToChildLayer(concat(createSafeByteArray(prepend_), data));
                }

                virtual void handleDataRead(const SafeByteArray& data) {
                    writeDataToParentLayer(concat(createSafeByteArray(prepend_), data));
                }

            private:
                std::string prepend_;
        };

        class TestLowLayer : public LowLayer {
            public:
                TestLowLayer() {
                }

                virtual void writeData(const SafeByteArray& data) {
                    data_.push_back(data);
                }

                void onDataRead(const SafeByteArray& data) {
                    writeDataToParentLayer(data);
                }

                std::vector<SafeByteArray> data_;
        };


    private:
        FullPayloadParserFactoryCollection parserFactories_;
        FullPayloadSerializerCollection serializers_;
        TestLowLayer* physicalStream_;
        PlatformXMLParserFactory xmlParserFactory_;
        XMPPLayer* xmppStream_;
        std::unique_ptr<StreamStack> testling_;
        int elementsReceived_;
        int dataWriteReceived_;
};

CPPUNIT_TEST_SUITE_REGISTRATION(StreamStackTest);