Factor out the waiting for children in bionic tests.

Change-Id: I4a1e51b6920b33dc892d447f5bd6d10f1cb2704a
This commit is contained in:
Elliott Hughes 2016-01-26 13:04:57 -08:00
parent 94bb0fab93
commit 33697a0c43
9 changed files with 48 additions and 70 deletions

View File

@ -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);
}
}

View File

@ -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);
}

View File

@ -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) {

View File

@ -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);
}

View File

@ -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) {

View File

@ -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) {

View File

@ -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));
}

View File

@ -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.

View File

@ -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