source: rtems/c/src/tests/psxtests/psx13/test.c @ 7b64b25b

4.104.114.84.95
Last change on this file since 7b64b25b was 7b64b25b, checked in by Joel Sherrill <joel.sherrill@…>, on 05/29/03 at 19:07:59

2003-05-29 Joel Sherrill <joel@…>

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