test/UnitHTTP.cpp |  135 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
 test/helpers.hpp  |   23 ++++++++-
 2 files changed, 151 insertions(+), 7 deletions(-)

New commits:
commit e423bd10f5eeee9b3b052b2b6028c63409be15e4
Author:     Michael Meeks <michael.me...@collabora.com>
AuthorDate: Wed May 22 10:54:36 2019 +0100
Commit:     Andras Timar <andras.ti...@collabora.com>
CommitDate: Thu Jun 13 10:16:09 2019 +0200

    tests for chunked transfer encoding parser.
    
    Change-Id: Ic55669ab7cc55bb44e8f7a00f30231b44f10535a
    Reviewed-on: https://gerrit.libreoffice.org/72749
    Reviewed-by: Andras Timar <andras.ti...@collabora.com>
    Tested-by: Andras Timar <andras.ti...@collabora.com>

diff --git a/test/UnitHTTP.cpp b/test/UnitHTTP.cpp
index d0530fe10..e86593f22 100644
--- a/test/UnitHTTP.cpp
+++ b/test/UnitHTTP.cpp
@@ -13,6 +13,7 @@
 
 #include <helpers.hpp>
 #include <Poco/Util/Application.h>
+#include <Poco/Net/StreamSocket.h>
 #include <Poco/Net/StringPartSource.h>
 #include <Poco/Net/HTMLForm.h>
 #include <Poco/Net/HTTPRequest.h>
@@ -37,9 +38,9 @@ public:
         config.setBool("ssl.enable", true);
     }
 
-    // FIXME: can hook with (UnitWSD::get().handleHttpRequest(request, 
message, socket)) ...
-    void invokeTest() override
+    void testContinue()
     {
+        std::cerr << "testContinue\n";
         for (int i = 0; i < 3; ++i)
         {
             std::unique_ptr<Poco::Net::HTTPClientSession> 
session(helpers::createSession(Poco::URI(helpers::getTestServerURI())));
@@ -81,9 +82,135 @@ public:
                 return;
             }
         }
