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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/errno.h>
#include <sys/types.h>
#include <sys/fcntl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <errno.h>
#include <sys/wait.h>
#include <unistd.h>
#include <time.h>
/* TESTS :
* - open(const char *pathname, int flags, mode_t mode);
1) Attempt to create file that already exists - EEXIST
2) Attempt to open a directory for writing - EISDIR
3) Pathname does not exist - ENOENT
4) Open for write but no write permission - EACCES
read(int fd, void *buf, size_t count);
1) Read using invalid file descriptor - EBADF
write(int fd, const void *buf, size_t count);
1) Write using invalid file descriptor - EBADF
2) Attempt to write to read-only file - EBADF
lseek(int fildes, off_t offset, int whence);
1) Seeking on an invalid file descriptor - EBADF
2) Invalid "whence" (3rd param) value - EINVAL
close(int fd);
1) Attempt to close an invalid file descriptor - EBADF
stat(const char *file_name, struct stat *buf);
1) Pathname is a null string - ENOENT
2) Pathname does not exist - ENOENT
fstat(int filedes, struct stat *buf);
1) Attempt to stat using an invalid file descriptor - EBADF
isatty (int desc);
Not applicable. We will test that it returns 1 when expected and a case
where it should return 0.
rename(const char *oldpath, const char *newpath);
1) newpath is an existing directory, but oldpath is not a directory. - EISDIR
2) newpath is a non-empty directory. - ENOTEMPTY or EEXIST
3) newpath is a subdirectory of old path. - EINVAL
4) oldpath does not exist. - ENOENT
unlink(const char *pathname);
1) pathname does not have write access. - EACCES
2) pathname does not exist. - ENOENT
time(time_t *t);
Not applicable.
system (const char * string);
1) See if shell available - returns 0
2) See if shell available - returns !0
3) Execute simple shell command - returns 0
4) Invalid string/command. - returns 127. */
static const char *strerrno (int err);
/* Note that OUTDIR is defined by the test suite. */
#define FILENAME "foo.fileio.test"
#define RENAMED "bar.fileio.test"
#define NONEXISTANT "nofoo.fileio.test"
#define NOWRITE "nowrt.fileio.test"
#define TESTDIR1 "dir1.fileio.test"
#define TESTDIR2 "dir2.fileio.test"
#define TESTSUBDIR "dir1.fileio.test/subdir.fileio.test"
#define STRING "Hello World"
static void stop (void) {}
/* A NULL string. We pass this to stat below instead of a NULL
literal to avoid -Wnonnull warnings. */
const char *null_str;
void
test_open (void)
{
int ret;
/* Test opening */
errno = 0;
ret = open (OUTDIR FILENAME, O_CREAT | O_TRUNC | O_RDWR, S_IWUSR | S_IRUSR);
printf ("open 1: ret = %d, errno = %d %s\n", ret, errno,
ret >= 0 ? "OK" : "");
if (ret >= 0)
close (ret);
stop ();
/* Creating an already existing file (created by fileio.exp) */
errno = 0;
ret = open (OUTDIR FILENAME, O_CREAT | O_EXCL | O_WRONLY, S_IWUSR | S_IRUSR);
printf ("open 2: ret = %d, errno = %d %s\n", ret, errno,
strerrno (errno));
if (ret >= 0)
close (ret);
stop ();
/* Open directory (for writing) */
errno = 0;
ret = open (".", O_WRONLY);
printf ("open 3: ret = %d, errno = %d %s\n", ret, errno,
strerrno (errno));
if (ret >= 0)
close (ret);
stop ();
/* Opening nonexistant file */
errno = 0;
ret = open (NONEXISTANT, O_RDONLY);
printf ("open 4: ret = %d, errno = %d %s\n", ret, errno,
strerrno (errno));
if (ret >= 0)
close (ret);
stop ();
/* Open for write but no write permission */
errno = 0;
ret = open (OUTDIR NOWRITE, O_CREAT | O_RDONLY, S_IRUSR);
if (ret >= 0)
{
close (ret);
stop ();
errno = 0;
ret = open (OUTDIR NOWRITE, O_WRONLY);
printf ("open 5: ret = %d, errno = %d %s\n", ret, errno,
strerrno (errno));
if (ret >= 0)
close (ret);
}
else
{
stop ();
printf ("open 5: ret = %d, errno = %d\n", ret, errno);
}
stop ();
}
void
test_write (void)
{
int fd, ret;
/* Test writing */
errno = 0;
fd = open (OUTDIR FILENAME, O_WRONLY);
if (fd >= 0)
{
errno = 0;
ret = write (fd, STRING, strlen (STRING));
printf ("write 1: ret = %d, errno = %d %s\n", ret, errno,
ret == strlen (STRING) ? "OK" : "");
close (fd);
}
else
printf ("write 1: errno = %d\n", errno);
stop ();
/* Write using invalid file descriptor */
errno = 0;
ret = write (999, STRING, strlen (STRING));
printf ("write 2: ret = %d, errno = %d, %s\n", ret, errno,
strerrno (errno));
stop ();
/* Write to a read-only file */
errno = 0;
fd = open (OUTDIR FILENAME, O_RDONLY);
if (fd >= 0)
{
errno = 0;
ret = write (fd, STRING, strlen (STRING));
printf ("write 3: ret = %d, errno = %d %s\n", ret, errno,
strerrno (errno));
close (fd);
}
else
printf ("write 3: errno = %d\n", errno);
stop ();
}
void
test_read (void)
{
int fd, ret;
char buf[16];
/* Test reading */
errno = 0;
fd = open (OUTDIR FILENAME, O_RDONLY);
if (fd >= 0)
{
memset (buf, 0, 16);
errno = 0;
ret = read (fd, buf, 16);
buf[15] = '\0'; /* Don't trust anybody... */
if (ret == strlen (STRING))
printf ("read 1: %s %s\n", buf, !strcmp (buf, STRING) ? "OK" : "");
else
printf ("read 1: ret = %d, errno = %d\n", ret, errno);
close (fd);
}
else
printf ("read 1: errno = %d\n", errno);
stop ();
/* Read using invalid file descriptor */
errno = 0;
ret = read (999, buf, 16);
printf ("read 2: ret = %d, errno = %d %s\n", ret, errno,
strerrno (errno));
stop ();
}
void
test_lseek (void)
{
int fd;
off_t ret = 0;
/* Test seeking */
errno = 0;
fd = open (OUTDIR FILENAME, O_RDONLY);
if (fd >= 0)
{
errno = 0;
ret = lseek (fd, 0, SEEK_CUR);
printf ("lseek 1: ret = %ld, errno = %d, %s\n", (long) ret, errno,
ret == 0 ? "OK" : "");
stop ();
errno = 0;
ret = lseek (fd, 0, SEEK_END);
printf ("lseek 2: ret = %ld, errno = %d, %s\n", (long) ret, errno,
ret == 11 ? "OK" : "");
stop ();
errno = 0;
ret = lseek (fd, 3, SEEK_SET);
printf ("lseek 3: ret = %ld, errno = %d, %s\n", (long) ret, errno,
ret == 3 ? "OK" : "");
close (fd);
}
else
{
printf ("lseek 1: ret = %ld, errno = %d %s\n", (long) ret, errno,
strerrno (errno));
stop ();
printf ("lseek 2: ret = %ld, errno = %d %s\n", (long) ret, errno,
strerrno (errno));
stop ();
printf ("lseek 3: ret = %ld, errno = %d %s\n", (long) ret, errno,
strerrno (errno));
}
/* Seeking on an invalid file descriptor */
stop ();
}
void
test_close (void)
{
int fd, ret;
/* Test close */
errno = 0;
fd = open (OUTDIR FILENAME, O_RDONLY);
if (fd >= 0)
{
errno = 0;
ret = close (fd);
printf ("close 1: ret = %d, errno = %d, %s\n", ret, errno,
ret == 0 ? "OK" : "");
}
else
printf ("close 1: errno = %d\n", errno);
stop ();
/* Close an invalid file descriptor */
errno = 0;
ret = close (999);
printf ("close 2: ret = %d, errno = %d, %s\n", ret, errno,
strerrno (errno));
stop ();
}
void
test_stat (void)
{
int ret;
struct stat st;
/* Test stat */
errno = 0;
ret = stat (OUTDIR FILENAME, &st);
if (!ret)
printf ("stat 1: ret = %d, errno = %d %s\n", ret, errno,
st.st_size == 11 ? "OK" : "");
else
printf ("stat 1: ret = %d, errno = %d\n", ret, errno);
stop ();
/* NULL pathname */
errno = 0;
ret = stat (null_str, &st);
printf ("stat 2: ret = %d, errno = %d %s\n", ret, errno,
strerrno (errno));
stop ();
/* Empty pathname */
errno = 0;
ret = stat ("", &st);
printf ("stat 3: ret = %d, errno = %d %s\n", ret, errno,
strerrno (errno));
stop ();
/* Nonexistant file */
errno = 0;
ret = stat (NONEXISTANT, &st);
printf ("stat 4: ret = %d, errno = %d %s\n", ret, errno,
strerrno (errno));
stop ();
}
void
test_fstat (void)
{
int fd, ret;
struct stat st;
/* Test fstat */
errno = 0;
fd = open (OUTDIR FILENAME, O_RDONLY);
if (fd >= 0)
{
errno = 0;
ret = fstat (fd, &st);
if (!ret)
printf ("fstat 1: ret = %d, errno = %d %s\n", ret, errno,
st.st_size == 11 ? "OK" : "");
else
printf ("fstat 1: ret = %d, errno = %d\n", ret, errno);
close (fd);
}
else
printf ("fstat 1: errno = %d\n", errno);
stop ();
/* Fstat using invalid file descriptor */
errno = 0;
ret = fstat (999, &st);
printf ("fstat 2: ret = %d, errno = %d %s\n", ret, errno,
strerrno (errno));
stop ();
}
void
test_isatty (void)
{
int fd;
/* Check std I/O */
printf ("isatty 1: stdin %s\n", isatty (0) ? "yes OK" : "no");
stop ();
printf ("isatty 2: stdout %s\n", isatty (1) ? "yes OK" : "no");
stop ();
printf ("isatty 3: stderr %s\n", isatty (2) ? "yes OK" : "no");
stop ();
/* Check invalid fd */
printf ("isatty 4: invalid %s\n", isatty (999) ? "yes" : "no OK");
stop ();
/* Check open file */
fd = open (OUTDIR FILENAME, O_RDONLY);
if (fd >= 0)
{
printf ("isatty 5: file %s\n", isatty (fd) ? "yes" : "no OK");
close (fd);
}
else
printf ("isatty 5: file couldn't open\n");
stop ();
}
char sys[1512];
void
test_system (void)
{
/*
* Requires test framework to switch on "set remote system-call-allowed 1"
*/
int ret;
/* Test for shell ('set remote system-call-allowed' is disabled
by default). */
ret = system (NULL);
printf ("system 1: ret = %d %s\n", ret, ret == 0 ? "OK" : "");
stop ();
/* Test for shell again (the testsuite will have enabled it now). */
ret = system (NULL);
printf ("system 2: ret = %d %s\n", ret, ret != 0 ? "OK" : "");
stop ();
/* This test prepares the directory for test_rename() */
sprintf (sys, "mkdir -p %s/%s %s/%s", OUTDIR, TESTSUBDIR, OUTDIR, TESTDIR2);
ret = system (sys);
if (ret == 127)
printf ("system 3: ret = %d /bin/sh unavailable???\n", ret);
else
printf ("system 3: ret = %d %s\n", ret, ret == 0 ? "OK" : "");
stop ();
/* Invalid command (just guessing ;-) ) */
ret = system ("wrtzlpfrmpft");
printf ("system 4: ret = %d %s\n", ret,
WEXITSTATUS (ret) == 127 ? "OK" : "");
stop ();
}
void
test_rename (void)
{
int ret;
struct stat st;
/* Test rename */
errno = 0;
ret = rename (OUTDIR FILENAME, OUTDIR RENAMED);
if (!ret)
{
errno = 0;
ret = stat (FILENAME, &st);
if (ret && errno == ENOENT)
{
errno = 0;
ret = stat (OUTDIR RENAMED, &st);
printf ("rename 1: ret = %d, errno = %d %s\n", ret, errno,
strerrno (errno));
errno = 0;
}
else
printf ("rename 1: ret = %d, errno = %d\n", ret, errno);
}
else
printf ("rename 1: ret = %d, errno = %d\n", ret, errno);
stop ();
/* newpath is existing directory, oldpath is not a directory */
errno = 0;
ret = rename (OUTDIR RENAMED, OUTDIR TESTDIR2);
printf ("rename 2: ret = %d, errno = %d %s\n", ret, errno,
strerrno (errno));
stop ();
/* newpath is a non-empty directory */
errno = 0;
ret = rename (OUTDIR TESTDIR2, OUTDIR TESTDIR1);
printf ("rename 3: ret = %d, errno = %d %s\n", ret, errno,
strerrno (errno));
stop ();
/* newpath is a subdirectory of old path */
errno = 0;
ret = rename (OUTDIR TESTDIR1, OUTDIR TESTSUBDIR);
printf ("rename 4: ret = %d, errno = %d %s\n", ret, errno,
strerrno (errno));
stop ();
/* oldpath does not exist */
errno = 0;
ret = rename (OUTDIR NONEXISTANT, OUTDIR FILENAME);
printf ("rename 5: ret = %d, errno = %d %s\n", ret, errno,
strerrno (errno));
stop ();
}
char name[1256];
void
test_unlink (void)
{
int ret;
/* Test unlink */
errno = 0;
ret = unlink (OUTDIR RENAMED);
printf ("unlink 1: ret = %d, errno = %d %s\n", ret, errno,
strerrno (errno));
stop ();
/* No write access */
sprintf (name, "%s/%s/%s", OUTDIR, TESTDIR2, FILENAME);
errno = 0;
ret = open (name, O_CREAT | O_RDONLY, S_IRUSR | S_IWUSR);
if (ret >= 0)
{
sprintf (sys, "chmod -w %s/%s", OUTDIR, TESTDIR2);
ret = system (sys);
if (!ret)
{
errno = 0;
ret = unlink (name);
printf ("unlink 2: ret = %d, errno = %d %s\n", ret, errno,
strerrno (errno));
}
else
printf ("unlink 2: ret = %d chmod failed, errno= %d\n", ret, errno);
}
else
printf ("unlink 2: ret = %d, errno = %d\n", ret, errno);
stop ();
/* pathname doesn't exist */
errno = 0;
ret = unlink (OUTDIR NONEXISTANT);
printf ("unlink 3: ret = %d, errno = %d %s\n", ret, errno,
strerrno (errno));
stop ();
}
void
test_time (void)
{
time_t ret, t;
errno = 0;
ret = time (&t);
printf ("time 1: ret = %ld, errno = %d, t = %ld %s\n", (long) ret, errno, (long) t, ret == t ? "OK" : "");
stop ();
errno = 0;
ret = time (NULL);
printf ("time 2: ret = %ld, errno = %d, t = %ld %s\n",
(long) ret, errno, (long) t, ret >= t && ret < t + 10 ? "OK" : "");
stop ();
}
static const char *
strerrno (int err)
{
switch (err)
{
case 0: return "OK";
#ifdef EACCES
case EACCES: return "EACCES";
#endif
#ifdef EBADF
case EBADF: return "EBADF";
#endif
#ifdef EEXIST
case EEXIST: return "EEXIST";
#endif
#ifdef EFAULT
case EFAULT: return "EFAULT";
#endif
#ifdef EINVAL
case EINVAL: return "EINVAL";
#endif
#ifdef EISDIR
case EISDIR: return "EISDIR";
#endif
#ifdef ENOENT
case ENOENT: return "ENOENT";
#endif
#ifdef ENOTEMPTY
case ENOTEMPTY: return "ENOTEMPTY";
#endif
#ifdef EBUSY
case EBUSY: return "EBUSY";
#endif
default: return "E??";
}
}
int
main ()
{
/* Don't change the order of the calls. They partly depend on each other */
test_open ();
test_write ();
test_read ();
test_lseek ();
test_close ();
test_stat ();
test_fstat ();
test_isatty ();
test_system ();
test_rename ();
test_unlink ();
test_time ();
return 0;
}
|