summaryrefslogtreecommitdiffstats
path: root/test/httpcrashtest.cpp
blob: 915b11801a403e46b8bdae38104d070252feaa24 (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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * 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 <config.h>

#include <errno.h>
#include <signal.h>
#include <sys/types.h>

#include <cstring>

#include <Poco/Dynamic/Var.h>
#include <Poco/JSON/JSON.h>
#include <Poco/JSON/Parser.h>
#include <Poco/Net/AcceptCertificateHandler.h>
#include <Poco/Net/HTTPClientSession.h>
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/Net/HTTPSClientSession.h>
#include <Poco/Net/InvalidCertificateHandler.h>
#include <Poco/Net/NetException.h>
#include <Poco/Net/PrivateKeyPassphraseHandler.h>
#include <Poco/Net/SSLManager.h>
#include <Poco/Net/Socket.h>
#include <Poco/Path.h>
#include <Poco/StreamCopier.h>
#include <Poco/URI.h>
#include <cppunit/extensions/HelperMacros.h>

#include <Common.hpp>
#include <UserMessages.hpp>
#include <Util.hpp>
#include <Protocol.hpp>
#include <LOOLWebSocket.hpp>
#include <test.hpp>
#include <helpers.hpp>
#include <countloolkits.hpp>

using namespace helpers;

/// Tests the HTTP WebSocket API of loolwsd. The server has to be started manually before running this test.
class HTTPCrashTest : public CPPUNIT_NS::TestFixture
{
    const Poco::URI _uri;
    Poco::Net::HTTPResponse _response;

    CPPUNIT_TEST_SUITE(HTTPCrashTest);

    CPPUNIT_TEST(testBarren);
    CPPUNIT_TEST(testCrashKit);
    CPPUNIT_TEST(testRecoverAfterKitCrash);
    CPPUNIT_TEST(testCrashForkit);

    CPPUNIT_TEST_SUITE_END();

    void testBarren();
    void testCrashKit();
    void testRecoverAfterKitCrash();
    void testCrashForkit();

    static
    void killLoKitProcesses();
    void killForkitProcess();

public:
    HTTPCrashTest()
        : _uri(helpers::getTestServerURI())
    {
#if ENABLE_SSL
        Poco::Net::initializeSSL();
        // Just accept the certificate anyway for testing purposes
        Poco::SharedPtr<Poco::Net::InvalidCertificateHandler> invalidCertHandler = new Poco::Net::AcceptCertificateHandler(false);
        Poco::Net::Context::Params sslParams;
        Poco::Net::Context::Ptr sslContext = new Poco::Net::Context(Poco::Net::Context::CLIENT_USE, sslParams);
        Poco::Net::SSLManager::instance().initializeClient(nullptr, invalidCertHandler, sslContext);
#endif
    }

#if ENABLE_SSL
    ~HTTPCrashTest()
    {
        Poco::Net::uninitializeSSL();
    }
#endif

    void setUp()
    {
        resetTestStartTime();
        testCountHowManyLoolkits();
        resetTestStartTime();
    }

    void tearDown()
    {
        resetTestStartTime();
        testNoExtraLoolKitsLeft();
        resetTestStartTime();
    }
};

void HTTPCrashTest::testBarren()
{
#if 0 // FIXME why does this fail?
    // Kill all kit processes and try loading a document.
    const char* testname = "barren ";
    try
    {
        killLoKitProcesses();
        countLoolKitProcesses(0);

        TST_LOG("Loading after kill.");

        // Load a document and get its status.
        std::shared_ptr<LOOLWebSocket> socket = loadDocAndGetSocket("hello.odt", _uri, testname);

        sendTextFrame(socket, "status", testname);
        assertResponseString(socket, "status:", testname);
    }
    catch (const Poco::Exception& exc)
    {
        LOK_ASSERT_FAIL(exc.displayText());
    }
#endif
}

void HTTPCrashTest::testCrashKit()
{
    const char* testname = "crashKit ";
    try
    {
        std::shared_ptr<LOOLWebSocket> socket = loadDocAndGetSocket("empty.odt", _uri, testname);

        TST_LOG("Allowing time for kits to spawn and connect to wsd to get cleanly killed");
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));

        TST_LOG("Killing loolkit instances.");

        killLoKitProcesses();
        countLoolKitProcesses(0);

        // We expect the client connection to close.
        // In the future we might restore the kit, but currently we don't.
        TST_LOG("Reading after kill.");

        // Drain the socket.
        getResponseMessage(socket, "", testname, 1000);

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

        // respond close frame
        TST_LOG("Shutting down socket.");
        socket->shutdown();

        TST_LOG("Reading after shutdown.");

        // no more messages is received.
        int flags;
        char buffer[READ_BUFFER_SIZE];
        const int bytes = socket->receiveFrame(buffer, sizeof(buffer), flags);
        TST_LOG(testname << "Got " << LOOLWebSocket::getAbbreviatedFrameDump(buffer, bytes, flags));

        // While we expect no more messages after shutdown call, apparently
        // sometimes we _do_ get data. Even when the receiveFrame in the loop
        // returns a CLOSE frame (with 2 bytes) the one after shutdown sometimes
        // returns a BINARY frame with the next payload sent by wsd.
        // This is an oddity of Poco and is not something we need to validate here.
        //LOK_ASSERT_MESSAGE("Expected no more data", bytes <= 2); // The 2-byte marker is ok.
        //LOK_ASSERT_EQUAL(0x88, flags);
    }
    catch (const Poco::Exception& exc)
    {
        LOK_ASSERT_FAIL(exc.displayText());
    }
}

