1 | //===-- Unittests for posix_spwan_file_actions_t manipulation -------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "src/spawn/file_actions.h" |
10 | #include "src/spawn/posix_spawn_file_actions_addclose.h" |
11 | #include "src/spawn/posix_spawn_file_actions_adddup2.h" |
12 | #include "src/spawn/posix_spawn_file_actions_addopen.h" |
13 | #include "src/spawn/posix_spawn_file_actions_destroy.h" |
14 | #include "src/spawn/posix_spawn_file_actions_init.h" |
15 | #include "test/UnitTest/Test.h" |
16 | |
17 | #include <errno.h> |
18 | #include <spawn.h> |
19 | #include <stdint.h> |
20 | |
21 | TEST(LlvmLibcPosixSpawnFileActionsTest, AddActions) { |
22 | posix_spawn_file_actions_t actions; |
23 | ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_init(&actions), 0); |
24 | |
25 | ASSERT_EQ(uintptr_t(actions.__front), uintptr_t(nullptr)); |
26 | ASSERT_EQ(uintptr_t(actions.__back), uintptr_t(nullptr)); |
27 | |
28 | ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_addclose(&actions, 10), 0); |
29 | ASSERT_NE(uintptr_t(actions.__front), uintptr_t(nullptr)); |
30 | ASSERT_NE(uintptr_t(actions.__back), uintptr_t(nullptr)); |
31 | |
32 | ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_adddup2(&actions, 11, 12), |
33 | 0); |
34 | ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_addopen( |
35 | &actions, 13, "path/to/file" , 0, 0), |
36 | 0); |
37 | |
38 | LIBC_NAMESPACE::BaseSpawnFileAction *act = |
39 | reinterpret_cast<LIBC_NAMESPACE::BaseSpawnFileAction *>(actions.__front); |
40 | int action_count = 0; |
41 | while (act != nullptr) { |
42 | ++action_count; |
43 | if (action_count == 1) { |
44 | ASSERT_EQ(act->type, LIBC_NAMESPACE::BaseSpawnFileAction::CLOSE); |
45 | } |
46 | if (action_count == 2) { |
47 | ASSERT_EQ(act->type, LIBC_NAMESPACE::BaseSpawnFileAction::DUP2); |
48 | } |
49 | if (action_count == 3) { |
50 | ASSERT_EQ(act->type, LIBC_NAMESPACE::BaseSpawnFileAction::OPEN); |
51 | } |
52 | act = act->next; |
53 | } |
54 | ASSERT_EQ(action_count, 3); |
55 | ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_destroy(&actions), 0); |
56 | } |
57 | |
58 | TEST(LlvmLibcPosixSpawnFileActionsTest, InvalidActions) { |
59 | ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_addclose(nullptr, 1), |
60 | EINVAL); |
61 | ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_adddup2(nullptr, 1, 2), |
62 | EINVAL); |
63 | ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_addopen(nullptr, 1, |
64 | nullptr, 0, 0), |
65 | EINVAL); |
66 | ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_destroy(nullptr), EINVAL); |
67 | |
68 | posix_spawn_file_actions_t actions; |
69 | ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_init(&actions), 0); |
70 | ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_addclose(&actions, -1), |
71 | EBADF); |
72 | ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_adddup2(&actions, -1, 2), |
73 | EBADF); |
74 | ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_adddup2(&actions, 1, -2), |
75 | EBADF); |
76 | ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_addopen(&actions, -1, |
77 | nullptr, 0, 0), |
78 | EBADF); |
79 | ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_destroy(&actions), 0); |
80 | } |
81 | |