1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * This test exercises the posix_devctl() method. |
---|
5 | */ |
---|
6 | |
---|
7 | /* |
---|
8 | * COPYRIGHT (c) 2016. |
---|
9 | * On-Line Applications Research Corporation (OAR). |
---|
10 | * |
---|
11 | * The license and distribution terms for this file may be |
---|
12 | * found in the file LICENSE in this distribution or at |
---|
13 | * http://www.rtems.org/license/LICENSE. |
---|
14 | */ |
---|
15 | |
---|
16 | #define _POSIX_26_C_SOURCE |
---|
17 | |
---|
18 | #ifdef HAVE_CONFIG_H |
---|
19 | #include "config.h" |
---|
20 | #endif |
---|
21 | |
---|
22 | #include "tmacros.h" |
---|
23 | #include <errno.h> |
---|
24 | #include <sys/ioctl.h> |
---|
25 | #include <stdlib.h> |
---|
26 | |
---|
27 | #include <devctl.h> |
---|
28 | |
---|
29 | #include <unistd.h> |
---|
30 | #include <fcntl.h> |
---|
31 | |
---|
32 | const char rtems_test_name[] = "PSXDEVCTL 1"; |
---|
33 | |
---|
34 | /* forward declarations to avoid warnings */ |
---|
35 | int test_main(void); |
---|
36 | |
---|
37 | /* |
---|
38 | * main entry point to the test |
---|
39 | */ |
---|
40 | |
---|
41 | #if defined(__rtems__) |
---|
42 | int test_main(void) |
---|
43 | #else |
---|
44 | int main( |
---|
45 | int argc, |
---|
46 | char **argv |
---|
47 | ) |
---|
48 | #endif |
---|
49 | { |
---|
50 | int status; |
---|
51 | int fd; |
---|
52 | int dcmd; |
---|
53 | int dev_data; |
---|
54 | void *dev_data_ptr; |
---|
55 | size_t nbyte; |
---|
56 | |
---|
57 | TEST_BEGIN(); |
---|
58 | |
---|
59 | puts( "posix_devctl() SOCKCLOSE on invalid file descriptor -- EBADF" ); |
---|
60 | fd = -1; |
---|
61 | dcmd = SOCKCLOSE; |
---|
62 | dev_data_ptr = NULL; |
---|
63 | nbyte = 0; |
---|
64 | status = posix_devctl( fd, dcmd, dev_data_ptr, nbyte, NULL ); |
---|
65 | rtems_test_assert( status == EBADF ); |
---|
66 | |
---|
67 | /* |
---|
68 | * Create a file, open it, and close it via posix_devctl(). |
---|
69 | * Then verify it is really closed. |
---|
70 | */ |
---|
71 | puts( "posix_devctl() SOCKCLOSE on valid file descriptor -- OK" ); |
---|
72 | fd = open("tmp_for_close", O_CREAT | O_RDWR, S_IRWXU ); |
---|
73 | rtems_test_assert( fd != -1 ); |
---|
74 | |
---|
75 | dcmd = SOCKCLOSE; |
---|
76 | dev_data_ptr = NULL; |
---|
77 | nbyte = 0; |
---|
78 | status = posix_devctl( fd, dcmd, dev_data_ptr, nbyte, NULL ); |
---|
79 | rtems_test_assert( status == 0 ); |
---|
80 | |
---|
81 | status = close( fd ); |
---|
82 | rtems_test_assert( status == -1 ); |
---|
83 | rtems_test_assert( errno == EBADF ); |
---|
84 | |
---|
85 | puts( "posix_devctl() FIONBIO with invalid nbyte -- EINVAL" ); |
---|
86 | fd = 0; |
---|
87 | dcmd = FIONBIO; |
---|
88 | dev_data_ptr = NULL; |
---|
89 | nbyte = 0; |
---|
90 | status = posix_devctl( fd, dcmd, dev_data_ptr, nbyte, NULL ); |
---|
91 | rtems_test_assert( status == EINVAL ); |
---|
92 | |
---|
93 | puts( "posix_devctl() FIONBIO with invalid file descriptor -- EBADF" ); |
---|
94 | fd = -1; |
---|
95 | dcmd = FIONBIO; |
---|
96 | dev_data_ptr = NULL; |
---|
97 | nbyte = sizeof(int); |
---|
98 | status = posix_devctl( fd, dcmd, dev_data_ptr, nbyte, NULL ); |
---|
99 | rtems_test_assert( status == EBADF ); |
---|
100 | |
---|
101 | puts( "posix_devctl() FIONBIO flag not zero -- 0" ); |
---|
102 | fd = 0; |
---|
103 | dcmd = FIONBIO; |
---|
104 | dev_data = 1; |
---|
105 | dev_data_ptr = &dev_data; |
---|
106 | nbyte = sizeof(int); |
---|
107 | status = posix_devctl( fd, dcmd, dev_data_ptr, nbyte, NULL ); |
---|
108 | rtems_test_assert( status == 0 ); |
---|
109 | |
---|
110 | puts( "posix_devctl() FIONBIO flag is zero -- 0" ); |
---|
111 | fd = 0; |
---|
112 | dcmd = FIONBIO; |
---|
113 | dev_data = 0; |
---|
114 | dev_data_ptr = &dev_data; |
---|
115 | nbyte = sizeof(int); |
---|
116 | status = posix_devctl( fd, dcmd, dev_data_ptr, nbyte, NULL ); |
---|
117 | rtems_test_assert( status == 0 ); |
---|
118 | |
---|
119 | puts( "posix_devctl() dcmd not valid value -- EBADF" ); |
---|
120 | fd = 0; |
---|
121 | dcmd = 1; |
---|
122 | dev_data = 0; |
---|
123 | dev_data_ptr = &dev_data; |
---|
124 | nbyte = sizeof(int); |
---|
125 | status = posix_devctl( fd, dcmd, dev_data_ptr, nbyte, NULL ); |
---|
126 | rtems_test_assert( status == EBADF ); |
---|
127 | |
---|
128 | TEST_END(); |
---|
129 | exit(0); |
---|
130 | } |
---|