1 /*
2 * Copyright (c) 2019 Linaro Limited
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
13 #include <zephyr/net/socket.h>
14
ZTEST_USER(net_socket_getnameinfo,test_getnameinfo_ipv4)15 ZTEST_USER(net_socket_getnameinfo, test_getnameinfo_ipv4)
16 {
17 struct sockaddr_in saddr;
18 char host[80];
19 char serv[10];
20 int ret;
21
22 memset(&saddr, 0, sizeof(saddr));
23 saddr.sin_family = AF_INET;
24
25 ret = zsock_getnameinfo((struct sockaddr *)&saddr, sizeof(saddr),
26 host, sizeof(host), serv, sizeof(serv), 0);
27 zassert_equal(ret, 0, "");
28
29 printk("%s %s\n", host, serv);
30 zassert_equal(strcmp(host, "0.0.0.0"), 0, "");
31 zassert_equal(strcmp(serv, "0"), 0, "");
32
33 saddr.sin_port = htons(1234);
34 saddr.sin_addr.s_addr = htonl(0x7f000001);
35
36 ret = zsock_getnameinfo((struct sockaddr *)&saddr, sizeof(saddr),
37 host, sizeof(host), serv, sizeof(serv), 0);
38 zassert_equal(ret, 0, "");
39
40 printk("%s %s\n", host, serv);
41 zassert_equal(strcmp(host, "127.0.0.1"), 0, "");
42 zassert_equal(strcmp(serv, "1234"), 0, "");
43 }
44
ZTEST_USER(net_socket_getnameinfo,test_getnameinfo_ipv6)45 ZTEST_USER(net_socket_getnameinfo, test_getnameinfo_ipv6)
46 {
47 struct sockaddr_in6 saddr;
48 char host[80];
49 char serv[10];
50 int ret;
51
52 memset(&saddr, 0, sizeof(saddr));
53 saddr.sin6_family = AF_INET6;
54
55 ret = zsock_getnameinfo((struct sockaddr *)&saddr, sizeof(saddr),
56 host, sizeof(host), serv, sizeof(serv), 0);
57 zassert_equal(ret, 0, "");
58
59 printk("%s %s\n", host, serv);
60 zassert_equal(strcmp(host, "::"), 0, "");
61 zassert_equal(strcmp(serv, "0"), 0, "");
62
63 saddr.sin6_port = htons(4321);
64 saddr.sin6_addr.s6_addr[0] = 0xff;
65 saddr.sin6_addr.s6_addr[1] = 0x55;
66 saddr.sin6_addr.s6_addr[15] = 0x11;
67
68 ret = zsock_getnameinfo((struct sockaddr *)&saddr, sizeof(saddr),
69 host, sizeof(host), serv, sizeof(serv), 0);
70 zassert_equal(ret, 0, "");
71
72 printk("%s %s\n", host, serv);
73 zassert_equal(strcmp(host, "ff55::11"), 0, "");
74 zassert_equal(strcmp(serv, "4321"), 0, "");
75 }
76
77 ZTEST_SUITE(net_socket_getnameinfo, NULL, NULL, NULL, NULL, NULL);
78