void HTTPCrashTest::testRecoverAfterKitCrash()
{
    const char* testname = "recoverAfterKitCrash ";
    try
    {
        std::shared_ptr<LOOLWebSocket> socket = loadDocAndGetSocket("empty.odt", _uri, testname);

        TST_LOG("Killing loolkit instances.");

        killLoKitProcesses();
        countLoolKitProcesses(0);

        // We expect the client connection to close.
        TST_LOG("Reconnect after kill.");

        std::shared_ptr<LOOLWebSocket> socket2 = loadDocAndGetSocket("empty.odt", _uri, testname, /*isView=*/true, /*isAssert=*/false);
        if (!socket2)
        {
            // In case still starting up.
            sleep(2);
            socket2 = loadDocAndGetSocket("empty.odt", _uri, testname);
        }
        sendTextFrame(socket2, "status", testname);
        assertResponseString(socket2, "status:", testname);
    }
    catch (const Poco::Exception& exc)
    {
        LOK_ASSERT_FAIL(exc.displayText());
    }
}

void HTTPCrashTest::testCrashForkit()
{
    const char* testname = "crashForkit ";
    try
    {
        std::shared_ptr<LOOLWebSocket> socket = loadDocAndGetSocket("empty.odt", _uri, testname);

        TST_LOG("Killing forkit.");
        killForkitProcess();
        TST_LOG("Communicating after kill.");

        sendTextFrame(socket, "status", testname);
        assertResponseString(socket, "status:", testname);

        // respond close frame
        socket->shutdown();

        TST_LOG("Killing loolkit.");
        killLoKitProcesses();
        countLoolKitProcesses(0);
        TST_LOG("Communicating after kill.");
        loadDocAndGetSocket("empty.odt", _uri, testname);
    }
    catch (const Poco::Exception& exc)
    {
        LOK_ASSERT_FAIL(exc.displayText());
    }
}

static void killPids(const std::vector<int> &pids, const std::string& testname)
{
    TST_LOG("kill pids " << pids.size());
    // Now kill them
    for (int pid : pids)
    {
        TST_LOG_BEGIN("Killing " << pid);
        if (kill(pid, SIGKILL) == -1)
            TST_LOG_APPEND("kill(" << pid << ", SIGKILL) failed: " << Util::symbolicErrno(errno) << ": " << std::strerror(errno));
        TST_LOG_END;
    }
}

void HTTPCrashTest::killLoKitProcesses()
{
    killPids(getKitPids(), "killLoKitProcesses ");
    InitialLoolKitCount = 1; // non-intuitive but it will arrive soon.
}

void HTTPCrashTest::killForkitProcess()
{
    killPids(getForKitPids(), "killForkitProcess ");
}

CPPUNIT_TEST_SUITE_REGISTRATION(HTTPCrashTest);

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