source: rtems/testsuites/psxtests/psx13/test.c @ 99e6fb52

4.115
Last change on this file since 99e6fb52 was 99e6fb52, checked in by Joel Sherrill <joel.sherrill@…>, on 08/07/10 at 00:22:46

2010-08-06 Bharath Suri <bharath.s.jois@…>

PR 1654/testing

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