[Libreoffice-commits] online.git: Branch 'distro/collabora/collabora-online-cd' - test/Makefile.am test/UnitWOPI.cpp test/WopiTestServer.hpp

Jan Holesovsky kendy at collabora.com
Wed Sep 27 13:49:15 UTC 2017


 test/Makefile.am        |    7 +-
 test/UnitWOPI.cpp       |  129 ++++++++++++++++++++++++++++++++++++++++++++++++
 test/WopiTestServer.hpp |   22 ++++++--
 3 files changed, 152 insertions(+), 6 deletions(-)

New commits:
commit aba676d2682fd7353f50a37eb3643681fb12c8e4
Author: Jan Holesovsky <kendy at 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/42860
    Reviewed-by: Andras Timar <andras.timar at collabora.com>
    Tested-by: Andras Timar <andras.timar at collabora.com>

diff --git a/test/Makefile.am b/test/Makefile.am
index 754f0f12..ab1c2c92 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-fuzz.la unit-oob.la \
+	unit-wopi.la
 
 MAGIC_TO_FORCE_SHLIB_CREATION = -rpath /dummy
 AM_LDFLAGS = -pthread -module $(MAGIC_TO_FORCE_SHLIB_CREATION) $(ZLIB_LIBS)
@@ -60,6 +61,8 @@ unit_timeout_la_SOURCES = UnitTimeout.cpp
 unit_prefork_la_SOURCES = UnitPrefork.cpp
 unit_storage_la_SOURCES = UnitStorage.cpp
 unit_tilecache_la_SOURCES = UnitTileCache.cpp
+unit_wopi_la_SOURCES = UnitWOPI.cpp
+unit_wopi_la_LIBADD = $(CPPUNIT_LIBS)
 
 if HAVE_LO_PATH
 SYSTEM_STAMP = @SYSTEMPLATE_PATH@/system_stamp
@@ -73,7 +76,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-storage.la # unit-admin.la
+TESTS = unit-prefork.la unit-tilecache.la unit-timeout.la unit-wopi.la # unit-storage.la # unit-admin.la
 else
 TESTS = ${top_builddir}/test/test
 endif
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 17f6966b..db52e507 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: " << HTTP_AGENT_STRING << "\r\n"
+                << "\r\n";
+
+            socket->send(oss.str());
+            socket->shutdown();
 
             return true;
         }


More information about the Libreoffice-commits mailing list