1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /* |
---|
4 | * COPYRIGHT (c) 1989-2012. |
---|
5 | * On-Line Applications Research Corporation (OAR). |
---|
6 | * |
---|
7 | * Redistribution and use in source and binary forms, with or without |
---|
8 | * modification, are permitted provided that the following conditions |
---|
9 | * are met: |
---|
10 | * 1. Redistributions of source code must retain the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer. |
---|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
13 | * notice, this list of conditions and the following disclaimer in the |
---|
14 | * documentation and/or other materials provided with the distribution. |
---|
15 | * |
---|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
26 | * POSSIBILITY OF SUCH DAMAGE. |
---|
27 | */ |
---|
28 | |
---|
29 | #ifdef HAVE_CONFIG_H |
---|
30 | #include "config.h" |
---|
31 | #endif |
---|
32 | |
---|
33 | #include <stdio.h> |
---|
34 | #include <sys/uio.h> |
---|
35 | #include <sys/types.h> |
---|
36 | #include <sys/stat.h> |
---|
37 | #include <fcntl.h> |
---|
38 | #include <unistd.h> |
---|
39 | #include <errno.h> |
---|
40 | #include <string.h> |
---|
41 | #include <ctype.h> |
---|
42 | #include <rtems/imfs.h> |
---|
43 | #include <reent.h> |
---|
44 | |
---|
45 | #include <rtems.h> |
---|
46 | #include <rtems/libio.h> |
---|
47 | |
---|
48 | #include <tmacros.h> |
---|
49 | #include "test_support.h" |
---|
50 | |
---|
51 | const char rtems_test_name[] = "PSXFILE 2"; |
---|
52 | |
---|
53 | /* forward declarations to avoid warnings */ |
---|
54 | rtems_task Init(rtems_task_argument argument); |
---|
55 | void do_with_fd(int fd, const char *description); |
---|
56 | |
---|
57 | void do_with_fd( |
---|
58 | int fd, |
---|
59 | const char *description |
---|
60 | ) |
---|
61 | { |
---|
62 | struct stat stat_buff; |
---|
63 | struct iovec vec[2]; |
---|
64 | char buf[2][1]; |
---|
65 | off_t res; |
---|
66 | int status; |
---|
67 | |
---|
68 | vec[0].iov_base = buf[0]; |
---|
69 | vec[0].iov_len = sizeof(buf[0]); |
---|
70 | vec[1].iov_base = buf[1]; |
---|
71 | vec[1].iov_len = sizeof(buf[1]); |
---|
72 | |
---|
73 | printf("ftruncate %s\n", description); |
---|
74 | status = ftruncate(fd, 40); |
---|
75 | rtems_test_assert( status == -1 ); |
---|
76 | printf( "%d: %s\n", errno, strerror( errno ) ); |
---|
77 | rtems_test_assert( errno == EBADF ); |
---|
78 | |
---|
79 | printf("_fcntl_r %s\n", description); |
---|
80 | status = _fcntl_r( NULL, fd, F_SETFD, 1 ); |
---|
81 | rtems_test_assert( status == -1 ); |
---|
82 | printf( "%d: %s\n", errno, strerror( errno ) ); |
---|
83 | rtems_test_assert( errno == EBADF ); |
---|
84 | |
---|
85 | printf("fdatasync %s\n", description); |
---|
86 | status = fdatasync( fd ); |
---|
87 | rtems_test_assert( status == -1 ); |
---|
88 | printf( "%d: %s\n", errno, strerror( errno ) ); |
---|
89 | rtems_test_assert( errno == EBADF ); |
---|
90 | |
---|
91 | printf("fstat %s\n", description); |
---|
92 | status = fstat( fd, &stat_buff ); |
---|
93 | rtems_test_assert( status == -1 ); |
---|
94 | printf( "%d: %s\n", errno, strerror( errno ) ); |
---|
95 | rtems_test_assert( errno == EBADF ); |
---|
96 | |
---|
97 | printf("fsync %s\n", description); |
---|
98 | status = fsync( fd ); |
---|
99 | rtems_test_assert( status == -1 ); |
---|
100 | printf( "%d: %s\n", errno, strerror( errno ) ); |
---|
101 | rtems_test_assert( errno == EBADF ); |
---|
102 | |
---|
103 | printf("ioctl %s\n", description); |
---|
104 | status = ioctl( fd, 0 ); |
---|
105 | rtems_test_assert( status == -1 ); |
---|
106 | printf( "%d: %s\n", errno, strerror( errno ) ); |
---|
107 | rtems_test_assert( errno == EBADF ); |
---|
108 | |
---|
109 | printf("_lseek_r %s\n", description); |
---|
110 | res = _lseek_r (NULL, fd, 0, SEEK_SET); |
---|
111 | rtems_test_assert( res == -1 ); |
---|
112 | printf( "%d: %s\n", errno, strerror( errno ) ); |
---|
113 | rtems_test_assert( errno == EBADF ); |
---|
114 | |
---|
115 | printf("readv %s\n", description); |
---|
116 | status = readv(fd, vec, 2); |
---|
117 | rtems_test_assert( status == -1 ); |
---|
118 | printf( "%d: %s\n", errno, strerror( errno ) ); |
---|
119 | rtems_test_assert( errno == EBADF ); |
---|
120 | |
---|
121 | printf("writev %s\n", description); |
---|
122 | status = writev(fd, vec, 2); |
---|
123 | rtems_test_assert( status == -1 ); |
---|
124 | printf( "%d: %s\n", errno, strerror( errno ) ); |
---|
125 | rtems_test_assert( errno == EBADF ); |
---|
126 | |
---|
127 | printf("write %s\n", description); |
---|
128 | status = write(fd, "1234", 4); |
---|
129 | rtems_test_assert( status == -1 ); |
---|
130 | printf( "%d: %s\n", errno, strerror( errno ) ); |
---|
131 | rtems_test_assert( errno == EBADF ); |
---|
132 | } |
---|
133 | |
---|
134 | rtems_task Init( |
---|
135 | rtems_task_argument argument |
---|
136 | ) |
---|
137 | { |
---|
138 | int status; |
---|
139 | int fd; |
---|
140 | |
---|
141 | TEST_BEGIN(); |
---|
142 | |
---|
143 | /* |
---|
144 | * Simple open case where the file is created. |
---|
145 | */ |
---|
146 | puts( "mkdir /tmp" ); |
---|
147 | status = mkdir( "/tmp", S_IRWXU ); |
---|
148 | rtems_test_assert( !status ); |
---|
149 | |
---|
150 | puts( "open /tmp/j" ); |
---|
151 | fd = open( "/tmp/j", O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO ); |
---|
152 | rtems_test_assert( fd != -1 ); |
---|
153 | printf( "open returned file descriptor %d\n", fd ); |
---|
154 | |
---|
155 | puts( "close /tmp/j" ); |
---|
156 | status = close( fd ); |
---|
157 | rtems_test_assert( !status ); |
---|
158 | |
---|
159 | do_with_fd( fd, "an unopened file" ); |
---|
160 | puts(""); |
---|
161 | do_with_fd( 1000, "a too large file descriptor" ); |
---|
162 | |
---|
163 | TEST_END(); |
---|
164 | |
---|
165 | rtems_test_exit(0); |
---|
166 | } |
---|
167 | |
---|
168 | /* configuration information */ |
---|
169 | |
---|
170 | #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER |
---|
171 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
172 | #define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 4 |
---|
173 | #define CONFIGURE_MAXIMUM_TASKS 1 |
---|
174 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
175 | |
---|
176 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
177 | |
---|
178 | #define CONFIGURE_INIT |
---|
179 | |
---|
180 | #include <rtems/confdefs.h> |
---|
181 | /* end of file */ |
---|