summaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.base/fileio.c
blob: 591b3b67588921ceb513b6ba9655dc0599526149 (plain)
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
#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>
/* 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) Invalid string/command. -  returns 127.  */

static const char *strerrno (int err);

#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"

int
test_open ()
{
  int ret;

  /* Test opening */
  errno = 0;
  ret = open (FILENAME, O_CREAT | O_TRUNC | O_RDONLY, S_IWUSR | S_IRUSR);
  printf ("open 1: ret = %d, errno = %d %s\n", ret, errno,
	  ret >= 0 ? "OK" : "");
  if (ret >= 0)
    close (ret);
  /* Creating an already existing file (created by fileio.exp) */
  errno = 0;
  ret = open (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);
  /* 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);
  /* 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);
  /* Open for write but no write permission */
  errno = 0;
  ret = open (NOWRITE, O_CREAT | O_RDONLY, S_IRUSR | S_IWUSR);
  if (ret >= 0)
    {
      close (ret);
      errno = 0;
      ret = open (NOWRITE, O_WRONLY);
      printf ("open 5: ret = %d, errno = %d %s\n", ret, errno,
	      strerrno (errno));
      if (ret >= 0)
	close (ret);
    }
  else
    printf ("open 5: ret = %d, errno = %d\n", ret, errno);
}

int
test_write ()
{
  int fd, ret;

  /* Test writing */
  errno = 0;
  fd = open (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: ret = %d, errno = %d\n", ret, errno);
  /* 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));
  /* Write to a read-only file */
  errno = 0;
  fd = open (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));
    }
  else
    printf ("write 3: ret = %d, errno = %d\n", ret, errno);
}

int
test_read ()
{
  int fd, ret;
  char buf[16];

  /* Test reading */
  errno = 0;
  fd = open (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: ret = %d, errno = %d\n", ret, errno);
  /* 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));
}

int
test_lseek ()
{
  int fd;
  off_t ret = 0;

  /* Test seeking */
  errno = 0;
  fd = open (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" : "");
      errno = 0;
      ret = lseek (fd, 0, SEEK_END);
      printf ("lseek 2: ret = %ld, errno = %d, %s\n", (long) ret, errno,
              ret == 11 ? "OK" : "");
      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 = %d, errno = %d\n", ret, errno);
      printf ("lseek 2: ret = %d, errno = %d\n", ret, errno);
      printf ("lseek 3: ret = %d, errno = %d\n", ret, errno);
    }
  /* Seeking on an invalid file descriptor */

}

int
test_close ()
{
  int fd, ret;

  /* Test close */
  errno = 0;
  fd = open (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: ret = %d, errno = %d\n", ret, errno);
  /* Close an invalid file descriptor */
  errno = 0;
  ret = close (999);
  printf ("close 2: ret = %d, errno = %d, %s\n", ret, errno,
  	  strerrno (errno));
}

int
test_stat ()
{
  int ret;
  struct stat st;

  /* Test stat */
  errno = 0;
  ret = stat (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);
  /* NULL pathname */
  errno = 0;
  ret = stat (NULL, &st);
  printf ("stat 2: ret = %d, errno = %d %s\n", ret, errno,
  	  strerrno (errno));
  /* Empty pathname */
  errno = 0;
  ret = stat ("", &st);
  printf ("stat 3: ret = %d, errno = %d %s\n", ret, errno,
  	  strerrno (errno));
  /* Nonexistant file */
  errno = 0;
  ret = stat (NONEXISTANT, &st);
  printf ("stat 4: ret = %d, errno = %d %s\n", ret, errno,
  	  strerrno (errno));
}

int
test_fstat ()
{
  int fd, ret;
  struct stat st;

  /* Test fstat */
  errno = 0;
  fd = open (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: ret = %d, errno = %d\n", ret, errno);
  /* Fstat using invalid file descriptor */
  errno = 0;
  ret = fstat (999, &st);
  printf ("fstat 2: ret = %d, errno = %d %s\n", ret, errno,
  	  strerrno (errno));
}

int
test_isatty ()
{
  int fd;

  /* Check std I/O */
  printf ("isatty 1: stdin %s\n", isatty (0) ? "yes OK" : "no");
  printf ("isatty 2: stdout %s\n", isatty (1) ? "yes OK" : "no");
  printf ("isatty 3: stderr %s\n", isatty (2) ? "yes OK" : "no");
  /* Check invalid fd */
  printf ("isatty 4: invalid %s\n", isatty (999) ? "yes" : "no OK");
  /* Check open file */
  fd = open (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");
}


int
test_system ()
{
  /*
   * Requires test framework to switch on "set remote system-call-allowed 1"
   */
  int ret;
  char sys[512];

  /* This test prepares the directory for test_rename() */
  sprintf (sys, "mkdir -p %s %s", TESTSUBDIR, TESTDIR2);
  ret = system (sys);
  if (ret == 127)
    printf ("system 1: ret = %d /bin/sh unavailable???\n", ret);
  else
    printf ("system 1: ret = %d %s\n", ret, ret == 0 ? "OK" : "");
  /* Invalid command (just guessing ;-) ) */
  ret = system ("wrtzlpfrmpft");
  printf ("system 2: ret = %d %s\n", ret, WEXITSTATUS (ret) == 127 ? "OK" : "");
}

int
test_rename ()
{
  int ret;
  struct stat st;

  /* Test rename */
  errno = 0;
  ret = rename (FILENAME, RENAMED);
  if (!ret)
    {
      errno = 0;
      ret = stat (FILENAME, &st);
      if (ret && errno == ENOENT)
        {
	  errno = 0;
	  ret = stat (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);
  /* newpath is existing directory, oldpath is not a directory */
  errno = 0;
  ret = rename (RENAMED, TESTDIR2);
  printf ("rename 2: ret = %d, errno = %d %s\n", ret, errno,
	  strerrno (errno));
  /* newpath is a non-empty directory */
  errno = 0;
  ret = rename (TESTDIR2, TESTDIR1);
  printf ("rename 3: ret = %d, errno = %d %s\n", ret, errno,
          strerrno (errno));
  /* newpath is a subdirectory of old path */
  errno = 0;
  ret = rename (TESTDIR1, TESTSUBDIR);
  printf ("rename 4: ret = %d, errno = %d %s\n", ret, errno,
	  strerrno (errno));
  /* oldpath does not exist */
  errno = 0;
  ret = rename (NONEXISTANT, FILENAME);
  printf ("rename 5: ret = %d, errno = %d %s\n", ret, errno,
	  strerrno (errno));
}

int
test_unlink ()
{
  int ret;
  char name[256];
  char sys[512];

  /* Test unlink */
  errno = 0;
  ret = unlink (RENAMED);
  printf ("unlink 1: ret = %d, errno = %d %s\n", ret, errno,
	  strerrno (errno));
  /* No write access */
  sprintf (name, "%s/%s", TESTDIR2, FILENAME);
  errno = 0;
  ret = open (name, O_CREAT | O_RDONLY, S_IRUSR | S_IWUSR);
  if (ret >= 0)
    {
      sprintf (sys, "chmod -w %s", 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\n", ret, errno);
    }
  else
    printf ("unlink 2: ret = %d, errno = %d\n", ret, errno);
  /* pathname doesn't exist */
  errno = 0;
  ret = unlink (NONEXISTANT);
  printf ("unlink 3: ret = %d, errno = %d %s\n", ret, errno,
          strerrno (errno));
}

int
test_time ()
{
  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" : "");
  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" : "");
}

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
    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;
}