2019-06-20 14:20:26 -05:00
|
|
|
/* -*- 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/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Test various copy/paste pieces ...
|
|
|
|
|
2021-02-25 07:49:54 -06:00
|
|
|
#include "lokassert.hpp"
|
2019-06-20 14:20:26 -05:00
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <Unit.hpp>
|
|
|
|
#include <UnitHTTP.hpp>
|
|
|
|
#include <helpers.hpp>
|
|
|
|
#include <wsd/LOOLWSD.hpp>
|
2019-06-22 11:45:36 -05:00
|
|
|
#include <common/Clipboard.hpp>
|
2019-06-21 06:35:17 -05:00
|
|
|
#include <wsd/ClientSession.hpp>
|
2019-06-21 11:48:57 -05:00
|
|
|
#include <Poco/Net/HTTPResponse.h>
|
2019-06-21 06:35:17 -05:00
|
|
|
#include <Poco/Net/HTTPServerRequest.h>
|
|
|
|
#include <Poco/Net/HTMLForm.h>
|
|
|
|
#include <Poco/Net/StringPartSource.h>
|
|
|
|
#include <Poco/Util/LayeredConfiguration.h>
|
2019-06-20 14:20:26 -05:00
|
|
|
|
|
|
|
#include <test.hpp>
|
|
|
|
|
2019-06-21 11:48:57 -05:00
|
|
|
using namespace Poco::Net;
|
|
|
|
|
2019-06-20 14:20:26 -05:00
|
|
|
// Inside the WSD process
|
|
|
|
class UnitCopyPaste : public UnitWSD
|
|
|
|
{
|
|
|
|
public:
|
2019-06-21 06:35:17 -05:00
|
|
|
UnitCopyPaste()
|
|
|
|
{
|
|
|
|
}
|
2019-06-20 14:20:26 -05:00
|
|
|
|
|
|
|
void configure(Poco::Util::LayeredConfiguration& config) override
|
|
|
|
{
|
|
|
|
UnitWSD::configure(config);
|
|
|
|
// force HTTPS - to test harder
|
|
|
|
config.setBool("ssl.enable", true);
|
|
|
|
}
|
|
|
|
|
2019-06-22 06:46:50 -05:00
|
|
|
std::string getRawClipboard(const std::string &clipURIstr)
|
|
|
|
{
|
|
|
|
Poco::URI clipURI(clipURIstr);
|
|
|
|
|
|
|
|
HTTPResponse response;
|
|
|
|
HTTPRequest request(HTTPRequest::HTTP_GET, clipURI.getPathAndQuery());
|
|
|
|
std::unique_ptr<HTTPClientSession> session(helpers::createSession(clipURI));
|
|
|
|
session->setTimeout(Poco::Timespan(10, 0)); // 10 seconds.
|
|
|
|
session->sendRequest(request);
|
|
|
|
std::istream& responseStream = session->receiveResponse(response);
|
|
|
|
return std::string(std::istreambuf_iterator<char>(responseStream), {});
|
|
|
|
}
|
|
|
|
|
2019-06-24 14:10:44 -05:00
|
|
|
std::shared_ptr<ClipboardData> getClipboard(const std::string &clipURIstr,
|
|
|
|
HTTPResponse::HTTPStatus expected)
|
2019-06-22 05:26:42 -05:00
|
|
|
{
|
2021-02-25 06:42:39 -06:00
|
|
|
LOG_TST("connect to " << clipURIstr);
|
2019-06-22 05:26:42 -05:00
|
|
|
Poco::URI clipURI(clipURIstr);
|
|
|
|
|
|
|
|
HTTPResponse response;
|
|
|
|
HTTPRequest request(HTTPRequest::HTTP_GET, clipURI.getPathAndQuery());
|
|
|
|
std::unique_ptr<HTTPClientSession> session(helpers::createSession(clipURI));
|
|
|
|
session->setTimeout(Poco::Timespan(10, 0)); // 10 seconds.
|
|
|
|
session->sendRequest(request);
|
2021-02-25 06:42:39 -06:00
|
|
|
LOG_TST("sent request.");
|
2019-06-22 05:26:42 -05:00
|
|
|
|
2019-06-24 14:10:44 -05:00
|
|
|
try {
|
|
|
|
std::istream& responseStream = session->receiveResponse(response);
|
2021-02-25 07:49:54 -06:00
|
|
|
LOG_TST("HTTP get request returned reason: " << response.getReason());
|
2019-06-24 14:10:44 -05:00
|
|
|
|
|
|
|
if (response.getStatus() != expected)
|
|
|
|
{
|
2021-02-25 07:49:54 -06:00
|
|
|
LOK_ASSERT_EQUAL_MESSAGE("clipboard status mismatches expected", expected,
|
|
|
|
response.getStatus());
|
2019-06-24 14:10:44 -05:00
|
|
|
exitTest(TestResult::Failed);
|
|
|
|
return std::shared_ptr<ClipboardData>();
|
|
|
|
}
|
|
|
|
|
2021-02-25 07:49:54 -06:00
|
|
|
LOK_ASSERT_EQUAL_MESSAGE("clipboard content-type mismatches expected",
|
|
|
|
std::string("application/octet-stream"),
|
|
|
|
response.getContentType());
|
2019-06-24 14:10:44 -05:00
|
|
|
|
|
|
|
auto clipboard = std::make_shared<ClipboardData>();
|
|
|
|
clipboard->read(responseStream);
|
|
|
|
clipboard->dumpState(std::cerr);
|
|
|
|
|
2021-02-25 06:42:39 -06:00
|
|
|
LOG_TST("got response");
|
2019-06-24 14:10:44 -05:00
|
|
|
return clipboard;
|
|
|
|
} catch (Poco::Exception &e) {
|
2021-02-25 06:42:39 -06:00
|
|
|
LOG_TST("Poco exception: " << e.message());
|
2019-06-22 05:26:42 -05:00
|
|
|
exitTest(TestResult::Failed);
|
|
|
|
return std::shared_ptr<ClipboardData>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool assertClipboard(const std::shared_ptr<ClipboardData> &clipboard,
|
|
|
|
const std::string &mimeType, const std::string &content)
|
|
|
|
{
|
|
|
|
bool failed = false;
|
2019-06-22 06:46:50 -05:00
|
|
|
|
2019-06-22 05:26:42 -05:00
|
|
|
std::string value;
|
2019-06-24 14:10:44 -05:00
|
|
|
|
|
|
|
// allow empty clipboards
|
2020-06-02 22:17:04 -05:00
|
|
|
if (clipboard && mimeType.empty() && content.empty())
|
2019-06-24 14:10:44 -05:00
|
|
|
return true;
|
|
|
|
|
2019-06-22 05:26:42 -05:00
|
|
|
if (!clipboard || !clipboard->findType(mimeType, value))
|
|
|
|
{
|
2021-02-25 06:42:39 -06:00
|
|
|
LOG_TST("Error: missing clipboard or missing clipboard mime type '" << mimeType
|
|
|
|
<< '\'');
|
2021-02-25 07:49:54 -06:00
|
|
|
LOK_ASSERT_FAIL("Missing clipboard mime type");
|
2019-06-22 05:26:42 -05:00
|
|
|
failed = true;
|
|
|
|
}
|
|
|
|
else if (value != content)
|
|
|
|
{
|
2021-02-25 06:42:39 -06:00
|
|
|
LOG_TST("Error: clipboard content mismatch " << value.length() << " vs. "
|
|
|
|
<< content.length());
|
2019-06-22 06:46:50 -05:00
|
|
|
sleep (1); // output settle.
|
|
|
|
Util::dumpHex(std::cerr, "\tclipboard:\n", "", value);
|
|
|
|
Util::dumpHex(std::cerr, "\tshould be:\n", "", content);
|
2021-02-25 07:49:54 -06:00
|
|
|
LOK_ASSERT_EQUAL_MESSAGE("Clipboard content mismatch", value.size(), content.size());
|
2019-06-22 05:26:42 -05:00
|
|
|
failed = true;
|
|
|
|
}
|
|
|
|
if (failed)
|
|
|
|
{
|
|
|
|
exitTest(TestResult::Failed);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-22 07:00:48 -05:00
|
|
|
bool fetchClipboardAssert(const std::string &clipURI,
|
|
|
|
const std::string &mimeType,
|
2019-06-24 14:10:44 -05:00
|
|
|
const std::string &content,
|
|
|
|
HTTPResponse::HTTPStatus expected = HTTPResponse::HTTP_OK)
|
2019-06-22 07:00:48 -05:00
|
|
|
{
|
|
|
|
std::shared_ptr<ClipboardData> clipboard;
|
|
|
|
try {
|
2019-06-24 14:10:44 -05:00
|
|
|
clipboard = getClipboard(clipURI, expected);
|
2019-06-22 07:00:48 -05:00
|
|
|
} catch (ParseError &err) {
|
2021-02-25 06:42:39 -06:00
|
|
|
LOG_TST("Error: parse error " << err.toString());
|
2019-06-24 14:10:44 -05:00
|
|
|
exitTest(TestResult::Failed);
|
|
|
|
return false;
|
|
|
|
} catch (...) {
|
2021-02-25 06:42:39 -06:00
|
|
|
LOG_TST("Error: unknown exception during read / parse");
|
2019-06-22 07:00:48 -05:00
|
|
|
exitTest(TestResult::Failed);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!assertClipboard(clipboard, mimeType, content))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-22 07:29:54 -05:00
|
|
|
bool setClipboard(const std::string &clipURIstr, const std::string &rawData,
|
|
|
|
HTTPResponse::HTTPStatus expected)
|
|
|
|
{
|
2021-02-25 06:42:39 -06:00
|
|
|
LOG_TST("connect to " << clipURIstr);
|
2019-06-22 07:29:54 -05:00
|
|
|
Poco::URI clipURI(clipURIstr);
|
|
|
|
|
|
|
|
std::unique_ptr<HTTPClientSession> session(helpers::createSession(clipURI));
|
|
|
|
Poco::URI clipURIPoco(clipURI);
|
|
|
|
HTTPRequest request(HTTPRequest::HTTP_POST, clipURIPoco.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 &) {
|
2021-02-25 06:42:39 -06:00
|
|
|
LOG_TST("Error: No response from setting clipboard.");
|
2019-06-22 07:29:54 -05:00
|
|
|
exitTest(TestResult::Failed);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.getStatus() != expected)
|
|
|
|
{
|
2021-02-25 06:42:39 -06:00
|
|
|
LOG_TST("Error: response for clipboard " << response.getStatus() << " != expected "
|
|
|
|
<< expected);
|
2019-06-22 07:29:54 -05:00
|
|
|
exitTest(TestResult::Failed);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-24 14:10:44 -05:00
|
|
|
std::string getSessionClipboardURI(size_t session)
|
|
|
|
{
|
|
|
|
std::shared_ptr<DocumentBroker> broker;
|
|
|
|
std::shared_ptr<ClientSession> clientSession;
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<DocumentBroker>> brokers = LOOLWSD::getBrokersTestOnly();
|
|
|
|
assert(brokers.size() > 0);
|
|
|
|
broker = brokers[0];
|
|
|
|
auto sessions = broker->getSessionsTestOnlyUnsafe();
|
|
|
|
assert(sessions.size() > 0 && session < sessions.size());
|
|
|
|
clientSession = sessions[session];
|
|
|
|
|
2019-07-04 04:50:33 -05:00
|
|
|
std::string tag = clientSession->getClipboardURI(false); // nominally thread unsafe
|
2021-02-25 06:42:39 -06:00
|
|
|
LOG_TST("Got tag '" << tag << "' for session " << session);
|
2019-07-04 04:50:33 -05:00
|
|
|
return tag;
|
2019-06-24 14:10:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string buildClipboardText(const std::string &text)
|
|
|
|
{
|
|
|
|
std::stringstream clipData;
|
|
|
|
clipData << "text/plain;charset=utf-8\n"
|
2020-05-24 08:10:18 -05:00
|
|
|
<< std::hex << text.length() << '\n'
|
|
|
|
<< text << '\n';
|
2019-06-24 14:10:44 -05:00
|
|
|
return clipData.str();
|
|
|
|
}
|
|
|
|
|
2020-12-24 11:15:37 -06:00
|
|
|
void invokeWSDTest() override
|
2019-06-20 14:20:26 -05:00
|
|
|
{
|
|
|
|
std::string testname = "copypaste";
|
|
|
|
|
2019-06-24 14:10:44 -05:00
|
|
|
try {
|
|
|
|
|
2019-06-20 14:20:26 -05:00
|
|
|
// Load a doc with the cursor saved at a top row.
|
|
|
|
std::string documentPath, documentURL;
|
|
|
|
helpers::getDocumentPathAndURL("empty.ods", documentPath, documentURL, testname);
|
|
|
|
std::shared_ptr<LOOLWebSocket> socket =
|
|
|
|
helpers::loadDocAndGetSocket(Poco::URI(helpers::getTestServerURI()), documentURL, testname);
|
2019-06-21 06:35:17 -05:00
|
|
|
|
2019-06-24 14:10:44 -05:00
|
|
|
std::string clipURI = getSessionClipboardURI(0);
|
2019-06-22 07:00:48 -05:00
|
|
|
|
2021-02-25 06:42:39 -06:00
|
|
|
LOG_TST("Fetch empty clipboard content");
|
2019-06-24 14:10:44 -05:00
|
|
|
if (!fetchClipboardAssert(clipURI, "", ""))
|
2019-06-21 06:35:17 -05:00
|
|
|
return;
|
2019-06-20 14:20:26 -05:00
|
|
|
|
2019-06-22 07:00:48 -05:00
|
|
|
// Check existing content
|
2021-02-25 06:42:39 -06:00
|
|
|
LOG_TST("Fetch pristine content");
|
2019-06-22 07:00:48 -05:00
|
|
|
helpers::sendTextFrame(socket, "uno .uno:SelectAll", testname);
|
|
|
|
helpers::sendTextFrame(socket, "uno .uno:Copy", testname);
|
|
|
|
std::string oneColumn = "2\n3\n5\n";
|
|
|
|
if (!fetchClipboardAssert(clipURI, "text/plain;charset=utf-8", oneColumn))
|
2019-06-22 06:46:50 -05:00
|
|
|
return;
|
|
|
|
|
2021-02-25 06:42:39 -06:00
|
|
|
LOG_TST("Open second connection");
|
2019-06-24 14:10:44 -05:00
|
|
|
std::shared_ptr<LOOLWebSocket> socket2 =
|
|
|
|
helpers::loadDocAndGetSocket(Poco::URI(helpers::getTestServerURI()), documentURL, testname);
|
|
|
|
std::string clipURI2 = getSessionClipboardURI(1);
|
|
|
|
|
2021-02-25 06:42:39 -06:00
|
|
|
LOG_TST("Check no clipboard content");
|
2019-06-24 14:10:44 -05:00
|
|
|
if (!fetchClipboardAssert(clipURI2, "", ""))
|
|
|
|
return;
|
|
|
|
|
2021-02-25 06:42:39 -06:00
|
|
|
LOG_TST("Inject content");
|
2019-06-24 10:38:27 -05:00
|
|
|
helpers::sendTextFrame(socket, "uno .uno:Deselect", testname);
|
2019-06-22 06:46:50 -05:00
|
|
|
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::sendTextFrame(socket, "uno .uno:Copy", testname);
|
|
|
|
|
2019-06-24 10:38:27 -05:00
|
|
|
std::string existing = "2\t\n3\t\n5\t";
|
2020-05-24 08:10:18 -05:00
|
|
|
if (!fetchClipboardAssert(clipURI, "text/plain;charset=utf-8", existing + text + '\n'))
|
2019-06-22 06:46:50 -05:00
|
|
|
return;
|
|
|
|
|
2021-02-25 06:42:39 -06:00
|
|
|
LOG_TST("re-check no clipboard content");
|
2019-06-24 14:10:44 -05:00
|
|
|
if (!fetchClipboardAssert(clipURI2, "", ""))
|
|
|
|
return;
|
2019-06-22 11:45:36 -05:00
|
|
|
|
2021-02-25 06:42:39 -06:00
|
|
|
LOG_TST("Push new clipboard content");
|
2019-06-24 14:10:44 -05:00
|
|
|
std::string newcontent = "1234567890";
|
2019-06-24 10:38:27 -05:00
|
|
|
helpers::sendTextFrame(socket, "uno .uno:Deselect", testname);
|
2019-06-24 14:10:44 -05:00
|
|
|
if (!setClipboard(clipURI, buildClipboardText(newcontent), HTTPResponse::HTTP_OK))
|
2019-06-22 11:45:36 -05:00
|
|
|
return;
|
|
|
|
helpers::sendTextFrame(socket, "uno .uno:Paste", testname);
|
2019-06-22 07:00:48 -05:00
|
|
|
|
2019-06-24 10:38:27 -05:00
|
|
|
if (!fetchClipboardAssert(clipURI, "text/plain;charset=utf-8", newcontent))
|
|
|
|
return;
|
|
|
|
|
2021-02-25 06:42:39 -06:00
|
|
|
LOG_TST("Check the result.");
|
2019-06-24 10:38:27 -05:00
|
|
|
helpers::sendTextFrame(socket, "uno .uno:SelectAll", testname);
|
|
|
|
helpers::sendTextFrame(socket, "uno .uno:Copy", testname);
|
2020-05-24 08:10:18 -05:00
|
|
|
if (!fetchClipboardAssert(clipURI, "text/plain;charset=utf-8", existing + newcontent + '\n'))
|
2019-06-22 11:45:36 -05:00
|
|
|
return;
|
2019-06-21 17:56:45 -05:00
|
|
|
|
2021-02-25 06:42:39 -06:00
|
|
|
LOG_TST("Setup clipboards:");
|
2019-06-24 14:10:44 -05:00
|
|
|
if (!setClipboard(clipURI2, buildClipboardText("kippers"), HTTPResponse::HTTP_OK))
|
|
|
|
return;
|
|
|
|
if (!setClipboard(clipURI, buildClipboardText("herring"), HTTPResponse::HTTP_OK))
|
|
|
|
return;
|
2021-02-25 06:42:39 -06:00
|
|
|
LOG_TST("Fetch clipboards:");
|
2019-06-24 14:10:44 -05:00
|
|
|
if (!fetchClipboardAssert(clipURI2, "text/plain;charset=utf-8", "kippers"))
|
|
|
|
return;
|
|
|
|
if (!fetchClipboardAssert(clipURI, "text/plain;charset=utf-8", "herring"))
|
|
|
|
return;
|
|
|
|
|
2021-02-25 06:42:39 -06:00
|
|
|
LOG_TST("Close sockets:");
|
2019-07-04 04:50:33 -05:00
|
|
|
socket->shutdown();
|
|
|
|
socket2->shutdown();
|
|
|
|
|
|
|
|
sleep(1); // paranoia.
|
|
|
|
|
2021-02-25 06:42:39 -06:00
|
|
|
LOG_TST("Fetch clipboards after shutdown:");
|
2019-07-04 04:50:33 -05:00
|
|
|
if (!fetchClipboardAssert(clipURI2, "text/plain;charset=utf-8", "kippers"))
|
|
|
|
return;
|
|
|
|
if (!fetchClipboardAssert(clipURI, "text/plain;charset=utf-8", "herring"))
|
|
|
|
return;
|
|
|
|
|
2021-02-25 06:42:39 -06:00
|
|
|
LOG_TST("Clipboard tests succeeded");
|
2019-06-21 11:48:57 -05:00
|
|
|
exitTest(TestResult::Ok);
|
2019-06-24 14:10:44 -05:00
|
|
|
|
|
|
|
} catch (...) {
|
2021-02-25 06:42:39 -06:00
|
|
|
LOG_TST("Error: exception failure during tests");
|
2019-06-24 14:10:44 -05:00
|
|
|
exitTest(TestResult::Failed);
|
|
|
|
}
|
2019-06-20 14:20:26 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
UnitBase *unit_create_wsd(void)
|
|
|
|
{
|
|
|
|
return new UnitCopyPaste();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|