-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp2-test.c
executable file
·392 lines (354 loc) · 9.54 KB
/
p2-test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
A basic test suite for Portland State University CS333 Operating Systems Project 2.
Created by Joe Coleman
*/
#ifdef CS333_P2
#include "types.h"
#include "user.h"
// comment out tests for features the student doesn't have implemented
// Note the CPUTIME_TEST requires GETPROCS_TEST
#define UIDGIDPPID_TEST
#define CPUTIME_TEST
#define GETPROCS_TEST
#define TIME_TEST
#ifdef GETPROCS_TEST
#include "uproc.h"
#endif
#ifdef UIDGIDPPID_TEST
static void
testppid(void){
int ret, pid, ppid;
printf(1, "\n----------\nRunning PPID Test\n----------\n");
pid = getpid();
ret = fork();
if(ret == 0){
ppid = getppid();
if(ppid != pid)
printf(2, "FAILED: Parent PID is %d, Child's PPID is %d\n", pid, ppid);
else
printf(1, "** Test passed! **\n");
exit();
}
else
wait();
}
static int
testgid(uint new_val, uint expected_get_val, int expected_set_ret){
int ret;
uint post_gid, pre_gid;
int success = 0;
pre_gid = getgid();
ret = setgid(new_val);
if((ret < 0 && expected_set_ret >= 0) || (ret >= 0 && expected_set_ret < 0)){
printf(2, "FAILED: setgid(%d) returned %d, expected %d\n", new_val, ret, expected_set_ret);
success = -1;
}
post_gid = getgid();
if(post_gid != expected_get_val){
printf(2, "FAILED: UID was %d. After setgid(%d), getgid() returned %d, expected %d\n",
pre_gid, new_val, post_gid, expected_get_val);
success = -1;
}
return success;
}
static int
testuid(uint new_val, uint expected_get_val, int expected_set_ret){
int ret;
uint post_uid, pre_uid;
int success = 0;
pre_uid = getuid();
ret = setuid(new_val);
if((ret < 0 && expected_set_ret >= 0) || (ret >= 0 && expected_set_ret < 0)){
printf(2, "FAILED: setuid(%d) returned %d, expected %d\n", new_val, ret, expected_set_ret);
success = -1;
}
post_uid = getuid();
if(post_uid != expected_get_val){
printf(2, "FAILED: UID was %d. After setuid(%d), getuid() returned %d, expected %d\n",
pre_uid, new_val, post_uid, expected_get_val);
success = -1;
}
return success;
}
static void
testuidgid(void)
{
int uid, gid;
int success = 0;
printf(1, "\n----------\nRunning UID / GID Tests\n----------\n");
uid = getuid();
if(uid < 0 || uid > 32767){
printf(1, "FAILED: Default UID %d, out of range\n", uid);
success = -1;
}
if (testuid(0, 0, 0))
success = -1;
if (testuid(5, 5, 0))
success = -1;
if (testuid(32767, 32767, 0))
success = -1;
if (testuid(32768, 32767, -1))
success = -1;
if (testuid(-1, 32767, -1))
success = -1;
gid = getgid();
if(gid < 0 || gid > 32767){
printf(1, "FAILED: Default GID %d, out of range\n", gid);
success = -1;
}
if (testgid(0, 0, 0))
success = -1;
if (testgid(5, 5, 0))
success = -1;
if (testgid(32767, 32767, 0))
success = -1;
if (testgid(-1, 32767, -1))
success = -1;
if (testgid(32768, 32767, -1))
success = -1;
if (success == 0)
printf(1, "** All tests passed! **\n");
}
static void
testuidgidinheritance(void){
int ret, success, uid, gid;
success = 0;
printf(1, "\n----------\nRunning UID / GID Inheritance Test\n----------\n");
if (testuid(12345, 12345, 0))
success = -1;
if (testgid(12345, 12345, 0))
success = -1;
if(success != 0)
return;
ret = fork();
if(ret == 0){
uid = getuid();
gid = getgid();
if(uid != 12345){
printf(2, "FAILED: Parent UID is 12345, child UID is %d\n", uid);
}
else if(gid != 12345){
printf(2, "FAILED: Parent GID is 12345, child GID is %d\n", gid);
}
else
printf(1, "** Test Passed! **\n");
exit();
}
else {
wait();
}
}
#endif
#ifdef GETPROCS_TEST
#ifdef CPUTIME_TEST
// Simple test to have the program sleep for 200 milliseconds to see if CPU_time properly doesn't change
// And then gets CPU_time again to see if elapsed CPU_total_ticks is reasonable
static int
getcputime(char * name, struct uproc * table){
struct uproc *p = 0;
int size;
size = getprocs(64, table);
for(int i = 0; i < size; ++i){
if(strcmp(table[i].name, name) == 0){
p = table + i;
break;
}
}
if(p == 0){
printf(2, "FAILED: Test program \"%s\" not found in table returned by getprocs\n", name);
return -1;
}
else
return p->CPU_total_ticks;
}
static void
testcputime(char * name){
struct uproc *table;
uint time1, time2, pre_sleep, post_sleep;
int success = 0;
int i, num;
printf(1, "\n----------\nRunning CPU Time Test\n----------\n");
table = malloc(sizeof(struct uproc) * 64);
if (!table) {
printf(2, "Error: malloc() call failed. %s at line %d\n", __FUNCTION__, __LINE__);
exit();
}
printf(1, "This will take a couple seconds\n");
// Loop for a long time to see if the elapsed CPU_total_ticks is in a reasonable range
time1 = getcputime(name, table);
for(i = 0, num = 0; i < 1000000; ++i){
++num;
if(num % 100000 == 0){
pre_sleep = getcputime(name, table);
sleep(200);
post_sleep = getcputime(name, table);
if((post_sleep - pre_sleep) >= 100){
printf(2, "FAILED: CPU_total_ticks changed by 100+ milliseconds while process was asleep\n");
success = -1;
}
}
}
time2 = getcputime(name, table);
if((time2 - time1) < 0){
printf(2, "FAILED: difference in CPU_total_ticks is negative. T2 - T1 = %d\n", (time2 - time1));
success = -1;
}
if((time2 - time1) > 400){
printf(2, "ABNORMALLY HIGH: T2 - T1 = %d milliseconds. Run test again\n", (time2 - time1));
success = -1;
}
printf(1, "T2 - T1 = %d milliseconds\n", (time2 - time1));
free(table);
if(success == 0)
printf(1, "** All Tests Passed! **\n");
}
#endif
#endif
#ifdef GETPROCS_TEST
// Fork to 64 process and then make sure we get all when passing table array
// of sizes 1, 16, 64, 72. NOTE: caller does all forks.
static int
testprocarray(int max, int expected_ret){
struct uproc * table;
int ret, success = 0;
table = malloc(sizeof(struct uproc) * max); // bad code, assumes success
if (!table) {
printf(2, "Error: malloc() call failed. %s at line %d\n", __FUNCTION__, __LINE__);
exit();
}
ret = getprocs(max, table);
if (ret != expected_ret){
printf(2, "FAILED: getprocs(%d) returned %d, expected %d\n", max, ret, expected_ret);
success = -1;
}
else{
printf(2, "getprocs() was asked for %d processes and returned %d. SUCCESS\n", max, expected_ret);
}
free(table);
return success;
}
static int
testinvalidarray(void){
struct uproc * table;
int ret;
table = malloc(sizeof(struct uproc));
if (!table) {
printf(2, "Error: malloc() call failed. %s at line %d\n", __FUNCTION__, __LINE__);
exit();
}
ret = getprocs(1024, table);
free(table);
if(ret >= 0){
printf(2, "FAILED: called getprocs with max way larger than table and returned %d, not error\n", ret);
return -1;
}
return 0;
}
static void
testgetprocs(){
int ret, success;
printf(1, "\n----------\nRunning GetProcs Test\n----------\n");
printf(1, "Filling the proc[] array with dummy processes\n");
// Fork until no space left in ptable
ret = fork();
if (ret == 0){
while((ret = fork()) == 0);
if(ret > 0){
wait();
exit();
}
// Only return left is -1, which is no space left in ptable
success = testinvalidarray();
success |= testprocarray( 1, 1);
success |= testprocarray(16, 16);
success |= testprocarray(64, 64);
success |= testprocarray(72, 64);
if (success == 0)
printf(1, "** All Tests Passed **\n");
exit();
}
wait();
}
#endif
#ifdef TIME_TEST
// Forks a process and execs with time + args to see how it handles no args, invalid args, mulitple args
void
testtimewitharg(char **arg){
int ret;
ret = fork();
if (ret == 0){
exec(arg[0], arg);
printf(2, "FAILED: exec failed to execute %s\n", arg[0]);
exit();
}
else if(ret == -1){
printf(2, "FAILED: fork failed\n");
}
else
wait();
}
void
testtime(void){
char **arg1 = malloc(sizeof(char *));
char **arg2 = malloc(sizeof(char *)*2);
char **arg3 = malloc(sizeof(char *)*2);
char **arg4 = malloc(sizeof(char *)*4);
arg1[0] = malloc(sizeof(char) * 5);
strcpy(arg1[0], "time");
arg2[0] = malloc(sizeof(char) * 5);
strcpy(arg2[0], "time");
arg2[1] = malloc(sizeof(char) * 4);
strcpy(arg2[1], "abc");
arg3[0] = malloc(sizeof(char) * 5);
strcpy(arg3[0], "time");
arg3[1] = malloc(sizeof(char) * 5);
strcpy(arg3[1], "date");
arg4[0] = malloc(sizeof(char) * 5);
strcpy(arg4[0], "time");
arg4[1] = malloc(sizeof(char) * 5);
strcpy(arg4[1], "time");
arg4[2] = malloc(sizeof(char) * 5);
strcpy(arg4[2], "echo");
arg4[3] = malloc(sizeof(char) * 6);
strcpy(arg4[3], "\"abc\"");
printf(1, "\n----------\nRunning Time Test\n----------\n");
printf(1, "You will need to verify these tests passed\n");
printf(1,"\n%s\n", arg1[0]);
testtimewitharg(arg1);
printf(1,"\n%s %s\n", arg2[0], arg2[1]);
testtimewitharg(arg2);
printf(1,"\n%s %s\n", arg3[0], arg3[1]);
testtimewitharg(arg3);
printf(1,"\n%s %s %s %s\n", arg4[0], arg4[1], arg4[2], arg4[3]);
testtimewitharg(arg4);
free(arg1[0]);
free(arg1);
free(arg2[0]); free(arg2[1]);
free(arg2);
free(arg3[0]); free(arg3[1]);
free(arg3);
free(arg4[0]); free(arg4[1]); free(arg4[2]); free(arg4[3]);
free(arg4);
}
#endif
int
main(int argc, char *argv[])
{
#ifdef CPUTIME_TEST
testcputime(argv[0]);
#endif
#ifdef UIDGIDPPID_TEST
testuidgid();
testuidgidinheritance();
testppid();
#endif
#ifdef GETPROCS_TEST
testgetprocs(); // no need to pass argv[0]
#endif
#ifdef TIME_TEST
testtime();
#endif
printf(1, "\n** End of Tests **\n");
exit();
}
#endif