xref: /openbmc/linux/tools/testing/selftests/net/tls.c (revision 18afb028)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #define _GNU_SOURCE
4 
5 #include <arpa/inet.h>
6 #include <errno.h>
7 #include <error.h>
8 #include <fcntl.h>
9 #include <poll.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 
14 #include <linux/tls.h>
15 #include <linux/tcp.h>
16 #include <linux/socket.h>
17 
18 #include <sys/epoll.h>
19 #include <sys/types.h>
20 #include <sys/sendfile.h>
21 #include <sys/socket.h>
22 #include <sys/stat.h>
23 
24 #include "../kselftest_harness.h"
25 
26 #define TLS_PAYLOAD_MAX_LEN 16384
27 #define SOL_TLS 282
28 
29 static int fips_enabled;
30 
31 struct tls_crypto_info_keys {
32 	union {
33 		struct tls_crypto_info crypto_info;
34 		struct tls12_crypto_info_aes_gcm_128 aes128;
35 		struct tls12_crypto_info_chacha20_poly1305 chacha20;
36 		struct tls12_crypto_info_sm4_gcm sm4gcm;
37 		struct tls12_crypto_info_sm4_ccm sm4ccm;
38 		struct tls12_crypto_info_aes_ccm_128 aesccm128;
39 		struct tls12_crypto_info_aes_gcm_256 aesgcm256;
40 		struct tls12_crypto_info_aria_gcm_128 ariagcm128;
41 		struct tls12_crypto_info_aria_gcm_256 ariagcm256;
42 	};
43 	size_t len;
44 };
45 
46 static void tls_crypto_info_init(uint16_t tls_version, uint16_t cipher_type,
47 				 struct tls_crypto_info_keys *tls12)
48 {
49 	memset(tls12, 0, sizeof(*tls12));
50 
51 	switch (cipher_type) {
52 	case TLS_CIPHER_CHACHA20_POLY1305:
53 		tls12->len = sizeof(struct tls12_crypto_info_chacha20_poly1305);
54 		tls12->chacha20.info.version = tls_version;
55 		tls12->chacha20.info.cipher_type = cipher_type;
56 		break;
57 	case TLS_CIPHER_AES_GCM_128:
58 		tls12->len = sizeof(struct tls12_crypto_info_aes_gcm_128);
59 		tls12->aes128.info.version = tls_version;
60 		tls12->aes128.info.cipher_type = cipher_type;
61 		break;
62 	case TLS_CIPHER_SM4_GCM:
63 		tls12->len = sizeof(struct tls12_crypto_info_sm4_gcm);
64 		tls12->sm4gcm.info.version = tls_version;
65 		tls12->sm4gcm.info.cipher_type = cipher_type;
66 		break;
67 	case TLS_CIPHER_SM4_CCM:
68 		tls12->len = sizeof(struct tls12_crypto_info_sm4_ccm);
69 		tls12->sm4ccm.info.version = tls_version;
70 		tls12->sm4ccm.info.cipher_type = cipher_type;
71 		break;
72 	case TLS_CIPHER_AES_CCM_128:
73 		tls12->len = sizeof(struct tls12_crypto_info_aes_ccm_128);
74 		tls12->aesccm128.info.version = tls_version;
75 		tls12->aesccm128.info.cipher_type = cipher_type;
76 		break;
77 	case TLS_CIPHER_AES_GCM_256:
78 		tls12->len = sizeof(struct tls12_crypto_info_aes_gcm_256);
79 		tls12->aesgcm256.info.version = tls_version;
80 		tls12->aesgcm256.info.cipher_type = cipher_type;
81 		break;
82 	case TLS_CIPHER_ARIA_GCM_128:
83 		tls12->len = sizeof(struct tls12_crypto_info_aria_gcm_128);
84 		tls12->ariagcm128.info.version = tls_version;
85 		tls12->ariagcm128.info.cipher_type = cipher_type;
86 		break;
87 	case TLS_CIPHER_ARIA_GCM_256:
88 		tls12->len = sizeof(struct tls12_crypto_info_aria_gcm_256);
89 		tls12->ariagcm256.info.version = tls_version;
90 		tls12->ariagcm256.info.cipher_type = cipher_type;
91 		break;
92 	default:
93 		break;
94 	}
95 }
96 
97 static void memrnd(void *s, size_t n)
98 {
99 	int *dword = s;
100 	char *byte;
101 
102 	for (; n >= 4; n -= 4)
103 		*dword++ = rand();
104 	byte = (void *)dword;
105 	while (n--)
106 		*byte++ = rand();
107 }
108 
109 static void ulp_sock_pair(struct __test_metadata *_metadata,
110 			  int *fd, int *cfd, bool *notls)
111 {
112 	struct sockaddr_in addr;
113 	socklen_t len;
114 	int sfd, ret;
115 
116 	*notls = false;
117 	len = sizeof(addr);
118 
119 	addr.sin_family = AF_INET;
120 	addr.sin_addr.s_addr = htonl(INADDR_ANY);
121 	addr.sin_port = 0;
122 
123 	*fd = socket(AF_INET, SOCK_STREAM, 0);
124 	sfd = socket(AF_INET, SOCK_STREAM, 0);
125 
126 	ret = bind(sfd, &addr, sizeof(addr));
127 	ASSERT_EQ(ret, 0);
128 	ret = listen(sfd, 10);
129 	ASSERT_EQ(ret, 0);
130 
131 	ret = getsockname(sfd, &addr, &len);
132 	ASSERT_EQ(ret, 0);
133 
134 	ret = connect(*fd, &addr, sizeof(addr));
135 	ASSERT_EQ(ret, 0);
136 
137 	*cfd = accept(sfd, &addr, &len);
138 	ASSERT_GE(*cfd, 0);
139 
140 	close(sfd);
141 
142 	ret = setsockopt(*fd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
143 	if (ret != 0) {
144 		ASSERT_EQ(errno, ENOENT);
145 		*notls = true;
146 		printf("Failure setting TCP_ULP, testing without tls\n");
147 		return;
148 	}
149 
150 	ret = setsockopt(*cfd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
151 	ASSERT_EQ(ret, 0);
152 }
153 
154 /* Produce a basic cmsg */
155 static int tls_send_cmsg(int fd, unsigned char record_type,
156 			 void *data, size_t len, int flags)
157 {
158 	char cbuf[CMSG_SPACE(sizeof(char))];
159 	int cmsg_len = sizeof(char);
160 	struct cmsghdr *cmsg;
161 	struct msghdr msg;
162 	struct iovec vec;
163 
164 	vec.iov_base = data;
165 	vec.iov_len = len;
166 	memset(&msg, 0, sizeof(struct msghdr));
167 	msg.msg_iov = &vec;
168 	msg.msg_iovlen = 1;
169 	msg.msg_control = cbuf;
170 	msg.msg_controllen = sizeof(cbuf);
171 	cmsg = CMSG_FIRSTHDR(&msg);
172 	cmsg->cmsg_level = SOL_TLS;
173 	/* test sending non-record types. */
174 	cmsg->cmsg_type = TLS_SET_RECORD_TYPE;
175 	cmsg->cmsg_len = CMSG_LEN(cmsg_len);
176 	*CMSG_DATA(cmsg) = record_type;
177 	msg.msg_controllen = cmsg->cmsg_len;
178 
179 	return sendmsg(fd, &msg, flags);
180 }
181 
182 static int tls_recv_cmsg(struct __test_metadata *_metadata,
183 			 int fd, unsigned char record_type,
184 			 void *data, size_t len, int flags)
185 {
186 	char cbuf[CMSG_SPACE(sizeof(char))];
187 	struct cmsghdr *cmsg;
188 	unsigned char ctype;
189 	struct msghdr msg;
190 	struct iovec vec;
191 	int n;
192 
193 	vec.iov_base = data;
194 	vec.iov_len = len;
195 	memset(&msg, 0, sizeof(struct msghdr));
196 	msg.msg_iov = &vec;
197 	msg.msg_iovlen = 1;
198 	msg.msg_control = cbuf;
199 	msg.msg_controllen = sizeof(cbuf);
200 
201 	n = recvmsg(fd, &msg, flags);
202 
203 	cmsg = CMSG_FIRSTHDR(&msg);
204 	EXPECT_NE(cmsg, NULL);
205 	EXPECT_EQ(cmsg->cmsg_level, SOL_TLS);
206 	EXPECT_EQ(cmsg->cmsg_type, TLS_GET_RECORD_TYPE);
207 	ctype = *((unsigned char *)CMSG_DATA(cmsg));
208 	EXPECT_EQ(ctype, record_type);
209 
210 	return n;
211 }
212 
213 FIXTURE(tls_basic)
214 {
215 	int fd, cfd;
216 	bool notls;
217 };
218 
219 FIXTURE_SETUP(tls_basic)
220 {
221 	ulp_sock_pair(_metadata, &self->fd, &self->cfd, &self->notls);
222 }
223 
224 FIXTURE_TEARDOWN(tls_basic)
225 {
226 	close(self->fd);
227 	close(self->cfd);
228 }
229 
230 /* Send some data through with ULP but no keys */
231 TEST_F(tls_basic, base_base)
232 {
233 	char const *test_str = "test_read";
234 	int send_len = 10;
235 	char buf[10];
236 
237 	ASSERT_EQ(strlen(test_str) + 1, send_len);
238 
239 	EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
240 	EXPECT_NE(recv(self->cfd, buf, send_len, 0), -1);
241 	EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
242 };
243 
244 TEST_F(tls_basic, bad_cipher)
245 {
246 	struct tls_crypto_info_keys tls12;
247 
248 	tls12.crypto_info.version = 200;
249 	tls12.crypto_info.cipher_type = TLS_CIPHER_AES_GCM_128;
250 	EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX, &tls12, sizeof(struct tls12_crypto_info_aes_gcm_128)), -1);
251 
252 	tls12.crypto_info.version = TLS_1_2_VERSION;
253 	tls12.crypto_info.cipher_type = 50;
254 	EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX, &tls12, sizeof(struct tls12_crypto_info_aes_gcm_128)), -1);
255 
256 	tls12.crypto_info.version = TLS_1_2_VERSION;
257 	tls12.crypto_info.cipher_type = 59;
258 	EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX, &tls12, sizeof(struct tls12_crypto_info_aes_gcm_128)), -1);
259 
260 	tls12.crypto_info.version = TLS_1_2_VERSION;
261 	tls12.crypto_info.cipher_type = 10;
262 	EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX, &tls12, sizeof(struct tls12_crypto_info_aes_gcm_128)), -1);
263 
264 	tls12.crypto_info.version = TLS_1_2_VERSION;
265 	tls12.crypto_info.cipher_type = 70;
266 	EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX, &tls12, sizeof(struct tls12_crypto_info_aes_gcm_128)), -1);
267 }
268 
269 FIXTURE(tls)
270 {
271 	int fd, cfd;
272 	bool notls;
273 };
274 
275 FIXTURE_VARIANT(tls)
276 {
277 	uint16_t tls_version;
278 	uint16_t cipher_type;
279 	bool nopad, fips_non_compliant;
280 };
281 
282 FIXTURE_VARIANT_ADD(tls, 12_aes_gcm)
283 {
284 	.tls_version = TLS_1_2_VERSION,
285 	.cipher_type = TLS_CIPHER_AES_GCM_128,
286 };
287 
288 FIXTURE_VARIANT_ADD(tls, 13_aes_gcm)
289 {
290 	.tls_version = TLS_1_3_VERSION,
291 	.cipher_type = TLS_CIPHER_AES_GCM_128,
292 };
293 
294 FIXTURE_VARIANT_ADD(tls, 12_chacha)
295 {
296 	.tls_version = TLS_1_2_VERSION,
297 	.cipher_type = TLS_CIPHER_CHACHA20_POLY1305,
298 	.fips_non_compliant = true,
299 };
300 
301 FIXTURE_VARIANT_ADD(tls, 13_chacha)
302 {
303 	.tls_version = TLS_1_3_VERSION,
304 	.cipher_type = TLS_CIPHER_CHACHA20_POLY1305,
305 	.fips_non_compliant = true,
306 };
307 
308 FIXTURE_VARIANT_ADD(tls, 13_sm4_gcm)
309 {
310 	.tls_version = TLS_1_3_VERSION,
311 	.cipher_type = TLS_CIPHER_SM4_GCM,
312 	.fips_non_compliant = true,
313 };
314 
315 FIXTURE_VARIANT_ADD(tls, 13_sm4_ccm)
316 {
317 	.tls_version = TLS_1_3_VERSION,
318 	.cipher_type = TLS_CIPHER_SM4_CCM,
319 	.fips_non_compliant = true,
320 };
321 
322 FIXTURE_VARIANT_ADD(tls, 12_aes_ccm)
323 {
324 	.tls_version = TLS_1_2_VERSION,
325 	.cipher_type = TLS_CIPHER_AES_CCM_128,
326 };
327 
328 FIXTURE_VARIANT_ADD(tls, 13_aes_ccm)
329 {
330 	.tls_version = TLS_1_3_VERSION,
331 	.cipher_type = TLS_CIPHER_AES_CCM_128,
332 };
333 
334 FIXTURE_VARIANT_ADD(tls, 12_aes_gcm_256)
335 {
336 	.tls_version = TLS_1_2_VERSION,
337 	.cipher_type = TLS_CIPHER_AES_GCM_256,
338 };
339 
340 FIXTURE_VARIANT_ADD(tls, 13_aes_gcm_256)
341 {
342 	.tls_version = TLS_1_3_VERSION,
343 	.cipher_type = TLS_CIPHER_AES_GCM_256,
344 };
345 
346 FIXTURE_VARIANT_ADD(tls, 13_nopad)
347 {
348 	.tls_version = TLS_1_3_VERSION,
349 	.cipher_type = TLS_CIPHER_AES_GCM_128,
350 	.nopad = true,
351 };
352 
353 FIXTURE_VARIANT_ADD(tls, 12_aria_gcm)
354 {
355 	.tls_version = TLS_1_2_VERSION,
356 	.cipher_type = TLS_CIPHER_ARIA_GCM_128,
357 };
358 
359 FIXTURE_VARIANT_ADD(tls, 12_aria_gcm_256)
360 {
361 	.tls_version = TLS_1_2_VERSION,
362 	.cipher_type = TLS_CIPHER_ARIA_GCM_256,
363 };
364 
365 FIXTURE_SETUP(tls)
366 {
367 	struct tls_crypto_info_keys tls12;
368 	int one = 1;
369 	int ret;
370 
371 	if (fips_enabled && variant->fips_non_compliant)
372 		SKIP(return, "Unsupported cipher in FIPS mode");
373 
374 	tls_crypto_info_init(variant->tls_version, variant->cipher_type,
375 			     &tls12);
376 
377 	ulp_sock_pair(_metadata, &self->fd, &self->cfd, &self->notls);
378 
379 	if (self->notls)
380 		return;
381 
382 	ret = setsockopt(self->fd, SOL_TLS, TLS_TX, &tls12, tls12.len);
383 	ASSERT_EQ(ret, 0);
384 
385 	ret = setsockopt(self->cfd, SOL_TLS, TLS_RX, &tls12, tls12.len);
386 	ASSERT_EQ(ret, 0);
387 
388 	if (variant->nopad) {
389 		ret = setsockopt(self->cfd, SOL_TLS, TLS_RX_EXPECT_NO_PAD,
390 				 (void *)&one, sizeof(one));
391 		ASSERT_EQ(ret, 0);
392 	}
393 }
394 
395 FIXTURE_TEARDOWN(tls)
396 {
397 	close(self->fd);
398 	close(self->cfd);
399 }
400 
401 TEST_F(tls, sendfile)
402 {
403 	int filefd = open("/proc/self/exe", O_RDONLY);
404 	struct stat st;
405 
406 	EXPECT_GE(filefd, 0);
407 	fstat(filefd, &st);
408 	EXPECT_GE(sendfile(self->fd, filefd, 0, st.st_size), 0);
409 }
410 
411 TEST_F(tls, send_then_sendfile)
412 {
413 	int filefd = open("/proc/self/exe", O_RDONLY);
414 	char const *test_str = "test_send";
415 	int to_send = strlen(test_str) + 1;
416 	char recv_buf[10];
417 	struct stat st;
418 	char *buf;
419 
420 	EXPECT_GE(filefd, 0);
421 	fstat(filefd, &st);
422 	buf = (char *)malloc(st.st_size);
423 
424 	EXPECT_EQ(send(self->fd, test_str, to_send, 0), to_send);
425 	EXPECT_EQ(recv(self->cfd, recv_buf, to_send, MSG_WAITALL), to_send);
426 	EXPECT_EQ(memcmp(test_str, recv_buf, to_send), 0);
427 
428 	EXPECT_GE(sendfile(self->fd, filefd, 0, st.st_size), 0);
429 	EXPECT_EQ(recv(self->cfd, buf, st.st_size, MSG_WAITALL), st.st_size);
430 }
431 
432 static void chunked_sendfile(struct __test_metadata *_metadata,
433 			     struct _test_data_tls *self,
434 			     uint16_t chunk_size,
435 			     uint16_t extra_payload_size)
436 {
437 	char buf[TLS_PAYLOAD_MAX_LEN];
438 	uint16_t test_payload_size;
439 	int size = 0;
440 	int ret;
441 	char filename[] = "/tmp/mytemp.XXXXXX";
442 	int fd = mkstemp(filename);
443 	off_t offset = 0;
444 
445 	unlink(filename);
446 	ASSERT_GE(fd, 0);
447 	EXPECT_GE(chunk_size, 1);
448 	test_payload_size = chunk_size + extra_payload_size;
449 	ASSERT_GE(TLS_PAYLOAD_MAX_LEN, test_payload_size);
450 	memset(buf, 1, test_payload_size);
451 	size = write(fd, buf, test_payload_size);
452 	EXPECT_EQ(size, test_payload_size);
453 	fsync(fd);
454 
455 	while (size > 0) {
456 		ret = sendfile(self->fd, fd, &offset, chunk_size);
457 		EXPECT_GE(ret, 0);
458 		size -= ret;
459 	}
460 
461 	EXPECT_EQ(recv(self->cfd, buf, test_payload_size, MSG_WAITALL),
462 		  test_payload_size);
463 
464 	close(fd);
465 }
466 
467 TEST_F(tls, multi_chunk_sendfile)
468 {
469 	chunked_sendfile(_metadata, self, 4096, 4096);
470 	chunked_sendfile(_metadata, self, 4096, 0);
471 	chunked_sendfile(_metadata, self, 4096, 1);
472 	chunked_sendfile(_metadata, self, 4096, 2048);
473 	chunked_sendfile(_metadata, self, 8192, 2048);
474 	chunked_sendfile(_metadata, self, 4096, 8192);
475 	chunked_sendfile(_metadata, self, 8192, 4096);
476 	chunked_sendfile(_metadata, self, 12288, 1024);
477 	chunked_sendfile(_metadata, self, 12288, 2000);
478 	chunked_sendfile(_metadata, self, 15360, 100);
479 	chunked_sendfile(_metadata, self, 15360, 300);
480 	chunked_sendfile(_metadata, self, 1, 4096);
481 	chunked_sendfile(_metadata, self, 2048, 4096);
482 	chunked_sendfile(_metadata, self, 2048, 8192);
483 	chunked_sendfile(_metadata, self, 4096, 8192);
484 	chunked_sendfile(_metadata, self, 1024, 12288);
485 	chunked_sendfile(_metadata, self, 2000, 12288);
486 	chunked_sendfile(_metadata, self, 100, 15360);
487 	chunked_sendfile(_metadata, self, 300, 15360);
488 }
489 
490 TEST_F(tls, recv_max)
491 {
492 	unsigned int send_len = TLS_PAYLOAD_MAX_LEN;
493 	char recv_mem[TLS_PAYLOAD_MAX_LEN];
494 	char buf[TLS_PAYLOAD_MAX_LEN];
495 
496 	memrnd(buf, sizeof(buf));
497 
498 	EXPECT_GE(send(self->fd, buf, send_len, 0), 0);
499 	EXPECT_NE(recv(self->cfd, recv_mem, send_len, 0), -1);
500 	EXPECT_EQ(memcmp(buf, recv_mem, send_len), 0);
501 }
502 
503 TEST_F(tls, recv_small)
504 {
505 	char const *test_str = "test_read";
506 	int send_len = 10;
507 	char buf[10];
508 
509 	send_len = strlen(test_str) + 1;
510 	EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
511 	EXPECT_NE(recv(self->cfd, buf, send_len, 0), -1);
512 	EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
513 }
514 
515 TEST_F(tls, msg_more)
516 {
517 	char const *test_str = "test_read";
518 	int send_len = 10;
519 	char buf[10 * 2];
520 
521 	EXPECT_EQ(send(self->fd, test_str, send_len, MSG_MORE), send_len);
522 	EXPECT_EQ(recv(self->cfd, buf, send_len, MSG_DONTWAIT), -1);
523 	EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
524 	EXPECT_EQ(recv(self->cfd, buf, send_len * 2, MSG_WAITALL),
525 		  send_len * 2);
526 	EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
527 }
528 
529 TEST_F(tls, msg_more_unsent)
530 {
531 	char const *test_str = "test_read";
532 	int send_len = 10;
533 	char buf[10];
534 
535 	EXPECT_EQ(send(self->fd, test_str, send_len, MSG_MORE), send_len);
536 	EXPECT_EQ(recv(self->cfd, buf, send_len, MSG_DONTWAIT), -1);
537 }
538 
539 TEST_F(tls, msg_eor)
540 {
541 	char const *test_str = "test_read";
542 	int send_len = 10;
543 	char buf[10];
544 
545 	EXPECT_EQ(send(self->fd, test_str, send_len, MSG_EOR), send_len);
546 	EXPECT_EQ(recv(self->cfd, buf, send_len, MSG_WAITALL), send_len);
547 	EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
548 }
549 
550 TEST_F(tls, sendmsg_single)
551 {
552 	struct msghdr msg;
553 
554 	char const *test_str = "test_sendmsg";
555 	size_t send_len = 13;
556 	struct iovec vec;
557 	char buf[13];
558 
559 	vec.iov_base = (char *)test_str;
560 	vec.iov_len = send_len;
561 	memset(&msg, 0, sizeof(struct msghdr));
562 	msg.msg_iov = &vec;
563 	msg.msg_iovlen = 1;
564 	EXPECT_EQ(sendmsg(self->fd, &msg, 0), send_len);
565 	EXPECT_EQ(recv(self->cfd, buf, send_len, MSG_WAITALL), send_len);
566 	EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
567 }
568 
569 #define MAX_FRAGS	64
570 #define SEND_LEN	13
571 TEST_F(tls, sendmsg_fragmented)
572 {
573 	char const *test_str = "test_sendmsg";
574 	char buf[SEND_LEN * MAX_FRAGS];
575 	struct iovec vec[MAX_FRAGS];
576 	struct msghdr msg;
577 	int i, frags;
578 
579 	for (frags = 1; frags <= MAX_FRAGS; frags++) {
580 		for (i = 0; i < frags; i++) {
581 			vec[i].iov_base = (char *)test_str;
582 			vec[i].iov_len = SEND_LEN;
583 		}
584 
585 		memset(&msg, 0, sizeof(struct msghdr));
586 		msg.msg_iov = vec;
587 		msg.msg_iovlen = frags;
588 
589 		EXPECT_EQ(sendmsg(self->fd, &msg, 0), SEND_LEN * frags);
590 		EXPECT_EQ(recv(self->cfd, buf, SEND_LEN * frags, MSG_WAITALL),
591 			  SEND_LEN * frags);
592 
593 		for (i = 0; i < frags; i++)
594 			EXPECT_EQ(memcmp(buf + SEND_LEN * i,
595 					 test_str, SEND_LEN), 0);
596 	}
597 }
598 #undef MAX_FRAGS
599 #undef SEND_LEN
600 
601 TEST_F(tls, sendmsg_large)
602 {
603 	void *mem = malloc(16384);
604 	size_t send_len = 16384;
605 	size_t sends = 128;
606 	struct msghdr msg;
607 	size_t recvs = 0;
608 	size_t sent = 0;
609 
610 	memset(&msg, 0, sizeof(struct msghdr));
611 	while (sent++ < sends) {
612 		struct iovec vec = { (void *)mem, send_len };
613 
614 		msg.msg_iov = &vec;
615 		msg.msg_iovlen = 1;
616 		EXPECT_EQ(sendmsg(self->fd, &msg, 0), send_len);
617 	}
618 
619 	while (recvs++ < sends) {
620 		EXPECT_NE(recv(self->cfd, mem, send_len, 0), -1);
621 	}
622 
623 	free(mem);
624 }
625 
626 TEST_F(tls, sendmsg_multiple)
627 {
628 	char const *test_str = "test_sendmsg_multiple";
629 	struct iovec vec[5];
630 	char *test_strs[5];
631 	struct msghdr msg;
632 	int total_len = 0;
633 	int len_cmp = 0;
634 	int iov_len = 5;
635 	char *buf;
636 	int i;
637 
638 	memset(&msg, 0, sizeof(struct msghdr));
639 	for (i = 0; i < iov_len; i++) {
640 		test_strs[i] = (char *)malloc(strlen(test_str) + 1);
641 		snprintf(test_strs[i], strlen(test_str) + 1, "%s", test_str);
642 		vec[i].iov_base = (void *)test_strs[i];
643 		vec[i].iov_len = strlen(test_strs[i]) + 1;
644 		total_len += vec[i].iov_len;
645 	}
646 	msg.msg_iov = vec;
647 	msg.msg_iovlen = iov_len;
648 
649 	EXPECT_EQ(sendmsg(self->fd, &msg, 0), total_len);
650 	buf = malloc(total_len);
651 	EXPECT_NE(recv(self->cfd, buf, total_len, 0), -1);
652 	for (i = 0; i < iov_len; i++) {
653 		EXPECT_EQ(memcmp(test_strs[i], buf + len_cmp,
654 				 strlen(test_strs[i])),
655 			  0);
656 		len_cmp += strlen(buf + len_cmp) + 1;
657 	}
658 	for (i = 0; i < iov_len; i++)
659 		free(test_strs[i]);
660 	free(buf);
661 }
662 
663 TEST_F(tls, sendmsg_multiple_stress)
664 {
665 	char const *test_str = "abcdefghijklmno";
666 	struct iovec vec[1024];
667 	char *test_strs[1024];
668 	int iov_len = 1024;
669 	int total_len = 0;
670 	char buf[1 << 14];
671 	struct msghdr msg;
672 	int len_cmp = 0;
673 	int i;
674 
675 	memset(&msg, 0, sizeof(struct msghdr));
676 	for (i = 0; i < iov_len; i++) {
677 		test_strs[i] = (char *)malloc(strlen(test_str) + 1);
678 		snprintf(test_strs[i], strlen(test_str) + 1, "%s", test_str);
679 		vec[i].iov_base = (void *)test_strs[i];
680 		vec[i].iov_len = strlen(test_strs[i]) + 1;
681 		total_len += vec[i].iov_len;
682 	}
683 	msg.msg_iov = vec;
684 	msg.msg_iovlen = iov_len;
685 
686 	EXPECT_EQ(sendmsg(self->fd, &msg, 0), total_len);
687 	EXPECT_NE(recv(self->cfd, buf, total_len, 0), -1);
688 
689 	for (i = 0; i < iov_len; i++)
690 		len_cmp += strlen(buf + len_cmp) + 1;
691 
692 	for (i = 0; i < iov_len; i++)
693 		free(test_strs[i]);
694 }
695 
696 TEST_F(tls, splice_from_pipe)
697 {
698 	int send_len = TLS_PAYLOAD_MAX_LEN;
699 	char mem_send[TLS_PAYLOAD_MAX_LEN];
700 	char mem_recv[TLS_PAYLOAD_MAX_LEN];
701 	int p[2];
702 
703 	ASSERT_GE(pipe(p), 0);
704 	EXPECT_GE(write(p[1], mem_send, send_len), 0);
705 	EXPECT_GE(splice(p[0], NULL, self->fd, NULL, send_len, 0), 0);
706 	EXPECT_EQ(recv(self->cfd, mem_recv, send_len, MSG_WAITALL), send_len);
707 	EXPECT_EQ(memcmp(mem_send, mem_recv, send_len), 0);
708 }
709 
710 TEST_F(tls, splice_from_pipe2)
711 {
712 	int send_len = 16000;
713 	char mem_send[16000];
714 	char mem_recv[16000];
715 	int p2[2];
716 	int p[2];
717 
718 	memrnd(mem_send, sizeof(mem_send));
719 
720 	ASSERT_GE(pipe(p), 0);
721 	ASSERT_GE(pipe(p2), 0);
722 	EXPECT_EQ(write(p[1], mem_send, 8000), 8000);
723 	EXPECT_EQ(splice(p[0], NULL, self->fd, NULL, 8000, 0), 8000);
724 	EXPECT_EQ(write(p2[1], mem_send + 8000, 8000), 8000);
725 	EXPECT_EQ(splice(p2[0], NULL, self->fd, NULL, 8000, 0), 8000);
726 	EXPECT_EQ(recv(self->cfd, mem_recv, send_len, MSG_WAITALL), send_len);
727 	EXPECT_EQ(memcmp(mem_send, mem_recv, send_len), 0);
728 }
729 
730 TEST_F(tls, send_and_splice)
731 {
732 	int send_len = TLS_PAYLOAD_MAX_LEN;
733 	char mem_send[TLS_PAYLOAD_MAX_LEN];
734 	char mem_recv[TLS_PAYLOAD_MAX_LEN];
735 	char const *test_str = "test_read";
736 	int send_len2 = 10;
737 	char buf[10];
738 	int p[2];
739 
740 	ASSERT_GE(pipe(p), 0);
741 	EXPECT_EQ(send(self->fd, test_str, send_len2, 0), send_len2);
742 	EXPECT_EQ(recv(self->cfd, buf, send_len2, MSG_WAITALL), send_len2);
743 	EXPECT_EQ(memcmp(test_str, buf, send_len2), 0);
744 
745 	EXPECT_GE(write(p[1], mem_send, send_len), send_len);
746 	EXPECT_GE(splice(p[0], NULL, self->fd, NULL, send_len, 0), send_len);
747 
748 	EXPECT_EQ(recv(self->cfd, mem_recv, send_len, MSG_WAITALL), send_len);
749 	EXPECT_EQ(memcmp(mem_send, mem_recv, send_len), 0);
750 }
751 
752 TEST_F(tls, splice_to_pipe)
753 {
754 	int send_len = TLS_PAYLOAD_MAX_LEN;
755 	char mem_send[TLS_PAYLOAD_MAX_LEN];
756 	char mem_recv[TLS_PAYLOAD_MAX_LEN];
757 	int p[2];
758 
759 	memrnd(mem_send, sizeof(mem_send));
760 
761 	ASSERT_GE(pipe(p), 0);
762 	EXPECT_EQ(send(self->fd, mem_send, send_len, 0), send_len);
763 	EXPECT_EQ(splice(self->cfd, NULL, p[1], NULL, send_len, 0), send_len);
764 	EXPECT_EQ(read(p[0], mem_recv, send_len), send_len);
765 	EXPECT_EQ(memcmp(mem_send, mem_recv, send_len), 0);
766 }
767 
768 TEST_F(tls, splice_cmsg_to_pipe)
769 {
770 	char *test_str = "test_read";
771 	char record_type = 100;
772 	int send_len = 10;
773 	char buf[10];
774 	int p[2];
775 
776 	if (self->notls)
777 		SKIP(return, "no TLS support");
778 
779 	ASSERT_GE(pipe(p), 0);
780 	EXPECT_EQ(tls_send_cmsg(self->fd, 100, test_str, send_len, 0), 10);
781 	EXPECT_EQ(splice(self->cfd, NULL, p[1], NULL, send_len, 0), -1);
782 	EXPECT_EQ(errno, EINVAL);
783 	EXPECT_EQ(recv(self->cfd, buf, send_len, 0), -1);
784 	EXPECT_EQ(errno, EIO);
785 	EXPECT_EQ(tls_recv_cmsg(_metadata, self->cfd, record_type,
786 				buf, sizeof(buf), MSG_WAITALL),
787 		  send_len);
788 	EXPECT_EQ(memcmp(test_str, buf, send_len), 0);
789 }
790 
791 TEST_F(tls, splice_dec_cmsg_to_pipe)
792 {
793 	char *test_str = "test_read";
794 	char record_type = 100;
795 	int send_len = 10;
796 	char buf[10];
797 	int p[2];
798 
799 	if (self->notls)
800 		SKIP(return, "no TLS support");
801 
802 	ASSERT_GE(pipe(p), 0);
803 	EXPECT_EQ(tls_send_cmsg(self->fd, 100, test_str, send_len, 0), 10);
804 	EXPECT_EQ(recv(self->cfd, buf, send_len, 0), -1);
805 	EXPECT_EQ(errno, EIO);
806 	EXPECT_EQ(splice(self->cfd, NULL, p[1], NULL, send_len, 0), -1);
807 	EXPECT_EQ(errno, EINVAL);
808 	EXPECT_EQ(tls_recv_cmsg(_metadata, self->cfd, record_type,
809 				buf, sizeof(buf), MSG_WAITALL),
810 		  send_len);
811 	EXPECT_EQ(memcmp(test_str, buf, send_len), 0);
812 }
813 
814 TEST_F(tls, recv_and_splice)
815 {
816 	int send_len = TLS_PAYLOAD_MAX_LEN;
817 	char mem_send[TLS_PAYLOAD_MAX_LEN];
818 	char mem_recv[TLS_PAYLOAD_MAX_LEN];
819 	int half = send_len / 2;
820 	int p[2];
821 
822 	ASSERT_GE(pipe(p), 0);
823 	EXPECT_EQ(send(self->fd, mem_send, send_len, 0), send_len);
824 	/* Recv hald of the record, splice the other half */
825 	EXPECT_EQ(recv(self->cfd, mem_recv, half, MSG_WAITALL), half);
826 	EXPECT_EQ(splice(self->cfd, NULL, p[1], NULL, half, SPLICE_F_NONBLOCK),
827 		  half);
828 	EXPECT_EQ(read(p[0], &mem_recv[half], half), half);
829 	EXPECT_EQ(memcmp(mem_send, mem_recv, send_len), 0);
830 }
831 
832 TEST_F(tls, peek_and_splice)
833 {
834 	int send_len = TLS_PAYLOAD_MAX_LEN;
835 	char mem_send[TLS_PAYLOAD_MAX_LEN];
836 	char mem_recv[TLS_PAYLOAD_MAX_LEN];
837 	int chunk = TLS_PAYLOAD_MAX_LEN / 4;
838 	int n, i, p[2];
839 
840 	memrnd(mem_send, sizeof(mem_send));
841 
842 	ASSERT_GE(pipe(p), 0);
843 	for (i = 0; i < 4; i++)
844 		EXPECT_EQ(send(self->fd, &mem_send[chunk * i], chunk, 0),
845 			  chunk);
846 
847 	EXPECT_EQ(recv(self->cfd, mem_recv, chunk * 5 / 2,
848 		       MSG_WAITALL | MSG_PEEK),
849 		  chunk * 5 / 2);
850 	EXPECT_EQ(memcmp(mem_send, mem_recv, chunk * 5 / 2), 0);
851 
852 	n = 0;
853 	while (n < send_len) {
854 		i = splice(self->cfd, NULL, p[1], NULL, send_len - n, 0);
855 		EXPECT_GT(i, 0);
856 		n += i;
857 	}
858 	EXPECT_EQ(n, send_len);
859 	EXPECT_EQ(read(p[0], mem_recv, send_len), send_len);
860 	EXPECT_EQ(memcmp(mem_send, mem_recv, send_len), 0);
861 }
862 
863 TEST_F(tls, recvmsg_single)
864 {
865 	char const *test_str = "test_recvmsg_single";
866 	int send_len = strlen(test_str) + 1;
867 	char buf[20];
868 	struct msghdr hdr;
869 	struct iovec vec;
870 
871 	memset(&hdr, 0, sizeof(hdr));
872 	EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
873 	vec.iov_base = (char *)buf;
874 	vec.iov_len = send_len;
875 	hdr.msg_iovlen = 1;
876 	hdr.msg_iov = &vec;
877 	EXPECT_NE(recvmsg(self->cfd, &hdr, 0), -1);
878 	EXPECT_EQ(memcmp(test_str, buf, send_len), 0);
879 }
880 
881 TEST_F(tls, recvmsg_single_max)
882 {
883 	int send_len = TLS_PAYLOAD_MAX_LEN;
884 	char send_mem[TLS_PAYLOAD_MAX_LEN];
885 	char recv_mem[TLS_PAYLOAD_MAX_LEN];
886 	struct iovec vec;
887 	struct msghdr hdr;
888 
889 	memrnd(send_mem, sizeof(send_mem));
890 
891 	EXPECT_EQ(send(self->fd, send_mem, send_len, 0), send_len);
892 	vec.iov_base = (char *)recv_mem;
893 	vec.iov_len = TLS_PAYLOAD_MAX_LEN;
894 
895 	hdr.msg_iovlen = 1;
896 	hdr.msg_iov = &vec;
897 	EXPECT_NE(recvmsg(self->cfd, &hdr, 0), -1);
898 	EXPECT_EQ(memcmp(send_mem, recv_mem, send_len), 0);
899 }
900 
901 TEST_F(tls, recvmsg_multiple)
902 {
903 	unsigned int msg_iovlen = 1024;
904 	struct iovec vec[1024];
905 	char *iov_base[1024];
906 	unsigned int iov_len = 16;
907 	int send_len = 1 << 14;
908 	char buf[1 << 14];
909 	struct msghdr hdr;
910 	int i;
911 
912 	memrnd(buf, sizeof(buf));
913 
914 	EXPECT_EQ(send(self->fd, buf, send_len, 0), send_len);
915 	for (i = 0; i < msg_iovlen; i++) {
916 		iov_base[i] = (char *)malloc(iov_len);
917 		vec[i].iov_base = iov_base[i];
918 		vec[i].iov_len = iov_len;
919 	}
920 
921 	hdr.msg_iovlen = msg_iovlen;
922 	hdr.msg_iov = vec;
923 	EXPECT_NE(recvmsg(self->cfd, &hdr, 0), -1);
924 
925 	for (i = 0; i < msg_iovlen; i++)
926 		free(iov_base[i]);
927 }
928 
929 TEST_F(tls, single_send_multiple_recv)
930 {
931 	unsigned int total_len = TLS_PAYLOAD_MAX_LEN * 2;
932 	unsigned int send_len = TLS_PAYLOAD_MAX_LEN;
933 	char send_mem[TLS_PAYLOAD_MAX_LEN * 2];
934 	char recv_mem[TLS_PAYLOAD_MAX_LEN * 2];
935 
936 	memrnd(send_mem, sizeof(send_mem));
937 
938 	EXPECT_GE(send(self->fd, send_mem, total_len, 0), 0);
939 	memset(recv_mem, 0, total_len);
940 
941 	EXPECT_NE(recv(self->cfd, recv_mem, send_len, 0), -1);
942 	EXPECT_NE(recv(self->cfd, recv_mem + send_len, send_len, 0), -1);
943 	EXPECT_EQ(memcmp(send_mem, recv_mem, total_len), 0);
944 }
945 
946 TEST_F(tls, multiple_send_single_recv)
947 {
948 	unsigned int total_len = 2 * 10;
949 	unsigned int send_len = 10;
950 	char recv_mem[2 * 10];
951 	char send_mem[10];
952 
953 	memrnd(send_mem, sizeof(send_mem));
954 
955 	EXPECT_GE(send(self->fd, send_mem, send_len, 0), 0);
956 	EXPECT_GE(send(self->fd, send_mem, send_len, 0), 0);
957 	memset(recv_mem, 0, total_len);
958 	EXPECT_EQ(recv(self->cfd, recv_mem, total_len, MSG_WAITALL), total_len);
959 
960 	EXPECT_EQ(memcmp(send_mem, recv_mem, send_len), 0);
961 	EXPECT_EQ(memcmp(send_mem, recv_mem + send_len, send_len), 0);
962 }
963 
964 TEST_F(tls, single_send_multiple_recv_non_align)
965 {
966 	const unsigned int total_len = 15;
967 	const unsigned int recv_len = 10;
968 	char recv_mem[recv_len * 2];
969 	char send_mem[total_len];
970 
971 	memrnd(send_mem, sizeof(send_mem));
972 
973 	EXPECT_GE(send(self->fd, send_mem, total_len, 0), 0);
974 	memset(recv_mem, 0, total_len);
975 
976 	EXPECT_EQ(recv(self->cfd, recv_mem, recv_len, 0), recv_len);
977 	EXPECT_EQ(recv(self->cfd, recv_mem + recv_len, recv_len, 0), 5);
978 	EXPECT_EQ(memcmp(send_mem, recv_mem, total_len), 0);
979 }
980 
981 TEST_F(tls, recv_partial)
982 {
983 	char const *test_str = "test_read_partial";
984 	char const *test_str_first = "test_read";
985 	char const *test_str_second = "_partial";
986 	int send_len = strlen(test_str) + 1;
987 	char recv_mem[18];
988 
989 	memset(recv_mem, 0, sizeof(recv_mem));
990 	EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
991 	EXPECT_NE(recv(self->cfd, recv_mem, strlen(test_str_first),
992 		       MSG_WAITALL), -1);
993 	EXPECT_EQ(memcmp(test_str_first, recv_mem, strlen(test_str_first)), 0);
994 	memset(recv_mem, 0, sizeof(recv_mem));
995 	EXPECT_NE(recv(self->cfd, recv_mem, strlen(test_str_second),
996 		       MSG_WAITALL), -1);
997 	EXPECT_EQ(memcmp(test_str_second, recv_mem, strlen(test_str_second)),
998 		  0);
999 }
1000 
1001 TEST_F(tls, recv_nonblock)
1002 {
1003 	char buf[4096];
1004 	bool err;
1005 
1006 	EXPECT_EQ(recv(self->cfd, buf, sizeof(buf), MSG_DONTWAIT), -1);
1007 	err = (errno == EAGAIN || errno == EWOULDBLOCK);
1008 	EXPECT_EQ(err, true);
1009 }
1010 
1011 TEST_F(tls, recv_peek)
1012 {
1013 	char const *test_str = "test_read_peek";
1014 	int send_len = strlen(test_str) + 1;
1015 	char buf[15];
1016 
1017 	EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
1018 	EXPECT_EQ(recv(self->cfd, buf, send_len, MSG_PEEK), send_len);
1019 	EXPECT_EQ(memcmp(test_str, buf, send_len), 0);
1020 	memset(buf, 0, sizeof(buf));
1021 	EXPECT_EQ(recv(self->cfd, buf, send_len, 0), send_len);
1022 	EXPECT_EQ(memcmp(test_str, buf, send_len), 0);
1023 }
1024 
1025 TEST_F(tls, recv_peek_multiple)
1026 {
1027 	char const *test_str = "test_read_peek";
1028 	int send_len = strlen(test_str) + 1;
1029 	unsigned int num_peeks = 100;
1030 	char buf[15];
1031 	int i;
1032 
1033 	EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
1034 	for (i = 0; i < num_peeks; i++) {
1035 		EXPECT_NE(recv(self->cfd, buf, send_len, MSG_PEEK), -1);
1036 		EXPECT_EQ(memcmp(test_str, buf, send_len), 0);
1037 		memset(buf, 0, sizeof(buf));
1038 	}
1039 	EXPECT_NE(recv(self->cfd, buf, send_len, 0), -1);
1040 	EXPECT_EQ(memcmp(test_str, buf, send_len), 0);
1041 }
1042 
1043 TEST_F(tls, recv_peek_multiple_records)
1044 {
1045 	char const *test_str = "test_read_peek_mult_recs";
1046 	char const *test_str_first = "test_read_peek";
1047 	char const *test_str_second = "_mult_recs";
1048 	int len;
1049 	char buf[64];
1050 
1051 	len = strlen(test_str_first);
1052 	EXPECT_EQ(send(self->fd, test_str_first, len, 0), len);
1053 
1054 	len = strlen(test_str_second) + 1;
1055 	EXPECT_EQ(send(self->fd, test_str_second, len, 0), len);
1056 
1057 	len = strlen(test_str_first);
1058 	memset(buf, 0, len);
1059 	EXPECT_EQ(recv(self->cfd, buf, len, MSG_PEEK | MSG_WAITALL), len);
1060 
1061 	/* MSG_PEEK can only peek into the current record. */
1062 	len = strlen(test_str_first);
1063 	EXPECT_EQ(memcmp(test_str_first, buf, len), 0);
1064 
1065 	len = strlen(test_str) + 1;
1066 	memset(buf, 0, len);
1067 	EXPECT_EQ(recv(self->cfd, buf, len, MSG_WAITALL), len);
1068 
1069 	/* Non-MSG_PEEK will advance strparser (and therefore record)
1070 	 * however.
1071 	 */
1072 	len = strlen(test_str) + 1;
1073 	EXPECT_EQ(memcmp(test_str, buf, len), 0);
1074 
1075 	/* MSG_MORE will hold current record open, so later MSG_PEEK
1076 	 * will see everything.
1077 	 */
1078 	len = strlen(test_str_first);
1079 	EXPECT_EQ(send(self->fd, test_str_first, len, MSG_MORE), len);
1080 
1081 	len = strlen(test_str_second) + 1;
1082 	EXPECT_EQ(send(self->fd, test_str_second, len, 0), len);
1083 
1084 	len = strlen(test_str) + 1;
1085 	memset(buf, 0, len);
1086 	EXPECT_EQ(recv(self->cfd, buf, len, MSG_PEEK | MSG_WAITALL), len);
1087 
1088 	len = strlen(test_str) + 1;
1089 	EXPECT_EQ(memcmp(test_str, buf, len), 0);
1090 }
1091 
1092 TEST_F(tls, recv_peek_large_buf_mult_recs)
1093 {
1094 	char const *test_str = "test_read_peek_mult_recs";
1095 	char const *test_str_first = "test_read_peek";
1096 	char const *test_str_second = "_mult_recs";
1097 	int len;
1098 	char buf[64];
1099 
1100 	len = strlen(test_str_first);
1101 	EXPECT_EQ(send(self->fd, test_str_first, len, 0), len);
1102 
1103 	len = strlen(test_str_second) + 1;
1104 	EXPECT_EQ(send(self->fd, test_str_second, len, 0), len);
1105 
1106 	len = strlen(test_str) + 1;
1107 	memset(buf, 0, len);
1108 	EXPECT_NE((len = recv(self->cfd, buf, len,
1109 			      MSG_PEEK | MSG_WAITALL)), -1);
1110 	len = strlen(test_str) + 1;
1111 	EXPECT_EQ(memcmp(test_str, buf, len), 0);
1112 }
1113 
1114 TEST_F(tls, recv_lowat)
1115 {
1116 	char send_mem[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
1117 	char recv_mem[20];
1118 	int lowat = 8;
1119 
1120 	EXPECT_EQ(send(self->fd, send_mem, 10, 0), 10);
1121 	EXPECT_EQ(send(self->fd, send_mem, 5, 0), 5);
1122 
1123 	memset(recv_mem, 0, 20);
1124 	EXPECT_EQ(setsockopt(self->cfd, SOL_SOCKET, SO_RCVLOWAT,
1125 			     &lowat, sizeof(lowat)), 0);
1126 	EXPECT_EQ(recv(self->cfd, recv_mem, 1, MSG_WAITALL), 1);
1127 	EXPECT_EQ(recv(self->cfd, recv_mem + 1, 6, MSG_WAITALL), 6);
1128 	EXPECT_EQ(recv(self->cfd, recv_mem + 7, 10, 0), 8);
1129 
1130 	EXPECT_EQ(memcmp(send_mem, recv_mem, 10), 0);
1131 	EXPECT_EQ(memcmp(send_mem, recv_mem + 10, 5), 0);
1132 }
1133 
1134 TEST_F(tls, bidir)
1135 {
1136 	char const *test_str = "test_read";
1137 	int send_len = 10;
1138 	char buf[10];
1139 	int ret;
1140 
1141 	if (!self->notls) {
1142 		struct tls_crypto_info_keys tls12;
1143 
1144 		tls_crypto_info_init(variant->tls_version, variant->cipher_type,
1145 				     &tls12);
1146 
1147 		ret = setsockopt(self->fd, SOL_TLS, TLS_RX, &tls12,
1148 				 tls12.len);
1149 		ASSERT_EQ(ret, 0);
1150 
1151 		ret = setsockopt(self->cfd, SOL_TLS, TLS_TX, &tls12,
1152 				 tls12.len);
1153 		ASSERT_EQ(ret, 0);
1154 	}
1155 
1156 	ASSERT_EQ(strlen(test_str) + 1, send_len);
1157 
1158 	EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
1159 	EXPECT_NE(recv(self->cfd, buf, send_len, 0), -1);
1160 	EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
1161 
1162 	memset(buf, 0, sizeof(buf));
1163 
1164 	EXPECT_EQ(send(self->cfd, test_str, send_len, 0), send_len);
1165 	EXPECT_NE(recv(self->fd, buf, send_len, 0), -1);
1166 	EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
1167 };
1168 
1169 TEST_F(tls, pollin)
1170 {
1171 	char const *test_str = "test_poll";
1172 	struct pollfd fd = { 0, 0, 0 };
1173 	char buf[10];
1174 	int send_len = 10;
1175 
1176 	EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
1177 	fd.fd = self->cfd;
1178 	fd.events = POLLIN;
1179 
1180 	EXPECT_EQ(poll(&fd, 1, 20), 1);
1181 	EXPECT_EQ(fd.revents & POLLIN, 1);
1182 	EXPECT_EQ(recv(self->cfd, buf, send_len, MSG_WAITALL), send_len);
1183 	/* Test timing out */
1184 	EXPECT_EQ(poll(&fd, 1, 20), 0);
1185 }
1186 
1187 TEST_F(tls, poll_wait)
1188 {
1189 	char const *test_str = "test_poll_wait";
1190 	int send_len = strlen(test_str) + 1;
1191 	struct pollfd fd = { 0, 0, 0 };
1192 	char recv_mem[15];
1193 
1194 	fd.fd = self->cfd;
1195 	fd.events = POLLIN;
1196 	EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
1197 	/* Set timeout to inf. secs */
1198 	EXPECT_EQ(poll(&fd, 1, -1), 1);
1199 	EXPECT_EQ(fd.revents & POLLIN, 1);
1200 	EXPECT_EQ(recv(self->cfd, recv_mem, send_len, MSG_WAITALL), send_len);
1201 }
1202 
1203 TEST_F(tls, poll_wait_split)
1204 {
1205 	struct pollfd fd = { 0, 0, 0 };
1206 	char send_mem[20] = {};
1207 	char recv_mem[15];
1208 
1209 	fd.fd = self->cfd;
1210 	fd.events = POLLIN;
1211 	/* Send 20 bytes */
1212 	EXPECT_EQ(send(self->fd, send_mem, sizeof(send_mem), 0),
1213 		  sizeof(send_mem));
1214 	/* Poll with inf. timeout */
1215 	EXPECT_EQ(poll(&fd, 1, -1), 1);
1216 	EXPECT_EQ(fd.revents & POLLIN, 1);
1217 	EXPECT_EQ(recv(self->cfd, recv_mem, sizeof(recv_mem), MSG_WAITALL),
1218 		  sizeof(recv_mem));
1219 
1220 	/* Now the remaining 5 bytes of record data are in TLS ULP */
1221 	fd.fd = self->cfd;
1222 	fd.events = POLLIN;
1223 	EXPECT_EQ(poll(&fd, 1, -1), 1);
1224 	EXPECT_EQ(fd.revents & POLLIN, 1);
1225 	EXPECT_EQ(recv(self->cfd, recv_mem, sizeof(recv_mem), 0),
1226 		  sizeof(send_mem) - sizeof(recv_mem));
1227 }
1228 
1229 TEST_F(tls, blocking)
1230 {
1231 	size_t data = 100000;
1232 	int res = fork();
1233 
1234 	EXPECT_NE(res, -1);
1235 
1236 	if (res) {
1237 		/* parent */
1238 		size_t left = data;
1239 		char buf[16384];
1240 		int status;
1241 		int pid2;
1242 
1243 		while (left) {
1244 			int res = send(self->fd, buf,
1245 				       left > 16384 ? 16384 : left, 0);
1246 
1247 			EXPECT_GE(res, 0);
1248 			left -= res;
1249 		}
1250 
1251 		pid2 = wait(&status);
1252 		EXPECT_EQ(status, 0);
1253 		EXPECT_EQ(res, pid2);
1254 	} else {
1255 		/* child */
1256 		size_t left = data;
1257 		char buf[16384];
1258 
1259 		while (left) {
1260 			int res = recv(self->cfd, buf,
1261 				       left > 16384 ? 16384 : left, 0);
1262 
1263 			EXPECT_GE(res, 0);
1264 			left -= res;
1265 		}
1266 	}
1267 }
1268 
1269 TEST_F(tls, nonblocking)
1270 {
1271 	size_t data = 100000;
1272 	int sendbuf = 100;
1273 	int flags;
1274 	int res;
1275 
1276 	flags = fcntl(self->fd, F_GETFL, 0);
1277 	fcntl(self->fd, F_SETFL, flags | O_NONBLOCK);
1278 	fcntl(self->cfd, F_SETFL, flags | O_NONBLOCK);
1279 
1280 	/* Ensure nonblocking behavior by imposing a small send
1281 	 * buffer.
1282 	 */
1283 	EXPECT_EQ(setsockopt(self->fd, SOL_SOCKET, SO_SNDBUF,
1284 			     &sendbuf, sizeof(sendbuf)), 0);
1285 
1286 	res = fork();
1287 	EXPECT_NE(res, -1);
1288 
1289 	if (res) {
1290 		/* parent */
1291 		bool eagain = false;
1292 		size_t left = data;
1293 		char buf[16384];
1294 		int status;
1295 		int pid2;
1296 
1297 		while (left) {
1298 			int res = send(self->fd, buf,
1299 				       left > 16384 ? 16384 : left, 0);
1300 
1301 			if (res == -1 && errno == EAGAIN) {
1302 				eagain = true;
1303 				usleep(10000);
1304 				continue;
1305 			}
1306 			EXPECT_GE(res, 0);
1307 			left -= res;
1308 		}
1309 
1310 		EXPECT_TRUE(eagain);
1311 		pid2 = wait(&status);
1312 
1313 		EXPECT_EQ(status, 0);
1314 		EXPECT_EQ(res, pid2);
1315 	} else {
1316 		/* child */
1317 		bool eagain = false;
1318 		size_t left = data;
1319 		char buf[16384];
1320 
1321 		while (left) {
1322 			int res = recv(self->cfd, buf,
1323 				       left > 16384 ? 16384 : left, 0);
1324 
1325 			if (res == -1 && errno == EAGAIN) {
1326 				eagain = true;
1327 				usleep(10000);
1328 				continue;
1329 			}
1330 			EXPECT_GE(res, 0);
1331 			left -= res;
1332 		}
1333 		EXPECT_TRUE(eagain);
1334 	}
1335 }
1336 
1337 static void
1338 test_mutliproc(struct __test_metadata *_metadata, struct _test_data_tls *self,
1339 	       bool sendpg, unsigned int n_readers, unsigned int n_writers)
1340 {
1341 	const unsigned int n_children = n_readers + n_writers;
1342 	const size_t data = 6 * 1000 * 1000;
1343 	const size_t file_sz = data / 100;
1344 	size_t read_bias, write_bias;
1345 	int i, fd, child_id;
1346 	char buf[file_sz];
1347 	pid_t pid;
1348 
1349 	/* Only allow multiples for simplicity */
1350 	ASSERT_EQ(!(n_readers % n_writers) || !(n_writers % n_readers), true);
1351 	read_bias = n_writers / n_readers ?: 1;
1352 	write_bias = n_readers / n_writers ?: 1;
1353 
1354 	/* prep a file to send */
1355 	fd = open("/tmp/", O_TMPFILE | O_RDWR, 0600);
1356 	ASSERT_GE(fd, 0);
1357 
1358 	memset(buf, 0xac, file_sz);
1359 	ASSERT_EQ(write(fd, buf, file_sz), file_sz);
1360 
1361 	/* spawn children */
1362 	for (child_id = 0; child_id < n_children; child_id++) {
1363 		pid = fork();
1364 		ASSERT_NE(pid, -1);
1365 		if (!pid)
1366 			break;
1367 	}
1368 
1369 	/* parent waits for all children */
1370 	if (pid) {
1371 		for (i = 0; i < n_children; i++) {
1372 			int status;
1373 
1374 			wait(&status);
1375 			EXPECT_EQ(status, 0);
1376 		}
1377 
1378 		return;
1379 	}
1380 
1381 	/* Split threads for reading and writing */
1382 	if (child_id < n_readers) {
1383 		size_t left = data * read_bias;
1384 		char rb[8001];
1385 
1386 		while (left) {
1387 			int res;
1388 
1389 			res = recv(self->cfd, rb,
1390 				   left > sizeof(rb) ? sizeof(rb) : left, 0);
1391 
1392 			EXPECT_GE(res, 0);
1393 			left -= res;
1394 		}
1395 	} else {
1396 		size_t left = data * write_bias;
1397 
1398 		while (left) {
1399 			int res;
1400 
1401 			ASSERT_EQ(lseek(fd, 0, SEEK_SET), 0);
1402 			if (sendpg)
1403 				res = sendfile(self->fd, fd, NULL,
1404 					       left > file_sz ? file_sz : left);
1405 			else
1406 				res = send(self->fd, buf,
1407 					   left > file_sz ? file_sz : left, 0);
1408 
1409 			EXPECT_GE(res, 0);
1410 			left -= res;
1411 		}
1412 	}
1413 }
1414 
1415 TEST_F(tls, mutliproc_even)
1416 {
1417 	test_mutliproc(_metadata, self, false, 6, 6);
1418 }
1419 
1420 TEST_F(tls, mutliproc_readers)
1421 {
1422 	test_mutliproc(_metadata, self, false, 4, 12);
1423 }
1424 
1425 TEST_F(tls, mutliproc_writers)
1426 {
1427 	test_mutliproc(_metadata, self, false, 10, 2);
1428 }
1429 
1430 TEST_F(tls, mutliproc_sendpage_even)
1431 {
1432 	test_mutliproc(_metadata, self, true, 6, 6);
1433 }
1434 
1435 TEST_F(tls, mutliproc_sendpage_readers)
1436 {
1437 	test_mutliproc(_metadata, self, true, 4, 12);
1438 }
1439 
1440 TEST_F(tls, mutliproc_sendpage_writers)
1441 {
1442 	test_mutliproc(_metadata, self, true, 10, 2);
1443 }
1444 
1445 TEST_F(tls, control_msg)
1446 {
1447 	char *test_str = "test_read";
1448 	char record_type = 100;
1449 	int send_len = 10;
1450 	char buf[10];
1451 
1452 	if (self->notls)
1453 		SKIP(return, "no TLS support");
1454 
1455 	EXPECT_EQ(tls_send_cmsg(self->fd, record_type, test_str, send_len, 0),
1456 		  send_len);
1457 	/* Should fail because we didn't provide a control message */
1458 	EXPECT_EQ(recv(self->cfd, buf, send_len, 0), -1);
1459 
1460 	EXPECT_EQ(tls_recv_cmsg(_metadata, self->cfd, record_type,
1461 				buf, sizeof(buf), MSG_WAITALL | MSG_PEEK),
1462 		  send_len);
1463 	EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
1464 
1465 	/* Recv the message again without MSG_PEEK */
1466 	memset(buf, 0, sizeof(buf));
1467 
1468 	EXPECT_EQ(tls_recv_cmsg(_metadata, self->cfd, record_type,
1469 				buf, sizeof(buf), MSG_WAITALL),
1470 		  send_len);
1471 	EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
1472 }
1473 
1474 TEST_F(tls, shutdown)
1475 {
1476 	char const *test_str = "test_read";
1477 	int send_len = 10;
1478 	char buf[10];
1479 
1480 	ASSERT_EQ(strlen(test_str) + 1, send_len);
1481 
1482 	EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
1483 	EXPECT_NE(recv(self->cfd, buf, send_len, 0), -1);
1484 	EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
1485 
1486 	shutdown(self->fd, SHUT_RDWR);
1487 	shutdown(self->cfd, SHUT_RDWR);
1488 }
1489 
1490 TEST_F(tls, shutdown_unsent)
1491 {
1492 	char const *test_str = "test_read";
1493 	int send_len = 10;
1494 
1495 	EXPECT_EQ(send(self->fd, test_str, send_len, MSG_MORE), send_len);
1496 
1497 	shutdown(self->fd, SHUT_RDWR);
1498 	shutdown(self->cfd, SHUT_RDWR);
1499 }
1500 
1501 TEST_F(tls, shutdown_reuse)
1502 {
1503 	struct sockaddr_in addr;
1504 	int ret;
1505 
1506 	shutdown(self->fd, SHUT_RDWR);
1507 	shutdown(self->cfd, SHUT_RDWR);
1508 	close(self->cfd);
1509 
1510 	addr.sin_family = AF_INET;
1511 	addr.sin_addr.s_addr = htonl(INADDR_ANY);
1512 	addr.sin_port = 0;
1513 
1514 	ret = bind(self->fd, &addr, sizeof(addr));
1515 	EXPECT_EQ(ret, 0);
1516 	ret = listen(self->fd, 10);
1517 	EXPECT_EQ(ret, -1);
1518 	EXPECT_EQ(errno, EINVAL);
1519 
1520 	ret = connect(self->fd, &addr, sizeof(addr));
1521 	EXPECT_EQ(ret, -1);
1522 	EXPECT_EQ(errno, EISCONN);
1523 }
1524 
1525 TEST_F(tls, getsockopt)
1526 {
1527 	struct tls_crypto_info_keys expect, get;
1528 	socklen_t len;
1529 
1530 	/* get only the version/cipher */
1531 	len = sizeof(struct tls_crypto_info);
1532 	memrnd(&get, sizeof(get));
1533 	EXPECT_EQ(getsockopt(self->fd, SOL_TLS, TLS_TX, &get, &len), 0);
1534 	EXPECT_EQ(len, sizeof(struct tls_crypto_info));
1535 	EXPECT_EQ(get.crypto_info.version, variant->tls_version);
1536 	EXPECT_EQ(get.crypto_info.cipher_type, variant->cipher_type);
1537 
1538 	/* get the full crypto_info */
1539 	tls_crypto_info_init(variant->tls_version, variant->cipher_type, &expect);
1540 	len = expect.len;
1541 	memrnd(&get, sizeof(get));
1542 	EXPECT_EQ(getsockopt(self->fd, SOL_TLS, TLS_TX, &get, &len), 0);
1543 	EXPECT_EQ(len, expect.len);
1544 	EXPECT_EQ(get.crypto_info.version, variant->tls_version);
1545 	EXPECT_EQ(get.crypto_info.cipher_type, variant->cipher_type);
1546 	EXPECT_EQ(memcmp(&get, &expect, expect.len), 0);
1547 
1548 	/* short get should fail */
1549 	len = sizeof(struct tls_crypto_info) - 1;
1550 	EXPECT_EQ(getsockopt(self->fd, SOL_TLS, TLS_TX, &get, &len), -1);
1551 	EXPECT_EQ(errno, EINVAL);
1552 
1553 	/* partial get of the cipher data should fail */
1554 	len = expect.len - 1;
1555 	EXPECT_EQ(getsockopt(self->fd, SOL_TLS, TLS_TX, &get, &len), -1);
1556 	EXPECT_EQ(errno, EINVAL);
1557 }
1558 
1559 FIXTURE(tls_err)
1560 {
1561 	int fd, cfd;
1562 	int fd2, cfd2;
1563 	bool notls;
1564 };
1565 
1566 FIXTURE_VARIANT(tls_err)
1567 {
1568 	uint16_t tls_version;
1569 };
1570 
1571 FIXTURE_VARIANT_ADD(tls_err, 12_aes_gcm)
1572 {
1573 	.tls_version = TLS_1_2_VERSION,
1574 };
1575 
1576 FIXTURE_VARIANT_ADD(tls_err, 13_aes_gcm)
1577 {
1578 	.tls_version = TLS_1_3_VERSION,
1579 };
1580 
1581 FIXTURE_SETUP(tls_err)
1582 {
1583 	struct tls_crypto_info_keys tls12;
1584 	int ret;
1585 
1586 	tls_crypto_info_init(variant->tls_version, TLS_CIPHER_AES_GCM_128,
1587 			     &tls12);
1588 
1589 	ulp_sock_pair(_metadata, &self->fd, &self->cfd, &self->notls);
1590 	ulp_sock_pair(_metadata, &self->fd2, &self->cfd2, &self->notls);
1591 	if (self->notls)
1592 		return;
1593 
1594 	ret = setsockopt(self->fd, SOL_TLS, TLS_TX, &tls12, tls12.len);
1595 	ASSERT_EQ(ret, 0);
1596 
1597 	ret = setsockopt(self->cfd2, SOL_TLS, TLS_RX, &tls12, tls12.len);
1598 	ASSERT_EQ(ret, 0);
1599 }
1600 
1601 FIXTURE_TEARDOWN(tls_err)
1602 {
1603 	close(self->fd);
1604 	close(self->cfd);
1605 	close(self->fd2);
1606 	close(self->cfd2);
1607 }
1608 
1609 TEST_F(tls_err, bad_rec)
1610 {
1611 	char buf[64];
1612 
1613 	if (self->notls)
1614 		SKIP(return, "no TLS support");
1615 
1616 	memset(buf, 0x55, sizeof(buf));
1617 	EXPECT_EQ(send(self->fd2, buf, sizeof(buf), 0), sizeof(buf));
1618 	EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1);
1619 	EXPECT_EQ(errno, EMSGSIZE);
1620 	EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), MSG_DONTWAIT), -1);
1621 	EXPECT_EQ(errno, EAGAIN);
1622 }
1623 
1624 TEST_F(tls_err, bad_auth)
1625 {
1626 	char buf[128];
1627 	int n;
1628 
1629 	if (self->notls)
1630 		SKIP(return, "no TLS support");
1631 
1632 	memrnd(buf, sizeof(buf) / 2);
1633 	EXPECT_EQ(send(self->fd, buf, sizeof(buf) / 2, 0), sizeof(buf) / 2);
1634 	n = recv(self->cfd, buf, sizeof(buf), 0);
1635 	EXPECT_GT(n, sizeof(buf) / 2);
1636 
1637 	buf[n - 1]++;
1638 
1639 	EXPECT_EQ(send(self->fd2, buf, n, 0), n);
1640 	EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1);
1641 	EXPECT_EQ(errno, EBADMSG);
1642 	EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1);
1643 	EXPECT_EQ(errno, EBADMSG);
1644 }
1645 
1646 TEST_F(tls_err, bad_in_large_read)
1647 {
1648 	char txt[3][64];
1649 	char cip[3][128];
1650 	char buf[3 * 128];
1651 	int i, n;
1652 
1653 	if (self->notls)
1654 		SKIP(return, "no TLS support");
1655 
1656 	/* Put 3 records in the sockets */
1657 	for (i = 0; i < 3; i++) {
1658 		memrnd(txt[i], sizeof(txt[i]));
1659 		EXPECT_EQ(send(self->fd, txt[i], sizeof(txt[i]), 0),
1660 			  sizeof(txt[i]));
1661 		n = recv(self->cfd, cip[i], sizeof(cip[i]), 0);
1662 		EXPECT_GT(n, sizeof(txt[i]));
1663 		/* Break the third message */
1664 		if (i == 2)
1665 			cip[2][n - 1]++;
1666 		EXPECT_EQ(send(self->fd2, cip[i], n, 0), n);
1667 	}
1668 
1669 	/* We should be able to receive the first two messages */
1670 	EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), sizeof(txt[0]) * 2);
1671 	EXPECT_EQ(memcmp(buf, txt[0], sizeof(txt[0])), 0);
1672 	EXPECT_EQ(memcmp(buf + sizeof(txt[0]), txt[1], sizeof(txt[1])), 0);
1673 	/* Third mesasge is bad */
1674 	EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1);
1675 	EXPECT_EQ(errno, EBADMSG);
1676 	EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1);
1677 	EXPECT_EQ(errno, EBADMSG);
1678 }
1679 
1680 TEST_F(tls_err, bad_cmsg)
1681 {
1682 	char *test_str = "test_read";
1683 	int send_len = 10;
1684 	char cip[128];
1685 	char buf[128];
1686 	char txt[64];
1687 	int n;
1688 
1689 	if (self->notls)
1690 		SKIP(return, "no TLS support");
1691 
1692 	/* Queue up one data record */
1693 	memrnd(txt, sizeof(txt));
1694 	EXPECT_EQ(send(self->fd, txt, sizeof(txt), 0), sizeof(txt));
1695 	n = recv(self->cfd, cip, sizeof(cip), 0);
1696 	EXPECT_GT(n, sizeof(txt));
1697 	EXPECT_EQ(send(self->fd2, cip, n, 0), n);
1698 
1699 	EXPECT_EQ(tls_send_cmsg(self->fd, 100, test_str, send_len, 0), 10);
1700 	n = recv(self->cfd, cip, sizeof(cip), 0);
1701 	cip[n - 1]++; /* Break it */
1702 	EXPECT_GT(n, send_len);
1703 	EXPECT_EQ(send(self->fd2, cip, n, 0), n);
1704 
1705 	EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), sizeof(txt));
1706 	EXPECT_EQ(memcmp(buf, txt, sizeof(txt)), 0);
1707 	EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1);
1708 	EXPECT_EQ(errno, EBADMSG);
1709 	EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1);
1710 	EXPECT_EQ(errno, EBADMSG);
1711 }
1712 
1713 TEST_F(tls_err, timeo)
1714 {
1715 	struct timeval tv = { .tv_usec = 10000, };
1716 	char buf[128];
1717 	int ret;
1718 
1719 	if (self->notls)
1720 		SKIP(return, "no TLS support");
1721 
1722 	ret = setsockopt(self->cfd2, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
1723 	ASSERT_EQ(ret, 0);
1724 
1725 	ret = fork();
1726 	ASSERT_GE(ret, 0);
1727 
1728 	if (ret) {
1729 		usleep(1000); /* Give child a head start */
1730 
1731 		EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1);
1732 		EXPECT_EQ(errno, EAGAIN);
1733 
1734 		EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1);
1735 		EXPECT_EQ(errno, EAGAIN);
1736 
1737 		wait(&ret);
1738 	} else {
1739 		EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1);
1740 		EXPECT_EQ(errno, EAGAIN);
1741 		exit(0);
1742 	}
1743 }
1744 
1745 TEST_F(tls_err, poll_partial_rec)
1746 {
1747 	struct pollfd pfd = { };
1748 	ssize_t rec_len;
1749 	char rec[256];
1750 	char buf[128];
1751 
1752 	if (self->notls)
1753 		SKIP(return, "no TLS support");
1754 
1755 	pfd.fd = self->cfd2;
1756 	pfd.events = POLLIN;
1757 	EXPECT_EQ(poll(&pfd, 1, 1), 0);
1758 
1759 	memrnd(buf, sizeof(buf));
1760 	EXPECT_EQ(send(self->fd, buf, sizeof(buf), 0), sizeof(buf));
1761 	rec_len = recv(self->cfd, rec, sizeof(rec), 0);
1762 	EXPECT_GT(rec_len, sizeof(buf));
1763 
1764 	/* Write 100B, not the full record ... */
1765 	EXPECT_EQ(send(self->fd2, rec, 100, 0), 100);
1766 	/* ... no full record should mean no POLLIN */
1767 	pfd.fd = self->cfd2;
1768 	pfd.events = POLLIN;
1769 	EXPECT_EQ(poll(&pfd, 1, 1), 0);
1770 	/* Now write the rest, and it should all pop out of the other end. */
1771 	EXPECT_EQ(send(self->fd2, rec + 100, rec_len - 100, 0), rec_len - 100);
1772 	pfd.fd = self->cfd2;
1773 	pfd.events = POLLIN;
1774 	EXPECT_EQ(poll(&pfd, 1, 1), 1);
1775 	EXPECT_EQ(recv(self->cfd2, rec, sizeof(rec), 0), sizeof(buf));
1776 	EXPECT_EQ(memcmp(buf, rec, sizeof(buf)), 0);
1777 }
1778 
1779 TEST_F(tls_err, epoll_partial_rec)
1780 {
1781 	struct epoll_event ev, events[10];
1782 	ssize_t rec_len;
1783 	char rec[256];
1784 	char buf[128];
1785 	int epollfd;
1786 
1787 	if (self->notls)
1788 		SKIP(return, "no TLS support");
1789 
1790 	epollfd = epoll_create1(0);
1791 	ASSERT_GE(epollfd, 0);
1792 
1793 	memset(&ev, 0, sizeof(ev));
1794 	ev.events = EPOLLIN;
1795 	ev.data.fd = self->cfd2;
1796 	ASSERT_GE(epoll_ctl(epollfd, EPOLL_CTL_ADD, self->cfd2, &ev), 0);
1797 
1798 	EXPECT_EQ(epoll_wait(epollfd, events, 10, 0), 0);
1799 
1800 	memrnd(buf, sizeof(buf));
1801 	EXPECT_EQ(send(self->fd, buf, sizeof(buf), 0), sizeof(buf));
1802 	rec_len = recv(self->cfd, rec, sizeof(rec), 0);
1803 	EXPECT_GT(rec_len, sizeof(buf));
1804 
1805 	/* Write 100B, not the full record ... */
1806 	EXPECT_EQ(send(self->fd2, rec, 100, 0), 100);
1807 	/* ... no full record should mean no POLLIN */
1808 	EXPECT_EQ(epoll_wait(epollfd, events, 10, 0), 0);
1809 	/* Now write the rest, and it should all pop out of the other end. */
1810 	EXPECT_EQ(send(self->fd2, rec + 100, rec_len - 100, 0), rec_len - 100);
1811 	EXPECT_EQ(epoll_wait(epollfd, events, 10, 0), 1);
1812 	EXPECT_EQ(recv(self->cfd2, rec, sizeof(rec), 0), sizeof(buf));
1813 	EXPECT_EQ(memcmp(buf, rec, sizeof(buf)), 0);
1814 
1815 	close(epollfd);
1816 }
1817 
1818 TEST_F(tls_err, poll_partial_rec_async)
1819 {
1820 	struct pollfd pfd = { };
1821 	ssize_t rec_len;
1822 	char rec[256];
1823 	char buf[128];
1824 	char token;
1825 	int p[2];
1826 	int ret;
1827 
1828 	if (self->notls)
1829 		SKIP(return, "no TLS support");
1830 
1831 	ASSERT_GE(pipe(p), 0);
1832 
1833 	memrnd(buf, sizeof(buf));
1834 	EXPECT_EQ(send(self->fd, buf, sizeof(buf), 0), sizeof(buf));
1835 	rec_len = recv(self->cfd, rec, sizeof(rec), 0);
1836 	EXPECT_GT(rec_len, sizeof(buf));
1837 
1838 	ret = fork();
1839 	ASSERT_GE(ret, 0);
1840 
1841 	if (ret) {
1842 		int status, pid2;
1843 
1844 		close(p[1]);
1845 		usleep(1000); /* Give child a head start */
1846 
1847 		EXPECT_EQ(send(self->fd2, rec, 100, 0), 100);
1848 
1849 		EXPECT_EQ(read(p[0], &token, 1), 1); /* Barrier #1 */
1850 
1851 		EXPECT_EQ(send(self->fd2, rec + 100, rec_len - 100, 0),
1852 			  rec_len - 100);
1853 
1854 		pid2 = wait(&status);
1855 		EXPECT_EQ(pid2, ret);
1856 		EXPECT_EQ(status, 0);
1857 	} else {
1858 		close(p[0]);
1859 
1860 		/* Child should sleep in poll(), never get a wake */
1861 		pfd.fd = self->cfd2;
1862 		pfd.events = POLLIN;
1863 		EXPECT_EQ(poll(&pfd, 1, 5), 0);
1864 
1865 		EXPECT_EQ(write(p[1], &token, 1), 1); /* Barrier #1 */
1866 
1867 		pfd.fd = self->cfd2;
1868 		pfd.events = POLLIN;
1869 		EXPECT_EQ(poll(&pfd, 1, 5), 1);
1870 
1871 		exit(!_metadata->passed);
1872 	}
1873 }
1874 
1875 TEST(non_established) {
1876 	struct tls12_crypto_info_aes_gcm_256 tls12;
1877 	struct sockaddr_in addr;
1878 	int sfd, ret, fd;
1879 	socklen_t len;
1880 
1881 	len = sizeof(addr);
1882 
1883 	memset(&tls12, 0, sizeof(tls12));
1884 	tls12.info.version = TLS_1_2_VERSION;
1885 	tls12.info.cipher_type = TLS_CIPHER_AES_GCM_256;
1886 
1887 	addr.sin_family = AF_INET;
1888 	addr.sin_addr.s_addr = htonl(INADDR_ANY);
1889 	addr.sin_port = 0;
1890 
1891 	fd = socket(AF_INET, SOCK_STREAM, 0);
1892 	sfd = socket(AF_INET, SOCK_STREAM, 0);
1893 
1894 	ret = bind(sfd, &addr, sizeof(addr));
1895 	ASSERT_EQ(ret, 0);
1896 	ret = listen(sfd, 10);
1897 	ASSERT_EQ(ret, 0);
1898 
1899 	ret = setsockopt(fd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
1900 	EXPECT_EQ(ret, -1);
1901 	/* TLS ULP not supported */
1902 	if (errno == ENOENT)
1903 		return;
1904 	EXPECT_EQ(errno, ENOTCONN);
1905 
1906 	ret = setsockopt(sfd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
1907 	EXPECT_EQ(ret, -1);
1908 	EXPECT_EQ(errno, ENOTCONN);
1909 
1910 	ret = getsockname(sfd, &addr, &len);
1911 	ASSERT_EQ(ret, 0);
1912 
1913 	ret = connect(fd, &addr, sizeof(addr));
1914 	ASSERT_EQ(ret, 0);
1915 
1916 	ret = setsockopt(fd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
1917 	ASSERT_EQ(ret, 0);
1918 
1919 	ret = setsockopt(fd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
1920 	EXPECT_EQ(ret, -1);
1921 	EXPECT_EQ(errno, EEXIST);
1922 
1923 	close(fd);
1924 	close(sfd);
1925 }
1926 
1927 TEST(keysizes) {
1928 	struct tls12_crypto_info_aes_gcm_256 tls12;
1929 	int ret, fd, cfd;
1930 	bool notls;
1931 
1932 	memset(&tls12, 0, sizeof(tls12));
1933 	tls12.info.version = TLS_1_2_VERSION;
1934 	tls12.info.cipher_type = TLS_CIPHER_AES_GCM_256;
1935 
1936 	ulp_sock_pair(_metadata, &fd, &cfd, &notls);
1937 
1938 	if (!notls) {
1939 		ret = setsockopt(fd, SOL_TLS, TLS_TX, &tls12,
1940 				 sizeof(tls12));
1941 		EXPECT_EQ(ret, 0);
1942 
1943 		ret = setsockopt(cfd, SOL_TLS, TLS_RX, &tls12,
1944 				 sizeof(tls12));
1945 		EXPECT_EQ(ret, 0);
1946 	}
1947 
1948 	close(fd);
1949 	close(cfd);
1950 }
1951 
1952 TEST(no_pad) {
1953 	struct tls12_crypto_info_aes_gcm_256 tls12;
1954 	int ret, fd, cfd, val;
1955 	socklen_t len;
1956 	bool notls;
1957 
1958 	memset(&tls12, 0, sizeof(tls12));
1959 	tls12.info.version = TLS_1_3_VERSION;
1960 	tls12.info.cipher_type = TLS_CIPHER_AES_GCM_256;
1961 
1962 	ulp_sock_pair(_metadata, &fd, &cfd, &notls);
1963 
1964 	if (notls)
1965 		exit(KSFT_SKIP);
1966 
1967 	ret = setsockopt(fd, SOL_TLS, TLS_TX, &tls12, sizeof(tls12));
1968 	EXPECT_EQ(ret, 0);
1969 
1970 	ret = setsockopt(cfd, SOL_TLS, TLS_RX, &tls12, sizeof(tls12));
1971 	EXPECT_EQ(ret, 0);
1972 
1973 	val = 1;
1974 	ret = setsockopt(cfd, SOL_TLS, TLS_RX_EXPECT_NO_PAD,
1975 			 (void *)&val, sizeof(val));
1976 	EXPECT_EQ(ret, 0);
1977 
1978 	len = sizeof(val);
1979 	val = 2;
1980 	ret = getsockopt(cfd, SOL_TLS, TLS_RX_EXPECT_NO_PAD,
1981 			 (void *)&val, &len);
1982 	EXPECT_EQ(ret, 0);
1983 	EXPECT_EQ(val, 1);
1984 	EXPECT_EQ(len, 4);
1985 
1986 	val = 0;
1987 	ret = setsockopt(cfd, SOL_TLS, TLS_RX_EXPECT_NO_PAD,
1988 			 (void *)&val, sizeof(val));
1989 	EXPECT_EQ(ret, 0);
1990 
1991 	len = sizeof(val);
1992 	val = 2;
1993 	ret = getsockopt(cfd, SOL_TLS, TLS_RX_EXPECT_NO_PAD,
1994 			 (void *)&val, &len);
1995 	EXPECT_EQ(ret, 0);
1996 	EXPECT_EQ(val, 0);
1997 	EXPECT_EQ(len, 4);
1998 
1999 	close(fd);
2000 	close(cfd);
2001 }
2002 
2003 TEST(tls_v6ops) {
2004 	struct tls_crypto_info_keys tls12;
2005 	struct sockaddr_in6 addr, addr2;
2006 	int sfd, ret, fd;
2007 	socklen_t len, len2;
2008 
2009 	tls_crypto_info_init(TLS_1_2_VERSION, TLS_CIPHER_AES_GCM_128, &tls12);
2010 
2011 	addr.sin6_family = AF_INET6;
2012 	addr.sin6_addr = in6addr_any;
2013 	addr.sin6_port = 0;
2014 
2015 	fd = socket(AF_INET6, SOCK_STREAM, 0);
2016 	sfd = socket(AF_INET6, SOCK_STREAM, 0);
2017 
2018 	ret = bind(sfd, &addr, sizeof(addr));
2019 	ASSERT_EQ(ret, 0);
2020 	ret = listen(sfd, 10);
2021 	ASSERT_EQ(ret, 0);
2022 
2023 	len = sizeof(addr);
2024 	ret = getsockname(sfd, &addr, &len);
2025 	ASSERT_EQ(ret, 0);
2026 
2027 	ret = connect(fd, &addr, sizeof(addr));
2028 	ASSERT_EQ(ret, 0);
2029 
2030 	len = sizeof(addr);
2031 	ret = getsockname(fd, &addr, &len);
2032 	ASSERT_EQ(ret, 0);
2033 
2034 	ret = setsockopt(fd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
2035 	if (ret) {
2036 		ASSERT_EQ(errno, ENOENT);
2037 		SKIP(return, "no TLS support");
2038 	}
2039 	ASSERT_EQ(ret, 0);
2040 
2041 	ret = setsockopt(fd, SOL_TLS, TLS_TX, &tls12, tls12.len);
2042 	ASSERT_EQ(ret, 0);
2043 
2044 	ret = setsockopt(fd, SOL_TLS, TLS_RX, &tls12, tls12.len);
2045 	ASSERT_EQ(ret, 0);
2046 
2047 	len2 = sizeof(addr2);
2048 	ret = getsockname(fd, &addr2, &len2);
2049 	ASSERT_EQ(ret, 0);
2050 
2051 	EXPECT_EQ(len2, len);
2052 	EXPECT_EQ(memcmp(&addr, &addr2, len), 0);
2053 
2054 	close(fd);
2055 	close(sfd);
2056 }
2057 
2058 TEST(prequeue) {
2059 	struct tls_crypto_info_keys tls12;
2060 	char buf[20000], buf2[20000];
2061 	struct sockaddr_in addr;
2062 	int sfd, cfd, ret, fd;
2063 	socklen_t len;
2064 
2065 	len = sizeof(addr);
2066 	memrnd(buf, sizeof(buf));
2067 
2068 	tls_crypto_info_init(TLS_1_2_VERSION, TLS_CIPHER_AES_GCM_256, &tls12);
2069 
2070 	addr.sin_family = AF_INET;
2071 	addr.sin_addr.s_addr = htonl(INADDR_ANY);
2072 	addr.sin_port = 0;
2073 
2074 	fd = socket(AF_INET, SOCK_STREAM, 0);
2075 	sfd = socket(AF_INET, SOCK_STREAM, 0);
2076 
2077 	ASSERT_EQ(bind(sfd, &addr, sizeof(addr)), 0);
2078 	ASSERT_EQ(listen(sfd, 10), 0);
2079 	ASSERT_EQ(getsockname(sfd, &addr, &len), 0);
2080 	ASSERT_EQ(connect(fd, &addr, sizeof(addr)), 0);
2081 	ASSERT_GE(cfd = accept(sfd, &addr, &len), 0);
2082 	close(sfd);
2083 
2084 	ret = setsockopt(fd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
2085 	if (ret) {
2086 		ASSERT_EQ(errno, ENOENT);
2087 		SKIP(return, "no TLS support");
2088 	}
2089 
2090 	ASSERT_EQ(setsockopt(fd, SOL_TLS, TLS_TX, &tls12, tls12.len), 0);
2091 	EXPECT_EQ(send(fd, buf, sizeof(buf), MSG_DONTWAIT), sizeof(buf));
2092 
2093 	ASSERT_EQ(setsockopt(cfd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls")), 0);
2094 	ASSERT_EQ(setsockopt(cfd, SOL_TLS, TLS_RX, &tls12, tls12.len), 0);
2095 	EXPECT_EQ(recv(cfd, buf2, sizeof(buf2), MSG_WAITALL), sizeof(buf2));
2096 
2097 	EXPECT_EQ(memcmp(buf, buf2, sizeof(buf)), 0);
2098 
2099 	close(fd);
2100 	close(cfd);
2101 }
2102 
2103 static void __attribute__((constructor)) fips_check(void) {
2104 	int res;
2105 	FILE *f;
2106 
2107 	f = fopen("/proc/sys/crypto/fips_enabled", "r");
2108 	if (f) {
2109 		res = fscanf(f, "%d", &fips_enabled);
2110 		if (res != 1)
2111 			ksft_print_msg("ERROR: Couldn't read /proc/sys/crypto/fips_enabled\n");
2112 		fclose(f);
2113 	}
2114 }
2115 
2116 TEST_HARNESS_MAIN
2117