source: rtems/testsuites/psxtests/psx13/test.c @ 0895bdb

4.104.114.84.95
Last change on this file since 0895bdb was 0895bdb, checked in by Joel Sherrill <joel.sherrill@…>, on 11/23/98 at 18:57:48

Added tests in support of the file system infrastructure.

  • Property mode set to 100644
File size: 13.8 KB
Line 
1/*
2 *  Psx13
3 *  Chris Bond (working under Jennifer's account)
4 *
5 *  This test exercises the following routines:
6 *
7 *     device_lseek - test implemented
8 *     dup          - test implemented
9 *     dup2         - test implemented
10 *     fdatasync    - test implemented
11 *     fsync        - test implemented
12 *     pathconf     - test implemented
13 *     fpathconf    - test implemented
14 *     pipe         - test implemented
15 *     umask        - test implemented
16 *     utime        - test implemented
17 *
18 *  COPYRIGHT (c) 1989-1998.
19 *  On-Line Applications Research Corporation (OAR).
20 *  Copyright assigned to U.S. Government, 1994.
21 *
22 *  The license and distribution terms for this file may be
23 *  found in the file LICENSE in this distribution or at
24 *  http://www.OARcorp.com/rtems/license.html.
25 *
26 *  $Id$
27 */
28
29#include <rtems.h>
30#include <rtems/libio.h>
31#include <fcntl.h>
32#include <unistd.h>
33#include <errno.h>
34#include <utime.h>
35
36#include <stdio.h>
37
38/*-------------------------------------------------------------------
39 * InitFiles function
40 *
41 * Initializes the three files to be used in the test.
42 *
43 * arguments: none
44 * assumptions: fopen, fprintf, fwrite, FILE are available
45 * actions: creates testfile1, a text file with 'a'..'z' listed 4 times.
46 *          creates testfile2, a text file with 'a'..'z' listed 4 times.
47 *          creates testfile3, a binary file with 0..9 listed 4 times.
48 * returns: TRUE if files opened successfully.
49 *          FALSE if fail on file open for write.
50 *
51 * ------------------------------------------------------------------
52 */
53
54int InitFiles (void) {
55
56  int count;
57  FILE *fp1, *fp2, *fp3;
58  char letter;
59  int number;
60  int retval;
61
62  fp1 = fopen("testfile1.tst", "wt");
63  fp2 = fopen("testfile2.tst", "wt");
64  fp3 = fopen("testfile4.tst", "wb");
65
66  if ((fp1 != NULL) && (fp2 != NULL) && (fp3 !=NULL)) {
67
68    letter = 'a';
69
70    for (count=0 ; count<(26*4); ++count) {
71      fprintf (fp1, "%c", letter);
72      fprintf (fp2, "%c", letter);
73
74      ++letter;
75      if (letter > 'z')
76        letter = 'a';
77    }
78   
79    number = 0;
80
81    for (count = 0; count <40; ++count) {
82
83      fwrite (&number, 1, sizeof(int), fp3);
84
85      ++number;
86      if (number > 9)
87        number = 0;
88    }
89
90    fclose(fp1);
91    fclose(fp2);
92    fclose(fp3);
93
94    retval = TRUE;
95  }
96
97  else
98    retval = FALSE;
99
100  /* assert (retval == TRUE);*/
101
102  return (retval);
103}
104
105/* ---------------------------------------------------------------
106 * DeviceLSeekTest function
107 *
108 * Hits the device_lseek code by lseeking on the console.
109 *
110 * arguments: none
111 * assumptions: lseek available
112 * actions: hits lseek with some dummy arguments.
113 * returns: value of return from lseek.
114 *
115 * ---------------------------------------------------------------
116 */
117
118int DeviceLSeekTest (void) {
119
120  int error = -1, retval = FALSE;
121
122  int fd = open ("/dev/console", O_RDONLY);
123
124  error = lseek(fd, 5, SEEK_SET);
125
126  if (error == 0)
127    retval = TRUE;
128  else
129    retval = FALSE;
130
131  /* assert (retval == TRUE);*/
132
133  return (retval);
134
135}
136
137/* ---------------------------------------------------------------
138 * DupTest function
139 *
140 * Hits the dup code.
141 *
142 * arguments: none
143 * assumptions: dup, open, close, fcntl available.
144 * actions: Gets a file descriptor(fd1) for test file1.
145 *          dups fd1 to fd2.
146 *          sets fd1 to append mode
147 *          checks fd2 to ensure it's in append mode, also.
148 * returns: success if fd2 is indeed a copy of fd1.
149 *
150 * ---------------------------------------------------------------
151 */
152
153int DupTest(void) {
154
155  int fd1, fd2;
156
157  int flags = 0, retval = FALSE;
158
159  fd1 = open ("testfile1.tst", O_RDONLY);
160  fd2 = dup(fd1);
161
162  if (fd2 != -1) {
163
164    fcntl(F_SETFL, fd1, O_APPEND);
165    flags = fcntl(F_GETFL, fd2);
166
167    close (fd1);
168
169    flags = (flags & O_APPEND);
170   
171    retval = (flags == O_APPEND);
172  }
173
174  else
175    retval = FALSE;
176
177  /* assert (retval == TRUE);*/
178
179  return (retval);
180
181}
182
183/* ---------------------------------------------------------------
184 * Dup2Test function
185 *
186 * Hits the dup2 code.
187 *
188 * arguments: none
189 * assumptions: dup, dup2, open, close, fcntl available.
190 * actions: Gets a file descriptor(fd1) for test file1.
191 *          dups fd1 to fd2.
192 *          sets fd1 to append mode
193 *          checks fd2 to ensure it's in append mode, also.
194 *          sets fd1 to invalid value, fd2 to valid, tries to dup2.
195 *          sets fd2 to invalid value, fd1 to valid tries to dup2.
196 * returns: success if fd2 is a copy of fd1, and invalid fd1 or fd2 produce errors.
197 *
198 * ---------------------------------------------------------------
199 */
200
201int Dup2Test(void) {
202
203  int fd1, fd2;
204
205  int flags = 0, retval = FALSE;
206
207  int error = 0;
208
209  fd1 = open ("testfile1.tst", O_RDONLY);
210  fd2 = open ("testfile2.tst", O_RDONLY);
211  error = dup2(fd1, fd2);
212
213  /* make sure dup2 works if both fd1 and fd2 are valid file descriptors. */
214
215  if (error != -1) {
216
217    fcntl(F_SETFL, fd1, O_APPEND);
218    flags = fcntl(F_GETFL, fd1);
219
220    flags = (flags & O_APPEND);
221    retval = (flags == O_APPEND);
222  }
223
224  else {
225    retval = FALSE;
226    close(fd2);
227  }
228
229  if (retval == TRUE) {
230
231    /* make sure dup2 fails correctly if one or the other arguments are invalid. */
232    /* this assumes -1 is an invalid value for a file descriptor!!! (POSIX book, p.135) */
233
234    fd1 = -1;
235
236    if (dup2 (fd1, fd2) != -1)
237      retval = FALSE;
238    else {
239      fd1 = dup(fd2);
240      fd2 = -1;
241     
242      if (dup2(fd1, fd2) != -1)
243        retval = FALSE;
244    }
245  }
246
247  close (fd1);
248 
249  /* assert (retval == TRUE);*/
250
251  return (retval);
252
253}
254
255/* ---------------------------------------------------------------
256 * FDataSyncTest function
257 *
258 * Hits the fdatasync code. Does NOT test the functionality of the
259 * underlying fdatasync entry in the IMFS op table.
260 *
261 * arguments: none
262 * assumptions: open, close, fdatasync functions available.
263 * actions: attempts to fdatasync a file descriptor flagged as read-only.
264 *          attempts to fdatasync an invalid file descriptor (-1).
265 *          attempts to fdatasync a perfectly valid fd opened as RDWR
266 *
267 * returns: TRUE if attempt to fdatasync invalid and read-only filed escriptor fail, and fdatasync succeeds on valid fd.
268 *          FALSE otherwise.
269 *
270 * ---------------------------------------------------------------
271 */
272
273int FDataSyncTest(void) {
274
275  int fd = -1;
276  int error = 0, retval = TRUE;
277 
278  /* Try it with a RD_ONLY file. */
279 
280  fd = open ("testfile1.tst", O_RDONLY);
281
282  error = fdatasync(fd);
283  if ((error == -1) && (errno == EINVAL))
284    retval = TRUE;
285  else
286    retval = FALSE;
287 
288  close (fd);
289
290  if (retval == TRUE) {
291
292    /* Try it with a bad file descriptor */
293
294    fd = -1;
295
296    error = fdatasync(fd);
297    if ((errno == EBADF) && (error == -1))
298      retval = TRUE;
299    else
300      retval = FALSE;
301  }
302
303  /* Okay - now the success case... */
304
305  if (retval == TRUE) {
306    fd = open ("testfile1.tst", O_RDWR);
307    error = fdatasync(fd);
308
309    if (error == 0)
310      retval = TRUE;
311    else
312      retval = FALSE;
313
314    close (fd);
315 
316  }
317
318  /* assert (retval == TRUE);*/
319
320  return (retval);
321}
322
323/* ---------------------------------------------------------------
324 * UMaskTest function
325 *
326 * Hits the umask code.
327 *
328 * arguments: none
329 * assumptions: umask function available.
330 * actions: set umask to 0ctal 23.
331 *          set umask to Octal 22, retrieve the old value.
332 *
333 * returns: TRUE if old value is 23,
334 *          FALSE otherwise.
335 *
336 * ---------------------------------------------------------------
337 */
338
339int UMaskTest (void) {
340
341  int error = 0, retval = FALSE;
342
343  umask (023);
344  error = umask(022);
345
346  if (error == 023)
347    retval = TRUE;
348  else
349    retval = FALSE;
350
351  /* assert (retval == TRUE);*/
352
353  return(retval);
354
355}
356
357/* ---------------------------------------------------------------
358 * UTimeTest function
359 *
360 * Hits the utime code. Does NOT test the functionality of the underlying utime
361 * entry in the IMFS op table.
362 *
363 * arguments: none
364 * assumptions: utime function available.
365 * actions: set utime for an invalid filename.
366 *          set utime for a valid filename.
367 *
368 * returns: TRUE if time on valid file is set correctly and utime failed on an invaid filename.
369 *          FALSE otherwise.
370 *
371 * ---------------------------------------------------------------
372 */
373
374int UTimeTest (void) {
375
376  int error = 0, retval = FALSE;
377  struct utimbuf time;
378  struct stat fstat;
379
380  /* First, an invalid filename. */
381  error = utime("!This is an =invalid p@thname!!! :)", NULL);
382
383  if (error == -1)
384    retval = TRUE;
385  else
386    retval = FALSE;
387
388  /* Now, the success test. */
389  if (retval == TRUE) {
390
391    time.actime  = 12345;
392    time.modtime = 54321;
393
394    error = utime("testfile1.tst", &time);
395
396    if (error == 0) {
397
398      /* But, did it set the time? */
399      stat ("testfile1.tst", &fstat);
400
401      if ((fstat.st_atime == 12345) && (fstat.st_mtime == 54321 ))
402        retval = TRUE;
403      else
404        retval = FALSE;
405    }
406
407    else
408      retval = FALSE;
409  }
410
411  /* assert (retval == TRUE);*/
412
413  return (retval);
414
415}
416
417/* ---------------------------------------------------------------
418 * PipeTest function
419 *
420 * Hits the pipe code.
421 *
422 * arguments: none
423 * assumptions: pipe function available.
424 * actions: call pipe.
425 *
426 * returns: TRUE if pipe retuens ENOSYS,
427 *          FALSE otherwise.
428 *
429 * ---------------------------------------------------------------
430 */
431
432int PipeTest (void) {
433
434  int error = 0, retval = FALSE;
435  int fd[2];
436 
437  error = pipe(fd);
438
439  if ((error == -1) && (errno == ENOSYS))
440    retval = TRUE;
441  else
442    retval = FALSE;
443
444  /* assert (retval == TRUE);*/
445
446  return(retval);
447
448}
449
450/* ---------------------------------------------------------------
451 * PathConfTest function
452 *
453 * Hits the pathconf code.
454 *
455 * arguments: none
456 * assumptions: pathconf function available.
457 * actions: Try to pathconf a bad filename.
458 *          Try to pathconf a good filename.
459 *
460 * returns: TRUE if pathconf fails on bad file, succeeds on good file.
461 *          FALSE otherwise.
462 *
463 * ---------------------------------------------------------------
464 */
465
466int PathConfTest (void) {
467
468  int error = 0, retval = FALSE;
469
470  error = pathconf("thisfiledoesnotexist", _PC_LINK_MAX);
471
472  if (error == -1) {
473    error = pathconf("testfile1.tst", _PC_LINK_MAX);
474
475    if (error != -1)
476      retval = TRUE;
477    else
478      retval = FALSE;
479  }
480 
481  else
482    retval = FALSE;
483
484  /* assert (retval == TRUE);*/
485
486  return(retval);
487
488}
489
490/* ---------------------------------------------------------------
491 * FPathConfTest function
492 *
493 * Hits the fpathconf code.
494 *
495 * arguments: none
496 * assumptions: fpathconf function available.
497 * actions: Call fpathconf with all arguments, plus an invalid.
498 *
499 * returns: TRUE always.
500 *
501 * ---------------------------------------------------------------
502 */
503
504int FPathConfTest (void) {
505
506  int error = 0, retval = TRUE;
507
508  int fd  = -1;
509
510  error = fpathconf(fd, _PC_LINK_MAX);
511
512  if (error == -1) {
513    fd = open("testfile1.tst", O_RDWR);
514   
515    error = fpathconf(fd, _PC_LINK_MAX);
516    error = fpathconf(fd, _PC_MAX_CANON);
517    error = fpathconf(fd, _PC_MAX_INPUT);
518    error = fpathconf(fd, _PC_NAME_MAX);
519    error = fpathconf(fd, _PC_PATH_MAX);
520    error = fpathconf(fd, _PC_PIPE_BUF);
521    error = fpathconf(fd, _PC_CHOWN_RESTRICTED);
522    error = fpathconf(fd, _PC_NO_TRUNC);     
523    error = fpathconf(fd, _PC_VDISABLE);
524    error = fpathconf(fd, _PC_ASYNC_IO);
525    error = fpathconf(fd, _PC_PRIO_IO);
526    error = fpathconf(fd, _PC_SYNC_IO);
527    error = fpathconf(fd, 255);
528   
529    retval = TRUE;
530  }
531 
532  else
533    retval = FALSE;
534
535  /* assert (retval == TRUE);*/
536
537  return(retval);
538
539}
540
541/* ---------------------------------------------------------------
542 * FSyncTest function
543 *
544 * Hits the fsync code.
545 *
546 * arguments: none
547 * assumptions: open, fsync functions available.
548 * actions: open test file,
549 *          try to fsync it.
550 *
551 * returns: TRUE if fsync doesn't return -1,
552 *          FALSE otherwise.
553 *
554 * ---------------------------------------------------------------
555 */
556
557int FSyncTest (void) {
558
559  int error = 0, retval = FALSE;
560  int fd = -1;
561 
562  fd = open("testfile1.tst", O_RDWR);
563
564  if (fd != -1) {
565
566    error = fsync(fd);
567
568    if (error != -1)
569      retval = TRUE;
570    else
571      retval = FALSE;
572
573    close(fd);
574  }
575
576  else
577    retval = FALSE;
578
579  /* assert (retval == TRUE);*/
580
581  return(retval);
582
583}
584
585/* ---------------------------------------------------------------
586 * Main function
587 *
588 *  main entry point to the test
589 *
590 * ---------------------------------------------------------------
591 */
592
593#if defined(__rtems__)
594int test_main(void)
595#else
596int main(
597  int    argc,
598  char **argv
599)
600#endif
601{
602  if (InitFiles() == TRUE) {
603    puts ("\nFiles initialized successfully.\n");
604
605    puts ("Testing device_lseek()...");
606    if (DeviceLSeekTest() == TRUE)
607      puts ("Success.\n");
608    else
609      puts ("Failed!!!\n");
610
611    puts ("Testing dup()...");
612    if (DupTest() == TRUE)
613      puts ("Success.\n");
614    else
615      puts ("Failed!!!\n");
616   
617    puts ("Testing dup2()...");
618    if (Dup2Test() == TRUE)
619      puts ("Success.\n");
620    else
621      puts ("Failed!!!\n");
622
623    puts ("Testing fdatasync()...");
624    if (FDataSyncTest() == TRUE)
625      puts ("Success.\n");
626    else
627      puts ("Failed!!!\n");
628
629    puts ("Testing umask()...");
630    if (UMaskTest() == TRUE)
631      puts ("Success.\n");
632    else
633      puts ("Failed!!!\n");
634
635   puts ("Testing utime()...");
636    if (UTimeTest() == TRUE)
637      puts ("Success.\n");
638    else
639      puts ("Failed!!!\n");
640
641   puts ("Testing pipe()...");
642    if (PipeTest() == TRUE)
643      puts ("Success.\n");
644    else
645      puts ("Failed!!!\n");
646
647   puts ("Testing fsync()...");
648    if (FSyncTest() == TRUE)
649      puts ("Success.\n");
650    else
651      puts ("Failed!!!\n");
652
653   puts ("Testing pathconf()...");
654    if (PathConfTest() == TRUE)
655      puts ("Success.\n");
656    else
657      puts ("Failed!!!\n");
658 
659   puts ("Testing fpathconf()...");
660    if (FPathConfTest() == TRUE)
661      puts ("Success.\n");
662    else
663      puts ("Failed!!!\n");
664  }
665
666
667  else
668    puts ("\n\nError opening files for write!!!!\n");
669
670  puts( "\n\n*** XXX ***" );
671
672  puts( "\n\n*** END OF TEST PSX13 ***" );
673  exit(0);
674}
675
676
677
678
679
680
681
682
683
684
685
686
687 
Note: See TracBrowser for help on using the repository browser.