Debian Codegraph @

lib/iolog/regress/iolog_mkpath/check_iolog_mkpath.c

raw
1 /*
2 * SPDX-License-Identifier: ISC
3 *
4 * Copyright (c) 2020 Todd C. Miller <[email protected]>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <config.h>
20
21 #include <sys/wait.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <time.h>
26 #include <unistd.h>
27
28 #define SUDO_ERROR_WRAP 0
29
30 #include <sudo_compat.h>
31 #include <sudo_util.h>
32 #include <sudo_fatal.h>
33 #include <sudo_iolog.h>
34
35 sudo_dso_public int main(int argc, char *argv[]);
36
37 static const char *test_paths[] = {
38 "testdir/a/b/c/user", /* create new */
39 "testdir/a/b/c/user", /* open existing */
40 "testdir/a/b/c/user.XXXXXX", /* mkdtemp new */
41 NULL
42 };
43
44 static void
45 test_iolog_mkpath(const char *testdir, int *ntests, int *nerrors)
46 {
47 const char **tp;
48 char *path;
49
50 iolog_set_owner(geteuid(), getegid());
51
52 for (tp = test_paths; *tp != NULL; tp++) {
53 if (asprintf(&path, "%s/%s", testdir, *tp) == -1)
54 sudo_fatalx("unable to allocate memory");
55
56 (*ntests)++;
57 if (!iolog_mkpath(path)) {
58 sudo_warnx("unable to mkpath %s", path);
59 (*nerrors)++;
60 }
61 free(path);
62 }
63 }
64
65 int
66 main(int argc, char *argv[])
67 {
68 char testdir[] = "mkpath.XXXXXX";
69 const char *rmargs[] = { "rm", "-rf", NULL, NULL };
70 int ch, status, ntests = 0, errors = 0;
71
72 initprogname(argc > 0 ? argv[0] : "check_iolog_mkpath");
73
74 while ((ch = getopt(argc, argv, "v")) != -1) {
75 switch (ch) {
76 case 'v':
77 /* ignore */
78 break;
79 default:
80 fprintf(stderr, "usage: %s [-v]\n", getprogname());
81 return EXIT_FAILURE;
82 }
83 }
84 argc -= optind;
85 argv += optind;
86
87 if (mkdtemp(testdir) == NULL)
88 sudo_fatal("unable to create test dir");
89 rmargs[2] = testdir;
90
91 test_iolog_mkpath(testdir, &ntests, &errors);
92
93 if (ntests != 0) {
94 printf("iolog_mkpath: %d test%s run, %d errors, %d%% success rate\n",
95 ntests, ntests == 1 ? "" : "s", errors,
96 (ntests - errors) * 100 / ntests);
97 }
98
99 /* Clean up (avoid running via shell) */
100 switch (fork()) {
101 case -1:
102 sudo_warn("fork");
103 _exit(1);
104 case 0:
105 execvp("rm", (char **)rmargs);
106 _exit(1);
107 default:
108 wait(&status);
109 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
110 errors++;
111 break;
112 }
113
114 return errors;
115 }