platform_bionic/tests/pidfd_test.cpp
Elliott Hughes 4ae4be9263 Clean up how we skip tests when a syscall isn't in the kernel.
The close_range() test in particular has been confusing a lot of
partners. I think the sys_epoll_test.cpp idiom is the clearest of the
ones in use, so let's use that everywhere. (I haven't actually touched
the SysV IPC tests, because if we do touch them, _deleting_ them --
since all those syscalls are not allowed on Android -- is probably the
change to be made!)

I'm on the fence about factoring this idiom out into a macro. There
should never be too many of these, and we should probably be removing
them? Is anyone still running the current bionic tests on 4.3 kernels
without membarrier(2), and if they are --- why?!

For now though, I haven't removed any of our existing tests; I've just
moved them over to the sys_epoll_test.cpp style.

Test: treehugger
Change-Id: Ie69a0bb8f416c79957188e187610ff8a3c4d1e8f
2023-09-22 17:15:25 -07:00

100 lines
2.9 KiB
C++

/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <gtest/gtest.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#if defined(__BIONIC__)
#include <sys/pidfd.h>
#endif
#include <android-base/silent_death_test.h>
#include <android-base/unique_fd.h>
#include "utils.h"
using android::base::unique_fd;
using namespace std::chrono_literals;
using pidfd_DeathTest = SilentDeathTest;
TEST(pidfd, pidfd_open) {
#if defined(__BIONIC__)
pid_t child = fork();
ASSERT_NE(-1, child);
if (child == 0) {
_exit(42);
}
unique_fd pidfd(pidfd_open(child, 0));
if (pidfd.get() == -1 && errno == ENOSYS) GTEST_SKIP() << "no pidfd_open() in this kernel";
ASSERT_NE(-1, pidfd.get()) << strerror(errno);
siginfo_t siginfo;
int rc = waitid(P_PIDFD, pidfd.get(), &siginfo, WEXITED);
if (rc == -1) {
ASSERT_ERRNO(EINVAL);
GTEST_SKIP() << "P_PIDFD not available";
}
ASSERT_EQ(child, siginfo.si_pid);
#endif
}
TEST(pidfd, pidfd_getfd) {
#if defined(__BIONIC__)
unique_fd r, w;
ASSERT_TRUE(android::base::Pipe(&r, &w));
unique_fd self(pidfd_open(getpid(), 0));
if (self.get() == -1 && errno == ENOSYS) GTEST_SKIP() << "no pidfd_open() in this kernel";
ASSERT_NE(-1, self.get()) << strerror(errno);
unique_fd dup(pidfd_getfd(self.get(), r.get(), 0));
if (dup.get() == -1 && errno == ENOSYS) GTEST_SKIP() << "no pidfd_getfd() in this kernel";
ASSERT_NE(-1, dup.get()) << strerror(errno);
ASSERT_NE(r.get(), dup.get());
ASSERT_EQ(3, write(w.get(), "foo", 3));
char buf[4];
ASSERT_EQ(3, read(dup.get(), buf, sizeof(buf)));
ASSERT_EQ(0, memcmp(buf, "foo", 3));
#endif
}
TEST_F(pidfd_DeathTest, pidfd_send_signal) {
#if defined(__BIONIC__)
unique_fd self(pidfd_open(getpid(), 0));
if (self.get() == -1 && errno == ENOSYS) GTEST_SKIP() << "no pidfd_open() in this kernel";
ASSERT_NE(-1, self.get()) << strerror(errno);
int rc = pidfd_send_signal(self.get(), 0, nullptr, 0);
if (rc == -1 && errno == ENOSYS) GTEST_SKIP() << "no pidfd_send_signal() in this kernel";
ASSERT_EQ(0, rc) << strerror(errno);
ASSERT_EXIT(({
// gtest will fork a child off for ASSERT_EXIT: `self` refers to the parent.
unique_fd child(pidfd_open(getpid(), 0));
pidfd_send_signal(child.get(), SIGINT, nullptr, 0);
}),
testing::KilledBySignal(SIGINT), "");
#endif
}