-        // Give those convertors time to save and cleanup.
-        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
+    }
+
+    void writeString(const std::shared_ptr<Poco::Net::StreamSocket> &socket, 
std::string str)
+    {
+        socket->sendBytes(str.c_str(), str.size());
+    }
+
+    bool expectString(const std::shared_ptr<Poco::Net::StreamSocket> &socket, 
std::string str)
+    {
+        char buffer[str.size() + 64] = { 0, };
+        int got = socket->receiveBytes(buffer, str.size());
+        if (got != (int)str.size() ||
+            strncmp(buffer, str.c_str(), got))
+        {
+            std::cerr << "testChunks got " << got << " mismatching strings '" 
<< buffer << " vs. expected '" << str << "'\n";
+            exitTest(TestResult::Failed);
+            return false;
+        }
+        else
+            return true;
+    }
+
+    void testChunks()
+    {
+        std::cerr << "testChunks\n";
+
+        std::shared_ptr<Poco::Net::StreamSocket> socket = 
helpers::createRawSocket();
+
+        writeString(
+            socket,
+            "POST /lool/convert-to/txt HTTP/1.1\r\n"
+            "Host: localhost:9980\r\n"
+            "User-Agent: looltests/1.2.3\r\n"
+            "Accept: */*\r\n"
+            "Expect: 100-continue\r\n"
+            "Transfer-Encoding: chunked\r\n"
+            "Content-Type: multipart/form-data; "
+            "boundary=------------------------5a0cd5c881663db4\r\n\r\n");
+        if (!expectString(
+                socket,
+                "HTTP/1.1 100 Continue\r\n\r\n"))
+            return;
+
+#define START_CHUNK_HEX(len) len "\r\n"
+#define END_CHUNK "\r\n"
+        writeString(
+            socket,
+            START_CHUNK_HEX("8A")
+            "--------------------------5a0cd5c881663db4\r\n"
+            "Content-Disposition: form-data; name=\"data\"; 
filename=\"test.txt\"\r\n"
+            "Content-Type: text/plain\r\n"
+            "\r\n"
+            END_CHUNK
+
+            START_CHUNK_HEX("12")
+            "This is some text."
+            END_CHUNK
+
+            START_CHUNK_HEX("1")
+            "\n"
+            END_CHUNK
+
+            "  4 room:for expansion!! cf. leading spaces and nasies 
<>!\"\'?=)\r\n"
+            "And "
+            END_CHUNK
+
+            START_CHUNK_HEX("1")
+            "s"
+            END_CHUNK
+
+            START_CHUNK_HEX("a")
+            "ome more.\n"
+            END_CHUNK
+            );
+        writeString(
+            socket,
+            START_CHUNK_HEX("30")
+            "\r\n"
+            "--------------------------5a0cd5c881663db4--\r\n"
+            END_CHUNK);
+
+        writeString(socket, START_CHUNK_HEX("0"));
+
+        char buffer[4096] = { 0, };
+        int got = socket->receiveBytes(buffer, 4096);
+        std::string start =
+            "HTTP/1.0 200 OK\r\n"
+            "Content-Disposition: attachment; filename=\"test.txt\"\r\n";
+
+        if (strncmp(buffer, start.c_str(), start.size()))
+        {
+            std::cerr << "missing pre-amble " << got << " '" << buffer << " 
vs. expected '" << start << "'\n";
+            exitTest(TestResult::Failed);
+            return;
+        }
 
+        // TODO: check content-length etc.
+
+        const char *ptr = strstr(buffer, "\r\n\r\n");
+        if (!ptr)
+        {
+            std::cerr << "missing separator " << got << " '" << buffer << "\n";
+            exitTest(TestResult::Failed);
+            return;
+        }
+
+        // Oddly we need another read to get the content.
+        got = socket->receiveBytes(buffer, 4096);
+        if (got >=0 )
+            buffer[got] = '\0';
+        else
+        {
+            std::cerr << "No content returned " << got << "\n";
+            exitTest(TestResult::Failed);
+            return;
+        }
+
+        if (strcmp(buffer, "\357\273\277This is some text.\nAnd some more.\n"))
+        {
+            std::cerr << "unexpected file content " << got << " '" << buffer 
<< "\n";
+            exitTest(TestResult::Failed);
+            return;
+        }
+    }
+
+    void invokeTest() override
+    {
+        testChunks();
+        testContinue();
         std::cerr << "All tests passed.\n";
         exitTest(TestResult::Ok);
     }
diff --git a/test/helpers.hpp b/test/helpers.hpp
index 13b351762..50aadada7 100644
--- a/test/helpers.hpp
+++ b/test/helpers.hpp
@@ -21,6 +21,8 @@
 #include <Poco/Net/HTTPResponse.h>
 #include <Poco/Net/HTTPSClientSession.h>
 #include <Poco/Net/NetException.h>
+#include <Poco/Net/StreamSocket.h>
+#include <Poco/Net/SecureStreamSocket.h>
 #include <Poco/Net/Socket.h>
 #include <Poco/Path.h>
 #include <Poco/StringTokenizer.h>
@@ -166,18 +168,33 @@ Poco::Net::HTTPClientSession* createSession(const 
Poco::URI& uri)
 #endif
 }
 
-inline
-std::string const & getTestServerURI()
+inline int getClientPort()
 {
     static const char* clientPort = std::getenv("LOOL_TEST_CLIENT_PORT");
+    return clientPort? atoi(clientPort) : DEFAULT_CLIENT_PORT_NUMBER;
+}
+
+inline std::shared_ptr<Poco::Net::StreamSocket> createRawSocket()
+{
+    return
+#if ENABLE_SSL
+        std::make_shared<Poco::Net::SecureStreamSocket>
+#else
+        std::make_shared<Poco::Net::StreamSocket>
+#endif
+            (Poco::Net::SocketAddress("127.0.0.1", getClientPort()));
+}
 
+inline
+std::string const & getTestServerURI()
+{
     static std::string serverURI(
 #if ENABLE_SSL
             "https://127.0.0.1:";
 #else
             "http://127.0.0.1:";
 #endif
-            + (clientPort? std::string(clientPort) : 
std::to_string(DEFAULT_CLIENT_PORT_NUMBER)));
+            + std::to_string(getClientPort()));
 
     return serverURI;
 }
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to