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"
78 ret
= open (FILENAME
, O_CREAT
| O_TRUNC
| O_RDWR
, S_IWUSR
| S_IRUSR
);
79 printf ("open 1: ret = %d, errno = %d %s\n", ret
, errno
,
80 ret
>= 0 ? "OK" : "");
83 /* Creating an already existing file (created by fileio.exp) */
85 ret
= open (FILENAME
, O_CREAT
| O_EXCL
| O_WRONLY
, S_IWUSR
| S_IRUSR
);
86 printf ("open 2: ret = %d, errno = %d %s\n", ret
, errno
,
90 /* Open directory (for writing) */
92 ret
= open (".", O_WRONLY
);
93 printf ("open 3: ret = %d, errno = %d %s\n", ret
, errno
,
97 /* Opening nonexistant file */
99 ret
= open (NONEXISTANT
, O_RDONLY
);
100 printf ("open 4: ret = %d, errno = %d %s\n", ret
, errno
,
104 /* Open for write but no write permission */
106 ret
= open (NOWRITE
, O_CREAT
| O_RDONLY
, S_IRUSR
);
111 ret
= open (NOWRITE
, O_WRONLY
);
112 printf ("open 5: ret = %d, errno = %d %s\n", ret
, errno
,
118 printf ("open 5: ret = %d, errno = %d\n", ret
, errno
);
128 fd
= open (FILENAME
, O_WRONLY
);
132 ret
= write (fd
, STRING
, strlen (STRING
));
133 printf ("write 1: ret = %d, errno = %d %s\n", ret
, errno
,
134 ret
== strlen (STRING
) ? "OK" : "");
138 printf ("write 1: ret = %d, errno = %d\n", ret
, errno
);
139 /* Write using invalid file descriptor */
141 ret
= write (999, STRING
, strlen (STRING
));
142 printf ("write 2: ret = %d, errno = %d, %s\n", ret
, errno
,
144 /* Write to a read-only file */
146 fd
= open (FILENAME
, O_RDONLY
);
150 ret
= write (fd
, STRING
, strlen (STRING
));
151 printf ("write 3: ret = %d, errno = %d %s\n", ret
, errno
,
155 printf ("write 3: ret = %d, errno = %d\n", ret
, errno
);
166 fd
= open (FILENAME
, O_RDONLY
);
171 ret
= read (fd
, buf
, 16);
172 buf
[15] = '\0'; /* Don't trust anybody... */
173 if (ret
== strlen (STRING
))
174 printf ("read 1: %s %s\n", buf
, !strcmp (buf
, STRING
) ? "OK" : "");
176 printf ("read 1: ret = %d, errno = %d\n", ret
, errno
);
180 printf ("read 1: ret = %d, errno = %d\n", ret
, errno
);
181 /* Read using invalid file descriptor */
183 ret
= read (999, buf
, 16);
184 printf ("read 2: ret = %d, errno = %d %s\n", ret
, errno
,
196 fd
= open (FILENAME
, O_RDONLY
);
200 ret
= lseek (fd
, 0, SEEK_CUR
);
201 printf ("lseek 1: ret = %ld, errno = %d, %s\n", (long) ret
, errno
,
202 ret
== 0 ? "OK" : "");
204 ret
= lseek (fd
, 0, SEEK_END
);
205 printf ("lseek 2: ret = %ld, errno = %d, %s\n", (long) ret
, errno
,
206 ret
== 11 ? "OK" : "");
208 ret
= lseek (fd
, 3, SEEK_SET
);
209 printf ("lseek 3: ret = %ld, errno = %d, %s\n", (long) ret
, errno
,
210 ret
== 3 ? "OK" : "");
215 printf ("lseek 1: ret = %d, errno = %d\n", ret
, errno
);
216 printf ("lseek 2: ret = %d, errno = %d\n", ret
, errno
);
217 printf ("lseek 3: ret = %d, errno = %d\n", ret
, errno
);
219 /* Seeking on an invalid file descriptor */
230 fd
= open (FILENAME
, O_RDONLY
);
235 printf ("close 1: ret = %d, errno = %d, %s\n", ret
, errno
,
236 ret
== 0 ? "OK" : "");
239 printf ("close 1: ret = %d, errno = %d\n", ret
, errno
);
240 /* Close an invalid file descriptor */
243 printf ("close 2: ret = %d, errno = %d, %s\n", ret
, errno
,
255 ret
= stat (FILENAME
, &st
);
257 printf ("stat 1: ret = %d, errno = %d %s\n", ret
, errno
,
258 st
.st_size
== 11 ? "OK" : "");
260 printf ("stat 1: ret = %d, errno = %d\n", ret
, errno
);
263 ret
= stat (NULL
, &st
);
264 printf ("stat 2: ret = %d, errno = %d %s\n", ret
, errno
,
268 ret
= stat ("", &st
);
269 printf ("stat 3: ret = %d, errno = %d %s\n", ret
, errno
,
271 /* Nonexistant file */
273 ret
= stat (NONEXISTANT
, &st
);
274 printf ("stat 4: ret = %d, errno = %d %s\n", ret
, errno
,
286 fd
= open (FILENAME
, O_RDONLY
);
290 ret
= fstat (fd
, &st
);
292 printf ("fstat 1: ret = %d, errno = %d %s\n", ret
, errno
,
293 st
.st_size
== 11 ? "OK" : "");
295 printf ("fstat 1: ret = %d, errno = %d\n", ret
, errno
);
299 printf ("fstat 1: ret = %d, errno = %d\n", ret
, errno
);
300 /* Fstat using invalid file descriptor */
302 ret
= fstat (999, &st
);
303 printf ("fstat 2: ret = %d, errno = %d %s\n", ret
, errno
,
313 printf ("isatty 1: stdin %s\n", isatty (0) ? "yes OK" : "no");
314 printf ("isatty 2: stdout %s\n", isatty (1) ? "yes OK" : "no");
315 printf ("isatty 3: stderr %s\n", isatty (2) ? "yes OK" : "no");
316 /* Check invalid fd */
317 printf ("isatty 4: invalid %s\n", isatty (999) ? "yes" : "no OK");
318 /* Check open file */
319 fd
= open (FILENAME
, O_RDONLY
);
322 printf ("isatty 5: file %s\n", isatty (fd
) ? "yes" : "no OK");
326 printf ("isatty 5: file couldn't open\n");
334 * Requires test framework to switch on "set remote system-call-allowed 1"
339 /* This test prepares the directory for test_rename() */
340 sprintf (sys
, "mkdir -p %s %s", TESTSUBDIR
, TESTDIR2
);
343 printf ("system 1: ret = %d /bin/sh unavailable???\n", ret
);
345 printf ("system 1: ret = %d %s\n", ret
, ret
== 0 ? "OK" : "");
346 /* Invalid command (just guessing ;-) ) */
347 ret
= system ("wrtzlpfrmpft");
348 printf ("system 2: ret = %d %s\n", ret
, WEXITSTATUS (ret
) == 127 ? "OK" : "");
359 ret
= rename (FILENAME
, RENAMED
);
363 ret
= stat (FILENAME
, &st
);
364 if (ret
&& errno
== ENOENT
)
367 ret
= stat (RENAMED
, &st
);
368 printf ("rename 1: ret = %d, errno = %d %s\n", ret
, errno
,
373 printf ("rename 1: ret = %d, errno = %d\n", ret
, errno
);
376 printf ("rename 1: ret = %d, errno = %d\n", ret
, errno
);
377 /* newpath is existing directory, oldpath is not a directory */
379 ret
= rename (RENAMED
, TESTDIR2
);
380 printf ("rename 2: ret = %d, errno = %d %s\n", ret
, errno
,
382 /* newpath is a non-empty directory */
384 ret
= rename (TESTDIR2
, TESTDIR1
);
385 printf ("rename 3: ret = %d, errno = %d %s\n", ret
, errno
,
387 /* newpath is a subdirectory of old path */
389 ret
= rename (TESTDIR1
, TESTSUBDIR
);
390 printf ("rename 4: ret = %d, errno = %d %s\n", ret
, errno
,
392 /* oldpath does not exist */
394 ret
= rename (NONEXISTANT
, FILENAME
);
395 printf ("rename 5: ret = %d, errno = %d %s\n", ret
, errno
,
408 ret
= unlink (RENAMED
);
409 printf ("unlink 1: ret = %d, errno = %d %s\n", ret
, errno
,
411 /* No write access */
412 sprintf (name
, "%s/%s", TESTDIR2
, FILENAME
);
414 ret
= open (name
, O_CREAT
| O_RDONLY
, S_IRUSR
| S_IWUSR
);
417 sprintf (sys
, "chmod -w %s", TESTDIR2
);
423 printf ("unlink 2: ret = %d, errno = %d %s\n", ret
, errno
,
427 printf ("unlink 2: ret = %d chmod failed\n", ret
, errno
);
430 printf ("unlink 2: ret = %d, errno = %d\n", ret
, errno
);
431 /* pathname doesn't exist */
433 ret
= unlink (NONEXISTANT
);
434 printf ("unlink 3: ret = %d, errno = %d %s\n", ret
, errno
,
445 printf ("time 1: ret = %ld, errno = %d, t = %ld %s\n", (long) ret
, errno
, (long) t
, ret
== t
? "OK" : "");
448 printf ("time 2: ret = %ld, errno = %d, t = %ld %s\n",
449 (long) ret
, errno
, (long) t
, ret
>= t
&& ret
< t
+ 10 ? "OK" : "");
459 case EACCES
: return "EACCES";
462 case EBADF
: return "EBADF";
465 case EEXIST
: return "EEXIST";
468 case EFAULT
: return "EFAULT";
471 case EINVAL
: return "EINVAL";
474 case EISDIR
: return "EISDIR";
477 case ENOENT
: return "ENOENT";
480 case ENOTEMPTY
: return "ENOTEMPTY";
483 case EBUSY
: return "EBUSY";
485 default: return "E??";
492 /* Don't change the order of the calls. They partly depend on each other */