1 /*
2  * Copyright (c) 2018-2020 Nordic Semiconductor ASA
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 <stdio.h>
11 #include <zephyr/ztest_assert.h>
12 #include <zephyr/sys/util.h>
13 #include <zephyr/sys/sem.h>
14 #include <zephyr/net/socket.h>
15 #include <zephyr/net/dns_resolve.h>
16 #include <zephyr/net_buf.h>
17 
18 #include "../../socket_helpers.h"
19 
20 #include "dns_pack.h"
21 
22 #define QUERY_HOST "www.zephyrproject.org"
23 
24 #define ANY_PORT 0
25 #define MAX_BUF_SIZE 128
26 #define STACK_SIZE (1024 + CONFIG_TEST_EXTRA_STACK_SIZE)
27 #define THREAD_PRIORITY K_PRIO_COOP(2)
28 #define WAIT_TIME K_MSEC(250)
29 
30 static uint8_t recv_buf[MAX_BUF_SIZE];
31 
32 static int sock_v4;
33 static int sock_v6;
34 
35 static struct sockaddr_in addr_v4;
36 static struct sockaddr_in6 addr_v6;
37 
38 static int queries_received;
39 static int expected_query_count =
40 	CONFIG_NET_SOCKETS_DNS_BACKOFF_INTERVAL >= CONFIG_NET_SOCKETS_DNS_TIMEOUT ?
41 	2 :
42 	/* Calculate for both IPv4 and IPv6 so need to double the value */
43 	2 * (LOG2CEIL(DIV_ROUND_UP(CONFIG_NET_SOCKETS_DNS_TIMEOUT,
44 				   CONFIG_NET_SOCKETS_DNS_BACKOFF_INTERVAL) + 1));
45 
46 /* The semaphore is there to wait the data to be received. */
47 static ZTEST_BMEM struct sys_sem wait_data;
48 
49 NET_BUF_POOL_DEFINE(test_dns_msg_pool, 1, 512, 0, NULL);
50 
check_dns_query(uint8_t * buf,int buf_len)51 static bool check_dns_query(uint8_t *buf, int buf_len)
52 {
53 	struct dns_msg_t dns_msg;
54 	struct net_buf *result;
55 	enum dns_rr_type qtype;
56 	enum dns_class qclass;
57 	int ret, queries;
58 
59 	/* Store the DNS query name into a temporary net_buf as that is
60 	 * expected by dns_unpack_query() function. In this test we are
61 	 * currently not sending any DNS response back as that is not
62 	 * really needed by these tests.
63 	 */
64 	result = net_buf_alloc(&test_dns_msg_pool, K_FOREVER);
65 	if (!result) {
66 		ret = -ENOMEM;
67 		return false;
68 	}
69 
70 	dns_msg.msg = buf;
71 	dns_msg.msg_size = buf_len;
72 
73 	ret = mdns_unpack_query_header(&dns_msg, NULL);
74 	if (ret < 0) {
75 		return false;
76 	}
77 
78 	queries = ret;
79 	queries_received++;
80 
81 	NET_DBG("Received %d %s", queries,
82 		queries > 1 ? "queries" : "query");
83 
84 	(void)memset(result->data, 0, net_buf_tailroom(result));
85 	result->len = 0U;
86 
87 	ret = dns_unpack_query(&dns_msg, result, &qtype, &qclass);
88 	if (ret < 0) {
89 		net_buf_unref(result);
90 		return false;
91 	}
92 
93 	NET_DBG("[%d] query %s/%s label %s (%d bytes)", queries,
94 		qtype == DNS_RR_TYPE_A ? "A" : "AAAA", "IN",
95 		result->data, ret);
96 
97 	/* In this test we are just checking if the query came to us in correct
98 	 * form, we are not creating a DNS server implementation here.
99 	 */
100 	if (strncmp(result->data + 1, QUERY_HOST,
101 		    sizeof(QUERY_HOST) - 1)) {
102 		net_buf_unref(result);
103 		return false;
104 	}
105 
106 	net_buf_unref(result);
107 
108 	return true;
109 }
110 
process_dns(void)111 static int process_dns(void)
112 {
113 	struct zsock_pollfd pollfds[2];
114 	struct sockaddr *addr;
115 	socklen_t addr_len;
116 	int ret, idx;
117 
118 	NET_DBG("Waiting for IPv4 DNS packets on port %d",
119 		ntohs(addr_v4.sin_port));
120 	NET_DBG("Waiting for IPv6 DNS packets on port %d",
121 		ntohs(addr_v6.sin6_port));
122 
123 	while (true) {
124 		memset(pollfds, 0, sizeof(pollfds));
125 		pollfds[0].fd = sock_v4;
126 		pollfds[0].events = ZSOCK_POLLIN;
127 		pollfds[1].fd = sock_v6;
128 		pollfds[1].events = ZSOCK_POLLIN;
129 
130 		NET_DBG("Polling...");
131 
132 		ret = zsock_poll(pollfds, ARRAY_SIZE(pollfds), -1);
133 		if (ret <= 0) {
134 			continue;
135 		}
136 
137 		for (idx = 0; idx < ARRAY_SIZE(pollfds); idx++) {
138 			if (pollfds[idx].revents & ZSOCK_POLLIN) {
139 				if (pollfds[idx].fd == sock_v4) {
140 					addr_len = sizeof(addr_v4);
141 					addr = (struct sockaddr *)&addr_v4;
142 				} else {
143 					addr_len = sizeof(addr_v6);
144 					addr = (struct sockaddr *)&addr_v6;
145 				}
146 
147 				ret = zsock_recvfrom(pollfds[idx].fd,
148 						     recv_buf, sizeof(recv_buf), 0,
149 						     addr, &addr_len);
150 				if (ret < 0) {
151 					/* Socket error */
152 					NET_ERR("DNS: Connection error (%d)",
153 						errno);
154 					break;
155 				}
156 
157 				NET_DBG("Received DNS query");
158 
159 				ret = check_dns_query(recv_buf,
160 						      sizeof(recv_buf));
161 				if (ret) {
162 					(void)sys_sem_give(&wait_data);
163 				}
164 			}
165 		}
166 	}
167 
168 	return -errno;
169 }
170 
171 K_THREAD_DEFINE(dns_server_thread_id, STACK_SIZE,
172 		process_dns, NULL, NULL, NULL,
173 		THREAD_PRIORITY, 0, -1);
174 
test_getaddrinfo_setup(void)175 static void *test_getaddrinfo_setup(void)
176 {
177 	char str[INET6_ADDRSTRLEN], *addr_str;
178 	struct sockaddr addr;
179 	int ret;
180 
181 	ret = net_ipaddr_parse(CONFIG_DNS_SERVER1,
182 			       sizeof(CONFIG_DNS_SERVER1) - 1,
183 			       &addr);
184 	zassert_true(ret, "Cannot parse IP address %s", CONFIG_DNS_SERVER1);
185 
186 	if (addr.sa_family == AF_INET) {
187 		memcpy(&addr_v4, net_sin(&addr), sizeof(struct sockaddr_in));
188 	} else if (addr.sa_family == AF_INET6) {
189 		memcpy(&addr_v6, net_sin6(&addr), sizeof(struct sockaddr_in6));
190 	}
191 
192 	ret = net_ipaddr_parse(CONFIG_DNS_SERVER2,
193 			       sizeof(CONFIG_DNS_SERVER2) - 1,
194 			       &addr);
195 	zassert_true(ret, "Cannot parse IP address %s", CONFIG_DNS_SERVER2);
196 
197 	if (addr.sa_family == AF_INET) {
198 		memcpy(&addr_v4, net_sin(&addr), sizeof(struct sockaddr_in));
199 	} else if (addr.sa_family == AF_INET6) {
200 		memcpy(&addr_v6, net_sin6(&addr), sizeof(struct sockaddr_in6));
201 	}
202 
203 	addr_str = zsock_inet_ntop(AF_INET, &addr_v4.sin_addr, str, sizeof(str));
204 	NET_DBG("v4: [%s]:%d", addr_str, ntohs(addr_v4.sin_port));
205 
206 	sock_v4 = prepare_listen_sock_udp_v4(&addr_v4);
207 	zassert_true(sock_v4 >= 0, "Invalid IPv4 socket");
208 
209 	addr_str = zsock_inet_ntop(AF_INET6, &addr_v6.sin6_addr, str, sizeof(str));
210 	NET_DBG("v6: [%s]:%d", addr_str, ntohs(addr_v6.sin6_port));
211 
212 	sock_v6 = prepare_listen_sock_udp_v6(&addr_v6);
213 	zassert_true(sock_v6 >= 0, "Invalid IPv6 socket");
214 
215 	sys_sem_init(&wait_data, 0, INT_MAX);
216 
217 	k_thread_start(dns_server_thread_id);
218 
219 	k_thread_priority_set(dns_server_thread_id,
220 			      k_thread_priority_get(k_current_get()));
221 	k_yield();
222 
223 	return NULL;
224 }
225 
ZTEST(net_socket_getaddrinfo,test_getaddrinfo_ok)226 ZTEST(net_socket_getaddrinfo, test_getaddrinfo_ok)
227 {
228 	struct zsock_addrinfo *res = NULL;
229 
230 	queries_received = 0;
231 
232 	/* This check simulates a local query that we will catch
233 	 * in process_dns() function. So we do not check the res variable
234 	 * as that will currently not contain anything useful. We just check
235 	 * that the query triggered a function call to process_dns() function
236 	 * and that it could parse the DNS query.
237 	 */
238 	(void)zsock_getaddrinfo(QUERY_HOST, NULL, NULL, &res);
239 
240 	(void)sys_sem_take(&wait_data, K_NO_WAIT);
241 	(void)sys_sem_take(&wait_data, K_NO_WAIT);
242 
243 	zassert_equal(queries_received, expected_query_count,
244 		      "Did not receive both IPv4 and IPv6 query (got %d, expected %d)",
245 		      queries_received, expected_query_count);
246 
247 	zsock_freeaddrinfo(res);
248 }
249 
ZTEST(net_socket_getaddrinfo,test_getaddrinfo_cancelled)250 ZTEST(net_socket_getaddrinfo, test_getaddrinfo_cancelled)
251 {
252 	struct zsock_addrinfo *res = NULL;
253 	int ret;
254 
255 	ret = zsock_getaddrinfo(QUERY_HOST, NULL, NULL, &res);
256 
257 	(void)sys_sem_take(&wait_data, K_NO_WAIT);
258 	(void)sys_sem_take(&wait_data, K_NO_WAIT);
259 
260 	zassert_equal(queries_received, expected_query_count,
261 		      "Did not receive both IPv4 and IPv6 query (got %d, expected %d)",
262 		      queries_received, expected_query_count);
263 
264 	/* Without a local DNS server this request will be canceled. */
265 	zassert_equal(ret, DNS_EAI_CANCELED, "Invalid result");
266 
267 	zsock_freeaddrinfo(res);
268 }
269 
ZTEST(net_socket_getaddrinfo,test_getaddrinfo_no_host)270 ZTEST(net_socket_getaddrinfo, test_getaddrinfo_no_host)
271 {
272 	struct zsock_addrinfo *res = NULL;
273 	int ret;
274 
275 	ret = zsock_getaddrinfo(NULL, NULL, NULL, &res);
276 
277 	zassert_equal(ret, DNS_EAI_SYSTEM, "Invalid result");
278 	zassert_equal(errno, EINVAL, "Invalid errno");
279 	zassert_is_null(res, "ai_addr is not NULL");
280 
281 	zsock_freeaddrinfo(res);
282 }
283 
ZTEST(net_socket_getaddrinfo,test_getaddrinfo_num_ipv4)284 ZTEST(net_socket_getaddrinfo, test_getaddrinfo_num_ipv4)
285 {
286 	struct zsock_addrinfo *res = NULL;
287 	struct sockaddr_in *saddr;
288 	int ret;
289 
290 	struct zsock_addrinfo hints = {
291 		.ai_family = AF_INET,
292 		.ai_socktype = SOCK_STREAM
293 	};
294 
295 	ret = zsock_getaddrinfo("1.2.3.255", "65534", NULL, &res);
296 
297 	zassert_equal(ret, 0, "Invalid result");
298 	zassert_not_null(res, "");
299 	zassert_is_null(res->ai_next, "");
300 	zassert_equal(res->ai_family, AF_INET, "");
301 	zassert_equal(res->ai_socktype, SOCK_STREAM, "");
302 	zassert_equal(res->ai_protocol, IPPROTO_TCP, "");
303 	zsock_freeaddrinfo(res);
304 
305 	ret = zsock_getaddrinfo("1.2.3.255", "65534", &hints, &res);
306 	zassert_equal(ret, 0, "Invalid result");
307 	zassert_not_null(res, "");
308 	zassert_is_null(res->ai_next, "");
309 	zassert_equal(res->ai_family, AF_INET, "");
310 	zassert_equal(res->ai_socktype, SOCK_STREAM, "");
311 	zassert_equal(res->ai_protocol, IPPROTO_TCP, "");
312 	zsock_freeaddrinfo(res);
313 
314 	hints.ai_socktype = SOCK_DGRAM;
315 	ret = zsock_getaddrinfo("1.2.3.255", "65534", &hints, &res);
316 	zassert_equal(ret, 0, "Invalid result");
317 	zassert_not_null(res, "");
318 	zassert_is_null(res->ai_next, "");
319 	zassert_equal(res->ai_family, AF_INET, "");
320 	zassert_equal(res->ai_socktype, SOCK_DGRAM, "");
321 	zassert_equal(res->ai_protocol, IPPROTO_UDP, "");
322 
323 	saddr = (struct sockaddr_in *)res->ai_addr;
324 	zassert_equal(saddr->sin_family, AF_INET, "");
325 	zassert_equal(saddr->sin_port, htons(65534), "");
326 	zassert_equal(saddr->sin_addr.s4_addr[0], 1, "");
327 	zassert_equal(saddr->sin_addr.s4_addr[1], 2, "");
328 	zassert_equal(saddr->sin_addr.s4_addr[2], 3, "");
329 	zassert_equal(saddr->sin_addr.s4_addr[3], 255, "");
330 	zsock_freeaddrinfo(res);
331 }
332 
ZTEST(net_socket_getaddrinfo,test_getaddrinfo_num_ipv6)333 ZTEST(net_socket_getaddrinfo, test_getaddrinfo_num_ipv6)
334 {
335 	struct zsock_addrinfo *res = NULL;
336 	struct sockaddr_in6 *saddr;
337 	int ret;
338 
339 	struct zsock_addrinfo hints = {
340 		.ai_family = AF_INET6,
341 		.ai_socktype = SOCK_STREAM
342 	};
343 
344 	ret = zsock_getaddrinfo("[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]",
345 			"65534", NULL, &res);
346 
347 	zassert_equal(ret, 0, "Invalid result");
348 	zassert_not_null(res, "");
349 	zassert_is_null(res->ai_next, "");
350 	zassert_equal(res->ai_family, AF_INET6, "");
351 	zassert_equal(res->ai_socktype, SOCK_STREAM, "");
352 	zassert_equal(res->ai_protocol, IPPROTO_TCP, "");
353 
354 	saddr = (struct sockaddr_in6 *)res->ai_addr;
355 	zassert_equal(saddr->sin6_family, AF_INET6, "");
356 	zassert_equal(saddr->sin6_port, htons(65534), "");
357 	zassert_equal(saddr->sin6_addr.s6_addr[0], 0xFE, "");
358 	zassert_equal(saddr->sin6_addr.s6_addr[1], 0xDC, "");
359 	zassert_equal(saddr->sin6_addr.s6_addr[2], 0xBA, "");
360 	zassert_equal(saddr->sin6_addr.s6_addr[3], 0x98, "");
361 	zassert_equal(saddr->sin6_addr.s6_addr[4], 0x76, "");
362 	zassert_equal(saddr->sin6_addr.s6_addr[5], 0x54, "");
363 	zassert_equal(saddr->sin6_addr.s6_addr[6], 0x32, "");
364 	zassert_equal(saddr->sin6_addr.s6_addr[7], 0x10, "");
365 	zassert_equal(saddr->sin6_addr.s6_addr[8], 0xFE, "");
366 	zassert_equal(saddr->sin6_addr.s6_addr[9], 0xDC, "");
367 	zassert_equal(saddr->sin6_addr.s6_addr[10], 0xBA, "");
368 	zassert_equal(saddr->sin6_addr.s6_addr[11], 0x98, "");
369 	zassert_equal(saddr->sin6_addr.s6_addr[12], 0x76, "");
370 	zassert_equal(saddr->sin6_addr.s6_addr[13], 0x54, "");
371 	zassert_equal(saddr->sin6_addr.s6_addr[14], 0x32, "");
372 	zassert_equal(saddr->sin6_addr.s6_addr[15], 0x10, "");
373 	zsock_freeaddrinfo(res);
374 
375 
376 	ret = zsock_getaddrinfo("[1080:0:0:0:8:800:200C:417A]",
377 			"65534", &hints, &res);
378 	zassert_equal(ret, 0, "Invalid result");
379 	zassert_not_null(res, "");
380 	zassert_is_null(res->ai_next, "");
381 	zassert_equal(res->ai_family, AF_INET6, "");
382 	zassert_equal(res->ai_socktype, SOCK_STREAM, "");
383 	zassert_equal(res->ai_protocol, IPPROTO_TCP, "");
384 
385 	saddr = (struct sockaddr_in6 *)res->ai_addr;
386 	zassert_equal(saddr->sin6_family, AF_INET6, "");
387 	zassert_equal(saddr->sin6_port, htons(65534), "");
388 	zassert_equal(saddr->sin6_addr.s6_addr[0], 0x10, "");
389 	zassert_equal(saddr->sin6_addr.s6_addr[1], 0x80, "");
390 	zassert_equal(saddr->sin6_addr.s6_addr[2], 0x0, "");
391 	zassert_equal(saddr->sin6_addr.s6_addr[3], 0x0, "");
392 	zassert_equal(saddr->sin6_addr.s6_addr[4], 0x0, "");
393 	zassert_equal(saddr->sin6_addr.s6_addr[5], 0x0, "");
394 	zassert_equal(saddr->sin6_addr.s6_addr[6], 0x0, "");
395 	zassert_equal(saddr->sin6_addr.s6_addr[7], 0x0, "");
396 	zassert_equal(saddr->sin6_addr.s6_addr[8], 0x0, "");
397 	zassert_equal(saddr->sin6_addr.s6_addr[9], 0x08, "");
398 	zassert_equal(saddr->sin6_addr.s6_addr[10], 0x08, "");
399 	zassert_equal(saddr->sin6_addr.s6_addr[11], 0x00, "");
400 	zassert_equal(saddr->sin6_addr.s6_addr[12], 0x20, "");
401 	zassert_equal(saddr->sin6_addr.s6_addr[13], 0x0C, "");
402 	zassert_equal(saddr->sin6_addr.s6_addr[14], 0x41, "");
403 	zassert_equal(saddr->sin6_addr.s6_addr[15], 0x7A, "");
404 	zsock_freeaddrinfo(res);
405 
406 
407 	hints.ai_socktype = SOCK_DGRAM;
408 	ret = zsock_getaddrinfo("[3ffe:2a00:100:7031::1]",
409 			"65534", &hints, &res);
410 	zassert_equal(ret, 0, "Invalid result");
411 	zassert_not_null(res, "");
412 	zassert_is_null(res->ai_next, "");
413 	zassert_equal(res->ai_family, AF_INET6, "");
414 	zassert_equal(res->ai_socktype, SOCK_DGRAM, "");
415 	zassert_equal(res->ai_protocol, IPPROTO_UDP, "");
416 
417 	saddr = (struct sockaddr_in6 *)res->ai_addr;
418 	zassert_equal(saddr->sin6_family, AF_INET6, "");
419 	zassert_equal(saddr->sin6_port, htons(65534), "");
420 	zassert_equal(saddr->sin6_addr.s6_addr[0], 0x3f, "");
421 	zassert_equal(saddr->sin6_addr.s6_addr[1], 0xfe, "");
422 	zassert_equal(saddr->sin6_addr.s6_addr[2], 0x2a, "");
423 	zassert_equal(saddr->sin6_addr.s6_addr[3], 0x00, "");
424 	zassert_equal(saddr->sin6_addr.s6_addr[4], 0x01, "");
425 	zassert_equal(saddr->sin6_addr.s6_addr[5], 0x00, "");
426 	zassert_equal(saddr->sin6_addr.s6_addr[6], 0x70, "");
427 	zassert_equal(saddr->sin6_addr.s6_addr[7], 0x31, "");
428 	zassert_equal(saddr->sin6_addr.s6_addr[8], 0x0, "");
429 	zassert_equal(saddr->sin6_addr.s6_addr[9], 0x0, "");
430 	zassert_equal(saddr->sin6_addr.s6_addr[10], 0x0, "");
431 	zassert_equal(saddr->sin6_addr.s6_addr[11], 0x0, "");
432 	zassert_equal(saddr->sin6_addr.s6_addr[12], 0x0, "");
433 	zassert_equal(saddr->sin6_addr.s6_addr[13], 0x0, "");
434 	zassert_equal(saddr->sin6_addr.s6_addr[14], 0x0, "");
435 	zassert_equal(saddr->sin6_addr.s6_addr[15], 0x1, "");
436 	zsock_freeaddrinfo(res);
437 
438 
439 	ret = zsock_getaddrinfo("[1080::8:800:200C:417A]",
440 			"65534", &hints, &res);
441 	zassert_equal(ret, 0, "Invalid result");
442 
443 	saddr = (struct sockaddr_in6 *)res->ai_addr;
444 	zassert_equal(saddr->sin6_family, AF_INET6, "");
445 	zassert_equal(saddr->sin6_port, htons(65534), "");
446 	zassert_equal(saddr->sin6_addr.s6_addr[0], 0x10, "");
447 	zassert_equal(saddr->sin6_addr.s6_addr[1], 0x80, "");
448 	zassert_equal(saddr->sin6_addr.s6_addr[2], 0x0, "");
449 	zassert_equal(saddr->sin6_addr.s6_addr[3], 0x0, "");
450 	zassert_equal(saddr->sin6_addr.s6_addr[4], 0x0, "");
451 	zassert_equal(saddr->sin6_addr.s6_addr[5], 0x0, "");
452 	zassert_equal(saddr->sin6_addr.s6_addr[6], 0x0, "");
453 	zassert_equal(saddr->sin6_addr.s6_addr[7], 0x0, "");
454 	zassert_equal(saddr->sin6_addr.s6_addr[8], 0x0, "");
455 	zassert_equal(saddr->sin6_addr.s6_addr[9], 0x8, "");
456 	zassert_equal(saddr->sin6_addr.s6_addr[10], 0x08, "");
457 	zassert_equal(saddr->sin6_addr.s6_addr[11], 0x00, "");
458 	zassert_equal(saddr->sin6_addr.s6_addr[12], 0x20, "");
459 	zassert_equal(saddr->sin6_addr.s6_addr[13], 0x0C, "");
460 	zassert_equal(saddr->sin6_addr.s6_addr[14], 0x41, "");
461 	zassert_equal(saddr->sin6_addr.s6_addr[15], 0x7A, "");
462 	zsock_freeaddrinfo(res);
463 
464 
465 	ret = zsock_getaddrinfo("[::192.9.5.5]", "65534", &hints, &res);
466 	zassert_equal(ret, 0, "Invalid result");
467 
468 	saddr = (struct sockaddr_in6 *)res->ai_addr;
469 	zassert_equal(saddr->sin6_family, AF_INET6, "");
470 	zassert_equal(saddr->sin6_port, htons(65534), "");
471 	zassert_equal(saddr->sin6_addr.s6_addr[0], 0x0, "");
472 	zassert_equal(saddr->sin6_addr.s6_addr[1], 0x0, "");
473 	zassert_equal(saddr->sin6_addr.s6_addr[2], 0x0, "");
474 	zassert_equal(saddr->sin6_addr.s6_addr[3], 0x0, "");
475 	zassert_equal(saddr->sin6_addr.s6_addr[4], 0x0, "");
476 	zassert_equal(saddr->sin6_addr.s6_addr[5], 0x0, "");
477 	zassert_equal(saddr->sin6_addr.s6_addr[6], 0x0, "");
478 	zassert_equal(saddr->sin6_addr.s6_addr[7], 0x0, "");
479 	zassert_equal(saddr->sin6_addr.s6_addr[8], 0x0, "");
480 	zassert_equal(saddr->sin6_addr.s6_addr[9], 0x0, "");
481 	zassert_equal(saddr->sin6_addr.s6_addr[10], 0x0, "");
482 	zassert_equal(saddr->sin6_addr.s6_addr[11], 0x0, "");
483 	zassert_equal(saddr->sin6_addr.s6_addr[12], 192, "");
484 	zassert_equal(saddr->sin6_addr.s6_addr[13], 9, "");
485 	zassert_equal(saddr->sin6_addr.s6_addr[14], 5, "");
486 	zassert_equal(saddr->sin6_addr.s6_addr[15], 5, "");
487 	zsock_freeaddrinfo(res);
488 
489 
490 	ret = zsock_getaddrinfo("[::FFFF:129.144.52.38]",
491 			"65534", &hints, &res);
492 	zassert_equal(ret, 0, "Invalid result");
493 
494 	saddr = (struct sockaddr_in6 *)res->ai_addr;
495 	zassert_equal(saddr->sin6_family, AF_INET6, "");
496 	zassert_equal(saddr->sin6_port, htons(65534), "");
497 	zassert_equal(saddr->sin6_addr.s6_addr[0], 0x0, "");
498 	zassert_equal(saddr->sin6_addr.s6_addr[1], 0x0, "");
499 	zassert_equal(saddr->sin6_addr.s6_addr[2], 0x0, "");
500 	zassert_equal(saddr->sin6_addr.s6_addr[3], 0x0, "");
501 	zassert_equal(saddr->sin6_addr.s6_addr[4], 0x0, "");
502 	zassert_equal(saddr->sin6_addr.s6_addr[5], 0x0, "");
503 	zassert_equal(saddr->sin6_addr.s6_addr[6], 0x0, "");
504 	zassert_equal(saddr->sin6_addr.s6_addr[7], 0x0, "");
505 	zassert_equal(saddr->sin6_addr.s6_addr[8], 0x0, "");
506 	zassert_equal(saddr->sin6_addr.s6_addr[9], 0x0, "");
507 	zassert_equal(saddr->sin6_addr.s6_addr[10], 0xFF, "");
508 	zassert_equal(saddr->sin6_addr.s6_addr[11], 0xFF, "");
509 	zassert_equal(saddr->sin6_addr.s6_addr[12], 129, "");
510 	zassert_equal(saddr->sin6_addr.s6_addr[13], 144, "");
511 	zassert_equal(saddr->sin6_addr.s6_addr[14], 52, "");
512 	zassert_equal(saddr->sin6_addr.s6_addr[15], 38, "");
513 	zsock_freeaddrinfo(res);
514 
515 
516 	ret = zsock_getaddrinfo("[2010:836B:4179::836B:4179]",
517 			"65534", &hints, &res);
518 	zassert_equal(ret, 0, "Invalid result");
519 
520 	saddr = (struct sockaddr_in6 *)res->ai_addr;
521 	zassert_equal(saddr->sin6_family, AF_INET6, "");
522 	zassert_equal(saddr->sin6_port, htons(65534), "");
523 	zassert_equal(saddr->sin6_addr.s6_addr[0], 0x20, "");
524 	zassert_equal(saddr->sin6_addr.s6_addr[1], 0x10, "");
525 	zassert_equal(saddr->sin6_addr.s6_addr[2], 0x83, "");
526 	zassert_equal(saddr->sin6_addr.s6_addr[3], 0x6B, "");
527 	zassert_equal(saddr->sin6_addr.s6_addr[4], 0x41, "");
528 	zassert_equal(saddr->sin6_addr.s6_addr[5], 0x79, "");
529 	zassert_equal(saddr->sin6_addr.s6_addr[6], 0x0, "");
530 	zassert_equal(saddr->sin6_addr.s6_addr[7], 0x0, "");
531 	zassert_equal(saddr->sin6_addr.s6_addr[8], 0x0, "");
532 	zassert_equal(saddr->sin6_addr.s6_addr[9], 0x0, "");
533 	zassert_equal(saddr->sin6_addr.s6_addr[10], 0x0, "");
534 	zassert_equal(saddr->sin6_addr.s6_addr[11], 0x0, "");
535 	zassert_equal(saddr->sin6_addr.s6_addr[12], 0x83, "");
536 	zassert_equal(saddr->sin6_addr.s6_addr[13], 0x6B, "");
537 	zassert_equal(saddr->sin6_addr.s6_addr[14], 0x41, "");
538 	zassert_equal(saddr->sin6_addr.s6_addr[15], 0x79, "");
539 	zsock_freeaddrinfo(res);
540 }
541 
ZTEST(net_socket_getaddrinfo,test_getaddrinfo_flags_numerichost)542 ZTEST(net_socket_getaddrinfo, test_getaddrinfo_flags_numerichost)
543 {
544 	int ret;
545 	struct zsock_addrinfo *res = NULL;
546 	struct zsock_addrinfo hints = {
547 		.ai_flags = AI_NUMERICHOST,
548 	};
549 
550 	ret = zsock_getaddrinfo("foo.bar", "65534", &hints, &res);
551 	zassert_equal(ret, DNS_EAI_FAIL, "Invalid result");
552 	zassert_is_null(res, "");
553 
554 	ret = zsock_getaddrinfo("1.2.3.4", "65534", &hints, &res);
555 	zassert_equal(ret, 0, "Invalid result");
556 	zassert_not_null(res, "");
557 
558 	zsock_freeaddrinfo(res);
559 }
560 
ZTEST(net_socket_getaddrinfo,test_getaddrinfo_ipv4_hints_ipv6)561 ZTEST(net_socket_getaddrinfo, test_getaddrinfo_ipv4_hints_ipv6)
562 {
563 	struct zsock_addrinfo *res = NULL;
564 	struct zsock_addrinfo hints = {
565 		.ai_family = AF_INET6,
566 	};
567 	int ret;
568 
569 	ret = zsock_getaddrinfo("192.0.2.1", NULL, &hints, &res);
570 	zassert_equal(ret, DNS_EAI_ADDRFAMILY, "Invalid result (%d)", ret);
571 	zassert_is_null(res, "");
572 	zsock_freeaddrinfo(res);
573 }
574 
ZTEST(net_socket_getaddrinfo,test_getaddrinfo_ipv6_hints_ipv4)575 ZTEST(net_socket_getaddrinfo, test_getaddrinfo_ipv6_hints_ipv4)
576 {
577 	struct zsock_addrinfo *res = NULL;
578 	struct zsock_addrinfo hints = {
579 		.ai_family = AF_INET,
580 	};
581 	int ret;
582 
583 	ret = zsock_getaddrinfo("2001:db8::1", NULL, &hints, &res);
584 	zassert_equal(ret, DNS_EAI_ADDRFAMILY, "Invalid result (%d)", ret);
585 	zassert_is_null(res, "");
586 	zsock_freeaddrinfo(res);
587 }
588 
ZTEST(net_socket_getaddrinfo,test_getaddrinfo_port_invalid)589 ZTEST(net_socket_getaddrinfo, test_getaddrinfo_port_invalid)
590 {
591 	int ret;
592 	struct zsock_addrinfo *res = NULL;
593 	ret = zsock_getaddrinfo("192.0.2.1", "70000", NULL, &res);
594 	zassert_equal(ret, DNS_EAI_NONAME, "Invalid result (%d)", ret);
595 	zassert_is_null(res, "");
596 	zsock_freeaddrinfo(res);
597 }
598 
ZTEST(net_socket_getaddrinfo,test_getaddrinfo_null_host)599 ZTEST(net_socket_getaddrinfo, test_getaddrinfo_null_host)
600 {
601 	struct sockaddr_in *saddr;
602 	struct sockaddr_in6 *saddr6;
603 	struct zsock_addrinfo *res = NULL;
604 	struct zsock_addrinfo hints = {
605 		.ai_family = AF_INET,
606 		.ai_socktype = SOCK_STREAM,
607 		.ai_flags = AI_PASSIVE
608 	};
609 	int ret;
610 
611 	/* Test IPv4 TCP */
612 	ret = zsock_getaddrinfo(NULL, "80", &hints, &res);
613 	zassert_equal(ret, 0, "Invalid result");
614 	zassert_not_null(res, "");
615 	zassert_is_null(res->ai_next, "");
616 	zassert_equal(res->ai_family, AF_INET, "");
617 	zassert_equal(res->ai_socktype, SOCK_STREAM, "");
618 	zassert_equal(res->ai_protocol, IPPROTO_TCP, "");
619 	saddr = net_sin(res->ai_addr);
620 	zassert_equal(saddr->sin_family, AF_INET, "");
621 	zassert_equal(saddr->sin_port, htons(80), "");
622 	zassert_equal(saddr->sin_addr.s_addr, INADDR_ANY, "");
623 	zsock_freeaddrinfo(res);
624 
625 	/* Test IPv6 TCP */
626 	hints.ai_family = AF_INET6;
627 	ret = zsock_getaddrinfo(NULL, "80", &hints, &res);
628 	zassert_equal(ret, 0, "Invalid result");
629 	zassert_not_null(res, "");
630 	zassert_is_null(res->ai_next, "");
631 	zassert_equal(res->ai_family, AF_INET6, "");
632 	zassert_equal(res->ai_socktype, SOCK_STREAM, "");
633 	zassert_equal(res->ai_protocol, IPPROTO_TCP, "");
634 	saddr6 = net_sin6(res->ai_addr);
635 	zassert_equal(saddr6->sin6_family, AF_INET6, "");
636 	zassert_equal(saddr6->sin6_port, htons(80), "");
637 	zassert_equal(0, memcmp(&saddr6->sin6_addr, &in6addr_any, sizeof(in6addr_any)), "");
638 	zsock_freeaddrinfo(res);
639 
640 	/* Test IPv6 UDP */
641 	hints.ai_socktype = SOCK_DGRAM;
642 	ret = zsock_getaddrinfo(NULL, "80", &hints, &res);
643 	zassert_equal(ret, 0, "Invalid result");
644 	zassert_not_null(res, "");
645 	zassert_is_null(res->ai_next, "");
646 	zassert_equal(res->ai_family, AF_INET6, "");
647 	zassert_equal(res->ai_socktype, SOCK_DGRAM, "");
648 	zassert_equal(res->ai_protocol, IPPROTO_UDP, "");
649 	saddr6 = (struct sockaddr_in6 *)res->ai_addr;
650 	zassert_equal(saddr6->sin6_family, AF_INET6, "");
651 	zassert_equal(saddr6->sin6_port, htons(80), "");
652 	zsock_freeaddrinfo(res);
653 
654 	/* Test IPv4 UDP */
655 	hints.ai_family = AF_INET;
656 	ret = zsock_getaddrinfo(NULL, "80", &hints, &res);
657 	zassert_equal(ret, 0, "Invalid result");
658 	zassert_not_null(res, "");
659 	zassert_is_null(res->ai_next, "");
660 	zassert_equal(res->ai_family, AF_INET, "");
661 	zassert_equal(res->ai_socktype, SOCK_DGRAM, "");
662 	zassert_equal(res->ai_protocol, IPPROTO_UDP, "");
663 	saddr = (struct sockaddr_in *)res->ai_addr;
664 	zassert_equal(saddr->sin_family, AF_INET, "");
665 	zassert_equal(saddr->sin_port, htons(80), "");
666 	zsock_freeaddrinfo(res);
667 }
668 
669 ZTEST_SUITE(net_socket_getaddrinfo, NULL, test_getaddrinfo_setup, NULL, NULL, NULL);
670