aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2016-01-26 13:04:57 -0800
committerElliott Hughes <enh@google.com>2016-01-26 13:13:52 -0800
commit33697a0c43c48e15c3bcf018138b9b837d0099cd (patch)
tree5abb2ab07c7f1322defd9fd6313cd11e12978579
parent94bb0fab931102f1fcf393aa507a6b77b741844f (diff)
downloadandroid_bionic-33697a0c43c48e15c3bcf018138b9b837d0099cd.tar.gz
android_bionic-33697a0c43c48e15c3bcf018138b9b837d0099cd.tar.bz2
android_bionic-33697a0c43c48e15c3bcf018138b9b837d0099cd.zip
Factor out the waiting for children in bionic tests.
Change-Id: I4a1e51b6920b33dc892d447f5bd6d10f1cb2704a
-rw-r--r--tests/dlext_test.cpp18
-rw-r--r--tests/pthread_dlfcn_test.cpp9
-rwxr-xr-xtests/pthread_test.cpp11
-rw-r--r--tests/pty_test.cpp7
-rw-r--r--tests/stdlib_test.cpp19
-rw-r--r--tests/sys_select_test.cpp5
-rw-r--r--tests/time_test.cpp8
-rw-r--r--tests/unistd_test.cpp31
-rw-r--r--tests/utils.h10
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<CHILDREN; ++i) {
char read_buf;
@@ -599,7 +596,7 @@ void DlExtRelroSharingTest::SpawnChildrenAndMeasurePss(const char* lib, bool sha
close(child_done_pipe[0]);
// save the child's pid and the parent_done_pipe
- childpid[i] = child;
+ child_pids[i] = child;
childpipe[i] = parent_done_pipe[1];
}
@@ -607,7 +604,7 @@ void DlExtRelroSharingTest::SpawnChildrenAndMeasurePss(const char* lib, bool sha
size_t total_pss = 0;
for (int i=0; i<CHILDREN; ++i) {
size_t child_pss;
- ASSERT_NO_FATAL_FAILURE(getPss(childpid[i], &child_pss));
+ ASSERT_NO_FATAL_FAILURE(getPss(child_pids[i], &child_pss));
total_pss += child_pss;
}
*pss_out = total_pss;
@@ -616,11 +613,8 @@ void DlExtRelroSharingTest::SpawnChildrenAndMeasurePss(const char* lib, bool sha
for (int i=0; i<CHILDREN; ++i) {
ASSERT_NOERROR(close(childpipe[i]));
}
- for (int i=0; i<CHILDREN; ++i) {
- int status;
- ASSERT_EQ(childpid[i], waitpid(childpid[i], &status, 0));
- ASSERT_TRUE(WIFEXITED(status));
- ASSERT_EQ(0, WEXITSTATUS(status));
+ for (int i = 0; i < CHILDREN; ++i) {
+ AssertChildExited(child_pids[i], 0);
}
}
diff --git a/tests/pthread_dlfcn_test.cpp b/tests/pthread_dlfcn_test.cpp
index 5e8b2061e..64423dada 100644
--- a/tests/pthread_dlfcn_test.cpp
+++ b/tests/pthread_dlfcn_test.cpp
@@ -17,6 +17,8 @@
#include <dlfcn.h>
+#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 <pty.h>
#include <sys/ioctl.h>
+#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 <gtest/gtest.h>
+
#include "BionicDeathTest.h"
#include "TemporaryFile.h"
+#include "utils.h"
#include <errno.h>
#include <libgen.h>
@@ -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 <sys/types.h>
#include <sys/wait.h>
+#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<ssize_t>(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 <atomic>
#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 <gtest/gtest.h>
+
#include "BionicDeathTest.h"
#include "ScopedSignalHandler.h"
#include "TemporaryFile.h"
+#include "utils.h"
#include <errno.h>
#include <fcntl.h>
@@ -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 <inttypes.h>
#include <sys/mman.h>
+#include <sys/types.h>
+#include <sys/wait.h>
#include <unistd.h>
#include <atomic>
@@ -109,4 +112,11 @@ static inline void WaitUntilThreadSleep(std::atomic<pid_t>& 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