about summary refs log tree commit diff
path: root/nixpkgs/pkgs/build-support/libredirect/test.c
blob: 20b27759f019fb862e64d38539458867e4e4a251 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <spawn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>

#define TESTDIR "/bar/baz"
#define TESTPATH "/foo/bar/test"
#define SUBTEST "./test sub"

extern char **environ;

void test_spawn(void) {
    pid_t pid;
    int ret;
    posix_spawn_file_actions_t file_actions;
    char *argv[] = {"true", NULL};

    assert(posix_spawn_file_actions_init(&file_actions) == 0);

    ret = posix_spawn(&pid, TESTPATH, &file_actions, NULL, argv, environ);

    assert(ret == 0);
    assert(waitpid(pid, NULL, 0) != -1);
}

void test_execv(void) {
    char *argv[] = {"true", NULL};
    assert(execv(TESTPATH, argv) == 0);
}

void test_system(void) {
    assert(system(TESTPATH) == 0);
}

void test_subprocess(void) {
    assert(system(SUBTEST) == 0);
}

void test_stat_with_null_path(void) {
    // This checks whether the compiler optimizes away the null pointer check
    // on the path passed to stat(). If that's the case, the following code
    // should segfault.
    struct stat buf;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wnonnull"
    stat(NULL, &buf);
#pragma GCC diagnostic pop
}

void assert_mktemp_path(
    const char * orig_prefix,
    const char * orig_suffix,
    const char * updated
) {
    // prefix unchanged
    assert(strncmp(updated, orig_prefix, strlen(orig_prefix)) == 0);
    // wildcards replaced
    assert(strcmp(updated + strlen(orig_prefix), "XXXXXX") != 0);
    // suffix unchanged
    assert(strcmp(updated + strlen(orig_prefix) + 6, orig_suffix) == 0);
}

int main(int argc, char *argv[])
{
    FILE *testfp;
    int testfd;
    struct stat testsb;
#ifndef __APPLE__
    struct stat64 testsb64;
#endif
#if defined(__linux__) && defined(STATX_TYPE)
    struct statx testsbx;
#endif
    char buf[PATH_MAX];

    testfp = fopen(TESTPATH, "r");
    assert(testfp != NULL);
    fclose(testfp);

    testfd = open(TESTPATH, O_RDONLY);
    assert(testfd != -1);
    close(testfd);

    assert(access(TESTPATH, X_OK) == 0);

    assert(stat(TESTPATH, &testsb) != -1);
#ifndef __APPLE__
    assert(stat64(TESTPATH, &testsb64) != -1);
#endif
    assert(fstatat(123, TESTPATH, &testsb, 0) != -1);
#ifndef __APPLE__
    assert(fstatat64(123, TESTPATH, &testsb64, 0) != -1);
#endif
#if defined(__linux__) && defined(STATX_TYPE)
    assert(statx(123, TESTPATH, 0, STATX_ALL, &testsbx) != -1);
#endif

    assert(getcwd(buf, PATH_MAX) != NULL);
    assert(chdir(TESTDIR) == 0);
    assert(chdir(buf) == 0);

    assert(mkdir(TESTDIR "/dir-mkdir", 0777) == 0);
    assert(unlink(TESTDIR "/dir-mkdir") == -1); // it's a directory!
#ifndef __APPLE__
    assert(errno == EISDIR);
#endif
    assert(rmdir(TESTDIR "/dir-mkdir") == 0);
    assert(unlink(TESTDIR "/dir-mkdir") == -1);
    assert(errno == ENOENT);

    assert(mkdirat(123, TESTDIR "/dir-mkdirat", 0777) == 0);
    assert(unlinkat(123, TESTDIR "/dir-mkdirat", 0) == -1); // it's a directory!
#ifndef __APPLE__
    assert(errno == EISDIR);
#endif
    assert(unlinkat(123, TESTDIR "/dir-mkdirat", AT_REMOVEDIR) == 0);

    strncpy(buf, TESTDIR "/tempXXXXXX", PATH_MAX);
    testfd = mkstemp(buf);
    assert(testfd > 0);
    assert_mktemp_path(TESTDIR "/temp", "", buf);
    close(testfd);

    strncpy(buf, TESTDIR "/tempXXXXXX", PATH_MAX);
    testfd = mkostemp(buf, 0);
    assert(testfd > 0);
    assert_mktemp_path(TESTDIR "/temp", "", buf);
    close(testfd);

    strncpy(buf, TESTDIR "/tempXXXXXX.test", PATH_MAX);
    testfd = mkstemps(buf, strlen(".test"));
    assert(testfd > 0);
    assert_mktemp_path(TESTDIR "/temp", ".test", buf);
    close(testfd);

    strncpy(buf, TESTDIR "/tempXXXXXX.test", PATH_MAX);
    testfd = mkostemps(buf, strlen(".test"), 0);
    assert(testfd > 0);
    assert_mktemp_path(TESTDIR "/temp", ".test", buf);
    close(testfd);

    strncpy(buf, TESTDIR "/tempXXXXXX", PATH_MAX);
    assert(mkdtemp(buf) == buf);
    assert_mktemp_path(TESTDIR "/temp", "", buf);

    strncpy(buf, TESTDIR "/tempXXXXXX", PATH_MAX);
    assert(mktemp(buf) == buf);
    assert_mktemp_path(TESTDIR "/temp", "", buf);

    test_spawn();
    test_system();
    test_stat_with_null_path();

    // Only run subprocess if no arguments are given
    // as the subprocess will be called without argument
    // otherwise we will have infinite recursion
    if (argc == 1) {
        test_subprocess();
    }

    test_execv();

    /* If all goes well, this is never reached because test_execv() replaces
     * the current process.
     */
    return 0;
}