source: rtems/testsuites/psxtests/psx13/test.c @ 2da9a31a

4.115
Last change on this file since 2da9a31a was 2da9a31a, checked in by Joel Sherrill <joel.sherrill@…>, on 07/02/10 at 18:51:48

2010-07-02 Joel Sherrill <joel.sherrill@…>

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