2016-04-18 08:29:17 -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-12-20 07:06:26 -06:00
|
|
|
#include <config.h>
|
2016-04-18 08:29:17 -05:00
|
|
|
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <mutex>
|
2019-11-19 02:30:30 -06:00
|
|
|
#include <thread>
|
|
|
|
#include <chrono>
|
2016-04-18 08:29:17 -05:00
|
|
|
|
|
|
|
#include <Poco/Net/HTTPBasicCredentials.h>
|
|
|
|
#include <Poco/Net/HTTPCookie.h>
|
|
|
|
#include <Poco/Net/HTTPResponse.h>
|
|
|
|
#include <Poco/Net/NameValueCollection.h>
|
|
|
|
#include <Poco/Net/NetException.h>
|
|
|
|
#include <Poco/URI.h>
|
|
|
|
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <Common.hpp>
|
|
|
|
#include <Log.hpp>
|
|
|
|
#include <Unit.hpp>
|
|
|
|
#include <UnitHTTP.hpp>
|
|
|
|
#include <Util.hpp>
|
|
|
|
#include <helpers.hpp>
|
2016-05-02 05:26:48 -05:00
|
|
|
|
2016-04-18 08:29:17 -05:00
|
|
|
#define UNIT_URI "/loolwsd/unit-admin"
|
|
|
|
|
|
|
|
using Poco::Net::HTTPBasicCredentials;
|
|
|
|
using Poco::Net::HTTPCookie;
|
|
|
|
using Poco::Net::HTTPRequest;
|
|
|
|
using Poco::Net::HTTPResponse;
|
2016-05-02 05:26:48 -05:00
|
|
|
using Poco::Net::HTTPClientSession;
|
2016-04-18 08:29:17 -05:00
|
|
|
|
|
|
|
// Inside the WSD process
|
|
|
|
class UnitAdmin : public UnitWSD
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
unsigned _testCounter = 0;
|
|
|
|
std::string _jwtCookie;
|
|
|
|
bool _isTestRunning = false;
|
|
|
|
const Poco::URI _uri;
|
2016-11-10 02:47:25 -06:00
|
|
|
std::shared_ptr<LOOLWebSocket> _adminWs;
|
2016-04-18 08:29:17 -05:00
|
|
|
|
|
|
|
typedef TestResult (UnitAdmin::*AdminTest)(void);
|
|
|
|
std::vector<AdminTest> _tests;
|
|
|
|
|
2016-11-10 02:47:25 -06:00
|
|
|
std::shared_ptr<LOOLWebSocket> _docWs1;
|
|
|
|
std::shared_ptr<LOOLWebSocket> _docWs2;
|
|
|
|
std::shared_ptr<LOOLWebSocket> _docWs3;
|
2016-04-18 08:29:17 -05:00
|
|
|
int _docPid1;
|
|
|
|
int _docPid2;
|
|
|
|
int _docPid3;
|
|
|
|
int _usersCount = 0;
|
|
|
|
int _docsCount = 0;
|
|
|
|
|
|
|
|
int _messageTimeoutMilliSeconds = 5000;
|
|
|
|
std::condition_variable _messageReceivedCV;
|
|
|
|
std::mutex _messageReceivedMutex;
|
|
|
|
std::string _messageReceived;
|
|
|
|
|
|
|
|
// Tests
|
|
|
|
private:
|
|
|
|
TestResult testIncorrectPassword()
|
|
|
|
{
|
|
|
|
HTTPResponse response;
|
|
|
|
std::string path(_uri.getPathAndQuery());
|
|
|
|
HTTPRequest request(HTTPRequest::HTTP_GET, path);
|
2016-10-20 16:09:00 -05:00
|
|
|
std::unique_ptr<HTTPClientSession> session(UnitHTTP::createSession());
|
2016-04-18 08:29:17 -05:00
|
|
|
|
2016-05-02 05:26:48 -05:00
|
|
|
session->sendRequest(request);
|
|
|
|
session->receiveResponse(response);
|
2016-12-22 03:27:30 -06:00
|
|
|
TestResult res = TestResult::Failed;
|
2016-04-18 08:29:17 -05:00
|
|
|
if (response.getStatus() == HTTPResponse::HTTP_UNAUTHORIZED)
|
2016-12-22 03:27:30 -06:00
|
|
|
res = TestResult::Ok;
|
2016-04-18 08:29:17 -05:00
|
|
|
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("testIncorrectPassword: " << (res == TestResult::Ok ? "OK" : "FAIL"));
|
2016-04-18 08:29:17 -05:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
TestResult testCorrectPassword()
|
|
|
|
{
|
|
|
|
HTTPResponse response;
|
|
|
|
std::string path(_uri.getPathAndQuery());
|
|
|
|
HTTPRequest request(HTTPRequest::HTTP_GET, path);
|
2016-10-20 16:09:00 -05:00
|
|
|
std::unique_ptr<HTTPClientSession> session(UnitHTTP::createSession());
|
2016-04-18 08:29:17 -05:00
|
|
|
HTTPBasicCredentials credentials("admin", "admin");
|
|
|
|
credentials.authenticate(request);
|
|
|
|
|
2016-05-02 05:26:48 -05:00
|
|
|
session->sendRequest(request);
|
|
|
|
session->receiveResponse(response);
|
2016-04-18 08:29:17 -05:00
|
|
|
std::vector<HTTPCookie> cookies;
|
|
|
|
response.getCookies(cookies);
|
|
|
|
|
|
|
|
// For now we only set one cookie
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT_EQUAL(1, static_cast<int>(cookies.size()));
|
2016-04-18 08:29:17 -05:00
|
|
|
// and it is jwt=
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT_EQUAL(std::string("jwt"), cookies[0].getName());
|
2016-04-18 08:29:17 -05:00
|
|
|
|
|
|
|
// Check cookie properties
|
|
|
|
std::string cookiePath = cookies[0].getPath();
|
|
|
|
bool secure = cookies[0].getSecure();
|
|
|
|
std::string value = cookies[0].getValue();
|
2016-12-22 03:27:30 -06:00
|
|
|
TestResult res = TestResult::Failed;
|
2016-07-28 07:28:40 -05:00
|
|
|
if (cookiePath.find_first_of("/loleaflet/dist/admin/") == 0 &&
|
|
|
|
value != "" &&
|
|
|
|
secure)
|
2016-04-18 08:29:17 -05:00
|
|
|
{
|
|
|
|
// Set JWT cookie to be used for subsequent tests
|
|
|
|
_jwtCookie = value;
|
2016-12-22 03:27:30 -06:00
|
|
|
res = TestResult::Ok;
|
2016-04-18 08:29:17 -05:00
|
|
|
}
|
2016-07-28 07:28:40 -05:00
|
|
|
else
|
|
|
|
{
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("testCorrectPassword: Invalid cookie properties");
|
2016-07-28 07:28:40 -05:00
|
|
|
}
|
2016-04-18 08:29:17 -05:00
|
|
|
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("testCorrectPassword: " << (res == TestResult::Ok ? "OK" : "FAIL"));
|
2016-04-18 08:29:17 -05:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-07-28 07:28:40 -05:00
|
|
|
TestResult testWebSocketWithoutAuthToken()
|
2016-04-18 08:29:17 -05:00
|
|
|
{
|
2016-07-28 07:28:40 -05:00
|
|
|
// try connecting without authentication; should result in NotAuthenticated
|
2016-04-18 08:29:17 -05:00
|
|
|
HTTPResponse response;
|
2016-07-28 07:28:40 -05:00
|
|
|
HTTPRequest request(HTTPRequest::HTTP_GET, "/lool/adminws/");
|
2016-10-20 16:09:00 -05:00
|
|
|
std::unique_ptr<HTTPClientSession> session(UnitHTTP::createSession());
|
2016-07-28 07:28:40 -05:00
|
|
|
|
2016-11-10 02:47:25 -06:00
|
|
|
_adminWs = std::make_shared<LOOLWebSocket>(*session, request, response);
|
2016-07-28 07:28:40 -05:00
|
|
|
const std::string testMessage = "documents";
|
|
|
|
std::unique_lock<std::mutex> lock(_messageReceivedMutex);
|
|
|
|
_messageReceived.clear();
|
|
|
|
_adminWs->sendFrame(testMessage.data(), testMessage.size());
|
|
|
|
if (_messageReceivedCV.wait_for(lock, std::chrono::milliseconds(_messageTimeoutMilliSeconds)) == std::cv_status::timeout)
|
2016-04-18 08:29:17 -05:00
|
|
|
{
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("testWebSocketWithoutAuth: Timed out waiting for admin console message");
|
2016-12-22 03:27:30 -06:00
|
|
|
return TestResult::TimedOut;
|
2016-04-18 08:29:17 -05:00
|
|
|
}
|
2016-07-28 07:28:40 -05:00
|
|
|
lock.unlock();
|
|
|
|
|
2020-02-28 03:50:58 -06:00
|
|
|
StringVector tokens(LOOLProtocol::tokenize(_messageReceived, ' '));
|
2019-11-13 00:48:30 -06:00
|
|
|
if (tokens.size() != 1 ||
|
2016-07-28 07:28:40 -05:00
|
|
|
tokens[0] != "NotAuthenticated")
|
2016-04-18 08:29:17 -05:00
|
|
|
{
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("testWebSocketWithoutAuth: Unrecognized message format");
|
2016-12-22 03:27:30 -06:00
|
|
|
return TestResult::Failed;
|
2016-04-18 08:29:17 -05:00
|
|
|
}
|
|
|
|
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("testWebSocketWithoutAuth: OK");
|
2016-12-22 03:27:30 -06:00
|
|
|
return TestResult::Ok;
|
2016-04-18 08:29:17 -05:00
|
|
|
}
|
|
|
|
|
2016-07-28 07:28:40 -05:00
|
|
|
TestResult testWebSocketWithIncorrectAuthToken()
|
2016-04-18 08:29:17 -05:00
|
|
|
{
|
2016-07-28 07:28:40 -05:00
|
|
|
// try connecting with incorrect auth token; should result in InvalidToken
|
2016-04-18 08:29:17 -05:00
|
|
|
HTTPResponse response;
|
2016-06-08 05:21:29 -05:00
|
|
|
HTTPRequest request(HTTPRequest::HTTP_GET, "/lool/adminws/");
|
2016-10-20 16:09:00 -05:00
|
|
|
std::unique_ptr<HTTPClientSession> session(UnitHTTP::createSession());
|
2016-04-18 08:29:17 -05:00
|
|
|
|
2016-11-10 02:47:25 -06:00
|
|
|
_adminWs = std::make_shared<LOOLWebSocket>(*session, request, response);
|
2016-07-28 07:28:40 -05:00
|
|
|
const std::string testMessage = "auth jwt=incorrectJWT";
|
|
|
|
std::unique_lock<std::mutex> lock(_messageReceivedMutex);
|
|
|
|
_messageReceived.clear();
|
|
|
|
_adminWs->sendFrame(testMessage.data(), testMessage.size());
|
|
|
|
if (_messageReceivedCV.wait_for(lock, std::chrono::milliseconds(_messageTimeoutMilliSeconds)) == std::cv_status::timeout)
|
2016-04-18 08:29:17 -05:00
|
|
|
{
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("testWebSocketWithIncorrectAuthToken: Timed out waiting for admin console message");
|
2016-12-22 03:27:30 -06:00
|
|
|
return TestResult::TimedOut;
|
2016-04-18 08:29:17 -05:00
|
|
|
}
|
2016-07-28 07:28:40 -05:00
|
|
|
lock.unlock();
|
|
|
|
|
2020-02-28 03:50:58 -06:00
|
|
|
StringVector tokens(LOOLProtocol::tokenize(_messageReceived, ' '));
|
2019-11-13 00:48:30 -06:00
|
|
|
if (tokens.size() != 1 ||
|
2016-07-28 07:28:40 -05:00
|
|
|
tokens[0] != "InvalidAuthToken")
|
2016-04-18 08:29:17 -05:00
|
|
|
{
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("testWebSocketWithIncorrectAuthToken: Unrecognized message format");
|
2016-12-22 03:27:30 -06:00
|
|
|
return TestResult::Failed;
|
2016-04-18 08:29:17 -05:00
|
|
|
}
|
|
|
|
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("testWebSocketWithIncorrectAuthToken: OK");
|
2016-12-22 03:27:30 -06:00
|
|
|
return TestResult::Ok;
|
2016-04-18 08:29:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
TestResult testAddDocNotify()
|
|
|
|
{
|
2016-07-28 07:28:40 -05:00
|
|
|
// Authenticate first
|
|
|
|
HTTPResponse response;
|
|
|
|
HTTPRequest request(HTTPRequest::HTTP_GET, "/lool/adminws/");
|
2016-10-20 16:09:00 -05:00
|
|
|
std::unique_ptr<HTTPClientSession> session(UnitHTTP::createSession());
|
2016-07-28 07:28:40 -05:00
|
|
|
|
2016-11-10 02:47:25 -06:00
|
|
|
_adminWs = std::make_shared<LOOLWebSocket>(*session, request, response);
|
2016-07-28 07:28:40 -05:00
|
|
|
const std::string authMessage = "auth jwt=" + _jwtCookie;
|
|
|
|
_adminWs->sendFrame(authMessage.data(), authMessage.size());
|
|
|
|
|
2016-04-18 08:29:17 -05:00
|
|
|
// subscribe notification on admin websocket
|
|
|
|
const std::string subscribeMessage = "subscribe adddoc";
|
|
|
|
_adminWs->sendFrame(subscribeMessage.data(), subscribeMessage.size());
|
|
|
|
|
2016-10-22 11:09:39 -05:00
|
|
|
// FIXME: we really should wait for the subscription to be
|
|
|
|
// registered and have a reply to avoid a race here.
|
2019-11-19 02:30:30 -06:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(250));
|
2016-09-29 12:20:52 -05:00
|
|
|
|
|
|
|
std::string documentPath1, documentURL1;
|
2017-02-28 15:49:24 -06:00
|
|
|
helpers::getDocumentPathAndURL("hello.odt", documentPath1, documentURL1, "unitAdmin-hello.odt ");
|
2016-04-18 08:29:17 -05:00
|
|
|
HTTPRequest request1(HTTPRequest::HTTP_GET, documentURL1);
|
|
|
|
HTTPResponse response1;
|
2016-05-03 02:40:25 -05:00
|
|
|
const Poco::URI docUri1(helpers::getTestServerURI());
|
2016-04-18 08:29:17 -05:00
|
|
|
const std::string loadMessage1 = "load url=" + documentURL1;
|
2016-10-20 16:09:00 -05:00
|
|
|
std::unique_ptr<HTTPClientSession> session1(UnitHTTP::createSession());
|
|
|
|
std::unique_ptr<HTTPClientSession> session2(UnitHTTP::createSession());
|
2016-04-18 08:29:17 -05:00
|
|
|
|
2016-07-13 14:16:16 -05:00
|
|
|
std::unique_lock<std::mutex> lock(_messageReceivedMutex);
|
|
|
|
_messageReceived.clear();
|
2016-11-10 02:47:25 -06:00
|
|
|
_docWs1 = std::make_shared<LOOLWebSocket>(*session1, request1, response1);
|
2016-07-13 14:16:16 -05:00
|
|
|
_docWs1->sendFrame(loadMessage1.data(), loadMessage1.size());
|
|
|
|
if (_messageReceivedCV.wait_for(lock, std::chrono::milliseconds(_messageTimeoutMilliSeconds)) == std::cv_status::timeout)
|
2016-04-18 08:29:17 -05:00
|
|
|
{
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("testAddDocNotify: Timed out waiting for admin console message");
|
2016-12-22 03:27:30 -06:00
|
|
|
return TestResult::TimedOut;
|
2016-07-13 14:16:16 -05:00
|
|
|
}
|
|
|
|
lock.unlock();
|
2016-04-18 08:29:17 -05:00
|
|
|
|
2016-07-13 14:16:16 -05:00
|
|
|
{
|
2020-02-28 03:50:58 -06:00
|
|
|
StringVector tokens(LOOLProtocol::tokenize(_messageReceived, ' '));
|
2019-11-13 00:48:30 -06:00
|
|
|
if (tokens.size() != 5 ||
|
2016-04-18 08:29:17 -05:00
|
|
|
tokens[0] != "adddoc" ||
|
2019-01-23 02:05:58 -06:00
|
|
|
tokens[2] != documentPath1.substr(documentPath1.find_last_of('/') + 1) )
|
2016-04-18 08:29:17 -05:00
|
|
|
{
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("testAddDocNotify: Unrecognized message format");
|
2016-12-22 03:27:30 -06:00
|
|
|
return TestResult::Failed;
|
2016-04-18 08:29:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// store document pid
|
|
|
|
_docPid1 = std::stoi(tokens[1]);
|
|
|
|
_usersCount++;
|
|
|
|
}
|
|
|
|
_docsCount++;
|
|
|
|
|
|
|
|
// Open another view of same document
|
2016-07-13 14:16:16 -05:00
|
|
|
lock.lock(); // lock _messageReceivedMutex
|
|
|
|
_messageReceived.clear();
|
2016-11-10 02:47:25 -06:00
|
|
|
_docWs2 = std::make_shared<LOOLWebSocket>(*session2, request1, response1);
|
2016-07-13 14:16:16 -05:00
|
|
|
_docWs2->sendFrame(loadMessage1.data(), loadMessage1.size());
|
|
|
|
if (_messageReceivedCV.wait_for(lock, std::chrono::milliseconds(_messageTimeoutMilliSeconds)) == std::cv_status::timeout)
|
2016-04-18 08:29:17 -05:00
|
|
|
{
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("testAddDocNotify: Timed out waiting for admin console message");
|
2016-12-22 03:27:30 -06:00
|
|
|
return TestResult::TimedOut;
|
2016-07-13 14:16:16 -05:00
|
|
|
}
|
|
|
|
lock.unlock();
|
2016-04-18 08:29:17 -05:00
|
|
|
|
2016-07-13 14:16:16 -05:00
|
|
|
{
|
2020-02-28 03:50:58 -06:00
|
|
|
StringVector tokens(LOOLProtocol::tokenize(_messageReceived, ' '));
|
2019-11-13 00:48:30 -06:00
|
|
|
if (tokens.size() != 5 ||
|
2016-04-18 08:29:17 -05:00
|
|
|
tokens[0] != "adddoc" ||
|
2019-01-23 02:05:58 -06:00
|
|
|
tokens[2] != documentPath1.substr(documentPath1.find_last_of('/') + 1) )
|
2016-04-18 08:29:17 -05:00
|
|
|
{
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("testAddDocNotify: Unrecognized message format");
|
2016-12-22 03:27:30 -06:00
|
|
|
return TestResult::Failed;
|
2016-04-18 08:29:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// store document pid
|
|
|
|
_docPid2 = std::stoi(tokens[1]);
|
|
|
|
_usersCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open another document (different)
|
2016-09-29 12:20:52 -05:00
|
|
|
std::string documentPath2, documentURL2;
|
2017-02-28 15:49:24 -06:00
|
|
|
helpers::getDocumentPathAndURL("insert-delete.odp", documentPath2, documentURL2, "unitAdmin-insert-delete.odp ");
|
2016-04-18 08:29:17 -05:00
|
|
|
HTTPRequest request2(HTTPRequest::HTTP_GET, documentURL2);
|
|
|
|
HTTPResponse response2;
|
2016-05-03 02:40:25 -05:00
|
|
|
const Poco::URI docUri2(helpers::getTestServerURI());
|
2016-04-18 08:29:17 -05:00
|
|
|
const std::string loadMessage2 = "load url=" + documentURL2;
|
2016-10-20 16:09:00 -05:00
|
|
|
std::unique_ptr<HTTPClientSession> session3(UnitHTTP::createSession());
|
2016-04-18 08:29:17 -05:00
|
|
|
|
2016-07-13 14:16:16 -05:00
|
|
|
lock.lock(); // lock _messageReceivedMutex
|
|
|
|
_messageReceived.clear();
|
2016-11-10 02:47:25 -06:00
|
|
|
_docWs3 = std::make_shared<LOOLWebSocket>(*session3, request2, response2);
|
2016-07-13 14:16:16 -05:00
|
|
|
_docWs3->sendFrame(loadMessage2.data(), loadMessage2.size());
|
|
|
|
if (_messageReceivedCV.wait_for(lock, std::chrono::milliseconds(_messageTimeoutMilliSeconds)) == std::cv_status::timeout)
|
2016-04-18 08:29:17 -05:00
|
|
|
{
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("testAddDocNotify: Timed out waiting for admin console message");
|
2016-12-22 03:27:30 -06:00
|
|
|
return TestResult::TimedOut;
|
2016-07-13 14:16:16 -05:00
|
|
|
}
|
|
|
|
lock.unlock();
|
2016-04-18 08:29:17 -05:00
|
|
|
|
2016-07-13 14:16:16 -05:00
|
|
|
{
|
2020-02-28 03:50:58 -06:00
|
|
|
StringVector tokens(LOOLProtocol::tokenize(_messageReceived, ' '));
|
2019-11-13 00:48:30 -06:00
|
|
|
if (tokens.size() != 5 ||
|
2016-04-18 08:29:17 -05:00
|
|
|
tokens[0] != "adddoc" ||
|
2019-01-23 02:05:58 -06:00
|
|
|
tokens[2] != documentPath2.substr(documentPath2.find_last_of('/') + 1) )
|
2016-04-18 08:29:17 -05:00
|
|
|
{
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("testAddDocNotify: Unrecognized message format");
|
2016-12-22 03:27:30 -06:00
|
|
|
return TestResult::Failed;
|
2016-04-18 08:29:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// store document pid
|
|
|
|
_docPid3 = std::stoi(tokens[1]);
|
|
|
|
_usersCount++;
|
|
|
|
}
|
|
|
|
_docsCount++;
|
|
|
|
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("testAddDocNotify: OK");
|
2016-12-22 03:27:30 -06:00
|
|
|
return TestResult::Ok;
|
2016-04-18 08:29:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
TestResult testUsersCount()
|
|
|
|
{
|
|
|
|
const std::string queryMessage = "active_users_count";
|
2016-07-13 14:37:00 -05:00
|
|
|
_messageReceived.clear();
|
2016-04-18 08:29:17 -05:00
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lock(_messageReceivedMutex);
|
2016-07-13 14:37:00 -05:00
|
|
|
_adminWs->sendFrame(queryMessage.data(), queryMessage.size());
|
|
|
|
if (_messageReceivedCV.wait_for(lock, std::chrono::milliseconds(_messageTimeoutMilliSeconds)) == std::cv_status::timeout)
|
2016-04-18 08:29:17 -05:00
|
|
|
{
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("testUsersCount: Timed out waiting for admin console message");
|
2016-12-22 03:27:30 -06:00
|
|
|
return TestResult::TimedOut;
|
2016-04-18 08:29:17 -05:00
|
|
|
}
|
|
|
|
lock.unlock();
|
|
|
|
|
2020-02-28 03:50:58 -06:00
|
|
|
StringVector tokens(LOOLProtocol::tokenize(_messageReceived, ' '));
|
2019-11-13 00:48:30 -06:00
|
|
|
if (tokens.size() != 2 ||
|
2016-06-01 04:08:24 -05:00
|
|
|
tokens[0] != "active_users_count")
|
2016-04-18 08:29:17 -05:00
|
|
|
{
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("testUsersCount: Unrecognized message format");
|
2016-12-22 03:27:30 -06:00
|
|
|
return TestResult::Failed;
|
2016-06-01 04:08:24 -05:00
|
|
|
}
|
|
|
|
else if (std::stoi(tokens[1]) != _usersCount)
|
|
|
|
{
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("testUsersCount: Incorrect users count "
|
2016-06-01 04:08:24 -05:00
|
|
|
", expected: " + std::to_string(_usersCount) +
|
|
|
|
", actual: " + tokens[1]);
|
2016-12-22 03:27:30 -06:00
|
|
|
return TestResult::Failed;
|
2016-04-18 08:29:17 -05:00
|
|
|
}
|
|
|
|
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("testUsersCount: OK");
|
2016-12-22 03:27:30 -06:00
|
|
|
return TestResult::Ok;
|
2016-04-18 08:29:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
TestResult testDocCount()
|
|
|
|
{
|
|
|
|
const std::string queryMessage = "active_docs_count";
|
2016-07-13 14:37:00 -05:00
|
|
|
_messageReceived.clear();
|
2016-04-18 08:29:17 -05:00
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lock(_messageReceivedMutex);
|
2016-07-13 14:37:00 -05:00
|
|
|
_adminWs->sendFrame(queryMessage.data(), queryMessage.size());
|
|
|
|
if (_messageReceivedCV.wait_for(lock, std::chrono::milliseconds(_messageTimeoutMilliSeconds)) == std::cv_status::timeout)
|
2016-04-18 08:29:17 -05:00
|
|
|
{
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("testDocCount: Timed out waiting for admin console message");
|
2016-12-22 03:27:30 -06:00
|
|
|
return TestResult::TimedOut;
|
2016-04-18 08:29:17 -05:00
|
|
|
}
|
|
|
|
lock.unlock();
|
|
|
|
|
2020-02-28 03:50:58 -06:00
|
|
|
StringVector tokens(LOOLProtocol::tokenize(_messageReceived, ' '));
|
2019-11-13 00:48:30 -06:00
|
|
|
if (tokens.size() != 2 ||
|
2016-04-18 08:29:17 -05:00
|
|
|
tokens[0] != "active_docs_count" ||
|
|
|
|
std::stoi(tokens[1]) != _docsCount)
|
|
|
|
{
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("testDocCount: Unrecognized message format");
|
2016-12-22 03:27:30 -06:00
|
|
|
return TestResult::Failed;
|
2016-06-01 04:08:24 -05:00
|
|
|
}
|
|
|
|
else if (std::stoi(tokens[1]) != _docsCount)
|
|
|
|
{
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("testDocCount: Incorrect doc count "
|
2016-06-01 04:08:24 -05:00
|
|
|
", expected: " + std::to_string(_docsCount) +
|
|
|
|
", actual: " + tokens[1]);
|
2016-12-22 03:27:30 -06:00
|
|
|
return TestResult::Failed;
|
2016-04-18 08:29:17 -05:00
|
|
|
}
|
|
|
|
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("testDocCount: OK");
|
2016-12-22 03:27:30 -06:00
|
|
|
return TestResult::Ok;
|
2016-04-18 08:29:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
TestResult testRmDocNotify()
|
|
|
|
{
|
|
|
|
// subscribe to rmdoc notification on admin websocket
|
|
|
|
const std::string subscribeMessage = "subscribe rmdoc";
|
|
|
|
_adminWs->sendFrame(subscribeMessage.data(), subscribeMessage.size());
|
2016-07-13 14:37:00 -05:00
|
|
|
_messageReceived.clear();
|
2016-04-18 08:29:17 -05:00
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lock(_messageReceivedMutex);
|
2016-07-13 14:37:00 -05:00
|
|
|
_docWs1->close();
|
|
|
|
if (_messageReceivedCV.wait_for(lock, std::chrono::milliseconds(_messageTimeoutMilliSeconds)) == std::cv_status::timeout)
|
2016-04-18 08:29:17 -05:00
|
|
|
{
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("testRmDocNotify: Timed out waiting for admin console message");
|
2016-12-22 03:27:30 -06:00
|
|
|
return TestResult::TimedOut;
|
2016-04-18 08:29:17 -05:00
|
|
|
}
|
|
|
|
lock.unlock();
|
|
|
|
|
2020-02-28 03:50:58 -06:00
|
|
|
StringVector tokens(LOOLProtocol::tokenize(_messageReceived, ' '));
|
2019-11-13 00:48:30 -06:00
|
|
|
if (tokens.size() != 3 ||
|
2016-04-18 08:29:17 -05:00
|
|
|
tokens[0] != "rmdoc" ||
|
|
|
|
stoi(tokens[1]) != _docPid1)
|
|
|
|
{
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("testRmDocNotify: Invalid message format");
|
2016-12-22 03:27:30 -06:00
|
|
|
return TestResult::Failed;
|
2016-04-18 08:29:17 -05:00
|
|
|
}
|
|
|
|
_usersCount--;
|
|
|
|
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("testRmDocNotify: OK");
|
2016-12-22 03:27:30 -06:00
|
|
|
return TestResult::Ok;
|
2016-04-18 08:29:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
UnitAdmin()
|
2016-05-03 02:40:25 -05:00
|
|
|
: _uri(helpers::getTestServerURI() + "/loleaflet/dist/admin/admin.html")
|
2016-04-18 08:29:17 -05:00
|
|
|
{
|
2016-06-01 11:28:07 -05:00
|
|
|
setTimeout(60 * 1000);
|
|
|
|
|
2016-04-18 08:29:17 -05:00
|
|
|
// Register tests here.
|
|
|
|
_tests.push_back(&UnitAdmin::testIncorrectPassword);
|
2016-09-27 11:08:34 -05:00
|
|
|
#if ENABLE_SSL
|
2016-04-18 08:29:17 -05:00
|
|
|
_tests.push_back(&UnitAdmin::testCorrectPassword);
|
2016-09-27 11:08:34 -05:00
|
|
|
#endif
|
2016-07-28 07:28:40 -05:00
|
|
|
_tests.push_back(&UnitAdmin::testWebSocketWithoutAuthToken);
|
2016-09-27 11:08:34 -05:00
|
|
|
#if ENABLE_SSL
|
2016-07-28 07:28:40 -05:00
|
|
|
_tests.push_back(&UnitAdmin::testWebSocketWithIncorrectAuthToken);
|
2016-04-18 08:29:17 -05:00
|
|
|
_tests.push_back(&UnitAdmin::testAddDocNotify);
|
|
|
|
_tests.push_back(&UnitAdmin::testUsersCount);
|
|
|
|
_tests.push_back(&UnitAdmin::testDocCount);
|
2016-10-17 08:00:27 -05:00
|
|
|
_tests.push_back(&UnitAdmin::testRmDocNotify);
|
2016-04-18 08:29:17 -05:00
|
|
|
_tests.push_back(&UnitAdmin::testUsersCount);
|
|
|
|
_tests.push_back(&UnitAdmin::testDocCount);
|
2016-09-27 11:08:34 -05:00
|
|
|
#endif
|
2016-04-18 08:29:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Runs tests sequentially in _tests
|
|
|
|
virtual void invokeTest()
|
|
|
|
{
|
|
|
|
if (!_isTestRunning)
|
|
|
|
{
|
|
|
|
_isTestRunning = true;
|
|
|
|
AdminTest test = _tests[_testCounter++];
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("UnitAdmin:: Starting test #" << _testCounter);
|
2016-04-18 08:29:17 -05:00
|
|
|
TestResult res = ((*this).*(test))();
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("UnitAdmin:: Finished test #" << _testCounter);
|
2016-12-22 03:27:30 -06:00
|
|
|
if (res != TestResult::Ok)
|
2016-04-18 08:29:17 -05:00
|
|
|
{
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("Exiting with " <<
|
|
|
|
(res == TestResult::Failed
|
|
|
|
? "FAIL"
|
|
|
|
: (res == TestResult::TimedOut)
|
|
|
|
? "TIMEOUT"
|
|
|
|
: "??? (" + std::to_string((int)res) + ")"));
|
2016-04-18 08:29:17 -05:00
|
|
|
exitTest(res);
|
2016-10-22 11:09:39 -05:00
|
|
|
assert(false);
|
2016-04-18 08:29:17 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// End this when all tests are finished
|
|
|
|
if (_tests.size() == _testCounter)
|
2016-10-17 05:40:44 -05:00
|
|
|
{
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("Exiting with OK");
|
2016-12-22 03:27:30 -06:00
|
|
|
exitTest(TestResult::Ok);
|
2016-10-17 05:40:44 -05:00
|
|
|
}
|
2016-04-18 08:29:17 -05:00
|
|
|
|
|
|
|
_isTestRunning = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onAdminNotifyMessage(const std::string& message)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_messageReceivedMutex);
|
|
|
|
_messageReceivedCV.notify_all();
|
|
|
|
_messageReceived = message;
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("UnitAdmin:: onAdminNotifyMessage: " << message);
|
2016-04-18 08:29:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onAdminQueryMessage(const std::string& message)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_messageReceivedMutex);
|
|
|
|
_messageReceivedCV.notify_all();
|
|
|
|
_messageReceived = message;
|
2018-01-14 19:39:06 -06:00
|
|
|
LOG_INF("UnitAdmin:: onAdminQueryMessage: " << message);
|
2016-04-18 08:29:17 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
UnitBase *unit_create_wsd(void)
|
|
|
|
{
|
|
|
|
return new UnitAdmin();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|