]> granicus.if.org Git - sudo/blob - lib/util/regress/atofoo/atofoo_test.c
Add SPDX-License-Identifier to files.
[sudo] / lib / util / regress / atofoo / atofoo_test.c
1 /*
2  * SPDX-License-Identifier: ISC
3  *
4  * Copyright (c) 2014 Todd C. Miller <Todd.Miller@sudo.ws>
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/types.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #ifdef HAVE_STDBOOL_H
25 # include <stdbool.h>
26 #else
27 # include "compat/stdbool.h"
28 #endif
29
30 #include "sudo_compat.h"
31 #include "sudo_util.h"
32 #include "sudo_fatal.h"
33
34 __dso_public int main(int argc, char *argv[]);
35
36 /* sudo_strtobool() tests */
37 static struct strtobool_data {
38     const char *bool_str;
39     int value;
40 } strtobool_data[] = {
41     { "true", true },
42     { "false", false },
43     { "TrUe", true },
44     { "fAlSe", false },
45     { "1", true },
46     { "0", false },
47     { "on", true },
48     { "off", false },
49     { "yes", true },
50     { "no", false },
51     { "nope", -1 },
52     { "10", -1 },
53     { "one", -1 },
54     { "zero", -1 },
55     { NULL, 0 }
56 };
57
58 static int
59 test_strtobool(int *ntests)
60 {
61     struct strtobool_data *d;
62     int errors = 0;
63     int value;
64
65     for (d = strtobool_data; d->bool_str != NULL; d++) {
66         (*ntests)++;
67         value = sudo_strtobool(d->bool_str);
68         if (value != d->value) {
69             sudo_warnx_nodebug("FAIL: %s != %d", d->bool_str, d->value);
70             errors++;
71         }
72     }
73
74     return errors;
75 }
76
77 /* sudo_strtoid() tests */
78 static struct strtoid_data {
79     const char *idstr;
80     id_t id;
81     const char *sep;
82     const char *ep;
83 } 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 },
89 #endif
90     { "4294967294", (id_t)4294967294U, NULL, NULL },
91     { NULL, 0, NULL, NULL }
92 };
93
94 static int
95 test_strtoid(int *ntests)
96 {
97     struct strtoid_data *d;
98     const char *errstr;
99     char *ep;
100     int errors = 0;
101     id_t value;
102
103     for (d = strtoid_data; d->idstr != NULL; d++) {
104         (*ntests)++;
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);
110                 errors++;
111             }
112         } else if (value != d->id) {
113             sudo_warnx_nodebug("FAIL: %s != %u", d->idstr, (unsigned int)d->id);
114             errors++;
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]);
118             errors++;
119         }
120     }
121
122     return errors;
123 }
124
125 /* sudo_strtomode() tests */
126 static struct strtomode_data {
127     const char *mode_str;
128     mode_t mode;
129 } strtomode_data[] = {
130     { "755", 0755 },
131     { "007", 007 },
132     { "7", 7 },
133     { "8", (mode_t)-1 },
134     { NULL, 0 }
135 };
136
137 static int
138 test_strtomode(int *ntests)
139 {
140     struct strtomode_data *d;
141     const char *errstr;
142     int errors = 0;
143     mode_t mode;
144
145     for (d = strtomode_data; d->mode_str != NULL; d++) {
146         (*ntests)++;
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);
152                 errors++;
153             }
154         } else if (mode != d->mode) {
155             sudo_warnx_nodebug("FAIL: %s != 0%o", d->mode_str,
156                 (unsigned int) d->mode);
157             errors++;
158         }
159     }
160
161     return errors;
162 }
163
164 /*
165  * Simple tests for sudo_strtobool(), sudo_strtoid(), sudo_strtomode().
166  */
167 int
168 main(int argc, char *argv[])
169 {
170     int errors = 0;
171     int ntests = 0;
172
173     initprogname(argc > 0 ? argv[0] : "atofoo");
174
175     errors += test_strtobool(&ntests);
176     errors += test_strtoid(&ntests);
177     errors += test_strtomode(&ntests);
178
179     if (ntests != 0) {
180         printf("%s: %d tests run, %d errors, %d%% success rate\n",
181             getprogname(), ntests, errors, (ntests - errors) * 100 / ntests);
182     }
183
184     exit(errors);
185 }