xref: /llvm-project/libc/test/src/spawn/posix_spawn_file_actions_test.cpp (revision 46944b0cbc9a9d8daad0182c40fcd3560bc9ca35)
128943d61SSiva Chandra Reddy //===-- Unittests for posix_spwan_file_actions_t manipulation -------------===//
228943d61SSiva Chandra Reddy //
328943d61SSiva Chandra Reddy // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
428943d61SSiva Chandra Reddy // See https://llvm.org/LICENSE.txt for license information.
528943d61SSiva Chandra Reddy // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
628943d61SSiva Chandra Reddy //
728943d61SSiva Chandra Reddy //===----------------------------------------------------------------------===//
828943d61SSiva Chandra Reddy 
9*46944b0cSJob Henandez Lara #include "src/errno/libc_errno.h"
1028943d61SSiva Chandra Reddy #include "src/spawn/file_actions.h"
1128943d61SSiva Chandra Reddy #include "src/spawn/posix_spawn_file_actions_addclose.h"
1228943d61SSiva Chandra Reddy #include "src/spawn/posix_spawn_file_actions_adddup2.h"
1328943d61SSiva Chandra Reddy #include "src/spawn/posix_spawn_file_actions_addopen.h"
1428943d61SSiva Chandra Reddy #include "src/spawn/posix_spawn_file_actions_destroy.h"
1528943d61SSiva Chandra Reddy #include "src/spawn/posix_spawn_file_actions_init.h"
16af1315c2SSiva Chandra Reddy #include "test/UnitTest/Test.h"
1728943d61SSiva Chandra Reddy 
1828943d61SSiva Chandra Reddy #include <spawn.h>
1928943d61SSiva Chandra Reddy #include <stdint.h>
2028943d61SSiva Chandra Reddy 
2128943d61SSiva Chandra Reddy TEST(LlvmLibcPosixSpawnFileActionsTest, AddActions) {
2228943d61SSiva Chandra Reddy   posix_spawn_file_actions_t actions;
23b6bc9d72SGuillaume Chatelet   ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_init(&actions), 0);
2428943d61SSiva Chandra Reddy 
2528943d61SSiva Chandra Reddy   ASSERT_EQ(uintptr_t(actions.__front), uintptr_t(nullptr));
2628943d61SSiva Chandra Reddy   ASSERT_EQ(uintptr_t(actions.__back), uintptr_t(nullptr));
2728943d61SSiva Chandra Reddy 
28b6bc9d72SGuillaume Chatelet   ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_addclose(&actions, 10), 0);
2928943d61SSiva Chandra Reddy   ASSERT_NE(uintptr_t(actions.__front), uintptr_t(nullptr));
3028943d61SSiva Chandra Reddy   ASSERT_NE(uintptr_t(actions.__back), uintptr_t(nullptr));
3128943d61SSiva Chandra Reddy 
32b6bc9d72SGuillaume Chatelet   ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_adddup2(&actions, 11, 12),
33b6bc9d72SGuillaume Chatelet             0);
34b6bc9d72SGuillaume Chatelet   ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_addopen(
35b6bc9d72SGuillaume Chatelet                 &actions, 13, "path/to/file", 0, 0),
3628943d61SSiva Chandra Reddy             0);
3728943d61SSiva Chandra Reddy 
38b6bc9d72SGuillaume Chatelet   LIBC_NAMESPACE::BaseSpawnFileAction *act =
39b6bc9d72SGuillaume Chatelet       reinterpret_cast<LIBC_NAMESPACE::BaseSpawnFileAction *>(actions.__front);
4028943d61SSiva Chandra Reddy   int action_count = 0;
4128943d61SSiva Chandra Reddy   while (act != nullptr) {
4228943d61SSiva Chandra Reddy     ++action_count;
43714b4c82SMikhail R. Gadelha     if (action_count == 1) {
44b6bc9d72SGuillaume Chatelet       ASSERT_EQ(act->type, LIBC_NAMESPACE::BaseSpawnFileAction::CLOSE);
45714b4c82SMikhail R. Gadelha     }
46714b4c82SMikhail R. Gadelha     if (action_count == 2) {
47b6bc9d72SGuillaume Chatelet       ASSERT_EQ(act->type, LIBC_NAMESPACE::BaseSpawnFileAction::DUP2);
48714b4c82SMikhail R. Gadelha     }
49714b4c82SMikhail R. Gadelha     if (action_count == 3) {
50b6bc9d72SGuillaume Chatelet       ASSERT_EQ(act->type, LIBC_NAMESPACE::BaseSpawnFileAction::OPEN);
51714b4c82SMikhail R. Gadelha     }
5228943d61SSiva Chandra Reddy     act = act->next;
5328943d61SSiva Chandra Reddy   }
5428943d61SSiva Chandra Reddy   ASSERT_EQ(action_count, 3);
55b6bc9d72SGuillaume Chatelet   ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_destroy(&actions), 0);
5628943d61SSiva Chandra Reddy }
5728943d61SSiva Chandra Reddy 
5828943d61SSiva Chandra Reddy TEST(LlvmLibcPosixSpawnFileActionsTest, InvalidActions) {
59b6bc9d72SGuillaume Chatelet   ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_addclose(nullptr, 1),
6028943d61SSiva Chandra Reddy             EINVAL);
61b6bc9d72SGuillaume Chatelet   ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_adddup2(nullptr, 1, 2),
6228943d61SSiva Chandra Reddy             EINVAL);
63b6bc9d72SGuillaume Chatelet   ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_addopen(nullptr, 1,
64b6bc9d72SGuillaume Chatelet                                                              nullptr, 0, 0),
65b6bc9d72SGuillaume Chatelet             EINVAL);
66b6bc9d72SGuillaume Chatelet   ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_destroy(nullptr), EINVAL);
6728943d61SSiva Chandra Reddy 
6828943d61SSiva Chandra Reddy   posix_spawn_file_actions_t actions;
69b6bc9d72SGuillaume Chatelet   ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_init(&actions), 0);
70b6bc9d72SGuillaume Chatelet   ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_addclose(&actions, -1),
7128943d61SSiva Chandra Reddy             EBADF);
72b6bc9d72SGuillaume Chatelet   ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_adddup2(&actions, -1, 2),
7328943d61SSiva Chandra Reddy             EBADF);
74b6bc9d72SGuillaume Chatelet   ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_adddup2(&actions, 1, -2),
7528943d61SSiva Chandra Reddy             EBADF);
76b6bc9d72SGuillaume Chatelet   ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_addopen(&actions, -1,
77b6bc9d72SGuillaume Chatelet                                                              nullptr, 0, 0),
7828943d61SSiva Chandra Reddy             EBADF);
79b6bc9d72SGuillaume Chatelet   ASSERT_EQ(LIBC_NAMESPACE::posix_spawn_file_actions_destroy(&actions), 0);
8028943d61SSiva Chandra Reddy }
81