2021-11-11 03:17:43 -06: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/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2022-02-01 10:17:29 -06:00
|
|
|
#include "lokassert.hpp"
|
2021-11-11 03:17:43 -06:00
|
|
|
#include <WopiTestServer.hpp>
|
|
|
|
#include <Log.hpp>
|
|
|
|
#include <helpers.hpp>
|
2022-04-04 21:36:05 -05:00
|
|
|
#include <wsd/ClientSession.hpp>
|
|
|
|
|
2021-11-11 03:17:43 -06:00
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
|
2022-11-13 11:51:41 -06:00
|
|
|
/// This is to test that we unlock before unloading the last editor.
|
2021-11-11 03:17:43 -06:00
|
|
|
class UnitWopiLock : public WopiTestServer
|
|
|
|
{
|
2022-04-01 20:04:41 -05:00
|
|
|
STATE_ENUM(Phase, Load, LockDocument, UnlockDocument, Done) _phase;
|
2021-11-11 03:17:43 -06:00
|
|
|
|
|
|
|
std::string _lockState;
|
2022-11-13 11:51:41 -06:00
|
|
|
std::string _lockToken;
|
|
|
|
std::size_t _checkFileInfoCount;
|
|
|
|
std::size_t _viewCount;
|
2021-11-11 03:17:43 -06:00
|
|
|
|
|
|
|
public:
|
|
|
|
UnitWopiLock()
|
|
|
|
: WopiTestServer("UnitWopiLock")
|
|
|
|
, _phase(Phase::Load)
|
|
|
|
, _lockState("UNLOCK")
|
2022-11-13 11:51:41 -06:00
|
|
|
, _checkFileInfoCount(0)
|
|
|
|
, _viewCount(0)
|
2021-11-11 03:17:43 -06:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-11-02 06:11:03 -05:00
|
|
|
void configCheckFileInfo(Poco::JSON::Object::Ptr fileInfo) override
|
2021-11-11 03:17:43 -06:00
|
|
|
{
|
2022-11-13 11:51:41 -06:00
|
|
|
// Make the first session the editor, subsequent ones read-only.
|
|
|
|
const bool firstView = _checkFileInfoCount == 0;
|
|
|
|
++_checkFileInfoCount;
|
|
|
|
|
|
|
|
LOG_TST("CheckFileInfo: " << (firstView ? "editor" : "viewer"));
|
2021-11-11 03:17:43 -06:00
|
|
|
|
2022-11-02 06:11:03 -05:00
|
|
|
fileInfo->set("SupportsLocks", "true");
|
2022-11-13 11:51:41 -06:00
|
|
|
fileInfo->set("UserCanWrite", firstView ? "true" : "false");
|
2021-11-11 03:17:43 -06:00
|
|
|
|
2022-11-13 11:51:41 -06:00
|
|
|
// An extension that doesn't allow commenting. By omitting this,
|
|
|
|
// the test fails because we allow commenting and consider the
|
|
|
|
// document editable, and don't unlock when the editor disconnects.
|
|
|
|
fileInfo->set("BaseFileName", "doc.odt");
|
2021-11-11 03:17:43 -06:00
|
|
|
}
|
|
|
|
|
2023-02-19 09:42:34 -06:00
|
|
|
std::unique_ptr<http::Response>
|
|
|
|
assertLockRequest(const Poco::Net::HTTPRequest& request) override
|
2021-11-11 03:17:43 -06:00
|
|
|
{
|
2022-02-01 10:17:29 -06:00
|
|
|
const std::string lock = request.get("X-WOPI-Lock", std::string());
|
|
|
|
const std::string newLockState = request.get("X-WOPI-Override", std::string());
|
|
|
|
LOG_TST("In " << toString(_phase) << ", X-WOPI-Lock: " << lock << ", X-WOPI-Override: "
|
|
|
|
<< newLockState << ", for URI: " << request.getURI());
|
2022-01-13 02:44:43 -06:00
|
|
|
|
2022-02-01 10:17:29 -06:00
|
|
|
if (_phase == Phase::LockDocument)
|
2021-11-11 03:17:43 -06:00
|
|
|
{
|
2022-02-01 10:17:29 -06:00
|
|
|
LOK_ASSERT_EQUAL_MESSAGE("Expected X-WOPI-Override:LOCK", std::string("LOCK"),
|
|
|
|
newLockState);
|
2022-11-13 11:51:41 -06:00
|
|
|
LOK_ASSERT_MESSAGE("Lock token cannot be empty", !lock.empty());
|
2021-11-11 03:17:43 -06:00
|
|
|
_lockState = newLockState;
|
2022-11-13 11:51:41 -06:00
|
|
|
_lockToken = lock;
|
2021-11-11 03:17:43 -06:00
|
|
|
}
|
2022-02-01 10:17:29 -06:00
|
|
|
else if (_phase == Phase::UnlockDocument)
|
2021-11-11 03:17:43 -06:00
|
|
|
{
|
2022-02-01 10:17:29 -06:00
|
|
|
LOK_ASSERT_EQUAL_MESSAGE("Expected X-WOPI-Override:UNLOCK", std::string("UNLOCK"),
|
|
|
|
newLockState);
|
2022-11-13 11:51:41 -06:00
|
|
|
LOK_ASSERT_EQUAL_MESSAGE("Document is not unlocked", std::string("LOCK"), _lockState);
|
|
|
|
LOK_ASSERT_EQUAL_MESSAGE("The lock token has changed", _lockToken, lock);
|
|
|
|
|
2022-02-01 10:17:29 -06:00
|
|
|
TRANSITION_STATE(_phase, Phase::Done);
|
2021-11-11 03:17:43 -06:00
|
|
|
exitTest(TestResult::Ok);
|
|
|
|
}
|
2022-02-01 10:17:29 -06:00
|
|
|
else
|
|
|
|
{
|
|
|
|
LOK_ASSERT_FAIL("Unexpected lock-state change while in " + toString(_phase));
|
|
|
|
}
|
2023-02-19 09:42:34 -06:00
|
|
|
|
|
|
|
return nullptr; // Success.
|
2021-11-11 03:17:43 -06:00
|
|
|
}
|
|
|
|
|
2022-04-04 21:36:05 -05:00
|
|
|
void onDocBrokerViewLoaded(const std::string&,
|
|
|
|
const std::shared_ptr<ClientSession>& session) override
|
|
|
|
{
|
2022-11-13 11:51:41 -06:00
|
|
|
LOG_TST("View #" << _viewCount + 1 << " [" << session->getName() << "] loaded");
|
2022-04-04 21:36:05 -05:00
|
|
|
|
2022-11-13 11:51:41 -06:00
|
|
|
++_viewCount;
|
|
|
|
if (_viewCount == 2)
|
2022-04-04 21:36:05 -05:00
|
|
|
{
|
|
|
|
// Transition before disconnecting.
|
|
|
|
TRANSITION_STATE(_phase, Phase::UnlockDocument);
|
|
|
|
|
|
|
|
// force kill the session with edit permission
|
|
|
|
LOG_TST("Disconnecting first connection with edit permission");
|
|
|
|
deleteSocketAt(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-11 03:17:43 -06:00
|
|
|
void invokeWSDTest() override
|
|
|
|
{
|
|
|
|
switch (_phase)
|
|
|
|
{
|
|
|
|
case Phase::Load:
|
|
|
|
{
|
2022-02-01 10:17:29 -06:00
|
|
|
// Always transition before issuing commands.
|
|
|
|
TRANSITION_STATE(_phase, Phase::LockDocument);
|
|
|
|
|
2022-02-01 07:33:25 -06:00
|
|
|
LOG_TST("Creating first connection");
|
2021-11-11 03:17:43 -06:00
|
|
|
initWebsocket("/wopi/files/0?access_token=anything");
|
2022-02-19 16:36:38 -06:00
|
|
|
|
2022-02-01 07:33:25 -06:00
|
|
|
LOG_TST("Creating second connection");
|
2021-11-11 03:17:43 -06:00
|
|
|
addWebSocket();
|
2022-02-19 16:36:38 -06:00
|
|
|
|
2022-02-01 07:33:25 -06:00
|
|
|
LOG_TST("Loading first view (editor)");
|
2022-02-19 16:36:38 -06:00
|
|
|
WSD_CMD_BY_CONNECTION_INDEX(0, "load url=" + getWopiSrc());
|
2022-02-01 07:33:25 -06:00
|
|
|
LOG_TST("Loading second view (viewer)");
|
2022-02-19 16:36:38 -06:00
|
|
|
WSD_CMD_BY_CONNECTION_INDEX(1, "load url=" + getWopiSrc());
|
2021-11-11 03:17:43 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Phase::LockDocument:
|
|
|
|
case Phase::UnlockDocument:
|
|
|
|
break;
|
2022-02-01 10:17:29 -06:00
|
|
|
case Phase::Done:
|
2021-11-11 03:17:43 -06:00
|
|
|
{
|
|
|
|
// just wait for the results
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-11-13 11:51:41 -06:00
|
|
|
UnitBase* unit_create_wsd(void) { return new UnitWopiLock(); }
|
2021-11-11 03:17:43 -06:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|