Close the file in fileio.exp test
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / fileio.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/errno.h>
5 #include <sys/types.h>
6 #include <sys/fcntl.h>
7 #include <sys/stat.h>
8 #include <sys/time.h>
9 #include <errno.h>
10 #include <sys/wait.h>
11 #include <unistd.h>
12 /* TESTS :
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
18
19 read(int fd, void *buf, size_t count);
20 1) Read using invalid file descriptor - EBADF
21
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
25
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
29
30 close(int fd);
31 1) Attempt to close an invalid file descriptor - EBADF
32
33 stat(const char *file_name, struct stat *buf);
34 1) Pathname is a null string - ENOENT
35 2) Pathname does not exist - ENOENT
36
37 fstat(int filedes, struct stat *buf);
38 1) Attempt to stat using an invalid file descriptor - EBADF
39
40 isatty (int desc);
41 Not applicable. We will test that it returns 1 when expected and a case
42 where it should return 0.
43
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
49
50 unlink(const char *pathname);
51 1) pathname does not have write access. - EACCES
52 2) pathname does not exist. - ENOENT
53
54 time(time_t *t);
55 Not applicable.
56
57 system (const char * string);
58 1) See if shell available - returns 0
59 2) See if shell available - returns !0
60 3) Execute simple shell command - returns 0
61 4) Invalid string/command. - returns 127. */
62
63 static const char *strerrno (int err);
64
65 /* Note that OUTDIR is defined by the test suite. */
66 #define FILENAME "foo.fileio.test"
67 #define RENAMED "bar.fileio.test"
68 #define NONEXISTANT "nofoo.fileio.test"
69 #define NOWRITE "nowrt.fileio.test"
70 #define TESTDIR1 "dir1.fileio.test"
71 #define TESTDIR2 "dir2.fileio.test"
72 #define TESTSUBDIR "dir1.fileio.test/subdir.fileio.test"
73
74 #define STRING "Hello World"
75
76 static void stop () {}
77
78 int
79 test_open ()
80 {
81 int ret;
82
83 /* Test opening */
84 errno = 0;
85 ret = open (OUTDIR FILENAME, O_CREAT | O_TRUNC | O_RDWR, S_IWUSR | S_IRUSR);
86 printf ("open 1: ret = %d, errno = %d %s\n", ret, errno,
87 ret >= 0 ? "OK" : "");
88
89 if (ret >= 0)
90 close (ret);
91 stop ();
92 /* Creating an already existing file (created by fileio.exp) */
93 errno = 0;
94 ret = open (OUTDIR FILENAME, O_CREAT | O_EXCL | O_WRONLY, S_IWUSR | S_IRUSR);
95 printf ("open 2: ret = %d, errno = %d %s\n", ret, errno,
96 strerrno (errno));
97 if (ret >= 0)
98 close (ret);
99 stop ();
100 /* Open directory (for writing) */
101 errno = 0;
102 ret = open (".", O_WRONLY);
103 printf ("open 3: ret = %d, errno = %d %s\n", ret, errno,
104 strerrno (errno));
105 if (ret >= 0)
106 close (ret);
107 stop ();
108 /* Opening nonexistant file */
109 errno = 0;
110 ret = open (NONEXISTANT, O_RDONLY);
111 printf ("open 4: ret = %d, errno = %d %s\n", ret, errno,
112 strerrno (errno));
113 if (ret >= 0)
114 close (ret);
115 stop ();
116 /* Open for write but no write permission */
117 errno = 0;
118 ret = open (OUTDIR NOWRITE, O_CREAT | O_RDONLY, S_IRUSR);
119 if (ret >= 0)
120 {
121 close (ret);
122 stop ();
123 errno = 0;
124 ret = open (OUTDIR NOWRITE, O_WRONLY);
125 printf ("open 5: ret = %d, errno = %d %s\n", ret, errno,
126 strerrno (errno));
127 if (ret >= 0)
128 close (ret);
129 }
130 else
131 {
132 stop ();
133 printf ("open 5: ret = %d, errno = %d\n", ret, errno);
134 }
135 stop ();
136 }
137
138 int
139 test_write ()
140 {
141 int fd, ret;
142
143 /* Test writing */
144 errno = 0;
145 fd = open (OUTDIR FILENAME, O_WRONLY);
146 if (fd >= 0)
147 {
148 errno = 0;
149 ret = write (fd, STRING, strlen (STRING));
150 printf ("write 1: ret = %d, errno = %d %s\n", ret, errno,
151 ret == strlen (STRING) ? "OK" : "");
152 close (fd);
153 }
154 else
155 printf ("write 1: ret = %d, errno = %d\n", ret, errno);
156 stop ();
157 /* Write using invalid file descriptor */
158 errno = 0;
159 ret = write (999, STRING, strlen (STRING));
160 printf ("write 2: ret = %d, errno = %d, %s\n", ret, errno,
161 strerrno (errno));
162 stop ();
163 /* Write to a read-only file */
164 errno = 0;
165 fd = open (OUTDIR FILENAME, O_RDONLY);
166 if (fd >= 0)
167 {
168 errno = 0;
169 ret = write (fd, STRING, strlen (STRING));
170 printf ("write 3: ret = %d, errno = %d %s\n", ret, errno,
171 strerrno (errno));
172 close (fd);
173 }
174 else
175 printf ("write 3: ret = %d, errno = %d\n", ret, errno);
176 stop ();
177 }
178
179 int
180 test_read ()
181 {
182 int fd, ret;
183 char buf[16];
184
185 /* Test reading */
186 errno = 0;
187 fd = open (OUTDIR FILENAME, O_RDONLY);
188 if (fd >= 0)
189 {
190 memset (buf, 0, 16);
191 errno = 0;
192 ret = read (fd, buf, 16);
193 buf[15] = '\0'; /* Don't trust anybody... */
194 if (ret == strlen (STRING))
195 printf ("read 1: %s %s\n", buf, !strcmp (buf, STRING) ? "OK" : "");
196 else
197 printf ("read 1: ret = %d, errno = %d\n", ret, errno);
198 close (fd);
199 }
200 else
201 printf ("read 1: ret = %d, errno = %d\n", ret, errno);
202 stop ();
203 /* Read using invalid file descriptor */
204 errno = 0;
205 ret = read (999, buf, 16);
206 printf ("read 2: ret = %d, errno = %d %s\n", ret, errno,
207 strerrno (errno));
208 stop ();
209 }
210
211 int
212 test_lseek ()
213 {
214 int fd;
215 off_t ret = 0;
216
217 /* Test seeking */
218 errno = 0;
219 fd = open (OUTDIR FILENAME, O_RDONLY);
220 if (fd >= 0)
221 {
222 errno = 0;
223 ret = lseek (fd, 0, SEEK_CUR);
224 printf ("lseek 1: ret = %ld, errno = %d, %s\n", (long) ret, errno,
225 ret == 0 ? "OK" : "");
226 stop ();
227 errno = 0;
228 ret = lseek (fd, 0, SEEK_END);
229 printf ("lseek 2: ret = %ld, errno = %d, %s\n", (long) ret, errno,
230 ret == 11 ? "OK" : "");
231 stop ();
232 errno = 0;
233 ret = lseek (fd, 3, SEEK_SET);
234 printf ("lseek 3: ret = %ld, errno = %d, %s\n", (long) ret, errno,
235 ret == 3 ? "OK" : "");
236 close (fd);
237 }
238 else
239 {
240 printf ("lseek 1: ret = %ld, errno = %d %s\n", (long) ret, errno,
241 strerrno (errno));
242 stop ();
243 printf ("lseek 2: ret = %ld, errno = %d %s\n", (long) ret, errno,
244 strerrno (errno));
245 stop ();
246 printf ("lseek 3: ret = %ld, errno = %d %s\n", (long) ret, errno,
247 strerrno (errno));
248 }
249 /* Seeking on an invalid file descriptor */
250 stop ();
251 }
252
253 int
254 test_close ()
255 {
256 int fd, ret;
257
258 /* Test close */
259 errno = 0;
260 fd = open (OUTDIR FILENAME, O_RDONLY);
261 if (fd >= 0)
262 {
263 errno = 0;
264 ret = close (fd);
265 printf ("close 1: ret = %d, errno = %d, %s\n", ret, errno,
266 ret == 0 ? "OK" : "");
267 }
268 else
269 printf ("close 1: ret = %d, errno = %d\n", ret, errno);
270 stop ();
271 /* Close an invalid file descriptor */
272 errno = 0;
273 ret = close (999);
274 printf ("close 2: ret = %d, errno = %d, %s\n", ret, errno,
275 strerrno (errno));
276 stop ();
277 }
278
279 int
280 test_stat ()
281 {
282 int ret;
283 struct stat st;
284
285 /* Test stat */
286 errno = 0;
287 ret = stat (OUTDIR FILENAME, &st);
288 if (!ret)
289 printf ("stat 1: ret = %d, errno = %d %s\n", ret, errno,
290 st.st_size == 11 ? "OK" : "");
291 else
292 printf ("stat 1: ret = %d, errno = %d\n", ret, errno);
293 stop ();
294 /* NULL pathname */
295 errno = 0;
296 ret = stat (NULL, &st);
297 printf ("stat 2: ret = %d, errno = %d %s\n", ret, errno,
298 strerrno (errno));
299 stop ();
300 /* Empty pathname */
301 errno = 0;
302 ret = stat ("", &st);
303 printf ("stat 3: ret = %d, errno = %d %s\n", ret, errno,
304 strerrno (errno));
305 stop ();
306 /* Nonexistant file */
307 errno = 0;
308 ret = stat (NONEXISTANT, &st);
309 printf ("stat 4: ret = %d, errno = %d %s\n", ret, errno,
310 strerrno (errno));
311 stop ();
312 }
313
314 int
315 test_fstat ()
316 {
317 int fd, ret;
318 struct stat st;
319
320 /* Test fstat */
321 errno = 0;
322 fd = open (OUTDIR FILENAME, O_RDONLY);
323 if (fd >= 0)
324 {
325 errno = 0;
326 ret = fstat (fd, &st);
327 if (!ret)
328 printf ("fstat 1: ret = %d, errno = %d %s\n", ret, errno,
329 st.st_size == 11 ? "OK" : "");
330 else
331 printf ("fstat 1: ret = %d, errno = %d\n", ret, errno);
332 close (fd);
333 }
334 else
335 printf ("fstat 1: ret = %d, errno = %d\n", ret, errno);
336 stop ();
337 /* Fstat using invalid file descriptor */
338 errno = 0;
339 ret = fstat (999, &st);
340 printf ("fstat 2: ret = %d, errno = %d %s\n", ret, errno,
341 strerrno (errno));
342 stop ();
343 }
344
345 int
346 test_isatty ()
347 {
348 int fd;
349
350 /* Check std I/O */
351 printf ("isatty 1: stdin %s\n", isatty (0) ? "yes OK" : "no");
352 stop ();
353 printf ("isatty 2: stdout %s\n", isatty (1) ? "yes OK" : "no");
354 stop ();
355 printf ("isatty 3: stderr %s\n", isatty (2) ? "yes OK" : "no");
356 stop ();
357 /* Check invalid fd */
358 printf ("isatty 4: invalid %s\n", isatty (999) ? "yes" : "no OK");
359 stop ();
360 /* Check open file */
361 fd = open (OUTDIR FILENAME, O_RDONLY);
362 if (fd >= 0)
363 {
364 printf ("isatty 5: file %s\n", isatty (fd) ? "yes" : "no OK");
365 close (fd);
366 }
367 else
368 printf ("isatty 5: file couldn't open\n");
369 stop ();
370 }
371
372
373 char sys[1512];
374
375 int
376 test_system ()
377 {
378 /*
379 * Requires test framework to switch on "set remote system-call-allowed 1"
380 */
381 int ret;
382
383 /* Test for shell ('set remote system-call-allowed' is disabled
384 by default). */
385 ret = system (NULL);
386 printf ("system 1: ret = %d %s\n", ret, ret == 0 ? "OK" : "");
387 stop ();
388 /* Test for shell again (the testsuite will have enabled it now). */
389 ret = system (NULL);
390 printf ("system 2: ret = %d %s\n", ret, ret != 0 ? "OK" : "");
391 stop ();
392 /* This test prepares the directory for test_rename() */
393 sprintf (sys, "mkdir -p %s/%s %s/%s", OUTDIR, TESTSUBDIR, OUTDIR, TESTDIR2);
394 ret = system (sys);
395 if (ret == 127)
396 printf ("system 3: ret = %d /bin/sh unavailable???\n", ret);
397 else
398 printf ("system 3: ret = %d %s\n", ret, ret == 0 ? "OK" : "");
399 stop ();
400 /* Invalid command (just guessing ;-) ) */
401 ret = system ("wrtzlpfrmpft");
402 printf ("system 4: ret = %d %s\n", ret,
403 WEXITSTATUS (ret) == 127 ? "OK" : "");
404 stop ();
405 }
406
407 int
408 test_rename ()
409 {
410 int ret;
411 struct stat st;
412
413 /* Test rename */
414 errno = 0;
415 ret = rename (OUTDIR FILENAME, OUTDIR RENAMED);
416 if (!ret)
417 {
418 errno = 0;
419 ret = stat (FILENAME, &st);
420 if (ret && errno == ENOENT)
421 {
422 errno = 0;
423 ret = stat (OUTDIR RENAMED, &st);
424 printf ("rename 1: ret = %d, errno = %d %s\n", ret, errno,
425 strerrno (errno));
426 errno = 0;
427 }
428 else
429 printf ("rename 1: ret = %d, errno = %d\n", ret, errno);
430 }
431 else
432 printf ("rename 1: ret = %d, errno = %d\n", ret, errno);
433 stop ();
434 /* newpath is existing directory, oldpath is not a directory */
435 errno = 0;
436 ret = rename (OUTDIR RENAMED, OUTDIR TESTDIR2);
437 printf ("rename 2: ret = %d, errno = %d %s\n", ret, errno,
438 strerrno (errno));
439 stop ();
440 /* newpath is a non-empty directory */
441 errno = 0;
442 ret = rename (OUTDIR TESTDIR2, OUTDIR TESTDIR1);
443 printf ("rename 3: ret = %d, errno = %d %s\n", ret, errno,
444 strerrno (errno));
445 stop ();
446 /* newpath is a subdirectory of old path */
447 errno = 0;
448 ret = rename (OUTDIR TESTDIR1, OUTDIR TESTSUBDIR);
449 printf ("rename 4: ret = %d, errno = %d %s\n", ret, errno,
450 strerrno (errno));
451 stop ();
452 /* oldpath does not exist */
453 errno = 0;
454 ret = rename (OUTDIR NONEXISTANT, OUTDIR FILENAME);
455 printf ("rename 5: ret = %d, errno = %d %s\n", ret, errno,
456 strerrno (errno));
457 stop ();
458 }
459
460 char name[1256];
461
462 int
463 test_unlink ()
464 {
465 int ret;
466
467 /* Test unlink */
468 errno = 0;
469 ret = unlink (OUTDIR RENAMED);
470 printf ("unlink 1: ret = %d, errno = %d %s\n", ret, errno,
471 strerrno (errno));
472 stop ();
473 /* No write access */
474 sprintf (name, "%s/%s/%s", OUTDIR, TESTDIR2, FILENAME);
475 errno = 0;
476 ret = open (name, O_CREAT | O_RDONLY, S_IRUSR | S_IWUSR);
477 if (ret >= 0)
478 {
479 sprintf (sys, "chmod -w %s/%s", OUTDIR, TESTDIR2);
480 ret = system (sys);
481 if (!ret)
482 {
483 errno = 0;
484 ret = unlink (name);
485 printf ("unlink 2: ret = %d, errno = %d %s\n", ret, errno,
486 strerrno (errno));
487 }
488 else
489 printf ("unlink 2: ret = %d chmod failed, errno= %d\n", ret, errno);
490 }
491 else
492 printf ("unlink 2: ret = %d, errno = %d\n", ret, errno);
493 stop ();
494 /* pathname doesn't exist */
495 errno = 0;
496 ret = unlink (OUTDIR NONEXISTANT);
497 printf ("unlink 3: ret = %d, errno = %d %s\n", ret, errno,
498 strerrno (errno));
499 stop ();
500 }
501
502 int
503 test_time ()
504 {
505 time_t ret, t;
506
507 errno = 0;
508 ret = time (&t);
509 printf ("time 1: ret = %ld, errno = %d, t = %ld %s\n", (long) ret, errno, (long) t, ret == t ? "OK" : "");
510 stop ();
511 errno = 0;
512 ret = time (NULL);
513 printf ("time 2: ret = %ld, errno = %d, t = %ld %s\n",
514 (long) ret, errno, (long) t, ret >= t && ret < t + 10 ? "OK" : "");
515 stop ();
516 }
517
518 static const char *
519 strerrno (int err)
520 {
521 switch (err)
522 {
523 case 0: return "OK";
524 #ifdef EACCES
525 case EACCES: return "EACCES";
526 #endif
527 #ifdef EBADF
528 case EBADF: return "EBADF";
529 #endif
530 #ifdef EEXIST
531 case EEXIST: return "EEXIST";
532 #endif
533 #ifdef EFAULT
534 case EFAULT: return "EFAULT";
535 #endif
536 #ifdef EINVAL
537 case EINVAL: return "EINVAL";
538 #endif
539 #ifdef EISDIR
540 case EISDIR: return "EISDIR";
541 #endif
542 #ifdef ENOENT
543 case ENOENT: return "ENOENT";
544 #endif
545 #ifdef ENOTEMPTY
546 case ENOTEMPTY: return "ENOTEMPTY";
547 #endif
548 #ifdef EBUSY
549 case EBUSY: return "EBUSY";
550 #endif
551 default: return "E??";
552 }
553 }
554
555 int
556 main ()
557 {
558 /* Don't change the order of the calls. They partly depend on each other */
559 test_open ();
560 test_write ();
561 test_read ();
562 test_lseek ();
563 test_close ();
564 test_stat ();
565 test_fstat ();
566 test_isatty ();
567 test_system ();
568 test_rename ();
569 test_unlink ();
570 test_time ();
571 return 0;
572 }
This page took 0.058874 seconds and 5 git commands to generate.