@@ -32,13 +32,6 @@ ZTEST(posix_device_io, test_FD_CLR)
32
32
FD_CLR (0 , & fds );
33
33
}
34
34
35
- ZTEST (posix_device_io , test_FD_ISSET )
36
- {
37
- fd_set fds ;
38
-
39
- zassert_false (FD_ISSET (0 , & fds ));
40
- }
41
-
42
35
ZTEST (posix_device_io , test_FD_SET )
43
36
{
44
37
fd_set fds ;
@@ -112,73 +105,108 @@ ZTEST(posix_device_io, test_open)
112
105
zexpect_equal (errno , ENOENT );
113
106
}
114
107
108
+ #if (defined(CONFIG_POSIX_DEVICE_IO_STDIO_CONSOLE ) && \
109
+ (CONFIG_POSIX_DEVICE_IO_STDIN_BUFSIZE + CONFIG_POSIX_DEVICE_IO_STDOUT_BUFSIZE > 0 ))
110
+ #define STDIO_POLL
111
+ #endif
112
+
115
113
ZTEST (posix_device_io , test_poll )
116
114
{
117
- struct pollfd fds [1 ] = {{.fd = STDIN_FILENO , .events = POLLIN }};
115
+ struct pollfd fds [] = {
116
+ {.fd = STDIN_FILENO , .events = POLLIN },
117
+ {.fd = STDOUT_FILENO , .events = POLLOUT },
118
+ {.fd = STDERR_FILENO , .events = POLLOUT },
119
+ };
118
120
119
121
/*
120
122
* Note: poll() is already exercised extensively in tests/posix/eventfd, but we should test
121
123
* it here on device nodes as well.
122
124
*/
123
- zexpect_equal (poll (fds , ARRAY_SIZE (fds ), 0 ), 1 );
125
+ #ifdef STDIO_POLL
126
+ zexpect_equal (poll (fds , ARRAY_SIZE (fds ), 0 ), 2 );
127
+ /* stdin shouldn't have any bytes ready to read */
128
+ zexpect_equal (fds [0 ].revents , 0 );
129
+ zexpect_equal (fds [1 ].revents , POLLOUT );
130
+ zexpect_equal (fds [2 ].revents , POLLOUT );
131
+ #else
132
+ zexpect_equal (poll (fds , ARRAY_SIZE (fds ), 0 ), 3 );
133
+ zexpect_equal (fds [0 ].revents , POLLNVAL );
134
+ zexpect_equal (fds [1 ].revents , POLLNVAL );
135
+ zexpect_equal (fds [2 ].revents , POLLNVAL );
136
+ #endif
124
137
}
125
138
126
139
ZTEST (posix_device_io , test_pread )
127
140
{
128
141
uint8_t buf [8 ];
129
142
143
+ /* stdio is non-seekable */
130
144
zexpect_equal (pread (STDIN_FILENO , buf , sizeof (buf ), 0 ), -1 );
131
- zexpect_equal (errno , ENOTSUP );
145
+ zexpect_equal (errno , ESPIPE );
132
146
}
133
147
134
148
ZTEST (posix_device_io , test_pselect )
135
149
{
136
- fd_set fds ;
150
+ fd_set readfds ;
151
+ fd_set writefds ;
137
152
struct timespec timeout = {.tv_sec = 0 , .tv_nsec = 0 };
138
153
139
- FD_ZERO (& fds );
140
- FD_SET (STDIN_FILENO , & fds );
141
-
142
- /* Zephyr does not yet support select or poll on stdin */
143
- zexpect_equal (pselect (STDIN_FILENO + 1 , & fds , NULL , NULL , & timeout , NULL ), -1 );
144
- zexpect_equal (errno , EBADF );
154
+ FD_ZERO (& readfds );
155
+ FD_SET (STDIN_FILENO , & readfds );
156
+ FD_ZERO (& writefds );
157
+ FD_SET (STDOUT_FILENO , & writefds );
158
+ FD_SET (STDERR_FILENO , & writefds );
159
+
160
+ #ifdef STDIO_POLL
161
+ zexpect_equal (pselect (STDERR_FILENO + 1 , & readfds , & writefds , NULL , & timeout , NULL ), 2 );
162
+ zassert_false (FD_ISSET (STDIN_FILENO , & readfds ));
163
+ zassert_true (FD_ISSET (STDOUT_FILENO , & writefds ));
164
+ zassert_true (FD_ISSET (STDERR_FILENO , & writefds ));
165
+ #else
166
+ zexpect_equal (pselect (STDERR_FILENO + 1 , & readfds , & writefds , NULL , & timeout , NULL ), -1 );
167
+ zassert_equal (errno , EBADF );
168
+ #endif
145
169
}
146
170
147
171
ZTEST (posix_device_io , test_pwrite )
148
172
{
149
- /* Zephyr does not yet support writing through a file descriptor */
173
+ /* stdio is non-seekable */
150
174
zexpect_equal (pwrite (STDOUT_FILENO , "x" , 1 , 0 ), -1 );
151
- zexpect_equal (errno , ENOTSUP , "%d" , errno );
152
- }
153
-
154
- ZTEST (posix_device_io , test_read )
155
- {
156
- uint8_t buf [8 ];
157
-
158
- /* reading from stdin does not work in Zephyr */
159
- zassert_equal (read (STDIN_FILENO , buf , sizeof (buf )), 0 );
175
+ zexpect_equal (errno , ESPIPE , "%d" , errno );
160
176
}
161
177
162
178
ZTEST (posix_device_io , test_select )
163
179
{
164
- fd_set fds ;
180
+ fd_set readfds ;
181
+ fd_set writefds ;
165
182
struct timeval timeout = {.tv_sec = 0 , .tv_usec = 0 };
166
183
167
- FD_ZERO (& fds );
168
- FD_SET (STDIN_FILENO , & fds );
169
-
170
- /* Zephyr does not yet support select or poll on stdin */
171
- zassert_equal (select (STDIN_FILENO + 1 , & fds , NULL , NULL , & timeout ), -1 );
172
- zexpect_equal (errno , EBADF , "%d" , errno );
184
+ FD_ZERO (& readfds );
185
+ FD_SET (STDIN_FILENO , & readfds );
186
+ FD_ZERO (& writefds );
187
+ FD_SET (STDOUT_FILENO , & writefds );
188
+ FD_SET (STDERR_FILENO , & writefds );
189
+
190
+ #ifdef STDIO_POLL
191
+ zassert_equal (select (STDERR_FILENO + 1 , & readfds , & writefds , NULL , & timeout ), 2 );
192
+ zassert_false (FD_ISSET (STDIN_FILENO , & readfds ));
193
+ zassert_true (FD_ISSET (STDOUT_FILENO , & writefds ));
194
+ zassert_true (FD_ISSET (STDERR_FILENO , & writefds ));
195
+ #else
196
+ zassert_equal (select (STDERR_FILENO + 1 , & readfds , & writefds , NULL , & timeout ), -1 );
197
+ zassert_equal (errno , EBADF );
198
+ #endif
173
199
}
174
200
175
201
ZTEST (posix_device_io , test_write )
176
202
{
177
- /* write is only implemented in newlib and arcmwdt */
178
- #if defined(CONFIG_NEWLIB_LIBC ) || defined(CONFIG_ARCMWDT_LIBC )
179
- zexpect_equal (write (STDOUT_FILENO , "x" , 1 ), 1 );
203
+ static const char msg [] = "Hello world!\n" ;
204
+
205
+ #if defined(CONFIG_POSIX_DEVICE_IO_STDIO_CONSOLE ) || defined(CONFIG_ARCMWDT_LIBC ) || \
206
+ defined(CONFIG_MINIMAL_LIBC ) || defined(CONFIG_NEWLIB_LIBC ) || defined(CONFIG_PICOLIBC )
207
+ zexpect_equal (write (STDOUT_FILENO , msg , ARRAY_SIZE (msg )), ARRAY_SIZE (msg ));
180
208
#else
181
- zexpect_equal (write (STDOUT_FILENO , "x" , 1 ), 0 );
209
+ zexpect_equal (write (STDOUT_FILENO , msg , ARRAY_SIZE ( msg ) ), 0 );
182
210
#endif
183
211
}
184
212
0 commit comments