xref: /libuv/test/test-ip-name.c (revision 011a1ac1)
1 /* Copyright The libuv project and contributors. All rights reserved.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE.
20  */
21 
22 #include "uv.h"
23 #include "task.h"
24 
25 #include <stdio.h>
26 #include <string.h>
27 
28 union TestAddr {
29     struct sockaddr addr;
30     struct sockaddr_in addr4;
31     struct sockaddr_in6 addr6;
32 };
33 
34 
TEST_IMPL(ip_name)35 TEST_IMPL(ip_name) {
36     char dst[INET6_ADDRSTRLEN];
37     union TestAddr test_addr;
38     struct sockaddr* addr = &test_addr.addr;
39     struct sockaddr_in* addr4 = &test_addr.addr4;
40     struct sockaddr_in6* addr6 = &test_addr.addr6;
41 
42     /* test ip4_name */
43     ASSERT_OK(uv_ip4_addr("192.168.0.1", TEST_PORT, addr4));
44     ASSERT_OK(uv_ip4_name(addr4, dst, INET_ADDRSTRLEN));
45     ASSERT_OK(strcmp("192.168.0.1", dst));
46 
47     ASSERT_OK(uv_ip_name(addr, dst, INET_ADDRSTRLEN));
48     ASSERT_OK(strcmp("192.168.0.1", dst));
49 
50     /* test ip6_name */
51     ASSERT_OK(uv_ip6_addr("fe80::2acf:daff:fedd:342a", TEST_PORT, addr6));
52     ASSERT_OK(uv_ip6_name(addr6, dst, INET6_ADDRSTRLEN));
53     ASSERT_OK(strcmp("fe80::2acf:daff:fedd:342a", dst));
54 
55     ASSERT_OK(uv_ip_name(addr, dst, INET6_ADDRSTRLEN));
56     ASSERT_OK(strcmp("fe80::2acf:daff:fedd:342a", dst));
57 
58     /* test other sa_family */
59     addr->sa_family = AF_UNIX;
60     /* size is not a concern here */
61     ASSERT_EQ(UV_EAFNOSUPPORT, uv_ip_name(addr, dst, INET6_ADDRSTRLEN));
62 
63     MAKE_VALGRIND_HAPPY(uv_default_loop());
64     return 0;
65 }
66