2 * SPDX-License-Identifier: ISC
4 * Copyright (c) 2014 Todd C. Miller <Todd.Miller@sudo.ws>
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.
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.
21 #include <sys/types.h>
27 # include "compat/stdbool.h"
30 #include "sudo_compat.h"
31 #include "sudo_util.h"
32 #include "sudo_fatal.h"
34 __dso_public int main(int argc, char *argv[]);
36 /* sudo_strtobool() tests */
37 static struct strtobool_data {
40 } strtobool_data[] = {
59 test_strtobool(int *ntests)
61 struct strtobool_data *d;
65 for (d = strtobool_data; d->bool_str != NULL; d++) {
67 value = sudo_strtobool(d->bool_str);
68 if (value != d->value) {
69 sudo_warnx_nodebug("FAIL: %s != %d", d->bool_str, d->value);
77 /* sudo_strtoid() tests */
78 static struct strtoid_data {
84 { "0,1", 0, ",", "," },
85 { "10", 10, NULL, NULL },
86 { "-2", -2, NULL, NULL },
87 #if SIZEOF_ID_T != SIZEOF_LONG_LONG
88 { "-2", (id_t)4294967294U, NULL, NULL },
90 { "4294967294", (id_t)4294967294U, NULL, NULL },
91 { NULL, 0, NULL, NULL }
95 test_strtoid(int *ntests)
97 struct strtoid_data *d;
103 for (d = strtoid_data; d->idstr != NULL; d++) {
105 errstr = "some error";
106 value = sudo_strtoid(d->idstr, d->sep, &ep, &errstr);
107 if (errstr != NULL) {
108 if (d->id != (id_t)-1) {
109 sudo_warnx_nodebug("FAIL: %s: %s", d->idstr, errstr);
112 } else if (value != d->id) {
113 sudo_warnx_nodebug("FAIL: %s != %u", d->idstr, (unsigned int)d->id);
115 } else if (d->ep != NULL && ep[0] != d->ep[0]) {
116 sudo_warnx_nodebug("FAIL: ep[0] %d != %d", (int)(unsigned char)ep[0],
117 (int)(unsigned char)d->ep[0]);
125 /* sudo_strtomode() tests */
126 static struct strtomode_data {
127 const char *mode_str;
129 } strtomode_data[] = {
138 test_strtomode(int *ntests)
140 struct strtomode_data *d;
145 for (d = strtomode_data; d->mode_str != NULL; d++) {
147 errstr = "some error";
148 mode = sudo_strtomode(d->mode_str, &errstr);
149 if (errstr != NULL) {
150 if (d->mode != (mode_t)-1) {
151 sudo_warnx_nodebug("FAIL: %s: %s", d->mode_str, errstr);
154 } else if (mode != d->mode) {
155 sudo_warnx_nodebug("FAIL: %s != 0%o", d->mode_str,
156 (unsigned int) d->mode);
165 * Simple tests for sudo_strtobool(), sudo_strtoid(), sudo_strtomode().
168 main(int argc, char *argv[])
173 initprogname(argc > 0 ? argv[0] : "atofoo");
175 errors += test_strtobool(&ntests);
176 errors += test_strtoid(&ntests);
177 errors += test_strtomode(&ntests);
180 printf("%s: %d tests run, %d errors, %d%% success rate\n",
181 getprogname(), ntests, errors, (ntests - errors) * 100 / ntests);