1 /*
2  * Copyright (c) 2020 Friedt Professional Engineering Services, Inc
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <fcntl.h>
8 
9 #include <logging/log.h>
10 LOG_MODULE_DECLARE(net_test, CONFIG_NET_SOCKETS_LOG_LEVEL);
11 
12 #include <stdio.h>
13 #include <string.h>
14 #include <net/socket.h>
15 #include <sys/util.h>
16 #include <posix/unistd.h>
17 
18 #include <ztest_assert.h>
19 
20 #undef read
21 #define read(fd, buf, len) zsock_recv(fd, buf, len, 0)
22 
23 #undef write
24 #define write(fd, buf, len) zsock_send(fd, buf, len, 0)
25 
test_socketpair_fcntl(void)26 void test_socketpair_fcntl(void)
27 {
28 	int res;
29 	int sv[2] = {-1, -1};
30 	int flags;
31 
32 	res = socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
33 	zassert_equal(res, 0,
34 		"socketpair(AF_UNIX, SOCK_STREAM, 0, sv) failed");
35 
36 	res = fcntl(sv[0], F_GETFL, 0);
37 	zassert_not_equal(res, -1,
38 		"fcntl(sv[0], F_GETFL) failed. errno: %d", errno);
39 
40 	flags = res;
41 	zassert_equal(res & O_NONBLOCK, 0,
42 		"socketpair should block by default");
43 
44 	res = fcntl(sv[0], F_SETFL, flags | O_NONBLOCK);
45 	zassert_not_equal(res, -1,
46 		"fcntl(sv[0], F_SETFL, flags | O_NONBLOCK) failed. errno: %d",
47 		errno);
48 
49 	res = fcntl(sv[0], F_GETFL, 0);
50 	zassert_equal(res ^ flags, O_NONBLOCK, "expected O_NONBLOCK set");
51 
52 	close(sv[0]);
53 	close(sv[1]);
54 }
55