forked from Klomgor/online
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnitCopyPaste.cpp
445 lines (379 loc) · 16.9 KB
/
UnitCopyPaste.cpp
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* Copyright the Collabora Online contributors.
*
* SPDX-License-Identifier: MPL-2.0
*
* 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/.
*/
// Test various copy/paste pieces ...
#include <config.h>
#include <HttpRequest.hpp>
#include <Unit.hpp>
#include <UnitHTTP.hpp>
#include <WebSocketSession.hpp>
#include <common/Clipboard.hpp>
#include <helpers.hpp>
#include <lokassert.hpp>
#include <test.hpp>
#include <wsd/COOLWSD.hpp>
#include <wsd/ClientSession.hpp>
#include <Poco/Net/HTMLForm.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/Net/HTTPServerRequest.h>
#include <Poco/Net/StringPartSource.h>
#include <Poco/Util/LayeredConfiguration.h>
#include <sstream>
using namespace Poco::Net;
// Inside the WSD process
class UnitCopyPaste : public UnitWSD
{
STATE_ENUM(Phase, RunTest, WaitDocClose, PostCloseTest, Done) _phase;
std::string _clipURI;
std::string _clipURI2;
public:
UnitCopyPaste()
: UnitWSD("UnitCopyPaste")
, _phase(Phase::RunTest)
{
}
void configure(Poco::Util::LayeredConfiguration& config) override
{
UnitWSD::configure(config);
// force HTTPS - to test harder
config.setBool("ssl.enable", true);
}
std::string getRawClipboard(const std::string &clipURIstr)
{
auto httpSession = http::Session::create(clipURIstr);
std::shared_ptr<const http::Response> httpResponse =
httpSession->syncRequest(http::Request(Poco::URI(clipURIstr).getPathAndQuery()));
return httpResponse->getBody();
}
std::shared_ptr<ClipboardData> getClipboard(const std::string& clipURIstr,
http::StatusCode expected)
{
LOG_TST("getClipboard: connect to " << clipURIstr);
Poco::URI clipURI(clipURIstr);
auto httpSession = http::Session::create(clipURIstr);
std::shared_ptr<const http::Response> httpResponse =
httpSession->syncRequest(http::Request(Poco::URI(clipURIstr).getPathAndQuery()));
// Note that this is expected for both living and closed documents.
// This failed when either case didn't add the custom header.
LOK_ASSERT_EQUAL(std::string("true"), httpResponse->get("X-COOL-Clipboard"));
LOG_TST("getClipboard: sent request: " << clipURI.getPathAndQuery());
try {
LOG_TST("getClipboard: HTTP get request returned: "
<< httpResponse->statusLine().statusCode());
if (httpResponse->statusLine().statusCode() != expected)
{
LOK_ASSERT_EQUAL_MESSAGE("clipboard status mismatches", expected,
httpResponse->statusLine().statusCode());
exitTest(TestResult::Failed);
return std::shared_ptr<ClipboardData>();
}
LOK_ASSERT_EQUAL_MESSAGE("getClipboard: clipboard content-type mismatches expected",
std::string("application/octet-stream"),
httpResponse->header().getContentType());
std::string body = httpResponse->getBody();
removeSessionClipboardMeta(body);
std::istringstream responseStream(body);
auto clipboard = std::make_shared<ClipboardData>();
clipboard->read(responseStream);
std::ostringstream oss;
clipboard->dumpState(oss);
LOG_TST("getClipboard: got response. State:\n" << oss.str());
return clipboard;
} catch (Poco::Exception &e) {
LOG_TST("Poco exception: " << e.message());
exitTest(TestResult::Failed);
return std::shared_ptr<ClipboardData>();
}
}
bool assertClipboard(const std::shared_ptr<ClipboardData> &clipboard,
const std::string &mimeType, const std::string &content)
{
std::string value;
// allow empty clipboards
if (clipboard && mimeType.empty() && content.empty())
return true;
if (!clipboard || !clipboard->findType(mimeType, value))
{
LOG_TST("Error: missing clipboard or missing clipboard mime type '" << mimeType
<< '\'');
LOK_ASSERT_FAIL("Missing clipboard mime type");
exitTest(TestResult::Failed);
return false;
}
else if (value != content)
{
LOG_TST("Error: clipboard content mismatch "
<< value.length() << " bytes vs. " << content.length() << " bytes. Clipboard:\n"
<< Util::dumpHex(value) << "Expected:\n"
<< Util::dumpHex(content));
LOK_ASSERT_EQUAL_MESSAGE("Clipboard content mismatch", content, value);
exitTest(TestResult::Failed);
return false;
}
return true;
}
bool fetchClipboardAssert(const std::string& clipURI, const std::string& mimeType,
const std::string& content,
http::StatusCode expected = http::StatusCode::OK)
{
try
{
std::shared_ptr<ClipboardData> clipboard = getClipboard(clipURI, expected);
return assertClipboard(clipboard, mimeType, content);
}
catch (const ParseError& err)
{
LOG_TST("Error fetching clipboard: parse error: " << err.toString());
exitTest(TestResult::Failed);
return false;
}
catch (const std::exception& ex)
{
LOG_TST("Error fetching clipboard: " << ex.what());
exitTest(TestResult::Failed);
return false;
}
catch (...)
{
LOG_TST("Error fetching clipboard: unknown exception during read / parse");
exitTest(TestResult::Failed);
return false;
}
return true;
}
bool setClipboard(const std::string &clipURIstr, const std::string &rawData,
HTTPResponse::HTTPStatus expected)
{
LOG_TST("connect to " << clipURIstr);
Poco::URI clipURI(clipURIstr);
std::unique_ptr<HTTPClientSession> session(helpers::createSession(clipURI));
HTTPRequest request(HTTPRequest::HTTP_POST, clipURI.getPathAndQuery());
HTMLForm form;
form.setEncoding(HTMLForm::ENCODING_MULTIPART);
form.set("format", "txt");
form.addPart("data", new StringPartSource(rawData, "application/octet-stream", "clipboard"));
form.prepareSubmit(request);
form.write(session->sendRequest(request));
HTTPResponse response;
std::stringstream actualStream;
try {
session->receiveResponse(response);
} catch (NoMessageException &) {
LOG_TST("Error: No response from setting clipboard.");
exitTest(TestResult::Failed);
return false;
}
if (response.getStatus() != expected)
{
LOG_TST("Error: response for clipboard " << response.getStatus() << " != expected "
<< expected);
exitTest(TestResult::Failed);
return false;
}
return true;
}
std::shared_ptr<ClientSession> getChildSession(size_t session)
{
std::shared_ptr<DocumentBroker> broker;
std::shared_ptr<ClientSession> clientSession;
std::vector<std::shared_ptr<DocumentBroker>> brokers = COOLWSD::getBrokersTestOnly();
assert(brokers.size() > 0);
broker = brokers[0];
auto sessions = broker->getSessionsTestOnlyUnsafe();
assert(sessions.size() > 0 && session < sessions.size());
return sessions[session];
}
std::string getSessionClipboardURI(size_t session)
{
std::shared_ptr<ClientSession> clientSession = getChildSession(session);
std::string tag = clientSession->getClipboardURI(false); // nominally thread unsafe
LOG_TST("Got tag '" << tag << "' for session " << session);
return tag;
}
void removeSessionClipboardMeta(std::string& payload)
{
if (COOLWSD::getBrokersTestOnly().empty())
{
return;
}
// The removal doesn't depend on the clipboard URL, so just ask the first session to do the
// removal for us.
std::shared_ptr<ClientSession> clientSession = getChildSession(0);
clientSession->preProcessSetClipboardPayload(payload);
}
std::string buildClipboardText(const std::string &text)
{
std::stringstream clipData;
clipData << "text/plain;charset=utf-8\n"
<< std::hex << text.length() << '\n'
<< text << '\n';
return clipData.str();
}
void onDocBrokerDestroy(const std::string& docKey) override
{
LOG_TST("Destroyed dockey [" << docKey << ']');
LOK_ASSERT_STATE(_phase, Phase::WaitDocClose);
TRANSITION_STATE(_phase, Phase::PostCloseTest);
}
void runTest()
{
// NOTE: This code has multiple race-conditions!
// The main one is that the fetching of clipboard
// data (via fetchClipboardAssert) is done via
// independent HTTP GET requests that have nothing
// whatever with the long-lived WebSocket that
// processes all the client commands below.
// This suffers a very interesting race, which is
// that the getClipboard command may end up
// anywhere within the Kit queue, even before the
// other commands preceeding it in this test!
// As an example: in the 'Inject content' case we
// send .uno:Deselect, paste, .uno:SelectAll,
// .uno:Copy (all through the WebSocket), and an
// HTTP GET for the getClipboard. But, in DocBroker,
// the WebSocket commands might be interleaved with
// the getClipboard, such that the Kit might receive
// .uno:Deselect, paste, .uno:SelectAll, getClipboard,
// .uno:Copy! This has been observed in practice.
//
// There are other race-conditions, too.
// For example, even if Kit gets these commands in
// the correct order (i.e. getClipboard last, after
// .uno:Copy), getClipboard is executed immediately
// while all the .uno commands are queued for async
// execution. This means that when getClipboard is
// executed in Core, the .uno:Copy command might not
// have yet been executed and, therefore, will not
// contain the expected contents, failing the test.
//
// To combat these races, we drain the events coming
// from Core before we read the clipboard through
// getClipboard. This way we are reasonably in
// control of the state. Notice that the client
// code will suffer these races, if the getClipboard
// HTTP GET is done "too soon" after a copy, but
// that is unlikely in practice because the URL for
// getClipboard is generated as a link to the user,
// which is clicked to download the clipboard contents.
// Load a doc with the cursor saved at a top row.
// NOTE: This load a spreadsheet, not a writer doc!
std::string documentPath, documentURL;
helpers::getDocumentPathAndURL("empty.ods", documentPath, documentURL, testname);
std::shared_ptr<http::WebSocketSession> socket = helpers::loadDocAndGetSession(
socketPoll(), Poco::URI(helpers::getTestServerURI()), documentURL, testname);
_clipURI = getSessionClipboardURI(0);
LOG_TST("Fetch empty clipboard content after loading");
if (!fetchClipboardAssert(_clipURI, "", ""))
return;
// Check existing content
LOG_TST("Fetch pristine content from the document");
helpers::sendTextFrame(socket, "uno .uno:SelectAll", testname);
helpers::sendAndDrain(socket, testname, "uno .uno:Copy", "statechanged:");
const std::string oneColumn = "2\n3\n5\n";
if (!fetchClipboardAssert(_clipURI, "text/plain;charset=utf-8", oneColumn))
return;
LOG_TST("Open second connection");
std::shared_ptr<http::WebSocketSession> socket2 = helpers::loadDocAndGetSession(
socketPoll(), Poco::URI(helpers::getTestServerURI()), documentURL, testname);
_clipURI2 = getSessionClipboardURI(1);
LOG_TST("Check no clipboard content on second view");
if (!fetchClipboardAssert(_clipURI2, "", ""))
return;
LOG_TST("Inject content through first view");
helpers::sendTextFrame(socket, "uno .uno:Deselect", testname);
const std::string text = "This is some content?&*/\\!!";
helpers::sendTextFrame(socket, "paste mimetype=text/plain;charset=utf-8\n" + text, testname);
helpers::sendTextFrame(socket, "uno .uno:SelectAll", testname);
helpers::sendAndDrain(socket, testname, "uno .uno:Copy", "statechanged:");
const std::string existing = "2\t\n3\t\n5\t";
if (!fetchClipboardAssert(_clipURI, "text/plain;charset=utf-8", existing + text + '\n'))
return;
LOG_TST("re-check no clipboard content");
if (!fetchClipboardAssert(_clipURI2, "", ""))
return;
LOG_TST("Push new clipboard content");
const std::string newcontent = "1234567890";
helpers::sendAndWait(socket, testname, "uno .uno:Deselect", "statechanged:");
if (!setClipboard(_clipURI, buildClipboardText(newcontent), HTTPResponse::HTTP_OK))
return;
helpers::sendAndWait(socket, testname, "uno .uno:Paste", "statechanged:");
if (!fetchClipboardAssert(_clipURI, "text/plain;charset=utf-8", newcontent))
return;
LOG_TST("Check the result.");
helpers::sendTextFrame(socket, "uno .uno:SelectAll", testname);
helpers::sendAndDrain(socket, testname, "uno .uno:Copy", "statechanged:");
if (!fetchClipboardAssert(_clipURI, "text/plain;charset=utf-8",
existing + newcontent + '\n'))
return;
// Test setting HTML clipboard:
std::string html("<!DOCTYPE html><html><body>myword</body></html>");
// Intentionally no buildClipboardText() here, just raw HTML.
if (!setClipboard(_clipURI, html, HTTPResponse::HTTP_OK))
return;
// This failed with: ERROR: Forced failure: Missing clipboard mime type, because we tried to
// parse HTML when we expected a list of mimetype-size-bytes entries.
if (!fetchClipboardAssert(_clipURI, "text/html", html))
return;
// Setup state that will be also asserte in postCloseTest():
LOG_TST("Setup clipboards:");
if (!setClipboard(_clipURI2, buildClipboardText("kippers"), HTTPResponse::HTTP_OK))
return;
if (!setClipboard(_clipURI, buildClipboardText("herring"), HTTPResponse::HTTP_OK))
return;
LOG_TST("Fetch clipboards:");
if (!fetchClipboardAssert(_clipURI2, "text/plain;charset=utf-8", "kippers"))
return;
if (!fetchClipboardAssert(_clipURI, "text/plain;charset=utf-8", "herring"))
return;
TRANSITION_STATE(_phase, Phase::WaitDocClose);
LOG_TST("Close sockets:");
socket2->asyncShutdown();
socket->asyncShutdown();
LOK_ASSERT_MESSAGE("Expected successful disconnection of the WebSocket 2",
socket2->waitForDisconnection(std::chrono::seconds(5)));
LOK_ASSERT_MESSAGE("Expected successful disconnection of the WebSocket 0",
socket->waitForDisconnection(std::chrono::seconds(5)));
}
void postCloseTest()
{
sleep(1); // paranoia.
LOG_TST("Fetch clipboards after shutdown:");
LOK_ASSERT_MESSAGE("Failed to get Session #2 clipboard content 'kippers'",
fetchClipboardAssert(_clipURI2, "text/plain;charset=utf-8", "kippers"));
LOK_ASSERT_MESSAGE("Failed to get Session #1 clipboard content 'herring'",
fetchClipboardAssert(_clipURI, "text/plain;charset=utf-8", "herring"));
TRANSITION_STATE(_phase, Phase::Done);
LOG_TST("Clipboard tests succeeded");
passTest("Got clipboard contents after shutdown");
}
void invokeWSDTest() override
{
switch (_phase)
{
case Phase::RunTest:
{
runTest();
break;
}
case Phase::WaitDocClose:
break;
case Phase::PostCloseTest:
postCloseTest();
break;
case Phase::Done:
break;
}
}
};
UnitBase *unit_create_wsd(void)
{
return new UnitCopyPaste();
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */