[Libreoffice-commits] online.git: loolwsd/test loolwsd/UserMessages.hpp

Henry Castro hcastro at collabora.com
Wed Sep 28 20:03:37 UTC 2016


 loolwsd/UserMessages.hpp     |    2 
 loolwsd/test/Makefile.am     |    2 
 loolwsd/test/helpers.hpp     |   28 +++++++
 loolwsd/test/httpwserror.cpp |  152 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 182 insertions(+), 2 deletions(-)

New commits:
commit 5e19aa635e510b88397f8ea45f30c5bc07ce6f0c
Author: Henry Castro <hcastro at collabora.com>
Date:   Wed Sep 28 16:04:51 2016 -0400

    loolwsd: test: connections and documents

diff --git a/loolwsd/UserMessages.hpp b/loolwsd/UserMessages.hpp
index 2ef4c8b..8d1dee7 100644
--- a/loolwsd/UserMessages.hpp
+++ b/loolwsd/UserMessages.hpp
@@ -15,7 +15,7 @@
 //NOTE: For whatever reason Poco seems to trim the first character.
 
 constexpr auto SERVICE_UNAVALABLE_INTERNAL_ERROR = " Service is unavailable. Please try again later and report to your administrator if the issue persists.";
-constexpr auto SERVICE_UNAVALABLE_LIMIT_REACHED = " This server has reached the number of connections or documents it supports at a given time.";
+constexpr auto SERVICE_UNAVALABLE_LIMIT_REACHED = "This development build is limited to %d documents, and %d connections - to avoid the impression that it is suitable for deployment in large enterprises. To find out more about deploying and scaling %s checkout - <a href=\"%s\">%s</a>.";
 
 #endif
 
diff --git a/loolwsd/test/Makefile.am b/loolwsd/test/Makefile.am
index 6bd6d73..d8e152e 100644
--- a/loolwsd/test/Makefile.am
+++ b/loolwsd/test/Makefile.am
@@ -29,7 +29,7 @@ wsd_sources = \
 
 test_CPPFLAGS = -DTDOC=\"$(abs_top_srcdir)/test/data\" -I$(top_srcdir)
 test_SOURCES = TileCacheTests.cpp WhiteBoxTests.cpp integration-http-server.cpp \
-               httpwstest.cpp httpcrashtest.cpp test.cpp $(wsd_sources)
+               httpwstest.cpp httpcrashtest.cpp httpwserror.cpp test.cpp $(wsd_sources)
 test_LDADD = $(CPPUNIT_LIBS)
 
 # unit test modules:
diff --git a/loolwsd/test/helpers.hpp b/loolwsd/test/helpers.hpp
index 90cb973..f73db3c 100644
--- a/loolwsd/test/helpers.hpp
+++ b/loolwsd/test/helpers.hpp
@@ -15,6 +15,7 @@
 #include <thread>
 #include <regex>
 
+#include <Poco/BinaryReader.h>
 #include <Poco/DirectoryIterator.h>
 #include <Poco/Dynamic/Var.h>
 #include <Poco/FileStream.h>
@@ -188,6 +189,33 @@ std::string getTestServerURI()
 }
 
 inline
+int getErrorCode(Poco::Net::WebSocket& ws, std::string& message)
+{
+    int flags = 0;
+    int bytes = 0;
+    Poco::UInt16 statusCode = -1;
+    Poco::Buffer<char> buffer(READ_BUFFER_SIZE);
+
+    message.clear();
+    Poco::Timespan timeout(250000);
+    ws.setReceiveTimeout(timeout);
+    do
+    {
+        bytes = ws.receiveFrame(buffer.begin(), READ_BUFFER_SIZE, flags);
+    }
+    while ((flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE);
+
+    if (bytes > 0)
+    {
+        Poco::MemoryBinaryReader reader(buffer, Poco::BinaryReader::NETWORK_BYTE_ORDER);
+        reader >> statusCode;
+        message.append(buffer.begin() + 2, bytes - 2);
+    }
+
+    return statusCode;
+}
+
+inline
 void getResponseMessage(Poco::Net::WebSocket& ws, const std::string& prefix, std::string& response, const bool isLine, const std::string& name = "")
 {
     try
diff --git a/loolwsd/test/httpwserror.cpp b/loolwsd/test/httpwserror.cpp
new file mode 100644
index 0000000..6a130bc
--- /dev/null
+++ b/loolwsd/test/httpwserror.cpp
@@ -0,0 +1,152 @@
+/* -*- 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 <vector>
+#include <string>
+
+#include <Poco/Net/HTTPClientSession.h>
+#include <Poco/Net/HTTPRequest.h>
+#include <Poco/Net/HTTPResponse.h>
+#include <Poco/Net/HTTPSClientSession.h>
+#include <Poco/Net/NetException.h>
+#include <Poco/Net/WebSocket.h>
+#include <Poco/URI.h>
+
+#include <cppunit/extensions/HelperMacros.h>
+
+#include "Common.hpp"
+#include "LOOLProtocol.hpp"
+#include "helpers.hpp"
+
+using namespace helpers;
+
+class HTTPWSError : public CPPUNIT_NS::TestFixture
+{
+    const Poco::URI _uri;
+    Poco::Net::HTTPResponse _response;
+
+    CPPUNIT_TEST_SUITE(HTTPWSError);
+
+    CPPUNIT_TEST(testMaxDocuments);
+    CPPUNIT_TEST(testMaxConnections);
+
+    CPPUNIT_TEST_SUITE_END();
+
+    void testMaxDocuments();
+    void testMaxConnections();
+
+public:
+    HTTPWSError()
+        : _uri(helpers::getTestServerURI())
+    {
+#if ENABLE_SSL
+        Poco::Net::initializeSSL();
+        // Just accept the certificate anyway for testing purposes
+        Poco::SharedPtr<Poco::Net::InvalidCertificateHandler> invalidCertHandler = new Poco::Net::AcceptCertificateHandler(false);
+        Poco::Net::Context::Params sslParams;
+        Poco::Net::Context::Ptr sslContext = new Poco::Net::Context(Poco::Net::Context::CLIENT_USE, sslParams);
+        Poco::Net::SSLManager::instance().initializeClient(0, invalidCertHandler, sslContext);
+#endif
+    }
+
+#if ENABLE_SSL
+    ~HTTPWSError()
+    {
+        Poco::Net::uninitializeSSL();
+    }
+#endif
+
+    void setUp()
+    {
+    }
+
+    void tearDown()
+    {
+    }
+};
+
+void HTTPWSError::testMaxDocuments()
+{
+#if MAX_DOCUMENTS > 0
+    try
+    {
+        // Load a document.
+        std::string docPath;
+        std::string docURL;
+        std::string message;
+        Poco::UInt16 statusCode;
+        std::vector<std::shared_ptr<Poco::Net::WebSocket>> docs;
+
+        for(int it = 1; it <= MAX_DOCUMENTS; it++)
+        {
+            getDocumentPathAndURL("empty.odt", docPath, docURL);
+            Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, docURL);
+            docs.emplace_back(connectLOKit(_uri, request, _response));
+        }
+
+        // try to open MAX_DOCUMENTS + 1
+        getDocumentPathAndURL("empty.odt", docPath, docURL);
+        Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, docURL);
+        std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));
+        Poco::Net::WebSocket socket(*session, request, _response);
+        statusCode = getErrorCode(socket, message);
+        CPPUNIT_ASSERT_EQUAL(static_cast<Poco::UInt16>(Poco::Net::WebSocket::WS_ENDPOINT_GOING_AWAY), statusCode);
+        CPPUNIT_ASSERT_MESSAGE("Wrong error message ", message.find("This development build") != std::string::npos);
+    }
+    catch (const Poco::Exception& exc)
+    {
+        CPPUNIT_FAIL(exc.displayText());
+    }
+#endif
+}
+
+void HTTPWSError::testMaxConnections()
+{
+#if MAX_CONNECTIONS > 0
+    try
+    {
+        // Load a document.
+        std::string docPath;
+        std::string docURL;
+        std::string message;
+        Poco::UInt16 statusCode;
+        std::vector<std::shared_ptr<Poco::Net::WebSocket>> views;
+
+        getDocumentPathAndURL("empty.odt", docPath, docURL);
+        Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, docURL);
+        auto socket = loadDocAndGetSocket(_uri, docURL, "testMaxConnections ");
+
+        for(int it = 1; it < MAX_CONNECTIONS; it++)
+        {
+            std::cerr << it << std::endl;
+            std::unique_ptr<Poco::Net::HTTPClientSession> session(createSession(_uri));
+            auto ws = std::make_shared<Poco::Net::WebSocket>(*session, request, _response);
+            views.emplace_back(ws);
+        }
+
+        // try to connect MAX_CONNECTIONS + 1
+        std::unique_ptr<Poco::Net::HTTPClientSession> session(createSession(_uri));
+        auto socketN = std::make_shared<Poco::Net::WebSocket>(*session, request, _response);
+        statusCode = getErrorCode(*socketN, message);
+        CPPUNIT_ASSERT_EQUAL(static_cast<Poco::UInt16>(Poco::Net::WebSocket::WS_ENDPOINT_GOING_AWAY), statusCode);
+        CPPUNIT_ASSERT_MESSAGE("Wrong error message ", message.find("This development build") != std::string::npos);
+
+    }
+    catch (const Poco::Exception& exc)
+    {
+        CPPUNIT_FAIL(exc.displayText());
+    }
+#endif
+}
+
+CPPUNIT_TEST_SUITE_REGISTRATION(HTTPWSError);
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */


More information about the Libreoffice-commits mailing list