summaryrefslogtreecommitdiff
path: root/test/UnitBadDocLoad.cpp
blob: 550191f6972cf6af63e2973a12d29f156faeb0eb (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <memory>
#include <ostream>
#include <set>
#include <string>

#include <Poco/Exception.h>
#include <Poco/RegularExpression.h>
#include <Poco/URI.h>
#include <test/lokassert.hpp>

#include <Png.hpp>
#include <Unit.hpp>
#include <helpers.hpp>

// Include config.h last, so the test server URI is still HTTP, even in SSL builds.
#include <config.h>

class LOOLWebSocket;

/// Test suite for bad document loading, etc.
class UnitBadDocLoad : public UnitWSD
{
    TestResult testBadDocLoadFail();
    TestResult testMaxDocuments();
    TestResult testMaxConnections();
    TestResult testMaxViews();

public:
    void invokeTest() override;
};

UnitBase::TestResult UnitBadDocLoad::testBadDocLoadFail()
{
    // Load corrupted document and validate error.
    const char* testname = "docLoadFail ";
    try
    {
        std::string documentPath, documentURL;
        helpers::getDocumentPathAndURL("corrupted.odt", documentPath, documentURL, testname);

        Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL);
        Poco::URI uri(helpers::getTestServerURI());
        Poco::Net::HTTPResponse httpResponse;
        std::shared_ptr<LOOLWebSocket> socket
            = helpers::connectLOKit(uri, request, httpResponse, testname);

        // Send a load request with incorrect password
        helpers::sendTextFrame(socket, "load url=" + documentURL, testname);

        const auto response = helpers::getResponseString(socket, "error:", testname);
        StringVector tokens(Util::tokenize(response, ' '));
        LOK_ASSERT_EQUAL(static_cast<size_t>(3), tokens.size());

        std::string errorCommand;
        std::string errorKind;
        LOOLProtocol::getTokenString(tokens[1], "cmd", errorCommand);
        LOOLProtocol::getTokenString(tokens[2], "kind", errorKind);
        LOK_ASSERT_EQUAL(std::string("load"), errorCommand);
        LOK_ASSERT_EQUAL(std::string("faileddocloading"), errorKind);
    }
    catch (const Poco::Exception& exc)
    {
        LOK_ASSERT_FAIL(exc.displayText());
    }
    return TestResult::Ok;
}

UnitBase::TestResult UnitBadDocLoad::testMaxDocuments()
{
    static_assert(MAX_DOCUMENTS >= 2, "MAX_DOCUMENTS must be at least 2");
    const char* testname = "maxDocuments ";

    if (MAX_DOCUMENTS > 20)
    {
        std::cerr << "Skipping " << testname << "test since MAX_DOCUMENTS (" << MAX_DOCUMENTS
                  << ") is too high to test. Set to a more sensible number, ideally a dozen or so."
                  << std::endl;
        return TestResult::Ok;
    }

    try
    {
        // Load a document.
        std::vector<std::shared_ptr<LOOLWebSocket>> docs;

        std::cerr << "Loading max number of documents: " << MAX_DOCUMENTS << std::endl;
        for (int it = 1; it <= MAX_DOCUMENTS; ++it)
        {
            Poco::URI uri(helpers::getTestServerURI());
            docs.emplace_back(helpers::loadDocAndGetSocket("empty.odt", uri, testname));
            std::cerr << "Loaded document #" << it << " of " << MAX_DOCUMENTS << std::endl;
        }

        std::cerr << "Loading one more document beyond the limit." << std::endl;

        // try to open MAX_DOCUMENTS + 1
        std::string docPath;
        std::string docURL;
        helpers::getDocumentPathAndURL("empty.odt", docPath, docURL, testname);
        Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, docURL);
        Poco::URI uri(helpers::getTestServerURI());
        std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(uri));
        Poco::Net::HTTPResponse httpResponse;
        auto socket = std::make_shared<LOOLWebSocket>(*session, request, httpResponse);

        // Send load request, which will fail.
        helpers::sendTextFrame(socket, "load url=" + docURL, testname);

        helpers::assertResponseString(socket, "error:", testname);

        std::string message;
        const int statusCode = helpers::getErrorCode(socket, message, testname);
        LOK_ASSERT_EQUAL(static_cast<int>(Poco::Net::WebSocket::WS_POLICY_VIOLATION),
                             statusCode);

        socket->shutdown();
    }
    catch (const Poco::Exception& exc)
    {
        LOK_ASSERT_FAIL(exc.displayText());
    }
    return TestResult::Ok;
}

