test/Makefile.am | 7 +- test/UnitOAuth.cpp | 13 ++-- test/UnitWOPI.cpp | 129 ++++++++++++++++++++++++++++++++++++++++++++++++ test/WopiTestServer.hpp | 22 ++++++-- 4 files changed, 159 insertions(+), 12 deletions(-)
New commits: commit ee4a934179e2320e5d35ef1797b017e9ebd375b6 Author: Jan Holesovsky <ke...@collabora.com> Date: Wed Sep 27 14:13:43 2017 +0200 PutFile ext: X-LOOL-WOPI-IsModifiedByUser unit test. Change-Id: I0b1ffc74dbbc771f0dcb68f87d46af3ba469ae9e Reviewed-on: https://gerrit.libreoffice.org/42855 Reviewed-by: pranavk <pran...@collabora.co.uk> Tested-by: pranavk <pran...@collabora.co.uk> diff --git a/test/Makefile.am b/test/Makefile.am index 8f611d2e..d834dcb2 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -16,7 +16,8 @@ noinst_LTLIBRARIES = \ unit-timeout.la unit-prefork.la \ unit-storage.la \ unit-admin.la unit-tilecache.la \ - unit-fuzz.la unit-oob.la unit-oauth.la + unit-fuzz.la unit-oob.la unit-oauth.la \ + unit-wopi.la MAGIC_TO_FORCE_SHLIB_CREATION = -rpath /dummy AM_LDFLAGS = -pthread -module $(MAGIC_TO_FORCE_SHLIB_CREATION) $(ZLIB_LIBS) @@ -63,6 +64,8 @@ unit_storage_la_SOURCES = UnitStorage.cpp unit_tilecache_la_SOURCES = UnitTileCache.cpp unit_oauth_la_SOURCES = UnitOAuth.cpp unit_oauth_la_LIBADD = $(CPPUNIT_LIBS) +unit_wopi_la_SOURCES = UnitWOPI.cpp +unit_wopi_la_LIBADD = $(CPPUNIT_LIBS) if HAVE_LO_PATH SYSTEM_STAMP = @SYSTEMPLATE_PATH@/system_stamp @@ -76,7 +79,7 @@ check-local: ./run_unit.sh --log-file test.log --trs-file test.trs # FIXME 2: unit-oob.la fails with symbol undefined: # UnitWSD::testHandleRequest(UnitWSD::TestRequest, UnitHTTPServerRequest&, UnitHTTPServerResponse&) , -TESTS = unit-prefork.la unit-tilecache.la unit-timeout.la unit-oauth.la # unit-storage.la # unit-admin.la +TESTS = unit-prefork.la unit-tilecache.la unit-timeout.la unit-oauth.la unit-wopi.la # unit-storage.la # unit-admin.la else TESTS = ${top_builddir}/test/test endif diff --git a/test/UnitOAuth.cpp b/test/UnitOAuth.cpp index baf05c0e..dfba4aa1 100644 --- a/test/UnitOAuth.cpp +++ b/test/UnitOAuth.cpp @@ -26,7 +26,7 @@ class UnitOAuth : public WopiTestServer { LoadToken, // loading the document with Bearer token LoadHeader, // loading the document with Basic auth - Polling // let the loading progress, and when it succeeds, finish + Finish // assert all went fine and finish } _phase; bool _finishedToken; @@ -84,9 +84,9 @@ public: } } - bool wopiServerFinish() override + void assertPutFileRequest(const Poco::Net::HTTPRequest& /*request*/) override { - return _finishedToken && _finishedHeader; + // nothing to assert } void invokeTest() override @@ -116,12 +116,13 @@ public: if (_phase == Phase::LoadToken) _phase = Phase::LoadHeader; else - _phase = Phase::Polling; + _phase = Phase::Finish; break; } - case Phase::Polling: + case Phase::Finish: { - // let handleHttpRequest() perform the checks... + CPPUNIT_ASSERT(_finishedToken && _finishedHeader); + exitTest(TestResult::Ok); break; } } diff --git a/test/UnitWOPI.cpp b/test/UnitWOPI.cpp new file mode 100644 index 00000000..fade8d0f --- /dev/null +++ b/test/UnitWOPI.cpp @@ -0,0 +1,129 @@ +/* -*- 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/. + */ + +#include "config.h" + +#include "WopiTestServer.hpp" +#include "Log.hpp" +#include "Unit.hpp" +#include "UnitHTTP.hpp" +#include "helpers.hpp" +#include <Poco/Net/HTTPRequest.h> +#include <Poco/Util/LayeredConfiguration.h> + +class UnitWOPI : public WopiTestServer +{ + enum class Phase + { + LoadAndSave, + Modify, + SaveModified, + Finish + } _phase; + + enum class SavingPhase + { + Unmodified, + Modified + } _savingPhase; + + bool _finishedSaveUnmodified; + bool _finishedSaveModified; + + std::unique_ptr<UnitWebSocket> _ws; + +public: + UnitWOPI() : + _phase(Phase::LoadAndSave), + _finishedSaveUnmodified(false), + _finishedSaveModified(false) + { + } + + void assertCheckFileInfoRequest(const Poco::Net::HTTPRequest& /*request*/) override + { + // nothing to assert in CheckFileInfo + } + + void assertGetFileRequest(const Poco::Net::HTTPRequest& /*request*/) override + { + // nothing to assert in GetFile + } + + void assertPutFileRequest(const Poco::Net::HTTPRequest& request) override + { + if (_savingPhase == SavingPhase::Unmodified) + { + CPPUNIT_ASSERT_EQUAL(std::string("false"), request.get("X-LOOL-WOPI-IsModifiedByUser")); + _finishedSaveUnmodified = true; + } + else if (_savingPhase == SavingPhase::Modified) + { + CPPUNIT_ASSERT_EQUAL(std::string("true"), request.get("X-LOOL-WOPI-IsModifiedByUser")); + _finishedSaveModified = true; + } + } + + void invokeTest() override + { + constexpr char testName[] = "UnitWOPI"; + + switch (_phase) + { + case Phase::LoadAndSave: + { + Poco::URI wopiURL(helpers::getTestServerURI() + "/wopi/files/0?access_token=anything"); + std::string wopiSrc; + Poco::URI::encode(wopiURL.toString(), ":/?", wopiSrc); + Poco::URI loolUri(helpers::getTestServerURI()); + + LOG_INF("Connecting to the fake WOPI server: /lool/" << wopiSrc << "/ws"); + + _ws.reset(new UnitWebSocket("/lool/" + wopiSrc + "/ws")); + assert(_ws.get()); + + helpers::sendTextFrame(*_ws->getLOOLWebSocket(), "load url=" + wopiSrc, testName); + helpers::sendTextFrame(*_ws->getLOOLWebSocket(), "save dontTerminateEdit=1 dontSaveIfUnmodified=0", testName); + + _phase = Phase::Modify; + _savingPhase = SavingPhase::Unmodified; + break; + } + case Phase::Modify: + { + helpers::sendTextFrame(*_ws->getLOOLWebSocket(), "key type=input char=97 key=0", testName); + helpers::sendTextFrame(*_ws->getLOOLWebSocket(), "key type=up char=0 key=512", testName); + + _phase = Phase::SaveModified; + break; + } + case Phase::SaveModified: + { + helpers::sendTextFrame(*_ws->getLOOLWebSocket(), "save dontTerminateEdit=0 dontSaveIfUnmodified=0", testName); + + _phase = Phase::Finish; + _savingPhase = SavingPhase::Modified; + break; + } + case Phase::Finish: + { + CPPUNIT_ASSERT(_finishedSaveUnmodified && _finishedSaveModified); + exitTest(TestResult::Ok); + break; + } + } + } +}; + +UnitBase *unit_create_wsd(void) +{ + return new UnitWOPI(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/test/WopiTestServer.hpp b/test/WopiTestServer.hpp index 63cf4c7f..5ac79e81 100644 --- a/test/WopiTestServer.hpp +++ b/test/WopiTestServer.hpp @@ -29,7 +29,7 @@ public: virtual void assertGetFileRequest(const Poco::Net::HTTPRequest& request) = 0; - virtual bool wopiServerFinish() = 0; + virtual void assertPutFileRequest(const Poco::Net::HTTPRequest& request) = 0; protected: /// Here we act as a WOPI server, so that we have a server that responds to @@ -81,7 +81,7 @@ protected: return true; } // GetFile - else if (uriReq.getPath() == "/wopi/files/0/contents" || uriReq.getPath() == "/wopi/files/1/contents") + else if (request.getMethod() == "GET" && (uriReq.getPath() == "/wopi/files/0/contents" || uriReq.getPath() == "/wopi/files/1/contents")) { LOG_INF("Fake wopi host request, handling GetFile: " << uriReq.getPath()); @@ -101,8 +101,22 @@ protected: socket->send(oss.str()); socket->shutdown(); - if (wopiServerFinish()) - exitTest(TestResult::Ok); + return true; + } + else if (request.getMethod() == "POST" && (uriReq.getPath() == "/wopi/files/0/contents" || uriReq.getPath() == "/wopi/files/1/contents")) + { + LOG_INF("Fake wopi host request, handling PutFile: " << uriReq.getPath()); + + assertPutFileRequest(request); + + std::ostringstream oss; + oss << "HTTP/1.1 200 OK\r\n" + << "Last-Modified: " << Poco::DateTimeFormatter::format(Poco::Timestamp(), Poco::DateTimeFormat::HTTP_FORMAT) << "\r\n" + << "User-Agent: " << WOPI_AGENT_STRING << "\r\n" + << "\r\n"; + + socket->send(oss.str()); + socket->shutdown(); return true; } _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits