1 /*
2 * Copyright (c) 2023, Meta
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include "_main.h"
8
9 #undef read
10 #define read(fd, buf, len) zsock_recv(fd, buf, len, 0)
11
12 #undef write
13 #define write(fd, buf, len) zsock_send(fd, buf, len, 0)
14
ZTEST_F(net_socketpair,test_ioctl_fionread)15 ZTEST_F(net_socketpair, test_ioctl_fionread)
16 {
17 int avail;
18 uint8_t byte;
19
20 /* both ends should have zero bytes available after being newly created */
21 for (int i = 0; i < 2; ++i) {
22 avail = 42;
23 zassert_ok(zsock_ioctl(fixture->sv[i], ZFD_IOCTL_FIONREAD, &avail));
24 zassert_equal(avail, 0);
25 }
26
27 /* write something to one end, check availability from the other end */
28 for (int i = 0; i < 2; ++i) {
29 int j = (i + 1) % 2;
30
31 zassert_equal(1, write(fixture->sv[i], "\x42", 1));
32 zassert_ok(zsock_ioctl(fixture->sv[j], ZFD_IOCTL_FIONREAD, &avail));
33 zassert_equal(avail, 1);
34 }
35
36 /* read the other end, ensure availability is zero again */
37 for (int i = 0; i < 2; ++i) {
38 zassert_equal(1, read(fixture->sv[i], &byte, 1));
39 zassert_ok(zsock_ioctl(fixture->sv[i], ZFD_IOCTL_FIONREAD, &avail));
40 zassert_equal(avail, 0);
41 }
42 }
43