summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/net/af_unix/msg_oob.c
blob: 62361b5e98c313f5c863e9ea4c770678470ba909 (plain) (blame)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// SPDX-License-Identifier: GPL-2.0
/* Copyright Amazon.com Inc. or its affiliates. */

#include <fcntl.h>
#include <string.h>
#include <unistd.h>

#include <netinet/in.h>
#include <sys/socket.h>

#include "../../kselftest_harness.h"

#define BUF_SZ	32

FIXTURE(msg_oob)
{
	int fd[4];		/* 0: AF_UNIX sender
				 * 1: AF_UNIX receiver
				 * 2: TCP sender
				 * 3: TCP receiver
				 */
	bool tcp_compliant;
};

FIXTURE_VARIANT(msg_oob)
{
	bool peek;
};

FIXTURE_VARIANT_ADD(msg_oob, no_peek)
{
	.peek = false,
};

FIXTURE_VARIANT_ADD(msg_oob, peek)
{
	.peek = true
};

static void create_unix_socketpair(struct __test_metadata *_metadata,
				   FIXTURE_DATA(msg_oob) *self)
{
	int ret;

	ret = socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0, self->fd);
	ASSERT_EQ(ret, 0);
}

static void create_tcp_socketpair(struct __test_metadata *_metadata,
				  FIXTURE_DATA(msg_oob) *self)
{
	struct sockaddr_in addr;
	socklen_t addrlen;
	int listen_fd;
	int ret;

	listen_fd = socket(AF_INET, SOCK_STREAM, 0);
	ASSERT_GE(listen_fd, 0);

	ret = listen(listen_fd, -1);
	ASSERT_EQ(ret, 0);

	addrlen = sizeof(addr);
	ret = getsockname(listen_fd, (struct sockaddr *)&addr, &addrlen);
	ASSERT_EQ(ret, 0);

	self->fd[2] = socket(AF_INET, SOCK_STREAM, 0);
	ASSERT_GE(self->fd[2], 0);

	ret = connect(self->fd[2], (struct sockaddr *)&addr, addrlen);
	ASSERT_EQ(ret, 0);

	self->fd[3] = accept(listen_fd, (struct sockaddr *)&addr, &addrlen);
	ASSERT_GE(self->fd[3], 0);

	ret = fcntl(self->fd[3], F_SETFL, O_NONBLOCK);
	ASSERT_EQ(ret, 0);
}

static void close_sockets(FIXTURE_DATA(msg_oob) *self)
{
	int i;

	for (i = 0; i < 4; i++)
		close(self->fd[i]);
}

FIXTURE_SETUP(msg_oob)
{
	create_unix_socketpair(_metadata, self);
	create_tcp_socketpair(_metadata, self);

	self->tcp_compliant = true;
}

FIXTURE_TEARDOWN(msg_oob)
{
	close_sockets(self);
}

static void __sendpair(struct __test_metadata *_metadata,
		       FIXTURE_DATA(msg_oob) *self,
		       const void *buf, size_t len, int flags)
{
	int i, ret[2];

	for (i = 0; i < 2; i++)
		ret[i] = send(self->fd[i * 2], buf, len, flags);

	ASSERT_EQ(ret[0], len);
	ASSERT_EQ(ret[0], ret[1]);
}