UnitBase::TestResult UnitBadDocLoad::testMaxConnections()
{
    static_assert(MAX_CONNECTIONS >= 3, "MAX_CONNECTIONS must be at least 3");
    const char* testname = "maxConnections ";

    if (MAX_CONNECTIONS > 40)
    {
        std::cerr << "Skipping " << testname << "test since MAX_CONNECTION (" << MAX_CONNECTIONS
                  << ") is too high to test. Set to a more sensible number, ideally a dozen or so."
                  << std::endl;
        return TestResult::Ok;
    }

    try
    {
        std::cerr << "Opening max number of connections: " << MAX_CONNECTIONS << std::endl;

        // Load a document.
        std::string docPath;
        std::string docURL;

        helpers::getDocumentPathAndURL("empty.odt", docPath, docURL, testname);
        Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, docURL);
        Poco::URI uri(helpers::getTestServerURI());
        std::shared_ptr<LOOLWebSocket> socket = helpers::loadDocAndGetSocket(uri, docURL, testname);
        std::cerr << "Opened connection #1 of " << MAX_CONNECTIONS << std::endl;

        std::vector<std::shared_ptr<LOOLWebSocket>> views;
        for (int it = 1; it < MAX_CONNECTIONS; ++it)
        {
            std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(uri));
            Poco::Net::HTTPResponse httpResponse;
            auto ws = std::make_shared<LOOLWebSocket>(*session, request, httpResponse);
            views.emplace_back(ws);
            std::cerr << "Opened connection #" << (it + 1) << " of " << MAX_CONNECTIONS
                      << std::endl;
        }

        std::cerr << "Opening one more connection beyond the limit." << std::endl;

        // try to connect MAX_CONNECTIONS + 1
        std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(uri));
        Poco::Net::HTTPResponse httpResponse;
        auto socketN = std::make_shared<LOOLWebSocket>(*session, request, httpResponse);

        // Send load request, which will fail.
        helpers::sendTextFrame(socketN, "load url=" + docURL, testname);

        std::string message;
        const int statusCode = helpers::getErrorCode(socketN, message, testname);
        LOK_ASSERT_EQUAL(static_cast<int>(Poco::Net::WebSocket::WS_POLICY_VIOLATION),
                             statusCode);

        socketN->shutdown();
    }
    catch (const Poco::Exception& exc)
    {
        LOK_ASSERT_FAIL(exc.displayText());
    }
    return TestResult::Ok;
}

UnitBase::TestResult UnitBadDocLoad::testMaxViews()
{
    static_assert(MAX_CONNECTIONS >= 3, "MAX_CONNECTIONS must be at least 3");
    const char* testname = "maxViews ";

    if (MAX_CONNECTIONS > 40)
    {
        std::cerr << "Skipping " << testname << "test since MAX_CONNECTION (" << MAX_CONNECTIONS
                  << ") is too high to test. Set to a more sensible number, ideally a dozen or so."
                  << std::endl;
        return TestResult::Ok;
    }

    try
    {
        std::cerr << "Opening max number of views: " << MAX_CONNECTIONS << std::endl;

        // Load a document.
        std::string docPath;
        std::string docURL;

        helpers::getDocumentPathAndURL("empty.odt", docPath, docURL, testname);
        Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, docURL);
        Poco::URI uri(helpers::getTestServerURI());
        std::shared_ptr<LOOLWebSocket> socket = helpers::loadDocAndGetSocket(uri, docURL, testname);
        std::cerr << "Opened view #1 of " << MAX_CONNECTIONS << std::endl;

        std::vector<std::shared_ptr<LOOLWebSocket>> views;
        for (int it = 1; it < MAX_CONNECTIONS; ++it)
        {
            views.emplace_back(helpers::loadDocAndGetSocket(uri, docURL, testname));
            std::cerr << "Opened view #" << (it + 1) << " of " << MAX_CONNECTIONS << std::endl;
        }

        std::cerr << "Opening one more connection beyond the limit." << std::endl;

        // try to connect MAX_CONNECTIONS + 1
        std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(uri));
        Poco::Net::HTTPResponse httpResponse;
        auto socketN = std::make_shared<LOOLWebSocket>(*session, request, httpResponse);

        // Send load request, which will fail.
        helpers::sendTextFrame(socketN, "load url=" + docURL, testname);

        std::string message;
        const int statusCode = helpers::getErrorCode(socketN, message, testname);
        LOK_ASSERT_EQUAL(static_cast<int>(Poco::Net::WebSocket::WS_POLICY_VIOLATION),
                             statusCode);
    }
    catch (const Poco::Exception& exc)
    {
        LOK_ASSERT_FAIL(exc.displayText());
    }
    return TestResult::Ok;
}

void UnitBadDocLoad::invokeTest()
{
    UnitBase::TestResult result = testBadDocLoadFail();
    if (result != TestResult::Ok)
        exitTest(result);

// FIXME: Disabled recently - breaking the tests - should
//        check for the warning popup instead.
#if 0
    result = testMaxDocuments();
    if (result != TestResult::Ok)
        exitTest(result);

    result = testMaxConnections();
    if (result != TestResult::Ok)
        exitTest(result);

    result = testMaxViews();
    if (result != TestResult::Ok)
        exitTest(result);
#endif

    exitTest(TestResult::Ok);
}

UnitBase* unit_create_wsd(void) { return new UnitBadDocLoad(); }

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */