loolwsd/Storage.hpp | 2 +- loolwsd/Util.hpp | 16 ++++++++++++++++ loolwsd/test/httpposttest.cpp | 8 +++++++- loolwsd/test/httpwstest.cpp | 30 ++++++++++++++++++++++-------- 4 files changed, 46 insertions(+), 10 deletions(-)
New commits: commit d48d9044a1d0263d4865de9df143233948c9bee0 Author: Ashod Nakashian <[email protected]> Date: Sat Mar 19 17:35:24 2016 -0400 loolwsd: run tests on doc copies Tests can modify the test documents they use. Currently there is data-loss protection that saves an open doc if connection is lost with the client. For tests this means modification are saved when a connection is terminated ungracefully and this both adds noise to the git checkout and makes subsequent tests fail. This patch makes temp copies of the original doc before a test is run and deletes them afterwards. Change-Id: I1dd6ff2b839701e85c8bd502ba75170c01fa106e Reviewed-on: https://gerrit.libreoffice.org/23447 Reviewed-by: Ashod Nakashian <[email protected]> Tested-by: Ashod Nakashian <[email protected]> diff --git a/loolwsd/Storage.hpp b/loolwsd/Storage.hpp index b3605c5..d185957 100644 --- a/loolwsd/Storage.hpp +++ b/loolwsd/Storage.hpp @@ -104,7 +104,7 @@ public: if (!Poco::File(_jailedFilePath).exists() && link(publicFilePath.c_str(), _jailedFilePath.c_str()) == -1) { // Failed - Log::error("link(\"" + publicFilePath + "\", \"" + _jailedFilePath + "\") failed."); + Log::warn("link(\"" + publicFilePath + "\", \"" + _jailedFilePath + "\") failed. Will copy."); } try diff --git a/loolwsd/Util.hpp b/loolwsd/Util.hpp index 2e83ea8..c496287 100644 --- a/loolwsd/Util.hpp +++ b/loolwsd/Util.hpp @@ -111,6 +111,22 @@ namespace Util removeFile(path.toString(), recursive); } + /// Make a temp copy of a file. + /// Primarily used by tests to avoid tainting the originals. + /// srcDir shouldn't end with '/' and srcFilename shouldn't contain '/'. + /// Returns the created file path. + inline + std::string getTempFilePath(const std::string srcDir, const std::string& srcFilename) + { + const std::string srcPath = srcDir + '/' + srcFilename; + + std::string dstPath = std::tmpnam(nullptr); + dstPath += '_' + srcFilename; + + Poco::File(srcPath).copyTo(dstPath); + return dstPath; + } + /// Returns the name of the signal. std::string signalName(int signo); diff --git a/loolwsd/test/httpposttest.cpp b/loolwsd/test/httpposttest.cpp index 0f843ed..5f43061 100644 --- a/loolwsd/test/httpposttest.cpp +++ b/loolwsd/test/httpposttest.cpp @@ -21,6 +21,7 @@ #include <cppunit/extensions/HelperMacros.h> #include <Common.hpp> +#include <Util.hpp> #include <ChildProcessSession.hpp> /// Tests the HTTP POST API of loolwsd. The server has to be started manually before running this test. @@ -51,6 +52,8 @@ public: void HTTPPostTest::testConvertTo() { + const auto srcPath = Util::getTempFilePath(TDOC, "hello.odt"); + Poco::URI uri("https://127.0.0.1:" + std::to_string(ClientPortNumber)); Poco::Net::HTTPSClientSession session(uri.getHost(), uri.getPort()); @@ -58,7 +61,7 @@ void HTTPPostTest::testConvertTo() Poco::Net::HTMLForm form; form.setEncoding(Poco::Net::HTMLForm::ENCODING_MULTIPART); form.set("format", "txt"); - form.addPart("data", new Poco::Net::FilePartSource(TDOC "/hello.odt")); + form.addPart("data", new Poco::Net::FilePartSource(srcPath)); form.prepareSubmit(request); // If this results in a Poco::Net::ConnectionRefusedException, loolwsd is not running. form.write(session.sendRequest(request)); @@ -73,6 +76,9 @@ void HTTPPostTest::testConvertTo() std::stringstream expectedStream; expectedStream << fileStream.rdbuf(); + // Remove the temp files. + Util::removeFile(srcPath); + // In some cases the result is prefixed with (the UTF-8 encoding of) the Unicode BOM // (U+FEFF). Skip that. std::string actualString = actualStream.str(); diff --git a/loolwsd/test/httpwstest.cpp b/loolwsd/test/httpwstest.cpp index 9b097fd..2e4ff0a 100644 --- a/loolwsd/test/httpwstest.cpp +++ b/loolwsd/test/httpwstest.cpp @@ -24,8 +24,10 @@ #include <Poco/JSON/JSON.h> #include <Poco/JSON/Parser.h> #include <Poco/Dynamic/Var.h> -#include <LOOLProtocol.hpp> + #include <Common.hpp> +#include <Util.hpp> +#include <LOOLProtocol.hpp> #include <ChildProcessSession.hpp> /// Tests the HTTP WebSocket API of loolwsd. The server has to be started manually before running this test. @@ -90,7 +92,12 @@ public: void tearDown() { + // Remove the temp files. + Util::removeFile(_tmpFilePath); } + +private: + std::string _tmpFilePath; }; void HTTPWSTest::testPaste() @@ -98,7 +105,8 @@ void HTTPWSTest::testPaste() try { // Load a document and make it empty. - const std::string documentPath = TDOC "/hello.odt"; + const std::string documentPath = Util::getTempFilePath(TDOC, "hello.odt"); + _tmpFilePath = documentPath; const std::string documentURL = "file://" + Poco::Path(documentPath).makeAbsolute().toString(); Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL); @@ -152,7 +160,8 @@ void HTTPWSTest::testLargePaste() try { // Load a document and make it empty. - const std::string documentPath = TDOC "/hello.odt"; + const std::string documentPath = Util::getTempFilePath(TDOC, "hello.odt"); + _tmpFilePath = documentPath; const std::string documentURL = "file://" + Poco::Path(documentPath).makeAbsolute().toString(); Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL); @@ -203,7 +212,8 @@ void HTTPWSTest::testRenderingOptions() try { // Load a document and get its size. - const std::string documentPath = TDOC "/hide-whitespace.odt"; + const std::string documentPath = Util::getTempFilePath(TDOC, "hide-whitespace.odt"); + _tmpFilePath = documentPath; const std::string documentURL = "file://" + Poco::Path(documentPath).makeAbsolute().toString(); const std::string options = "{\"rendering\":{\".uno:HideWhitespace\":{\"type\":\"boolean\",\"value\":\"true\"}}}"; @@ -256,7 +266,8 @@ void HTTPWSTest::testPasswordProtectedDocumentWithoutPassword() { try { - const std::string documentPath = TDOC "/password-protected.ods"; + const std::string documentPath = Util::getTempFilePath(TDOC, "password-protected.ods"); + _tmpFilePath = documentPath; const std::string documentURL = "file://" + Poco::Path(documentPath).makeAbsolute().toString(); Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL); @@ -291,7 +302,8 @@ void HTTPWSTest::testPasswordProtectedDocumentWithWrongPassword() { try { - const std::string documentPath = TDOC "/password-protected.ods"; + const std::string documentPath = Util::getTempFilePath(TDOC, "password-protected.ods"); + _tmpFilePath = documentPath; const std::string documentURL = "file://" + Poco::Path(documentPath).makeAbsolute().toString(); Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL); @@ -326,7 +338,8 @@ void HTTPWSTest::testPasswordProtectedDocumentWithCorrectPassword() { try { - const std::string documentPath = TDOC "/password-protected.ods"; + const std::string documentPath = Util::getTempFilePath(TDOC, "password-protected.ods"); + _tmpFilePath = documentPath; const std::string documentURL = "file://" + Poco::Path(documentPath).makeAbsolute().toString(); Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL); @@ -354,7 +367,8 @@ void HTTPWSTest::testImpressPartCountChanged() try { // Load a document - const std::string documentPath = TDOC "/insert-delete.odp"; + const std::string documentPath = Util::getTempFilePath(TDOC, "insert-delete.odp"); + _tmpFilePath = documentPath; const std::string documentURL = "file://" + Poco::Path(documentPath).makeAbsolute().toString(); Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL); _______________________________________________ Libreoffice-commits mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits
