]> granicus.if.org Git - sudo/blob - lib/util/regress/strsplit/strsplit_test.c
c9aecdae49fff68c9ba588e574222c808bb965a5
[sudo] / lib / util / regress / strsplit / strsplit_test.c
1 /*
2  * Copyright (c) 2015 Todd C. Miller <Todd.Miller@sudo.ws>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <config.h>
18
19 #include <sys/types.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #ifdef HAVE_STRING_H
23 # include <string.h>
24 #endif /* HAVE_STRING_H */
25 #ifdef HAVE_STRINGS_H
26 # include <strings.h>
27 #endif /* HAVE_STRINGS_H */
28 #ifdef HAVE_STDBOOL_H
29 # include <stdbool.h>
30 #else
31 # include "compat/stdbool.h"
32 #endif
33
34 #include "sudo_compat.h"
35 #include "sudo_fatal.h"
36 #include "sudo_util.h"
37
38 __dso_public int main(int argc, char *argv[]);
39
40 /*
41  * Test that sudo_strsplit() works as expected.
42  */
43
44 struct strsplit_test {
45     const char *input;
46     size_t input_len;
47     const char **output;
48 };
49
50 static const char test1_in[] = " vi ";
51 static const char *test1_out[] = { "vi", NULL };
52 static const char test2_in[] = "vi -r ";
53 static const char *test2_out[] = { "vi", "-r", NULL };
54 static const char test3_in[] = "vi -r  -R abc\tdef ";
55 static const char *test3_out[] = { "vi", "-r", "-R", "abc", "def", NULL };
56 static const char test4_in[] = "vi -r  -R abc\tdef ";
57 static const char *test4_out[] = { "vi", "-r", "-R", "abc", NULL };
58 static const char test5_in[] = "";
59 static const char *test5_out[] = { NULL };
60
61 static struct strsplit_test test_data[] = {
62     { test1_in, sizeof(test1_in) - 1, test1_out },
63     { test2_in, sizeof(test2_in) - 1, test2_out },
64     { test3_in, sizeof(test3_in) - 1, test3_out },
65     { test4_in, sizeof(test4_in) - 5, test4_out },
66     { test5_in, sizeof(test5_in) - 1, test5_out },
67     { NULL, 0, NULL }
68 };
69
70 int
71 main(int argc, char *argv[])
72 {
73     const char *cp, *ep, *input_end;
74     int i, j, errors = 0, ntests = 0;
75     size_t len;
76     initprogname(argc > 0 ? argv[0] : "strsplit_test");
77
78     for (i = 0; test_data[i].input != NULL; i++) {
79         input_end = test_data[i].input + test_data[i].input_len;
80         cp = sudo_strsplit(test_data[i].input, input_end, " \t", &ep);
81         for (j = 0; test_data[i].output[j] != NULL; j++) {
82             ntests++;
83             len = strlen(test_data[i].output[j]);
84             if ((size_t)(ep - cp) != len) {
85                 sudo_warnx_nodebug("failed test #%d: bad length, expected "
86                     "%zu, got %zu", ntests, len, (size_t)(ep - cp));
87                 errors++;
88                 continue;
89             }
90             ntests++;
91             if (strncmp(cp, test_data[i].output[j], len) != 0) {
92                 sudo_warnx_nodebug("failed test #%d: expected %s, got %.*s",
93                     ntests, test_data[i].output[j], (int)(ep - cp), cp);
94                 errors++;
95                 continue;
96             }
97             cp = sudo_strsplit(NULL, input_end, " \t", &ep);
98         }
99         ntests++;
100         if (cp != NULL) {
101             sudo_warnx_nodebug("failed test #%d: extra tokens \"%.*s\"",
102                 ntests, (int)(input_end - cp), cp);
103             errors++;
104         }
105     }
106     if (ntests != 0) {
107         printf("%s: %d tests run, %d errors, %d%% success rate\n",
108             getprogname(), ntests, errors, (ntests - errors) * 100 / ntests);
109     }
110     exit(errors);
111 }