13 * - open(const char *pathname, int flags, mode_t mode);
14 1) Attempt to create file that already exists - EEXIST
15 2) Attempt to open a directory for writing - EISDIR
16 3) Pathname does not exist - ENOENT
17 4) Open for write but no write permission - EACCES
19 read(int fd, void *buf, size_t count);
20 1) Read using invalid file descriptor - EBADF
22 write(int fd, const void *buf, size_t count);
23 1) Write using invalid file descriptor - EBADF
24 2) Attempt to write to read-only file - EBADF
26 lseek(int fildes, off_t offset, int whence);
27 1) Seeking on an invalid file descriptor - EBADF
28 2) Invalid "whence" (3rd param) value - EINVAL
31 1) Attempt to close an invalid file descriptor - EBADF
33 stat(const char *file_name, struct stat *buf);
34 1) Pathname is a null string - ENOENT
35 2) Pathname does not exist - ENOENT
37 fstat(int filedes, struct stat *buf);
38 1) Attempt to stat using an invalid file descriptor - EBADF
41 Not applicable. We will test that it returns 1 when expected and a case
42 where it should return 0.
44 rename(const char *oldpath, const char *newpath);
45 1) newpath is an existing directory, but oldpath is not a directory. - EISDIR
46 2) newpath is a non-empty directory. - ENOTEMPTY or EEXIST
47 3) newpath is a subdirectory of old path. - EINVAL
48 4) oldpath does not exist. - ENOENT
50 unlink(const char *pathname);
51 1) pathname does not have write access. - EACCES
52 2) pathname does not exist. - ENOENT
57 system (const char * string);
58 1) Invalid string/command. - returns 127. */
59 static const char *strerrno (int err
);
61 #define FILENAME "foo.fileio.test"
62 #define RENAMED "bar.fileio.test"
63 #define NONEXISTANT "nofoo.fileio.test"
64 #define NOWRITE "nowrt.fileio.test"
65 #define TESTDIR1 "dir1.fileio.test"
66 #define TESTDIR2 "dir2.fileio.test"
67 #define TESTSUBDIR "dir1.fileio.test/subdir.fileio.test"
69 #define STRING "Hello World"
71 static void stop () {}
80 ret
= open (FILENAME
, O_CREAT
| O_TRUNC
| O_RDWR
, S_IWUSR
| S_IRUSR
);
81 printf ("open 1: ret = %d, errno = %d %s\n", ret
, errno
,
82 ret
>= 0 ? "OK" : "");
87 /* Creating an already existing file (created by fileio.exp) */
89 ret
= open (FILENAME
, O_CREAT
| O_EXCL
| O_WRONLY
, S_IWUSR
| S_IRUSR
);
90 printf ("open 2: ret = %d, errno = %d %s\n", ret
, errno
,
95 /* Open directory (for writing) */
97 ret
= open (".", O_WRONLY
);
98 printf ("open 3: ret = %d, errno = %d %s\n", ret
, errno
,
103 /* Opening nonexistant file */
105 ret
= open (NONEXISTANT
, O_RDONLY
);
106 printf ("open 4: ret = %d, errno = %d %s\n", ret
, errno
,
111 /* Open for write but no write permission */
113 ret
= open (NOWRITE
, O_CREAT
| O_RDONLY
, S_IRUSR
);
119 ret
= open (NOWRITE
, O_WRONLY
);
120 printf ("open 5: ret = %d, errno = %d %s\n", ret
, errno
,
128 printf ("open 5: ret = %d, errno = %d\n", ret
, errno
);
140 fd
= open (FILENAME
, O_WRONLY
);
144 ret
= write (fd
, STRING
, strlen (STRING
));
145 printf ("write 1: ret = %d, errno = %d %s\n", ret
, errno
,
146 ret
== strlen (STRING
) ? "OK" : "");
150 printf ("write 1: ret = %d, errno = %d\n", ret
, errno
);
152 /* Write using invalid file descriptor */
154 ret
= write (999, STRING
, strlen (STRING
));
155 printf ("write 2: ret = %d, errno = %d, %s\n", ret
, errno
,
158 /* Write to a read-only file */
160 fd
= open (FILENAME
, O_RDONLY
);
164 ret
= write (fd
, STRING
, strlen (STRING
));
165 printf ("write 3: ret = %d, errno = %d %s\n", ret
, errno
,
169 printf ("write 3: ret = %d, errno = %d\n", ret
, errno
);
181 fd
= open (FILENAME
, O_RDONLY
);
186 ret
= read (fd
, buf
, 16);
187 buf
[15] = '\0'; /* Don't trust anybody... */
188 if (ret
== strlen (STRING
))
189 printf ("read 1: %s %s\n", buf
, !strcmp (buf
, STRING
) ? "OK" : "");
191 printf ("read 1: ret = %d, errno = %d\n", ret
, errno
);
195 printf ("read 1: ret = %d, errno = %d\n", ret
, errno
);
197 /* Read using invalid file descriptor */
199 ret
= read (999, buf
, 16);
200 printf ("read 2: ret = %d, errno = %d %s\n", ret
, errno
,
213 fd
= open (FILENAME
, O_RDONLY
);
217 ret
= lseek (fd
, 0, SEEK_CUR
);
218 printf ("lseek 1: ret = %ld, errno = %d, %s\n", (long) ret
, errno
,
219 ret
== 0 ? "OK" : "");
222 ret
= lseek (fd
, 0, SEEK_END
);
223 printf ("lseek 2: ret = %ld, errno = %d, %s\n", (long) ret
, errno
,
224 ret
== 11 ? "OK" : "");
227 ret
= lseek (fd
, 3, SEEK_SET
);
228 printf ("lseek 3: ret = %ld, errno = %d, %s\n", (long) ret
, errno
,
229 ret
== 3 ? "OK" : "");
234 printf ("lseek 1: ret = %d, errno = %d %s\n", ret
, errno
,
237 printf ("lseek 2: ret = %d, errno = %d %s\n", ret
, errno
,
240 printf ("lseek 3: ret = %d, errno = %d %s\n", ret
, errno
,
243 /* Seeking on an invalid file descriptor */
254 fd
= open (FILENAME
, O_RDONLY
);
259 printf ("close 1: ret = %d, errno = %d, %s\n", ret
, errno
,
260 ret
== 0 ? "OK" : "");
263 printf ("close 1: ret = %d, errno = %d\n", ret
, errno
);
265 /* Close an invalid file descriptor */
268 printf ("close 2: ret = %d, errno = %d, %s\n", ret
, errno
,
281 ret
= stat (FILENAME
, &st
);
283 printf ("stat 1: ret = %d, errno = %d %s\n", ret
, errno
,
284 st
.st_size
== 11 ? "OK" : "");
286 printf ("stat 1: ret = %d, errno = %d\n", ret
, errno
);
290 ret
= stat (NULL
, &st
);
291 printf ("stat 2: ret = %d, errno = %d %s\n", ret
, errno
,
296 ret
= stat ("", &st
);
297 printf ("stat 3: ret = %d, errno = %d %s\n", ret
, errno
,
300 /* Nonexistant file */
302 ret
= stat (NONEXISTANT
, &st
);
303 printf ("stat 4: ret = %d, errno = %d %s\n", ret
, errno
,
316 fd
= open (FILENAME
, O_RDONLY
);
320 ret
= fstat (fd
, &st
);
322 printf ("fstat 1: ret = %d, errno = %d %s\n", ret
, errno
,
323 st
.st_size
== 11 ? "OK" : "");
325 printf ("fstat 1: ret = %d, errno = %d\n", ret
, errno
);
329 printf ("fstat 1: ret = %d, errno = %d\n", ret
, errno
);
331 /* Fstat using invalid file descriptor */
333 ret
= fstat (999, &st
);
334 printf ("fstat 2: ret = %d, errno = %d %s\n", ret
, errno
,
345 printf ("isatty 1: stdin %s\n", isatty (0) ? "yes OK" : "no");
347 printf ("isatty 2: stdout %s\n", isatty (1) ? "yes OK" : "no");
349 printf ("isatty 3: stderr %s\n", isatty (2) ? "yes OK" : "no");
351 /* Check invalid fd */
352 printf ("isatty 4: invalid %s\n", isatty (999) ? "yes" : "no OK");
354 /* Check open file */
355 fd
= open (FILENAME
, O_RDONLY
);
358 printf ("isatty 5: file %s\n", isatty (fd
) ? "yes" : "no OK");
362 printf ("isatty 5: file couldn't open\n");
371 * Requires test framework to switch on "set remote system-call-allowed 1"
378 printf ("system 1: ret = %d %s\n", ret
, ret
!= 0 ? "OK" : "");
380 /* This test prepares the directory for test_rename() */
381 sprintf (sys
, "mkdir -p %s %s", TESTSUBDIR
, TESTDIR2
);
384 printf ("system 2: ret = %d /bin/sh unavailable???\n", ret
);
386 printf ("system 2: ret = %d %s\n", ret
, ret
== 0 ? "OK" : "");
388 /* Invalid command (just guessing ;-) ) */
389 ret
= system ("wrtzlpfrmpft");
390 printf ("system 3: ret = %d %s\n", ret
, WEXITSTATUS (ret
) == 127 ? "OK" : "");
402 ret
= rename (FILENAME
, RENAMED
);
406 ret
= stat (FILENAME
, &st
);
407 if (ret
&& errno
== ENOENT
)
410 ret
= stat (RENAMED
, &st
);
411 printf ("rename 1: ret = %d, errno = %d %s\n", ret
, errno
,
416 printf ("rename 1: ret = %d, errno = %d\n", ret
, errno
);
419 printf ("rename 1: ret = %d, errno = %d\n", ret
, errno
);
421 /* newpath is existing directory, oldpath is not a directory */
423 ret
= rename (RENAMED
, TESTDIR2
);
424 printf ("rename 2: ret = %d, errno = %d %s\n", ret
, errno
,
427 /* newpath is a non-empty directory */
429 ret
= rename (TESTDIR2
, TESTDIR1
);
430 printf ("rename 3: ret = %d, errno = %d %s\n", ret
, errno
,
433 /* newpath is a subdirectory of old path */
435 ret
= rename (TESTDIR1
, TESTSUBDIR
);
436 printf ("rename 4: ret = %d, errno = %d %s\n", ret
, errno
,
439 /* oldpath does not exist */
441 ret
= rename (NONEXISTANT
, FILENAME
);
442 printf ("rename 5: ret = %d, errno = %d %s\n", ret
, errno
,
456 ret
= unlink (RENAMED
);
457 printf ("unlink 1: ret = %d, errno = %d %s\n", ret
, errno
,
460 /* No write access */
461 sprintf (name
, "%s/%s", TESTDIR2
, FILENAME
);
463 ret
= open (name
, O_CREAT
| O_RDONLY
, S_IRUSR
| S_IWUSR
);
466 sprintf (sys
, "chmod -w %s", TESTDIR2
);
472 printf ("unlink 2: ret = %d, errno = %d %s\n", ret
, errno
,
476 printf ("unlink 2: ret = %d chmod failed\n", ret
, errno
);
479 printf ("unlink 2: ret = %d, errno = %d\n", ret
, errno
);
481 /* pathname doesn't exist */
483 ret
= unlink (NONEXISTANT
);
484 printf ("unlink 3: ret = %d, errno = %d %s\n", ret
, errno
,
496 printf ("time 1: ret = %ld, errno = %d, t = %ld %s\n", (long) ret
, errno
, (long) t
, ret
== t
? "OK" : "");
500 printf ("time 2: ret = %ld, errno = %d, t = %ld %s\n",
501 (long) ret
, errno
, (long) t
, ret
>= t
&& ret
< t
+ 10 ? "OK" : "");
512 case EACCES
: return "EACCES";
515 case EBADF
: return "EBADF";
518 case EEXIST
: return "EEXIST";
521 case EFAULT
: return "EFAULT";
524 case EINVAL
: return "EINVAL";
527 case EISDIR
: return "EISDIR";
530 case ENOENT
: return "ENOENT";
533 case ENOTEMPTY
: return "ENOTEMPTY";
536 case EBUSY
: return "EBUSY";
538 default: return "E??";
545 /* Don't change the order of the calls. They partly depend on each other */