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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
|
/* -*- 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 <config.h>
#include <Poco/Net/AcceptCertificateHandler.h>
#include <Poco/Net/FilePartSource.h>
#include <Poco/Net/HTMLForm.h>
#include <Poco/Net/HTTPClientSession.h>
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/Net/InvalidCertificateHandler.h>
#include <Poco/Net/SSLManager.h>
#include <Poco/RegularExpression.h>
#include <Poco/StreamCopier.h>
#include <Poco/URI.h>
#include <cppunit/extensions/HelperMacros.h>
#include <Common.hpp>
#include <common/FileUtil.hpp>
#include <countloolkits.hpp>
#include <helpers.hpp>
/// Tests the HTTP GET API of loolwsd.
class HTTPServerTest : public CPPUNIT_NS::TestFixture
{
const Poco::URI _uri;
CPPUNIT_TEST_SUITE(HTTPServerTest);
CPPUNIT_TEST(testLoleafletGet);
CPPUNIT_TEST(testLoleafletPost);
CPPUNIT_TEST(testScriptsAndLinksGet);
CPPUNIT_TEST(testScriptsAndLinksPost);
CPPUNIT_TEST(testConvertTo);
CPPUNIT_TEST(testConvertTo2);
CPPUNIT_TEST(testConvertToWithForwardedClientIP);
CPPUNIT_TEST_SUITE_END();
void testLoleafletGet();
void testLoleafletPost();
void testScriptsAndLinksGet();
void testScriptsAndLinksPost();
void testConvertTo();
void testConvertTo2();
void testConvertToWithForwardedClientIP();
public:
HTTPServerTest()
: _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
}
~HTTPServerTest()
{
#if ENABLE_SSL
Poco::Net::uninitializeSSL();
#endif
}
void setUp()
{
helpers::resetTestStartTime();
testCountHowManyLoolkits();
helpers::resetTestStartTime();
}
void tearDown()
{
helpers::resetTestStartTime();
testNoExtraLoolKitsLeft();
helpers::resetTestStartTime();
}
// A server URI which was not added to loolwsd.xml as post_allow IP or a wopi storage host
Poco::URI getNotAllowedTestServerURI()
{
static std::string serverURI(
#if ENABLE_SSL
"https://165.227.162.232:9980"
#else
"http://165.227.162.232:9980"
#endif
);
return Poco::URI(serverURI);
}
};
void HTTPServerTest::testLoleafletGet()
{
std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, "/loleaflet/dist/loleaflet.html?access_token=111111111");
Poco::Net::HTMLForm param(request);
session->sendRequest(request);
Poco::Net::HTTPResponse response;
std::istream& rs = session->receiveResponse(response);
LOK_ASSERT_EQUAL(Poco::Net::HTTPResponse::HTTP_OK, response.getStatus());
LOK_ASSERT_EQUAL(std::string("text/html"), response.getContentType());
std::string html;
Poco::StreamCopier::copyToString(rs, html);
LOK_ASSERT(html.find(param["access_token"]) != std::string::npos);
LOK_ASSERT(html.find(_uri.getHost()) != std::string::npos);
LOK_ASSERT(html.find(std::string(LOOLWSD_VERSION_HASH)) != std::string::npos);
}
void HTTPServerTest::testLoleafletPost()
{
std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/loleaflet/dist/loleaflet.html");
Poco::Net::HTMLForm form;
form.set("access_token", "2222222222");
form.prepareSubmit(request);
std::ostream& ostr = session->sendRequest(request);
form.write(ostr);
Poco::Net::HTTPResponse response;
std::istream& rs = session->receiveResponse(response);
LOK_ASSERT_EQUAL(Poco::Net::HTTPResponse::HTTP_OK, response.getStatus());
std::string html;
Poco::StreamCopier::copyToString(rs, html);
LOK_ASSERT(html.find(form["access_token"]) != std::string::npos);
LOK_ASSERT(html.find(_uri.getHost()) != std::string::npos);
}
namespace
{
void assertHTTPFilesExist(const Poco::URI& uri, Poco::RegularExpression& expr, const std::string& html, const std::string& mimetype = std::string())
{
Poco::RegularExpression::MatchVec matches;
bool found = false;
for (int offset = 0; expr.match(html, offset, matches) > 0; offset = static_cast<int>(matches[0].offset + matches[0].length))
{
found = true;
LOK_ASSERT_EQUAL(2, (int)matches.size());
Poco::URI uriScript(html.substr(matches[1].offset, matches[1].length));
if (uriScript.getHost().empty())
{
std::string scriptString(uriScript.toString());
// ignore the branding bits, it's not an error when they aren't present.
if (scriptString.find("/branding.") != std::string::npos)
continue;
std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(uri));
Poco::Net::HTTPRequest requestScript(Poco::Net::HTTPRequest::HTTP_GET, scriptString);
session->sendRequest(requestScript);
Poco::Net::HTTPResponse responseScript;
session->receiveResponse(responseScript);
LOK_ASSERT_EQUAL(Poco::Net::HTTPResponse::HTTP_OK, responseScript.getStatus());
if (!mimetype.empty())
LOK_ASSERT_EQUAL(mimetype, responseScript.getContentType());
}
}
LOK_ASSERT_MESSAGE("No match found", found);
}
}
void HTTPServerTest::testScriptsAndLinksGet()
{
std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, "/loleaflet/dist/loleaflet.html");
session->sendRequest(request);
Poco::Net::HTTPResponse response;
std::istream& rs = session->receiveResponse(response);
LOK_ASSERT_EQUAL(Poco::Net::HTTPResponse::HTTP_OK, response.getStatus());
std::string html;
Poco::StreamCopier::copyToString(rs, html);
Poco::RegularExpression script("<script.*?src=\"(.*?)\"");
assertHTTPFilesExist(_uri, script, html, "application/javascript");
Poco::RegularExpression link("<link.*?href=\"(.*?)\"");
assertHTTPFilesExist(_uri, link, html);
}
void HTTPServerTest::testScriptsAndLinksPost()
{
std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/loleaflet/dist/loleaflet.html");
std::string body;
request.setContentLength((int) body.length());
session->sendRequest(request) << body;
Poco::Net::HTTPResponse response;
std::istream& rs = session->receiveResponse(response);
LOK_ASSERT_EQUAL(Poco::Net::HTTPResponse::HTTP_OK, response.getStatus());
std::string html;
Poco::StreamCopier::copyToString(rs, html);
Poco::RegularExpression script("<script.*?src=\"(.*?)\"");
assertHTTPFilesExist(_uri, script, html, "application/javascript");
Poco::RegularExpression link("<link.*?href=\"(.*?)\"");
assertHTTPFilesExist(_uri, link, html);
}
void HTTPServerTest::testConvertTo()
{
const char *testname = "testConvertTo";
const std::string srcPath = FileUtil::getTempFilePath(TDOC, "hello.odt", "convertTo_");
std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));
session->setTimeout(Poco::Timespan(5, 0)); // 5 seconds.
TST_LOG("Convert-to odt -> txt");
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/lool/convert-to");
Poco::Net::HTMLForm form;
form.setEncoding(Poco::Net::HTMLForm::ENCODING_MULTIPART);
form.set("format", "txt");
form.addPart("data", new Poco::Net::FilePartSource(srcPath));
form.prepareSubmit(request);
try
{
form.write(session->sendRequest(request));
}
catch (const std::exception& ex)
{
// In case the server is still starting up.
sleep(5);
form.write(session->sendRequest(request));
}
Poco::Net::HTTPResponse response;
std::stringstream actualStream;
std::istream& responseStream = session->receiveResponse(response);
Poco::StreamCopier::copyStream(responseStream, actualStream);
std::ifstream fileStream(TDOC "/hello.txt");
std::stringstream expectedStream;
expectedStream << fileStream.rdbuf();
// Remove the temp files.
FileUtil::removeFile(srcPath);
// In some cases the result is prefixed with (the UTF-8 encoding of) the Unicode BOM
// (U+FEFF). Skip that.
std::string actualString = actualStream.str();
if (actualString.size() > 3 && actualString[0] == '\xEF' && actualString[1] == '\xBB' && actualString[2] == '\xBF')
actualString = actualString.substr(3);
LOK_ASSERT_EQUAL(expectedStream.str(), actualString);
}
void HTTPServerTest::testConvertTo2()
{
const char *testname = "testConvertTo2";
const std::string srcPath = FileUtil::getTempFilePath(TDOC, "convert-to.xlsx", "convertTo_");
std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));
session->setTimeout(Poco::Timespan(10, 0)); // 10 seconds.
TST_LOG("Convert-to #2 xlsx -> png");
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/lool/convert-to");
Poco::Net::HTMLForm form;
form.setEncoding(Poco::Net::HTMLForm::ENCODING_MULTIPART);
form.set("format", "png");
form.addPart("data", new Poco::Net::FilePartSource(srcPath));
form.prepareSubmit(request);
try
{
form.write(session->sendRequest(request));
}
catch (const std::exception& ex)
{
// In case the server is still starting up.
sleep(5);
form.write(session->sendRequest(request));
}
Poco::Net::HTTPResponse response;
std::stringstream actualStream;
std::istream& responseStream = session->receiveResponse(response);
Poco::StreamCopier::copyStream(responseStream, actualStream);
// Remove the temp files.
FileUtil::removeFile(srcPath);
std::string actualString = actualStream.str();
LOK_ASSERT(actualString.size() >= 100);
// LOK_ASSERT_EQUAL(actualString[0], 0x89);
LOK_ASSERT_EQUAL(actualString[1], 'P');
LOK_ASSERT_EQUAL(actualString[2], 'N');
LOK_ASSERT_EQUAL(actualString[3], 'G');
}
void HTTPServerTest::testConvertToWithForwardedClientIP()
{
const std::string testname = "convertToWithForwardedClientIP-";
constexpr int TimeoutSeconds = 10; // Sometimes dns resolving is slow.
// Test a forwarded IP which is not allowed to use convert-to feature
try
{
TST_LOG("Converting from a disallowed IP.");
const std::string srcPath = FileUtil::getTempFilePath(TDOC, "hello.odt", testname + "disallowed");
std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));
session->setTimeout(Poco::Timespan(TimeoutSeconds, 0));
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/lool/convert-to");
LOK_ASSERT(!request.has("X-Forwarded-For"));
request.add("X-Forwarded-For", getNotAllowedTestServerURI().getHost() + ", " + _uri.getHost());
Poco::Net::HTMLForm form;
form.setEncoding(Poco::Net::HTMLForm::ENCODING_MULTIPART);
form.set("format", "txt");
form.addPart("data", new Poco::Net::FilePartSource(srcPath));
form.prepareSubmit(request);
try
{
form.write(session->sendRequest(request));
}
catch (const std::exception& ex)
{
// In case the server is still starting up.
sleep(2);
form.write(session->sendRequest(request));
}
Poco::Net::HTTPResponse response;
std::stringstream actualStream;
std::istream& responseStream = session->receiveResponse(response);
Poco::StreamCopier::copyStream(responseStream, actualStream);
// Remove the temp files.
FileUtil::removeFile(srcPath);
std::string actualString = actualStream.str();
LOK_ASSERT(actualString.empty()); // <- we did not get the converted file
}
catch(const Poco::Exception& exc)
{
LOK_ASSERT_FAIL(exc.displayText() + ": " + (exc.nested() ? exc.nested()->displayText() : ""));
}
// Test a forwarded IP which is allowed to use convert-to feature
try
{
TST_LOG("Converting from an allowed IP.");
const std::string srcPath = FileUtil::getTempFilePath(TDOC, "hello.odt", testname + "allowed");
std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));
session->setTimeout(Poco::Timespan(TimeoutSeconds, 0));
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/lool/convert-to");
LOK_ASSERT(!request.has("X-Forwarded-For"));
request.add("X-Forwarded-For", _uri.getHost() + ", " + _uri.getHost());
Poco::Net::HTMLForm form;
form.setEncoding(Poco::Net::HTMLForm::ENCODING_MULTIPART);
form.set("format", "txt");
form.addPart("data", new Poco::Net::FilePartSource(srcPath));
form.prepareSubmit(request);
form.write(session->sendRequest(request));
Poco::Net::HTTPResponse response;
std::stringstream actualStream;
std::istream& responseStream = session->receiveResponse(response);
Poco::StreamCopier::copyStream(responseStream, actualStream);
std::ifstream fileStream(TDOC "/hello.txt");
std::stringstream expectedStream;
expectedStream << fileStream.rdbuf();
// Remove the temp files.
FileUtil::removeFile(srcPath);
// In some cases the result is prefixed with (the UTF-8 encoding of) the Unicode BOM
// (U+FEFF). Skip that.
std::string actualString = actualStream.str();
if (actualString.size() > 3 && actualString[0] == '\xEF' && actualString[1] == '\xBB' && actualString[2] == '\xBF')
actualString = actualString.substr(3);
LOK_ASSERT_EQUAL(expectedStream.str(), actualString); // <- we got the converted file
}
catch(const Poco::Exception& exc)
{
LOK_ASSERT_FAIL(exc.displayText() + ": " + (exc.nested() ? exc.nested()->displayText() : ""));
}
// Test a forwarded header with three IPs, one is not allowed -> request is denied.
try
{
TST_LOG("Converting from multiple IPs, on disallowed.");
const std::string srcPath = FileUtil::getTempFilePath(TDOC, "hello.odt", testname + "disallowed-multi");
std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));
session->setTimeout(Poco::Timespan(TimeoutSeconds, 0));
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/lool/convert-to");
LOK_ASSERT(!request.has("X-Forwarded-For"));
request.add("X-Forwarded-For", _uri.getHost() + ", "
+ getNotAllowedTestServerURI().getHost() + ", "
+ _uri.getHost());
Poco::Net::HTMLForm form;
form.setEncoding(Poco::Net::HTMLForm::ENCODING_MULTIPART);
form.set("format", "txt");
form.addPart("data", new Poco::Net::FilePartSource(srcPath));
form.prepareSubmit(request);
form.write(session->sendRequest(request));
Poco::Net::HTTPResponse response;
std::stringstream actualStream;
std::istream& responseStream = session->receiveResponse(response);
Poco::StreamCopier::copyStream(responseStream, actualStream);
// Remove the temp files.
FileUtil::removeFile(srcPath);
std::string actualString = actualStream.str();
LOK_ASSERT(actualString.empty()); // <- we did not get the converted file
}
catch(const Poco::Exception& exc)
{
LOK_ASSERT_FAIL(exc.displayText() + ": " + (exc.nested() ? exc.nested()->displayText() : ""));
}
}
CPPUNIT_TEST_SUITE_REGISTRATION(HTTPServerTest);
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|