File: exec-alt-t.c

package info (click to toggle)
openafs 1.8.14-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 42,972 kB
  • sloc: ansic: 455,934; xml: 66,858; perl: 11,967; makefile: 10,038; sh: 7,955; objc: 6,354; java: 5,638; cpp: 2,268; asm: 1,214; yacc: 441; tcl: 249; lex: 201; csh: 85
file content (245 lines) | stat: -rw-r--r-- 5,906 bytes parent folder | download | duplicates (5)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
 * Copyright 2010, Sine Nomine Associates and others.
 * All Rights Reserved.
 *
 * This software has been released under the terms of the IBM Public
 * License.  For details, see the LICENSE file in the top-level source
 * directory or online at http://www.openafs.org/dl/license10.html
 */

#include <afsconfig.h>
#include <afs/param.h>

#include <afs/afsutil.h>
#include <tests/tap/basic.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <errno.h>
#include <libgen.h>
#include <sys/stat.h>
#include <fcntl.h>

#define FAILSTR "exec test failure\n"
#define ARGSTRING "teststring"
#define PSTR(s) ((s) != NULL ? (s) : "(null)")

static struct exec_test {
    const char *prefix; /* program prefix to run */
    const char *suffix; /* program suffix to run */
    const char *result; /* expected output from stdout from the child program,
                           or NULL if we should fail to run anything */
} tests[] = {
    { "exec-test1.",      NULL,          "exec test 1\n" },
    { NULL,               ".exec-test2", "exec test 2\n" },
    { "exec-test3.",      ".suffix",     "exec test 3\n" },
    { NULL,               NULL,          NULL },
    { "nonexistent",      NULL,          NULL },
};

static char*
create_child_script(const char *argv0, struct exec_test *test)
{
    char *script;
    char *dirc, *basec, *bname, *dname;
    const char *prefix, *suffix;
    int fd;
    FILE *fh;

    if (!test->result) {
	return NULL;
    }

    dirc = strdup(argv0);
    basec = strdup(argv0);
    script = malloc(PATH_MAX);

    if (!dirc || !basec || !script) {
	sysbail("malloc");
    }

    dname = dirname(dirc);
    bname = basename(basec);

    prefix = test->prefix;
    if (!prefix) {
	prefix = "";
    }
    suffix = test->suffix;
    if (!suffix) {
	suffix = "";
    }

    sprintf(script, "%s/%s%s%s", dname, prefix, bname, suffix);

    free(dirc);
    free(basec);

    fd = open(script, O_WRONLY | O_CREAT | O_EXCL, 0770);
    if (fd < 0) {
	sysbail("open(%s)", script);
    }

    fh = fdopen(fd, "w");
    if (!fh) {
	sysbail("fdopen");
    }

    fprintf(fh, "#!/bin/sh\n"
"if test x\"$1\" = x%s ; then\n"
"    echo %s\n"
"else\n"
"    exit 1\n"
"fi\n", ARGSTRING, test->result);

    fclose(fh);

    return script;
}

int
main(int argc, char **argv)
{
    int fds[2];
    pid_t pid;
    char *child_argv[3];
    int child_argc = 2;
    char buf[1024];
    int i;
    unsigned long nTests = sizeof(tests) / sizeof(tests[0]);

    if (argc != 1) {
	/* in case afs_exec_alt tries to run us again */
	exit(1);
    }

    child_argv[0] = argv[0];
    child_argv[1] = ARGSTRING;
    child_argv[2] = NULL;

    plan(nTests * 4);

    /*
     * Testing afs_exec_alt is a little interesting, since a successful run
     * means that we exec() someone else. Our general strategy is to fork and
     * run some generated shell scripts that just output a (known) string and
     * exit successfully. We have each of those scripts output something
     * different so we can make sure we're executing the right one.
     *
     * This isn't 100% foolproof, since afs_exec_alt could bug out and exec
     * some entirely different program, which happens to have the exact same
     * behavior as our shell scripts. But we've tried to make that unlikely.
     *
     * The shell scripts are passed exactly one argument: 'teststring'. All
     * they should do is see if that were given that argument, and if so,
     * they should print out the expected 'result' string to stdout, and
     * should exit with successful status.
     */

    for (i = 0; i < nTests; i++) {
	char *child_script;
	struct exec_test *t;

	t = &tests[i];

	if (pipe(fds)) {
	    sysbail("pipe");
	}

	child_script = create_child_script(argv[0], t);

	pid = fork();
	if (pid < 0) {
	    sysbail("fork");
	}

	if (pid == 0) {
	    char *prog;

	    if (close(fds[0])) {
		printf("child: close(%d) failed: %s", fds[0], strerror(errno));
	    }

	    /* child */
	    if (dup2(fds[1], 1) < 0) {
		printf("dup2: %s", strerror(errno));
		exit(1);
	    }

	    prog = afs_exec_alt(child_argc, child_argv, t->prefix, t->suffix);
	    if (t->result == NULL) {
		printf("%s", FAILSTR);
		exit(0);
	    }
	    printf("afs_exec_alt failed to exec %s: %s", prog, strerror(errno));
	    exit(1);

	} else {
	    /* parent */

	    ssize_t result_len, nBytes;
	    const char *result;
	    int status;

	    if (close(fds[1])) {
		sysdiag("parent: close(%d) failed", fds[1]);
	    }

	    result = t->result;
	    if (!result) {
		result = FAILSTR;
	    }

	    result_len = strlen(result);

	    nBytes = read(fds[0], buf, sizeof(buf)-1);
	    is_int(result_len, nBytes,
	           "child output size for prefix=%s, suffix=%s",
	           PSTR(t->prefix), PSTR(t->suffix));

	    if (nBytes < 0) {
		skip("read() failed; cannot test read buffer");
	    } else {
		/* just so is_string doesn't go running off into memory... */
		buf[nBytes] = '\0';

		is_string(result, buf,
		          "child output for prefix=%s, suffix=%s",
		          PSTR(t->prefix), PSTR(t->suffix));
	    }

	    if (close(fds[0])) {
		sysdiag("parent: close(%d) failed", fds[0]);
	    }

	    if (waitpid(pid, &status, 0) <= 0) {
		sysbail("waitpid");
	    }

	    if (child_script) {
		if (unlink(child_script)) {
		    sysdiag("unlink(%s)", child_script);
		}
		free(child_script);
	    }

	    ok(WIFEXITED(status), "child exited for prefix=%s, suffix=%s",
	                          PSTR(t->prefix), PSTR(t->suffix));

	    if (WIFEXITED(status)) {
		is_int(0, WEXITSTATUS(status),
		       "child exit code for prefix=%s, suffix=%s",
		       PSTR(t->prefix), PSTR(t->suffix));
	    } else {
		skip("!WIFEXITED(status) (status=%d), cannot check exit code",
		     status);
		if (WIFSIGNALED(status)) {
		    diag("terminated with signal %d", WTERMSIG(status));
		}
	    }
	}
    }
    return 0;
}