2 * Copyright (c) 2015 Todd C. Miller <Todd.Miller@sudo.ws>
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.
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.
19 #include <sys/types.h>
24 #endif /* HAVE_STRING_H */
27 #endif /* HAVE_STRINGS_H */
31 # include "compat/stdbool.h"
34 #include "sudo_compat.h"
35 #include "sudo_fatal.h"
36 #include "sudo_util.h"
38 __dso_public int main(int argc, char *argv[]);
41 * Test that sudo_strsplit() works as expected.
44 struct strsplit_test {
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 };
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 },
71 main(int argc, char *argv[])
73 const char *cp, *ep, *input_end;
74 int i, j, errors = 0, ntests = 0;
76 initprogname(argc > 0 ? argv[0] : "strsplit_test");
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++) {
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));
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);
97 cp = sudo_strsplit(NULL, input_end, " \t", &ep);
101 sudo_warnx_nodebug("failed test #%d: extra tokens \"%.*s\"",
102 ntests, (int)(input_end - cp), cp);
107 printf("%s: %d tests run, %d errors, %d%% success rate\n",
108 getprogname(), ntests, errors, (ntests - errors) * 100 / ntests);