static void __recvpair(struct __test_metadata *_metadata,
		       FIXTURE_DATA(msg_oob) *self,
		       const void *expected_buf, int expected_len,
		       int buf_len, int flags)
{
	int i, ret[2], recv_errno[2], expected_errno = 0;
	char recv_buf[2][BUF_SZ] = {};
	bool printed = false;

	ASSERT_GE(BUF_SZ, buf_len);

	errno = 0;

	for (i = 0; i < 2; i++) {
		ret[i] = recv(self->fd[i * 2 + 1], recv_buf[i], buf_len, flags);
		recv_errno[i] = errno;
	}

	if (expected_len < 0) {
		expected_errno = -expected_len;
		expected_len = -1;
	}

	if (ret[0] != expected_len || recv_errno[0] != expected_errno) {
		TH_LOG("AF_UNIX :%s", ret[0] < 0 ? strerror(recv_errno[0]) : recv_buf[0]);
		TH_LOG("Expected:%s", expected_errno ? strerror(expected_errno) : expected_buf);

		ASSERT_EQ(ret[0], expected_len);
		ASSERT_EQ(recv_errno[0], expected_errno);
	}

	if (ret[0] != ret[1] || recv_errno[0] != recv_errno[1]) {
		TH_LOG("AF_UNIX :%s", ret[0] < 0 ? strerror(recv_errno[0]) : recv_buf[0]);
		TH_LOG("TCP     :%s", ret[1] < 0 ? strerror(recv_errno[1]) : recv_buf[1]);

		printed = true;

		if (self->tcp_compliant) {
			ASSERT_EQ(ret[0], ret[1]);
			ASSERT_EQ(recv_errno[0], recv_errno[1]);
		}
	}

	if (expected_len >= 0) {
		int cmp;

		cmp = strncmp(expected_buf, recv_buf[0], expected_len);
		if (cmp) {
			TH_LOG("AF_UNIX :%s", ret[0] < 0 ? strerror(recv_errno[0]) : recv_buf[0]);
			TH_LOG("Expected:%s", expected_errno ? strerror(expected_errno) : expected_buf);

			ASSERT_EQ(cmp, 0);
		}

		cmp = strncmp(recv_buf[0], recv_buf[1], expected_len);
		if (cmp) {
			if (!printed) {
				TH_LOG("AF_UNIX :%s", ret[0] < 0 ? strerror(recv_errno[0]) : recv_buf[0]);
				TH_LOG("TCP     :%s", ret[1] < 0 ? strerror(recv_errno[1]) : recv_buf[1]);
			}

			if (self->tcp_compliant)
				ASSERT_EQ(cmp, 0);
		}
	}
}

static void __setinlinepair(struct __test_metadata *_metadata,
			    FIXTURE_DATA(msg_oob) *self)
{
	int i, oob_inline = 1;

	for (i = 0; i < 2; i++) {
		int ret;

		ret = setsockopt(self->fd[i * 2 + 1], SOL_SOCKET, SO_OOBINLINE,
				 &oob_inline, sizeof(oob_inline));
		ASSERT_EQ(ret, 0);
	}
}

#define sendpair(buf, len, flags)					\
	__sendpair(_metadata, self, buf, len, flags)

#define recvpair(expected_buf, expected_len, buf_len, flags)		\
	do {								\
		if (variant->peek)					\
			__recvpair(_metadata, self,			\
				   expected_buf, expected_len,		\
				   buf_len, (flags) | MSG_PEEK);	\
		__recvpair(_metadata, self,				\
			   expected_buf, expected_len, buf_len, flags);	\
	} while (0)

#define setinlinepair()							\
	__setinlinepair(_metadata, self)

#define tcp_incompliant							\
	for (self->tcp_compliant = false;				\
	     self->tcp_compliant == false;				\
	     self->tcp_compliant = true)

TEST_F(msg_oob, non_oob)
{
	sendpair("x", 1, 0);

	recvpair("", -EINVAL, 1, MSG_OOB);
}

TEST_F(msg_oob, oob)
{
	sendpair("x", 1, MSG_OOB);

	recvpair("x", 1, 1, MSG_OOB);
}

TEST_F(msg_oob, oob_drop)
{
	sendpair("x", 1, MSG_OOB);

	recvpair("", -EAGAIN, 1, 0);		/* Drop OOB. */
	recvpair("", -EINVAL, 1, MSG_OOB);
}

TEST_F(msg_oob, oob_ahead)
{
	sendpair("hello", 5, MSG_OOB);

	recvpair("o", 1, 1, MSG_OOB);
	recvpair("hell", 4, 4, 0);
}

TEST_F(msg_oob, oob_break)
{
	sendpair("hello", 5, MSG_OOB);

	recvpair("hell", 4, 5, 0);		/* Break at OOB even with enough buffer. */
	recvpair("o", 1, 1, MSG_OOB);
}

TEST_F(msg_oob, oob_ahead_break)
{
	sendpair("hello", 5, MSG_OOB);
	sendpair("world", 5, 0);

	recvpair("o", 1, 1, MSG_OOB);
	recvpair("hell", 4, 9, 0);		/* Break at OOB even after it's recv()ed. */
	recvpair("world", 5, 5, 0);
}

