12 #include <sys/types.h>
15 #define TESTDIR "/bar/baz"
16 #define TESTPATH "/foo/bar/test"
17 #define SUBTEST "./test sub"
19 extern char **environ
;
21 void test_spawn(void) {
24 posix_spawn_file_actions_t file_actions
;
25 char *argv
[] = {"true", NULL
};
27 assert(posix_spawn_file_actions_init(&file_actions
) == 0);
29 ret
= posix_spawn(&pid
, TESTPATH
, &file_actions
, NULL
, argv
, environ
);
32 assert(waitpid(pid
, NULL
, 0) != -1);
35 void test_execv(void) {
36 char *argv
[] = {"true", NULL
};
37 assert(execv(TESTPATH
, argv
) == 0);
40 void test_system(void) {
41 assert(system(TESTPATH
) == 0);
44 void test_subprocess(void) {
45 assert(system(SUBTEST
) == 0);
48 void test_stat_with_null_path(void) {
49 // This checks whether the compiler optimizes away the null pointer check
50 // on the path passed to stat(). If that's the case, the following code
53 #pragma GCC diagnostic push
54 #pragma GCC diagnostic ignored "-Wnonnull"
56 #pragma GCC diagnostic pop
59 void assert_mktemp_path(
60 const char * orig_prefix
,
61 const char * orig_suffix
,
65 assert(strncmp(updated
, orig_prefix
, strlen(orig_prefix
)) == 0);
67 assert(strcmp(updated
+ strlen(orig_prefix
), "XXXXXX") != 0);
69 assert(strcmp(updated
+ strlen(orig_prefix
) + 6, orig_suffix
) == 0);
72 int main(int argc
, char *argv
[])
78 struct stat64 testsb64
;
80 #if defined(__linux__) && defined(STATX_TYPE)
85 testfp
= fopen(TESTPATH
, "r");
86 assert(testfp
!= NULL
);
89 testfd
= open(TESTPATH
, O_RDONLY
);
93 assert(access(TESTPATH
, X_OK
) == 0);
95 assert(stat(TESTPATH
, &testsb
) != -1);
97 assert(stat64(TESTPATH
, &testsb64
) != -1);
99 assert(fstatat(123, TESTPATH
, &testsb
, 0) != -1);
101 assert(fstatat64(123, TESTPATH
, &testsb64
, 0) != -1);
103 #if defined(__linux__) && defined(STATX_TYPE)
104 assert(statx(123, TESTPATH
, 0, STATX_ALL
, &testsbx
) != -1);
107 assert(getcwd(buf
, PATH_MAX
) != NULL
);
108 assert(chdir(TESTDIR
) == 0);
109 assert(chdir(buf
) == 0);
111 assert(mkdir(TESTDIR
"/dir-mkdir", 0777) == 0);
112 assert(unlink(TESTDIR
"/dir-mkdir") == -1); // it's a directory!
114 assert(errno
== EISDIR
);
116 assert(rmdir(TESTDIR
"/dir-mkdir") == 0);
117 assert(unlink(TESTDIR
"/dir-mkdir") == -1);
118 assert(errno
== ENOENT
);
120 assert(mkdirat(123, TESTDIR
"/dir-mkdirat", 0777) == 0);
121 assert(unlinkat(123, TESTDIR
"/dir-mkdirat", 0) == -1); // it's a directory!
123 assert(errno
== EISDIR
);
125 assert(unlinkat(123, TESTDIR
"/dir-mkdirat", AT_REMOVEDIR
) == 0);
127 strncpy(buf
, TESTDIR
"/tempXXXXXX", PATH_MAX
);
128 testfd
= mkstemp(buf
);
130 assert_mktemp_path(TESTDIR
"/temp", "", buf
);
133 strncpy(buf
, TESTDIR
"/tempXXXXXX", PATH_MAX
);
134 testfd
= mkostemp(buf
, 0);
136 assert_mktemp_path(TESTDIR
"/temp", "", buf
);
139 strncpy(buf
, TESTDIR
"/tempXXXXXX.test", PATH_MAX
);
140 testfd
= mkstemps(buf
, strlen(".test"));
142 assert_mktemp_path(TESTDIR
"/temp", ".test", buf
);
145 strncpy(buf
, TESTDIR
"/tempXXXXXX.test", PATH_MAX
);
146 testfd
= mkostemps(buf
, strlen(".test"), 0);
148 assert_mktemp_path(TESTDIR
"/temp", ".test", buf
);
151 strncpy(buf
, TESTDIR
"/tempXXXXXX", PATH_MAX
);
152 assert(mkdtemp(buf
) == buf
);
153 assert_mktemp_path(TESTDIR
"/temp", "", buf
);
155 strncpy(buf
, TESTDIR
"/tempXXXXXX", PATH_MAX
);
156 assert(mktemp(buf
) == buf
);
157 assert_mktemp_path(TESTDIR
"/temp", "", buf
);
161 test_stat_with_null_path();
163 // Only run subprocess if no arguments are given
164 // as the subprocess will be called without argument
165 // otherwise we will have infinite recursion
172 /* If all goes well, this is never reached because test_execv() replaces
173 * the current process.