Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/logsrvd/regress/dotdot/dotdot_test.c
1532 views
1
/*
2
* SPDX-License-Identifier: ISC
3
*
4
* Copyright (c) 2025 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 <stdio.h>
22
#include <stdlib.h>
23
#include <string.h>
24
#ifdef HAVE_STDBOOL_H
25
# include <stdbool.h>
26
#else
27
# include <compat/stdbool.h>
28
#endif /* HAVE_STDBOOL_H */
29
30
#define SUDO_ERROR_WRAP 0
31
32
#include <sudo_compat.h>
33
#include <sudo_util.h>
34
#include <sudo_fatal.h>
35
#include <sudo_queue.h>
36
37
#include <logsrv_util.h>
38
39
sudo_dso_public int main(int argc, char *argv[]);
40
41
struct test_data {
42
const char *str; /* input string */
43
bool expected; /* expected result */
44
};
45
46
static struct test_data test_data[] = {
47
{
48
"/foo/bar",
49
false
50
}, {
51
"..",
52
true
53
}, {
54
"../",
55
true
56
}, {
57
"/..",
58
true
59
}, {
60
"/../",
61
true
62
}, {
63
"foo/../",
64
true
65
}, {
66
"foo/..",
67
true
68
}, {
69
"foo/../bar",
70
true
71
}, {
72
"../bar",
73
true
74
}, {
75
"foo../bar",
76
false
77
}, {
78
"foo/..bar",
79
false
80
}, {
81
"...",
82
false
83
}, {
84
".../",
85
false
86
}, {
87
"/...",
88
false
89
}, {
90
"/.../",
91
false
92
}, {
93
NULL,
94
false
95
}
96
};
97
98
/*
99
* Verify contains_dot_dot() behavior
100
*/
101
int
102
main(int argc, char *argv[])
103
{
104
int errors = 0, ntests = 0;
105
size_t i;
106
int ch;
107
108
initprogname(argc > 0 ? argv[0] : "dotdot_test");
109
110
while ((ch = getopt(argc, argv, "v")) != -1) {
111
switch (ch) {
112
case 'v':
113
/* ignore */
114
break;
115
default:
116
fprintf(stderr, "usage: %s [-v]\n", getprogname());
117
return EXIT_FAILURE;
118
}
119
}
120
121
for (i = 0; test_data[i].str != NULL; i++) {
122
bool result = contains_dot_dot(test_data[i].str);
123
if (result != test_data[i].expected) {
124
sudo_warnx("test %zu:%s: expected %s, got %s", i,
125
test_data[i].str, test_data[i].expected ? "true" : "false",
126
result ? "true" : "false");
127
errors++;
128
}
129
ntests++;
130
}
131
132
if (ntests != 0) {
133
printf("%s: %d tests run, %d errors, %d%% success rate\n",
134
getprogname(), ntests, errors, (ntests - errors) * 100 / ntests);
135
}
136
137
return errors;
138
}
139
140