2017-08-03 16:05:48 -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/.
|
|
|
|
*/
|
|
|
|
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <config.h>
|
2017-08-03 16:05:48 -05:00
|
|
|
|
2023-05-30 17:30:21 -05:00
|
|
|
#include <HttpRequest.hpp>
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <WopiTestServer.hpp>
|
|
|
|
#include <Log.hpp>
|
|
|
|
#include <Unit.hpp>
|
|
|
|
#include <UnitHTTP.hpp>
|
|
|
|
#include <helpers.hpp>
|
2023-05-30 17:30:21 -05:00
|
|
|
#include <Util.hpp>
|
|
|
|
|
2017-08-03 16:05:48 -05:00
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
#include <Poco/Net/OAuth20Credentials.h>
|
2017-08-09 15:02:29 -05:00
|
|
|
#include <Poco/Util/LayeredConfiguration.h>
|
2017-08-03 16:05:48 -05:00
|
|
|
|
|
|
|
using Poco::Net::OAuth20Credentials;
|
|
|
|
|
2017-09-26 09:12:58 -05:00
|
|
|
class UnitOAuth : public WopiTestServer
|
2017-08-03 16:05:48 -05:00
|
|
|
{
|
2022-05-05 04:55:14 -05:00
|
|
|
STATE_ENUM(Phase,
|
2022-05-05 05:39:01 -05:00
|
|
|
Start, // Initial phase
|
2022-05-05 04:55:14 -05:00
|
|
|
LoadToken, // loading the document with Bearer token
|
2022-05-05 17:17:38 -05:00
|
|
|
ModifyAccessToken, // Modify the access-token after loading
|
|
|
|
LoadHeader, // load the document with Basic auth
|
|
|
|
LoadingHeader, // loading the document with Basic auth
|
2022-05-05 05:39:01 -05:00
|
|
|
Done // just wait for the results
|
|
|
|
)
|
2022-05-05 04:55:14 -05:00
|
|
|
_phase;
|
2017-08-09 15:02:29 -05:00
|
|
|
|
2022-05-05 17:17:38 -05:00
|
|
|
std::string _credential;
|
2022-05-05 05:39:01 -05:00
|
|
|
bool _checkFileInfoCalled;
|
|
|
|
bool _getFileCalled;
|
2017-08-17 04:47:14 -05:00
|
|
|
|
2017-08-03 16:05:48 -05:00
|
|
|
public:
|
2020-12-20 21:11:33 -06:00
|
|
|
UnitOAuth()
|
|
|
|
: WopiTestServer("UnitOAuth")
|
2022-05-05 05:39:01 -05:00
|
|
|
, _phase(Phase::Start)
|
|
|
|
, _checkFileInfoCalled(false)
|
|
|
|
, _getFileCalled(false)
|
2017-08-03 16:05:48 -05:00
|
|
|
{
|
2017-08-09 15:02:29 -05:00
|
|
|
}
|
2017-08-03 16:05:48 -05:00
|
|
|
|
2017-09-26 09:12:58 -05:00
|
|
|
/// The actual assert of the authentication.
|
2022-05-05 05:39:01 -05:00
|
|
|
void assertRequest(const Poco::Net::HTTPRequest& request)
|
2017-08-17 04:47:14 -05:00
|
|
|
{
|
|
|
|
// check that the request contains the Authorization: header
|
2022-05-05 04:55:14 -05:00
|
|
|
try
|
|
|
|
{
|
2022-05-05 17:17:38 -05:00
|
|
|
if (_phase == Phase::LoadToken || _phase == Phase::ModifyAccessToken)
|
2017-08-17 04:47:14 -05:00
|
|
|
{
|
|
|
|
OAuth20Credentials creds(request);
|
2022-05-05 17:17:38 -05:00
|
|
|
LOK_ASSERT_EQUAL(_credential, creds.getBearerToken());
|
2017-08-17 04:47:14 -05:00
|
|
|
}
|
2022-05-05 17:17:38 -05:00
|
|
|
else if (_phase == Phase::LoadingHeader)
|
2017-08-17 04:47:14 -05:00
|
|
|
{
|
|
|
|
OAuth20Credentials creds(request, "Basic");
|
2022-05-05 17:17:38 -05:00
|
|
|
LOK_ASSERT_EQUAL(_credential, creds.getBearerToken());
|
2017-08-17 04:47:14 -05:00
|
|
|
}
|
2022-05-05 05:39:01 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
LOK_ASSERT_FAIL("Unexpected phase: " + toString(_phase));
|
|
|
|
}
|
2017-08-17 04:47:14 -05:00
|
|
|
}
|
2022-05-05 04:55:14 -05:00
|
|
|
catch (const std::exception& ex)
|
2017-08-17 04:47:14 -05:00
|
|
|
{
|
|
|
|
// fail as fast as possible
|
2022-05-05 04:55:14 -05:00
|
|
|
failTest(std::string("Exception: ") + ex.what());
|
2017-08-17 04:47:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 06:35:15 -05:00
|
|
|
virtual std::unique_ptr<http::Response>
|
2023-06-21 07:14:06 -05:00
|
|
|
assertCheckFileInfoRequest(const Poco::Net::HTTPRequest& request) override
|
2017-08-09 15:02:29 -05:00
|
|
|
{
|
2022-05-05 05:39:01 -05:00
|
|
|
_checkFileInfoCalled = true;
|
|
|
|
assertRequest(request);
|
2023-05-31 06:35:15 -05:00
|
|
|
|
|
|
|
return nullptr; // Success.
|
2017-09-26 09:12:58 -05:00
|
|
|
}
|
2017-08-03 16:05:48 -05:00
|
|
|
|
2023-05-30 17:30:21 -05:00
|
|
|
std::unique_ptr<http::Response>
|
|
|
|
assertGetFileRequest(const Poco::Net::HTTPRequest& request) override
|
2017-09-26 09:12:58 -05:00
|
|
|
{
|
2022-05-05 05:39:01 -05:00
|
|
|
_getFileCalled = true;
|
|
|
|
assertRequest(request);
|
2023-05-30 17:30:21 -05:00
|
|
|
|
|
|
|
return nullptr; // Success.
|
2022-05-05 05:39:01 -05:00
|
|
|
}
|
|
|
|
|
2022-05-05 17:17:38 -05:00
|
|
|
std::unique_ptr<http::Response>
|
|
|
|
assertPutFileRequest(const Poco::Net::HTTPRequest& request) override
|
|
|
|
{
|
|
|
|
LOK_ASSERT_STATE(_phase, Phase::ModifyAccessToken);
|
|
|
|
assertRequest(request);
|
|
|
|
|
|
|
|
TRANSITION_STATE(_phase, Phase::LoadHeader);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-05-05 05:39:01 -05:00
|
|
|
bool onDocumentLoaded(const std::string& message) override
|
|
|
|
{
|
|
|
|
LOG_TST("Doc (" << name(_phase) << "): [" << message << ']');
|
|
|
|
|
|
|
|
LOK_ASSERT_EQUAL_MESSAGE("CheckFileInfo was not invoked", true, _checkFileInfoCalled);
|
|
|
|
_checkFileInfoCalled = false;
|
|
|
|
LOK_ASSERT_EQUAL_MESSAGE("GetFile was not invoked", true, _getFileCalled);
|
|
|
|
_getFileCalled = false;
|
|
|
|
|
|
|
|
switch (_phase)
|
2017-08-03 16:05:48 -05:00
|
|
|
{
|
2022-05-05 05:39:01 -05:00
|
|
|
case Phase::LoadToken:
|
2022-05-05 17:17:38 -05:00
|
|
|
TRANSITION_STATE(_phase, Phase::ModifyAccessToken);
|
|
|
|
_credential = "abcdefg123456_newtoken";
|
|
|
|
WSD_CMD("resetaccesstoken " + _credential);
|
|
|
|
WSD_CMD("save dontTerminateEdit=0 dontSaveIfUnmodified=0");
|
2023-01-25 19:04:39 -06:00
|
|
|
|
|
|
|
// Close the document after loading.
|
|
|
|
WSD_CMD("closedocument");
|
2022-05-05 17:17:38 -05:00
|
|
|
break;
|
|
|
|
case Phase::ModifyAccessToken:
|
2023-01-25 19:04:39 -06:00
|
|
|
// Close the document after loading.
|
|
|
|
WSD_CMD("closedocument");
|
2022-05-05 05:39:01 -05:00
|
|
|
break;
|
|
|
|
case Phase::LoadHeader:
|
2023-01-25 19:04:39 -06:00
|
|
|
// Close the document after loading.
|
|
|
|
WSD_CMD("closedocument");
|
2022-05-05 17:17:38 -05:00
|
|
|
TRANSITION_STATE(_phase, Phase::LoadingHeader);
|
|
|
|
_credential = "basic==";
|
|
|
|
initWebsocket("/wopi/files/1?access_header=Authorization: Basic " + _credential);
|
|
|
|
WSD_CMD("load url=" + getWopiSrc());
|
|
|
|
break;
|
|
|
|
case Phase::LoadingHeader:
|
2023-01-25 19:04:39 -06:00
|
|
|
// Close the document after loading.
|
|
|
|
WSD_CMD("closedocument");
|
2022-05-05 05:39:01 -05:00
|
|
|
passTest("Finished all cases successfully");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOK_ASSERT_FAIL("Unexpected phase: " + toString(_phase));
|
2017-08-09 15:02:29 -05:00
|
|
|
}
|
2017-09-27 13:13:16 -05:00
|
|
|
|
2022-05-05 05:39:01 -05:00
|
|
|
return true;
|
2017-09-26 09:12:58 -05:00
|
|
|
}
|
2017-08-09 15:02:29 -05:00
|
|
|
|
2020-12-24 11:15:37 -06:00
|
|
|
void invokeWSDTest() override
|
2017-08-03 16:05:48 -05:00
|
|
|
{
|
2017-08-09 15:02:29 -05:00
|
|
|
switch (_phase)
|
|
|
|
{
|
2022-05-05 05:39:01 -05:00
|
|
|
case Phase::Start:
|
2017-08-09 15:02:29 -05:00
|
|
|
{
|
2022-05-05 05:39:01 -05:00
|
|
|
TRANSITION_STATE(_phase, Phase::LoadToken);
|
2022-05-05 17:17:38 -05:00
|
|
|
_credential = "s3hn3ct0k3v";
|
|
|
|
initWebsocket("/wopi/files/0?access_token=" + _credential);
|
2017-08-09 15:02:29 -05:00
|
|
|
|
2022-05-05 04:55:14 -05:00
|
|
|
WSD_CMD("load url=" + getWopiSrc());
|
2017-08-09 15:02:29 -05:00
|
|
|
}
|
2022-05-05 05:39:01 -05:00
|
|
|
break;
|
|
|
|
case Phase::LoadToken:
|
2022-05-05 17:17:38 -05:00
|
|
|
case Phase::ModifyAccessToken:
|
|
|
|
break;
|
2022-05-05 05:39:01 -05:00
|
|
|
case Phase::LoadHeader:
|
2022-05-05 17:17:38 -05:00
|
|
|
{
|
|
|
|
TRANSITION_STATE(_phase, Phase::LoadingHeader);
|
|
|
|
_credential = "basic==";
|
|
|
|
initWebsocket("/wopi/files/1?access_header=Authorization: Basic " + _credential);
|
|
|
|
WSD_CMD("load url=" + getWopiSrc());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Phase::LoadingHeader:
|
2022-05-05 05:39:01 -05:00
|
|
|
case Phase::Done:
|
2017-08-09 15:02:29 -05:00
|
|
|
{
|
2017-09-27 13:13:16 -05:00
|
|
|
// just wait for the results
|
2017-08-09 15:02:29 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-08-03 16:05:48 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-05 04:55:14 -05:00
|
|
|
UnitBase* unit_create_wsd(void) { return new UnitOAuth(); }
|
2017-08-03 16:05:48 -05:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|