2022-02-08 23:10:25 -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>
|
|
|
|
|
|
|
|
#include "WOPIUploadConflictCommon.hpp"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
|
|
|
|
#include "Util.hpp"
|
|
|
|
#include "Log.hpp"
|
|
|
|
#include "UnitHTTP.hpp"
|
|
|
|
#include "helpers.hpp"
|
|
|
|
#include "lokassert.hpp"
|
|
|
|
|
|
|
|
/// This test simulates a permanently-failing upload.
|
|
|
|
class UnitWOPIFailUpload : public WOPIUploadConflictCommon
|
|
|
|
{
|
2022-02-19 18:57:47 -06:00
|
|
|
using Base = WOPIUploadConflictCommon;
|
2022-02-08 23:10:25 -06:00
|
|
|
|
2022-02-19 18:57:47 -06:00
|
|
|
using Base::Phase;
|
|
|
|
using Base::Scenario;
|
|
|
|
|
|
|
|
using Base::OriginalDocContent;
|
2022-02-08 23:10:25 -06:00
|
|
|
|
|
|
|
bool _unloadingModifiedDocDetected;
|
|
|
|
|
2022-02-26 15:55:04 -06:00
|
|
|
static constexpr std::size_t LimitStoreFailures = 2;
|
2022-04-02 18:22:00 -05:00
|
|
|
static constexpr bool SaveOnExit = true;
|
2022-02-26 15:55:04 -06:00
|
|
|
|
2022-02-08 23:10:25 -06:00
|
|
|
public:
|
|
|
|
UnitWOPIFailUpload()
|
2022-02-19 18:57:47 -06:00
|
|
|
: Base("UnitWOPIFailUpload", OriginalDocContent)
|
2022-02-08 23:10:25 -06:00
|
|
|
, _unloadingModifiedDocDetected(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void configure(Poco::Util::LayeredConfiguration& config) override
|
|
|
|
{
|
2022-02-19 18:57:47 -06:00
|
|
|
Base::configure(config);
|
2022-02-08 23:10:25 -06:00
|
|
|
|
|
|
|
// Small value to shorten the test run time.
|
2022-02-26 15:55:04 -06:00
|
|
|
config.setUInt("per_document.limit_store_failures", LimitStoreFailures);
|
2022-04-02 18:22:00 -05:00
|
|
|
config.setBool("per_document.always_save_on_exit", SaveOnExit);
|
2022-02-08 23:10:25 -06:00
|
|
|
}
|
|
|
|
|
2022-02-26 15:55:04 -06:00
|
|
|
void onDocBrokerCreate(const std::string& docKey) override
|
|
|
|
{
|
|
|
|
Base::onDocBrokerCreate(docKey);
|
|
|
|
|
|
|
|
if (_scenario == Scenario::VerifyOverwrite)
|
|
|
|
{
|
|
|
|
// By default, we don't upload when verifying (unless always_save_on_exit is set).
|
2022-04-02 18:22:00 -05:00
|
|
|
//FIXME: we exit too soon without considering always_save_on_exit.
|
|
|
|
setExpectedPutFile(/*SaveOnExit*/ 0);
|
2022-03-01 20:49:11 -06:00
|
|
|
}
|
2022-02-26 15:55:04 -06:00
|
|
|
else
|
|
|
|
{
|
2022-04-02 18:22:00 -05:00
|
|
|
// With always_save_on_exit=true and limit_store_failures=LimitStoreFailures,
|
|
|
|
// we expect exactly two PutFile requests per document.
|
2022-02-26 15:55:04 -06:00
|
|
|
setExpectedPutFile(LimitStoreFailures);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-08 23:10:25 -06:00
|
|
|
void assertGetFileRequest(const Poco::Net::HTTPRequest& /*request*/) override
|
|
|
|
{
|
|
|
|
LOG_TST("Testing " << toString(_scenario));
|
|
|
|
LOK_ASSERT_STATE(_phase, Phase::WaitLoadStatus);
|
|
|
|
|
2022-02-16 19:07:23 -06:00
|
|
|
assertGetFileCount();
|
|
|
|
|
2022-02-08 23:10:25 -06:00
|
|
|
//FIXME: check that unloading modified documents trigger test failure.
|
|
|
|
// LOK_ASSERT_EQUAL_MESSAGE("Expected modified document detection to have triggered", true,
|
|
|
|
// _unloadingModifiedDocDetected);
|
2022-02-16 19:07:23 -06:00
|
|
|
_unloadingModifiedDocDetected = false; // Reset.
|
2022-02-08 23:10:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<http::Response>
|
2022-02-23 21:33:53 -06:00
|
|
|
assertPutFileRequest(const Poco::Net::HTTPRequest& request) override
|
2022-02-08 23:10:25 -06:00
|
|
|
{
|
|
|
|
LOG_TST("Testing " << toString(_scenario));
|
|
|
|
LOK_ASSERT_STATE(_phase, Phase::WaitDocClose);
|
|
|
|
|
2022-02-16 19:07:23 -06:00
|
|
|
assertPutFileCount();
|
|
|
|
|
2022-02-23 21:33:53 -06:00
|
|
|
const std::string wopiTimestamp = request.get("X-COOL-WOPI-Timestamp", std::string());
|
|
|
|
const bool force = wopiTimestamp.empty(); // Without a timestamp we force to always store.
|
|
|
|
|
2022-02-08 23:10:25 -06:00
|
|
|
switch (_scenario)
|
|
|
|
{
|
2022-04-02 18:22:00 -05:00
|
|
|
case Scenario::Disconnect:
|
|
|
|
// When we disconnect, we unload the document. So SaveOnExit kicks in.
|
|
|
|
LOK_ASSERT_EQUAL_MESSAGE("Unexpected overwritting the document in storage",
|
|
|
|
SaveOnExit, force);
|
|
|
|
break;
|
|
|
|
case Scenario::CloseDiscard:
|
2022-02-08 23:10:25 -06:00
|
|
|
case Scenario::SaveDiscard:
|
2022-04-02 18:22:00 -05:00
|
|
|
break;
|
2022-02-23 21:33:53 -06:00
|
|
|
case Scenario::SaveOverwrite:
|
2022-04-02 18:22:00 -05:00
|
|
|
case Scenario::VerifyOverwrite:
|
2022-02-23 21:33:53 -06:00
|
|
|
if (getCountPutFile() < getExpectedPutFile())
|
|
|
|
{
|
|
|
|
// These are regular saves.
|
2022-04-02 18:22:00 -05:00
|
|
|
LOK_ASSERT_EQUAL_MESSAGE("Unexpected overwritting the document in storage",
|
|
|
|
false, force);
|
2022-02-23 21:33:53 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// The last one is the always_save_on_exit, and has to be forced.
|
|
|
|
LOK_ASSERT_EQUAL_MESSAGE("Expected forced overwritting the document in storage",
|
|
|
|
true, force);
|
|
|
|
}
|
|
|
|
break;
|
2022-02-08 23:10:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Internal Server Error.
|
|
|
|
return Util::make_unique<http::Response>(http::StatusLine(500));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onDocumentModified(const std::string& message) override
|
|
|
|
{
|
|
|
|
LOG_TST("Testing " << toString(_scenario) << ": [" << message << ']');
|
|
|
|
LOK_ASSERT_STATE(_phase, Phase::WaitModifiedStatus);
|
|
|
|
|
|
|
|
TRANSITION_STATE(_phase, Phase::WaitDocClose);
|
|
|
|
|
|
|
|
switch (_scenario)
|
|
|
|
{
|
|
|
|
case Scenario::Disconnect:
|
2022-02-23 21:33:53 -06:00
|
|
|
// Just disconnect.
|
2022-02-08 23:10:25 -06:00
|
|
|
LOG_TST("Disconnecting");
|
|
|
|
deleteSocketAt(0);
|
|
|
|
break;
|
|
|
|
case Scenario::SaveDiscard:
|
|
|
|
case Scenario::SaveOverwrite:
|
2022-02-23 21:33:53 -06:00
|
|
|
// Save the document.
|
2022-02-08 23:10:25 -06:00
|
|
|
LOG_TST("Saving the document");
|
|
|
|
WSD_CMD("save dontTerminateEdit=0 dontSaveIfUnmodified=0");
|
|
|
|
break;
|
|
|
|
case Scenario::CloseDiscard:
|
2022-02-23 21:33:53 -06:00
|
|
|
// Close the document.
|
2022-02-08 23:10:25 -06:00
|
|
|
LOG_TST("Closing the document");
|
|
|
|
WSD_CMD("closedocument");
|
|
|
|
break;
|
|
|
|
case Scenario::VerifyOverwrite:
|
|
|
|
LOK_ASSERT_FAIL("Unexpected modification in " + toString(_scenario));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onDocumentError(const std::string& message) override
|
|
|
|
{
|
|
|
|
LOG_TST("Testing " << toString(_scenario) << ": [" << message << ']');
|
|
|
|
LOK_ASSERT_STATE(_phase, Phase::WaitDocClose);
|
|
|
|
|
2022-02-23 21:33:53 -06:00
|
|
|
LOK_ASSERT_EQUAL_MESSAGE("Expect only documentconflict errors",
|
|
|
|
std::string("error: cmd=storage kind=savefailed"), message);
|
2022-02-08 23:10:25 -06:00
|
|
|
|
2022-02-23 21:33:53 -06:00
|
|
|
// Close the document.
|
|
|
|
LOG_TST("Closing the document");
|
|
|
|
WSD_CMD("closedocument");
|
2022-02-08 23:10:25 -06:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called when we have modified document data at exit.
|
|
|
|
void fail(const std::string& reason) override
|
|
|
|
{
|
|
|
|
LOG_TST("Modified document being unloaded: " << reason);
|
|
|
|
|
|
|
|
// We expect this to happen only with the disonnection test,
|
|
|
|
// because only in that case there is no user input.
|
2022-04-03 19:11:00 -05:00
|
|
|
LOK_ASSERT_MESSAGE("Expected reason to be 'Data-loss detected'",
|
|
|
|
Util::startsWith(reason, "Data-loss detected"));
|
2022-02-08 23:10:25 -06:00
|
|
|
LOK_ASSERT_MESSAGE("Expected to be in Phase::WaitDocClose but was " + toString(_phase),
|
|
|
|
_phase == Phase::WaitDocClose);
|
|
|
|
_unloadingModifiedDocDetected = true;
|
|
|
|
}
|
2022-02-26 15:55:04 -06:00
|
|
|
|
2022-02-23 21:33:53 -06:00
|
|
|
// Wait for clean unloading.
|
2022-02-26 15:55:04 -06:00
|
|
|
void onDocBrokerDestroy(const std::string& docKey) override
|
|
|
|
{
|
|
|
|
LOG_TST("Testing " << toString(_scenario) << " with dockey [" << docKey << "] closed.");
|
|
|
|
LOK_ASSERT_STATE(_phase, Phase::WaitDocClose);
|
|
|
|
|
2022-03-01 20:49:11 -06:00
|
|
|
// Uploading fails and we can't have anything but the original.
|
|
|
|
LOK_ASSERT_EQUAL_MESSAGE("Unexpected contents in storage", std::string(OriginalDocContent),
|
2022-02-26 15:55:04 -06:00
|
|
|
getFileContent());
|
|
|
|
|
|
|
|
Base::onDocBrokerDestroy(docKey);
|
|
|
|
}
|
2022-02-08 23:10:25 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
UnitBase* unit_create_wsd(void) { return new UnitWOPIFailUpload(); }
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|