TEST_F(msg_oob, oob_break_drop)
{
	sendpair("hello", 5, MSG_OOB);
	sendpair("world", 5, 0);

	recvpair("hell", 4, 10, 0);		/* Break at OOB even with enough buffer. */
	recvpair("world", 5, 10, 0);		/* Drop OOB and recv() the next skb. */
	recvpair("", -EINVAL, 1, MSG_OOB);
}

TEST_F(msg_oob, ex_oob_break)
{
	sendpair("hello", 5, MSG_OOB);
	sendpair("wor", 3, MSG_OOB);
	sendpair("ld", 2, 0);

	recvpair("hellowo", 7, 10, 0);		/* Break at OOB but not at ex-OOB. */
	recvpair("r", 1, 1, MSG_OOB);
	recvpair("ld", 2, 2, 0);
}

TEST_F(msg_oob, ex_oob_drop)
{
	sendpair("x", 1, MSG_OOB);
	sendpair("y", 1, MSG_OOB);		/* TCP drops "x" at this moment. */

	tcp_incompliant {
		recvpair("x", 1, 1, 0);		/* TCP drops "y" by passing through it. */
		recvpair("y", 1, 1, MSG_OOB);	/* TCP returns -EINVAL. */
	}
}

TEST_F(msg_oob, ex_oob_drop_2)
{
	sendpair("x", 1, MSG_OOB);
	sendpair("y", 1, MSG_OOB);		/* TCP drops "x" at this moment. */

	recvpair("y", 1, 1, MSG_OOB);

	tcp_incompliant {
		recvpair("x", 1, 1, 0);		/* TCP returns -EAGAIN. */
	}
}

TEST_F(msg_oob, ex_oob_ahead_break)
{
	sendpair("hello", 5, MSG_OOB);
	sendpair("wor", 3, MSG_OOB);

	recvpair("r", 1, 1, MSG_OOB);

	sendpair("ld", 2, MSG_OOB);

	tcp_incompliant {
		recvpair("hellowol", 8, 10, 0);	/* TCP recv()s "helloworl", why "r" ?? */
	}

	recvpair("d", 1, 1, MSG_OOB);
}

TEST_F(msg_oob, inline_oob)
{
	setinlinepair();

	sendpair("x", 1, MSG_OOB);

	recvpair("", -EINVAL, 1, MSG_OOB);
	recvpair("x", 1, 1, 0);
}

TEST_F(msg_oob, inline_oob_break)
{
	setinlinepair();

	sendpair("hello", 5, MSG_OOB);

	recvpair("", -EINVAL, 1, MSG_OOB);
	recvpair("hell", 4, 5, 0);		/* Break at OOB but not at ex-OOB. */
	recvpair("o", 1, 1, 0);
}

TEST_F(msg_oob, inline_oob_ahead_break)
{
	sendpair("hello", 5, MSG_OOB);
	sendpair("world", 5, 0);

	recvpair("o", 1, 1, MSG_OOB);

	setinlinepair();

	recvpair("hell", 4, 9, 0);		/* Break at OOB even with enough buffer. */

	tcp_incompliant {
		recvpair("world", 5, 6, 0);	/* TCP recv()s "oworld", ... "o" ??? */
	}
}

TEST_F(msg_oob, inline_ex_oob_break)
{
	sendpair("hello", 5, MSG_OOB);
	sendpair("wor", 3, MSG_OOB);
	sendpair("ld", 2, 0);

	setinlinepair();

	recvpair("hellowo", 7, 10, 0);		/* Break at OOB but not at ex-OOB. */
	recvpair("rld", 3, 3, 0);
}

TEST_F(msg_oob, inline_ex_oob_no_drop)
{
	sendpair("x", 1, MSG_OOB);

	setinlinepair();

	sendpair("y", 1, MSG_OOB);		/* TCP does NOT drops "x" at this moment. */

	recvpair("x", 1, 1, 0);
	recvpair("y", 1, 1, 0);
}

TEST_F(msg_oob, inline_ex_oob_drop)
{
	sendpair("x", 1, MSG_OOB);
	sendpair("y", 1, MSG_OOB);		/* TCP drops "x" at this moment. */

	setinlinepair();

	tcp_incompliant {
		recvpair("x", 1, 1, 0);		/* TCP recv()s "y". */
		recvpair("y", 1, 1, 0);		/* TCP returns -EAGAIN. */
	}
}

TEST_HARNESS_MAIN