summaryrefslogtreecommitdiff
path: root/Tests/LibC
diff options
context:
space:
mode:
authorDaniel Bertalan <dani@danielbertalan.dev>2022-01-16 09:03:46 +0100
committerBrian Gianforcaro <b.gianfo@gmail.com>2022-01-16 14:59:21 -0800
commit6e00dd64a1a8e37c8389759141b7d0226fe597c8 (patch)
tree655c5e344e8fa00454f2d58ba0a450e907bf71df /Tests/LibC
parenta1dfa1efb2da5b79683e8cc00706d12e0fe1bcf7 (diff)
downloadserenity-6e00dd64a1a8e37c8389759141b7d0226fe597c8.zip
Tests: Test whether stdio streams are flushed correctly on exit
Diffstat (limited to 'Tests/LibC')
-rw-r--r--Tests/LibC/CMakeLists.txt1
-rw-r--r--Tests/LibC/TestStdio.cpp42
2 files changed, 43 insertions, 0 deletions
diff --git a/Tests/LibC/CMakeLists.txt b/Tests/LibC/CMakeLists.txt
index 02bde80580..2391c05ae0 100644
--- a/Tests/LibC/CMakeLists.txt
+++ b/Tests/LibC/CMakeLists.txt
@@ -18,6 +18,7 @@ set(TEST_SOURCES
TestSearch.cpp
TestSnprintf.cpp
TestStackSmash.cpp
+ TestStdio.cpp
TestStrlcpy.cpp
TestStrtodAccuracy.cpp
TestWchar.cpp
diff --git a/Tests/LibC/TestStdio.cpp b/Tests/LibC/TestStdio.cpp
new file mode 100644
index 0000000000..0c0b818d0e
--- /dev/null
+++ b/Tests/LibC/TestStdio.cpp
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2022, Daniel Bertalan <dani@danielbertalan.dev>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibTest/TestCase.h>
+#include <stdio.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+// Test whether file writes are flushed to disk at program termination,
+// even if we do not close the files or call fflush().
+TEST_CASE(flush_on_exit)
+{
+ static constexpr auto test_str = "peekaboo";
+ auto pid = fork();
+ VERIFY(pid >= 0);
+
+ if (pid == 0) {
+ auto* fp = fopen("/tmp/flushtest", "w");
+ VERIFY(fp != nullptr);
+ auto nwritten = fwrite(test_str, 1, strlen(test_str), fp);
+ VERIFY(nwritten != 0);
+ // We intentionally leak `fp` here.
+ exit(0);
+ } else {
+ int wstatus;
+ auto rc = waitpid(pid, &wstatus, 0);
+ VERIFY(rc >= 0);
+
+ char buf[256];
+ auto* fp = fopen("/tmp/flushtest", "r");
+ VERIFY(fp != nullptr);
+ auto* sp = fgets(buf, sizeof(buf), fp);
+ VERIFY(sp != nullptr);
+ fclose(fp);
+ unlink("/tmp/flushtest");
+
+ EXPECT_EQ(strcmp(test_str, buf), 0);
+ }
+}