James Robinson | c4c1c59 | 2014-11-21 18:27:04 -0800 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "sandbox/linux/services/syscall_wrappers.h" |
| 6 | |
| 7 | #include <sys/syscall.h> |
| 8 | #include <sys/types.h> |
| 9 | #include <sys/wait.h> |
| 10 | #include <unistd.h> |
| 11 | |
| 12 | #include "base/logging.h" |
| 13 | #include "base/posix/eintr_wrapper.h" |
Etienne Membrives | 175837a | 2014-12-19 15:45:38 +0100 | [diff] [blame] | 14 | #include "base/third_party/valgrind/valgrind.h" |
James Robinson | c4c1c59 | 2014-11-21 18:27:04 -0800 | [diff] [blame] | 15 | #include "build/build_config.h" |
| 16 | #include "sandbox/linux/tests/test_utils.h" |
Etienne Membrives | 175837a | 2014-12-19 15:45:38 +0100 | [diff] [blame] | 17 | #include "sandbox/linux/tests/unit_tests.h" |
James Robinson | c4c1c59 | 2014-11-21 18:27:04 -0800 | [diff] [blame] | 18 | #include "testing/gtest/include/gtest/gtest.h" |
| 19 | |
| 20 | namespace sandbox { |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | TEST(SyscallWrappers, BasicSyscalls) { |
| 25 | EXPECT_EQ(getpid(), sys_getpid()); |
| 26 | } |
| 27 | |
| 28 | TEST(SyscallWrappers, CloneBasic) { |
| 29 | pid_t child = sys_clone(SIGCHLD); |
| 30 | TestUtils::HandlePostForkReturn(child); |
| 31 | EXPECT_LT(0, child); |
| 32 | } |
| 33 | |
| 34 | TEST(SyscallWrappers, CloneParentSettid) { |
| 35 | pid_t ptid = 0; |
| 36 | pid_t child = sys_clone(CLONE_PARENT_SETTID | SIGCHLD, nullptr, &ptid, |
| 37 | nullptr, nullptr); |
| 38 | TestUtils::HandlePostForkReturn(child); |
| 39 | EXPECT_LT(0, child); |
| 40 | EXPECT_EQ(child, ptid); |
| 41 | } |
| 42 | |
| 43 | TEST(SyscallWrappers, CloneChildSettid) { |
| 44 | pid_t ctid = 0; |
| 45 | pid_t pid = |
| 46 | sys_clone(CLONE_CHILD_SETTID | SIGCHLD, nullptr, nullptr, &ctid, nullptr); |
| 47 | |
| 48 | const int kSuccessExit = 0; |
| 49 | if (0 == pid) { |
| 50 | // In child. |
| 51 | if (sys_getpid() == ctid) |
| 52 | _exit(kSuccessExit); |
| 53 | _exit(1); |
| 54 | } |
| 55 | |
| 56 | ASSERT_NE(-1, pid); |
| 57 | int status = 0; |
| 58 | ASSERT_EQ(pid, HANDLE_EINTR(waitpid(pid, &status, 0))); |
| 59 | ASSERT_TRUE(WIFEXITED(status)); |
| 60 | EXPECT_EQ(kSuccessExit, WEXITSTATUS(status)); |
| 61 | } |
| 62 | |
James Robinson | c4c1c59 | 2014-11-21 18:27:04 -0800 | [diff] [blame] | 63 | } // namespace |
| 64 | |
| 65 | } // namespace sandbox |