2016-04-28 20:41:00 -05:00
|
|
|
/* -*- 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/.
|
|
|
|
*/
|
|
|
|
|
2017-02-07 10:16:59 -06:00
|
|
|
#ifndef INCLUDED_TEST_HELPERS_HPP
|
|
|
|
#define INCLUDED_TEST_HELPERS_HPP
|
|
|
|
|
2017-04-16 22:31:03 -05:00
|
|
|
#include <iterator>
|
2016-04-28 20:41:00 -05:00
|
|
|
|
2016-09-28 15:04:51 -05:00
|
|
|
#include <Poco/BinaryReader.h>
|
2017-05-01 23:01:28 -05:00
|
|
|
#include <Poco/Dynamic/Var.h>
|
|
|
|
#include <Poco/JSON/JSON.h>
|
|
|
|
#include <Poco/JSON/Parser.h>
|
2016-04-28 20:41:00 -05:00
|
|
|
#include <Poco/Net/HTTPClientSession.h>
|
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
#include <Poco/Net/HTTPResponse.h>
|
|
|
|
#include <Poco/Net/HTTPSClientSession.h>
|
|
|
|
#include <Poco/Net/NetException.h>
|
|
|
|
#include <Poco/Net/Socket.h>
|
|
|
|
#include <Poco/Path.h>
|
|
|
|
#include <Poco/StringTokenizer.h>
|
|
|
|
#include <Poco/URI.h>
|
2017-04-16 22:31:03 -05:00
|
|
|
|
2016-05-01 10:20:41 -05:00
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
2016-04-28 20:41:00 -05:00
|
|
|
|
|
|
|
#include <Common.hpp>
|
2016-11-12 15:38:13 -06:00
|
|
|
#include "common/FileUtil.hpp"
|
2016-11-10 02:47:25 -06:00
|
|
|
#include <LOOLWebSocket.hpp>
|
2016-04-28 20:41:00 -05:00
|
|
|
#include <Util.hpp>
|
|
|
|
|
2016-07-31 10:17:06 -05:00
|
|
|
#ifndef TDOC
|
2016-09-15 13:02:32 -05:00
|
|
|
#error TDOC must be defined (see Makefile.am)
|
2016-07-31 10:17:06 -05:00
|
|
|
#endif
|
|
|
|
|
2018-01-22 08:11:43 -06:00
|
|
|
// Logging in unit-tests go to cerr, for now at least.
|
|
|
|
#define TST_LOG_NAME_BEGIN(NAME, X) do { std::cerr << NAME << "(@" << helpers::timeSinceTestStartMs() << "ms) " << X; } while (false)
|
|
|
|
#define TST_LOG_BEGIN(X) TST_LOG_NAME_BEGIN(testname, X)
|
|
|
|
#define TST_LOG_APPEND(X) do { std::cerr << X; } while (false)
|
|
|
|
#define TST_LOG_END do { std::cerr << "| " << __FILE__ << ':' << __LINE__ << std::endl; } while (false)
|
|
|
|
#define TST_LOG_NAME(NAME, X) TST_LOG_NAME_BEGIN(NAME, X); TST_LOG_END
|
|
|
|
#define TST_LOG(X) TST_LOG_NAME(testname, X)
|
|
|
|
|
2016-04-28 20:41:00 -05:00
|
|
|
/// Common helper testing functions.
|
|
|
|
/// Avoid the temptation to reuse from LOOL code!
|
|
|
|
/// These are supposed to be testing the latter.
|
|
|
|
namespace helpers
|
|
|
|
{
|
2018-01-22 08:11:43 -06:00
|
|
|
inline
|
|
|
|
std::chrono::time_point<std::chrono::steady_clock>& getTestStartTime()
|
|
|
|
{
|
|
|
|
static auto TestStartTime = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
return TestStartTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
void resetTestStartTime()
|
|
|
|
{
|
|
|
|
getTestStartTime() = std::chrono::steady_clock::now();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
size_t timeSinceTestStartMs()
|
|
|
|
{
|
|
|
|
return std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
|
|
std::chrono::steady_clock::now() - getTestStartTime()).count();
|
|
|
|
}
|
|
|
|
|
2016-11-05 16:25:39 -05:00
|
|
|
inline
|
|
|
|
std::vector<char> genRandomData(const size_t size)
|
|
|
|
{
|
|
|
|
std::vector<char> v(size);
|
|
|
|
v.resize(size);
|
2018-02-07 03:17:42 -06:00
|
|
|
char* data = v.data();
|
2016-11-05 16:25:39 -05:00
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
data[i] = static_cast<char>(Util::rng::getNext());
|
|
|
|
}
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2016-11-05 16:26:39 -05:00
|
|
|
inline
|
|
|
|
std::string genRandomString(const size_t size)
|
|
|
|
{
|
|
|
|
std::string text;
|
|
|
|
text.reserve(size);
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
text += static_cast<char>('!' + Util::rng::getNext() % 95);
|
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2016-06-13 19:31:14 -05:00
|
|
|
inline
|
|
|
|
std::vector<char> readDataFromFile(const std::string& filename)
|
|
|
|
{
|
2016-07-06 22:23:43 -05:00
|
|
|
std::ifstream ifs(Poco::Path(TDOC, filename).toString(), std::ios::binary);
|
|
|
|
|
|
|
|
// Apparently std::ios::binary is not good
|
|
|
|
// enough to stop eating new-line chars!
|
|
|
|
ifs.unsetf(std::ios::skipws);
|
2016-06-13 19:31:14 -05:00
|
|
|
|
|
|
|
std::istream_iterator<char> start(ifs);
|
|
|
|
std::istream_iterator<char> end;
|
|
|
|
return std::vector<char>(start, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
std::vector<char> readDataFromFile(std::unique_ptr<std::fstream>& file)
|
|
|
|
{
|
|
|
|
file->seekg(0, std::ios_base::end);
|
|
|
|
const std::streamsize size = file->tellg();
|
|
|
|
|
|
|
|
std::vector<char> v;
|
|
|
|
v.resize(size);
|
|
|
|
|
|
|
|
file->seekg(0, std::ios_base::beg);
|
|
|
|
file->read(v.data(), size);
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2016-04-28 21:43:59 -05:00
|
|
|
inline
|
2017-02-28 15:49:24 -06:00
|
|
|
void getDocumentPathAndURL(const std::string& docFilename, std::string& documentPath, std::string& documentURL, std::string prefix)
|
2016-04-28 20:41:00 -05:00
|
|
|
{
|
2018-01-22 08:11:43 -06:00
|
|
|
const auto testname = prefix;
|
2017-01-05 21:46:00 -06:00
|
|
|
std::replace(prefix.begin(), prefix.end(), ' ', '_');
|
|
|
|
documentPath = FileUtil::getTempFilePath(TDOC, docFilename, prefix);
|
2016-09-29 12:20:52 -05:00
|
|
|
std::string encodedUri;
|
|
|
|
Poco::URI::encode("file://" + Poco::Path(documentPath).makeAbsolute().toString(), ":/?", encodedUri);
|
|
|
|
documentURL = "lool/" + encodedUri + "/ws";
|
2018-01-22 08:11:43 -06:00
|
|
|
TST_LOG("Test file: " << documentPath);
|
2016-04-28 20:41:00 -05:00
|
|
|
}
|
|
|
|
|
2016-04-28 21:44:40 -05:00
|
|
|
inline
|
2018-02-11 12:16:42 -06:00
|
|
|
void sendTextFrame(LOOLWebSocket& socket, const std::string& string, const std::string& testname)
|
2016-04-28 20:41:00 -05:00
|
|
|
{
|
2017-02-08 04:46:11 -06:00
|
|
|
#ifndef FUZZER
|
2018-01-22 08:11:43 -06:00
|
|
|
TST_LOG("Sending " << string.size() << " bytes: " << LOOLProtocol::getAbbreviatedMessage(string));
|
2017-02-08 04:46:11 -06:00
|
|
|
#else
|
2018-01-22 08:11:43 -06:00
|
|
|
(void) testname;
|
2017-02-08 04:46:11 -06:00
|
|
|
#endif
|
2016-04-28 20:41:00 -05:00
|
|
|
socket.sendFrame(string.data(), string.size());
|
|
|
|
}
|
|
|
|
|
2016-05-08 23:39:01 -05:00
|
|
|
inline
|
2016-11-10 02:47:25 -06:00
|
|
|
void sendTextFrame(const std::shared_ptr<LOOLWebSocket>& socket, const std::string& string, const std::string& name = "")
|
2016-05-08 23:39:01 -05:00
|
|
|
{
|
2016-05-13 07:46:39 -05:00
|
|
|
sendTextFrame(*socket, string, name);
|
2016-05-08 23:39:01 -05:00
|
|
|
}
|
|
|
|
|
2016-05-02 05:26:48 -05:00
|
|
|
inline
|
|
|
|
Poco::Net::HTTPClientSession* createSession(const Poco::URI& uri)
|
|
|
|
{
|
|
|
|
#if ENABLE_SSL
|
|
|
|
return new Poco::Net::HTTPSClientSession(uri.getHost(), uri.getPort());
|
|
|
|
#else
|
|
|
|
return new Poco::Net::HTTPClientSession(uri.getHost(), uri.getPort());
|
|
|
|
#endif
|
|
|
|
}
|
2016-04-28 20:41:00 -05:00
|
|
|
|
2016-05-03 02:40:25 -05:00
|
|
|
inline
|
2016-12-23 00:23:08 -06:00
|
|
|
std::string const & getTestServerURI()
|
2016-05-03 02:40:25 -05:00
|
|
|
{
|
2016-10-12 03:47:26 -05:00
|
|
|
static const char* clientPort = std::getenv("LOOL_TEST_CLIENT_PORT");
|
2016-05-03 02:40:25 -05:00
|
|
|
|
|
|
|
static std::string serverURI(
|
|
|
|
#if ENABLE_SSL
|
|
|
|
"https://127.0.0.1:"
|
|
|
|
#else
|
|
|
|
"http://127.0.0.1:"
|
|
|
|
#endif
|
|
|
|
+ (clientPort? std::string(clientPort) : std::to_string(DEFAULT_CLIENT_PORT_NUMBER)));
|
|
|
|
|
|
|
|
return serverURI;
|
|
|
|
}
|
|
|
|
|
2016-09-28 15:04:51 -05:00
|
|
|
inline
|
2018-02-11 12:16:42 -06:00
|
|
|
int getErrorCode(LOOLWebSocket& ws, std::string& message, const std::string& testname)
|
2016-09-28 15:04:51 -05:00
|
|
|
{
|
|
|
|
int flags = 0;
|
|
|
|
int bytes = 0;
|
|
|
|
Poco::UInt16 statusCode = -1;
|
|
|
|
Poco::Buffer<char> buffer(READ_BUFFER_SIZE);
|
|
|
|
|
|
|
|
message.clear();
|
2017-01-15 11:15:11 -06:00
|
|
|
ws.setReceiveTimeout(Poco::Timespan(5000000));
|
2016-09-28 15:04:51 -05:00
|
|
|
do
|
|
|
|
{
|
2016-11-25 03:46:01 -06:00
|
|
|
bytes = ws.receiveFrame(buffer.begin(), READ_BUFFER_SIZE, flags);
|
2018-01-22 08:11:43 -06:00
|
|
|
TST_LOG("Got " << LOOLProtocol::getAbbreviatedFrameDump(buffer.begin(), bytes, flags));
|
2017-06-15 14:03:09 -05:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(POLL_TIMEOUT_MS));
|
2016-09-28 15:04:51 -05:00
|
|
|
}
|
2017-02-28 21:31:36 -06:00
|
|
|
while (bytes > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE);
|
2016-09-28 15:04:51 -05:00
|
|
|
|
|
|
|
if (bytes > 0)
|
|
|
|
{
|
|
|
|
Poco::MemoryBinaryReader reader(buffer, Poco::BinaryReader::NETWORK_BYTE_ORDER);
|
|
|
|
reader >> statusCode;
|
|
|
|
message.append(buffer.begin() + 2, bytes - 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return statusCode;
|
|
|
|
}
|
|
|
|
|
2017-01-03 16:02:53 -06:00
|
|
|
inline
|
2018-02-11 12:16:42 -06:00
|
|
|
int getErrorCode(const std::shared_ptr<LOOLWebSocket>& ws, std::string& message, const std::string& testname)
|
2017-01-03 16:02:53 -06:00
|
|
|
{
|
2017-01-15 11:15:11 -06:00
|
|
|
return getErrorCode(*ws, message, testname);
|
2017-01-03 16:02:53 -06:00
|
|
|
}
|
|
|
|
|
2016-05-08 23:39:01 -05:00
|
|
|
inline
|
2018-02-11 12:16:42 -06:00
|
|
|
std::vector<char> getResponseMessage(LOOLWebSocket& ws, const std::string& prefix, const std::string& testname, const size_t timeoutMs = 10000)
|
2016-05-08 23:39:01 -05:00
|
|
|
{
|
2016-05-03 22:35:34 -05:00
|
|
|
try
|
|
|
|
{
|
2016-05-10 20:38:03 -05:00
|
|
|
int flags = 0;
|
2016-09-20 19:41:30 -05:00
|
|
|
int retries = timeoutMs / 500;
|
|
|
|
const Poco::Timespan waitTime(retries ? timeoutMs * 1000 / retries : timeoutMs * 1000);
|
2016-05-03 22:35:34 -05:00
|
|
|
std::vector<char> response;
|
|
|
|
|
2016-09-20 19:41:30 -05:00
|
|
|
bool timedout = false;
|
2016-05-03 22:35:34 -05:00
|
|
|
ws.setReceiveTimeout(0);
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (ws.poll(waitTime, Poco::Net::Socket::SELECT_READ))
|
|
|
|
{
|
2016-09-20 19:41:30 -05:00
|
|
|
if (timedout)
|
|
|
|
{
|
2018-01-22 08:11:43 -06:00
|
|
|
TST_LOG_END;
|
2016-09-20 19:41:30 -05:00
|
|
|
timedout = false;
|
|
|
|
}
|
|
|
|
|
2017-04-02 23:45:40 -05:00
|
|
|
response.resize(READ_BUFFER_SIZE * 8);
|
2016-11-25 03:48:59 -06:00
|
|
|
int bytes = ws.receiveFrame(response.data(), response.size(), flags);
|
2016-11-26 21:28:48 -06:00
|
|
|
response.resize(std::max(bytes, 0));
|
2016-10-21 11:59:03 -05:00
|
|
|
const auto message = LOOLProtocol::getFirstLine(response);
|
2016-05-03 22:35:34 -05:00
|
|
|
if (bytes > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE)
|
|
|
|
{
|
2016-10-21 11:59:03 -05:00
|
|
|
if (LOOLProtocol::matchPrefix(prefix, message))
|
2016-05-03 22:35:34 -05:00
|
|
|
{
|
2018-01-22 08:11:43 -06:00
|
|
|
TST_LOG("[" << prefix << "] Matched " <<
|
|
|
|
LOOLProtocol::getAbbreviatedFrameDump(response.data(), bytes, flags));
|
2016-05-03 22:35:34 -05:00
|
|
|
return response;
|
|
|
|
}
|
2016-11-25 03:48:59 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
response.resize(0);
|
2016-05-03 22:35:34 -05:00
|
|
|
}
|
|
|
|
|
2016-05-12 09:54:21 -05:00
|
|
|
if (bytes <= 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2016-09-11 13:29:09 -05:00
|
|
|
|
2016-09-19 05:53:14 -05:00
|
|
|
if ((flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE)
|
2016-09-20 19:41:30 -05:00
|
|
|
{
|
2016-10-21 21:02:15 -05:00
|
|
|
// Don't ignore errors.
|
|
|
|
if (LOOLProtocol::matchPrefix("error:", message))
|
|
|
|
{
|
|
|
|
throw std::runtime_error(message);
|
|
|
|
}
|
|
|
|
|
2018-01-22 08:11:43 -06:00
|
|
|
TST_LOG("[" << prefix << "] Ignored " <<
|
|
|
|
LOOLProtocol::getAbbreviatedFrameDump(response.data(), bytes, flags));
|
2016-09-20 19:41:30 -05:00
|
|
|
}
|
2016-05-03 22:35:34 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-09-20 19:41:30 -05:00
|
|
|
if (!timedout)
|
|
|
|
{
|
2018-01-22 08:11:43 -06:00
|
|
|
TST_LOG_BEGIN("Timeout (" << (retries > 1 ? "retrying" : "giving up") << ") ");
|
2016-09-20 19:41:30 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-01-22 08:11:43 -06:00
|
|
|
TST_LOG_APPEND(retries << ' ');
|
2016-09-20 19:41:30 -05:00
|
|
|
}
|
|
|
|
|
2016-05-03 22:35:34 -05:00
|
|
|
--retries;
|
2016-09-20 19:41:30 -05:00
|
|
|
timedout = true;
|
2016-05-03 22:35:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while (retries > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE);
|
2016-10-21 11:59:03 -05:00
|
|
|
|
|
|
|
if (timedout)
|
|
|
|
{
|
2018-01-22 08:11:43 -06:00
|
|
|
TST_LOG_END;
|
2016-10-21 11:59:03 -05:00
|
|
|
}
|
2016-05-03 22:35:34 -05:00
|
|
|
}
|
|
|
|
catch (const Poco::Net::WebSocketException& exc)
|
|
|
|
{
|
2018-01-22 08:11:43 -06:00
|
|
|
TST_LOG_END;
|
|
|
|
TST_LOG(exc.message());
|
2016-05-03 22:35:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return std::vector<char>();
|
|
|
|
}
|
|
|
|
|
2016-05-08 23:39:01 -05:00
|
|
|
inline
|
2018-02-11 12:16:42 -06:00
|
|
|
std::vector<char> getResponseMessage(const std::shared_ptr<LOOLWebSocket>& ws, const std::string& prefix, const std::string& testname, const size_t timeoutMs = 10000)
|
2016-05-10 20:38:03 -05:00
|
|
|
{
|
2018-01-22 08:11:43 -06:00
|
|
|
return getResponseMessage(*ws, prefix, testname, timeoutMs);
|
2016-05-10 20:38:03 -05:00
|
|
|
}
|
|
|
|
|
2016-10-08 20:24:45 -05:00
|
|
|
template <typename T>
|
2018-02-11 12:16:42 -06:00
|
|
|
std::string getResponseString(T& ws, const std::string& prefix, const std::string& testname, const size_t timeoutMs = 10000)
|
2016-10-08 18:40:49 -05:00
|
|
|
{
|
2018-01-22 08:11:43 -06:00
|
|
|
const auto response = getResponseMessage(ws, prefix, testname, timeoutMs);
|
2016-10-08 18:40:49 -05:00
|
|
|
return std::string(response.data(), response.size());
|
|
|
|
}
|
|
|
|
|
2016-05-10 20:38:03 -05:00
|
|
|
template <typename T>
|
2018-08-13 02:26:09 -05:00
|
|
|
std::string assertResponseString(T& ws, const std::string& prefix, const std::string& testname)
|
2016-05-10 20:38:03 -05:00
|
|
|
{
|
2018-01-22 08:11:43 -06:00
|
|
|
const auto res = getResponseString(ws, prefix, testname);
|
2016-07-06 22:23:43 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(prefix, res.substr(0, prefix.length()));
|
2016-05-13 12:46:13 -05:00
|
|
|
return res;
|
2016-05-10 20:38:03 -05:00
|
|
|
}
|
|
|
|
|
2016-08-30 22:15:44 -05:00
|
|
|
/// Assert that we don't get a response with the given prefix.
|
|
|
|
template <typename T>
|
2018-08-27 02:10:01 -05:00
|
|
|
std::string assertNotInResponse(T& ws, const std::string& prefix, const std::string& testname)
|
2016-08-30 22:15:44 -05:00
|
|
|
{
|
2018-01-22 08:11:43 -06:00
|
|
|
const auto res = getResponseString(ws, prefix, testname, 1000);
|
|
|
|
CPPUNIT_ASSERT_MESSAGE(testname + "Did not expect getting message [" + res + "].", res.empty());
|
2016-08-30 22:15:44 -05:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-10-08 20:48:41 -05:00
|
|
|
inline
|
2018-02-11 12:16:42 -06:00
|
|
|
bool isDocumentLoaded(LOOLWebSocket& ws, const std::string& testname, bool isView = true)
|
2016-10-08 20:48:41 -05:00
|
|
|
{
|
|
|
|
const std::string prefix = isView ? "status:" : "statusindicatorfinish:";
|
2018-01-22 08:11:43 -06:00
|
|
|
const auto message = getResponseString(ws, prefix, testname);
|
2016-11-26 21:24:35 -06:00
|
|
|
return LOOLProtocol::matchPrefix(prefix, message);
|
2016-10-08 20:48:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
2018-02-11 12:16:42 -06:00
|
|
|
bool isDocumentLoaded(std::shared_ptr<LOOLWebSocket>& ws, const std::string& testname, bool isView = true)
|
2016-10-08 20:48:41 -05:00
|
|
|
{
|
2018-01-22 08:11:43 -06:00
|
|
|
return isDocumentLoaded(*ws, testname, isView);
|
2016-10-08 20:48:41 -05:00
|
|
|
}
|
|
|
|
|
2016-05-10 20:38:03 -05:00
|
|
|
// Connecting to a Kit process is managed by document broker, that it does several
|
|
|
|
// jobs to establish the bridge connection between the Client and Kit process,
|
|
|
|
// The result, it is mostly time outs to get messages in the unit test and it could fail.
|
|
|
|
// connectLOKit ensures the websocket is connected to a kit process.
|
|
|
|
inline
|
2016-11-10 02:47:25 -06:00
|
|
|
std::shared_ptr<LOOLWebSocket>
|
2016-09-15 03:33:18 -05:00
|
|
|
connectLOKit(const Poco::URI& uri,
|
2016-05-10 20:38:03 -05:00
|
|
|
Poco::Net::HTTPRequest& request,
|
2016-05-13 07:46:39 -05:00
|
|
|
Poco::Net::HTTPResponse& response,
|
2018-02-11 12:16:42 -06:00
|
|
|
const std::string& testname)
|
2016-05-10 20:38:03 -05:00
|
|
|
{
|
2018-01-22 08:11:43 -06:00
|
|
|
TST_LOG_BEGIN("Connecting... ");
|
2016-05-10 20:38:03 -05:00
|
|
|
int retries = 10;
|
|
|
|
do
|
|
|
|
{
|
2016-11-15 15:22:02 -06:00
|
|
|
try
|
2016-11-12 12:25:35 -06:00
|
|
|
{
|
2016-11-15 15:22:02 -06:00
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> session(createSession(uri));
|
|
|
|
auto ws = std::make_shared<LOOLWebSocket>(*session, request, response);
|
2018-02-07 03:17:42 -06:00
|
|
|
const char* expected_response = "statusindicator: ready";
|
2018-01-22 08:11:43 -06:00
|
|
|
if (getResponseString(ws, expected_response, testname) == expected_response)
|
2016-11-15 15:22:02 -06:00
|
|
|
{
|
|
|
|
return ws;
|
|
|
|
}
|
|
|
|
|
2018-01-22 08:11:43 -06:00
|
|
|
TST_LOG_APPEND(11 - retries);
|
2016-11-15 15:22:02 -06:00
|
|
|
}
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
2018-01-22 08:11:43 -06:00
|
|
|
TST_LOG_END;
|
2018-06-15 08:14:14 -05:00
|
|
|
TST_LOG("Connection problem: " << ex.what());
|
2016-11-12 12:25:35 -06:00
|
|
|
}
|
2016-05-10 20:38:03 -05:00
|
|
|
|
2016-11-15 15:22:02 -06:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(POLL_TIMEOUT_MS));
|
2016-05-10 20:38:03 -05:00
|
|
|
}
|
|
|
|
while (retries--);
|
|
|
|
|
2018-01-22 08:11:43 -06:00
|
|
|
TST_LOG_END;
|
2016-11-12 12:25:35 -06:00
|
|
|
throw std::runtime_error("Cannot connect to [" + uri.toString() + "].");
|
2016-05-08 23:39:01 -05:00
|
|
|
}
|
2016-05-03 22:35:34 -05:00
|
|
|
|
2016-04-28 21:44:40 -05:00
|
|
|
inline
|
2018-02-11 12:16:42 -06:00
|
|
|
std::shared_ptr<LOOLWebSocket> loadDocAndGetSocket(const Poco::URI& uri, const std::string& documentURL, const std::string& testname, bool isView = true)
|
2016-04-28 20:41:00 -05:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Load a document and get its status.
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL);
|
|
|
|
Poco::Net::HTTPResponse response;
|
2018-01-22 08:11:43 -06:00
|
|
|
std::shared_ptr<LOOLWebSocket> socket = connectLOKit(uri, request, response, testname);
|
2016-04-28 20:41:00 -05:00
|
|
|
|
2018-01-22 08:11:43 -06:00
|
|
|
sendTextFrame(socket, "load url=" + documentURL, testname);
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("cannot load the document " + documentURL, isDocumentLoaded(*socket, testname, isView));
|
2016-04-28 20:41:00 -05:00
|
|
|
|
2018-01-22 08:11:43 -06:00
|
|
|
TST_LOG("Loaded document [" << documentURL << "].");
|
2016-04-28 20:41:00 -05:00
|
|
|
return socket;
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
|
|
|
CPPUNIT_FAIL(exc.displayText());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Really couldn't reach here, but the compiler doesn't know any better.
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-08-28 11:25:40 -05:00
|
|
|
inline
|
2018-02-11 12:16:42 -06:00
|
|
|
std::shared_ptr<LOOLWebSocket> loadDocAndGetSocket(const std::string& docFilename, const Poco::URI& uri, const std::string& testname, bool isView = true)
|
2016-08-28 11:25:40 -05:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
std::string documentPath, documentURL;
|
2018-01-22 08:11:43 -06:00
|
|
|
getDocumentPathAndURL(docFilename, documentPath, documentURL, testname);
|
|
|
|
return loadDocAndGetSocket(uri, documentURL, testname, isView);
|
2016-08-28 11:25:40 -05:00
|
|
|
}
|
|
|
|
catch (const Poco::Exception& exc)
|
|
|
|
{
|
|
|
|
CPPUNIT_FAIL(exc.displayText());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Really couldn't reach here, but the compiler doesn't know any better.
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-04-28 21:43:59 -05:00
|
|
|
inline
|
2018-01-22 08:11:43 -06:00
|
|
|
void SocketProcessor(const std::string& testname,
|
2016-11-10 02:47:25 -06:00
|
|
|
const std::shared_ptr<LOOLWebSocket>& socket,
|
2016-09-15 03:33:18 -05:00
|
|
|
const std::function<bool(const std::string& msg)>& handler,
|
2016-04-28 21:43:59 -05:00
|
|
|
const size_t timeoutMs = 10000)
|
2016-04-28 20:41:00 -05:00
|
|
|
{
|
2016-04-28 21:43:59 -05:00
|
|
|
socket->setReceiveTimeout(0);
|
|
|
|
|
|
|
|
const Poco::Timespan waitTime(timeoutMs * 1000);
|
2016-05-13 07:46:39 -05:00
|
|
|
int flags = 0;
|
|
|
|
int n = 0;
|
2016-11-25 03:46:01 -06:00
|
|
|
char buffer[READ_BUFFER_SIZE];
|
2016-04-28 20:41:00 -05:00
|
|
|
do
|
|
|
|
{
|
2016-04-28 21:43:59 -05:00
|
|
|
if (!socket->poll(waitTime, Poco::Net::Socket::SELECT_READ))
|
|
|
|
{
|
2018-01-22 08:11:43 -06:00
|
|
|
TST_LOG("Timeout polling.");
|
2016-04-28 21:43:59 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-11-25 03:46:01 -06:00
|
|
|
n = socket->receiveFrame(buffer, sizeof(buffer), flags);
|
2018-01-22 08:11:43 -06:00
|
|
|
TST_LOG("Got " << LOOLProtocol::getAbbreviatedFrameDump(buffer, n, flags));
|
2016-04-28 20:41:00 -05:00
|
|
|
if (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE)
|
|
|
|
{
|
2016-11-25 03:46:01 -06:00
|
|
|
if (!handler(std::string(buffer, n)))
|
2016-04-28 20:41:00 -05:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE);
|
|
|
|
}
|
|
|
|
|
2016-05-22 17:52:36 -05:00
|
|
|
inline
|
2016-08-30 17:19:05 -05:00
|
|
|
void parseDocSize(const std::string& message, const std::string& type,
|
|
|
|
int& part, int& parts, int& width, int& height, int& viewid)
|
2016-05-22 17:52:36 -05:00
|
|
|
{
|
|
|
|
Poco::StringTokenizer tokens(message, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
|
|
|
|
|
|
|
|
// Expected format is something like 'type= parts= current= width= height='.
|
|
|
|
const std::string text = tokens[0].substr(std::string("type=").size());
|
|
|
|
parts = std::stoi(tokens[1].substr(std::string("parts=").size()));
|
|
|
|
part = std::stoi(tokens[2].substr(std::string("current=").size()));
|
|
|
|
width = std::stoi(tokens[3].substr(std::string("width=").size()));
|
|
|
|
height = std::stoi(tokens[4].substr(std::string("height=").size()));
|
2016-07-26 07:03:46 -05:00
|
|
|
viewid = std::stoi(tokens[5].substr(std::string("viewid=").size()));
|
2016-05-22 17:52:36 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(type, text);
|
|
|
|
CPPUNIT_ASSERT(parts > 0);
|
|
|
|
CPPUNIT_ASSERT(part >= 0);
|
|
|
|
CPPUNIT_ASSERT(width > 0);
|
|
|
|
CPPUNIT_ASSERT(height > 0);
|
2016-07-26 07:03:46 -05:00
|
|
|
CPPUNIT_ASSERT(viewid >= 0);
|
2016-05-22 17:52:36 -05:00
|
|
|
}
|
|
|
|
|
2016-05-22 18:53:24 -05:00
|
|
|
inline
|
2018-02-11 12:16:42 -06:00
|
|
|
std::vector<char> getTileMessage(LOOLWebSocket& ws, const std::string& testname)
|
2016-05-22 18:53:24 -05:00
|
|
|
{
|
2018-01-22 08:11:43 -06:00
|
|
|
return getResponseMessage(ws, "tile", testname);
|
2016-05-22 18:53:24 -05:00
|
|
|
}
|
|
|
|
|
2016-08-21 19:16:56 -05:00
|
|
|
inline
|
2018-02-11 12:16:42 -06:00
|
|
|
std::vector<char> assertTileMessage(LOOLWebSocket& ws, const std::string& testname)
|
2016-08-21 19:16:56 -05:00
|
|
|
{
|
2018-01-22 08:11:43 -06:00
|
|
|
const std::vector<char> response = getTileMessage(ws, testname);
|
2016-08-21 19:16:56 -05:00
|
|
|
|
|
|
|
const std::string firstLine = LOOLProtocol::getFirstLine(response);
|
|
|
|
Poco::StringTokenizer tileTokens(firstLine, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("tile:"), tileTokens[0]);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("part="), tileTokens[1].substr(0, std::string("part=").size()));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("width="), tileTokens[2].substr(0, std::string("width=").size()));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("height="), tileTokens[3].substr(0, std::string("height=").size()));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("tileposx="), tileTokens[4].substr(0, std::string("tileposx=").size()));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("tileposy="), tileTokens[5].substr(0, std::string("tileposy=").size()));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("tilewidth="), tileTokens[6].substr(0, std::string("tilewidth=").size()));
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("tileheight="), tileTokens[7].substr(0, std::string("tileheight=").size()));
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
2018-02-11 12:16:42 -06:00
|
|
|
std::vector<char> assertTileMessage(const std::shared_ptr<LOOLWebSocket>& ws, const std::string& testname)
|
2016-08-21 19:16:56 -05:00
|
|
|
{
|
2018-01-22 08:11:43 -06:00
|
|
|
return assertTileMessage(*ws, testname);
|
2016-08-21 19:16:56 -05:00
|
|
|
}
|
|
|
|
|
2016-05-12 04:23:37 -05:00
|
|
|
enum SpecialKey { skNone=0, skShift=0x1000, skCtrl=0x2000, skAlt=0x4000 };
|
|
|
|
|
|
|
|
inline int getCharChar(char ch, SpecialKey specialKeys)
|
|
|
|
{
|
|
|
|
// Some primitive code just suitable to basic needs of specific test.
|
|
|
|
// TODO: improve as appropriate.
|
|
|
|
if (specialKeys & (skCtrl | skAlt))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
switch (ch)
|
|
|
|
{
|
|
|
|
case '\x0a': // Enter
|
|
|
|
return 13;
|
|
|
|
default:
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int getCharKey(char ch, SpecialKey specialKeys)
|
|
|
|
{
|
|
|
|
// Some primitive code just suitable to basic needs of specific test.
|
|
|
|
// TODO: improve as appropriate.
|
|
|
|
int result;
|
|
|
|
switch (ch)
|
|
|
|
{
|
|
|
|
case '\x0a': // Enter
|
|
|
|
result = 1280;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result = ch;
|
|
|
|
}
|
|
|
|
return result | specialKeys;
|
|
|
|
}
|
|
|
|
|
2018-02-11 12:16:42 -06:00
|
|
|
inline void sendKeyEvent(std::shared_ptr<LOOLWebSocket>& socket, const char* type, int chr, int key, const std::string& testname)
|
2016-05-12 04:23:37 -05:00
|
|
|
{
|
|
|
|
std::ostringstream ssIn;
|
|
|
|
ssIn << "key type=" << type << " char=" << chr << " key=" << key;
|
2016-10-07 22:38:11 -05:00
|
|
|
sendTextFrame(socket, ssIn.str(), testname);
|
2016-05-12 04:23:37 -05:00
|
|
|
}
|
|
|
|
|
2016-11-10 02:47:25 -06:00
|
|
|
inline void sendKeyPress(std::shared_ptr<LOOLWebSocket>& socket, int chr, int key, const std::string& testname)
|
2016-05-12 04:23:37 -05:00
|
|
|
{
|
2016-10-07 22:38:11 -05:00
|
|
|
sendKeyEvent(socket, "input", chr, key, testname);
|
|
|
|
sendKeyEvent(socket, "up", chr, key, testname);
|
2016-05-12 04:23:37 -05:00
|
|
|
}
|
|
|
|
|
2018-02-11 12:16:42 -06:00
|
|
|
inline void sendChar(std::shared_ptr<LOOLWebSocket>& socket, char ch, SpecialKey specialKeys, const std::string& testname)
|
2016-05-12 04:23:37 -05:00
|
|
|
{
|
2016-10-07 22:38:11 -05:00
|
|
|
sendKeyPress(socket, getCharChar(ch, specialKeys), getCharKey(ch, specialKeys), testname);
|
2016-05-12 04:23:37 -05:00
|
|
|
}
|
|
|
|
|
2018-02-11 12:16:42 -06:00
|
|
|
inline void sendText(std::shared_ptr<LOOLWebSocket>& socket, const std::string& text, const std::string& testname)
|
2016-05-12 04:23:37 -05:00
|
|
|
{
|
|
|
|
for (char ch : text)
|
|
|
|
{
|
2016-10-07 22:38:11 -05:00
|
|
|
sendChar(socket, ch, skNone, testname);
|
2016-05-12 04:23:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-30 20:54:38 -05:00
|
|
|
inline std::vector<char> getTileAndSave(std::shared_ptr<LOOLWebSocket>& socket,
|
|
|
|
const std::string& req,
|
|
|
|
const std::string& filename,
|
|
|
|
const std::string& testname)
|
|
|
|
{
|
2018-01-22 08:11:43 -06:00
|
|
|
TST_LOG("Requesting: " << req);
|
2017-04-30 20:54:38 -05:00
|
|
|
sendTextFrame(socket, req, testname);
|
|
|
|
|
2018-02-07 03:17:42 -06:00
|
|
|
const std::vector<char> tile = getResponseMessage(socket, "tile:", testname);
|
2018-01-22 08:11:43 -06:00
|
|
|
TST_LOG(" Tile PNG size: " << tile.size());
|
2017-04-30 20:54:38 -05:00
|
|
|
|
|
|
|
const std::string firstLine = LOOLProtocol::getFirstLine(tile);
|
|
|
|
std::vector<char> res(tile.begin() + firstLine.size() + 1, tile.end());
|
|
|
|
std::stringstream streamRes;
|
|
|
|
std::copy(res.begin(), res.end(), std::ostream_iterator<char>(streamRes));
|
|
|
|
|
|
|
|
if (!filename.empty())
|
|
|
|
{
|
|
|
|
std::fstream outStream(filename, std::ios::out);
|
|
|
|
outStream.write(res.data(), res.size());
|
|
|
|
outStream.close();
|
2018-01-22 08:11:43 -06:00
|
|
|
TST_LOG("Saved [" << firstLine << "] to [" << filename << "]");
|
2017-04-30 20:54:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-05-01 23:01:28 -05:00
|
|
|
inline void getServerVersion(LOOLWebSocket& socket,
|
|
|
|
int& major, int& minor,
|
|
|
|
const std::string& testname)
|
|
|
|
{
|
|
|
|
const std::string clientVersion = "loolclient 0.1";
|
2018-02-11 12:16:42 -06:00
|
|
|
sendTextFrame(socket, clientVersion, testname);
|
2017-05-01 23:01:28 -05:00
|
|
|
std::vector<char> loVersion = getResponseMessage(socket, "lokitversion", testname);
|
|
|
|
std::string line = LOOLProtocol::getFirstLine(loVersion.data(), loVersion.size());
|
|
|
|
line = line.substr(strlen("lokitversion "));
|
|
|
|
Poco::JSON::Parser parser;
|
|
|
|
Poco::Dynamic::Var loVersionVar = parser.parse(line);
|
|
|
|
const Poco::SharedPtr<Poco::JSON::Object>& loVersionObject = loVersionVar.extract<Poco::JSON::Object::Ptr>();
|
|
|
|
std::string loProductVersion = loVersionObject->get("ProductVersion").toString();
|
|
|
|
std::istringstream stream(loProductVersion);
|
|
|
|
stream >> major;
|
|
|
|
assert(stream.get() == '.');
|
|
|
|
stream >> minor;
|
|
|
|
|
2018-01-22 08:11:43 -06:00
|
|
|
TST_LOG("Client [" << major << '.' << minor << "].");
|
2017-05-01 23:01:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void getServerVersion(std::shared_ptr<LOOLWebSocket>& socket,
|
|
|
|
int& major, int& minor,
|
|
|
|
const std::string& testname)
|
|
|
|
{
|
|
|
|
getServerVersion(*socket, major, minor, testname);
|
|
|
|
}
|
|
|
|
|
2016-04-28 20:41:00 -05:00
|
|
|
}
|
|
|
|
|
2017-02-07 10:16:59 -06:00
|
|
|
#endif
|
|
|
|
|
2016-04-28 20:41:00 -05:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|