Skip to content

Commit 42c0978

Browse files
author
Jakub Klimczak
committed
tests: posix: device_io: Update tests for working select(), poll()
Update the device_io test suite so that it expects select(), pselect() and poll() to work on stdio. Signed-off-by: Jakub Klimczak <[email protected]>
1 parent ccc5027 commit 42c0978

File tree

2 files changed

+70
-38
lines changed

2 files changed

+70
-38
lines changed

tests/posix/device_io/src/main.c

Lines changed: 66 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,6 @@ ZTEST(posix_device_io, test_FD_CLR)
3232
FD_CLR(0, &fds);
3333
}
3434

35-
ZTEST(posix_device_io, test_FD_ISSET)
36-
{
37-
fd_set fds;
38-
39-
zassert_false(FD_ISSET(0, &fds));
40-
}
41-
4235
ZTEST(posix_device_io, test_FD_SET)
4336
{
4437
fd_set fds;
@@ -112,73 +105,108 @@ ZTEST(posix_device_io, test_open)
112105
zexpect_equal(errno, ENOENT);
113106
}
114107

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+
115113
ZTEST(posix_device_io, test_poll)
116114
{
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+
};
118120

119121
/*
120122
* Note: poll() is already exercised extensively in tests/posix/eventfd, but we should test
121123
* it here on device nodes as well.
122124
*/
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
124137
}
125138

126139
ZTEST(posix_device_io, test_pread)
127140
{
128141
uint8_t buf[8];
129142

143+
/* stdio is non-seekable */
130144
zexpect_equal(pread(STDIN_FILENO, buf, sizeof(buf), 0), -1);
131-
zexpect_equal(errno, ENOTSUP);
145+
zexpect_equal(errno, ESPIPE);
132146
}
133147

134148
ZTEST(posix_device_io, test_pselect)
135149
{
136-
fd_set fds;
150+
fd_set readfds;
151+
fd_set writefds;
137152
struct timespec timeout = {.tv_sec = 0, .tv_nsec = 0};
138153

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
145169
}
146170

147171
ZTEST(posix_device_io, test_pwrite)
148172
{
149-
/* Zephyr does not yet support writing through a file descriptor */
173+
/* stdio is non-seekable */
150174
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);
160176
}
161177

162178
ZTEST(posix_device_io, test_select)
163179
{
164-
fd_set fds;
180+
fd_set readfds;
181+
fd_set writefds;
165182
struct timeval timeout = {.tv_sec = 0, .tv_usec = 0};
166183

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
173199
}
174200

175201
ZTEST(posix_device_io, test_write)
176202
{
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));
180208
#else
181-
zexpect_equal(write(STDOUT_FILENO, "x", 1), 0);
209+
zexpect_equal(write(STDOUT_FILENO, msg, ARRAY_SIZE(msg)), 0);
182210
#endif
183211
}
184212

tests/posix/device_io/testcase.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ tests:
1212
portability.posix.device_io:
1313
extra_configs:
1414
- CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE=256
15+
portability.posix.device_io.tty_stdio:
16+
filter: CONFIG_UART_CONSOLE
17+
extra_configs:
18+
- CONFIG_CONSOLE_SUBSYS=y
1519
portability.posix.device_io.armclang_std_libc:
1620
toolchain_allow: armclang
1721
extra_configs:

0 commit comments

Comments
 (0)