1 /*
2 * tests/check-addr.c nl_addr unit tests
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation version 2.1
7 * of the License.
8 *
9 * Copyright (c) 2013 Thomas Graf <tgraf@suug.ch>
10 */
11
12 #include <check.h>
13 #include <netlink/addr.h>
14
15 #include "util.h"
16
START_TEST(addr_alloc)17 START_TEST(addr_alloc)
18 {
19 struct nl_addr *addr;
20
21 addr = nl_addr_alloc(16);
22 fail_if(addr == NULL,
23 "Allocation should not return NULL");
24
25 fail_if(nl_addr_iszero(addr) == 0,
26 "New empty address should be all zeros");
27
28 fail_if(nl_addr_get_family(addr) != AF_UNSPEC,
29 "New empty address should have family AF_UNSPEC");
30
31 fail_if(nl_addr_get_prefixlen(addr) != 0,
32 "New empty address should have prefix length 0");
33
34 fail_if(nl_addr_shared(addr),
35 "New empty address should not be shared");
36
37 fail_if(nl_addr_get(addr) != addr,
38 "nl_addr_get() should return pointer to address");
39
40 fail_if(nl_addr_shared(addr) == 0,
41 "Address should be shared after call to nl_addr_get()");
42
43 nl_addr_put(addr);
44
45 fail_if(nl_addr_shared(addr),
46 "Address should not be shared after call to nl_addr_put()");
47
48 fail_if(nl_addr_fill_sockaddr(addr, NULL, 0) == 0,
49 "Socket address filling should fail for empty address");
50
51 nl_addr_put(addr);
52 }
53 END_TEST
54
START_TEST(addr_binary_addr)55 START_TEST(addr_binary_addr)
56 {
57 struct nl_addr *addr, *addr2;
58 char baddr[4] = { 0x1, 0x2, 0x3, 0x4 };
59 char baddr2[6] = { 0x1, 0x2, 0x3, 0x4, 0x5, 0x6 };
60
61 addr = nl_addr_alloc(4);
62 fail_if(addr == NULL,
63 "Allocation should not return NULL");
64
65 fail_if(nl_addr_set_binary_addr(addr, baddr, 4) < 0,
66 "Valid binary address should be settable");
67
68 fail_if(nl_addr_get_prefixlen(addr) != 0,
69 "Prefix length should be unchanged after nl_addr_set_binary_addr()");
70
71 fail_if(nl_addr_get_len(addr) != 4,
72 "Address length should be 4");
73
74 fail_if(nl_addr_set_binary_addr(addr, baddr2, 6) == 0,
75 "Should not be able to set binary address exceeding maximum length");
76
77 fail_if(nl_addr_get_len(addr) != 4,
78 "Address length should still be 4");
79
80 fail_if(nl_addr_guess_family(addr) != AF_INET,
81 "Binary address of length 4 should be guessed as AF_INET");
82
83 fail_if(memcmp(baddr, nl_addr_get_binary_addr(addr), 4) != 0,
84 "Binary address mismatches");
85
86 addr2 = nl_addr_build(AF_UNSPEC, baddr, 4);
87 fail_if(addr2 == NULL,
88 "Building of address should not fail");
89
90 nl_addr_set_prefixlen(addr, 32);
91 fail_if(nl_addr_get_prefixlen(addr) != 32,
92 "Prefix length should be successful changed after nl_addr_set_prefixlen()");
93
94 fail_if(nl_addr_cmp(addr, addr2),
95 "Addresses built from same binary address should match");
96
97 nl_addr_put(addr);
98 nl_addr_put(addr2);
99 }
100 END_TEST
101
START_TEST(addr_parse4)102 START_TEST(addr_parse4)
103 {
104 struct nl_addr *addr4, *clone;
105 struct sockaddr_in sin;
106 socklen_t len = sizeof(sin);
107 char *addr_str = "10.0.0.1/16";
108 char buf[128];
109
110 fail_if(nl_addr_parse(addr_str, AF_INET6, &addr4) == 0,
111 "Should not be able to parse IPv4 address in IPv6 mode");
112
113 fail_if(nl_addr_parse(addr_str, AF_UNSPEC, &addr4) != 0,
114 "Should be able to parse \"%s\"", addr_str);
115
116 fail_if(nl_addr_get_family(addr4) != AF_INET,
117 "Address family should be AF_INET");
118
119 fail_if(nl_addr_get_prefixlen(addr4) != 16,
120 "Prefix length should be 16");
121
122 fail_if(nl_addr_iszero(addr4),
123 "Address should not be all zeroes");
124
125 clone = nl_addr_clone(addr4);
126 fail_if(clone == NULL,
127 "Cloned address should not be NULL");
128
129 fail_if(nl_addr_cmp(addr4, clone) != 0,
130 "Cloned address should not mismatch original");
131
132 fail_if(nl_addr_fill_sockaddr(addr4, (struct sockaddr *) &sin, &len) != 0,
133 "Should be able to fill socketaddr");
134
135 fail_if(strcmp(nl_addr2str(addr4, buf, sizeof(buf)), addr_str),
136 "Address translated back to string does not match original");
137
138 nl_addr_put(addr4);
139 nl_addr_put(clone);
140 }
141 END_TEST
142
START_TEST(addr_parse6)143 START_TEST(addr_parse6)
144 {
145 struct nl_addr *addr6, *clone;
146 struct sockaddr_in6 sin;
147 socklen_t len = sizeof(sin);
148 char *addr_str = "2001:1:2::3/64";
149 char buf[128];
150
151 fail_if(nl_addr_parse(addr_str, AF_INET, &addr6) == 0,
152 "Should not be able to parse IPv6 address in IPv4 mode");
153
154 fail_if(nl_addr_parse(addr_str, AF_UNSPEC, &addr6) != 0,
155 "Should be able to parse \"%s\"", addr_str);
156
157 fail_if(nl_addr_get_family(addr6) != AF_INET6,
158 "Address family should be AF_INET6");
159
160 fail_if(nl_addr_get_prefixlen(addr6) != 64,
161 "Prefix length should be 64");
162
163 fail_if(nl_addr_iszero(addr6),
164 "Address should not be all zeroes");
165
166 clone = nl_addr_clone(addr6);
167 fail_if(clone == NULL,
168 "Cloned address should not be NULL");
169
170 fail_if(nl_addr_cmp(addr6, clone) != 0,
171 "Cloned address should not mismatch original");
172
173 fail_if(nl_addr_fill_sockaddr(addr6, (struct sockaddr *) &sin, &len) != 0,
174 "Should be able to fill socketaddr");
175
176 fail_if(strcmp(nl_addr2str(addr6, buf, sizeof(buf)), addr_str),
177 "Address translated back to string does not match original");
178
179 nl_addr_put(addr6);
180 nl_addr_put(clone);
181 }
182 END_TEST
183
START_TEST(addr_info)184 START_TEST(addr_info)
185 {
186 struct nl_addr *addr;
187 char *addr_str = "127.0.0.1";
188 struct addrinfo *result;
189
190 fail_if(nl_addr_parse(addr_str, AF_UNSPEC, &addr) != 0,
191 "Parsing of valid address should not fail");
192
193 fail_if(nl_addr_info(addr, &result) != 0,
194 "getaddrinfo() on loopback address should work");
195
196 freeaddrinfo(result);
197 nl_addr_put(addr);
198 }
199 END_TEST
200
make_nl_addr_suite(void)201 Suite *make_nl_addr_suite(void)
202 {
203 Suite *suite = suite_create("Abstract addresses");
204
205 TCase *tc_addr = tcase_create("Core");
206 tcase_add_test(tc_addr, addr_alloc);
207 tcase_add_test(tc_addr, addr_binary_addr);
208 tcase_add_test(tc_addr, addr_parse4);
209 tcase_add_test(tc_addr, addr_parse6);
210 tcase_add_test(tc_addr, addr_info);
211 suite_add_tcase(suite, tc_addr);
212
213 return suite;
214 }
215