Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  *  @file
0005  *
0006  *  This test exercises the posix_devctl() method.
0007  */
0008 
0009 /*
0010  *  COPYRIGHT (c) 2016.
0011  *  On-Line Applications Research Corporation (OAR).
0012  *
0013  * Redistribution and use in source and binary forms, with or without
0014  * modification, are permitted provided that the following conditions
0015  * are met:
0016  * 1. Redistributions of source code must retain the above copyright
0017  *    notice, this list of conditions and the following disclaimer.
0018  * 2. Redistributions in binary form must reproduce the above copyright
0019  *    notice, this list of conditions and the following disclaimer in the
0020  *    documentation and/or other materials provided with the distribution.
0021  *
0022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0023  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0025  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0026  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0027  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0028  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0029  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0030  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0031  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0032  * POSSIBILITY OF SUCH DAMAGE.
0033  */
0034 
0035 #define _POSIX_26_C_SOURCE
0036 
0037 #ifdef HAVE_CONFIG_H
0038 #include "config.h"
0039 #endif
0040 
0041 #include "tmacros.h"
0042 #include <errno.h>
0043 #include <sys/ioctl.h>
0044 #include <stdlib.h>
0045 
0046 #include <devctl.h>
0047 
0048 #include <unistd.h>
0049 #include <fcntl.h>
0050 
0051 const char rtems_test_name[] = "PSXDEVCTL 1";
0052 
0053 /* forward declarations to avoid warnings */
0054 int test_main(void);
0055 
0056 /*
0057  *  main entry point to the test
0058  */
0059 
0060 #if defined(__rtems__)
0061 int test_main(void)
0062 #else
0063 int main(
0064   int    argc,
0065   char **argv
0066 )
0067 #endif
0068 {
0069   int     status;
0070   int     fd;
0071   int     dcmd;
0072   int     dev_data;
0073   void   *dev_data_ptr;
0074   size_t  nbyte;
0075 
0076   TEST_BEGIN();
0077 
0078   puts( "posix_devctl() SOCKCLOSE on invalid file descriptor -- EBADF" );
0079   fd = -1;
0080   dcmd = SOCKCLOSE;
0081   dev_data_ptr = NULL;
0082   nbyte = 0;
0083   status = posix_devctl( fd, dcmd, dev_data_ptr, nbyte, NULL );
0084   rtems_test_assert( status == EBADF );
0085 
0086   /*
0087    * Create a file, open it, and close it via posix_devctl().
0088    * Then verify it is really closed.
0089    */
0090   puts( "posix_devctl() SOCKCLOSE on valid file descriptor -- OK" );
0091   fd = open("tmp_for_close", O_CREAT | O_RDWR, S_IRWXU );
0092   rtems_test_assert( fd != -1 );
0093 
0094   dcmd = SOCKCLOSE;
0095   dev_data_ptr = NULL;
0096   nbyte = 0;
0097   status = posix_devctl( fd, dcmd, dev_data_ptr, nbyte, NULL );
0098   rtems_test_assert( status == 0 );
0099 
0100   status = close( fd );
0101   rtems_test_assert( status == -1 );
0102   rtems_test_assert( errno == EBADF );
0103 
0104   puts( "posix_devctl() FIONBIO with invalid nbyte -- EINVAL" );
0105   fd = 0;
0106   dcmd = FIONBIO;
0107   dev_data_ptr = NULL;
0108   nbyte = 0;
0109   status = posix_devctl( fd, dcmd, dev_data_ptr, nbyte, NULL );
0110   rtems_test_assert( status == EINVAL );
0111 
0112   puts( "posix_devctl() FIONBIO with invalid file descriptor -- EBADF" );
0113   fd = -1;
0114   dcmd = FIONBIO;
0115   dev_data_ptr = NULL;
0116   nbyte = sizeof(int);
0117   status = posix_devctl( fd, dcmd, dev_data_ptr, nbyte, NULL );
0118   rtems_test_assert( status == EBADF );
0119 
0120   puts( "posix_devctl() FIONBIO flag not zero -- 0" );
0121   fd = 0;
0122   dcmd = FIONBIO;
0123   dev_data = 1;
0124   dev_data_ptr = &dev_data;
0125   nbyte = sizeof(int);
0126   status = posix_devctl( fd, dcmd, dev_data_ptr, nbyte, NULL );
0127   rtems_test_assert( status == 0 );
0128 
0129   puts( "posix_devctl() FIONBIO flag is zero -- 0" );
0130   fd = 0;
0131   dcmd = FIONBIO;
0132   dev_data = 0;
0133   dev_data_ptr = &dev_data;
0134   nbyte = sizeof(int);
0135   status = posix_devctl( fd, dcmd, dev_data_ptr, nbyte, NULL );
0136   rtems_test_assert( status == 0 );
0137 
0138   puts( "posix_devctl() dcmd not valid value -- EBADF" );
0139   fd = 0;
0140   dcmd = 1;
0141   dev_data = 0;
0142   dev_data_ptr = &dev_data;
0143   nbyte = sizeof(int);
0144   status = posix_devctl( fd, dcmd, dev_data_ptr, nbyte, NULL );
0145   rtems_test_assert( status == EBADF );
0146 
0147   TEST_END();
0148   exit(0);
0149 }