-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile-tests.c
211 lines (164 loc) · 3.54 KB
/
file-tests.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
#include "ag.h"
#define reqExists 0
int exists() {
return ag1(reqExists);
}
void checkValue(int value) {
assert(value == agLoad(valRandom));
}
#define valFile1 0
#define valFile2 2
#define valFileDescriptor 4
void run(int argc, char *argv[]) {
int src, dst;
int i, value;
char file1[8], file2[8];
getStringArgument(file1, valFile1, 2);
getStringArgument(file2, valFile2, 2);
switch (testID) {
case 0:
/* check creat() */
assert(!exists());
dst = creat(file2);
assert(dst != -1);
assert(exists());
agDone();
break;
case 1:
/* check creat(), close(), and unlink() */
assert(!exists());
dst = creat(file2);
assert(dst != -1);
assert(exists());
assert(close(dst) == 0);
assert(unlink(file2) == 0);
assert(!exists());
agDone();
break;
case 2:
/* make sure close() really closes the file */
for (i=0; i<17; i++) {
dst = creat(file2);
assert(dst != -1);
assert(exists());
assert(close(dst) == 0);
assert(unlink(file2) == 0);
assert(!exists());
}
agDone();
break;
case 3:
/* make sure open eventually runs out of descriptors and that things don't die */
for (i=0; i<10; i++) {
assert(open(file1) != -1);
}
for (i=0; i<10; i++) {
if (open(file1) == -1)
agDone();
}
agFail();
break;
case 4:
/* make sure all open files closed on process termination */
if (processID == 0) {
for (i=0; i<5; i++) {
assert(restart() != -1);
waitChild();
}
agDone();
}
else {
for (i=0; i<5; i++) {
assert(open(file1) != -1);
}
signalParent();
exit(0);
}
break;
case 5:
/* check read */
src = open(file1);
assert(src != -1);
assert(read(src, &value, 4) == 4);
assert(close(src) == 0);
checkValue(value);
agDone();
break;
case 6:
/* check isolation of file descriptors */
if (processID == 0) {
src = open(file1);
assert(src != -1);
agStore(valFileDescriptor, src);
restart();
waitChild();
assert(read(src, &value, 4) == 4);
assert(close(src) == 0);
checkValue(value);
agDone();
}
else {
src = agLoad(valFileDescriptor);
assert(close(src) == -1);
signalParent();
exit(0);
}
break;
case 7:
/* check write */
src = open(file1);
assert(src != -1);
dst = creat(file2);
assert(dst != -1);
assert(read(src, &value, 4) == 4);
assert(write(dst, &value, 4) == 4);
assert(close(src) == 0);
assert(close(dst) == 0);
dst = open(file2);
assert(dst != -1);
assert(read(dst, &value, 4) == 4);
assert(close(dst) == 0);
checkValue(value);
agDone();
break;
case 8:
/* make sure write fails peacefully on bad address and length */
dst = creat(file2);
assert(dst != -1);
assert(write(dst, (void*) 0x7FFE1234, 0x7FFFFFFF) <= 0);
agDone();
break;
case 9:
/* make sure read fails peacefully on read-only address */
src = open(file1);
assert(src != -1);
assert(read(src, (void*) 0x00000000, 4) <= 0);
agDone();
break;
case 10:
/* make sure standard input uses the console */
for (i=0; i<4; i++)
assert(read(fdStandardInput, &((char*) &value)[i], 1) == 1);
checkValue(value);
agDone();
break;
case 11:
/* make sure standard output does not interleave */
if (processID == 0) {
restart();
i = strlen(file1);
waitChild();
write(fdStandardOutput, file1, i);
waitChild();
agDone();
}
else {
i = strlen(file1);
signalParent();
write(fdStandardOutput, file1, i);
signalParent();
}
break;
}
assertNotReached();
}