mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-11 13:36:13 +03:00
Tests: Test whether stdio streams are flushed correctly on exit
This commit is contained in:
parent
a1dfa1efb2
commit
6e00dd64a1
Notes:
sideshowbarker
2024-07-18 04:38:32 +09:00
Author: https://github.com/BertalanD Commit: https://github.com/SerenityOS/serenity/commit/6e00dd64a1a Pull-request: https://github.com/SerenityOS/serenity/pull/11907 Reviewed-by: https://github.com/Quaker762 Reviewed-by: https://github.com/bgianfo
@ -18,6 +18,7 @@ set(TEST_SOURCES
|
||||
TestSearch.cpp
|
||||
TestSnprintf.cpp
|
||||
TestStackSmash.cpp
|
||||
TestStdio.cpp
|
||||
TestStrlcpy.cpp
|
||||
TestStrtodAccuracy.cpp
|
||||
TestWchar.cpp
|
||||
|
42
Tests/LibC/TestStdio.cpp
Normal file
42
Tests/LibC/TestStdio.cpp
Normal file
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user