1 /*
2 * Copyright (c) 2021 Nordic Semiconductor
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/logging/log.h>
8 LOG_MODULE_REGISTER(net_test, CONFIG_NET_SOCKETS_LOG_LEVEL);
9
10 #include <zephyr/ztest_assert.h>
11 #include <zephyr/posix/fcntl.h>
12 #include <zephyr/net/loopback.h>
13 #include <zephyr/net/socket.h>
14 #include <zephyr/net/tls_credentials.h>
15 #include <mbedtls/ssl.h>
16
17 #include "../../socket_helpers.h"
18
19 #define TEST_STR_SMALL "test"
20
21 #define MY_IPV4_ADDR "127.0.0.1"
22 #define MY_IPV6_ADDR "::1"
23
24 #define ANY_PORT 0
25 #define SERVER_PORT 4242
26
27 #define PSK_TAG 1
28
29 #define MAX_CONNS 5
30
31 #define TCP_TEARDOWN_TIMEOUT K_MSEC(CONFIG_NET_TCP_TIME_WAIT_DELAY)
32
33 #define TLS_TEST_WORK_QUEUE_STACK_SIZE 3072
34
35 K_THREAD_STACK_DEFINE(tls_test_work_queue_stack, TLS_TEST_WORK_QUEUE_STACK_SIZE);
36 static struct k_work_q tls_test_work_queue;
37
38 int c_sock = -1, s_sock = -1, new_sock = -1;
39
test_work_reschedule(struct k_work_delayable * dwork,k_timeout_t delay)40 static void test_work_reschedule(struct k_work_delayable *dwork,
41 k_timeout_t delay)
42 {
43 k_work_reschedule_for_queue(&tls_test_work_queue, dwork, delay);
44 }
45
test_work_wait(struct k_work_delayable * dwork)46 static void test_work_wait(struct k_work_delayable *dwork)
47 {
48 struct k_work_sync sync;
49
50 k_work_cancel_delayable_sync(dwork, &sync);
51 }
52
53 static const unsigned char psk[] = {
54 0x01, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
55 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
56 };
57 static const char psk_id[] = "test_identity";
58
test_config_psk(int s_sock,int c_sock)59 static void test_config_psk(int s_sock, int c_sock)
60 {
61 sec_tag_t sec_tag_list[] = {
62 PSK_TAG
63 };
64
65 (void)tls_credential_delete(PSK_TAG, TLS_CREDENTIAL_PSK);
66 (void)tls_credential_delete(PSK_TAG, TLS_CREDENTIAL_PSK_ID);
67
68 zassert_equal(tls_credential_add(PSK_TAG, TLS_CREDENTIAL_PSK,
69 psk, sizeof(psk)),
70 0, "Failed to register PSK %d");
71 zassert_equal(tls_credential_add(PSK_TAG, TLS_CREDENTIAL_PSK_ID,
72 psk_id, strlen(psk_id)),
73 0, "Failed to register PSK ID");
74
75 if (s_sock >= 0) {
76 zassert_equal(zsock_setsockopt(s_sock, SOL_TLS, TLS_SEC_TAG_LIST,
77 sec_tag_list, sizeof(sec_tag_list)),
78 0, "Failed to set PSK on server socket");
79 }
80
81 if (c_sock >= 0) {
82 zassert_equal(zsock_setsockopt(c_sock, SOL_TLS, TLS_SEC_TAG_LIST,
83 sec_tag_list, sizeof(sec_tag_list)),
84 0, "Failed to set PSK on client socket");
85 }
86 }
87
test_fcntl(int sock,int cmd,int val)88 static void test_fcntl(int sock, int cmd, int val)
89 {
90 zassert_equal(zsock_fcntl(sock, cmd, val), 0, "fcntl failed");
91 }
92
test_bind(int sock,struct sockaddr * addr,socklen_t addrlen)93 static void test_bind(int sock, struct sockaddr *addr, socklen_t addrlen)
94 {
95 zassert_equal(zsock_bind(sock, addr, addrlen),
96 0,
97 "bind failed");
98 }
99
test_listen(int sock)100 static void test_listen(int sock)
101 {
102 zassert_equal(zsock_listen(sock, MAX_CONNS),
103 0,
104 "listen failed");
105 }
106
test_connect(int sock,struct sockaddr * addr,socklen_t addrlen)107 static void test_connect(int sock, struct sockaddr *addr, socklen_t addrlen)
108 {
109 k_yield();
110
111 zassert_equal(zsock_connect(sock, addr, addrlen),
112 0,
113 "connect failed");
114
115 if (IS_ENABLED(CONFIG_NET_TC_THREAD_PREEMPTIVE)) {
116 /* Let the connection proceed */
117 k_yield();
118 }
119 }
120
test_send(int sock,const void * buf,size_t len,int flags)121 static void test_send(int sock, const void *buf, size_t len, int flags)
122 {
123 zassert_equal(zsock_send(sock, buf, len, flags),
124 len,
125 "send failed");
126 }
127
test_sendmsg(int sock,const struct msghdr * msg,int flags)128 static void test_sendmsg(int sock, const struct msghdr *msg, int flags)
129 {
130 size_t total_len = 0;
131
132 for (int i = 0; i < msg->msg_iovlen; i++) {
133 struct iovec *vec = msg->msg_iov + i;
134
135 total_len += vec->iov_len;
136 }
137
138 zassert_equal(zsock_sendmsg(sock, msg, flags),
139 total_len,
140 "sendmsg failed");
141 }
142
test_accept(int sock,int * new_sock,struct sockaddr * addr,socklen_t * addrlen)143 static void test_accept(int sock, int *new_sock, struct sockaddr *addr,
144 socklen_t *addrlen)
145 {
146 zassert_not_null(new_sock, "null newsock");
147
148 *new_sock = zsock_accept(sock, addr, addrlen);
149 zassert_true(*new_sock >= 0, "accept failed");
150 }
151
test_shutdown(int sock,int how)152 static void test_shutdown(int sock, int how)
153 {
154 zassert_equal(zsock_shutdown(sock, how),
155 0,
156 "shutdown failed");
157 }
158
test_close(int sock)159 static void test_close(int sock)
160 {
161 zassert_equal(zsock_close(sock),
162 0,
163 "close failed");
164 }
165
test_sockets_close(void)166 static void test_sockets_close(void)
167 {
168 if (c_sock >= 0) {
169 test_close(c_sock);
170 c_sock = -1;
171 }
172
173 if (s_sock >= 0) {
174 test_close(s_sock);
175 s_sock = -1;
176 }
177
178 if (new_sock >= 0) {
179 test_close(new_sock);
180 new_sock = -1;
181 }
182 }
183
test_eof(int sock)184 static void test_eof(int sock)
185 {
186 char rx_buf[1];
187 ssize_t recved;
188
189 /* Test that EOF properly detected. */
190 recved = zsock_recv(sock, rx_buf, sizeof(rx_buf), 0);
191 zassert_equal(recved, 0, "");
192
193 /* Calling again should be OK. */
194 recved = zsock_recv(sock, rx_buf, sizeof(rx_buf), ZSOCK_MSG_DONTWAIT);
195 zassert_equal(recved, 0, "");
196
197 /* Calling when TCP connection is fully torn down should be still OK. */
198 k_sleep(TCP_TEARDOWN_TIMEOUT);
199 recved = zsock_recv(sock, rx_buf, sizeof(rx_buf), ZSOCK_MSG_DONTWAIT);
200 zassert_equal(recved, 0, "");
201 }
202
ZTEST(net_socket_tls,test_so_type)203 ZTEST(net_socket_tls, test_so_type)
204 {
205 struct sockaddr_in bind_addr4;
206 struct sockaddr_in6 bind_addr6;
207 int sock1, sock2, rv;
208 int optval;
209 socklen_t optlen = sizeof(optval);
210
211 prepare_sock_tls_v4(MY_IPV4_ADDR, ANY_PORT, &sock1, &bind_addr4, IPPROTO_TLS_1_2);
212 prepare_sock_tls_v6(MY_IPV6_ADDR, ANY_PORT, &sock2, &bind_addr6, IPPROTO_TLS_1_2);
213
214 rv = zsock_getsockopt(sock1, SOL_SOCKET, SO_TYPE, &optval, &optlen);
215 zassert_equal(rv, 0, "getsockopt failed (%d)", errno);
216 zassert_equal(optval, SOCK_STREAM, "getsockopt got invalid type");
217 zassert_equal(optlen, sizeof(optval), "getsockopt got invalid size");
218
219 rv = zsock_getsockopt(sock2, SOL_SOCKET, SO_TYPE, &optval, &optlen);
220 zassert_equal(rv, 0, "getsockopt failed (%d)", errno);
221 zassert_equal(optval, SOCK_STREAM, "getsockopt got invalid type");
222 zassert_equal(optlen, sizeof(optval), "getsockopt got invalid size");
223
224 test_close(sock1);
225 test_close(sock2);
226 }
227
ZTEST(net_socket_tls,test_so_protocol)228 ZTEST(net_socket_tls, test_so_protocol)
229 {
230 struct sockaddr_in bind_addr4;
231 struct sockaddr_in6 bind_addr6;
232 int sock1, sock2, rv;
233 int optval;
234 socklen_t optlen = sizeof(optval);
235
236 prepare_sock_tls_v4(MY_IPV4_ADDR, ANY_PORT, &sock1, &bind_addr4, IPPROTO_TLS_1_2);
237 prepare_sock_tls_v6(MY_IPV6_ADDR, ANY_PORT, &sock2, &bind_addr6, IPPROTO_TLS_1_1);
238
239 rv = zsock_getsockopt(sock1, SOL_SOCKET, SO_PROTOCOL, &optval, &optlen);
240 zassert_equal(rv, 0, "getsockopt failed (%d)", errno);
241 zassert_equal(optval, IPPROTO_TLS_1_2,
242 "getsockopt got invalid protocol");
243 zassert_equal(optlen, sizeof(optval), "getsockopt got invalid size");
244
245 rv = zsock_getsockopt(sock2, SOL_SOCKET, SO_PROTOCOL, &optval, &optlen);
246 zassert_equal(rv, 0, "getsockopt failed (%d)", errno);
247 zassert_equal(optval, IPPROTO_TLS_1_1,
248 "getsockopt got invalid protocol");
249 zassert_equal(optlen, sizeof(optval), "getsockopt got invalid size");
250
251 test_close(sock1);
252 test_close(sock2);
253 }
254
255 struct test_msg_waitall_data {
256 struct k_work_delayable tx_work;
257 int sock;
258 const uint8_t *data;
259 size_t offset;
260 int retries;
261 };
262
test_msg_waitall_tx_work_handler(struct k_work * work)263 static void test_msg_waitall_tx_work_handler(struct k_work *work)
264 {
265 struct k_work_delayable *dwork = k_work_delayable_from_work(work);
266 struct test_msg_waitall_data *test_data =
267 CONTAINER_OF(dwork, struct test_msg_waitall_data, tx_work);
268
269 if (test_data->retries > 0) {
270 test_send(test_data->sock, test_data->data + test_data->offset, 1, 0);
271 test_data->offset++;
272 test_data->retries--;
273 test_work_reschedule(&test_data->tx_work, K_MSEC(10));
274 }
275 }
276
277 struct connect_data {
278 struct k_work_delayable work;
279 int sock;
280 struct sockaddr *addr;
281 };
282
client_connect_work_handler(struct k_work * work)283 static void client_connect_work_handler(struct k_work *work)
284 {
285 struct k_work_delayable *dwork = k_work_delayable_from_work(work);
286 struct connect_data *data =
287 CONTAINER_OF(dwork, struct connect_data, work);
288
289 test_connect(data->sock, data->addr, data->addr->sa_family == AF_INET ?
290 sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6));
291 }
292
dtls_client_connect_send_work_handler(struct k_work * work)293 static void dtls_client_connect_send_work_handler(struct k_work *work)
294 {
295 struct k_work_delayable *dwork = k_work_delayable_from_work(work);
296 struct connect_data *data =
297 CONTAINER_OF(dwork, struct connect_data, work);
298 uint8_t tx_buf = 0;
299
300 test_connect(data->sock, data->addr, data->addr->sa_family == AF_INET ?
301 sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6));
302 test_send(data->sock, &tx_buf, sizeof(tx_buf), 0);
303 }
304
test_prepare_tls_connection(sa_family_t family)305 static void test_prepare_tls_connection(sa_family_t family)
306 {
307 struct sockaddr c_saddr;
308 struct sockaddr s_saddr;
309 socklen_t exp_addrlen = family == AF_INET6 ?
310 sizeof(struct sockaddr_in6) :
311 sizeof(struct sockaddr_in);
312 struct sockaddr addr;
313 socklen_t addrlen = sizeof(addr);
314 struct connect_data test_data;
315
316 if (family == AF_INET6) {
317 prepare_sock_tls_v6(MY_IPV6_ADDR, ANY_PORT, &c_sock,
318 (struct sockaddr_in6 *)&c_saddr,
319 IPPROTO_TLS_1_2);
320 prepare_sock_tls_v6(MY_IPV6_ADDR, ANY_PORT, &s_sock,
321 (struct sockaddr_in6 *)&s_saddr,
322 IPPROTO_TLS_1_2);
323 } else {
324 prepare_sock_tls_v4(MY_IPV4_ADDR, ANY_PORT, &c_sock,
325 (struct sockaddr_in *)&c_saddr,
326 IPPROTO_TLS_1_2);
327 prepare_sock_tls_v4(MY_IPV4_ADDR, ANY_PORT, &s_sock,
328 (struct sockaddr_in *)&s_saddr,
329 IPPROTO_TLS_1_2);
330 }
331
332 test_config_psk(s_sock, c_sock);
333
334 test_bind(s_sock, &s_saddr, exp_addrlen);
335 test_listen(s_sock);
336
337 /* Helper work for the connect operation - need to handle client/server
338 * in parallel due to handshake.
339 */
340 test_data.sock = c_sock;
341 test_data.addr = &s_saddr;
342 k_work_init_delayable(&test_data.work, client_connect_work_handler);
343 test_work_reschedule(&test_data.work, K_NO_WAIT);
344
345 test_accept(s_sock, &new_sock, &addr, &addrlen);
346 zassert_equal(addrlen, exp_addrlen, "Wrong addrlen");
347
348 test_work_wait(&test_data.work);
349 }
350
test_prepare_dtls_connection(sa_family_t family)351 static void test_prepare_dtls_connection(sa_family_t family)
352 {
353 struct sockaddr c_saddr;
354 struct sockaddr s_saddr;
355 socklen_t exp_addrlen = family == AF_INET6 ?
356 sizeof(struct sockaddr_in6) :
357 sizeof(struct sockaddr_in);
358 struct connect_data test_data;
359 int role = TLS_DTLS_ROLE_SERVER;
360 struct zsock_pollfd fds[1];
361 uint8_t rx_buf;
362 int ret;
363
364
365 if (family == AF_INET6) {
366 prepare_sock_dtls_v6(MY_IPV6_ADDR, ANY_PORT, &c_sock,
367 (struct sockaddr_in6 *)&c_saddr,
368 IPPROTO_DTLS_1_2);
369 prepare_sock_dtls_v6(MY_IPV6_ADDR, ANY_PORT, &s_sock,
370 (struct sockaddr_in6 *)&s_saddr,
371 IPPROTO_DTLS_1_2);
372 } else {
373 prepare_sock_dtls_v4(MY_IPV4_ADDR, ANY_PORT, &c_sock,
374 (struct sockaddr_in *)&c_saddr,
375 IPPROTO_DTLS_1_2);
376 prepare_sock_dtls_v4(MY_IPV4_ADDR, ANY_PORT, &s_sock,
377 (struct sockaddr_in *)&s_saddr,
378 IPPROTO_DTLS_1_2);
379 }
380
381 test_config_psk(s_sock, c_sock);
382
383 zassert_equal(zsock_setsockopt(s_sock, SOL_TLS, TLS_DTLS_ROLE,
384 &role, sizeof(role)),
385 0, "setsockopt() failed");
386
387 test_bind(s_sock, &s_saddr, exp_addrlen);
388
389 test_data.sock = c_sock;
390 test_data.addr = &s_saddr;
391 k_work_init_delayable(&test_data.work, dtls_client_connect_send_work_handler);
392 test_work_reschedule(&test_data.work, K_NO_WAIT);
393
394 /* DTLS has no separate call like accept() to know when the handshake
395 * is complete, therefore send a dummy byte once handshake is done to
396 * unblock poll().
397 */
398 fds[0].fd = s_sock;
399 fds[0].events = ZSOCK_POLLIN;
400 ret = zsock_poll(fds, 1, 1000);
401 zassert_equal(ret, 1, "poll() did not report data ready");
402
403 /* Flush the dummy byte. */
404 ret = zsock_recv(s_sock, &rx_buf, sizeof(rx_buf), 0);
405 zassert_equal(ret, sizeof(rx_buf), "recv() failed");
406
407 test_work_wait(&test_data.work);
408 }
409
ZTEST(net_socket_tls,test_v4_msg_waitall)410 ZTEST(net_socket_tls, test_v4_msg_waitall)
411 {
412 struct test_msg_waitall_data test_data = {
413 .data = TEST_STR_SMALL,
414 };
415 int ret;
416 uint8_t rx_buf[sizeof(TEST_STR_SMALL) - 1] = { 0 };
417 struct timeval timeo_optval = {
418 .tv_sec = 0,
419 .tv_usec = 500000,
420 };
421
422 test_prepare_tls_connection(AF_INET);
423
424 /* Regular MSG_WAITALL - make sure recv returns only after
425 * requested amount is received.
426 */
427 test_data.offset = 0;
428 test_data.retries = sizeof(rx_buf);
429 test_data.sock = c_sock;
430 k_work_init_delayable(&test_data.tx_work,
431 test_msg_waitall_tx_work_handler);
432 test_work_reschedule(&test_data.tx_work, K_MSEC(10));
433
434 ret = zsock_recv(new_sock, rx_buf, sizeof(rx_buf), ZSOCK_MSG_WAITALL);
435 zassert_equal(ret, sizeof(rx_buf), "Invalid length received");
436 zassert_mem_equal(rx_buf, TEST_STR_SMALL, sizeof(rx_buf),
437 "Invalid data received");
438 test_work_wait(&test_data.tx_work);
439
440 /* MSG_WAITALL + SO_RCVTIMEO - make sure recv returns the amount of data
441 * received so far
442 */
443 ret = zsock_setsockopt(new_sock, SOL_SOCKET, SO_RCVTIMEO, &timeo_optval,
444 sizeof(timeo_optval));
445 zassert_equal(ret, 0, "setsockopt failed (%d)", errno);
446
447 memset(rx_buf, 0, sizeof(rx_buf));
448 test_data.offset = 0;
449 test_data.retries = sizeof(rx_buf) - 1;
450 test_data.sock = c_sock;
451 k_work_init_delayable(&test_data.tx_work,
452 test_msg_waitall_tx_work_handler);
453 test_work_reschedule(&test_data.tx_work, K_MSEC(10));
454
455 ret = zsock_recv(new_sock, rx_buf, sizeof(rx_buf) - 1, ZSOCK_MSG_WAITALL);
456 zassert_equal(ret, sizeof(rx_buf) - 1, "Invalid length received");
457 zassert_mem_equal(rx_buf, TEST_STR_SMALL, sizeof(rx_buf) - 1,
458 "Invalid data received");
459 test_work_wait(&test_data.tx_work);
460
461 test_sockets_close();
462
463 k_sleep(TCP_TEARDOWN_TIMEOUT);
464 }
465
ZTEST(net_socket_tls,test_v6_msg_waitall)466 ZTEST(net_socket_tls, test_v6_msg_waitall)
467 {
468 struct test_msg_waitall_data test_data = {
469 .data = TEST_STR_SMALL,
470 };
471 int ret;
472 uint8_t rx_buf[sizeof(TEST_STR_SMALL) - 1] = { 0 };
473 struct timeval timeo_optval = {
474 .tv_sec = 0,
475 .tv_usec = 500000,
476 };
477
478 test_prepare_tls_connection(AF_INET6);
479
480 /* Regular MSG_WAITALL - make sure recv returns only after
481 * requested amount is received.
482 */
483 test_data.offset = 0;
484 test_data.retries = sizeof(rx_buf);
485 test_data.sock = c_sock;
486 k_work_init_delayable(&test_data.tx_work,
487 test_msg_waitall_tx_work_handler);
488 test_work_reschedule(&test_data.tx_work, K_MSEC(10));
489
490 ret = zsock_recv(new_sock, rx_buf, sizeof(rx_buf), ZSOCK_MSG_WAITALL);
491 zassert_equal(ret, sizeof(rx_buf), "Invalid length received");
492 zassert_mem_equal(rx_buf, TEST_STR_SMALL, sizeof(rx_buf),
493 "Invalid data received");
494 test_work_wait(&test_data.tx_work);
495
496 /* MSG_WAITALL + SO_RCVTIMEO - make sure recv returns the amount of data
497 * received so far
498 */
499 ret = zsock_setsockopt(new_sock, SOL_SOCKET, SO_RCVTIMEO, &timeo_optval,
500 sizeof(timeo_optval));
501 zassert_equal(ret, 0, "setsockopt failed (%d)", errno);
502
503 memset(rx_buf, 0, sizeof(rx_buf));
504 test_data.offset = 0;
505 test_data.retries = sizeof(rx_buf) - 1;
506 test_data.sock = c_sock;
507 k_work_init_delayable(&test_data.tx_work,
508 test_msg_waitall_tx_work_handler);
509 test_work_reschedule(&test_data.tx_work, K_MSEC(10));
510
511 ret = zsock_recv(new_sock, rx_buf, sizeof(rx_buf) - 1, ZSOCK_MSG_WAITALL);
512 zassert_equal(ret, sizeof(rx_buf) - 1, "Invalid length received");
513 zassert_mem_equal(rx_buf, TEST_STR_SMALL, sizeof(rx_buf) - 1,
514 "Invalid data received");
515 test_work_wait(&test_data.tx_work);
516
517 test_sockets_close();
518
519 k_sleep(TCP_TEARDOWN_TIMEOUT);
520 }
521
522 struct send_data {
523 struct k_work_delayable tx_work;
524 int sock;
525 const uint8_t *data;
526 size_t datalen;
527 };
528
send_work_handler(struct k_work * work)529 static void send_work_handler(struct k_work *work)
530 {
531 struct k_work_delayable *dwork = k_work_delayable_from_work(work);
532 struct send_data *test_data =
533 CONTAINER_OF(dwork, struct send_data, tx_work);
534
535 test_send(test_data->sock, test_data->data, test_data->datalen, 0);
536 }
537
test_msg_trunc(sa_family_t family)538 void test_msg_trunc(sa_family_t family)
539 {
540 int rv;
541 uint8_t rx_buf[sizeof(TEST_STR_SMALL) - 1];
542 struct send_data test_data = {
543 .data = TEST_STR_SMALL,
544 .datalen = sizeof(TEST_STR_SMALL) - 1
545 };
546
547 test_prepare_dtls_connection(family);
548
549 /* MSG_TRUNC */
550
551 test_data.sock = c_sock;
552 k_work_init_delayable(&test_data.tx_work, send_work_handler);
553 test_work_reschedule(&test_data.tx_work, K_MSEC(10));
554
555 memset(rx_buf, 0, sizeof(rx_buf));
556 rv = zsock_recv(s_sock, rx_buf, 2, ZSOCK_MSG_TRUNC);
557 zassert_equal(rv, sizeof(TEST_STR_SMALL) - 1, "MSG_TRUNC flag failed");
558 zassert_mem_equal(rx_buf, TEST_STR_SMALL, 2, "invalid rx data");
559 zassert_equal(rx_buf[2], 0, "received more than requested");
560
561 /* The remaining data should've been discarded */
562 rv = zsock_recv(s_sock, rx_buf, sizeof(rx_buf), ZSOCK_MSG_DONTWAIT);
563 zassert_equal(rv, -1, "consecutive recv should've failed");
564 zassert_equal(errno, EAGAIN, "incorrect errno value");
565
566 /* MSG_PEEK not supported by DTLS socket */
567
568 test_sockets_close();
569
570 test_work_wait(&test_data.tx_work);
571
572 /* Small delay for the final alert exchange */
573 k_msleep(10);
574 }
575
ZTEST(net_socket_tls,test_v4_msg_trunc)576 ZTEST(net_socket_tls, test_v4_msg_trunc)
577 {
578 test_msg_trunc(AF_INET);
579 }
580
ZTEST(net_socket_tls,test_v6_msg_trunc)581 ZTEST(net_socket_tls, test_v6_msg_trunc)
582 {
583 test_msg_trunc(AF_INET6);
584 }
585
586 struct test_sendmsg_data {
587 struct k_work_delayable tx_work;
588 int sock;
589 const struct msghdr *msg;
590 };
591
test_sendmsg_tx_work_handler(struct k_work * work)592 static void test_sendmsg_tx_work_handler(struct k_work *work)
593 {
594 struct k_work_delayable *dwork = k_work_delayable_from_work(work);
595 struct test_sendmsg_data *test_data =
596 CONTAINER_OF(dwork, struct test_sendmsg_data, tx_work);
597
598 test_sendmsg(test_data->sock, test_data->msg, 0);
599 }
600
test_dtls_sendmsg_no_buf(sa_family_t family)601 static void test_dtls_sendmsg_no_buf(sa_family_t family)
602 {
603 int rv;
604 uint8_t rx_buf[sizeof(TEST_STR_SMALL) - 1];
605 struct iovec iov[3] = {
606 {},
607 {
608 .iov_base = TEST_STR_SMALL,
609 .iov_len = sizeof(TEST_STR_SMALL) - 1,
610 },
611 {},
612 };
613 struct msghdr msg = {};
614 struct test_sendmsg_data test_data = {
615 .msg = &msg,
616 };
617
618 test_prepare_dtls_connection(family);
619
620 test_data.sock = c_sock;
621 k_work_init_delayable(&test_data.tx_work, test_sendmsg_tx_work_handler);
622
623 /* sendmsg() with single fragment */
624
625 msg.msg_iov = &iov[1];
626 msg.msg_iovlen = 1,
627
628 test_work_reschedule(&test_data.tx_work, K_MSEC(10));
629
630 memset(rx_buf, 0, sizeof(rx_buf));
631 rv = zsock_recv(s_sock, rx_buf, sizeof(rx_buf), 0);
632 zassert_equal(rv, sizeof(TEST_STR_SMALL) - 1, "recv failed");
633 zassert_mem_equal(rx_buf, TEST_STR_SMALL, sizeof(TEST_STR_SMALL) - 1, "invalid rx data");
634
635 test_work_wait(&test_data.tx_work);
636
637 /* sendmsg() with single non-empty fragment */
638
639 msg.msg_iov = iov;
640 msg.msg_iovlen = ARRAY_SIZE(iov);
641
642 test_work_reschedule(&test_data.tx_work, K_MSEC(10));
643
644 memset(rx_buf, 0, sizeof(rx_buf));
645 rv = zsock_recv(s_sock, rx_buf, sizeof(rx_buf), 0);
646 zassert_equal(rv, sizeof(TEST_STR_SMALL) - 1, "recv failed");
647 zassert_mem_equal(rx_buf, TEST_STR_SMALL, sizeof(TEST_STR_SMALL) - 1, "invalid rx data");
648
649 test_work_wait(&test_data.tx_work);
650
651 /* sendmsg() with multiple non-empty fragments */
652
653 iov[0].iov_base = TEST_STR_SMALL;
654 iov[0].iov_len = sizeof(TEST_STR_SMALL) - 1;
655
656 rv = zsock_sendmsg(c_sock, &msg, 0);
657 zassert_equal(rv, -1, "sendmsg succeeded");
658 zassert_equal(errno, EMSGSIZE, "incorrect errno value");
659
660 test_sockets_close();
661
662 /* Small delay for the final alert exchange */
663 k_msleep(10);
664 }
665
ZTEST(net_socket_tls,test_v4_dtls_sendmsg_no_buf)666 ZTEST(net_socket_tls, test_v4_dtls_sendmsg_no_buf)
667 {
668 if (CONFIG_NET_SOCKETS_DTLS_SENDMSG_BUF_SIZE > 0) {
669 ztest_test_skip();
670 }
671
672 test_dtls_sendmsg_no_buf(AF_INET);
673 }
674
ZTEST(net_socket_tls,test_v6_dtls_sendmsg_no_buf)675 ZTEST(net_socket_tls, test_v6_dtls_sendmsg_no_buf)
676 {
677 if (CONFIG_NET_SOCKETS_DTLS_SENDMSG_BUF_SIZE > 0) {
678 ztest_test_skip();
679 }
680
681 test_dtls_sendmsg_no_buf(AF_INET6);
682 }
683
test_dtls_sendmsg(sa_family_t family)684 static void test_dtls_sendmsg(sa_family_t family)
685 {
686 int rv;
687 uint8_t buf[128 + 1] = { 0 };
688 uint8_t dummy_byte = 0;
689 static const char expected_str[] = "testtest";
690 struct iovec iov[3] = {
691 {
692 .iov_base = TEST_STR_SMALL,
693 .iov_len = sizeof(TEST_STR_SMALL) - 1,
694 },
695 {
696 .iov_base = TEST_STR_SMALL,
697 .iov_len = sizeof(TEST_STR_SMALL) - 1,
698 },
699 {},
700 };
701 struct msghdr msg = {};
702 struct test_sendmsg_data test_data = {
703 .msg = &msg,
704 };
705
706 test_prepare_dtls_connection(family);
707
708 test_data.sock = c_sock;
709 k_work_init_delayable(&test_data.tx_work, test_sendmsg_tx_work_handler);
710
711 /* sendmsg() with multiple fragments */
712
713 msg.msg_iov = iov;
714 msg.msg_iovlen = 2,
715
716 test_work_reschedule(&test_data.tx_work, K_NO_WAIT);
717
718 memset(buf, 0, sizeof(buf));
719 rv = zsock_recv(s_sock, buf, sizeof(buf), 0);
720 zassert_equal(rv, sizeof(expected_str) - 1, "recv failed");
721 zassert_mem_equal(buf, expected_str, sizeof(expected_str) - 1, "invalid rx data");
722
723 test_work_wait(&test_data.tx_work);
724
725 /* sendmsg() with multiple fragments and empty fragment inbetween */
726
727 iov[1].iov_base = NULL;
728 iov[1].iov_len = 0;
729 iov[2].iov_base = TEST_STR_SMALL;
730 iov[2].iov_len = sizeof(TEST_STR_SMALL) - 1;
731 msg.msg_iov = iov;
732 msg.msg_iovlen = 3;
733
734 test_work_reschedule(&test_data.tx_work, K_NO_WAIT);
735
736 memset(buf, 0, sizeof(buf));
737 rv = zsock_recv(s_sock, buf, sizeof(buf), 0);
738 zassert_equal(rv, sizeof(expected_str) - 1, "recv failed");
739 zassert_mem_equal(buf, expected_str, sizeof(expected_str) - 1, "invalid rx data");
740
741 test_work_wait(&test_data.tx_work);
742
743 /* sendmsg() with single fragment should still work even if larger than
744 * intermediate buffer size
745 */
746
747 memset(buf, 'a', sizeof(buf));
748 iov[0].iov_base = buf;
749 iov[0].iov_len = sizeof(buf);
750 msg.msg_iov = iov;
751 msg.msg_iovlen = 1;
752
753 test_work_reschedule(&test_data.tx_work, K_NO_WAIT);
754
755 /* We reuse the buffer, so wait to make sure the message is sent. */
756 k_msleep(10);
757
758 memset(buf, 0, sizeof(buf));
759 rv = zsock_recv(s_sock, buf, sizeof(buf), 0);
760 zassert_equal(rv, sizeof(buf), "recv failed");
761 for (int i = 0; i < sizeof(buf); i++) {
762 zassert_equal(buf[i], 'a', "invalid rx data");
763 }
764
765 test_work_wait(&test_data.tx_work);
766
767 /* sendmsg() exceeding intermediate buf size */
768
769 iov[0].iov_base = buf;
770 iov[0].iov_len = sizeof(buf);
771 iov[1].iov_base = &dummy_byte;
772 iov[1].iov_len = sizeof(dummy_byte);
773 msg.msg_iov = iov;
774 msg.msg_iovlen = 2;
775
776 rv = zsock_sendmsg(c_sock, &msg, 0);
777 zassert_equal(rv, -1, "sendmsg succeeded");
778 zassert_equal(errno, EMSGSIZE, "incorrect errno value");
779
780 test_sockets_close();
781
782 /* Small delay for the final alert exchange */
783 k_msleep(10);
784 }
785
ZTEST(net_socket_tls,test_v4_dtls_sendmsg)786 ZTEST(net_socket_tls, test_v4_dtls_sendmsg)
787 {
788 if (CONFIG_NET_SOCKETS_DTLS_SENDMSG_BUF_SIZE == 0) {
789 ztest_test_skip();
790 }
791
792 test_dtls_sendmsg(AF_INET);
793 }
794
ZTEST(net_socket_tls,test_v6_dtls_sendmsg)795 ZTEST(net_socket_tls, test_v6_dtls_sendmsg)
796 {
797 if (CONFIG_NET_SOCKETS_DTLS_SENDMSG_BUF_SIZE == 0) {
798 ztest_test_skip();
799 }
800
801 test_dtls_sendmsg(AF_INET6);
802 }
803
804 struct close_data {
805 struct k_work_delayable work;
806 int *fd;
807 };
808
close_work(struct k_work * work)809 static void close_work(struct k_work *work)
810 {
811 struct k_work_delayable *dwork = k_work_delayable_from_work(work);
812 struct close_data *data = CONTAINER_OF(dwork, struct close_data, work);
813
814 zsock_close(*data->fd);
815 *data->fd = -1;
816 }
817
ZTEST(net_socket_tls,test_close_while_accept)818 ZTEST(net_socket_tls, test_close_while_accept)
819 {
820 struct sockaddr_in6 s_saddr;
821 struct sockaddr addr;
822 socklen_t addrlen = sizeof(addr);
823 struct close_data close_work_data;
824
825 prepare_sock_tls_v6(MY_IPV6_ADDR, ANY_PORT, &s_sock, &s_saddr, IPPROTO_TLS_1_2);
826
827 test_config_psk(s_sock, -1);
828
829 test_bind(s_sock, (struct sockaddr *)&s_saddr, sizeof(s_saddr));
830 test_listen(s_sock);
831
832 /* Schedule close() from workqueue */
833 k_work_init_delayable(&close_work_data.work, close_work);
834 close_work_data.fd = &s_sock;
835 test_work_reschedule(&close_work_data.work, K_MSEC(10));
836
837 /* Start blocking accept(), which should be unblocked by close() from
838 * another thread and return an error.
839 */
840 new_sock = zsock_accept(s_sock, &addr, &addrlen);
841 zassert_equal(new_sock, -1, "accept did not return error");
842 zassert_equal(errno, EINTR, "Unexpected errno value: %d", errno);
843
844 test_work_wait(&close_work_data.work);
845 k_sleep(TCP_TEARDOWN_TIMEOUT);
846 }
847
ZTEST(net_socket_tls,test_close_while_recv)848 ZTEST(net_socket_tls, test_close_while_recv)
849 {
850 int ret;
851 struct close_data close_work_data;
852 uint8_t rx_buf;
853
854 test_prepare_tls_connection(AF_INET6);
855
856 /* Schedule close() from workqueue */
857 k_work_init_delayable(&close_work_data.work, close_work);
858 close_work_data.fd = &new_sock;
859 test_work_reschedule(&close_work_data.work, K_MSEC(10));
860
861 ret = zsock_recv(new_sock, &rx_buf, sizeof(rx_buf), 0);
862 zassert_equal(ret, -1, "recv did not return error");
863 zassert_equal(errno, EINTR, "Unexpected errno value: %d", errno);
864
865 test_work_wait(&close_work_data.work);
866
867 test_sockets_close();
868
869 k_sleep(TCP_TEARDOWN_TIMEOUT);
870 }
871
ZTEST(net_socket_tls,test_connect_timeout)872 ZTEST(net_socket_tls, test_connect_timeout)
873 {
874 struct sockaddr_in6 c_saddr;
875 struct sockaddr_in6 s_saddr;
876 int ret;
877
878 prepare_sock_tls_v6(MY_IPV6_ADDR, ANY_PORT, &c_sock, &c_saddr,
879 IPPROTO_TLS_1_2);
880 test_config_psk(-1, c_sock);
881
882 s_saddr.sin6_family = AF_INET6;
883 s_saddr.sin6_port = htons(SERVER_PORT);
884 ret = zsock_inet_pton(AF_INET6, MY_IPV6_ADDR, &s_saddr.sin6_addr);
885 zassert_equal(ret, 1, "inet_pton failed");
886
887 loopback_set_packet_drop_ratio(1.0f);
888
889 zassert_equal(zsock_connect(c_sock, (struct sockaddr *)&s_saddr,
890 sizeof(s_saddr)),
891 -1, "connect succeed");
892 zassert_equal(errno, ETIMEDOUT,
893 "connect should be timed out, got %d", errno);
894
895 test_sockets_close();
896
897 loopback_set_packet_drop_ratio(0.0f);
898 k_sleep(TCP_TEARDOWN_TIMEOUT);
899 }
900
ZTEST(net_socket_tls,test_connect_closed_port)901 ZTEST(net_socket_tls, test_connect_closed_port)
902 {
903 struct sockaddr_in6 c_saddr;
904 struct sockaddr_in6 s_saddr;
905 int ret;
906
907 prepare_sock_tls_v6(MY_IPV6_ADDR, ANY_PORT, &c_sock, &c_saddr,
908 IPPROTO_TLS_1_2);
909 test_config_psk(-1, c_sock);
910
911 s_saddr.sin6_family = AF_INET6;
912 s_saddr.sin6_port = htons(SERVER_PORT);
913 ret = zsock_inet_pton(AF_INET6, MY_IPV6_ADDR, &s_saddr.sin6_addr);
914 zassert_equal(ret, 1, "inet_pton failed");
915
916 zassert_equal(zsock_connect(c_sock, (struct sockaddr *)&s_saddr,
917 sizeof(s_saddr)),
918 -1, "connect succeed");
919 zassert_equal(errno, ETIMEDOUT,
920 "connect should fail, got %d", errno);
921
922 test_sockets_close();
923 k_sleep(TCP_TEARDOWN_TIMEOUT);
924 }
925
926 struct fake_tcp_server_data {
927 struct k_work_delayable work;
928 int sock;
929 bool reply;
930 };
931
fake_tcp_server_work(struct k_work * work)932 static void fake_tcp_server_work(struct k_work *work)
933 {
934 struct k_work_delayable *dwork = k_work_delayable_from_work(work);
935 struct fake_tcp_server_data *data =
936 CONTAINER_OF(dwork, struct fake_tcp_server_data, work);
937
938 test_accept(data->sock, &new_sock, NULL, 0);
939
940 if (!data->reply) {
941 /* Add small delay to avoid race between incoming data and
942 * sending FIN.
943 */
944 k_msleep(10);
945 goto out;
946 }
947
948 while (true) {
949 int ret;
950 char rx_buf[32];
951
952 ret = zsock_recv(new_sock, rx_buf, sizeof(rx_buf), 0);
953 if (ret <= 0) {
954 break;
955 }
956
957 (void)zsock_send(new_sock, TEST_STR_SMALL, sizeof(TEST_STR_SMALL), 0);
958 }
959
960 out:
961 test_close(new_sock);
962 new_sock = -1;
963 }
964
test_prepare_fake_tcp_server(struct fake_tcp_server_data * s_data,sa_family_t family,int * s_sock,struct sockaddr * s_saddr,bool reply)965 static void test_prepare_fake_tcp_server(struct fake_tcp_server_data *s_data,
966 sa_family_t family, int *s_sock,
967 struct sockaddr *s_saddr, bool reply)
968 {
969 socklen_t exp_addrlen = family == AF_INET6 ?
970 sizeof(struct sockaddr_in6) :
971 sizeof(struct sockaddr_in);
972
973 if (family == AF_INET6) {
974 prepare_sock_tcp_v6(MY_IPV6_ADDR, SERVER_PORT, s_sock,
975 (struct sockaddr_in6 *)s_saddr);
976 } else {
977 prepare_sock_tcp_v4(MY_IPV4_ADDR, SERVER_PORT, s_sock,
978 (struct sockaddr_in *)s_saddr);
979 }
980
981 test_bind(*s_sock, s_saddr, exp_addrlen);
982 test_listen(*s_sock);
983
984 s_data->sock = *s_sock;
985 s_data->reply = reply;
986 k_work_init_delayable(&s_data->work, fake_tcp_server_work);
987 test_work_reschedule(&s_data->work, K_NO_WAIT);
988 }
989
ZTEST(net_socket_tls,test_connect_invalid_handshake_data)990 ZTEST(net_socket_tls, test_connect_invalid_handshake_data)
991 {
992 struct fake_tcp_server_data server_data;
993 struct sockaddr_in6 c_saddr;
994 struct sockaddr_in6 s_saddr;
995
996 prepare_sock_tls_v6(MY_IPV6_ADDR, ANY_PORT, &c_sock, &c_saddr,
997 IPPROTO_TLS_1_2);
998 test_config_psk(-1, c_sock);
999 test_prepare_fake_tcp_server(&server_data, AF_INET6, &s_sock,
1000 (struct sockaddr *)&s_saddr, true);
1001
1002 zassert_equal(zsock_connect(c_sock, (struct sockaddr *)&s_saddr,
1003 sizeof(s_saddr)),
1004 -1, "connect succeed");
1005 zassert_equal(errno, ECONNABORTED,
1006 "connect should fail, got %d", errno);
1007
1008 test_close(c_sock);
1009 c_sock = -1;
1010
1011 test_work_wait(&server_data.work);
1012 test_sockets_close();
1013
1014 k_sleep(TCP_TEARDOWN_TIMEOUT);
1015 }
1016
ZTEST(net_socket_tls,test_connect_no_handshake_data)1017 ZTEST(net_socket_tls, test_connect_no_handshake_data)
1018 {
1019 struct fake_tcp_server_data server_data;
1020 struct sockaddr_in6 c_saddr;
1021 struct sockaddr s_saddr;
1022
1023 prepare_sock_tls_v6(MY_IPV6_ADDR, ANY_PORT, &c_sock, &c_saddr,
1024 IPPROTO_TLS_1_2);
1025 test_config_psk(-1, c_sock);
1026 test_prepare_fake_tcp_server(&server_data, AF_INET6, &s_sock,
1027 (struct sockaddr *)&s_saddr, false);
1028
1029 zassert_equal(zsock_connect(c_sock, (struct sockaddr *)&s_saddr,
1030 sizeof(s_saddr)),
1031 -1, "connect succeed");
1032 zassert_equal(errno, ECONNABORTED,
1033 "connect should fail, got %d", errno);
1034
1035 test_work_wait(&server_data.work);
1036 test_sockets_close();
1037
1038 k_sleep(TCP_TEARDOWN_TIMEOUT);
1039 }
1040
ZTEST(net_socket_tls,test_accept_non_block)1041 ZTEST(net_socket_tls, test_accept_non_block)
1042 {
1043 uint32_t timestamp;
1044 struct sockaddr_in6 s_saddr;
1045
1046 prepare_sock_tls_v6(MY_IPV6_ADDR, SERVER_PORT, &s_sock, &s_saddr,
1047 IPPROTO_TLS_1_2);
1048
1049 test_config_psk(s_sock, -1);
1050 test_fcntl(s_sock, F_SETFL, O_NONBLOCK);
1051 test_bind(s_sock, (struct sockaddr *)&s_saddr, sizeof(s_saddr));
1052 test_listen(s_sock);
1053
1054 timestamp = k_uptime_get_32();
1055 new_sock = zsock_accept(s_sock, NULL, NULL);
1056 zassert_true(k_uptime_get_32() - timestamp <= 100, "");
1057 zassert_equal(new_sock, -1, "accept did not return error");
1058 zassert_equal(errno, EAGAIN, "Unexpected errno value: %d", errno);
1059
1060 test_sockets_close();
1061 k_sleep(TCP_TEARDOWN_TIMEOUT);
1062 }
1063
ZTEST(net_socket_tls,test_accept_invalid_handshake_data)1064 ZTEST(net_socket_tls, test_accept_invalid_handshake_data)
1065 {
1066 struct sockaddr_in6 s_saddr;
1067 struct sockaddr_in6 c_saddr;
1068
1069 prepare_sock_tls_v6(MY_IPV6_ADDR, ANY_PORT, &s_sock, &s_saddr,
1070 IPPROTO_TLS_1_2);
1071 prepare_sock_tcp_v6(MY_IPV6_ADDR, ANY_PORT, &c_sock, &c_saddr);
1072
1073 test_config_psk(s_sock, -1);
1074 test_bind(s_sock, (struct sockaddr *)&s_saddr, sizeof(s_saddr));
1075 test_listen(s_sock);
1076
1077 /* Connect at TCP level and send some unexpected data. */
1078 test_connect(c_sock, (struct sockaddr *)&s_saddr, sizeof(s_saddr));
1079 test_send(c_sock, TEST_STR_SMALL, sizeof(TEST_STR_SMALL), 0);
1080
1081 new_sock = zsock_accept(s_sock, NULL, NULL);
1082 zassert_equal(new_sock, -1, "accept did not return error");
1083 zassert_equal(errno, ECONNABORTED, "Unexpected errno value: %d", errno);
1084
1085 test_sockets_close();
1086 k_sleep(TCP_TEARDOWN_TIMEOUT);
1087 }
1088
ZTEST(net_socket_tls,test_recv_non_block)1089 ZTEST(net_socket_tls, test_recv_non_block)
1090 {
1091 int ret;
1092 uint8_t rx_buf[sizeof(TEST_STR_SMALL) - 1] = { 0 };
1093
1094 test_prepare_tls_connection(AF_INET6);
1095
1096 /* Verify ZSOCK_MSG_DONTWAIT flag first */
1097 ret = zsock_recv(new_sock, rx_buf, sizeof(rx_buf), ZSOCK_MSG_DONTWAIT);
1098 zassert_equal(ret, -1, "recv()) should've failed");
1099 zassert_equal(errno, EAGAIN, "Unexpected errno value: %d", errno);
1100
1101 /* Verify fcntl and O_NONBLOCK */
1102 test_fcntl(new_sock, F_SETFL, O_NONBLOCK);
1103 ret = zsock_recv(new_sock, rx_buf, sizeof(rx_buf), 0);
1104 zassert_equal(ret, -1, "recv() should've failed");
1105 zassert_equal(errno, EAGAIN, "Unexpected errno value: %d", errno);
1106
1107 ret = zsock_send(c_sock, TEST_STR_SMALL, strlen(TEST_STR_SMALL), 0);
1108 zassert_equal(ret, strlen(TEST_STR_SMALL), "send() failed");
1109
1110 /* Let the data got through. */
1111 k_sleep(K_MSEC(10));
1112
1113 /* Should get data now */
1114 ret = zsock_recv(new_sock, rx_buf, sizeof(rx_buf), 0);
1115 zassert_equal(ret, strlen(TEST_STR_SMALL), "recv() failed");
1116 zassert_mem_equal(rx_buf, TEST_STR_SMALL, ret, "Invalid data received");
1117
1118 /* And EAGAIN on consecutive read */
1119 ret = zsock_recv(new_sock, rx_buf, sizeof(rx_buf), 0);
1120 zassert_equal(ret, -1, "recv() should've failed");
1121 zassert_equal(errno, EAGAIN, "Unexpected errno value: %d", errno);
1122
1123 test_sockets_close();
1124
1125 k_sleep(TCP_TEARDOWN_TIMEOUT);
1126 }
1127
ZTEST(net_socket_tls,test_recv_block)1128 ZTEST(net_socket_tls, test_recv_block)
1129 {
1130 int ret;
1131 uint8_t rx_buf[sizeof(TEST_STR_SMALL) - 1] = { 0 };
1132 struct send_data test_data = {
1133 .data = TEST_STR_SMALL,
1134 .datalen = sizeof(TEST_STR_SMALL) - 1
1135 };
1136
1137 test_prepare_tls_connection(AF_INET6);
1138
1139 test_data.sock = c_sock;
1140 k_work_init_delayable(&test_data.tx_work, send_work_handler);
1141 test_work_reschedule(&test_data.tx_work, K_MSEC(10));
1142
1143 /* recv() shall block until send work sends the data. */
1144 ret = zsock_recv(new_sock, rx_buf, sizeof(rx_buf), 0);
1145 zassert_equal(ret, sizeof(TEST_STR_SMALL) - 1, "recv() failed");
1146 zassert_mem_equal(rx_buf, TEST_STR_SMALL, ret, "Invalid data received");
1147
1148 test_sockets_close();
1149
1150 k_sleep(TCP_TEARDOWN_TIMEOUT);
1151 }
1152
ZTEST(net_socket_tls,test_recv_eof_on_close)1153 ZTEST(net_socket_tls, test_recv_eof_on_close)
1154 {
1155 test_prepare_tls_connection(AF_INET6);
1156
1157 test_close(c_sock);
1158 c_sock = -1;
1159
1160 /* Verify recv() reports EOF */
1161 test_eof(new_sock);
1162
1163 test_sockets_close();
1164
1165 k_sleep(TCP_TEARDOWN_TIMEOUT);
1166 }
1167
1168 #define TLS_RECORD_OVERHEAD 81
1169
ZTEST(net_socket_tls,test_send_non_block)1170 ZTEST(net_socket_tls, test_send_non_block)
1171 {
1172 int ret;
1173 uint8_t rx_buf[sizeof(TEST_STR_SMALL) - 1] = { 0 };
1174 int buf_optval = TLS_RECORD_OVERHEAD + sizeof(TEST_STR_SMALL) - 1;
1175
1176 test_prepare_tls_connection(AF_INET6);
1177
1178 /* Simulate window full scenario with SO_RCVBUF option. */
1179 ret = zsock_setsockopt(new_sock, SOL_SOCKET, SO_RCVBUF, &buf_optval,
1180 sizeof(buf_optval));
1181 zassert_equal(ret, 0, "setsockopt failed (%d)", errno);
1182
1183 /* Fill out the window */
1184 ret = zsock_send(c_sock, TEST_STR_SMALL, strlen(TEST_STR_SMALL), 0);
1185 zassert_equal(ret, strlen(TEST_STR_SMALL), "send() failed");
1186
1187 /* Wait for ACK (empty window, min. 100 ms due to silly window
1188 * protection).
1189 */
1190 k_sleep(K_MSEC(150));
1191
1192 /* Verify ZSOCK_MSG_DONTWAIT flag first */
1193 ret = zsock_send(c_sock, TEST_STR_SMALL, strlen(TEST_STR_SMALL),
1194 ZSOCK_MSG_DONTWAIT);
1195 zassert_equal(ret, -1, "send() should've failed");
1196 zassert_equal(errno, EAGAIN, "Unexpected errno value: %d", errno);
1197
1198 /* Verify fcntl and O_NONBLOCK */
1199 test_fcntl(c_sock, F_SETFL, O_NONBLOCK);
1200 ret = zsock_send(c_sock, TEST_STR_SMALL, strlen(TEST_STR_SMALL), 0);
1201 zassert_equal(ret, -1, "send() should've failed");
1202 zassert_equal(errno, EAGAIN, "Unexpected errno value: %d", errno);
1203
1204 ret = zsock_recv(new_sock, rx_buf, sizeof(rx_buf), 0);
1205 zassert_equal(ret, strlen(TEST_STR_SMALL), "recv() failed");
1206 zassert_mem_equal(rx_buf, TEST_STR_SMALL, ret, "Invalid data received");
1207
1208 /* Wait for the window to update. */
1209 k_sleep(K_MSEC(10));
1210
1211 /* Should succeed now. */
1212 ret = zsock_send(c_sock, TEST_STR_SMALL, strlen(TEST_STR_SMALL), 0);
1213 zassert_equal(ret, strlen(TEST_STR_SMALL), "send() failed");
1214
1215 /* Flush the data */
1216 ret = zsock_recv(new_sock, rx_buf, sizeof(rx_buf), 0);
1217 zassert_equal(ret, strlen(TEST_STR_SMALL), "recv() failed");
1218 zassert_mem_equal(rx_buf, TEST_STR_SMALL, ret, "Invalid data received");
1219
1220 /* And make sure there's no more data left. */
1221 ret = zsock_recv(new_sock, rx_buf, sizeof(rx_buf), ZSOCK_MSG_DONTWAIT);
1222 zassert_equal(ret, -1, "recv() should've failed");
1223 zassert_equal(errno, EAGAIN, "Unexpected errno value: %d", errno);
1224
1225 test_sockets_close();
1226
1227 k_sleep(TCP_TEARDOWN_TIMEOUT);
1228 }
1229
1230 struct recv_data {
1231 struct k_work_delayable work;
1232 int sock;
1233 const uint8_t *data;
1234 size_t datalen;
1235 };
1236
recv_work_handler(struct k_work * work)1237 static void recv_work_handler(struct k_work *work)
1238 {
1239 struct k_work_delayable *dwork = k_work_delayable_from_work(work);
1240 struct recv_data *test_data = CONTAINER_OF(dwork, struct recv_data, work);
1241 char rx_buf[30] = { 0 };
1242 size_t off = 0;
1243 int ret;
1244
1245 while (off < test_data->datalen) {
1246 size_t recvlen = MIN(sizeof(rx_buf), test_data->datalen - off);
1247
1248 ret = zsock_recv(test_data->sock, rx_buf, recvlen, 0);
1249 zassert_true(ret > 0, "recv() error");
1250 zassert_mem_equal(rx_buf, test_data->data + off, ret,
1251 "unexpected data");
1252
1253 off += ret;
1254 zassert_true(off <= test_data->datalen,
1255 "received more than expected");
1256 }
1257 }
1258
ZTEST(net_socket_tls,test_send_block)1259 ZTEST(net_socket_tls, test_send_block)
1260 {
1261 int ret;
1262 int buf_optval = TLS_RECORD_OVERHEAD + sizeof(TEST_STR_SMALL) - 1;
1263 uint8_t rx_buf[sizeof(TEST_STR_SMALL) - 1] = { 0 };
1264 struct recv_data test_data = {
1265 .data = TEST_STR_SMALL,
1266 .datalen = sizeof(TEST_STR_SMALL) - 1
1267 };
1268
1269 test_prepare_tls_connection(AF_INET6);
1270
1271 /* Simulate window full scenario with SO_RCVBUF option. */
1272 ret = zsock_setsockopt(new_sock, SOL_SOCKET, SO_RCVBUF, &buf_optval,
1273 sizeof(buf_optval));
1274 zassert_equal(ret, 0, "setsockopt failed (%d)", errno);
1275
1276 /* Fill out the window */
1277 ret = zsock_send(c_sock, TEST_STR_SMALL, strlen(TEST_STR_SMALL), 0);
1278 zassert_equal(ret, strlen(TEST_STR_SMALL), "send() failed");
1279
1280 /* Wait for ACK (empty window, min. 100 ms due to silly window
1281 * protection).
1282 */
1283 k_sleep(K_MSEC(150));
1284
1285 test_data.sock = new_sock;
1286 k_work_init_delayable(&test_data.work, recv_work_handler);
1287 test_work_reschedule(&test_data.work, K_MSEC(10));
1288
1289 /* Should block and succeed. */
1290 ret = zsock_send(c_sock, TEST_STR_SMALL, strlen(TEST_STR_SMALL), 0);
1291 zassert_equal(ret, strlen(TEST_STR_SMALL), "send() failed");
1292
1293 /* Flush the data */
1294 ret = zsock_recv(new_sock, rx_buf, sizeof(rx_buf), 0);
1295 zassert_equal(ret, strlen(TEST_STR_SMALL), "recv() failed");
1296 zassert_mem_equal(rx_buf, TEST_STR_SMALL, ret, "Invalid data received");
1297
1298 /* And make sure there's no more data left. */
1299 ret = zsock_recv(new_sock, rx_buf, sizeof(rx_buf), ZSOCK_MSG_DONTWAIT);
1300 zassert_equal(ret, -1, "recv() should've failed");
1301 zassert_equal(errno, EAGAIN, "Unexpected errno value: %d", errno);
1302
1303 test_sockets_close();
1304
1305 k_sleep(TCP_TEARDOWN_TIMEOUT);
1306 }
1307
ZTEST(net_socket_tls,test_send_on_close)1308 ZTEST(net_socket_tls, test_send_on_close)
1309 {
1310 uint8_t rx_buf[sizeof(TEST_STR_SMALL) - 1] = { 0 };
1311 int ret;
1312
1313 test_prepare_tls_connection(AF_INET6);
1314
1315 test_close(new_sock);
1316 new_sock = -1;
1317
1318 /* Small delay for packets to propagate. */
1319 k_msleep(10);
1320
1321 /* Verify send() reports an error after connection is closed. */
1322 ret = zsock_send(c_sock, TEST_STR_SMALL, strlen(TEST_STR_SMALL), 0);
1323 zassert_equal(ret, -1, "send() should've failed");
1324 zassert_equal(errno, ECONNABORTED, "Unexpected errno value: %d", errno);
1325
1326 /* recv() on closed connection marked error on a socket. */
1327 ret = zsock_recv(c_sock, rx_buf, sizeof(rx_buf), 0);
1328 zassert_equal(ret, -1, "recv() should've failed");
1329 zassert_equal(errno, ECONNABORTED, "Unexpected errno value: %d", errno);
1330
1331 test_sockets_close();
1332
1333 /* And in reverse order */
1334
1335 test_prepare_tls_connection(AF_INET6);
1336
1337 test_close(new_sock);
1338 new_sock = -1;
1339
1340 /* Small delay for packets to propagate. */
1341 k_msleep(10);
1342
1343 /* Graceful connection close should be reported first. */
1344 ret = zsock_recv(c_sock, rx_buf, sizeof(rx_buf), 0);
1345 zassert_equal(ret, 0, "recv() should've reported connection close");
1346
1347 /* And consecutive send() should fail. */
1348 ret = zsock_send(c_sock, TEST_STR_SMALL, strlen(TEST_STR_SMALL), 0);
1349 zassert_equal(ret, -1, "send() should've failed");
1350 zassert_equal(errno, ECONNABORTED, "Unexpected errno value: %d", errno);
1351
1352 test_sockets_close();
1353
1354 k_sleep(TCP_TEARDOWN_TIMEOUT);
1355 }
1356
ZTEST(net_socket_tls,test_so_rcvtimeo)1357 ZTEST(net_socket_tls, test_so_rcvtimeo)
1358 {
1359 uint8_t rx_buf[sizeof(TEST_STR_SMALL) - 1];
1360 uint32_t start_time, time_diff;
1361 struct timeval optval = {
1362 .tv_sec = 0,
1363 .tv_usec = 500000,
1364 };
1365 struct send_data test_data = {
1366 .data = TEST_STR_SMALL,
1367 .datalen = sizeof(TEST_STR_SMALL) - 1
1368 };
1369 int ret;
1370
1371 test_prepare_tls_connection(AF_INET6);
1372
1373 ret = zsock_setsockopt(c_sock, SOL_SOCKET, SO_RCVTIMEO, &optval,
1374 sizeof(optval));
1375 zassert_equal(ret, 0, "setsockopt failed (%d)", errno);
1376
1377 start_time = k_uptime_get_32();
1378 ret = zsock_recv(c_sock, rx_buf, sizeof(rx_buf), 0);
1379 time_diff = k_uptime_get_32() - start_time;
1380
1381 zassert_equal(ret, -1, "recv() should've failed");
1382 zassert_equal(errno, EAGAIN, "Unexpected errno value: %d", errno);
1383 zassert_true(time_diff >= 500, "Expected timeout after 500ms but "
1384 "was %dms", time_diff);
1385
1386 test_data.sock = c_sock;
1387 k_work_init_delayable(&test_data.tx_work, send_work_handler);
1388 test_work_reschedule(&test_data.tx_work, K_MSEC(10));
1389
1390 /* recv() shall return as soon as it gets data, regardless of timeout. */
1391 ret = zsock_recv(new_sock, rx_buf, sizeof(rx_buf), 0);
1392 zassert_equal(ret, sizeof(TEST_STR_SMALL) - 1, "recv() failed");
1393 zassert_mem_equal(rx_buf, TEST_STR_SMALL, ret, "Invalid data received");
1394
1395 test_sockets_close();
1396
1397 k_sleep(TCP_TEARDOWN_TIMEOUT);
1398 }
1399
ZTEST(net_socket_tls,test_so_sndtimeo)1400 ZTEST(net_socket_tls, test_so_sndtimeo)
1401 {
1402 int buf_optval = TLS_RECORD_OVERHEAD + sizeof(TEST_STR_SMALL) - 1;
1403 uint32_t start_time, time_diff;
1404 struct timeval timeo_optval = {
1405 .tv_sec = 0,
1406 .tv_usec = 500000,
1407 };
1408 struct recv_data test_data = {
1409 .data = TEST_STR_SMALL,
1410 .datalen = sizeof(TEST_STR_SMALL) - 1
1411 };
1412 int ret;
1413
1414 test_prepare_tls_connection(AF_INET6);
1415
1416 ret = zsock_setsockopt(c_sock, SOL_SOCKET, SO_SNDTIMEO, &timeo_optval,
1417 sizeof(timeo_optval));
1418 zassert_equal(ret, 0, "setsockopt failed (%d)", errno);
1419
1420 /* Simulate window full scenario with SO_RCVBUF option. */
1421 ret = zsock_setsockopt(new_sock, SOL_SOCKET, SO_RCVBUF, &buf_optval,
1422 sizeof(buf_optval));
1423 zassert_equal(ret, 0, "setsockopt failed (%d)", errno);
1424
1425 ret = zsock_send(c_sock, TEST_STR_SMALL, sizeof(TEST_STR_SMALL) - 1, 0);
1426 zassert_equal(ret, sizeof(TEST_STR_SMALL) - 1, "send() failed");
1427
1428 /* Wait for ACK (empty window). */
1429 k_msleep(150);
1430
1431 /* Client should not be able to send now and time out after SO_SNDTIMEO */
1432 start_time = k_uptime_get_32();
1433 ret = zsock_send(c_sock, TEST_STR_SMALL, sizeof(TEST_STR_SMALL) - 1, 0);
1434 time_diff = k_uptime_get_32() - start_time;
1435
1436 zassert_equal(ret, -1, "send() should've failed");
1437 zassert_equal(errno, EAGAIN, "Unexpected errno value: %d", errno);
1438 zassert_true(time_diff >= 500, "Expected timeout after 500ms but "
1439 "was %dms", time_diff);
1440
1441 test_data.sock = new_sock;
1442 k_work_init_delayable(&test_data.work, recv_work_handler);
1443 test_work_reschedule(&test_data.work, K_MSEC(10));
1444
1445 /* Should block and succeed. */
1446 ret = zsock_send(c_sock, TEST_STR_SMALL, strlen(TEST_STR_SMALL), 0);
1447 zassert_equal(ret, strlen(TEST_STR_SMALL), "send() failed");
1448
1449 test_sockets_close();
1450
1451 k_sleep(TCP_TEARDOWN_TIMEOUT);
1452 }
1453
ZTEST(net_socket_tls,test_shutdown_rd_synchronous)1454 ZTEST(net_socket_tls, test_shutdown_rd_synchronous)
1455 {
1456 test_prepare_tls_connection(AF_INET6);
1457
1458 /* Shutdown reception */
1459 test_shutdown(c_sock, ZSOCK_SHUT_RD);
1460
1461 /* EOF should be notified by recv() */
1462 test_eof(c_sock);
1463
1464 test_sockets_close();
1465
1466 k_sleep(TCP_TEARDOWN_TIMEOUT);
1467 }
1468
1469 struct shutdown_data {
1470 struct k_work_delayable work;
1471 int sock;
1472 int how;
1473 };
1474
shutdown_work(struct k_work * work)1475 static void shutdown_work(struct k_work *work)
1476 {
1477 struct k_work_delayable *dwork = k_work_delayable_from_work(work);
1478 struct shutdown_data *data = CONTAINER_OF(dwork, struct shutdown_data,
1479 work);
1480
1481 zsock_shutdown(data->sock, data->how);
1482 }
1483
ZTEST(net_socket_tls,test_shutdown_rd_while_recv)1484 ZTEST(net_socket_tls, test_shutdown_rd_while_recv)
1485 {
1486 struct shutdown_data test_data = {
1487 .how = ZSOCK_SHUT_RD,
1488 };
1489
1490 test_prepare_tls_connection(AF_INET6);
1491
1492 /* Schedule reception shutdown from workqueue */
1493 k_work_init_delayable(&test_data.work, shutdown_work);
1494 test_data.sock = c_sock;
1495 test_work_reschedule(&test_data.work, K_MSEC(10));
1496
1497 /* EOF should be notified by recv() */
1498 test_eof(c_sock);
1499
1500 test_sockets_close();
1501
1502 k_sleep(TCP_TEARDOWN_TIMEOUT);
1503 }
1504
ZTEST(net_socket_tls,test_send_while_recv)1505 ZTEST(net_socket_tls, test_send_while_recv)
1506 {
1507 uint8_t rx_buf[sizeof(TEST_STR_SMALL) - 1];
1508 struct send_data test_data_c = {
1509 .data = TEST_STR_SMALL,
1510 .datalen = sizeof(TEST_STR_SMALL) - 1
1511 };
1512 struct send_data test_data_s = {
1513 .data = TEST_STR_SMALL,
1514 .datalen = sizeof(TEST_STR_SMALL) - 1
1515 };
1516 int ret;
1517
1518 test_prepare_tls_connection(AF_INET6);
1519
1520 test_data_c.sock = c_sock;
1521 k_work_init_delayable(&test_data_c.tx_work, send_work_handler);
1522 test_work_reschedule(&test_data_c.tx_work, K_MSEC(10));
1523
1524 test_data_s.sock = new_sock;
1525 k_work_init_delayable(&test_data_s.tx_work, send_work_handler);
1526 test_work_reschedule(&test_data_s.tx_work, K_MSEC(20));
1527
1528 /* recv() shall block until the second work is executed. The second work
1529 * will execute only if the first one won't block.
1530 */
1531 ret = zsock_recv(c_sock, rx_buf, sizeof(rx_buf), 0);
1532 zassert_equal(ret, sizeof(TEST_STR_SMALL) - 1, "recv() failed");
1533 zassert_mem_equal(rx_buf, TEST_STR_SMALL, ret, "Invalid data received");
1534
1535 /* Check if the server sock got its data. */
1536 ret = zsock_recv(new_sock, rx_buf, sizeof(rx_buf), 0);
1537 zassert_equal(ret, sizeof(TEST_STR_SMALL) - 1, "recv() failed");
1538 zassert_mem_equal(rx_buf, TEST_STR_SMALL, ret, "Invalid data received");
1539
1540 test_sockets_close();
1541
1542 k_sleep(TCP_TEARDOWN_TIMEOUT);
1543 }
1544
ZTEST(net_socket_tls,test_poll_tls_pollin)1545 ZTEST(net_socket_tls, test_poll_tls_pollin)
1546 {
1547 uint8_t rx_buf[sizeof(TEST_STR_SMALL) - 1];
1548 int ret;
1549 struct zsock_pollfd fds[1];
1550
1551 test_prepare_tls_connection(AF_INET6);
1552
1553 fds[0].fd = new_sock;
1554 fds[0].events = ZSOCK_POLLIN;
1555
1556 ret = zsock_poll(fds, 1, 0);
1557 zassert_equal(ret, 0, "Unexpected poll() event");
1558
1559 ret = zsock_send(c_sock, TEST_STR_SMALL, sizeof(TEST_STR_SMALL) - 1, 0);
1560 zassert_equal(ret, strlen(TEST_STR_SMALL), "send() failed");
1561
1562 ret = zsock_poll(fds, 1, 100);
1563 zassert_equal(ret, 1, "poll() should've report event");
1564 zassert_equal(fds[0].revents, ZSOCK_POLLIN, "No POLLIN event");
1565
1566 /* Check that data is really available */
1567 ret = zsock_recv(new_sock, rx_buf, sizeof(rx_buf), ZSOCK_MSG_DONTWAIT);
1568 zassert_equal(ret, sizeof(TEST_STR_SMALL) - 1, "recv() failed");
1569 zassert_mem_equal(rx_buf, TEST_STR_SMALL, ret, "Invalid data received");
1570
1571 test_sockets_close();
1572
1573 k_sleep(TCP_TEARDOWN_TIMEOUT);
1574 }
1575
ZTEST(net_socket_tls,test_poll_dtls_pollin)1576 ZTEST(net_socket_tls, test_poll_dtls_pollin)
1577 {
1578 uint8_t rx_buf[sizeof(TEST_STR_SMALL) - 1];
1579 struct send_data test_data = {
1580 .data = TEST_STR_SMALL,
1581 .datalen = sizeof(TEST_STR_SMALL) - 1
1582 };
1583 int ret;
1584 struct zsock_pollfd fds[1];
1585
1586 test_prepare_dtls_connection(AF_INET6);
1587
1588 fds[0].fd = s_sock;
1589 fds[0].events = ZSOCK_POLLIN;
1590
1591 ret = zsock_poll(fds, 1, 0);
1592 zassert_equal(ret, 0, "Unexpected poll() event");
1593
1594 test_data.sock = c_sock;
1595 k_work_init_delayable(&test_data.tx_work, send_work_handler);
1596 test_work_reschedule(&test_data.tx_work, K_NO_WAIT);
1597
1598 ret = zsock_poll(fds, 1, 100);
1599 zassert_equal(ret, 1, "poll() should've report event");
1600 zassert_equal(fds[0].revents, ZSOCK_POLLIN, "No POLLIN event");
1601
1602 /* Check that data is really available */
1603 ret = zsock_recv(s_sock, rx_buf, sizeof(rx_buf), ZSOCK_MSG_DONTWAIT);
1604 zassert_equal(ret, sizeof(TEST_STR_SMALL) - 1, "recv() failed");
1605 zassert_mem_equal(rx_buf, TEST_STR_SMALL, ret, "Invalid data received");
1606
1607 test_sockets_close();
1608
1609 /* Small delay for the final alert exchange */
1610 k_msleep(10);
1611 }
1612
ZTEST(net_socket_tls,test_poll_tls_pollout)1613 ZTEST(net_socket_tls, test_poll_tls_pollout)
1614 {
1615 int buf_optval = TLS_RECORD_OVERHEAD + sizeof(TEST_STR_SMALL) - 1;
1616 uint8_t rx_buf[sizeof(TEST_STR_SMALL) - 1];
1617 int ret;
1618 struct zsock_pollfd fds[1];
1619
1620 test_prepare_tls_connection(AF_INET6);
1621
1622 fds[0].fd = c_sock;
1623 fds[0].events = ZSOCK_POLLOUT;
1624
1625 ret = zsock_poll(fds, 1, 0);
1626 zassert_equal(ret, 1, "poll() should've report event");
1627 zassert_equal(fds[0].revents, ZSOCK_POLLOUT, "No POLLOUT event");
1628
1629 /* Simulate window full scenario with SO_RCVBUF option. */
1630 ret = zsock_setsockopt(new_sock, SOL_SOCKET, SO_RCVBUF, &buf_optval,
1631 sizeof(buf_optval));
1632 zassert_equal(ret, 0, "setsockopt failed (%d)", errno);
1633
1634 /* Fill out the window */
1635 ret = zsock_send(c_sock, TEST_STR_SMALL, strlen(TEST_STR_SMALL), 0);
1636 zassert_equal(ret, strlen(TEST_STR_SMALL), "send() failed");
1637
1638 /* Wait for ACK (empty window, min. 100 ms due to silly window
1639 * protection).
1640 */
1641 k_sleep(K_MSEC(150));
1642
1643 /* poll() shouldn't report POLLOUT now */
1644 ret = zsock_poll(fds, 1, 0);
1645 zassert_equal(ret, 0, "Unexpected poll() event");
1646
1647 /* Consume the data, and check if the client sock is writeable again */
1648 ret = zsock_recv(new_sock, rx_buf, sizeof(rx_buf), ZSOCK_MSG_DONTWAIT);
1649 zassert_equal(ret, sizeof(TEST_STR_SMALL) - 1, "recv() failed");
1650 zassert_mem_equal(rx_buf, TEST_STR_SMALL, ret, "Invalid data received");
1651
1652 ret = zsock_poll(fds, 1, 100);
1653 zassert_equal(ret, 1, "poll() should've report event");
1654 zassert_equal(fds[0].revents, ZSOCK_POLLOUT, "No POLLOUT event");
1655
1656 test_sockets_close();
1657
1658 k_sleep(TCP_TEARDOWN_TIMEOUT);
1659 }
1660
ZTEST(net_socket_tls,test_poll_dtls_pollout)1661 ZTEST(net_socket_tls, test_poll_dtls_pollout)
1662 {
1663 struct zsock_pollfd fds[1];
1664 int ret;
1665
1666 test_prepare_dtls_connection(AF_INET6);
1667
1668 fds[0].fd = c_sock;
1669 fds[0].events = ZSOCK_POLLOUT;
1670
1671 /* DTLS socket should always be writeable. */
1672 ret = zsock_poll(fds, 1, 0);
1673 zassert_equal(ret, 1, "poll() should've report event");
1674 zassert_equal(fds[0].revents, ZSOCK_POLLOUT, "No POLLOUT event");
1675
1676 test_sockets_close();
1677
1678 /* Small delay for the final alert exchange */
1679 k_msleep(10);
1680 }
1681
ZTEST(net_socket_tls,test_poll_tls_pollhup)1682 ZTEST(net_socket_tls, test_poll_tls_pollhup)
1683 {
1684 struct zsock_pollfd fds[1];
1685 uint8_t rx_buf;
1686 int ret;
1687
1688 test_prepare_tls_connection(AF_INET6);
1689
1690 fds[0].fd = new_sock;
1691 fds[0].events = ZSOCK_POLLIN;
1692
1693 test_close(c_sock);
1694 c_sock = -1;
1695
1696 ret = zsock_poll(fds, 1, 100);
1697 zassert_equal(ret, 1, "poll() should've report event");
1698 zassert_true(fds[0].revents & ZSOCK_POLLIN, "No POLLIN event");
1699 zassert_true(fds[0].revents & ZSOCK_POLLHUP, "No POLLHUP event");
1700
1701 /* Check that connection was indeed closed */
1702 ret = zsock_recv(new_sock, &rx_buf, sizeof(rx_buf), ZSOCK_MSG_DONTWAIT);
1703 zassert_equal(ret, 0, "recv() did not report connection close");
1704
1705 test_sockets_close();
1706
1707 k_sleep(TCP_TEARDOWN_TIMEOUT);
1708 }
1709
ZTEST(net_socket_tls,test_poll_dtls_pollhup)1710 ZTEST(net_socket_tls, test_poll_dtls_pollhup)
1711 {
1712 struct zsock_pollfd fds[1];
1713 uint8_t rx_buf;
1714 int ret;
1715
1716 test_prepare_dtls_connection(AF_INET6);
1717
1718 fds[0].fd = s_sock;
1719 fds[0].events = ZSOCK_POLLIN;
1720
1721 test_close(c_sock);
1722 c_sock = -1;
1723
1724 ret = zsock_poll(fds, 1, 100);
1725 zassert_equal(ret, 1, "poll() should've report event");
1726 zassert_equal(fds[0].revents, ZSOCK_POLLHUP, "No POLLHUP event");
1727
1728 /* Check that connection was indeed closed */
1729 ret = zsock_recv(s_sock, &rx_buf, sizeof(rx_buf), ZSOCK_MSG_DONTWAIT);
1730 zassert_equal(ret, -1, "recv() should report EAGAIN");
1731 zassert_equal(errno, EAGAIN, "Unexpected errno value: %d", errno);
1732
1733 test_sockets_close();
1734
1735 /* Small delay for the final alert exchange */
1736 k_msleep(10);
1737 }
1738
1739 mbedtls_ssl_context *ztls_get_mbedtls_ssl_context(int fd);
1740
ZTEST(net_socket_tls,test_poll_tls_pollerr)1741 ZTEST(net_socket_tls, test_poll_tls_pollerr)
1742 {
1743 uint8_t rx_buf;
1744 int ret;
1745 struct zsock_pollfd fds[1];
1746 int optval;
1747 socklen_t optlen = sizeof(optval);
1748 mbedtls_ssl_context *ssl_ctx;
1749
1750 test_prepare_tls_connection(AF_INET6);
1751
1752 fds[0].fd = new_sock;
1753 fds[0].events = ZSOCK_POLLIN;
1754
1755 /* Get access to the underlying ssl context, and send alert. */
1756 ssl_ctx = ztls_get_mbedtls_ssl_context(c_sock);
1757 mbedtls_ssl_send_alert_message(ssl_ctx, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1758 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1759
1760 ret = zsock_poll(fds, 1, 100);
1761 zassert_equal(ret, 1, "poll() should've report event");
1762 zassert_true(fds[0].revents & ZSOCK_POLLERR, "No POLLERR event");
1763
1764 ret = zsock_getsockopt(new_sock, SOL_SOCKET, SO_ERROR, &optval, &optlen);
1765 zassert_equal(ret, 0, "getsockopt failed (%d)", errno);
1766 zassert_equal(optval, ECONNABORTED, "getsockopt got invalid error %d",
1767 optval);
1768
1769 ret = zsock_recv(new_sock, &rx_buf, sizeof(rx_buf), ZSOCK_MSG_DONTWAIT);
1770 zassert_equal(ret, -1, "recv() did not report error");
1771 zassert_equal(errno, ECONNABORTED, "Unexpected errno value: %d", errno);
1772
1773 test_sockets_close();
1774
1775 k_sleep(TCP_TEARDOWN_TIMEOUT);
1776 }
1777
ZTEST(net_socket_tls,test_poll_dtls_pollerr)1778 ZTEST(net_socket_tls, test_poll_dtls_pollerr)
1779 {
1780 uint8_t rx_buf;
1781 int ret;
1782 struct zsock_pollfd fds[1];
1783 int optval;
1784 socklen_t optlen = sizeof(optval);
1785 mbedtls_ssl_context *ssl_ctx;
1786
1787 test_prepare_dtls_connection(AF_INET6);
1788
1789 fds[0].fd = s_sock;
1790 fds[0].events = ZSOCK_POLLIN;
1791
1792 /* Get access to the underlying ssl context, and send alert. */
1793 ssl_ctx = ztls_get_mbedtls_ssl_context(c_sock);
1794 mbedtls_ssl_send_alert_message(ssl_ctx, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
1795 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
1796
1797 ret = zsock_poll(fds, 1, 100);
1798 zassert_equal(ret, 1, "poll() should've report event");
1799 zassert_true(fds[0].revents & ZSOCK_POLLERR, "No POLLERR event");
1800
1801 ret = zsock_getsockopt(s_sock, SOL_SOCKET, SO_ERROR, &optval, &optlen);
1802 zassert_equal(ret, 0, "getsockopt failed (%d)", errno);
1803 zassert_equal(optval, ECONNABORTED, "getsockopt got invalid error %d",
1804 optval);
1805
1806 /* DTLS server socket should recover and be ready to accept new session. */
1807 ret = zsock_recv(s_sock, &rx_buf, sizeof(rx_buf), ZSOCK_MSG_DONTWAIT);
1808 zassert_equal(ret, -1, "recv() did not report error");
1809 zassert_equal(errno, EAGAIN, "Unexpected errno value: %d", errno);
1810
1811 test_sockets_close();
1812
1813 /* Small delay for the final alert exchange */
1814 k_msleep(10);
1815 }
1816
tls_tests_setup(void)1817 static void *tls_tests_setup(void)
1818 {
1819 k_work_queue_init(&tls_test_work_queue);
1820 k_work_queue_start(&tls_test_work_queue, tls_test_work_queue_stack,
1821 K_THREAD_STACK_SIZEOF(tls_test_work_queue_stack),
1822 K_LOWEST_APPLICATION_THREAD_PRIO, NULL);
1823
1824 return NULL;
1825 }
1826
tls_tests_after(void * arg)1827 static void tls_tests_after(void *arg)
1828 {
1829 ARG_UNUSED(arg);
1830
1831 test_sockets_close();
1832 }
1833
1834 ZTEST_SUITE(net_socket_tls, NULL, tls_tests_setup, NULL, tls_tests_after, NULL);
1835