From 33697a0c43c48e15c3bcf018138b9b837d0099cd Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Tue, 26 Jan 2016 13:04:57 -0800 Subject: [PATCH] Factor out the waiting for children in bionic tests. Change-Id: I4a1e51b6920b33dc892d447f5bd6d10f1cb2704a --- tests/dlext_test.cpp | 18 ++++++------------ tests/pthread_dlfcn_test.cpp | 9 +++++---- tests/pthread_test.cpp | 11 +++-------- tests/pty_test.cpp | 7 +++---- tests/stdlib_test.cpp | 19 ++++++------------- tests/sys_select_test.cpp | 5 ++++- tests/time_test.cpp | 8 +++----- tests/unistd_test.cpp | 31 ++++++++----------------------- tests/utils.h | 10 ++++++++++ 9 files changed, 48 insertions(+), 70 deletions(-) diff --git a/tests/dlext_test.cpp b/tests/dlext_test.cpp index c221402a7..c64ec159f 100644 --- a/tests/dlext_test.cpp +++ b/tests/dlext_test.cpp @@ -442,10 +442,7 @@ protected: // continuing in parent ASSERT_NOERROR(close(relro_fd)); ASSERT_NOERROR(pid); - int status; - ASSERT_EQ(pid, waitpid(pid, &status, 0)); - ASSERT_TRUE(WIFEXITED(status)); - ASSERT_EQ(0, WEXITSTATUS(status)); + AssertChildExited(pid, 0); // reopen file for reading so it can be used relro_fd = open(relro_file, O_RDONLY); @@ -554,7 +551,7 @@ void DlExtRelroSharingTest::SpawnChildrenAndMeasurePss(const char* lib, bool sha const int CHILDREN = 20; // Create children - pid_t childpid[CHILDREN]; + pid_t child_pids[CHILDREN]; int childpipe[CHILDREN]; for (int i=0; i +#include "utils.h" + static int g_atfork_prepare_calls = 0; static void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 1; } static void AtForkPrepare2() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 2; } @@ -49,7 +51,7 @@ TEST(pthread, pthread_atfork_with_dlclose) { ASSERT_EQ(0, pthread_atfork(AtForkPrepare4, AtForkParent4, AtForkChild4)); - int pid = fork(); + pid_t pid = fork(); ASSERT_NE(-1, pid) << strerror(errno); @@ -64,8 +66,7 @@ TEST(pthread, pthread_atfork_with_dlclose) { EXPECT_EQ(0, dlclose(handle)); g_atfork_prepare_calls = g_atfork_parent_calls = g_atfork_child_calls = 0; - int status; - ASSERT_EQ(pid, waitpid(pid, &status, 0)); + AssertChildExited(pid, 0); pid = fork(); @@ -79,5 +80,5 @@ TEST(pthread, pthread_atfork_with_dlclose) { ASSERT_EQ(14, g_atfork_parent_calls); ASSERT_EQ(41, g_atfork_prepare_calls); - ASSERT_EQ(pid, waitpid(pid, &status, 0)); + AssertChildExited(pid, 0); } diff --git a/tests/pthread_test.cpp b/tests/pthread_test.cpp index 78dbd3972..d11ea3f7b 100755 --- a/tests/pthread_test.cpp +++ b/tests/pthread_test.cpp @@ -37,7 +37,6 @@ #include "private/ScopeGuard.h" #include "BionicDeathTest.h" #include "ScopedSignalHandler.h" - #include "utils.h" TEST(pthread, pthread_key_create) { @@ -142,10 +141,7 @@ TEST(pthread, pthread_key_fork) { _exit(99); } - int status; - ASSERT_EQ(pid, waitpid(pid, &status, 0)); - ASSERT_TRUE(WIFEXITED(status)); - ASSERT_EQ(99, WEXITSTATUS(status)); + AssertChildExited(pid, 99); ASSERT_EQ(expected, pthread_getspecific(key)); ASSERT_EQ(0, pthread_key_delete(key)); @@ -1038,7 +1034,7 @@ TEST(pthread, pthread_atfork_smoke) { ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1)); ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2)); - int pid = fork(); + pid_t pid = fork(); ASSERT_NE(-1, pid) << strerror(errno); // Child and parent calls are made in the order they were registered. @@ -1050,8 +1046,7 @@ TEST(pthread, pthread_atfork_smoke) { // Prepare calls are made in the reverse order. ASSERT_EQ(21, g_atfork_prepare_calls); - int status; - ASSERT_EQ(pid, waitpid(pid, &status, 0)); + AssertChildExited(pid, 0); } TEST(pthread, pthread_attr_getscope) { diff --git a/tests/pty_test.cpp b/tests/pty_test.cpp index 7fe97e9af..91d1f5eec 100644 --- a/tests/pty_test.cpp +++ b/tests/pty_test.cpp @@ -19,6 +19,8 @@ #include #include +#include "utils.h" + TEST(pty, openpty) { int master, slave; char name[32]; @@ -58,10 +60,7 @@ TEST(pty, forkpty) { ASSERT_EQ(sid, getsid(0)); - int status; - ASSERT_EQ(pid, waitpid(pid, &status, 0)); - ASSERT_TRUE(WIFEXITED(status)); - ASSERT_EQ(0, WEXITSTATUS(status)); + AssertChildExited(pid, 0); close(master); } diff --git a/tests/stdlib_test.cpp b/tests/stdlib_test.cpp index 050f5a766..6ae6cda74 100644 --- a/tests/stdlib_test.cpp +++ b/tests/stdlib_test.cpp @@ -15,8 +15,10 @@ */ #include + #include "BionicDeathTest.h" #include "TemporaryFile.h" +#include "utils.h" #include #include @@ -323,10 +325,7 @@ TEST(stdlib, quick_exit) { quick_exit(99); } - int status; - ASSERT_EQ(pid, waitpid(pid, &status, 0)); - ASSERT_TRUE(WIFEXITED(status)); - ASSERT_EQ(99, WEXITSTATUS(status)); + AssertChildExited(pid, 99); } static int quick_exit_status = 0; @@ -355,24 +354,18 @@ TEST(stdlib, at_quick_exit) { quick_exit(99); } - int status; - ASSERT_EQ(pid, waitpid(pid, &status, 0)); - ASSERT_TRUE(WIFEXITED(status)); - ASSERT_EQ(99, WEXITSTATUS(status)); + AssertChildExited(pid, 99); } TEST(unistd, _Exit) { - int pid = fork(); + pid_t pid = fork(); ASSERT_NE(-1, pid) << strerror(errno); if (pid == 0) { _Exit(99); } - int status; - ASSERT_EQ(pid, waitpid(pid, &status, 0)); - ASSERT_TRUE(WIFEXITED(status)); - ASSERT_EQ(99, WEXITSTATUS(status)); + AssertChildExited(pid, 99); } TEST(stdlib, pty_smoke) { diff --git a/tests/sys_select_test.cpp b/tests/sys_select_test.cpp index d4ac333d2..4ad77f087 100644 --- a/tests/sys_select_test.cpp +++ b/tests/sys_select_test.cpp @@ -23,6 +23,8 @@ #include #include +#include "utils.h" + TEST(sys_select, fd_set_smoke) { fd_set fds; FD_ZERO(&fds); @@ -68,7 +70,8 @@ static void DelayedWriteCleanup(int pid, int fd) { char buf[sizeof(DELAY_MSG)]; ASSERT_EQ(static_cast(sizeof(DELAY_MSG)), read(fd, buf, sizeof(DELAY_MSG))); ASSERT_STREQ(DELAY_MSG, buf); - ASSERT_EQ(pid, waitpid(pid, NULL, 0)); + + AssertChildExited(pid, 0); } TEST(sys_select, select_smoke) { diff --git a/tests/time_test.cpp b/tests/time_test.cpp index a04c44976..ec1b54908 100644 --- a/tests/time_test.cpp +++ b/tests/time_test.cpp @@ -27,6 +27,7 @@ #include #include "ScopedSignalHandler.h" +#include "utils.h" #include "private/bionic_constants.h" @@ -218,7 +219,7 @@ TEST(time, timer_create) { timer_t timer_id; ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id)); - int pid = fork(); + pid_t pid = fork(); ASSERT_NE(-1, pid) << strerror(errno); if (pid == 0) { @@ -228,10 +229,7 @@ TEST(time, timer_create) { _exit(0); } - int status; - ASSERT_EQ(pid, waitpid(pid, &status, 0)); - ASSERT_TRUE(WIFEXITED(status)); - ASSERT_EQ(0, WEXITSTATUS(status)); + AssertChildExited(pid, 0); ASSERT_EQ(0, timer_delete(timer_id)); } diff --git a/tests/unistd_test.cpp b/tests/unistd_test.cpp index 5ef54a4ec..62e39fd45 100644 --- a/tests/unistd_test.cpp +++ b/tests/unistd_test.cpp @@ -15,9 +15,11 @@ */ #include + #include "BionicDeathTest.h" #include "ScopedSignalHandler.h" #include "TemporaryFile.h" +#include "utils.h" #include #include @@ -245,17 +247,14 @@ TEST(UNISTD_TEST, alarm) { } TEST(UNISTD_TEST, _exit) { - int pid = fork(); + pid_t pid = fork(); ASSERT_NE(-1, pid) << strerror(errno); if (pid == 0) { _exit(99); } - int status; - ASSERT_EQ(pid, waitpid(pid, &status, 0)); - ASSERT_TRUE(WIFEXITED(status)); - ASSERT_EQ(99, WEXITSTATUS(status)); + AssertChildExited(pid, 99); } TEST(UNISTD_TEST, getenv_unsetenv) { @@ -429,11 +428,7 @@ static void TestGetPidCachingWithFork(int (*fork_fn)()) { } else { // We're the parent. ASSERT_EQ(parent_pid, getpid()); - - int status; - ASSERT_EQ(fork_result, waitpid(fork_result, &status, 0)); - ASSERT_TRUE(WIFEXITED(status)); - ASSERT_EQ(123, WEXITSTATUS(status)); + AssertChildExited(fork_result, 123); } } @@ -464,10 +459,7 @@ TEST(UNISTD_TEST, getpid_caching_and_clone) { ASSERT_EQ(parent_pid, getpid()); - int status; - ASSERT_EQ(clone_result, waitpid(clone_result, &status, 0)); - ASSERT_TRUE(WIFEXITED(status)); - ASSERT_EQ(123, WEXITSTATUS(status)); + AssertChildExited(clone_result, 123); } static void* GetPidCachingPthreadStartRoutine(void*) { @@ -873,13 +865,6 @@ TEST(UNISTD_TEST, dup2_same) { ASSERT_EQ(EBADF, errno); } -static void WaitForChildExit() { - int status; - wait(&status); - ASSERT_TRUE(WIFEXITED(status)); - ASSERT_EQ(0, WEXITSTATUS(status)); -} - TEST(UNISTD_TEST, lockf_smoke) { constexpr off64_t file_size = 32*1024LL; @@ -964,7 +949,7 @@ TEST(UNISTD_TEST, lockf_with_child) { ASSERT_EQ(EACCES, errno); _exit(0); } - WaitForChildExit(); + AssertChildExited(pid, 0); } TEST(UNISTD_TEST, lockf_partial_with_child) { @@ -994,7 +979,7 @@ TEST(UNISTD_TEST, lockf_partial_with_child) { ASSERT_EQ(EACCES, errno); _exit(0); } - WaitForChildExit(); + AssertChildExited(pid, 0); // The second half was locked by the child, but the lock disappeared // when the process exited, so check it can be locked now. diff --git a/tests/utils.h b/tests/utils.h index a8f3441da..828c8d052 100644 --- a/tests/utils.h +++ b/tests/utils.h @@ -16,8 +16,11 @@ #ifndef __TEST_UTILS_H #define __TEST_UTILS_H + #include #include +#include +#include #include #include @@ -109,4 +112,11 @@ static inline void WaitUntilThreadSleep(std::atomic& tid) { } } +static inline void AssertChildExited(int pid, int expected_exit_status) { + int status; + ASSERT_EQ(pid, waitpid(pid, &status, 0)); + ASSERT_TRUE(WIFEXITED(status)); + ASSERT_EQ(expected_exit_status, WEXITSTATUS(status)); +} + #endif -- 2.11.0