1 /*
2  * Copyright (c) 2020 Tobias Svehagen
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "_main.h"
8 
ZTEST_F(eventfd,test_read_nonblock)9 ZTEST_F(eventfd, test_read_nonblock)
10 {
11 	eventfd_t val = 0;
12 	int ret;
13 
14 	reopen(&fixture->fd, 0, EFD_NONBLOCK);
15 
16 	ret = eventfd_read(fixture->fd, &val);
17 	zassert_true(ret == -1, "read unset ret %d", ret);
18 	zassert_true(errno == EAGAIN, "errno %d", errno);
19 
20 	ret = eventfd_write(fixture->fd, TESTVAL);
21 	zassert_true(ret == 0, "write ret %d", ret);
22 
23 	ret = eventfd_read(fixture->fd, &val);
24 	zassert_true(ret == 0, "read set ret %d", ret);
25 	zassert_true(val == TESTVAL, "red set val %d", val);
26 
27 	ret = eventfd_read(fixture->fd, &val);
28 	zassert_true(ret == -1, "read subsequent ret %d val %d", ret, val);
29 	zassert_true(errno == EAGAIN, "errno %d", errno);
30 }
31 
ZTEST_F(eventfd,test_set_poll_event_nonblock)32 ZTEST_F(eventfd, test_set_poll_event_nonblock)
33 {
34 	reopen(&fixture->fd, TESTVAL, EFD_NONBLOCK);
35 	eventfd_poll_set_common(fixture->fd);
36 }
37 
ZTEST_F(eventfd,test_unset_poll_event_nonblock)38 ZTEST_F(eventfd, test_unset_poll_event_nonblock)
39 {
40 	reopen(&fixture->fd, 0, EFD_NONBLOCK);
41 	eventfd_poll_unset_common(fixture->fd);
42 }
43 
ZTEST_F(eventfd,test_overflow)44 ZTEST_F(eventfd, test_overflow)
45 {
46 	short event;
47 	int ret;
48 
49 	reopen(&fixture->fd, 0, EFD_NONBLOCK);
50 
51 	event = POLLOUT;
52 	ret = is_blocked(fixture->fd, &event);
53 	zassert_equal(ret, 0, "eventfd write blocked with initval == 0");
54 
55 	ret = eventfd_write(fixture->fd, UINT64_MAX);
56 	zassert_equal(ret, -1, "fd == %d", fixture->fd);
57 	zassert_equal(errno, EINVAL, "did not get EINVAL");
58 
59 	ret = eventfd_write(fixture->fd, UINT64_MAX-1);
60 	zassert_equal(ret, 0, "fd == %d", fixture->fd);
61 
62 	event = POLLOUT;
63 	ret = is_blocked(fixture->fd, &event);
64 	zassert_equal(ret, 1, "eventfd write not blocked with cnt == UINT64_MAX-1");
65 
66 	ret = eventfd_write(fixture->fd, 1);
67 	zassert_equal(ret, -1, "fd == %d", fixture->fd);
68 	zassert_equal(errno, EAGAIN, "did not get EINVAL");
69 }
70