summaryrefslogtreecommitdiffstats
path: root/test/WhiteBoxTests.cpp
diff options
context:
space:
mode:
authorAshod Nakashian <ashod.nakashian@collabora.co.uk>2020-11-15 09:04:39 -0500
committerAshod Nakashian <Ashod@users.noreply.github.com>2020-11-15 13:50:16 -0500
commit20f95a17a96a615604fcb9214cc7b8624f42b68b (patch)
tree4954e6a4d0c63fae76ffd76bf0cc393caf7b000c /test/WhiteBoxTests.cpp
parentwsd: socket comments and cosmetics (diff)
downloadonline-20f95a17a96a615604fcb9214cc7b8624f42b68b.tar.gz
online-20f95a17a96a615604fcb9214cc7b8624f42b68b.zip
wsd: improved Stat modified-time and unit-tests
Change-Id: Ic6e7972e3d69c78681f599b6e6797e3e4164cd34 Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
Diffstat (limited to 'test/WhiteBoxTests.cpp')
-rw-r--r--test/WhiteBoxTests.cpp53
1 files changed, 53 insertions, 0 deletions
diff --git a/test/WhiteBoxTests.cpp b/test/WhiteBoxTests.cpp
index b7878543ce..338e4468ff 100644
--- a/test/WhiteBoxTests.cpp
+++ b/test/WhiteBoxTests.cpp
@@ -7,11 +7,14 @@
#include <config.h>
+#include <chrono>
+#include <fstream>
#include <test/lokassert.hpp>
#include <Auth.hpp>
#include <ChildSession.hpp>
#include <Common.hpp>
+#include <FileUtil.hpp>
#include <Kit.hpp>
#include <MessageQueue.hpp>
#include <Protocol.hpp>
@@ -49,6 +52,7 @@ class WhiteBoxTests : public CPPUNIT_NS::TestFixture
CPPUNIT_TEST(testRequestDetails);
CPPUNIT_TEST(testUIDefaults);
CPPUNIT_TEST(testCSSVars);
+ CPPUNIT_TEST(testStat);
CPPUNIT_TEST_SUITE_END();
@@ -73,6 +77,7 @@ class WhiteBoxTests : public CPPUNIT_NS::TestFixture
void testRequestDetails();
void testUIDefaults();
void testCSSVars();
+ void testStat();
};
void WhiteBoxTests::testLOOLProtocolFunctions()
@@ -1614,6 +1619,54 @@ void WhiteBoxTests::testCSSVars()
FileServerRequestHandler::cssVarsToStyle("--co-somestyle-text=#123456;;--some-val=3453--some-other-val=4536;;"));
}
+void WhiteBoxTests::testStat()
+{
+ FileUtil::Stat invalid("/missing/file/path");
+ LOK_ASSERT(!invalid.good());
+ LOK_ASSERT(invalid.bad());
+ LOK_ASSERT(!invalid.exists());
+
+ const std::string tmpFile = FileUtil::getTemporaryDirectoryPath() + "/test_stat";
+ std::ofstream ofs(tmpFile);
+ FileUtil::Stat st(tmpFile);
+ LOK_ASSERT(st.good());
+ LOK_ASSERT(!st.bad());
+ LOK_ASSERT(st.exists());
+ LOK_ASSERT(!st.isDirectory());
+ LOK_ASSERT(st.isFile());
+ LOK_ASSERT(!st.isLink());
+ LOK_ASSERT(st.path() == tmpFile);
+
+ // Modified-time tests.
+ // Some test might fail when the system has a different resolution for file timestamps
+ // and time_point. Specifically, if the filesystem has microsecond precision but time_point
+ // has lower resolution (milliseconds or seconds, f.e.), modifiedTimepoint() will not match
+ // modifiedTimeUs(), and the checks will fail.
+ // So far, microseconds seem to be the lower common denominator. At least on Android and
+ // iOS that's the precision of time_point (as of late 2020), but Linux servers have
+ // nanosecond precision.
+
+ LOK_ASSERT(std::chrono::time_point_cast<std::chrono::microseconds>(st.modifiedTimepoint())
+ .time_since_epoch()
+ .count()
+ == static_cast<long>(st.modifiedTimeUs()));
+ LOK_ASSERT(std::chrono::time_point_cast<std::chrono::milliseconds>(st.modifiedTimepoint())
+ .time_since_epoch()
+ .count()
+ == static_cast<long>(st.modifiedTimeMs()));
+ LOK_ASSERT(std::chrono::time_point_cast<std::chrono::seconds>(st.modifiedTimepoint())
+ .time_since_epoch()
+ .count()
+ == static_cast<long>(st.modifiedTimeMs() / 1000));
+ LOK_ASSERT(st.modifiedTime().tv_sec == static_cast<long>(st.modifiedTimeMs() / 1000));
+ LOK_ASSERT(st.modifiedTime().tv_nsec / 1000
+ == static_cast<long>(st.modifiedTimeUs())
+ - (st.modifiedTime().tv_sec * 1000 * 1000));
+
+ ofs.close();
+ FileUtil::removeFile(tmpFile);
+}
+
CPPUNIT_TEST_SUITE_REGISTRATION(WhiteBoxTests);
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */