Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
script3r
GitHub Repository: script3r/os161
Path: blob/master/user/testbin/badcall/common_path.c
734 views
1
/*
2
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
3
* The President and Fellows of Harvard College.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
7
* are met:
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
13
* 3. Neither the name of the University nor the names of its contributors
14
* may be used to endorse or promote products derived from this software
15
* without specific prior written permission.
16
*
17
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
18
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
21
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
* SUCH DAMAGE.
28
*/
29
30
/*
31
* Calls with invalid pathnames
32
*/
33
34
#include <sys/types.h>
35
#include <sys/stat.h>
36
#include <stdio.h>
37
#include <stdlib.h>
38
#include <string.h>
39
#include <unistd.h>
40
#include <limits.h>
41
#include <errno.h>
42
#include <err.h>
43
44
#include "config.h"
45
#include "test.h"
46
47
static
48
int
49
open_badpath(const char *path)
50
{
51
return open(path, O_RDONLY);
52
}
53
54
static
55
int
56
remove_badpath(const char *path)
57
{
58
return remove(path);
59
}
60
61
static
62
int
63
rename_badpath1(const char *path)
64
{
65
return rename(path, TESTFILE);
66
}
67
68
static
69
int
70
rename_badpath2(const char *path)
71
{
72
return rename(TESTFILE, path);
73
}
74
75
static
76
int
77
link_badpath1(const char *path)
78
{
79
return link(path, TESTFILE);
80
}
81
82
static
83
int
84
link_badpath2(const char *path)
85
{
86
return link(TESTFILE, path);
87
}
88
89
static
90
int
91
mkdir_badpath(const char *path)
92
{
93
return mkdir(path, 0775);
94
}
95
96
static
97
int
98
rmdir_badpath(const char *path)
99
{
100
return rmdir(path);
101
}
102
103
static
104
int
105
chdir_badpath(const char *path)
106
{
107
return chdir(path);
108
}
109
110
static
111
int
112
symlink_badpath1(const char *path)
113
{
114
return symlink(path, TESTFILE);
115
}
116
117
static
118
int
119
symlink_badpath2(const char *path)
120
{
121
return symlink(TESTFILE, path);
122
}
123
124
static
125
int
126
readlink_badpath(const char *path)
127
{
128
char buf[128];
129
return readlink(path, buf, sizeof(buf));
130
}
131
132
static
133
int
134
lstat_badpath(const char *name)
135
{
136
struct stat sb;
137
return lstat(name, &sb);
138
}
139
140
static
141
int
142
stat_badpath(const char *name)
143
{
144
struct stat sb;
145
return stat(name, &sb);
146
}
147
148
////////////////////////////////////////////////////////////
149
150
static
151
void
152
common_badpath(int (*func)(const char *path), int mk, int rm, const char *path,
153
const char *call, const char *pathdesc)
154
{
155
char mydesc[128];
156
int rv;
157
158
if (mk) {
159
if (create_testfile()<0) {
160
return;
161
}
162
}
163
164
snprintf(mydesc, sizeof(mydesc), "%s with %s path", call, pathdesc);
165
rv = func(path);
166
report_test(rv, errno, EFAULT, mydesc);
167
168
if (mk || rm) {
169
remove(TESTFILE);
170
}
171
}
172
173
static
174
void
175
any_badpath(int (*func)(const char *path), const char *call, int mk, int rm)
176
{
177
common_badpath(func, mk, rm, NULL, call, "NULL");
178
common_badpath(func, mk, rm, INVAL_PTR, call, "invalid-pointer");
179
common_badpath(func, mk, rm, KERN_PTR, call, "kernel-pointer");
180
}
181
182
////////////////////////////////////////////////////////////
183
184
/* functions with one pathname */
185
#define T(call) \
186
void \
187
test_##call##_path(void) \
188
{ \
189
any_badpath(call##_badpath, #call, 0, 0); \
190
}
191
192
T(open);
193
T(remove);
194
T(mkdir);
195
T(rmdir);
196
T(chdir);
197
T(readlink);
198
T(stat);
199
T(lstat);
200
201
/* functions with two pathnames */
202
#define T2(call) \
203
void \
204
test_##call##_paths(void) \
205
{ \
206
any_badpath(call##_badpath1, #call "(arg1)", 0, 1); \
207
any_badpath(call##_badpath2, #call "(arg2)", 1, 1); \
208
}
209
210
T2(rename);
211
T2(link);
212
T2(symlink);
213
214