loolwsd/test/data/password-protected.ods |binary loolwsd/test/httpwstest.cpp | 61 +++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+)
New commits: commit b902d80ec7354923f3680aea83b774114be387c4 Author: Pranav Kant <[email protected]> Date: Thu Feb 18 02:45:28 2016 +0530 loolwsd: Add a unit test for password protected documents Change-Id: Ia675d2750e11cb466b2e80b8f36f2ef04e0a7a09 Reviewed-on: https://gerrit.libreoffice.org/22466 Reviewed-by: Ashod Nakashian <[email protected]> Tested-by: Ashod Nakashian <[email protected]> diff --git a/loolwsd/test/data/password-protected.ods b/loolwsd/test/data/password-protected.ods new file mode 100644 index 0000000..d803d8e Binary files /dev/null and b/loolwsd/test/data/password-protected.ods differ diff --git a/loolwsd/test/httpwstest.cpp b/loolwsd/test/httpwstest.cpp index e17702d..351031d 100644 --- a/loolwsd/test/httpwstest.cpp +++ b/loolwsd/test/httpwstest.cpp @@ -22,6 +22,8 @@ #include <Common.hpp> #include <ChildProcessSession.hpp> +using Poco::StringTokenizer; + /// Tests the HTTP WebSocket API of loolwsd. The server has to be started manually before running this test. class HTTPWSTest : public CPPUNIT_NS::TestFixture { @@ -34,11 +36,13 @@ class HTTPWSTest : public CPPUNIT_NS::TestFixture CPPUNIT_TEST(testPaste); CPPUNIT_TEST(testLargePaste); CPPUNIT_TEST(testRenderingOptions); + CPPUNIT_TEST(testPasswordProtectedDocument); CPPUNIT_TEST_SUITE_END(); void testPaste(); void testLargePaste(); void testRenderingOptions(); + void testPasswordProtectedDocument(); static void sendTextFrame(Poco::Net::WebSocket& socket, const std::string& string); @@ -216,6 +220,63 @@ void HTTPWSTest::testRenderingOptions() } } +void HTTPWSTest::testPasswordProtectedDocument() +{ + // Load a password protected document + const std::string documentPath = TDOC "/password-protected.ods"; + const std::string documentURL = "file://" + Poco::Path(documentPath).makeAbsolute().toString(); + // Send a load request without password first + sendTextFrame(_socket, "load url=" + documentURL); + + int flags; + int n; + int counter = 0; + do + { + char buffer[READ_BUFFER_SIZE]; + n = _socket.receiveFrame(buffer, sizeof(buffer), flags); + if (n > 0) + { + std::string line = LOOLProtocol::getFirstLine(buffer, n); + StringTokenizer tokens(line, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM); + std::string errorCommand; + std::string errorKind; + if (counter == 0 && + tokens[0] == "error:" && + LOOLProtocol::getTokenString(tokens[1], "cmd", errorCommand) && + LOOLProtocol::getTokenString(tokens[2], "kind", errorKind) ) + { + CPPUNIT_ASSERT_EQUAL(std::string("load"), errorCommand); + // TODO: Do a test for document requiring password to edit + CPPUNIT_ASSERT_EQUAL(std::string("passwordrequired:to-view"), errorKind); + + // Send another load request with incorrect password + sendTextFrame(_socket, "load url=" + documentURL + " password=2"); + counter++; + } + else if (counter == 1 && + tokens[0] == "error:" && + LOOLProtocol::getTokenString(tokens[1], "cmd", errorCommand) && + LOOLProtocol::getTokenString(tokens[2], "kind", errorKind) ) + { + CPPUNIT_ASSERT_EQUAL(std::string("load"), errorCommand); + CPPUNIT_ASSERT_EQUAL(std::string("wrongpassword"), errorKind); + + // Send another load request with correct password + sendTextFrame(_socket, "load url=" + documentURL + " password=1"); + counter++; + } + else if (counter == 2 && + tokens[0] == "status:") + { + // Entering correct password opened the document + break; + } + } + } + while (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE); +} + void HTTPWSTest::sendTextFrame(Poco::Net::WebSocket& socket, const std::string& string) { socket.sendFrame(string.data(), string.size()); _______________________________________________ Libreoffice-commits mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits
