1 /*
2  * Copyright (c) 2020 Friedt Professional Engineering Services, Inc
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "_main.h"
8 
ZTEST_USER_F(net_socketpair,test_write_nonblock)9 ZTEST_USER_F(net_socketpair, test_write_nonblock)
10 {
11 	int res;
12 
13 	for (size_t i = 0; i < 2; ++i) {
14 		/* first, fill up the buffer */
15 		for (size_t k = 0; k < CONFIG_NET_SOCKETPAIR_BUFFER_SIZE;
16 			++k) {
17 			res = send(fixture->sv[i], "x", 1, 0);
18 			zassert_equal(res, 1, "send() failed: %d", errno);
19 		}
20 
21 		/* then set the O_NONBLOCK flag */
22 		res = fcntl(fixture->sv[i], F_GETFL, 0);
23 		zassert_not_equal(res, -1, "fcntl() failed: %d", i, errno);
24 
25 		res = fcntl(fixture->sv[i], F_SETFL, res | O_NONBLOCK);
26 		zassert_not_equal(res, -1, "fcntl() failed: %d", i, errno);
27 
28 		/* then, try to write one more byte */
29 		res = send(fixture->sv[i], "x", 1, 0);
30 		zassert_equal(res, -1, "expected send to fail");
31 		zassert_equal(errno, EAGAIN, "errno: expected: EAGAIN "
32 			"actual: %d", errno);
33 	}
34 }
35 
ZTEST_USER_F(net_socketpair,test_read_nonblock)36 ZTEST_USER_F(net_socketpair, test_read_nonblock)
37 {
38 	int res;
39 	char c;
40 
41 	for (size_t i = 0; i < 2; ++i) {
42 		/* set the O_NONBLOCK flag */
43 		res = fcntl(fixture->sv[i], F_GETFL, 0);
44 		zassert_not_equal(res, -1, "fcntl() failed: %d", i, errno);
45 
46 		res = fcntl(fixture->sv[i], F_SETFL, res | O_NONBLOCK);
47 		zassert_not_equal(res, -1, "fcntl() failed: %d", i, errno);
48 
49 		/* then, try to read one byte */
50 		res = recv(fixture->sv[i], &c, 1, 0);
51 		zassert_equal(res, -1, "expected recv() to fail");
52 		zassert_equal(errno, EAGAIN, "errno: expected: EAGAIN "
53 			"actual: %d", errno);
54 	}
55 }
56