Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:36

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  *  @file
0005  *
0006  *  @brief This tests various file system functions.
0007  *
0008  *  This test exercises the following routines:
0009  *
0010  *     - lseek()
0011  *     - dup()
0012  *     - dup2()
0013  *     - fdatasync()
0014  *     - fsync()
0015  *     - pathconf()
0016  *     - fpathconf()
0017  *     - umask()
0018  *     - utime()
0019  *     - utimes()
0020  *     - utimensat()
0021  *     - futimens()
0022  *     - sync()
0023  */
0024 
0025 /*
0026  * COPYRIGHT (C) 1989, 2021 On-Line Applications Research Corporation (OAR).
0027  *
0028  * Redistribution and use in source and binary forms, with or without
0029  * modification, are permitted provided that the following conditions
0030  * are met:
0031  * 1. Redistributions of source code must retain the above copyright
0032  *    notice, this list of conditions and the following disclaimer.
0033  * 2. Redistributions in binary form must reproduce the above copyright
0034  *    notice, this list of conditions and the following disclaimer in the
0035  *    documentation and/or other materials provided with the distribution.
0036  *
0037  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0038  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0039  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0040  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0041  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0042  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0043  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0044  * * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0045  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0046  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0047  * POSSIBILITY OF SUCH DAMAGE.
0048  */
0049 
0050 #ifdef HAVE_CONFIG_H
0051 #include "config.h"
0052 #endif
0053 
0054 #include <rtems.h>
0055 #include <rtems/libio.h>
0056 #include <rtems/score/timespec.h>
0057 #include <rtems/score/todimpl.h>
0058 #include <sys/time.h>
0059 #include <fcntl.h>
0060 #include <unistd.h>
0061 #include <errno.h>
0062 #include <utime.h>
0063 #include <tmacros.h>
0064 
0065 #include <stdio.h>
0066 #include <unistd.h>
0067 
0068 #include <pmacros.h>
0069 
0070 const char rtems_test_name[] = "PSX 13";
0071 
0072 /**
0073  * @brief Initializes the three files to be used for the test.
0074  */
0075 static void InitFiles( void )
0076 {
0077   int count;
0078   int rv;
0079   FILE *fp1, *fp2, *fp3;
0080   char letter;
0081   int number;
0082 
0083   fp1 = fopen( "testfile1.tst", "wt" );
0084   rtems_test_assert( fp1 != NULL );
0085 
0086   fp2 = fopen( "testfile2.tst", "wt" );
0087   rtems_test_assert( fp2 != NULL );
0088 
0089   fp3 = fopen( "testfile4.tst", "wb" );
0090   rtems_test_assert( fp3 != NULL );
0091 
0092   letter = 'a';
0093 
0094   for( count = 0 ; count < (26*4); ++count) {
0095     fprintf( fp1, "%c", letter );
0096     fprintf( fp2, "%c", letter );
0097 
0098     ++letter;
0099     if( letter > 'z' )
0100       letter = 'a';
0101   }
0102 
0103   number = 0;
0104 
0105   for( count = 0; count < 40; ++count ) {
0106     fwrite( &number, 1, sizeof(int), fp3 );
0107 
0108     ++number;
0109     if( number > 9 )
0110       number = 0;
0111   }
0112 
0113   rv = fclose( fp1 );
0114   rtems_test_assert( rv != EOF );
0115 
0116   rv = fclose( fp2 );
0117   rtems_test_assert( rv != EOF );
0118 
0119   rv = fclose( fp3 );
0120   rtems_test_assert( rv != EOF );
0121 }
0122 
0123 /**
0124  * @brief Exercises lseek() by lseeking on the console.
0125  */
0126 static void DeviceLSeekTest( void )
0127 {
0128   int rv;
0129   int fd;
0130 
0131   fd = open( "/dev/console", O_RDONLY );
0132   rtems_test_assert( fd != -1 );
0133 
0134   rv = lseek( fd, 5, SEEK_SET );
0135   rtems_test_assert( rv == -1 );
0136   rtems_test_assert( errno == ESPIPE );
0137 
0138   rv = close( fd );
0139   rtems_test_assert( rv == 0 );
0140 }
0141 
0142 /**
0143  * @brief Exercises dup().
0144  */
0145 static void DupTest( void )
0146 {
0147   int fd1, fd2;
0148   int flags;
0149   int rv;
0150 
0151   fd1 = open( "testfile1.tst", O_RDONLY );
0152   rtems_test_assert( fd1 != -1 );
0153 
0154   fd2 = dup( fd1 );
0155   rtems_test_assert( fd2 != -1 );
0156 
0157   rv = fcntl( fd1, F_SETFL, O_APPEND );
0158   rtems_test_assert( rv != -1 );
0159 
0160   flags = fcntl( fd2, F_GETFL ) & O_APPEND;
0161   rtems_test_assert( flags == 0 );
0162 
0163   rv = close( fd1 );
0164   rtems_test_assert( rv == 0 );
0165 
0166   rv = close( fd2 );
0167   rtems_test_assert( rv == 0 );
0168 }
0169 
0170 /**
0171  * @brief Exercises dup2().
0172  */
0173 static void Dup2Test( void )
0174 {
0175   int fd1, fd2;
0176   int flags;
0177   int rv;
0178 
0179   fd1 = open( "testfile1.tst", O_RDONLY );
0180   rtems_test_assert( fd1 != -1 );
0181 
0182   fd2 = open( "testfile2.tst", O_RDONLY );
0183   rtems_test_assert( fd2 != -1 );
0184 
0185   /* make sure dup2 works if both fd1 and fd2 are valid file descriptors. */
0186   rv = dup2( fd1, fd2 );
0187   rtems_test_assert( rv != -1 );
0188 
0189   rv = fcntl( fd1, F_SETFL, O_APPEND );
0190   rtems_test_assert( rv != -1 );
0191 
0192   flags = fcntl( fd1, F_GETFL ) & O_APPEND;
0193   rtems_test_assert( flags == O_APPEND );
0194 
0195   /* make sure dup2 fails correctly if one or the other arguments are invalid. */
0196   /* this assumes -1 is an invalid value for a file descriptor!!! (POSIX book, p.135) */
0197   rv = close( fd1 );
0198   rtems_test_assert( rv == 0 );
0199 
0200   fd1 = -1;
0201 
0202   rv = dup2( fd1, fd2 );
0203   rtems_test_assert( rv == -1 );
0204 
0205   fd1 = dup( fd2 );
0206   fd2 = -1;
0207 
0208   rv = dup2( fd1, fd2 );
0209   rtems_test_assert( rv == -1 );
0210 
0211   rv = close( fd1 );
0212   rtems_test_assert( rv == 0 );
0213 }
0214 
0215 /**
0216  * @brief Exercises fdatasync().
0217  */
0218 static void FDataSyncTest( void )
0219 {
0220   int fd;
0221   int rv;
0222 
0223   /* Try it with a RD_ONLY file. */
0224   fd = open( "testfile1.tst", O_RDONLY );
0225   rtems_test_assert( fd != -1 );
0226 
0227   rv = fdatasync( fd );
0228   rtems_test_assert( rv == -1 );
0229   rtems_test_assert( errno == EBADF );
0230 
0231   rv = close(fd);
0232   rtems_test_assert( rv == 0 );
0233 
0234   /* Try it with a bad file descriptor */
0235   fd = -1;
0236 
0237   rv = fdatasync( fd );
0238   rtems_test_assert( rv == -1 );
0239   rtems_test_assert( errno == EBADF );
0240 
0241   /* Okay - now the success case... */
0242   fd = open( "testfile1.tst", O_RDWR );
0243   rv = fdatasync( fd );
0244   rtems_test_assert( rv == 0 );
0245 
0246   rv = close( fd );
0247   rtems_test_assert( rv == 0 );
0248 }
0249 
0250 /**
0251  * @brief Exercises umask().
0252  */
0253 static void UMaskTest( void )
0254 {
0255   mode_t rv;
0256 
0257   (void) umask( 023 );
0258 
0259   rv = umask( 022 );
0260   rtems_test_assert( rv == 023 );
0261 }
0262 
0263 /**
0264  * @brief Exercises utime().
0265  */
0266 static void UTimeTest( void )
0267 {
0268   int rv;
0269   struct utimbuf time;
0270   struct timespec current_time;
0271   struct stat fstat;
0272 
0273   /* ENOENT test case */
0274 
0275   /* Case: Pass an invalid filename. */
0276   rv = utime( "!This is an =invalid p@thname!!! :)", NULL );
0277   rtems_test_assert( rv == -1 );
0278   rtems_test_assert( errno == ENOENT );
0279 
0280   /* EACCES test case */
0281 
0282   /* Case: Change user ID to someone besides root */
0283   rv = seteuid( 1 );
0284   rtems_test_assert( rv == 0 );
0285 
0286   rv = utime( "testfile1.tst", NULL );
0287   rtems_test_assert( rv == -1 );
0288   rtems_test_assert( errno == EACCES );
0289 
0290   rv = seteuid( 0 );
0291   rtems_test_assert( rv == 0 );
0292 
0293   /* EINVAL test cases */
0294 
0295   /* Case: Invalid access time */
0296   time.actime  = -1;
0297   time.modtime = 54321;
0298 
0299   rv = utime( "testfile1.tst", &time );
0300   rtems_test_assert( rv == -1 );
0301   rtems_test_assert( errno == EINVAL );
0302 
0303   /* Case: Invalid modified time */
0304   time.actime  = 12345;
0305   time.modtime = -1;
0306 
0307   rv = utime( "testfile1.tst", &time );
0308   rtems_test_assert( rv == -1 );
0309   rtems_test_assert( errno == EINVAL );
0310 
0311   /* Successful test cases */
0312 
0313   /* Case: Test without times argument */
0314   clock_gettime( CLOCK_REALTIME, &current_time );
0315 
0316   rv = utime( "testfile1.tst", NULL );
0317   rtems_test_assert( rv == 0 );
0318 
0319   rv = stat( "testfile1.tst", &fstat );
0320   rtems_test_assert( rv == 0 );
0321   rtems_test_assert( current_time.tv_sec <= fstat.st_atim.tv_sec );
0322   rtems_test_assert( current_time.tv_sec <= fstat.st_mtim.tv_sec );
0323 
0324   /* Case: time is filled with valid values */
0325   time.actime  = 12345;
0326   time.modtime = 54321;
0327 
0328   rv = utime( "testfile1.tst", &time );
0329   rtems_test_assert( rv == 0 );
0330 
0331   /* Check that it actually changed the time */
0332   rv = stat( "testfile1.tst", &fstat );
0333   rtems_test_assert( rv == 0 );
0334   rtems_test_assert( fstat.st_atime == 12345 );
0335   rtems_test_assert( fstat.st_mtime == 54321 );
0336 }
0337 
0338 /**
0339  * @brief Exercises utimes().
0340  */
0341 static void UTimesTest( void )
0342 {
0343   int rv;
0344   struct timeval time[2];
0345   struct timespec current_time;
0346   struct stat fstat;
0347 
0348   /* ENOENT test case */
0349 
0350   /* Case: First, an invalid filename. */
0351   rv = utimes( "!This is an =invalid p@thname!!! : )", NULL);
0352   rtems_test_assert( rv == -1 );
0353   rtems_test_assert( errno == ENOENT );
0354 
0355   /* EACCES test case */
0356 
0357   /* Change the user ID of the process to someone besides root */
0358   rv = seteuid( 1 );
0359   rtems_test_assert( rv == 0 );
0360 
0361   rv = utimes( "testfile1.tst", NULL );
0362   rtems_test_assert( rv == -1 );
0363   rtems_test_assert( errno == EACCES );
0364 
0365   rv = seteuid( 0 );
0366   rtems_test_assert( rv == 0 );
0367 
0368   /* EINVAL test cases */
0369 
0370   /* Case: Negative access time tv_sec value */
0371   time[0].tv_sec = -1;
0372   time[0].tv_usec = 12345;
0373   time[1].tv_sec = 54321;
0374   time[1].tv_usec = 54321;
0375 
0376   rv = utimes( "testfile1.tst", time );
0377   rtems_test_assert( rv == -1 );
0378   rtems_test_assert( errno == EINVAL );
0379 
0380   /* Case: Negative modified time second value */
0381   time[0].tv_sec = 12345;
0382   time[1].tv_sec = -1;
0383 
0384   rv = utimes( "testfile1.tst", time );
0385   rtems_test_assert( rv == -1 );
0386   rtems_test_assert( errno == EINVAL );
0387 
0388   /* Case: Negative access time microsecond value */
0389   time[1].tv_sec = 54321;
0390   time[0].tv_usec = -1;
0391 
0392   rv = utimes( "testfile1.tst", time );
0393   rtems_test_assert( rv == -1 );
0394   rtems_test_assert( errno == EINVAL );
0395 
0396   /* Case: Negative modified time microsecond value */
0397   time[0].tv_usec = 12345;
0398   time[1].tv_usec = -1;
0399 
0400   rv = utimes( "testfile1.tst", time );
0401   rtems_test_assert( rv == -1 );
0402   rtems_test_assert( errno == EINVAL );
0403 
0404   /* Case: Access time microsecond value too large */
0405   time[0].tv_usec = 1000000;
0406   time[1].tv_usec = 54321;
0407 
0408   rv = utimes( "testfile1.tst", time );
0409   rtems_test_assert( rv == -1 );
0410   rtems_test_assert( errno == EINVAL );
0411 
0412   /* Case: Modified time microsecond value too large */
0413   time[1].tv_usec = 1000000;
0414   time[0].tv_usec = 12345;
0415 
0416   rv = utimes( "testfile1.tst", time );
0417   rtems_test_assert( rv == -1 );
0418   rtems_test_assert( errno == EINVAL );
0419 
0420   /* Successful test cases */
0421 
0422   /* Case: Test without times argument */
0423   clock_gettime( CLOCK_REALTIME, &current_time );
0424 
0425   rv = utimes( "testfile1.tst", NULL );
0426   rtems_test_assert( rv == 0 );
0427 
0428   rv = stat( "testfile1.tst", &fstat );
0429   rtems_test_assert( rv == 0 );
0430   rtems_test_assert( current_time.tv_sec <= fstat.st_atim.tv_sec );
0431   rtems_test_assert( current_time.tv_sec <= fstat.st_mtim.tv_sec );
0432 
0433   /* Case: time is filled with valid values */
0434   time[0].tv_sec = 12345;
0435   time[0].tv_usec = 12345;
0436   time[1].tv_sec = 54321;
0437   time[1].tv_usec = 54321;
0438 
0439   rv = utimes( "testfile1.tst", time );
0440   rtems_test_assert( rv == 0 );
0441 
0442   /* Check that it actually changed the time */
0443   rv = stat( "testfile1.tst", &fstat );
0444   rtems_test_assert( rv == 0 );
0445   rtems_test_assert( fstat.st_atime == 12345 );
0446   rtems_test_assert( fstat.st_mtime == 54321 );
0447 }
0448 
0449 /**
0450  * @brief Exercises utimensat().
0451  */
0452 static void UTimensatTest( void )
0453 {
0454   int rv;
0455   struct timespec time[2];
0456   struct timespec current_time;
0457   struct stat fstat;
0458 
0459   /* ENOSYS test cases */
0460 
0461   /* Case: Pass an unsupported file descriptor */
0462   rv = utimensat(
0463     0,
0464     "!This is an =invalid p@thname!!! : )",
0465     NULL,
0466     0
0467   );
0468   rtems_test_assert( rv == -1 );
0469   rtems_test_assert( errno == ENOSYS );
0470 
0471   /* Case: Pass unsupported flag */
0472   rv = utimensat(
0473     AT_FDCWD,
0474     "!This is an =invalid p@thname!!! : )",
0475     NULL,
0476     1
0477   );
0478   rtems_test_assert( rv == -1 );
0479   rtems_test_assert( errno == ENOSYS );
0480 
0481   /* ENOENT test case */
0482 
0483   /* Use an invalid filename. */
0484   rv = utimensat(
0485     AT_FDCWD,
0486     "!This is an =invalid p@thname!!! : )",
0487     NULL,
0488     0
0489   );
0490   rtems_test_assert( rv == -1 );
0491   rtems_test_assert( errno == ENOENT );
0492 
0493   rv = stat( "testfile1.tst", &fstat );
0494   rtems_test_assert( rv == 0 );
0495 
0496   /* EACCES test Cases */
0497 
0498   /* Case: When times is NULL and the user has insufficient privileges */
0499 
0500   /* Change the user ID of the process to someone besides root */
0501   rv = seteuid( 1 );
0502   rtems_test_assert( rv == 0 );
0503 
0504   rv = utimensat( AT_FDCWD, "testfile1.tst", NULL, 0 );
0505   rtems_test_assert( rv == -1 );
0506   rtems_test_assert( errno == EACCES );
0507 
0508   rv = seteuid( 0 );
0509   rtems_test_assert( rv == 0 );
0510 
0511   /* Case: File is read-only and time's tv_nsec members are UTIME_NOW */
0512 
0513   /* Change file to be read-only */
0514   rv = chmod( "testfile1.tst", 06444 );
0515   rtems_test_assert( rv == 0 );
0516 
0517   _Timespec_Set( &time[0], 0, UTIME_NOW );
0518   _Timespec_Set( &time[1], 0, UTIME_NOW );
0519 
0520   rv = utimensat( AT_FDCWD, "testfile1.tst", time, 0 );
0521   rtems_test_assert( rv == -1 );
0522   rtems_test_assert( errno == EACCES );
0523 
0524   rv = chmod( "testfile1.tst", fstat.st_mode );
0525   rtems_test_assert( rv == 0 );
0526 
0527   /* EINVAL test cases */
0528 
0529   /* Case: Negative access time second value */
0530   _Timespec_Set( &time[0], -12345, 12345 );
0531   _Timespec_Set( &time[1], 54321, 54321 );
0532 
0533   rv = utimensat( AT_FDCWD, "testfile1.tst", time, 0 );
0534   rtems_test_assert( rv == -1 );
0535   rtems_test_assert( errno == EINVAL );
0536 
0537   /* Case: Negative modified time second value */
0538   _Timespec_Set( &time[0], 12345, 12345 );
0539   _Timespec_Set( &time[1], -54321, 54321 );
0540 
0541   rv = utimensat( AT_FDCWD, "testfile1.tst", time, 0 );
0542   rtems_test_assert( rv == -1 );
0543   rtems_test_assert( errno == EINVAL );
0544 
0545   /* Case: Negative access time nanosecond value */
0546   _Timespec_Set( &time[0], 12345, -12345 );
0547   _Timespec_Set( &time[1], 54321, 54321 );
0548 
0549   rv = utimensat( AT_FDCWD, "testfile1.tst", time, 0 );
0550   rtems_test_assert( rv == -1 );
0551   rtems_test_assert( errno == EINVAL );
0552 
0553   /* Case: Negative modified time nanosecond value */
0554   _Timespec_Set( &time[0], 12345, 12345 );
0555   _Timespec_Set( &time[1], 54321, -54321 );
0556 
0557   rv = utimensat( AT_FDCWD, "testfile1.tst", time, 0 );
0558   rtems_test_assert( rv == -1 );
0559   rtems_test_assert( errno == EINVAL );
0560 
0561   /* Case: Access time nanosecond value too large */
0562   _Timespec_Set( &time[0], 12345, TOD_NANOSECONDS_PER_SECOND );
0563   _Timespec_Set( &time[1], 54321, 54321 );
0564 
0565   rv = utimensat( AT_FDCWD, "testfile1.tst", time, 0 );
0566   rtems_test_assert( rv == -1 );
0567   rtems_test_assert( errno == EINVAL );
0568 
0569   /* Case: Modified time nanosecond value too large */
0570   _Timespec_Set( &time[0], 12345, 12345 );
0571   _Timespec_Set( &time[1], 54321, TOD_NANOSECONDS_PER_SECOND );
0572 
0573   rv = utimensat( AT_FDCWD, "testfile1.tst", time, 0 );
0574   rtems_test_assert( rv == -1 );
0575   rtems_test_assert( errno == EINVAL );
0576 
0577   /* Successful test cases */
0578 
0579   /* Case: Test without times argument */
0580   clock_gettime( CLOCK_REALTIME, &current_time );
0581 
0582   rv = utimensat( AT_FDCWD, "testfile1.tst", NULL, 0 );
0583   rtems_test_assert( rv == 0 );
0584 
0585   rv = stat( "testfile1.tst", &fstat );
0586   rtems_test_assert( rv == 0 );
0587   rtems_test_assert( current_time.tv_sec <= fstat.st_atim.tv_sec );
0588   rtems_test_assert( current_time.tv_sec <= fstat.st_mtim.tv_sec );
0589 
0590   /* Case: Running with access time nanosecond field equal to UTIME_NOW */
0591   _Timespec_Set( &time[0], 12345, UTIME_NOW );
0592   _Timespec_Set( &time[1], 54321, 54321 );
0593 
0594   rv = utimensat( AT_FDCWD, "testfile1.tst", time, 0 );
0595   rtems_test_assert( rv == 0 );
0596 
0597   rv = stat( "testfile1.tst", &fstat );
0598   rtems_test_assert( rv == 0 );
0599   rtems_test_assert( current_time.tv_sec <= fstat.st_atim.tv_sec );
0600   rtems_test_assert( fstat.st_mtime == 54321 );
0601 
0602   /* Case: Running with modified time nanosecond field equal to UTIME_NOW */
0603   _Timespec_Set( &time[0], 12345, 12345 );
0604   _Timespec_Set( &time[1], 54321, UTIME_NOW );
0605 
0606   rv = utimensat( AT_FDCWD, "testfile1.tst", time, 0 );
0607   rtems_test_assert( rv == 0 );
0608 
0609   rv = stat( "testfile1.tst", &fstat );
0610   rtems_test_assert( rv == 0 );
0611   rtems_test_assert( fstat.st_atime == 12345 );
0612   rtems_test_assert( current_time.tv_sec <= fstat.st_mtim.tv_sec );
0613 
0614   /* Case: Normal run */
0615   _Timespec_Set( &time[0], 12345, 12345 );
0616   _Timespec_Set( &time[1], 54321, 54321 );
0617 
0618   rv = utimensat( AT_FDCWD, "testfile1.tst", time, 0 );
0619   rtems_test_assert( rv == 0 );
0620 
0621   /* Check that it actually changed the time */
0622   rv = stat( "testfile1.tst", &fstat );
0623   rtems_test_assert( rv == 0 );
0624   rtems_test_assert( fstat.st_atime == 12345 );
0625   rtems_test_assert( fstat.st_mtime == 54321 );
0626 }
0627 
0628 /**
0629  * @brief Exercises futimens().
0630  */
0631 static void FutimensTest( void )
0632 {
0633   int rv;
0634   int fd;
0635   struct timespec time[2];
0636   struct timespec current_time;
0637   struct stat fstat;
0638 
0639   /* EBADF test case */
0640 
0641   /* Case: Pass an invalid file descriptor */
0642   _Timespec_Set_to_zero( &time[0] );
0643   _Timespec_Set_to_zero( &time[1] );
0644   rv = futimens( -1, time );
0645   rtems_test_assert( rv == -1 );
0646   rtems_test_assert( errno == EBADF );
0647 
0648   fd = open( "testfile1.tst", O_RDWR );
0649   rtems_test_assert( fd != -1 );
0650 
0651   /* EACCES test cases */
0652 
0653   /* Case: When times is NULL and the user has insufficient privileges */
0654 
0655   /* Change the user ID of the process to someone besides root */
0656   rv = seteuid( 1 );
0657   rtems_test_assert( rv == 0 );
0658 
0659   rv = futimens( fd, NULL );
0660   rtems_test_assert( rv == -1 );
0661   rtems_test_assert( errno == EACCES );
0662 
0663   rv = seteuid( 0 );
0664   rtems_test_assert( rv == 0 );
0665 
0666   /* Case: File is read-only and time's tv_nsec members are UTIME_NOW */
0667 
0668   /* Change file to be read-only */
0669   rv = chmod( "testfile1.tst", 06444 );
0670   rtems_test_assert( rv == 0 );
0671 
0672   _Timespec_Set( &time[0], 0, UTIME_NOW );
0673   _Timespec_Set( &time[1], 0, UTIME_NOW );
0674 
0675   rv = futimens( fd, time );
0676   rtems_test_assert( rv == -1 );
0677   rtems_test_assert( errno == EACCES );
0678 
0679   rv = chmod( "testfile1.tst", fstat.st_mode );
0680   rtems_test_assert( rv == 0 );
0681 
0682   /* EINVAL test cases */
0683 
0684   /* Case: Negative access time second value */
0685   _Timespec_Set( &time[0], -12345, 12345 );
0686   _Timespec_Set( &time[1], 54321, 54321 );
0687 
0688   rv = futimens( fd, time );
0689   rtems_test_assert( rv == -1 );
0690   rtems_test_assert( errno == EINVAL );
0691 
0692   /* Case: Negative modified time second value */
0693   _Timespec_Set( &time[0], 12345, 12345 );
0694   _Timespec_Set( &time[1], -54321, 54321 );
0695 
0696   rv = futimens( fd, time );
0697   rtems_test_assert( rv == -1 );
0698   rtems_test_assert( errno == EINVAL );
0699 
0700   /* Case: Negative access time nanosecond value */
0701   _Timespec_Set( &time[0], 12345, -12345 );
0702   _Timespec_Set( &time[1], 54321, 54321 );
0703 
0704   rv = futimens( fd, time );
0705   rtems_test_assert( rv == -1 );
0706   rtems_test_assert( errno == EINVAL );
0707 
0708   /* Case: Negative modified time nanosecond value */
0709   _Timespec_Set( &time[0], 12345, 12345 );
0710   _Timespec_Set( &time[1], 54321, -54321 );
0711 
0712   rv = futimens( fd, time );
0713   rtems_test_assert( rv == -1 );
0714   rtems_test_assert( errno == EINVAL );
0715 
0716   /* Case: Access time nanosecond value too large */
0717   _Timespec_Set( &time[0], 12345, TOD_NANOSECONDS_PER_SECOND );
0718   _Timespec_Set( &time[1], 54321, 54321 );
0719 
0720   rv = futimens( fd, time );
0721   rtems_test_assert( rv == -1 );
0722   rtems_test_assert( errno == EINVAL );
0723 
0724   /* Case: Modified time nanosecond value too large */
0725   _Timespec_Set( &time[0], 12345, 12345 );
0726   _Timespec_Set( &time[1], 54321, TOD_NANOSECONDS_PER_SECOND );
0727 
0728   rv = futimens( fd, time );
0729   rtems_test_assert( rv == -1 );
0730   rtems_test_assert( errno == EINVAL );
0731 
0732   /* Successful test cases */
0733 
0734   /* Case: Test without times argument */
0735   clock_gettime( CLOCK_REALTIME, &current_time );
0736 
0737   rv = futimens( fd, NULL );
0738   rtems_test_assert( rv == 0 );
0739 
0740   rv = stat( "testfile1.tst", &fstat );
0741   rtems_test_assert( rv == 0 );
0742   rtems_test_assert( current_time.tv_sec <= fstat.st_atim.tv_sec );
0743   rtems_test_assert( current_time.tv_sec <= fstat.st_mtim.tv_sec );
0744 
0745 /* Case: Running with access time nanosecond field equal to UTIME_NOW */
0746   _Timespec_Set( &time[0], 12345, UTIME_NOW );
0747   _Timespec_Set( &time[1], 54321, 54321 );
0748 
0749   rv = futimens( fd, time );
0750   rtems_test_assert( rv == 0 );
0751 
0752   rv = stat( "testfile1.tst", &fstat );
0753   rtems_test_assert( rv == 0 );
0754   rtems_test_assert( current_time.tv_sec <= fstat.st_atim.tv_sec );
0755   rtems_test_assert( fstat.st_mtime == 54321 );
0756 
0757   /* Case: Running with modified time nanosecond field equal to UTIME_NOW */
0758   _Timespec_Set( &time[0], 12345, 12345 );
0759   _Timespec_Set( &time[1], 54321, UTIME_NOW );
0760 
0761   rv = futimens( fd, time );
0762   rtems_test_assert( rv == 0 );
0763 
0764   rv = stat( "testfile1.tst", &fstat );
0765   rtems_test_assert( rv == 0 );
0766   rtems_test_assert( fstat.st_atime == 12345 );
0767   rtems_test_assert( current_time.tv_sec <= fstat.st_mtim.tv_sec );
0768 
0769   /* Case: Normal run */
0770   _Timespec_Set( &time[0], 12345, 12345 );
0771   _Timespec_Set( &time[1], 54321, 54321 );
0772 
0773   rv = futimens( fd, time );
0774   rtems_test_assert( rv == 0 );
0775 
0776   /* Check that it actually changed the time */
0777   rv = stat( "testfile1.tst", &fstat );
0778   rtems_test_assert( rv == 0 );
0779   rtems_test_assert( fstat.st_atime == 12345 );
0780   rtems_test_assert( fstat.st_mtime == 54321 );
0781 }
0782 
0783 /**
0784  * @brief Exercises pathconf().
0785  */
0786 static void PathConfTest( void )
0787 {
0788   int rv;
0789 
0790   rv = pathconf( "thisfiledoesnotexist", _PC_LINK_MAX );
0791   rtems_test_assert( rv == -1 );
0792 
0793   rv = pathconf( "testfile1.tst", _PC_LINK_MAX );
0794   rtems_test_assert( rv != -1 );
0795 }
0796 
0797 /**
0798  * @brief Exercises fpathconf().
0799  */
0800 static void FPathConfTest( void )
0801 {
0802   int rv;
0803   int fd;
0804 
0805   fd = -1;
0806   rv = fpathconf( fd, _PC_LINK_MAX );
0807   rtems_test_assert( rv == -1 );
0808 
0809   fd = open( "testfile1.tst", O_RDWR );
0810   rtems_test_assert( fd != -1 );
0811 
0812   rv = fpathconf( fd, _PC_LINK_MAX );
0813   rtems_test_assert( rv != -1 );
0814 
0815   rv = fpathconf( fd, _PC_MAX_CANON );
0816   rtems_test_assert( rv != -1 );
0817 
0818   rv = fpathconf( fd, _PC_MAX_INPUT );
0819   rtems_test_assert( rv != -1 );
0820 
0821   rv = fpathconf( fd, _PC_NAME_MAX );
0822   rtems_test_assert( rv != -1 );
0823 
0824   rv = fpathconf( fd, _PC_PATH_MAX );
0825   rtems_test_assert( rv != -1 );
0826 
0827   rv = fpathconf( fd, _PC_PIPE_BUF );
0828   rtems_test_assert( rv != -1 );
0829 
0830   rv = fpathconf( fd, _PC_CHOWN_RESTRICTED );
0831   rtems_test_assert( rv != -1 );
0832 
0833   rv = fpathconf( fd, _PC_NO_TRUNC );
0834   rtems_test_assert( rv != -1 );
0835 
0836   rv = fpathconf( fd, _PC_VDISABLE );
0837   rtems_test_assert( rv != -1 );
0838 
0839   rv = fpathconf( fd, _PC_ASYNC_IO );
0840   rtems_test_assert( rv != -1 );
0841 
0842   rv = fpathconf( fd, _PC_PRIO_IO );
0843   rtems_test_assert( rv != -1 );
0844 
0845   rv = fpathconf( fd, _PC_SYNC_IO );
0846   rtems_test_assert( rv != -1 );
0847 
0848   rv = fpathconf( fd, 255 );
0849   rtems_test_assert( rv == -1 );
0850 
0851   rv = close( fd );
0852   rtems_test_assert( rv == 0 );
0853 
0854   fd = open( "testfile1.tst", O_WRONLY );
0855   rtems_test_assert( rv != -1 );
0856 
0857   rv = fpathconf( fd, _PC_LINK_MAX );
0858   rtems_test_assert( rv != -1 );
0859 
0860   rv = close( fd );
0861   rtems_test_assert( rv == 0 );
0862 }
0863 
0864 /**
0865  * @brief Exercises fsync().
0866  */
0867 static void FSyncTest( void )
0868 {
0869   int rv;
0870   int fd;
0871 
0872   fd = open( "testfile1.tst", O_RDWR );
0873   rtems_test_assert( fd != -1 );
0874 
0875   rv = fsync(fd);
0876   rtems_test_assert( rv != -1 );
0877 
0878   rv = close( fd );
0879   rtems_test_assert( rv == 0 );
0880 }
0881 
0882 /**
0883  * @brief Exercises sync().
0884  */
0885 static void SyncTest( void )
0886 {
0887   sync();
0888 }
0889 
0890 /**
0891  * @brief The main entry point to the test.
0892  */
0893 int test_main( void );
0894 int test_main( void )
0895 {
0896   TEST_BEGIN();
0897 
0898   InitFiles();
0899 
0900   DeviceLSeekTest();
0901   DupTest();
0902   Dup2Test();
0903   FDataSyncTest();
0904   UMaskTest();
0905   UTimeTest();
0906   UTimesTest();
0907   UTimensatTest();
0908   FutimensTest();
0909   FSyncTest();
0910   PathConfTest();
0911   FPathConfTest();
0912   SyncTest();
0913 
0914   TEST_END();
0915 
0916   rtems_test_exit( 0 );
0917 }