summaryrefslogtreecommitdiff
path: root/tests/kernel/kqueue/t_empty.c
blob: e733e917c5287b60d7a5f44c96ab9f317ea9624c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* $NetBSD: t_empty.c,v 1.1 2021/10/23 01:28:33 thorpej Exp $ */

/*-
 * Copyright (c) 2021 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__RCSID("$NetBSD: t_empty.c,v 1.1 2021/10/23 01:28:33 thorpej Exp $");

#include <sys/event.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>

#include <netinet/in.h>

#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <atf-c.h>

static void
test_empty(int readfd, int writefd, bool is_tcp)
{
	struct timespec ts = { 0, 0 };
	struct kevent event;
	int kq, error, sndbufsize;
	char buf[1024] = { 0 };
	ssize_t rv;

	ATF_REQUIRE((kq = kqueue()) >= 0);

	EV_SET(&event, writefd, EVFILT_EMPTY, EV_ADD, 0, 0, NULL);
	ATF_REQUIRE(kevent(kq, &event, 1, NULL, 0, NULL) == 0);

	/* Check that EMPTY is true. */
	memset(&event, 0, sizeof(event));
	ATF_REQUIRE(kevent(kq, NULL, 0, &event, 1, &ts) == 1);
	ATF_REQUIRE(event.ident == (uintptr_t)writefd);
	ATF_REQUIRE(event.filter == EVFILT_EMPTY);

	if (is_tcp) {
		/*
		 * Get the write socket buffer size so that we can set
		 * the read socket buffer size to something larger
		 * later on.
		 */
		socklen_t slen = sizeof(sndbufsize);
		ATF_REQUIRE(getsockopt(writefd, SOL_SOCKET,
		    SO_SNDBUF, &sndbufsize, &slen) == 0);

		/*
		 * Set the receive buffer size to 1, slamming shut
		 * the TCP receive window, thus trapping all of the
		 * data in the sender's queue.
		 */
		int val = 1;
		ATF_REQUIRE(setsockopt(readfd, SOL_SOCKET,
		    SO_RCVBUF, &val, sizeof(val)) == 0);
	}

	/* Write until the write buffer is full. */
	for (rv = 0; rv != -1;) {
		rv = write(writefd, buf, sizeof(buf));
		error = errno;
		ATF_REQUIRE(rv > 0 || (rv == -1 && error == EAGAIN));
	}

	/* Check that EMPTY is false. */
	ATF_REQUIRE(kevent(kq, NULL, 0, &event, 1, &ts) == 0);

	if (is_tcp) {
		/*
		 * Set the receive buffer size to something larger than
		 * the sender's send buffer.
		 */
		int val = sndbufsize + 128;
		ATF_REQUIRE(setsockopt(readfd, SOL_SOCKET,
		    SO_RCVBUF, &val, sizeof(val)) == 0);
	}

	/* Read all of the data that's available. */
	for (rv = 0; rv != -1;) {
		rv = read(readfd, buf, sizeof(buf));
		error = errno;
		ATF_REQUIRE(rv > 0 || (rv == -1 && error == EAGAIN));
	}

	/*
	 * Check that EMPTY is true.  Check a few times (TCP might
	 * not drain immediately).
	 */
	if (is_tcp) {
		for (rv = 0; rv < 5; rv++) {
			if (kevent(kq, NULL, 0, &event, 1, &ts) == 1) {
				break;
			}
		}
		sleep(1);
	}
	memset(&event, 0, sizeof(event));
	ATF_REQUIRE(kevent(kq, NULL, 0, &event, 1, &ts) == 1);
	ATF_REQUIRE(event.ident == (uintptr_t)writefd);
	ATF_REQUIRE(event.filter == EVFILT_EMPTY);
}

ATF_TC(sock_tcp);
ATF_TC_HEAD(sock_tcp, tc)
{
	atf_tc_set_md_var(tc, "descr",
	    "Test EVFILT_EMPTY with TCP sockets.");
}

ATF_TC_BODY(sock_tcp, tc)
{
	int readsock, writesock;
	socklen_t slen;

	ATF_REQUIRE((readsock =
	    socket(PF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP)) != -1);
	ATF_REQUIRE((writesock =
	    socket(PF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP)) != -1);

	struct sockaddr_in sin = {
		.sin_len = sizeof(sin),
		.sin_family = AF_INET,
		.sin_port = 0,		/* no need to swap 0 */
		.sin_addr = { .s_addr = htonl(INADDR_LOOPBACK) },
	};
	ATF_REQUIRE(bind(readsock, (struct sockaddr *)&sin,
	    sizeof(sin)) == 0);
	ATF_REQUIRE(listen(readsock, 1) == 0);
	slen = sizeof(sin);
	ATF_REQUIRE(getsockname(readsock, (struct sockaddr *)&sin, &slen) == 0);

	ATF_REQUIRE_ERRNO(EINPROGRESS,
	    connect(writesock, (struct sockaddr *)&sin, sizeof(sin)) == -1);

	slen = sizeof(sin);
	ATF_REQUIRE((readsock = accept(readsock, (struct sockaddr *)&sin,
	    &slen)) != -1);

	test_empty(readsock, writesock, true);
}

ATF_TP_ADD_TCS(tp)
{
	ATF_TP_ADD_TC(tp, sock_tcp);

	return atf_no_error();
}