loolwsd/test/httpwstest.cpp | 82 +++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 45 deletions(-)
New commits: commit 6722d430a588d45b9b77e8490f0dcba0c175ac0a Author: Pranav Kant <pran...@collabora.com> Date: Sun Feb 21 20:20:40 2016 +0530 loolwsd: Use helper functions for document password unit test Change-Id: Ibf921ea1834f85b9a1d81232b1a018c1936fe620 diff --git a/loolwsd/test/httpwstest.cpp b/loolwsd/test/httpwstest.cpp index 5694d20..502bb2f 100644 --- a/loolwsd/test/httpwstest.cpp +++ b/loolwsd/test/httpwstest.cpp @@ -235,60 +235,52 @@ void HTTPWSTest::testPasswordProtectedDocument() Poco::Net::WebSocket socket(_session, _request, _response); - // 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); + std::string response; - int flags; - int n; - int counter = 0; - do + getResponseMessage(socket, "error:", response, true); + CPPUNIT_ASSERT_MESSAGE("failed command load: ", !response.empty()); { - 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); + Poco::StringTokenizer tokens(response, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), tokens.count()); + + std::string errorCommand; + std::string errorKind; + LOOLProtocol::getTokenString(tokens[0], "cmd", errorCommand); + LOOLProtocol::getTokenString(tokens[1], "kind", errorKind); + CPPUNIT_ASSERT_EQUAL(std::string("load"), errorCommand); + 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 incorrect password + sendTextFrame(socket, "load url=" + documentURL + " password=2"); - // 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); + getResponseMessage(socket, "error:", response, true); + CPPUNIT_ASSERT_MESSAGE("failed command load: ", !response.empty()); + { + Poco::StringTokenizer tokens(response, " ", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), tokens.count()); + + std::string errorCommand; + std::string errorKind; + LOOLProtocol::getTokenString(tokens[0], "cmd", errorCommand); + LOOLProtocol::getTokenString(tokens[1], "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"); + + CPPUNIT_ASSERT_MESSAGE("cannot load the document with correct password " + documentURL, isDocumentLoaded(socket)); + sendTextFrame(socket, "disconnect"); + socket.shutdown(); } - catch (const Poco::Exception& exc) { + catch (const Poco::Exception& exc) + { CPPUNIT_ASSERT_MESSAGE(exc.displayText(), false); } } _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits