pytrainer: unpin python 3.10
[NixPkgs.git] / pkgs / build-support / libredirect / test.c
blobe5685fcb31b71b6a532cea3358c4b5182f6235bf
1 #include <assert.h>
2 #include <errno.h>
3 #include <fcntl.h>
4 #include <limits.h>
5 #include <spawn.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
11 #include <sys/stat.h>
12 #include <sys/types.h>
13 #include <sys/wait.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) {
22 pid_t pid;
23 int ret;
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);
31 assert(ret == 0);
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
51 // should segfault.
52 struct stat buf;
53 #pragma GCC diagnostic push
54 #pragma GCC diagnostic ignored "-Wnonnull"
55 stat(NULL, &buf);
56 #pragma GCC diagnostic pop
59 void assert_mktemp_path(
60 const char * orig_prefix,
61 const char * orig_suffix,
62 const char * updated
63 ) {
64 // prefix unchanged
65 assert(strncmp(updated, orig_prefix, strlen(orig_prefix)) == 0);
66 // wildcards replaced
67 assert(strcmp(updated + strlen(orig_prefix), "XXXXXX") != 0);
68 // suffix unchanged
69 assert(strcmp(updated + strlen(orig_prefix) + 6, orig_suffix) == 0);
72 int main(int argc, char *argv[])
74 FILE *testfp;
75 int testfd;
76 struct stat testsb;
77 #ifdef __GLIBC__
78 struct stat64 testsb64;
79 #endif
80 #if defined(__linux__) && defined(STATX_TYPE)
81 struct statx testsbx;
82 #endif
83 char buf[PATH_MAX];
85 testfp = fopen(TESTPATH, "r");
86 assert(testfp != NULL);
87 fclose(testfp);
89 testfd = open(TESTPATH, O_RDONLY);
90 assert(testfd != -1);
91 close(testfd);
93 assert(access(TESTPATH, X_OK) == 0);
95 assert(stat(TESTPATH, &testsb) != -1);
96 #ifdef __GLIBC__
97 assert(stat64(TESTPATH, &testsb64) != -1);
98 #endif
99 assert(fstatat(123, TESTPATH, &testsb, 0) != -1);
100 #ifdef __GLIBC__
101 assert(fstatat64(123, TESTPATH, &testsb64, 0) != -1);
102 #endif
103 #if defined(__linux__) && defined(STATX_TYPE)
104 assert(statx(123, TESTPATH, 0, STATX_ALL, &testsbx) != -1);
105 #endif
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!
113 #ifndef __APPLE__
114 assert(errno == EISDIR);
115 #endif
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!
122 #ifndef __APPLE__
123 assert(errno == EISDIR);
124 #endif
125 assert(unlinkat(123, TESTDIR "/dir-mkdirat", AT_REMOVEDIR) == 0);
127 strncpy(buf, TESTDIR "/tempXXXXXX", PATH_MAX);
128 testfd = mkstemp(buf);
129 assert(testfd > 0);
130 assert_mktemp_path(TESTDIR "/temp", "", buf);
131 close(testfd);
133 strncpy(buf, TESTDIR "/tempXXXXXX", PATH_MAX);
134 testfd = mkostemp(buf, 0);
135 assert(testfd > 0);
136 assert_mktemp_path(TESTDIR "/temp", "", buf);
137 close(testfd);
139 strncpy(buf, TESTDIR "/tempXXXXXX.test", PATH_MAX);
140 testfd = mkstemps(buf, strlen(".test"));
141 assert(testfd > 0);
142 assert_mktemp_path(TESTDIR "/temp", ".test", buf);
143 close(testfd);
145 strncpy(buf, TESTDIR "/tempXXXXXX.test", PATH_MAX);
146 testfd = mkostemps(buf, strlen(".test"), 0);
147 assert(testfd > 0);
148 assert_mktemp_path(TESTDIR "/temp", ".test", buf);
149 close(testfd);
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);
159 test_spawn();
160 test_system();
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
166 if (argc == 1) {
167 test_subprocess();
170 test_execv();
172 /* If all goes well, this is never reached because test_execv() replaces
173 * the current process.
175 return 0;