1 /*
2  * Copyright (c) 2017 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/logging/log.h>
8 LOG_MODULE_REGISTER(net_test, CONFIG_DNS_RESOLVER_LOG_LEVEL);
9 
10 #include <zephyr/types.h>
11 #include <stdbool.h>
12 #include <stddef.h>
13 #include <string.h>
14 #include <errno.h>
15 #include <zephyr/sys/printk.h>
16 #include <zephyr/random/random.h>
17 
18 #include <zephyr/ztest.h>
19 
20 #include <zephyr/net/ethernet.h>
21 #include <zephyr/net/dummy.h>
22 #include <zephyr/net/buf.h>
23 #include <zephyr/net/net_ip.h>
24 #include <zephyr/net/net_if.h>
25 #include <zephyr/net/dns_resolve.h>
26 
27 #define NET_LOG_ENABLED 1
28 #include "net_private.h"
29 
30 #if defined(CONFIG_DNS_RESOLVER_LOG_LEVEL_DBG)
31 #define DBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
32 #else
33 #define DBG(fmt, ...)
34 #endif
35 
36 #define NAME4 "4.zephyr.test"
37 #define NAME6 "6.zephyr.test"
38 #define NAME_IPV4 "192.0.2.1"
39 #define NAME_IPV6 "2001:db8::1"
40 
41 #define DNS_TIMEOUT 500 /* ms */
42 #define THREAD_SLEEP 10
43 
44 #if defined(CONFIG_NET_IPV6)
45 /* Interface 1 addresses */
46 static struct in6_addr my_addr1 = { { { 0x20, 0x01, 0x0d, 0xb8, 1, 0, 0, 0,
47 					0, 0, 0, 0, 0, 0, 0, 0x1 } } };
48 static struct in6_addr my_addr3 = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
49 					0, 0, 0, 0, 0, 0, 0, 0x1 } } };
50 
51 /* Extra address is assigned to ll_addr */
52 static struct in6_addr ll_addr = { { { 0xfe, 0x80, 0x43, 0xb8, 0, 0, 0, 0,
53 				       0, 0, 0, 0xf2, 0xaa, 0x29, 0x02,
54 				       0x04 } } };
55 #endif
56 
57 #if defined(CONFIG_NET_IPV4)
58 /* Interface 1 addresses */
59 static struct in_addr my_addr2 = { { { 192, 0, 2, 1 } } };
60 #endif
61 
62 static struct net_if *iface1;
63 
64 static bool test_failed;
65 static bool test_started;
66 static bool timeout_query;
67 static struct k_sem wait_data;
68 static struct k_sem wait_data2;
69 static uint16_t current_dns_id;
70 static struct dns_addrinfo addrinfo;
71 
72 #if defined(CONFIG_NET_IPV4) && defined(CONFIG_NET_IPV6)
73 #define EXPECTED_SERVER_COUNT CONFIG_DNS_RESOLVER_MAX_SERVERS
74 #else
75 #define EXPECTED_SERVER_COUNT (CONFIG_DNS_RESOLVER_MAX_SERVERS / 2)
76 #endif
77 
78 /* this must be higher that the DNS_TIMEOUT */
79 #define WAIT_TIME K_MSEC(DNS_TIMEOUT + 300)
80 
81 struct net_if_test {
82 	uint8_t idx;
83 	uint8_t mac_addr[sizeof(struct net_eth_addr)];
84 	struct net_linkaddr ll_addr;
85 };
86 
net_iface_get_mac(const struct device * dev)87 static uint8_t *net_iface_get_mac(const struct device *dev)
88 {
89 	struct net_if_test *data = dev->data;
90 
91 	if (data->mac_addr[2] == 0x00) {
92 		/* 00-00-5E-00-53-xx Documentation RFC 7042 */
93 		data->mac_addr[0] = 0x00;
94 		data->mac_addr[1] = 0x00;
95 		data->mac_addr[2] = 0x5E;
96 		data->mac_addr[3] = 0x00;
97 		data->mac_addr[4] = 0x53;
98 		data->mac_addr[5] = sys_rand8_get();
99 	}
100 
101 	data->ll_addr.addr = data->mac_addr;
102 	data->ll_addr.len = 6U;
103 
104 	return data->mac_addr;
105 }
106 
net_iface_init(struct net_if * iface)107 static void net_iface_init(struct net_if *iface)
108 {
109 	uint8_t *mac = net_iface_get_mac(net_if_get_device(iface));
110 
111 	net_if_set_link_addr(iface, mac, sizeof(struct net_eth_addr),
112 			     NET_LINK_ETHERNET);
113 
114 	net_if_flag_set(iface, NET_IF_IPV6_NO_ND);
115 }
116 
get_slot_by_id(struct dns_resolve_context * ctx,uint16_t dns_id)117 static inline int get_slot_by_id(struct dns_resolve_context *ctx,
118 				 uint16_t dns_id)
119 {
120 	int i;
121 
122 	for (i = 0; i < CONFIG_DNS_NUM_CONCUR_QUERIES; i++) {
123 		if (ctx->queries[i].cb && ctx->queries[i].id == dns_id) {
124 			return i;
125 		}
126 	}
127 
128 	return -1;
129 }
130 
sender_iface(const struct device * dev,struct net_pkt * pkt)131 static int sender_iface(const struct device *dev, struct net_pkt *pkt)
132 {
133 	if (!pkt->frags) {
134 		DBG("No data to send!\n");
135 		return -ENODATA;
136 	}
137 
138 	if (!timeout_query) {
139 		struct net_if_test *data = dev->data;
140 		struct dns_resolve_context *ctx;
141 		int slot;
142 
143 		if (net_if_get_by_iface(net_pkt_iface(pkt)) != data->idx) {
144 			DBG("Invalid interface %d index, expecting %d\n",
145 			    data->idx, net_if_get_by_iface(net_pkt_iface(pkt)));
146 			test_failed = true;
147 		}
148 
149 		ctx = dns_resolve_get_default();
150 
151 		slot = get_slot_by_id(ctx, current_dns_id);
152 		if (slot < 0) {
153 			DBG("Skipping this query dns id %u\n", current_dns_id);
154 			goto out;
155 		}
156 
157 		/* We need to cancel the query manually so that we
158 		 * will not get a timeout.
159 		 */
160 		k_work_cancel_delayable(&ctx->queries[slot].timer);
161 
162 		DBG("Calling cb %p with user data %p\n",
163 		    ctx->queries[slot].cb,
164 		    ctx->queries[slot].user_data);
165 
166 		ctx->queries[slot].cb(DNS_EAI_INPROGRESS,
167 				      &addrinfo,
168 				      ctx->queries[slot].user_data);
169 		ctx->queries[slot].cb(DNS_EAI_ALLDONE,
170 				      NULL,
171 				      ctx->queries[slot].user_data);
172 
173 		ctx->queries[slot].cb = NULL;
174 	}
175 
176 out:
177 	return 0;
178 }
179 
180 struct net_if_test net_iface1_data;
181 
182 static struct dummy_api net_iface_api = {
183 	.iface_api.init = net_iface_init,
184 	.send = sender_iface,
185 };
186 
187 #define _ETH_L2_LAYER DUMMY_L2
188 #define _ETH_L2_CTX_TYPE NET_L2_GET_CTX_TYPE(DUMMY_L2)
189 
190 NET_DEVICE_INIT_INSTANCE(net_iface1_test,
191 			 "iface1",
192 			 iface1,
193 			 NULL,
194 			 NULL,
195 			 &net_iface1_data,
196 			 NULL,
197 			 CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
198 			 &net_iface_api,
199 			 _ETH_L2_LAYER,
200 			 _ETH_L2_CTX_TYPE,
201 			 127);
202 
test_init(void)203 static void *test_init(void)
204 {
205 	struct net_if_addr *ifaddr;
206 
207 	/* The semaphore is there to wait the data to be received. */
208 	k_sem_init(&wait_data, 0, UINT_MAX);
209 	k_sem_init(&wait_data2, 0, UINT_MAX);
210 
211 	iface1 = net_if_get_by_index(0);
212 	zassert_is_null(iface1, "iface1");
213 
214 	iface1 = net_if_get_by_index(1);
215 
216 	((struct net_if_test *) net_if_get_device(iface1)->data)->idx =
217 		net_if_get_by_iface(iface1);
218 
219 #if defined(CONFIG_NET_IPV6)
220 	ifaddr = net_if_ipv6_addr_add(iface1, &my_addr1,
221 				      NET_ADDR_MANUAL, 0);
222 	if (!ifaddr) {
223 		DBG("Cannot add IPv6 address %s\n",
224 		       net_sprint_ipv6_addr(&my_addr1));
225 		zassert_not_null(ifaddr, "addr1");
226 
227 		return NULL;
228 	}
229 
230 	/* For testing purposes we need to set the adddresses preferred */
231 	ifaddr->addr_state = NET_ADDR_PREFERRED;
232 
233 	ifaddr = net_if_ipv6_addr_add(iface1, &ll_addr,
234 				      NET_ADDR_MANUAL, 0);
235 	if (!ifaddr) {
236 		DBG("Cannot add IPv6 address %s\n",
237 		       net_sprint_ipv6_addr(&ll_addr));
238 		zassert_not_null(ifaddr, "ll_addr");
239 
240 		return NULL;
241 	}
242 
243 	ifaddr->addr_state = NET_ADDR_PREFERRED;
244 #endif
245 
246 #if defined(CONFIG_NET_IPV4)
247 	ifaddr = net_if_ipv4_addr_add(iface1, &my_addr2,
248 				      NET_ADDR_MANUAL, 0);
249 	if (!ifaddr) {
250 		DBG("Cannot add IPv4 address %s\n",
251 		       net_sprint_ipv4_addr(&my_addr2));
252 		zassert_not_null(ifaddr, "addr2");
253 
254 		return NULL;
255 	}
256 
257 	ifaddr->addr_state = NET_ADDR_PREFERRED;
258 #endif
259 
260 	net_if_up(iface1);
261 
262 	/* The interface might receive data which might fail the checks
263 	 * in the iface sending function, so we need to reset the failure
264 	 * flag.
265 	 */
266 	test_failed = false;
267 
268 	test_started = true;
269 
270 	return NULL;
271 }
272 
dns_result_cb_dummy(enum dns_resolve_status status,struct dns_addrinfo * info,void * user_data)273 void dns_result_cb_dummy(enum dns_resolve_status status,
274 			 struct dns_addrinfo *info,
275 			 void *user_data)
276 {
277 	return;
278 }
279 
ZTEST(dns_resolve,test_dns_query_invalid_timeout)280 ZTEST(dns_resolve, test_dns_query_invalid_timeout)
281 {
282 	int ret;
283 
284 	ret = dns_get_addr_info(NAME6,
285 				DNS_QUERY_TYPE_AAAA,
286 				NULL,
287 				dns_result_cb_dummy,
288 				NULL,
289 				0);
290 	zassert_equal(ret, -EINVAL, "Wrong return code for timeout");
291 }
292 
ZTEST(dns_resolve,test_dns_query_invalid_context)293 ZTEST(dns_resolve, test_dns_query_invalid_context)
294 {
295 	int ret;
296 
297 	ret = dns_resolve_name(NULL,
298 			       NAME6,
299 			       DNS_QUERY_TYPE_AAAA,
300 			       NULL,
301 			       dns_result_cb_dummy,
302 			       NULL,
303 			       DNS_TIMEOUT);
304 	zassert_equal(ret, -EINVAL, "Wrong return code for context");
305 }
306 
ZTEST(dns_resolve,test_dns_query_invalid_callback)307 ZTEST(dns_resolve, test_dns_query_invalid_callback)
308 {
309 	int ret;
310 
311 	ret = dns_get_addr_info(NAME6,
312 				DNS_QUERY_TYPE_AAAA,
313 				NULL,
314 				NULL,
315 				NULL,
316 				DNS_TIMEOUT);
317 	zassert_equal(ret, -EINVAL, "Wrong return code for callback");
318 }
319 
ZTEST(dns_resolve,test_dns_query_invalid_query)320 ZTEST(dns_resolve, test_dns_query_invalid_query)
321 {
322 	int ret;
323 
324 	ret = dns_get_addr_info(NULL,
325 				DNS_QUERY_TYPE_AAAA,
326 				NULL,
327 				dns_result_cb_dummy,
328 				NULL,
329 				DNS_TIMEOUT);
330 	zassert_equal(ret, -EINVAL, "Wrong return code for query");
331 }
332 
dns_result_cb_timeout(enum dns_resolve_status status,struct dns_addrinfo * info,void * user_data)333 void dns_result_cb_timeout(enum dns_resolve_status status,
334 			   struct dns_addrinfo *info,
335 			   void *user_data)
336 {
337 	int expected_status = POINTER_TO_INT(user_data);
338 
339 	if (expected_status != status) {
340 		DBG("Result status %d\n", status);
341 		DBG("Expected status %d\n", expected_status);
342 
343 		zassert_equal(expected_status, status, "Invalid status");
344 	}
345 
346 	k_sem_give(&wait_data);
347 }
348 
ZTEST(dns_resolve,test_dns_query_server_count)349 ZTEST(dns_resolve, test_dns_query_server_count)
350 {
351 	struct dns_resolve_context *ctx = dns_resolve_get_default();
352 	int i, count = 0;
353 
354 	for (i = 0; i < EXPECTED_SERVER_COUNT; i++) {
355 		if (ctx->state != DNS_RESOLVE_CONTEXT_ACTIVE) {
356 			continue;
357 		}
358 
359 		if (ctx->servers[i].sock < 0) {
360 			continue;
361 		}
362 
363 		count++;
364 	}
365 
366 	zassert_equal(count, EXPECTED_SERVER_COUNT,
367 		      "Invalid number of servers (%d vs %d)",
368 		      count, EXPECTED_SERVER_COUNT);
369 }
370 
ZTEST(dns_resolve,test_dns_query_ipv4_server_count)371 ZTEST(dns_resolve, test_dns_query_ipv4_server_count)
372 {
373 	struct dns_resolve_context *ctx = dns_resolve_get_default();
374 	int i, count = 0, port = 0;
375 
376 	if (!IS_ENABLED(CONFIG_NET_IPV4)) {
377 		return;
378 	}
379 
380 	for (i = 0; i < CONFIG_DNS_RESOLVER_MAX_SERVERS; i++) {
381 		if (ctx->state != DNS_RESOLVE_CONTEXT_ACTIVE) {
382 			continue;
383 		}
384 
385 		if (ctx->servers[i].sock < 0) {
386 			continue;
387 		}
388 
389 		if (ctx->servers[i].dns_server.sa_family == AF_INET6) {
390 			continue;
391 		}
392 
393 		count++;
394 
395 		if (net_sin(&ctx->servers[i].dns_server)->sin_port ==
396 		    ntohs(53)) {
397 			port++;
398 		}
399 	}
400 
401 	zassert_equal(count, 2, "Invalid number of IPv4 servers");
402 	zassert_equal(port, 1, "Invalid number of IPv4 servers with port 53");
403 }
404 
ZTEST(dns_resolve,test_dns_query_ipv6_server_count)405 ZTEST(dns_resolve, test_dns_query_ipv6_server_count)
406 {
407 	struct dns_resolve_context *ctx = dns_resolve_get_default();
408 	int i, count = 0, port = 0;
409 
410 	for (i = 0; i < CONFIG_DNS_RESOLVER_MAX_SERVERS; i++) {
411 		if (ctx->state != DNS_RESOLVE_CONTEXT_ACTIVE) {
412 			continue;
413 		}
414 
415 		if (ctx->servers[i].sock < 0) {
416 			continue;
417 		}
418 
419 		if (ctx->servers[i].dns_server.sa_family == AF_INET) {
420 			continue;
421 		}
422 
423 		count++;
424 
425 		if (net_sin6(&ctx->servers[i].dns_server)->sin6_port ==
426 		    ntohs(53)) {
427 			port++;
428 		}
429 	}
430 
431 #if defined(CONFIG_NET_IPV6)
432 	zassert_equal(count, 2, "Invalid number of IPv6 servers");
433 	zassert_equal(port, 1, "Invalid number of IPv6 servers with port 53");
434 #else
435 	zassert_equal(count, 0, "Invalid number of IPv6 servers");
436 	zassert_equal(port, 0, "Invalid number of IPv6 servers with port 53");
437 #endif
438 }
439 
ZTEST(dns_resolve,test_dns_query_too_many)440 ZTEST(dns_resolve, test_dns_query_too_many)
441 {
442 	int expected_status = DNS_EAI_CANCELED;
443 	int ret;
444 
445 	timeout_query = true;
446 
447 	ret = dns_get_addr_info(NAME4,
448 				DNS_QUERY_TYPE_A,
449 				NULL,
450 				dns_result_cb_timeout,
451 				INT_TO_POINTER(expected_status),
452 				DNS_TIMEOUT);
453 	zassert_equal(ret, 0, "Cannot create IPv4 query");
454 
455 	ret = dns_get_addr_info(NAME4,
456 				DNS_QUERY_TYPE_A,
457 				NULL,
458 				dns_result_cb_dummy,
459 				INT_TO_POINTER(expected_status),
460 				DNS_TIMEOUT);
461 	zassert_equal(ret, -EAGAIN, "Should have run out of space");
462 
463 	if (k_sem_take(&wait_data, WAIT_TIME)) {
464 		zassert_true(false, "Timeout while waiting data");
465 	}
466 
467 	timeout_query = false;
468 }
469 
ZTEST(dns_resolve,test_dns_query_ipv4_timeout)470 ZTEST(dns_resolve, test_dns_query_ipv4_timeout)
471 {
472 	int expected_status = DNS_EAI_CANCELED;
473 	int ret;
474 
475 	timeout_query = true;
476 
477 	ret = dns_get_addr_info(NAME4,
478 				DNS_QUERY_TYPE_A,
479 				NULL,
480 				dns_result_cb_timeout,
481 				INT_TO_POINTER(expected_status),
482 				DNS_TIMEOUT);
483 	zassert_equal(ret, 0, "Cannot create IPv4 query");
484 
485 	if (k_sem_take(&wait_data, WAIT_TIME)) {
486 		zassert_true(false, "Timeout while waiting data");
487 	}
488 
489 	timeout_query = false;
490 }
491 
ZTEST(dns_resolve,test_dns_query_ipv6_timeout)492 ZTEST(dns_resolve, test_dns_query_ipv6_timeout)
493 {
494 	int expected_status = DNS_EAI_CANCELED;
495 	int ret;
496 
497 	timeout_query = true;
498 
499 	ret = dns_get_addr_info(NAME6,
500 				DNS_QUERY_TYPE_AAAA,
501 				NULL,
502 				dns_result_cb_timeout,
503 				INT_TO_POINTER(expected_status),
504 				DNS_TIMEOUT);
505 	zassert_equal(ret, 0, "Cannot create IPv6 query");
506 
507 	if (k_sem_take(&wait_data, WAIT_TIME)) {
508 		zassert_true(false, "Timeout while waiting data");
509 	}
510 
511 	timeout_query = false;
512 }
513 
verify_cancelled(void)514 static void verify_cancelled(void)
515 {
516 	struct dns_resolve_context *ctx = dns_resolve_get_default();
517 	int i, count = 0, timer_not_stopped = 0;
518 
519 	for (i = 0; i < CONFIG_DNS_NUM_CONCUR_QUERIES; i++) {
520 		if (ctx->queries[i].cb) {
521 			count++;
522 		}
523 
524 		if (k_work_delayable_busy_get(&ctx->queries[i].timer) != 0) {
525 			timer_not_stopped++;
526 		}
527 	}
528 
529 	zassert_equal(count, 0, "Not all pending queries vere cancelled");
530 	zassert_equal(timer_not_stopped, 0, "Not all timers vere cancelled");
531 }
532 
ZTEST(dns_resolve,test_dns_query_ipv4_cancel)533 ZTEST(dns_resolve, test_dns_query_ipv4_cancel)
534 {
535 	int expected_status = DNS_EAI_CANCELED;
536 	uint16_t dns_id;
537 	int ret;
538 
539 	timeout_query = true;
540 
541 	ret = dns_get_addr_info(NAME4,
542 				DNS_QUERY_TYPE_A,
543 				&dns_id,
544 				dns_result_cb_timeout,
545 				INT_TO_POINTER(expected_status),
546 				DNS_TIMEOUT);
547 	zassert_equal(ret, 0, "Cannot create IPv4 query");
548 
549 	ret = dns_cancel_addr_info(dns_id);
550 	zassert_equal(ret, 0, "Cannot cancel IPv4 query");
551 
552 	if (k_sem_take(&wait_data, WAIT_TIME)) {
553 		zassert_true(false, "Timeout while waiting data");
554 	}
555 
556 	verify_cancelled();
557 }
558 
ZTEST(dns_resolve,test_dns_query_ipv6_cancel)559 ZTEST(dns_resolve, test_dns_query_ipv6_cancel)
560 {
561 	int expected_status = DNS_EAI_CANCELED;
562 	uint16_t dns_id;
563 	int ret;
564 
565 	timeout_query = true;
566 
567 	ret = dns_get_addr_info(NAME6,
568 				DNS_QUERY_TYPE_AAAA,
569 				&dns_id,
570 				dns_result_cb_timeout,
571 				INT_TO_POINTER(expected_status),
572 				DNS_TIMEOUT);
573 	zassert_equal(ret, 0, "Cannot create IPv6 query");
574 
575 	ret = dns_cancel_addr_info(dns_id);
576 	zassert_equal(ret, 0, "Cannot cancel IPv6 query");
577 
578 	if (k_sem_take(&wait_data, WAIT_TIME)) {
579 		zassert_true(false, "Timeout while waiting data");
580 	}
581 
582 	verify_cancelled();
583 }
584 
585 struct expected_status {
586 	int status1;
587 	int status2;
588 	const char *caller;
589 };
590 
dns_result_cb(enum dns_resolve_status status,struct dns_addrinfo * info,void * user_data)591 void dns_result_cb(enum dns_resolve_status status,
592 		   struct dns_addrinfo *info,
593 		   void *user_data)
594 {
595 	struct expected_status *expected = user_data;
596 
597 	if (status != expected->status1 && status != expected->status2) {
598 		DBG("Result status %d\n", status);
599 		DBG("Expected status1 %d\n", expected->status1);
600 		DBG("Expected status2 %d\n", expected->status2);
601 		DBG("Caller %s\n", expected->caller);
602 
603 		zassert_true(false, "Invalid status");
604 	}
605 
606 	k_sem_give(&wait_data2);
607 }
608 
ZTEST(dns_resolve,test_dns_query_ipv4)609 ZTEST(dns_resolve, test_dns_query_ipv4)
610 {
611 	struct expected_status status = {
612 		.status1 = DNS_EAI_INPROGRESS,
613 		.status2 = DNS_EAI_ALLDONE,
614 		.caller = __func__,
615 	};
616 	int ret;
617 
618 	timeout_query = false;
619 
620 	ret = dns_get_addr_info(NAME4,
621 				DNS_QUERY_TYPE_A,
622 				&current_dns_id,
623 				dns_result_cb,
624 				&status,
625 				DNS_TIMEOUT);
626 	zassert_equal(ret, 0, "Cannot create IPv4 query");
627 
628 	DBG("Query id %u\n", current_dns_id);
629 
630 	/* Let the network stack to proceed */
631 	k_msleep(THREAD_SLEEP);
632 
633 	if (k_sem_take(&wait_data2, WAIT_TIME)) {
634 		zassert_true(false, "Timeout while waiting data");
635 	}
636 }
637 
ZTEST(dns_resolve,test_dns_query_ipv6)638 ZTEST(dns_resolve, test_dns_query_ipv6)
639 {
640 	struct expected_status status = {
641 		.status1 = DNS_EAI_INPROGRESS,
642 		.status2 = DNS_EAI_ALLDONE,
643 		.caller = __func__,
644 	};
645 	int ret;
646 
647 	timeout_query = false;
648 
649 	ret = dns_get_addr_info(NAME6,
650 				DNS_QUERY_TYPE_AAAA,
651 				&current_dns_id,
652 				dns_result_cb,
653 				&status,
654 				DNS_TIMEOUT);
655 	zassert_equal(ret, 0, "Cannot create IPv6 query");
656 
657 	DBG("Query id %u\n", current_dns_id);
658 
659 	k_yield(); /* mandatory so that net_if send func gets to run */
660 
661 	if (k_sem_take(&wait_data2, WAIT_TIME)) {
662 		zassert_true(false, "Timeout while waiting data");
663 	}
664 }
665 
666 struct expected_addr_status {
667 	struct sockaddr addr;
668 	int status1;
669 	int status2;
670 	const char *caller;
671 };
672 
dns_result_numeric_cb(enum dns_resolve_status status,struct dns_addrinfo * info,void * user_data)673 void dns_result_numeric_cb(enum dns_resolve_status status,
674 			   struct dns_addrinfo *info,
675 			   void *user_data)
676 {
677 	struct expected_addr_status *expected = user_data;
678 
679 	if (status != expected->status1 && status != expected->status2) {
680 		DBG("Result status %d\n", status);
681 		DBG("Expected status1 %d\n", expected->status1);
682 		DBG("Expected status2 %d\n", expected->status2);
683 		DBG("Caller %s\n", expected->caller);
684 
685 		zassert_true(false, "Invalid status");
686 	}
687 
688 	if (info && info->ai_family == AF_INET) {
689 #if defined(CONFIG_NET_IPV4)
690 		if (net_ipv4_addr_cmp(&net_sin(&info->ai_addr)->sin_addr,
691 				      &my_addr2) != true) {
692 			zassert_true(false, "IPv4 address does not match");
693 		}
694 #endif
695 	}
696 
697 	if (info && info->ai_family == AF_INET6) {
698 #if defined(CONFIG_NET_IPV6)
699 		if (net_ipv6_addr_cmp(&net_sin6(&info->ai_addr)->sin6_addr,
700 				      &my_addr3) != true) {
701 			zassert_true(false, "IPv6 address does not match");
702 		}
703 #endif
704 	}
705 
706 	k_sem_give(&wait_data2);
707 }
708 
ZTEST(dns_resolve,test_dns_query_ipv4_numeric)709 ZTEST(dns_resolve, test_dns_query_ipv4_numeric)
710 {
711 	struct expected_addr_status status = {
712 		.status1 = DNS_EAI_INPROGRESS,
713 		.status2 = DNS_EAI_ALLDONE,
714 		.caller = __func__,
715 	};
716 	int ret;
717 
718 	timeout_query = false;
719 
720 	ret = dns_get_addr_info(NAME_IPV4,
721 				DNS_QUERY_TYPE_A,
722 				&current_dns_id,
723 				dns_result_numeric_cb,
724 				&status,
725 				DNS_TIMEOUT);
726 	zassert_equal(ret, 0, "Cannot create IPv4 numeric query");
727 
728 	DBG("Query id %u\n", current_dns_id);
729 
730 	k_yield(); /* mandatory so that net_if send func gets to run */
731 
732 	if (k_sem_take(&wait_data2, WAIT_TIME)) {
733 		zassert_true(false, "Timeout while waiting data");
734 	}
735 }
736 
ZTEST(dns_resolve,test_dns_query_ipv6_numeric)737 ZTEST(dns_resolve, test_dns_query_ipv6_numeric)
738 {
739 	struct expected_addr_status status = {
740 		.status1 = DNS_EAI_INPROGRESS,
741 		.status2 = DNS_EAI_ALLDONE,
742 		.caller = __func__,
743 	};
744 	int ret;
745 
746 	timeout_query = false;
747 
748 	ret = dns_get_addr_info(NAME_IPV6,
749 				DNS_QUERY_TYPE_AAAA,
750 				&current_dns_id,
751 				dns_result_numeric_cb,
752 				&status,
753 				DNS_TIMEOUT);
754 	zassert_equal(ret, 0, "Cannot create IPv6 query");
755 
756 	DBG("Query id %u\n", current_dns_id);
757 
758 	k_yield(); /* mandatory so that net_if send func gets to run */
759 
760 	if (k_sem_take(&wait_data2, WAIT_TIME)) {
761 		zassert_true(false, "Timeout while waiting data");
762 	}
763 }
764 
765 ZTEST_SUITE(dns_resolve, NULL, test_init, NULL, NULL, NULL);
766