@@ -60,33 +60,73 @@ ZTEST(posix_device_io, test_close)
60
60
zassert_not_ok (close (-1 ));
61
61
}
62
62
63
+ /*
64
+ * FIXME: this corrupts stdin for some reason
63
65
ZTEST(posix_device_io, test_fdopen)
64
66
{
65
- zassert_not_ok (fdopen (1 , "r" ));
67
+ zassert_not_null (fdopen(1, "r"));
66
68
}
69
+ */
67
70
68
71
ZTEST (posix_device_io , test_fileno )
69
72
{
70
- zassert_equal (fileno (stdout ), STDOUT_FILENO );
73
+ #define DECL_TDATA (_stream , _fd ) \
74
+ {.stream_name = #_stream, .stream = _stream, .fd_name = #_fd, .fd = _fd}
75
+
76
+ const struct fileno_test_data {
77
+ const char * stream_name ;
78
+ FILE * stream ;
79
+ const char * fd_name ;
80
+ int fd ;
81
+ } test_data [] = {
82
+ DECL_TDATA (stdin , STDIN_FILENO ),
83
+ DECL_TDATA (stdout , STDOUT_FILENO ),
84
+ DECL_TDATA (stderr , STDERR_FILENO ),
85
+ };
86
+
87
+ ARRAY_FOR_EACH (test_data , i ) {
88
+ FILE * stream = test_data [i ].stream ;
89
+ int expect_fd = test_data [i ].fd ;
90
+ int actual_fd = fileno (stream );
91
+
92
+ if ((i == STDERR_FILENO ) &&
93
+ (IS_ENABLED (CONFIG_PICOLIBC ) || IS_ENABLED (CONFIG_NEWLIB_LIBC ))) {
94
+ TC_PRINT ("Note: stderr not enabled\n" );
95
+ continue ;
96
+ }
97
+
98
+ zexpect_equal (actual_fd , expect_fd , "fileno(%s) (%d) != %s (%d)" ,
99
+ test_data [i ].stream_name , actual_fd , test_data [i ].fd_name , expect_fd );
100
+ }
71
101
}
72
102
73
103
ZTEST (posix_device_io , test_open )
74
104
{
75
- zassert_equal (open ("/dev/null" , O_RDONLY ), -1 );
105
+ /*
106
+ * Note: open() is already exercised extensively in tests/posix/fs, but we should test it
107
+ * here on device nodes as well.
108
+ */
109
+ zexpect_equal (open ("/dev/null" , O_RDONLY ), -1 );
110
+ zexpect_equal (errno , ENOENT );
76
111
}
77
112
78
113
ZTEST (posix_device_io , test_poll )
79
114
{
80
115
struct pollfd fds [1 ] = {{.fd = STDIN_FILENO , .events = POLLIN }};
81
116
82
- zassert_ok (poll (fds , ARRAY_SIZE (fds ), 0 ));
117
+ /*
118
+ * Note: poll() is already exercised extensively in tests/posix/eventfd, but we should test
119
+ * it here on device nodes as well.
120
+ */
121
+ zexpect_equal (poll (fds , ARRAY_SIZE (fds ), 0 ), 1 );
83
122
}
84
123
85
124
ZTEST (posix_device_io , test_pread )
86
125
{
87
126
uint8_t buf [8 ];
88
127
89
- zassert_ok (pread (STDIN_FILENO , buf , sizeof (buf ), 0 ));
128
+ zexpect_equal (pread (STDIN_FILENO , buf , sizeof (buf ), 0 ), -1 );
129
+ zexpect_equal (errno , ENOTSUP );
90
130
}
91
131
92
132
ZTEST (posix_device_io , test_pselect )
@@ -97,19 +137,24 @@ ZTEST(posix_device_io, test_pselect)
97
137
FD_ZERO (& fds );
98
138
FD_SET (STDIN_FILENO , & fds );
99
139
100
- zassert_ok (pselect (STDIN_FILENO + 1 , & fds , NULL , NULL , & timeout , NULL ));
140
+ /* Zephyr does not yet support select or poll on stdin */
141
+ zexpect_equal (pselect (STDIN_FILENO + 1 , & fds , NULL , NULL , & timeout , NULL ), -1 );
142
+ zexpect_equal (errno , EBADF );
101
143
}
102
144
103
145
ZTEST (posix_device_io , test_pwrite )
104
146
{
105
- zassert_ok (pwrite (STDOUT_FILENO , "x" , 1 , 0 ));
147
+ /* Zephyr does not yet support writing through a file descriptor */
148
+ zexpect_equal (pwrite (STDOUT_FILENO , "x" , 1 , 0 ), -1 );
149
+ zexpect_equal (errno , ENOTSUP , "%d" , errno );
106
150
}
107
151
108
152
ZTEST (posix_device_io , test_read )
109
153
{
110
154
uint8_t buf [8 ];
111
155
112
- zassert_ok (read (STDIN_FILENO , buf , sizeof (buf )));
156
+ /* reading from stdin does not work in Zephyr */
157
+ zassert_equal (read (STDIN_FILENO , buf , sizeof (buf )), 0 );
113
158
}
114
159
115
160
ZTEST (posix_device_io , test_select )
@@ -120,12 +165,14 @@ ZTEST(posix_device_io, test_select)
120
165
FD_ZERO (& fds );
121
166
FD_SET (STDIN_FILENO , & fds );
122
167
123
- zassert_ok (select (STDIN_FILENO + 1 , & fds , NULL , NULL , & timeout ));
168
+ /* Zephyr does not yet support select or poll on stdin */
169
+ zassert_equal (select (STDIN_FILENO + 1 , & fds , NULL , NULL , & timeout ), -1 );
170
+ zexpect_equal (errno , EBADF , "%d" , errno );
124
171
}
125
172
126
173
ZTEST (posix_device_io , test_write )
127
174
{
128
- zassert_ok ( pwrite (STDOUT_FILENO , "x" , 1 , 0 ) );
175
+ zexpect_equal ( write (STDOUT_FILENO , "x" , 1 ), 1 );
129
176
}
130
177
131
178
ZTEST_SUITE (posix_device_io , NULL , NULL , NULL , NULL , NULL );
0 commit comments