1c43e99fdSEd Maste /*
2c43e99fdSEd Maste * Copyright (c) 2009-2012 Nick Mathewson and Niels Provos
3c43e99fdSEd Maste *
4c43e99fdSEd Maste * Redistribution and use in source and binary forms, with or without
5c43e99fdSEd Maste * modification, are permitted provided that the following conditions
6c43e99fdSEd Maste * are met:
7c43e99fdSEd Maste * 1. Redistributions of source code must retain the above copyright
8c43e99fdSEd Maste * notice, this list of conditions and the following disclaimer.
9c43e99fdSEd Maste * 2. Redistributions in binary form must reproduce the above copyright
10c43e99fdSEd Maste * notice, this list of conditions and the following disclaimer in the
11c43e99fdSEd Maste * documentation and/or other materials provided with the distribution.
12c43e99fdSEd Maste * 3. The name of the author may not be used to endorse or promote products
13c43e99fdSEd Maste * derived from this software without specific prior written permission.
14c43e99fdSEd Maste *
15c43e99fdSEd Maste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16c43e99fdSEd Maste * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17c43e99fdSEd Maste * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18c43e99fdSEd Maste * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19c43e99fdSEd Maste * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20c43e99fdSEd Maste * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21c43e99fdSEd Maste * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22c43e99fdSEd Maste * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23c43e99fdSEd Maste * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24c43e99fdSEd Maste * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25c43e99fdSEd Maste */
26*b50261e2SCy Schubert
27*b50261e2SCy Schubert /** For event_debug() usage/coverage */
28*b50261e2SCy Schubert #define EVENT_VISIBILITY_WANT_DLLIMPORT
29*b50261e2SCy Schubert
30c43e99fdSEd Maste #include "../util-internal.h"
31c43e99fdSEd Maste
32c43e99fdSEd Maste #ifdef _WIN32
33c43e99fdSEd Maste #include <winsock2.h>
34c43e99fdSEd Maste #include <windows.h>
35c43e99fdSEd Maste #include <ws2tcpip.h>
36c43e99fdSEd Maste #endif
37c43e99fdSEd Maste
38c43e99fdSEd Maste #include "event2/event-config.h"
39c43e99fdSEd Maste
40c43e99fdSEd Maste #include <sys/types.h>
41c43e99fdSEd Maste
42c43e99fdSEd Maste #ifndef _WIN32
43c43e99fdSEd Maste #include <sys/socket.h>
44c43e99fdSEd Maste #include <netinet/in.h>
45c43e99fdSEd Maste #include <arpa/inet.h>
46c43e99fdSEd Maste #include <unistd.h>
47c43e99fdSEd Maste #endif
48c43e99fdSEd Maste #ifdef EVENT__HAVE_NETINET_IN6_H
49c43e99fdSEd Maste #include <netinet/in6.h>
50c43e99fdSEd Maste #endif
51c43e99fdSEd Maste #ifdef EVENT__HAVE_SYS_WAIT_H
52c43e99fdSEd Maste #include <sys/wait.h>
53c43e99fdSEd Maste #endif
54c43e99fdSEd Maste #include <signal.h>
55c43e99fdSEd Maste #include <stdio.h>
56c43e99fdSEd Maste #include <stdlib.h>
57c43e99fdSEd Maste #include <string.h>
58c43e99fdSEd Maste
59c43e99fdSEd Maste #include "event2/event.h"
60c43e99fdSEd Maste #include "event2/util.h"
61c43e99fdSEd Maste #include "../ipv6-internal.h"
62c43e99fdSEd Maste #include "../log-internal.h"
63c43e99fdSEd Maste #include "../strlcpy-internal.h"
64c43e99fdSEd Maste #include "../mm-internal.h"
65c43e99fdSEd Maste #include "../time-internal.h"
66c43e99fdSEd Maste
67c43e99fdSEd Maste #include "regress.h"
68c43e99fdSEd Maste
69c43e99fdSEd Maste enum entry_status { NORMAL, CANONICAL, BAD };
70c43e99fdSEd Maste
71c43e99fdSEd Maste /* This is a big table of results we expect from generating and parsing */
72c43e99fdSEd Maste static struct ipv4_entry {
73c43e99fdSEd Maste const char *addr;
74c43e99fdSEd Maste ev_uint32_t res;
75c43e99fdSEd Maste enum entry_status status;
76c43e99fdSEd Maste } ipv4_entries[] = {
77c43e99fdSEd Maste { "1.2.3.4", 0x01020304u, CANONICAL },
78c43e99fdSEd Maste { "255.255.255.255", 0xffffffffu, CANONICAL },
79c43e99fdSEd Maste { "256.0.0.0", 0, BAD },
80c43e99fdSEd Maste { "ABC", 0, BAD },
81c43e99fdSEd Maste { "1.2.3.4.5", 0, BAD },
82c43e99fdSEd Maste { "176.192.208.244", 0xb0c0d0f4, CANONICAL },
83c43e99fdSEd Maste { NULL, 0, BAD },
84c43e99fdSEd Maste };
85c43e99fdSEd Maste
86c43e99fdSEd Maste static struct ipv6_entry {
87c43e99fdSEd Maste const char *addr;
88c43e99fdSEd Maste ev_uint32_t res[4];
89c43e99fdSEd Maste enum entry_status status;
90c43e99fdSEd Maste } ipv6_entries[] = {
91c43e99fdSEd Maste { "::", { 0, 0, 0, 0, }, CANONICAL },
92c43e99fdSEd Maste { "0:0:0:0:0:0:0:0", { 0, 0, 0, 0, }, NORMAL },
93c43e99fdSEd Maste { "::1", { 0, 0, 0, 1, }, CANONICAL },
94c43e99fdSEd Maste { "::1.2.3.4", { 0, 0, 0, 0x01020304, }, CANONICAL },
95c43e99fdSEd Maste { "ffff:1::", { 0xffff0001u, 0, 0, 0, }, CANONICAL },
96c43e99fdSEd Maste { "ffff:0000::", { 0xffff0000u, 0, 0, 0, }, NORMAL },
97c43e99fdSEd Maste { "ffff::1234", { 0xffff0000u, 0, 0, 0x1234, }, CANONICAL },
98c43e99fdSEd Maste { "0102::1.2.3.4", {0x01020000u, 0, 0, 0x01020304u }, NORMAL },
99c43e99fdSEd Maste { "::9:c0a8:1:1", { 0, 0, 0x0009c0a8u, 0x00010001u }, CANONICAL },
100c43e99fdSEd Maste { "::ffff:1.2.3.4", { 0, 0, 0x000ffffu, 0x01020304u }, CANONICAL },
101c43e99fdSEd Maste { "FFFF::", { 0xffff0000u, 0, 0, 0 }, NORMAL },
102c43e99fdSEd Maste { "foobar.", { 0, 0, 0, 0 }, BAD },
103c43e99fdSEd Maste { "foobar", { 0, 0, 0, 0 }, BAD },
104c43e99fdSEd Maste { "fo:obar", { 0, 0, 0, 0 }, BAD },
105c43e99fdSEd Maste { "ffff", { 0, 0, 0, 0 }, BAD },
106c43e99fdSEd Maste { "fffff::", { 0, 0, 0, 0 }, BAD },
107c43e99fdSEd Maste { "fffff::", { 0, 0, 0, 0 }, BAD },
108c43e99fdSEd Maste { "::1.0.1.1000", { 0, 0, 0, 0 }, BAD },
109c43e99fdSEd Maste { "1:2:33333:4::", { 0, 0, 0, 0 }, BAD },
110c43e99fdSEd Maste { "1:2:3:4:5:6:7:8:9", { 0, 0, 0, 0 }, BAD },
111c43e99fdSEd Maste { "1::2::3", { 0, 0, 0, 0 }, BAD },
112c43e99fdSEd Maste { ":::1", { 0, 0, 0, 0 }, BAD },
113c43e99fdSEd Maste { NULL, { 0, 0, 0, 0, }, BAD },
114c43e99fdSEd Maste };
115c43e99fdSEd Maste
116c43e99fdSEd Maste static void
regress_ipv4_parse(void * ptr)117c43e99fdSEd Maste regress_ipv4_parse(void *ptr)
118c43e99fdSEd Maste {
119c43e99fdSEd Maste int i;
120c43e99fdSEd Maste for (i = 0; ipv4_entries[i].addr; ++i) {
121c43e99fdSEd Maste char written[128];
122c43e99fdSEd Maste struct ipv4_entry *ent = &ipv4_entries[i];
123c43e99fdSEd Maste struct in_addr in;
124c43e99fdSEd Maste int r;
125c43e99fdSEd Maste r = evutil_inet_pton(AF_INET, ent->addr, &in);
126c43e99fdSEd Maste if (r == 0) {
127c43e99fdSEd Maste if (ent->status != BAD) {
128c43e99fdSEd Maste TT_FAIL(("%s did not parse, but it's a good address!",
129c43e99fdSEd Maste ent->addr));
130c43e99fdSEd Maste }
131c43e99fdSEd Maste continue;
132c43e99fdSEd Maste }
133c43e99fdSEd Maste if (ent->status == BAD) {
134c43e99fdSEd Maste TT_FAIL(("%s parsed, but we expected an error", ent->addr));
135c43e99fdSEd Maste continue;
136c43e99fdSEd Maste }
137c43e99fdSEd Maste if (ntohl(in.s_addr) != ent->res) {
138c43e99fdSEd Maste TT_FAIL(("%s parsed to %lx, but we expected %lx", ent->addr,
139c43e99fdSEd Maste (unsigned long)ntohl(in.s_addr),
140c43e99fdSEd Maste (unsigned long)ent->res));
141c43e99fdSEd Maste continue;
142c43e99fdSEd Maste }
143c43e99fdSEd Maste if (ent->status == CANONICAL) {
144c43e99fdSEd Maste const char *w = evutil_inet_ntop(AF_INET, &in, written,
145c43e99fdSEd Maste sizeof(written));
146c43e99fdSEd Maste if (!w) {
147c43e99fdSEd Maste TT_FAIL(("Tried to write out %s; got NULL.", ent->addr));
148c43e99fdSEd Maste continue;
149c43e99fdSEd Maste }
150c43e99fdSEd Maste if (strcmp(written, ent->addr)) {
151c43e99fdSEd Maste TT_FAIL(("Tried to write out %s; got %s",
152c43e99fdSEd Maste ent->addr, written));
153c43e99fdSEd Maste continue;
154c43e99fdSEd Maste }
155c43e99fdSEd Maste }
156c43e99fdSEd Maste
157c43e99fdSEd Maste }
158c43e99fdSEd Maste
159c43e99fdSEd Maste }
160c43e99fdSEd Maste
161c43e99fdSEd Maste static void
regress_ipv6_parse(void * ptr)162c43e99fdSEd Maste regress_ipv6_parse(void *ptr)
163c43e99fdSEd Maste {
164c43e99fdSEd Maste #ifdef AF_INET6
165c43e99fdSEd Maste int i, j;
166c43e99fdSEd Maste
167c43e99fdSEd Maste for (i = 0; ipv6_entries[i].addr; ++i) {
168c43e99fdSEd Maste char written[128];
169c43e99fdSEd Maste struct ipv6_entry *ent = &ipv6_entries[i];
170c43e99fdSEd Maste struct in6_addr in6;
171c43e99fdSEd Maste int r;
172c43e99fdSEd Maste r = evutil_inet_pton(AF_INET6, ent->addr, &in6);
173c43e99fdSEd Maste if (r == 0) {
174c43e99fdSEd Maste if (ent->status != BAD)
175c43e99fdSEd Maste TT_FAIL(("%s did not parse, but it's a good address!",
176c43e99fdSEd Maste ent->addr));
177c43e99fdSEd Maste continue;
178c43e99fdSEd Maste }
179c43e99fdSEd Maste if (ent->status == BAD) {
180c43e99fdSEd Maste TT_FAIL(("%s parsed, but we expected an error", ent->addr));
181c43e99fdSEd Maste continue;
182c43e99fdSEd Maste }
183c43e99fdSEd Maste for (j = 0; j < 4; ++j) {
184c43e99fdSEd Maste /* Can't use s6_addr32 here; some don't have it. */
185c43e99fdSEd Maste ev_uint32_t u =
186c43e99fdSEd Maste ((ev_uint32_t)in6.s6_addr[j*4 ] << 24) |
187c43e99fdSEd Maste ((ev_uint32_t)in6.s6_addr[j*4+1] << 16) |
188c43e99fdSEd Maste ((ev_uint32_t)in6.s6_addr[j*4+2] << 8) |
189c43e99fdSEd Maste ((ev_uint32_t)in6.s6_addr[j*4+3]);
190c43e99fdSEd Maste if (u != ent->res[j]) {
191c43e99fdSEd Maste TT_FAIL(("%s did not parse as expected.", ent->addr));
192c43e99fdSEd Maste continue;
193c43e99fdSEd Maste }
194c43e99fdSEd Maste }
195c43e99fdSEd Maste if (ent->status == CANONICAL) {
196c43e99fdSEd Maste const char *w = evutil_inet_ntop(AF_INET6, &in6, written,
197c43e99fdSEd Maste sizeof(written));
198c43e99fdSEd Maste if (!w) {
199c43e99fdSEd Maste TT_FAIL(("Tried to write out %s; got NULL.", ent->addr));
200c43e99fdSEd Maste continue;
201c43e99fdSEd Maste }
202c43e99fdSEd Maste if (strcmp(written, ent->addr)) {
203c43e99fdSEd Maste TT_FAIL(("Tried to write out %s; got %s", ent->addr, written));
204c43e99fdSEd Maste continue;
205c43e99fdSEd Maste }
206c43e99fdSEd Maste }
207c43e99fdSEd Maste
208c43e99fdSEd Maste }
209c43e99fdSEd Maste #else
210c43e99fdSEd Maste TT_BLATHER(("Skipping IPv6 address parsing."));
211c43e99fdSEd Maste #endif
212c43e99fdSEd Maste }
213c43e99fdSEd Maste
214*b50261e2SCy Schubert static struct ipv6_entry_scope {
215*b50261e2SCy Schubert const char *addr;
216*b50261e2SCy Schubert ev_uint32_t res[4];
217*b50261e2SCy Schubert unsigned scope;
218*b50261e2SCy Schubert enum entry_status status;
219*b50261e2SCy Schubert } ipv6_entries_scope[] = {
220*b50261e2SCy Schubert { "2001:DB8::", { 0x20010db8, 0, 0 }, 0, NORMAL },
221*b50261e2SCy Schubert { "2001:DB8::%0", { 0x20010db8, 0, 0, 0 }, 0, NORMAL },
222*b50261e2SCy Schubert { "2001:DB8::%1", { 0x20010db8, 0, 0, 0 }, 1, NORMAL },
223*b50261e2SCy Schubert { "foobar.", { 0, 0, 0, 0 }, 0, BAD },
224*b50261e2SCy Schubert { "2001:DB8::%does-not-exist", { 0, 0, 0, 0 }, 0, BAD },
225*b50261e2SCy Schubert { NULL, { 0, 0, 0, 0, }, 0, BAD },
226*b50261e2SCy Schubert };
227*b50261e2SCy Schubert static void
regress_ipv6_parse_scope(void * ptr)228*b50261e2SCy Schubert regress_ipv6_parse_scope(void *ptr)
229*b50261e2SCy Schubert {
230*b50261e2SCy Schubert #ifdef AF_INET6
231*b50261e2SCy Schubert int i, j;
232*b50261e2SCy Schubert unsigned if_scope;
233*b50261e2SCy Schubert
234*b50261e2SCy Schubert for (i = 0; ipv6_entries_scope[i].addr; ++i) {
235*b50261e2SCy Schubert struct ipv6_entry_scope *ent = &ipv6_entries_scope[i];
236*b50261e2SCy Schubert struct in6_addr in6;
237*b50261e2SCy Schubert int r;
238*b50261e2SCy Schubert r = evutil_inet_pton_scope(AF_INET6, ent->addr, &in6,
239*b50261e2SCy Schubert &if_scope);
240*b50261e2SCy Schubert if (r == 0) {
241*b50261e2SCy Schubert if (ent->status != BAD)
242*b50261e2SCy Schubert TT_FAIL(("%s did not parse, but it's a good address!",
243*b50261e2SCy Schubert ent->addr));
244*b50261e2SCy Schubert continue;
245*b50261e2SCy Schubert }
246*b50261e2SCy Schubert if (ent->status == BAD) {
247*b50261e2SCy Schubert TT_FAIL(("%s parsed, but we expected an error", ent->addr));
248*b50261e2SCy Schubert continue;
249*b50261e2SCy Schubert }
250*b50261e2SCy Schubert for (j = 0; j < 4; ++j) {
251*b50261e2SCy Schubert /* Can't use s6_addr32 here; some don't have it. */
252*b50261e2SCy Schubert ev_uint32_t u =
253*b50261e2SCy Schubert ((ev_uint32_t)in6.s6_addr[j*4 ] << 24) |
254*b50261e2SCy Schubert ((ev_uint32_t)in6.s6_addr[j*4+1] << 16) |
255*b50261e2SCy Schubert ((ev_uint32_t)in6.s6_addr[j*4+2] << 8) |
256*b50261e2SCy Schubert ((ev_uint32_t)in6.s6_addr[j*4+3]);
257*b50261e2SCy Schubert if (u != ent->res[j]) {
258*b50261e2SCy Schubert TT_FAIL(("%s did not parse as expected.", ent->addr));
259*b50261e2SCy Schubert continue;
260*b50261e2SCy Schubert }
261*b50261e2SCy Schubert }
262*b50261e2SCy Schubert if (if_scope != ent->scope) {
263*b50261e2SCy Schubert TT_FAIL(("%s did not parse as expected.", ent->addr));
264*b50261e2SCy Schubert continue;
265*b50261e2SCy Schubert }
266*b50261e2SCy Schubert }
267*b50261e2SCy Schubert #else
268*b50261e2SCy Schubert TT_BLATHER(("Skipping IPv6 address parsing."));
269*b50261e2SCy Schubert #endif
270*b50261e2SCy Schubert }
271*b50261e2SCy Schubert
272*b50261e2SCy Schubert
273c43e99fdSEd Maste static struct sa_port_ent {
274c43e99fdSEd Maste const char *parse;
275c43e99fdSEd Maste int safamily;
276c43e99fdSEd Maste const char *addr;
277c43e99fdSEd Maste int port;
278c43e99fdSEd Maste } sa_port_ents[] = {
279c43e99fdSEd Maste { "[ffff::1]:1000", AF_INET6, "ffff::1", 1000 },
280c43e99fdSEd Maste { "[ffff::1]", AF_INET6, "ffff::1", 0 },
281c43e99fdSEd Maste { "[ffff::1", 0, NULL, 0 },
282c43e99fdSEd Maste { "[ffff::1]:65599", 0, NULL, 0 },
283c43e99fdSEd Maste { "[ffff::1]:0", 0, NULL, 0 },
284c43e99fdSEd Maste { "[ffff::1]:-1", 0, NULL, 0 },
285c43e99fdSEd Maste { "::1", AF_INET6, "::1", 0 },
286c43e99fdSEd Maste { "1:2::1", AF_INET6, "1:2::1", 0 },
287c43e99fdSEd Maste { "192.168.0.1:50", AF_INET, "192.168.0.1", 50 },
288c43e99fdSEd Maste { "1.2.3.4", AF_INET, "1.2.3.4", 0 },
289c43e99fdSEd Maste { NULL, 0, NULL, 0 },
290c43e99fdSEd Maste };
291c43e99fdSEd Maste
292c43e99fdSEd Maste static void
regress_sockaddr_port_parse(void * ptr)293c43e99fdSEd Maste regress_sockaddr_port_parse(void *ptr)
294c43e99fdSEd Maste {
295c43e99fdSEd Maste struct sockaddr_storage ss;
296c43e99fdSEd Maste int i, r;
297c43e99fdSEd Maste
298c43e99fdSEd Maste for (i = 0; sa_port_ents[i].parse; ++i) {
299c43e99fdSEd Maste struct sa_port_ent *ent = &sa_port_ents[i];
300c43e99fdSEd Maste int len = sizeof(ss);
301c43e99fdSEd Maste memset(&ss, 0, sizeof(ss));
302c43e99fdSEd Maste r = evutil_parse_sockaddr_port(ent->parse, (struct sockaddr*)&ss, &len);
303c43e99fdSEd Maste if (r < 0) {
304c43e99fdSEd Maste if (ent->safamily)
305c43e99fdSEd Maste TT_FAIL(("Couldn't parse %s!", ent->parse));
306c43e99fdSEd Maste continue;
307c43e99fdSEd Maste } else if (! ent->safamily) {
308c43e99fdSEd Maste TT_FAIL(("Shouldn't have been able to parse %s!", ent->parse));
309c43e99fdSEd Maste continue;
310c43e99fdSEd Maste }
311c43e99fdSEd Maste if (ent->safamily == AF_INET) {
312c43e99fdSEd Maste struct sockaddr_in sin;
313c43e99fdSEd Maste memset(&sin, 0, sizeof(sin));
314c43e99fdSEd Maste #ifdef EVENT__HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
315c43e99fdSEd Maste sin.sin_len = sizeof(sin);
316c43e99fdSEd Maste #endif
317c43e99fdSEd Maste sin.sin_family = AF_INET;
318c43e99fdSEd Maste sin.sin_port = htons(ent->port);
319c43e99fdSEd Maste r = evutil_inet_pton(AF_INET, ent->addr, &sin.sin_addr);
320c43e99fdSEd Maste if (1 != r) {
321c43e99fdSEd Maste TT_FAIL(("Couldn't parse ipv4 target %s.", ent->addr));
322c43e99fdSEd Maste } else if (memcmp(&sin, &ss, sizeof(sin))) {
323c43e99fdSEd Maste TT_FAIL(("Parse for %s was not as expected.", ent->parse));
324c43e99fdSEd Maste } else if (len != sizeof(sin)) {
325c43e99fdSEd Maste TT_FAIL(("Length for %s not as expected.",ent->parse));
326c43e99fdSEd Maste }
327c43e99fdSEd Maste } else {
328c43e99fdSEd Maste struct sockaddr_in6 sin6;
329c43e99fdSEd Maste memset(&sin6, 0, sizeof(sin6));
330c43e99fdSEd Maste #ifdef EVENT__HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN
331c43e99fdSEd Maste sin6.sin6_len = sizeof(sin6);
332c43e99fdSEd Maste #endif
333c43e99fdSEd Maste sin6.sin6_family = AF_INET6;
334c43e99fdSEd Maste sin6.sin6_port = htons(ent->port);
335c43e99fdSEd Maste r = evutil_inet_pton(AF_INET6, ent->addr, &sin6.sin6_addr);
336c43e99fdSEd Maste if (1 != r) {
337c43e99fdSEd Maste TT_FAIL(("Couldn't parse ipv6 target %s.", ent->addr));
338c43e99fdSEd Maste } else if (memcmp(&sin6, &ss, sizeof(sin6))) {
339c43e99fdSEd Maste TT_FAIL(("Parse for %s was not as expected.", ent->parse));
340c43e99fdSEd Maste } else if (len != sizeof(sin6)) {
341c43e99fdSEd Maste TT_FAIL(("Length for %s not as expected.",ent->parse));
342c43e99fdSEd Maste }
343c43e99fdSEd Maste }
344c43e99fdSEd Maste }
345c43e99fdSEd Maste }
346c43e99fdSEd Maste
347c43e99fdSEd Maste
348c43e99fdSEd Maste static void
regress_sockaddr_port_format(void * ptr)349c43e99fdSEd Maste regress_sockaddr_port_format(void *ptr)
350c43e99fdSEd Maste {
351c43e99fdSEd Maste struct sockaddr_storage ss;
352c43e99fdSEd Maste int len;
353c43e99fdSEd Maste const char *cp;
354c43e99fdSEd Maste char cbuf[128];
355c43e99fdSEd Maste int r;
356c43e99fdSEd Maste
357c43e99fdSEd Maste len = sizeof(ss);
358c43e99fdSEd Maste r = evutil_parse_sockaddr_port("192.168.1.1:80",
359c43e99fdSEd Maste (struct sockaddr*)&ss, &len);
360c43e99fdSEd Maste tt_int_op(r,==,0);
361c43e99fdSEd Maste cp = evutil_format_sockaddr_port_(
362c43e99fdSEd Maste (struct sockaddr*)&ss, cbuf, sizeof(cbuf));
363c43e99fdSEd Maste tt_ptr_op(cp,==,cbuf);
364c43e99fdSEd Maste tt_str_op(cp,==,"192.168.1.1:80");
365c43e99fdSEd Maste
366c43e99fdSEd Maste len = sizeof(ss);
367c43e99fdSEd Maste r = evutil_parse_sockaddr_port("[ff00::8010]:999",
368c43e99fdSEd Maste (struct sockaddr*)&ss, &len);
369c43e99fdSEd Maste tt_int_op(r,==,0);
370c43e99fdSEd Maste cp = evutil_format_sockaddr_port_(
371c43e99fdSEd Maste (struct sockaddr*)&ss, cbuf, sizeof(cbuf));
372c43e99fdSEd Maste tt_ptr_op(cp,==,cbuf);
373c43e99fdSEd Maste tt_str_op(cp,==,"[ff00::8010]:999");
374c43e99fdSEd Maste
375c43e99fdSEd Maste ss.ss_family=99;
376c43e99fdSEd Maste cp = evutil_format_sockaddr_port_(
377c43e99fdSEd Maste (struct sockaddr*)&ss, cbuf, sizeof(cbuf));
378c43e99fdSEd Maste tt_ptr_op(cp,==,cbuf);
379c43e99fdSEd Maste tt_str_op(cp,==,"<addr with socktype 99>");
380c43e99fdSEd Maste end:
381c43e99fdSEd Maste ;
382c43e99fdSEd Maste }
383c43e99fdSEd Maste
384c43e99fdSEd Maste static struct sa_pred_ent {
385c43e99fdSEd Maste const char *parse;
386c43e99fdSEd Maste
387c43e99fdSEd Maste int is_loopback;
388c43e99fdSEd Maste } sa_pred_entries[] = {
389c43e99fdSEd Maste { "127.0.0.1", 1 },
390c43e99fdSEd Maste { "127.0.3.2", 1 },
391c43e99fdSEd Maste { "128.1.2.3", 0 },
392c43e99fdSEd Maste { "18.0.0.1", 0 },
393c43e99fdSEd Maste { "129.168.1.1", 0 },
394c43e99fdSEd Maste
395c43e99fdSEd Maste { "::1", 1 },
396c43e99fdSEd Maste { "::0", 0 },
397c43e99fdSEd Maste { "f::1", 0 },
398c43e99fdSEd Maste { "::501", 0 },
399c43e99fdSEd Maste { NULL, 0 },
400c43e99fdSEd Maste
401c43e99fdSEd Maste };
402c43e99fdSEd Maste
403c43e99fdSEd Maste static void
test_evutil_sockaddr_predicates(void * ptr)404c43e99fdSEd Maste test_evutil_sockaddr_predicates(void *ptr)
405c43e99fdSEd Maste {
406c43e99fdSEd Maste struct sockaddr_storage ss;
407c43e99fdSEd Maste int r, i;
408c43e99fdSEd Maste
409c43e99fdSEd Maste for (i=0; sa_pred_entries[i].parse; ++i) {
410c43e99fdSEd Maste struct sa_pred_ent *ent = &sa_pred_entries[i];
411c43e99fdSEd Maste int len = sizeof(ss);
412c43e99fdSEd Maste
413c43e99fdSEd Maste r = evutil_parse_sockaddr_port(ent->parse, (struct sockaddr*)&ss, &len);
414c43e99fdSEd Maste
415c43e99fdSEd Maste if (r<0) {
416c43e99fdSEd Maste TT_FAIL(("Couldn't parse %s!", ent->parse));
417c43e99fdSEd Maste continue;
418c43e99fdSEd Maste }
419c43e99fdSEd Maste
420c43e99fdSEd Maste /* sockaddr_is_loopback */
421c43e99fdSEd Maste if (ent->is_loopback != evutil_sockaddr_is_loopback_((struct sockaddr*)&ss)) {
422c43e99fdSEd Maste TT_FAIL(("evutil_sockaddr_loopback(%s) not as expected",
423c43e99fdSEd Maste ent->parse));
424c43e99fdSEd Maste }
425c43e99fdSEd Maste }
426c43e99fdSEd Maste }
427c43e99fdSEd Maste
428c43e99fdSEd Maste static void
test_evutil_strtoll(void * ptr)429c43e99fdSEd Maste test_evutil_strtoll(void *ptr)
430c43e99fdSEd Maste {
431c43e99fdSEd Maste const char *s;
432c43e99fdSEd Maste char *endptr;
433c43e99fdSEd Maste
434c43e99fdSEd Maste tt_want(evutil_strtoll("5000000000", NULL, 10) ==
435c43e99fdSEd Maste ((ev_int64_t)5000000)*1000);
436c43e99fdSEd Maste tt_want(evutil_strtoll("-5000000000", NULL, 10) ==
437c43e99fdSEd Maste ((ev_int64_t)5000000)*-1000);
438c43e99fdSEd Maste s = " 99999stuff";
439c43e99fdSEd Maste tt_want(evutil_strtoll(s, &endptr, 10) == (ev_int64_t)99999);
440c43e99fdSEd Maste tt_want(endptr == s+6);
441c43e99fdSEd Maste tt_want(evutil_strtoll("foo", NULL, 10) == 0);
442c43e99fdSEd Maste }
443c43e99fdSEd Maste
444c43e99fdSEd Maste static void
test_evutil_snprintf(void * ptr)445c43e99fdSEd Maste test_evutil_snprintf(void *ptr)
446c43e99fdSEd Maste {
447c43e99fdSEd Maste char buf[16];
448c43e99fdSEd Maste int r;
449c43e99fdSEd Maste ev_uint64_t u64 = ((ev_uint64_t)1000000000)*200;
450c43e99fdSEd Maste ev_int64_t i64 = -1 * (ev_int64_t) u64;
451c43e99fdSEd Maste size_t size = 8000;
452c43e99fdSEd Maste ev_ssize_t ssize = -9000;
453c43e99fdSEd Maste
454c43e99fdSEd Maste r = evutil_snprintf(buf, sizeof(buf), "%d %d", 50, 100);
455c43e99fdSEd Maste tt_str_op(buf, ==, "50 100");
456c43e99fdSEd Maste tt_int_op(r, ==, 6);
457c43e99fdSEd Maste
458c43e99fdSEd Maste r = evutil_snprintf(buf, sizeof(buf), "longish %d", 1234567890);
459c43e99fdSEd Maste tt_str_op(buf, ==, "longish 1234567");
460c43e99fdSEd Maste tt_int_op(r, ==, 18);
461c43e99fdSEd Maste
462c43e99fdSEd Maste r = evutil_snprintf(buf, sizeof(buf), EV_U64_FMT, EV_U64_ARG(u64));
463c43e99fdSEd Maste tt_str_op(buf, ==, "200000000000");
464c43e99fdSEd Maste tt_int_op(r, ==, 12);
465c43e99fdSEd Maste
466c43e99fdSEd Maste r = evutil_snprintf(buf, sizeof(buf), EV_I64_FMT, EV_I64_ARG(i64));
467c43e99fdSEd Maste tt_str_op(buf, ==, "-200000000000");
468c43e99fdSEd Maste tt_int_op(r, ==, 13);
469c43e99fdSEd Maste
470c43e99fdSEd Maste r = evutil_snprintf(buf, sizeof(buf), EV_SIZE_FMT" "EV_SSIZE_FMT,
471c43e99fdSEd Maste EV_SIZE_ARG(size), EV_SSIZE_ARG(ssize));
472c43e99fdSEd Maste tt_str_op(buf, ==, "8000 -9000");
473c43e99fdSEd Maste tt_int_op(r, ==, 10);
474c43e99fdSEd Maste
475c43e99fdSEd Maste end:
476c43e99fdSEd Maste ;
477c43e99fdSEd Maste }
478c43e99fdSEd Maste
479c43e99fdSEd Maste static void
test_evutil_casecmp(void * ptr)480c43e99fdSEd Maste test_evutil_casecmp(void *ptr)
481c43e99fdSEd Maste {
482c43e99fdSEd Maste tt_int_op(evutil_ascii_strcasecmp("ABC", "ABC"), ==, 0);
483c43e99fdSEd Maste tt_int_op(evutil_ascii_strcasecmp("ABC", "abc"), ==, 0);
484c43e99fdSEd Maste tt_int_op(evutil_ascii_strcasecmp("ABC", "abcd"), <, 0);
485c43e99fdSEd Maste tt_int_op(evutil_ascii_strcasecmp("ABC", "abb"), >, 0);
486c43e99fdSEd Maste tt_int_op(evutil_ascii_strcasecmp("ABCd", "abc"), >, 0);
487c43e99fdSEd Maste
488c43e99fdSEd Maste tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibEvEnT", 100), ==, 0);
489c43e99fdSEd Maste tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibEvEnT", 4), ==, 0);
490c43e99fdSEd Maste tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibEXXXX", 4), ==, 0);
491c43e99fdSEd Maste tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibE", 4), ==, 0);
492c43e99fdSEd Maste tt_int_op(evutil_ascii_strncasecmp("Libe", "LibEvEnT", 4), ==, 0);
493c43e99fdSEd Maste tt_int_op(evutil_ascii_strncasecmp("Lib", "LibEvEnT", 4), <, 0);
494c43e99fdSEd Maste tt_int_op(evutil_ascii_strncasecmp("abc", "def", 99), <, 0);
495c43e99fdSEd Maste tt_int_op(evutil_ascii_strncasecmp("Z", "qrst", 1), >, 0);
496c43e99fdSEd Maste end:
497c43e99fdSEd Maste ;
498c43e99fdSEd Maste }
499c43e99fdSEd Maste
500c43e99fdSEd Maste static void
test_evutil_rtrim(void * ptr)501c43e99fdSEd Maste test_evutil_rtrim(void *ptr)
502c43e99fdSEd Maste {
503c43e99fdSEd Maste #define TEST_TRIM(s, result) \
504c43e99fdSEd Maste do { \
505c43e99fdSEd Maste if (cp) mm_free(cp); \
506c43e99fdSEd Maste cp = mm_strdup(s); \
507c43e99fdSEd Maste tt_assert(cp); \
508c43e99fdSEd Maste evutil_rtrim_lws_(cp); \
509c43e99fdSEd Maste tt_str_op(cp, ==, result); \
510c43e99fdSEd Maste } while(0)
511c43e99fdSEd Maste
512c43e99fdSEd Maste char *cp = NULL;
513c43e99fdSEd Maste (void) ptr;
514c43e99fdSEd Maste
515c43e99fdSEd Maste TEST_TRIM("", "");
516c43e99fdSEd Maste TEST_TRIM("a", "a");
517c43e99fdSEd Maste TEST_TRIM("abcdef ghi", "abcdef ghi");
518c43e99fdSEd Maste
519c43e99fdSEd Maste TEST_TRIM(" ", "");
520c43e99fdSEd Maste TEST_TRIM(" ", "");
521c43e99fdSEd Maste TEST_TRIM("a ", "a");
522c43e99fdSEd Maste TEST_TRIM("abcdef gH ", "abcdef gH");
523c43e99fdSEd Maste
524c43e99fdSEd Maste TEST_TRIM("\t\t", "");
525c43e99fdSEd Maste TEST_TRIM(" \t", "");
526c43e99fdSEd Maste TEST_TRIM("\t", "");
527c43e99fdSEd Maste TEST_TRIM("a \t", "a");
528c43e99fdSEd Maste TEST_TRIM("a\t ", "a");
529c43e99fdSEd Maste TEST_TRIM("a\t", "a");
530c43e99fdSEd Maste TEST_TRIM("abcdef gH \t ", "abcdef gH");
531c43e99fdSEd Maste
532c43e99fdSEd Maste end:
533c43e99fdSEd Maste if (cp)
534c43e99fdSEd Maste mm_free(cp);
535c43e99fdSEd Maste }
536c43e99fdSEd Maste
537c43e99fdSEd Maste static int logsev = 0;
538c43e99fdSEd Maste static char *logmsg = NULL;
539c43e99fdSEd Maste
540c43e99fdSEd Maste static void
logfn(int severity,const char * msg)541c43e99fdSEd Maste logfn(int severity, const char *msg)
542c43e99fdSEd Maste {
543c43e99fdSEd Maste logsev = severity;
544c43e99fdSEd Maste tt_want(msg);
545c43e99fdSEd Maste if (msg) {
546c43e99fdSEd Maste if (logmsg)
547c43e99fdSEd Maste free(logmsg);
548c43e99fdSEd Maste logmsg = strdup(msg);
549c43e99fdSEd Maste }
550c43e99fdSEd Maste }
551c43e99fdSEd Maste
552c43e99fdSEd Maste static int fatal_want_severity = 0;
553c43e99fdSEd Maste static const char *fatal_want_message = NULL;
554c43e99fdSEd Maste static void
fatalfn(int exitcode)555c43e99fdSEd Maste fatalfn(int exitcode)
556c43e99fdSEd Maste {
557c43e99fdSEd Maste if (logsev != fatal_want_severity ||
558c43e99fdSEd Maste !logmsg ||
559c43e99fdSEd Maste strcmp(logmsg, fatal_want_message))
560c43e99fdSEd Maste exit(0);
561c43e99fdSEd Maste else
562c43e99fdSEd Maste exit(exitcode);
563c43e99fdSEd Maste }
564c43e99fdSEd Maste
565c43e99fdSEd Maste #ifndef _WIN32
566c43e99fdSEd Maste #define CAN_CHECK_ERR
567c43e99fdSEd Maste static void
check_error_logging(void (* fn)(void),int wantexitcode,int wantseverity,const char * wantmsg)568c43e99fdSEd Maste check_error_logging(void (*fn)(void), int wantexitcode,
569c43e99fdSEd Maste int wantseverity, const char *wantmsg)
570c43e99fdSEd Maste {
571c43e99fdSEd Maste pid_t pid;
572c43e99fdSEd Maste int status = 0, exitcode;
573c43e99fdSEd Maste fatal_want_severity = wantseverity;
574c43e99fdSEd Maste fatal_want_message = wantmsg;
575c43e99fdSEd Maste if ((pid = regress_fork()) == 0) {
576c43e99fdSEd Maste /* child process */
577c43e99fdSEd Maste fn();
578c43e99fdSEd Maste exit(0); /* should be unreachable. */
579c43e99fdSEd Maste } else {
580c43e99fdSEd Maste wait(&status);
581c43e99fdSEd Maste exitcode = WEXITSTATUS(status);
582c43e99fdSEd Maste tt_int_op(wantexitcode, ==, exitcode);
583c43e99fdSEd Maste }
584c43e99fdSEd Maste end:
585c43e99fdSEd Maste ;
586c43e99fdSEd Maste }
587c43e99fdSEd Maste
588c43e99fdSEd Maste static void
errx_fn(void)589c43e99fdSEd Maste errx_fn(void)
590c43e99fdSEd Maste {
591c43e99fdSEd Maste event_errx(2, "Fatal error; too many kumquats (%d)", 5);
592c43e99fdSEd Maste }
593c43e99fdSEd Maste
594c43e99fdSEd Maste static void
err_fn(void)595c43e99fdSEd Maste err_fn(void)
596c43e99fdSEd Maste {
597c43e99fdSEd Maste errno = ENOENT;
598c43e99fdSEd Maste event_err(5,"Couldn't open %s", "/very/bad/file");
599c43e99fdSEd Maste }
600c43e99fdSEd Maste
601c43e99fdSEd Maste static void
sock_err_fn(void)602c43e99fdSEd Maste sock_err_fn(void)
603c43e99fdSEd Maste {
604c43e99fdSEd Maste evutil_socket_t fd = socket(AF_INET, SOCK_STREAM, 0);
605c43e99fdSEd Maste #ifdef _WIN32
606c43e99fdSEd Maste EVUTIL_SET_SOCKET_ERROR(WSAEWOULDBLOCK);
607c43e99fdSEd Maste #else
608c43e99fdSEd Maste errno = EAGAIN;
609c43e99fdSEd Maste #endif
610c43e99fdSEd Maste event_sock_err(20, fd, "Unhappy socket");
611c43e99fdSEd Maste }
612c43e99fdSEd Maste #endif
613c43e99fdSEd Maste
614c43e99fdSEd Maste static void
test_evutil_log(void * ptr)615c43e99fdSEd Maste test_evutil_log(void *ptr)
616c43e99fdSEd Maste {
617c43e99fdSEd Maste evutil_socket_t fd = -1;
618c43e99fdSEd Maste char buf[128];
619c43e99fdSEd Maste
620c43e99fdSEd Maste event_set_log_callback(logfn);
621c43e99fdSEd Maste event_set_fatal_callback(fatalfn);
622c43e99fdSEd Maste #define RESET() do { \
623c43e99fdSEd Maste logsev = 0; \
624c43e99fdSEd Maste if (logmsg) free(logmsg); \
625c43e99fdSEd Maste logmsg = NULL; \
626c43e99fdSEd Maste } while (0)
627c43e99fdSEd Maste #define LOGEQ(sev,msg) do { \
628c43e99fdSEd Maste tt_int_op(logsev,==,sev); \
629c43e99fdSEd Maste tt_assert(logmsg != NULL); \
630c43e99fdSEd Maste tt_str_op(logmsg,==,msg); \
631c43e99fdSEd Maste } while (0)
632c43e99fdSEd Maste
633c43e99fdSEd Maste #ifdef CAN_CHECK_ERR
634c43e99fdSEd Maste /* We need to disable these tests for now. Previously, the logging
635c43e99fdSEd Maste * module didn't enforce the requirement that a fatal callback
636c43e99fdSEd Maste * actually exit. Now, it exits no matter what, so if we wan to
637c43e99fdSEd Maste * reinstate these tests, we'll need to fork for each one. */
638c43e99fdSEd Maste check_error_logging(errx_fn, 2, EVENT_LOG_ERR,
639c43e99fdSEd Maste "Fatal error; too many kumquats (5)");
640c43e99fdSEd Maste RESET();
641c43e99fdSEd Maste #endif
642c43e99fdSEd Maste
643c43e99fdSEd Maste event_warnx("Far too many %s (%d)", "wombats", 99);
644c43e99fdSEd Maste LOGEQ(EVENT_LOG_WARN, "Far too many wombats (99)");
645c43e99fdSEd Maste RESET();
646c43e99fdSEd Maste
647c43e99fdSEd Maste event_msgx("Connecting lime to coconut");
648c43e99fdSEd Maste LOGEQ(EVENT_LOG_MSG, "Connecting lime to coconut");
649c43e99fdSEd Maste RESET();
650c43e99fdSEd Maste
651c43e99fdSEd Maste event_debug(("A millisecond passed! We should log that!"));
652c43e99fdSEd Maste #ifdef USE_DEBUG
653c43e99fdSEd Maste LOGEQ(EVENT_LOG_DEBUG, "A millisecond passed! We should log that!");
654c43e99fdSEd Maste #else
655c43e99fdSEd Maste tt_int_op(logsev,==,0);
656c43e99fdSEd Maste tt_ptr_op(logmsg,==,NULL);
657c43e99fdSEd Maste #endif
658c43e99fdSEd Maste RESET();
659c43e99fdSEd Maste
660c43e99fdSEd Maste /* Try with an errno. */
661c43e99fdSEd Maste errno = ENOENT;
662c43e99fdSEd Maste event_warn("Couldn't open %s", "/bad/file");
663c43e99fdSEd Maste evutil_snprintf(buf, sizeof(buf),
664c43e99fdSEd Maste "Couldn't open /bad/file: %s",strerror(ENOENT));
665c43e99fdSEd Maste LOGEQ(EVENT_LOG_WARN,buf);
666c43e99fdSEd Maste RESET();
667c43e99fdSEd Maste
668c43e99fdSEd Maste #ifdef CAN_CHECK_ERR
669c43e99fdSEd Maste evutil_snprintf(buf, sizeof(buf),
670c43e99fdSEd Maste "Couldn't open /very/bad/file: %s",strerror(ENOENT));
671c43e99fdSEd Maste check_error_logging(err_fn, 5, EVENT_LOG_ERR, buf);
672c43e99fdSEd Maste RESET();
673c43e99fdSEd Maste #endif
674c43e99fdSEd Maste
675c43e99fdSEd Maste /* Try with a socket errno. */
676c43e99fdSEd Maste fd = socket(AF_INET, SOCK_STREAM, 0);
677c43e99fdSEd Maste #ifdef _WIN32
678c43e99fdSEd Maste evutil_snprintf(buf, sizeof(buf),
679c43e99fdSEd Maste "Unhappy socket: %s",
680c43e99fdSEd Maste evutil_socket_error_to_string(WSAEWOULDBLOCK));
681c43e99fdSEd Maste EVUTIL_SET_SOCKET_ERROR(WSAEWOULDBLOCK);
682c43e99fdSEd Maste #else
683c43e99fdSEd Maste evutil_snprintf(buf, sizeof(buf),
684c43e99fdSEd Maste "Unhappy socket: %s", strerror(EAGAIN));
685c43e99fdSEd Maste errno = EAGAIN;
686c43e99fdSEd Maste #endif
687c43e99fdSEd Maste event_sock_warn(fd, "Unhappy socket");
688c43e99fdSEd Maste LOGEQ(EVENT_LOG_WARN, buf);
689c43e99fdSEd Maste RESET();
690c43e99fdSEd Maste
691c43e99fdSEd Maste #ifdef CAN_CHECK_ERR
692c43e99fdSEd Maste check_error_logging(sock_err_fn, 20, EVENT_LOG_ERR, buf);
693c43e99fdSEd Maste RESET();
694c43e99fdSEd Maste #endif
695c43e99fdSEd Maste
696c43e99fdSEd Maste #undef RESET
697c43e99fdSEd Maste #undef LOGEQ
698c43e99fdSEd Maste end:
699c43e99fdSEd Maste if (logmsg)
700c43e99fdSEd Maste free(logmsg);
701c43e99fdSEd Maste if (fd >= 0)
702c43e99fdSEd Maste evutil_closesocket(fd);
703c43e99fdSEd Maste }
704c43e99fdSEd Maste
705c43e99fdSEd Maste static void
test_evutil_strlcpy(void * arg)706c43e99fdSEd Maste test_evutil_strlcpy(void *arg)
707c43e99fdSEd Maste {
708c43e99fdSEd Maste char buf[8];
709c43e99fdSEd Maste
710c43e99fdSEd Maste /* Successful case. */
711c43e99fdSEd Maste tt_int_op(5, ==, strlcpy(buf, "Hello", sizeof(buf)));
712c43e99fdSEd Maste tt_str_op(buf, ==, "Hello");
713c43e99fdSEd Maste
714c43e99fdSEd Maste /* Overflow by a lot. */
715c43e99fdSEd Maste tt_int_op(13, ==, strlcpy(buf, "pentasyllabic", sizeof(buf)));
716c43e99fdSEd Maste tt_str_op(buf, ==, "pentasy");
717c43e99fdSEd Maste
718c43e99fdSEd Maste /* Overflow by exactly one. */
719c43e99fdSEd Maste tt_int_op(8, ==, strlcpy(buf, "overlong", sizeof(buf)));
720c43e99fdSEd Maste tt_str_op(buf, ==, "overlon");
721c43e99fdSEd Maste end:
722c43e99fdSEd Maste ;
723c43e99fdSEd Maste }
724c43e99fdSEd Maste
725c43e99fdSEd Maste struct example_struct {
726c43e99fdSEd Maste const char *a;
727c43e99fdSEd Maste const char *b;
728c43e99fdSEd Maste long c;
729c43e99fdSEd Maste };
730c43e99fdSEd Maste
731c43e99fdSEd Maste static void
test_evutil_upcast(void * arg)732c43e99fdSEd Maste test_evutil_upcast(void *arg)
733c43e99fdSEd Maste {
734c43e99fdSEd Maste struct example_struct es1;
735c43e99fdSEd Maste const char **cp;
736c43e99fdSEd Maste es1.a = "World";
737c43e99fdSEd Maste es1.b = "Hello";
738c43e99fdSEd Maste es1.c = -99;
739c43e99fdSEd Maste
740c43e99fdSEd Maste tt_int_op(evutil_offsetof(struct example_struct, b), ==, sizeof(char*));
741c43e99fdSEd Maste
742c43e99fdSEd Maste cp = &es1.b;
743c43e99fdSEd Maste tt_ptr_op(EVUTIL_UPCAST(cp, struct example_struct, b), ==, &es1);
744c43e99fdSEd Maste
745c43e99fdSEd Maste end:
746c43e99fdSEd Maste ;
747c43e99fdSEd Maste }
748c43e99fdSEd Maste
749c43e99fdSEd Maste static void
test_evutil_integers(void * arg)750c43e99fdSEd Maste test_evutil_integers(void *arg)
751c43e99fdSEd Maste {
752c43e99fdSEd Maste ev_int64_t i64;
753c43e99fdSEd Maste ev_uint64_t u64;
754c43e99fdSEd Maste ev_int32_t i32;
755c43e99fdSEd Maste ev_uint32_t u32;
756c43e99fdSEd Maste ev_int16_t i16;
757c43e99fdSEd Maste ev_uint16_t u16;
758c43e99fdSEd Maste ev_int8_t i8;
759c43e99fdSEd Maste ev_uint8_t u8;
760c43e99fdSEd Maste
761c43e99fdSEd Maste void *ptr;
762c43e99fdSEd Maste ev_intptr_t iptr;
763c43e99fdSEd Maste ev_uintptr_t uptr;
764c43e99fdSEd Maste
765c43e99fdSEd Maste ev_ssize_t ssize;
766c43e99fdSEd Maste
767c43e99fdSEd Maste tt_int_op(sizeof(u64), ==, 8);
768c43e99fdSEd Maste tt_int_op(sizeof(i64), ==, 8);
769c43e99fdSEd Maste tt_int_op(sizeof(u32), ==, 4);
770c43e99fdSEd Maste tt_int_op(sizeof(i32), ==, 4);
771c43e99fdSEd Maste tt_int_op(sizeof(u16), ==, 2);
772c43e99fdSEd Maste tt_int_op(sizeof(i16), ==, 2);
773c43e99fdSEd Maste tt_int_op(sizeof(u8), ==, 1);
774c43e99fdSEd Maste tt_int_op(sizeof(i8), ==, 1);
775c43e99fdSEd Maste
776c43e99fdSEd Maste tt_int_op(sizeof(ev_ssize_t), ==, sizeof(size_t));
777c43e99fdSEd Maste tt_int_op(sizeof(ev_intptr_t), >=, sizeof(void *));
778c43e99fdSEd Maste tt_int_op(sizeof(ev_uintptr_t), ==, sizeof(intptr_t));
779c43e99fdSEd Maste
780c43e99fdSEd Maste u64 = 1000000000;
781c43e99fdSEd Maste u64 *= 1000000000;
782c43e99fdSEd Maste tt_assert(u64 / 1000000000 == 1000000000);
783c43e99fdSEd Maste i64 = -1000000000;
784c43e99fdSEd Maste i64 *= 1000000000;
785c43e99fdSEd Maste tt_assert(i64 / 1000000000 == -1000000000);
786c43e99fdSEd Maste
787c43e99fdSEd Maste u64 = EV_UINT64_MAX;
788c43e99fdSEd Maste i64 = EV_INT64_MAX;
789c43e99fdSEd Maste tt_assert(u64 > 0);
790c43e99fdSEd Maste tt_assert(i64 > 0);
791c43e99fdSEd Maste u64++;
792c43e99fdSEd Maste /* i64++; */
793c43e99fdSEd Maste tt_assert(u64 == 0);
794c43e99fdSEd Maste /* tt_assert(i64 == EV_INT64_MIN); */
795c43e99fdSEd Maste /* tt_assert(i64 < 0); */
796c43e99fdSEd Maste
797c43e99fdSEd Maste u32 = EV_UINT32_MAX;
798c43e99fdSEd Maste i32 = EV_INT32_MAX;
799c43e99fdSEd Maste tt_assert(u32 > 0);
800c43e99fdSEd Maste tt_assert(i32 > 0);
801c43e99fdSEd Maste u32++;
802c43e99fdSEd Maste /* i32++; */
803c43e99fdSEd Maste tt_assert(u32 == 0);
804c43e99fdSEd Maste /* tt_assert(i32 == EV_INT32_MIN); */
805c43e99fdSEd Maste /* tt_assert(i32 < 0); */
806c43e99fdSEd Maste
807c43e99fdSEd Maste u16 = EV_UINT16_MAX;
808c43e99fdSEd Maste i16 = EV_INT16_MAX;
809c43e99fdSEd Maste tt_assert(u16 > 0);
810c43e99fdSEd Maste tt_assert(i16 > 0);
811c43e99fdSEd Maste u16++;
812c43e99fdSEd Maste /* i16++; */
813c43e99fdSEd Maste tt_assert(u16 == 0);
814c43e99fdSEd Maste /* tt_assert(i16 == EV_INT16_MIN); */
815c43e99fdSEd Maste /* tt_assert(i16 < 0); */
816c43e99fdSEd Maste
817c43e99fdSEd Maste u8 = EV_UINT8_MAX;
818c43e99fdSEd Maste i8 = EV_INT8_MAX;
819c43e99fdSEd Maste tt_assert(u8 > 0);
820c43e99fdSEd Maste tt_assert(i8 > 0);
821c43e99fdSEd Maste u8++;
822c43e99fdSEd Maste /* i8++;*/
823c43e99fdSEd Maste tt_assert(u8 == 0);
824c43e99fdSEd Maste /* tt_assert(i8 == EV_INT8_MIN); */
825c43e99fdSEd Maste /* tt_assert(i8 < 0); */
826c43e99fdSEd Maste
827c43e99fdSEd Maste /*
828c43e99fdSEd Maste ssize = EV_SSIZE_MAX;
829c43e99fdSEd Maste tt_assert(ssize > 0);
830c43e99fdSEd Maste ssize++;
831c43e99fdSEd Maste tt_assert(ssize < 0);
832c43e99fdSEd Maste tt_assert(ssize == EV_SSIZE_MIN);
833c43e99fdSEd Maste */
834c43e99fdSEd Maste
835c43e99fdSEd Maste ptr = &ssize;
836c43e99fdSEd Maste iptr = (ev_intptr_t)ptr;
837c43e99fdSEd Maste uptr = (ev_uintptr_t)ptr;
838c43e99fdSEd Maste ptr = (void *)iptr;
839c43e99fdSEd Maste tt_assert(ptr == &ssize);
840c43e99fdSEd Maste ptr = (void *)uptr;
841c43e99fdSEd Maste tt_assert(ptr == &ssize);
842c43e99fdSEd Maste
843c43e99fdSEd Maste iptr = -1;
844c43e99fdSEd Maste tt_assert(iptr < 0);
845c43e99fdSEd Maste end:
846c43e99fdSEd Maste ;
847c43e99fdSEd Maste }
848c43e99fdSEd Maste
849c43e99fdSEd Maste struct evutil_addrinfo *
ai_find_by_family(struct evutil_addrinfo * ai,int family)850c43e99fdSEd Maste ai_find_by_family(struct evutil_addrinfo *ai, int family)
851c43e99fdSEd Maste {
852c43e99fdSEd Maste while (ai) {
853c43e99fdSEd Maste if (ai->ai_family == family)
854c43e99fdSEd Maste return ai;
855c43e99fdSEd Maste ai = ai->ai_next;
856c43e99fdSEd Maste }
857c43e99fdSEd Maste return NULL;
858c43e99fdSEd Maste }
859c43e99fdSEd Maste
860c43e99fdSEd Maste struct evutil_addrinfo *
ai_find_by_protocol(struct evutil_addrinfo * ai,int protocol)861c43e99fdSEd Maste ai_find_by_protocol(struct evutil_addrinfo *ai, int protocol)
862c43e99fdSEd Maste {
863c43e99fdSEd Maste while (ai) {
864c43e99fdSEd Maste if (ai->ai_protocol == protocol)
865c43e99fdSEd Maste return ai;
866c43e99fdSEd Maste ai = ai->ai_next;
867c43e99fdSEd Maste }
868c43e99fdSEd Maste return NULL;
869c43e99fdSEd Maste }
870c43e99fdSEd Maste
871c43e99fdSEd Maste
872c43e99fdSEd Maste int
test_ai_eq_(const struct evutil_addrinfo * ai,const char * sockaddr_port,int socktype,int protocol,int line)873c43e99fdSEd Maste test_ai_eq_(const struct evutil_addrinfo *ai, const char *sockaddr_port,
874c43e99fdSEd Maste int socktype, int protocol, int line)
875c43e99fdSEd Maste {
876c43e99fdSEd Maste struct sockaddr_storage ss;
877c43e99fdSEd Maste int slen = sizeof(ss);
878c43e99fdSEd Maste int gotport;
879c43e99fdSEd Maste char buf[128];
880c43e99fdSEd Maste memset(&ss, 0, sizeof(ss));
881c43e99fdSEd Maste if (socktype > 0)
882c43e99fdSEd Maste tt_int_op(ai->ai_socktype, ==, socktype);
883c43e99fdSEd Maste if (protocol > 0)
884c43e99fdSEd Maste tt_int_op(ai->ai_protocol, ==, protocol);
885c43e99fdSEd Maste
886c43e99fdSEd Maste if (evutil_parse_sockaddr_port(
887c43e99fdSEd Maste sockaddr_port, (struct sockaddr*)&ss, &slen)<0) {
888c43e99fdSEd Maste TT_FAIL(("Couldn't parse expected address %s on line %d",
889c43e99fdSEd Maste sockaddr_port, line));
890c43e99fdSEd Maste return -1;
891c43e99fdSEd Maste }
892c43e99fdSEd Maste if (ai->ai_family != ss.ss_family) {
893c43e99fdSEd Maste TT_FAIL(("Address family %d did not match %d on line %d",
894c43e99fdSEd Maste ai->ai_family, ss.ss_family, line));
895c43e99fdSEd Maste return -1;
896c43e99fdSEd Maste }
897c43e99fdSEd Maste if (ai->ai_addr->sa_family == AF_INET) {
898c43e99fdSEd Maste struct sockaddr_in *sin = (struct sockaddr_in*)ai->ai_addr;
899c43e99fdSEd Maste evutil_inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf));
900c43e99fdSEd Maste gotport = ntohs(sin->sin_port);
901c43e99fdSEd Maste if (ai->ai_addrlen != sizeof(struct sockaddr_in)) {
902c43e99fdSEd Maste TT_FAIL(("Addr size mismatch on line %d", line));
903c43e99fdSEd Maste return -1;
904c43e99fdSEd Maste }
905c43e99fdSEd Maste } else {
906c43e99fdSEd Maste struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)ai->ai_addr;
907c43e99fdSEd Maste evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, buf, sizeof(buf));
908c43e99fdSEd Maste gotport = ntohs(sin6->sin6_port);
909c43e99fdSEd Maste if (ai->ai_addrlen != sizeof(struct sockaddr_in6)) {
910c43e99fdSEd Maste TT_FAIL(("Addr size mismatch on line %d", line));
911c43e99fdSEd Maste return -1;
912c43e99fdSEd Maste }
913c43e99fdSEd Maste }
914c43e99fdSEd Maste if (evutil_sockaddr_cmp(ai->ai_addr, (struct sockaddr*)&ss, 1)) {
915c43e99fdSEd Maste TT_FAIL(("Wanted %s, got %s:%d on line %d", sockaddr_port,
916c43e99fdSEd Maste buf, gotport, line));
917c43e99fdSEd Maste return -1;
918c43e99fdSEd Maste } else {
919c43e99fdSEd Maste TT_BLATHER(("Wanted %s, got %s:%d on line %d", sockaddr_port,
920c43e99fdSEd Maste buf, gotport, line));
921c43e99fdSEd Maste }
922c43e99fdSEd Maste return 0;
923c43e99fdSEd Maste end:
924c43e99fdSEd Maste TT_FAIL(("Test failed on line %d", line));
925c43e99fdSEd Maste return -1;
926c43e99fdSEd Maste }
927c43e99fdSEd Maste
928c43e99fdSEd Maste static void
test_evutil_rand(void * arg)929c43e99fdSEd Maste test_evutil_rand(void *arg)
930c43e99fdSEd Maste {
931c43e99fdSEd Maste char buf1[32];
932c43e99fdSEd Maste char buf2[32];
933c43e99fdSEd Maste int counts[256];
934c43e99fdSEd Maste int i, j, k, n=0;
935c43e99fdSEd Maste struct evutil_weakrand_state seed = { 12346789U };
936c43e99fdSEd Maste
937c43e99fdSEd Maste memset(buf2, 0, sizeof(buf2));
938c43e99fdSEd Maste memset(counts, 0, sizeof(counts));
939c43e99fdSEd Maste
940c43e99fdSEd Maste for (k=0;k<32;++k) {
941c43e99fdSEd Maste /* Try a few different start and end points; try to catch
942c43e99fdSEd Maste * the various misaligned cases of arc4random_buf */
943c43e99fdSEd Maste int startpoint = evutil_weakrand_(&seed) % 4;
944c43e99fdSEd Maste int endpoint = 32 - (evutil_weakrand_(&seed) % 4);
945c43e99fdSEd Maste
946c43e99fdSEd Maste memset(buf2, 0, sizeof(buf2));
947c43e99fdSEd Maste
948c43e99fdSEd Maste /* Do 6 runs over buf1, or-ing the result into buf2 each
949c43e99fdSEd Maste * time, to make sure we're setting each byte that we mean
950c43e99fdSEd Maste * to set. */
951c43e99fdSEd Maste for (i=0;i<8;++i) {
952c43e99fdSEd Maste memset(buf1, 0, sizeof(buf1));
953c43e99fdSEd Maste evutil_secure_rng_get_bytes(buf1 + startpoint,
954c43e99fdSEd Maste endpoint-startpoint);
955c43e99fdSEd Maste n += endpoint - startpoint;
956c43e99fdSEd Maste for (j=0; j<32; ++j) {
957c43e99fdSEd Maste if (j >= startpoint && j < endpoint) {
958c43e99fdSEd Maste buf2[j] |= buf1[j];
959c43e99fdSEd Maste ++counts[(unsigned char)buf1[j]];
960c43e99fdSEd Maste } else {
961c43e99fdSEd Maste tt_assert(buf1[j] == 0);
962c43e99fdSEd Maste tt_int_op(buf1[j], ==, 0);
963c43e99fdSEd Maste
964c43e99fdSEd Maste }
965c43e99fdSEd Maste }
966c43e99fdSEd Maste }
967c43e99fdSEd Maste
968c43e99fdSEd Maste /* This will give a false positive with P=(256**8)==(2**64)
969c43e99fdSEd Maste * for each character. */
970c43e99fdSEd Maste for (j=startpoint;j<endpoint;++j) {
971c43e99fdSEd Maste tt_int_op(buf2[j], !=, 0);
972c43e99fdSEd Maste }
973c43e99fdSEd Maste }
974c43e99fdSEd Maste
975c43e99fdSEd Maste evutil_weakrand_seed_(&seed, 0);
976c43e99fdSEd Maste for (i = 0; i < 10000; ++i) {
977c43e99fdSEd Maste ev_int32_t r = evutil_weakrand_range_(&seed, 9999);
978c43e99fdSEd Maste tt_int_op(0, <=, r);
979c43e99fdSEd Maste tt_int_op(r, <, 9999);
980c43e99fdSEd Maste }
981c43e99fdSEd Maste
982c43e99fdSEd Maste /* for (i=0;i<256;++i) { printf("%3d %2d\n", i, counts[i]); } */
983c43e99fdSEd Maste end:
984c43e99fdSEd Maste ;
985c43e99fdSEd Maste }
986c43e99fdSEd Maste
987c43e99fdSEd Maste static void
test_EVUTIL_IS_(void * arg)988*b50261e2SCy Schubert test_EVUTIL_IS_(void *arg)
989*b50261e2SCy Schubert {
990*b50261e2SCy Schubert tt_int_op(EVUTIL_ISDIGIT_('0'), ==, 1);
991*b50261e2SCy Schubert tt_int_op(EVUTIL_ISDIGIT_('a'), ==, 0);
992*b50261e2SCy Schubert tt_int_op(EVUTIL_ISDIGIT_('\xff'), ==, 0);
993*b50261e2SCy Schubert end:
994*b50261e2SCy Schubert ;
995*b50261e2SCy Schubert }
996*b50261e2SCy Schubert
997*b50261e2SCy Schubert static void
test_evutil_getaddrinfo(void * arg)998c43e99fdSEd Maste test_evutil_getaddrinfo(void *arg)
999c43e99fdSEd Maste {
1000c43e99fdSEd Maste struct evutil_addrinfo *ai = NULL, *a;
1001c43e99fdSEd Maste struct evutil_addrinfo hints;
1002c43e99fdSEd Maste int r;
1003c43e99fdSEd Maste
1004c43e99fdSEd Maste /* Try using it as a pton. */
1005c43e99fdSEd Maste memset(&hints, 0, sizeof(hints));
1006c43e99fdSEd Maste hints.ai_family = PF_UNSPEC;
1007c43e99fdSEd Maste hints.ai_socktype = SOCK_STREAM;
1008c43e99fdSEd Maste r = evutil_getaddrinfo("1.2.3.4", "8080", &hints, &ai);
1009c43e99fdSEd Maste tt_int_op(r, ==, 0);
1010c43e99fdSEd Maste tt_assert(ai);
1011c43e99fdSEd Maste tt_ptr_op(ai->ai_next, ==, NULL); /* no ambiguity */
1012c43e99fdSEd Maste test_ai_eq(ai, "1.2.3.4:8080", SOCK_STREAM, IPPROTO_TCP);
1013c43e99fdSEd Maste evutil_freeaddrinfo(ai);
1014c43e99fdSEd Maste ai = NULL;
1015c43e99fdSEd Maste
1016c43e99fdSEd Maste memset(&hints, 0, sizeof(hints));
1017c43e99fdSEd Maste hints.ai_family = PF_UNSPEC;
1018c43e99fdSEd Maste hints.ai_protocol = IPPROTO_UDP;
1019c43e99fdSEd Maste r = evutil_getaddrinfo("1001:b0b::f00f", "4321", &hints, &ai);
1020c43e99fdSEd Maste tt_int_op(r, ==, 0);
1021c43e99fdSEd Maste tt_assert(ai);
1022c43e99fdSEd Maste tt_ptr_op(ai->ai_next, ==, NULL); /* no ambiguity */
1023c43e99fdSEd Maste test_ai_eq(ai, "[1001:b0b::f00f]:4321", SOCK_DGRAM, IPPROTO_UDP);
1024c43e99fdSEd Maste evutil_freeaddrinfo(ai);
1025c43e99fdSEd Maste ai = NULL;
1026c43e99fdSEd Maste
1027c43e99fdSEd Maste /* Try out the behavior of nodename=NULL */
1028c43e99fdSEd Maste memset(&hints, 0, sizeof(hints));
1029c43e99fdSEd Maste hints.ai_family = PF_INET;
1030c43e99fdSEd Maste hints.ai_protocol = IPPROTO_TCP;
1031c43e99fdSEd Maste hints.ai_flags = EVUTIL_AI_PASSIVE; /* as if for bind */
1032c43e99fdSEd Maste r = evutil_getaddrinfo(NULL, "9999", &hints, &ai);
1033c43e99fdSEd Maste tt_int_op(r,==,0);
1034c43e99fdSEd Maste tt_assert(ai);
1035c43e99fdSEd Maste tt_ptr_op(ai->ai_next, ==, NULL);
1036c43e99fdSEd Maste test_ai_eq(ai, "0.0.0.0:9999", SOCK_STREAM, IPPROTO_TCP);
1037c43e99fdSEd Maste evutil_freeaddrinfo(ai);
1038c43e99fdSEd Maste ai = NULL;
1039c43e99fdSEd Maste hints.ai_flags = 0; /* as if for connect */
1040c43e99fdSEd Maste r = evutil_getaddrinfo(NULL, "9998", &hints, &ai);
1041c43e99fdSEd Maste tt_assert(ai);
1042c43e99fdSEd Maste tt_int_op(r,==,0);
1043c43e99fdSEd Maste test_ai_eq(ai, "127.0.0.1:9998", SOCK_STREAM, IPPROTO_TCP);
1044c43e99fdSEd Maste tt_ptr_op(ai->ai_next, ==, NULL);
1045c43e99fdSEd Maste evutil_freeaddrinfo(ai);
1046c43e99fdSEd Maste ai = NULL;
1047c43e99fdSEd Maste
1048c43e99fdSEd Maste hints.ai_flags = 0; /* as if for connect */
1049c43e99fdSEd Maste hints.ai_family = PF_INET6;
1050c43e99fdSEd Maste r = evutil_getaddrinfo(NULL, "9997", &hints, &ai);
1051c43e99fdSEd Maste tt_assert(ai);
1052c43e99fdSEd Maste tt_int_op(r,==,0);
1053c43e99fdSEd Maste tt_ptr_op(ai->ai_next, ==, NULL);
1054c43e99fdSEd Maste test_ai_eq(ai, "[::1]:9997", SOCK_STREAM, IPPROTO_TCP);
1055c43e99fdSEd Maste evutil_freeaddrinfo(ai);
1056c43e99fdSEd Maste ai = NULL;
1057c43e99fdSEd Maste
1058c43e99fdSEd Maste hints.ai_flags = EVUTIL_AI_PASSIVE; /* as if for bind. */
1059c43e99fdSEd Maste hints.ai_family = PF_INET6;
1060c43e99fdSEd Maste r = evutil_getaddrinfo(NULL, "9996", &hints, &ai);
1061c43e99fdSEd Maste tt_assert(ai);
1062c43e99fdSEd Maste tt_int_op(r,==,0);
1063c43e99fdSEd Maste tt_ptr_op(ai->ai_next, ==, NULL);
1064c43e99fdSEd Maste test_ai_eq(ai, "[::]:9996", SOCK_STREAM, IPPROTO_TCP);
1065c43e99fdSEd Maste evutil_freeaddrinfo(ai);
1066c43e99fdSEd Maste ai = NULL;
1067c43e99fdSEd Maste
1068c43e99fdSEd Maste /* Now try an unspec one. We should get a v6 and a v4. */
1069c43e99fdSEd Maste hints.ai_family = PF_UNSPEC;
1070c43e99fdSEd Maste r = evutil_getaddrinfo(NULL, "9996", &hints, &ai);
1071c43e99fdSEd Maste tt_assert(ai);
1072c43e99fdSEd Maste tt_int_op(r,==,0);
1073c43e99fdSEd Maste a = ai_find_by_family(ai, PF_INET6);
1074c43e99fdSEd Maste tt_assert(a);
1075c43e99fdSEd Maste test_ai_eq(a, "[::]:9996", SOCK_STREAM, IPPROTO_TCP);
1076c43e99fdSEd Maste a = ai_find_by_family(ai, PF_INET);
1077c43e99fdSEd Maste tt_assert(a);
1078c43e99fdSEd Maste test_ai_eq(a, "0.0.0.0:9996", SOCK_STREAM, IPPROTO_TCP);
1079c43e99fdSEd Maste evutil_freeaddrinfo(ai);
1080c43e99fdSEd Maste ai = NULL;
1081c43e99fdSEd Maste
1082c43e99fdSEd Maste /* Try out AI_NUMERICHOST: successful case. Also try
1083c43e99fdSEd Maste * multiprotocol. */
1084c43e99fdSEd Maste memset(&hints, 0, sizeof(hints));
1085c43e99fdSEd Maste hints.ai_family = PF_UNSPEC;
1086c43e99fdSEd Maste hints.ai_flags = EVUTIL_AI_NUMERICHOST;
1087c43e99fdSEd Maste r = evutil_getaddrinfo("1.2.3.4", NULL, &hints, &ai);
1088c43e99fdSEd Maste tt_int_op(r, ==, 0);
1089c43e99fdSEd Maste a = ai_find_by_protocol(ai, IPPROTO_TCP);
1090c43e99fdSEd Maste tt_assert(a);
1091c43e99fdSEd Maste test_ai_eq(a, "1.2.3.4", SOCK_STREAM, IPPROTO_TCP);
1092c43e99fdSEd Maste a = ai_find_by_protocol(ai, IPPROTO_UDP);
1093c43e99fdSEd Maste tt_assert(a);
1094c43e99fdSEd Maste test_ai_eq(a, "1.2.3.4", SOCK_DGRAM, IPPROTO_UDP);
1095c43e99fdSEd Maste evutil_freeaddrinfo(ai);
1096c43e99fdSEd Maste ai = NULL;
1097c43e99fdSEd Maste
1098c43e99fdSEd Maste /* Try the failing case of AI_NUMERICHOST */
1099c43e99fdSEd Maste memset(&hints, 0, sizeof(hints));
1100c43e99fdSEd Maste hints.ai_family = PF_UNSPEC;
1101c43e99fdSEd Maste hints.ai_flags = EVUTIL_AI_NUMERICHOST;
1102c43e99fdSEd Maste r = evutil_getaddrinfo("www.google.com", "80", &hints, &ai);
1103c43e99fdSEd Maste tt_int_op(r, ==, EVUTIL_EAI_NONAME);
1104c43e99fdSEd Maste tt_ptr_op(ai, ==, NULL);
1105c43e99fdSEd Maste
1106c43e99fdSEd Maste /* Try symbolic service names wit AI_NUMERICSERV */
1107c43e99fdSEd Maste memset(&hints, 0, sizeof(hints));
1108c43e99fdSEd Maste hints.ai_family = PF_UNSPEC;
1109c43e99fdSEd Maste hints.ai_socktype = SOCK_STREAM;
1110c43e99fdSEd Maste hints.ai_flags = EVUTIL_AI_NUMERICSERV;
1111c43e99fdSEd Maste r = evutil_getaddrinfo("1.2.3.4", "http", &hints, &ai);
1112c43e99fdSEd Maste tt_int_op(r,==,EVUTIL_EAI_NONAME);
1113c43e99fdSEd Maste
1114c43e99fdSEd Maste /* Try symbolic service names */
1115c43e99fdSEd Maste memset(&hints, 0, sizeof(hints));
1116c43e99fdSEd Maste hints.ai_family = PF_UNSPEC;
1117c43e99fdSEd Maste hints.ai_socktype = SOCK_STREAM;
1118c43e99fdSEd Maste r = evutil_getaddrinfo("1.2.3.4", "http", &hints, &ai);
1119c43e99fdSEd Maste if (r!=0) {
1120c43e99fdSEd Maste TT_DECLARE("SKIP", ("Symbolic service names seem broken."));
1121c43e99fdSEd Maste } else {
1122c43e99fdSEd Maste tt_assert(ai);
1123c43e99fdSEd Maste test_ai_eq(ai, "1.2.3.4:80", SOCK_STREAM, IPPROTO_TCP);
1124c43e99fdSEd Maste evutil_freeaddrinfo(ai);
1125c43e99fdSEd Maste ai = NULL;
1126c43e99fdSEd Maste }
1127c43e99fdSEd Maste
1128c43e99fdSEd Maste end:
1129c43e99fdSEd Maste if (ai)
1130c43e99fdSEd Maste evutil_freeaddrinfo(ai);
1131c43e99fdSEd Maste }
1132c43e99fdSEd Maste
1133c43e99fdSEd Maste static void
test_evutil_getaddrinfo_live(void * arg)1134c43e99fdSEd Maste test_evutil_getaddrinfo_live(void *arg)
1135c43e99fdSEd Maste {
1136c43e99fdSEd Maste struct evutil_addrinfo *ai = NULL;
1137c43e99fdSEd Maste struct evutil_addrinfo hints;
1138c43e99fdSEd Maste
1139c43e99fdSEd Maste struct sockaddr_in6 *sin6;
1140c43e99fdSEd Maste struct sockaddr_in *sin;
1141c43e99fdSEd Maste char buf[128];
1142c43e99fdSEd Maste const char *cp;
1143c43e99fdSEd Maste int r;
1144c43e99fdSEd Maste
1145c43e99fdSEd Maste /* Now do some actual lookups. */
1146c43e99fdSEd Maste memset(&hints, 0, sizeof(hints));
1147c43e99fdSEd Maste hints.ai_family = PF_INET;
1148c43e99fdSEd Maste hints.ai_protocol = IPPROTO_TCP;
1149c43e99fdSEd Maste hints.ai_socktype = SOCK_STREAM;
1150c43e99fdSEd Maste r = evutil_getaddrinfo("www.google.com", "80", &hints, &ai);
1151c43e99fdSEd Maste if (r != 0) {
1152c43e99fdSEd Maste TT_DECLARE("SKIP", ("Couldn't resolve www.google.com"));
1153c43e99fdSEd Maste } else {
1154c43e99fdSEd Maste tt_assert(ai);
1155c43e99fdSEd Maste tt_int_op(ai->ai_family, ==, PF_INET);
1156c43e99fdSEd Maste tt_int_op(ai->ai_protocol, ==, IPPROTO_TCP);
1157c43e99fdSEd Maste tt_int_op(ai->ai_socktype, ==, SOCK_STREAM);
1158c43e99fdSEd Maste tt_int_op(ai->ai_addrlen, ==, sizeof(struct sockaddr_in));
1159c43e99fdSEd Maste sin = (struct sockaddr_in*)ai->ai_addr;
1160c43e99fdSEd Maste tt_int_op(sin->sin_family, ==, AF_INET);
1161c43e99fdSEd Maste tt_int_op(sin->sin_port, ==, htons(80));
1162c43e99fdSEd Maste tt_int_op(sin->sin_addr.s_addr, !=, 0xffffffff);
1163c43e99fdSEd Maste
1164c43e99fdSEd Maste cp = evutil_inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf));
1165c43e99fdSEd Maste TT_BLATHER(("www.google.com resolved to %s",
1166c43e99fdSEd Maste cp?cp:"<unwriteable>"));
1167c43e99fdSEd Maste evutil_freeaddrinfo(ai);
1168c43e99fdSEd Maste ai = NULL;
1169c43e99fdSEd Maste }
1170c43e99fdSEd Maste
1171c43e99fdSEd Maste hints.ai_family = PF_INET6;
1172c43e99fdSEd Maste r = evutil_getaddrinfo("ipv6.google.com", "80", &hints, &ai);
1173c43e99fdSEd Maste if (r != 0) {
1174c43e99fdSEd Maste TT_BLATHER(("Couldn't do an ipv6 lookup for ipv6.google.com"));
1175c43e99fdSEd Maste } else {
1176c43e99fdSEd Maste tt_assert(ai);
1177c43e99fdSEd Maste tt_int_op(ai->ai_family, ==, PF_INET6);
1178c43e99fdSEd Maste tt_int_op(ai->ai_addrlen, ==, sizeof(struct sockaddr_in6));
1179c43e99fdSEd Maste sin6 = (struct sockaddr_in6*)ai->ai_addr;
1180c43e99fdSEd Maste tt_int_op(sin6->sin6_port, ==, htons(80));
1181c43e99fdSEd Maste
1182c43e99fdSEd Maste cp = evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, buf,
1183c43e99fdSEd Maste sizeof(buf));
1184c43e99fdSEd Maste TT_BLATHER(("ipv6.google.com resolved to %s",
1185c43e99fdSEd Maste cp?cp:"<unwriteable>"));
1186c43e99fdSEd Maste }
1187c43e99fdSEd Maste
1188c43e99fdSEd Maste end:
1189c43e99fdSEd Maste if (ai)
1190c43e99fdSEd Maste evutil_freeaddrinfo(ai);
1191c43e99fdSEd Maste }
1192c43e99fdSEd Maste
1193*b50261e2SCy Schubert static void
test_evutil_getaddrinfo_AI_ADDRCONFIG(void * arg)1194*b50261e2SCy Schubert test_evutil_getaddrinfo_AI_ADDRCONFIG(void *arg)
1195*b50261e2SCy Schubert {
1196*b50261e2SCy Schubert struct evutil_addrinfo *ai = NULL;
1197*b50261e2SCy Schubert struct evutil_addrinfo hints;
1198*b50261e2SCy Schubert int r;
1199*b50261e2SCy Schubert
1200*b50261e2SCy Schubert memset(&hints, 0, sizeof(hints));
1201*b50261e2SCy Schubert hints.ai_family = AF_UNSPEC;
1202*b50261e2SCy Schubert hints.ai_socktype = SOCK_STREAM;
1203*b50261e2SCy Schubert hints.ai_flags = EVUTIL_AI_PASSIVE|EVUTIL_AI_ADDRCONFIG;
1204*b50261e2SCy Schubert
1205*b50261e2SCy Schubert /* IPv4 */
1206*b50261e2SCy Schubert r = evutil_getaddrinfo("127.0.0.1", "80", &hints, &ai);
1207*b50261e2SCy Schubert tt_int_op(r, ==, 0);
1208*b50261e2SCy Schubert tt_assert(ai);
1209*b50261e2SCy Schubert tt_ptr_op(ai->ai_next, ==, NULL);
1210*b50261e2SCy Schubert test_ai_eq(ai, "127.0.0.1:80", SOCK_STREAM, IPPROTO_TCP);
1211*b50261e2SCy Schubert evutil_freeaddrinfo(ai);
1212*b50261e2SCy Schubert ai = NULL;
1213*b50261e2SCy Schubert
1214*b50261e2SCy Schubert /* IPv6 */
1215*b50261e2SCy Schubert r = evutil_getaddrinfo("::1", "80", &hints, &ai);
1216*b50261e2SCy Schubert tt_int_op(r, ==, 0);
1217*b50261e2SCy Schubert tt_assert(ai);
1218*b50261e2SCy Schubert tt_ptr_op(ai->ai_next, ==, NULL);
1219*b50261e2SCy Schubert test_ai_eq(ai, "[::1]:80", SOCK_STREAM, IPPROTO_TCP);
1220*b50261e2SCy Schubert evutil_freeaddrinfo(ai);
1221*b50261e2SCy Schubert ai = NULL;
1222*b50261e2SCy Schubert
1223*b50261e2SCy Schubert end:
1224*b50261e2SCy Schubert if (ai)
1225*b50261e2SCy Schubert evutil_freeaddrinfo(ai);
1226*b50261e2SCy Schubert }
1227*b50261e2SCy Schubert
1228c43e99fdSEd Maste #ifdef _WIN32
1229c43e99fdSEd Maste static void
test_evutil_loadsyslib(void * arg)1230c43e99fdSEd Maste test_evutil_loadsyslib(void *arg)
1231c43e99fdSEd Maste {
1232c43e99fdSEd Maste HMODULE h=NULL;
1233c43e99fdSEd Maste
1234c43e99fdSEd Maste h = evutil_load_windows_system_library_(TEXT("kernel32.dll"));
1235c43e99fdSEd Maste tt_assert(h);
1236c43e99fdSEd Maste
1237c43e99fdSEd Maste end:
1238c43e99fdSEd Maste if (h)
1239c43e99fdSEd Maste CloseHandle(h);
1240c43e99fdSEd Maste
1241c43e99fdSEd Maste }
1242c43e99fdSEd Maste #endif
1243c43e99fdSEd Maste
1244c43e99fdSEd Maste /** Test mm_malloc(). */
1245c43e99fdSEd Maste static void
test_event_malloc(void * arg)1246c43e99fdSEd Maste test_event_malloc(void *arg)
1247c43e99fdSEd Maste {
1248c43e99fdSEd Maste void *p = NULL;
1249c43e99fdSEd Maste (void)arg;
1250c43e99fdSEd Maste
1251c43e99fdSEd Maste /* mm_malloc(0) should simply return NULL. */
1252c43e99fdSEd Maste #ifndef EVENT__DISABLE_MM_REPLACEMENT
1253c43e99fdSEd Maste errno = 0;
1254c43e99fdSEd Maste p = mm_malloc(0);
1255c43e99fdSEd Maste tt_assert(p == NULL);
1256c43e99fdSEd Maste tt_int_op(errno, ==, 0);
1257c43e99fdSEd Maste #endif
1258c43e99fdSEd Maste
1259c43e99fdSEd Maste /* Trivial case. */
1260c43e99fdSEd Maste errno = 0;
1261c43e99fdSEd Maste p = mm_malloc(8);
1262c43e99fdSEd Maste tt_assert(p != NULL);
1263c43e99fdSEd Maste tt_int_op(errno, ==, 0);
1264c43e99fdSEd Maste mm_free(p);
1265c43e99fdSEd Maste
1266c43e99fdSEd Maste end:
1267c43e99fdSEd Maste errno = 0;
1268c43e99fdSEd Maste return;
1269c43e99fdSEd Maste }
1270c43e99fdSEd Maste
1271c43e99fdSEd Maste static void
test_event_calloc(void * arg)1272c43e99fdSEd Maste test_event_calloc(void *arg)
1273c43e99fdSEd Maste {
1274c43e99fdSEd Maste void *p = NULL;
1275c43e99fdSEd Maste (void)arg;
1276c43e99fdSEd Maste
1277c43e99fdSEd Maste #ifndef EVENT__DISABLE_MM_REPLACEMENT
1278c43e99fdSEd Maste /* mm_calloc() should simply return NULL
1279c43e99fdSEd Maste * if either argument is zero. */
1280c43e99fdSEd Maste errno = 0;
1281c43e99fdSEd Maste p = mm_calloc(0, 0);
1282c43e99fdSEd Maste tt_assert(p == NULL);
1283c43e99fdSEd Maste tt_int_op(errno, ==, 0);
1284c43e99fdSEd Maste errno = 0;
1285c43e99fdSEd Maste p = mm_calloc(0, 1);
1286c43e99fdSEd Maste tt_assert(p == NULL);
1287c43e99fdSEd Maste tt_int_op(errno, ==, 0);
1288c43e99fdSEd Maste errno = 0;
1289c43e99fdSEd Maste p = mm_calloc(1, 0);
1290c43e99fdSEd Maste tt_assert(p == NULL);
1291c43e99fdSEd Maste tt_int_op(errno, ==, 0);
1292c43e99fdSEd Maste #endif
1293c43e99fdSEd Maste
1294c43e99fdSEd Maste /* Trivial case. */
1295c43e99fdSEd Maste errno = 0;
1296c43e99fdSEd Maste p = mm_calloc(8, 8);
1297c43e99fdSEd Maste tt_assert(p != NULL);
1298c43e99fdSEd Maste tt_int_op(errno, ==, 0);
1299c43e99fdSEd Maste mm_free(p);
1300c43e99fdSEd Maste p = NULL;
1301c43e99fdSEd Maste
1302c43e99fdSEd Maste /* mm_calloc() should set errno = ENOMEM and return NULL
1303c43e99fdSEd Maste * in case of potential overflow. */
1304c43e99fdSEd Maste errno = 0;
1305c43e99fdSEd Maste p = mm_calloc(EV_SIZE_MAX/2, EV_SIZE_MAX/2 + 8);
1306c43e99fdSEd Maste tt_assert(p == NULL);
1307c43e99fdSEd Maste tt_int_op(errno, ==, ENOMEM);
1308c43e99fdSEd Maste
1309c43e99fdSEd Maste end:
1310c43e99fdSEd Maste errno = 0;
1311c43e99fdSEd Maste if (p)
1312c43e99fdSEd Maste mm_free(p);
1313c43e99fdSEd Maste
1314c43e99fdSEd Maste return;
1315c43e99fdSEd Maste }
1316c43e99fdSEd Maste
1317c43e99fdSEd Maste static void
test_event_strdup(void * arg)1318c43e99fdSEd Maste test_event_strdup(void *arg)
1319c43e99fdSEd Maste {
1320c43e99fdSEd Maste void *p = NULL;
1321c43e99fdSEd Maste (void)arg;
1322c43e99fdSEd Maste
1323c43e99fdSEd Maste #ifndef EVENT__DISABLE_MM_REPLACEMENT
1324c43e99fdSEd Maste /* mm_strdup(NULL) should set errno = EINVAL and return NULL. */
1325c43e99fdSEd Maste errno = 0;
1326c43e99fdSEd Maste p = mm_strdup(NULL);
1327c43e99fdSEd Maste tt_assert(p == NULL);
1328c43e99fdSEd Maste tt_int_op(errno, ==, EINVAL);
1329c43e99fdSEd Maste #endif
1330c43e99fdSEd Maste
1331c43e99fdSEd Maste /* Trivial cases. */
1332c43e99fdSEd Maste
1333c43e99fdSEd Maste errno = 0;
1334c43e99fdSEd Maste p = mm_strdup("");
1335c43e99fdSEd Maste tt_assert(p != NULL);
1336c43e99fdSEd Maste tt_int_op(errno, ==, 0);
1337c43e99fdSEd Maste tt_str_op(p, ==, "");
1338c43e99fdSEd Maste mm_free(p);
1339c43e99fdSEd Maste
1340c43e99fdSEd Maste errno = 0;
1341c43e99fdSEd Maste p = mm_strdup("foo");
1342c43e99fdSEd Maste tt_assert(p != NULL);
1343c43e99fdSEd Maste tt_int_op(errno, ==, 0);
1344c43e99fdSEd Maste tt_str_op(p, ==, "foo");
1345c43e99fdSEd Maste mm_free(p);
1346c43e99fdSEd Maste
1347c43e99fdSEd Maste /* XXX
1348c43e99fdSEd Maste * mm_strdup(str) where str is a string of length EV_SIZE_MAX
1349c43e99fdSEd Maste * should set errno = ENOMEM and return NULL. */
1350c43e99fdSEd Maste
1351c43e99fdSEd Maste end:
1352c43e99fdSEd Maste errno = 0;
1353c43e99fdSEd Maste return;
1354c43e99fdSEd Maste }
1355c43e99fdSEd Maste
1356c43e99fdSEd Maste static void
test_evutil_usleep(void * arg)1357c43e99fdSEd Maste test_evutil_usleep(void *arg)
1358c43e99fdSEd Maste {
1359c43e99fdSEd Maste struct timeval tv1, tv2, tv3, diff1, diff2;
1360c43e99fdSEd Maste const struct timeval quarter_sec = {0, 250*1000};
1361c43e99fdSEd Maste const struct timeval tenth_sec = {0, 100*1000};
1362c43e99fdSEd Maste long usec1, usec2;
1363c43e99fdSEd Maste
1364c43e99fdSEd Maste evutil_gettimeofday(&tv1, NULL);
1365c43e99fdSEd Maste evutil_usleep_(&quarter_sec);
1366c43e99fdSEd Maste evutil_gettimeofday(&tv2, NULL);
1367c43e99fdSEd Maste evutil_usleep_(&tenth_sec);
1368c43e99fdSEd Maste evutil_gettimeofday(&tv3, NULL);
1369c43e99fdSEd Maste
1370c43e99fdSEd Maste evutil_timersub(&tv2, &tv1, &diff1);
1371c43e99fdSEd Maste evutil_timersub(&tv3, &tv2, &diff2);
1372c43e99fdSEd Maste usec1 = diff1.tv_sec * 1000000 + diff1.tv_usec;
1373c43e99fdSEd Maste usec2 = diff2.tv_sec * 1000000 + diff2.tv_usec;
1374c43e99fdSEd Maste
1375c43e99fdSEd Maste tt_int_op(usec1, >, 200000);
1376c43e99fdSEd Maste tt_int_op(usec1, <, 300000);
1377c43e99fdSEd Maste tt_int_op(usec2, >, 80000);
1378c43e99fdSEd Maste tt_int_op(usec2, <, 120000);
1379c43e99fdSEd Maste
1380c43e99fdSEd Maste end:
1381c43e99fdSEd Maste ;
1382c43e99fdSEd Maste }
1383c43e99fdSEd Maste
1384c43e99fdSEd Maste static void
test_evutil_monotonic_res(void * data_)1385c43e99fdSEd Maste test_evutil_monotonic_res(void *data_)
1386c43e99fdSEd Maste {
1387c43e99fdSEd Maste /* Basic santity-test for monotonic timers. What we'd really like
1388c43e99fdSEd Maste * to do is make sure that they can't go backwards even when the
1389c43e99fdSEd Maste * system clock goes backwards. But we haven't got a good way to
1390c43e99fdSEd Maste * move the system clock backwards.
1391c43e99fdSEd Maste */
1392c43e99fdSEd Maste struct basic_test_data *data = data_;
1393c43e99fdSEd Maste struct evutil_monotonic_timer timer;
1394c43e99fdSEd Maste const int precise = strstr(data->setup_data, "precise") != NULL;
1395c43e99fdSEd Maste const int fallback = strstr(data->setup_data, "fallback") != NULL;
1396c43e99fdSEd Maste struct timeval tv[10], delay;
1397c43e99fdSEd Maste int total_diff = 0;
1398c43e99fdSEd Maste
1399c43e99fdSEd Maste int flags = 0, wantres, acceptdiff, i;
1400c43e99fdSEd Maste if (precise)
1401c43e99fdSEd Maste flags |= EV_MONOT_PRECISE;
1402c43e99fdSEd Maste if (fallback)
1403c43e99fdSEd Maste flags |= EV_MONOT_FALLBACK;
1404c43e99fdSEd Maste if (precise || fallback) {
1405c43e99fdSEd Maste #ifdef _WIN32
1406c43e99fdSEd Maste wantres = 10*1000;
1407c43e99fdSEd Maste acceptdiff = 1000;
1408c43e99fdSEd Maste #else
1409c43e99fdSEd Maste wantres = 1000;
1410c43e99fdSEd Maste acceptdiff = 300;
1411c43e99fdSEd Maste #endif
1412c43e99fdSEd Maste } else {
1413c43e99fdSEd Maste wantres = 40*1000;
1414c43e99fdSEd Maste acceptdiff = 20*1000;
1415c43e99fdSEd Maste }
1416c43e99fdSEd Maste
1417c43e99fdSEd Maste TT_BLATHER(("Precise = %d", precise));
1418c43e99fdSEd Maste TT_BLATHER(("Fallback = %d", fallback));
1419c43e99fdSEd Maste
1420c43e99fdSEd Maste /* First, make sure we match up with usleep. */
1421c43e99fdSEd Maste
1422c43e99fdSEd Maste delay.tv_sec = 0;
1423c43e99fdSEd Maste delay.tv_usec = wantres;
1424c43e99fdSEd Maste
1425c43e99fdSEd Maste tt_int_op(evutil_configure_monotonic_time_(&timer, flags), ==, 0);
1426c43e99fdSEd Maste
1427c43e99fdSEd Maste for (i = 0; i < 10; ++i) {
1428c43e99fdSEd Maste evutil_gettime_monotonic_(&timer, &tv[i]);
1429c43e99fdSEd Maste evutil_usleep_(&delay);
1430c43e99fdSEd Maste }
1431c43e99fdSEd Maste
1432c43e99fdSEd Maste for (i = 0; i < 9; ++i) {
1433c43e99fdSEd Maste struct timeval diff;
1434c43e99fdSEd Maste tt_assert(evutil_timercmp(&tv[i], &tv[i+1], <));
1435c43e99fdSEd Maste evutil_timersub(&tv[i+1], &tv[i], &diff);
1436c43e99fdSEd Maste tt_int_op(diff.tv_sec, ==, 0);
1437c43e99fdSEd Maste total_diff += diff.tv_usec;
1438c43e99fdSEd Maste TT_BLATHER(("Difference = %d", (int)diff.tv_usec));
1439c43e99fdSEd Maste }
1440c43e99fdSEd Maste tt_int_op(abs(total_diff/9 - wantres), <, acceptdiff);
1441c43e99fdSEd Maste
1442c43e99fdSEd Maste end:
1443c43e99fdSEd Maste ;
1444c43e99fdSEd Maste }
1445c43e99fdSEd Maste
1446c43e99fdSEd Maste static void
test_evutil_monotonic_prc(void * data_)1447c43e99fdSEd Maste test_evutil_monotonic_prc(void *data_)
1448c43e99fdSEd Maste {
1449c43e99fdSEd Maste struct basic_test_data *data = data_;
1450c43e99fdSEd Maste struct evutil_monotonic_timer timer;
1451c43e99fdSEd Maste const int precise = strstr(data->setup_data, "precise") != NULL;
1452c43e99fdSEd Maste const int fallback = strstr(data->setup_data, "fallback") != NULL;
1453c43e99fdSEd Maste struct timeval tv[10];
1454c43e99fdSEd Maste int total_diff = 0;
1455c43e99fdSEd Maste int i, maxstep = 25*1000,flags=0;
1456c43e99fdSEd Maste if (precise)
1457c43e99fdSEd Maste maxstep = 500;
1458c43e99fdSEd Maste if (precise)
1459c43e99fdSEd Maste flags |= EV_MONOT_PRECISE;
1460c43e99fdSEd Maste if (fallback)
1461c43e99fdSEd Maste flags |= EV_MONOT_FALLBACK;
1462c43e99fdSEd Maste tt_int_op(evutil_configure_monotonic_time_(&timer, flags), ==, 0);
1463c43e99fdSEd Maste
1464c43e99fdSEd Maste /* find out what precision we actually see. */
1465c43e99fdSEd Maste
1466c43e99fdSEd Maste evutil_gettime_monotonic_(&timer, &tv[0]);
1467c43e99fdSEd Maste for (i = 1; i < 10; ++i) {
1468c43e99fdSEd Maste do {
1469c43e99fdSEd Maste evutil_gettime_monotonic_(&timer, &tv[i]);
1470c43e99fdSEd Maste } while (evutil_timercmp(&tv[i-1], &tv[i], ==));
1471c43e99fdSEd Maste }
1472c43e99fdSEd Maste
1473c43e99fdSEd Maste total_diff = 0;
1474c43e99fdSEd Maste for (i = 0; i < 9; ++i) {
1475c43e99fdSEd Maste struct timeval diff;
1476c43e99fdSEd Maste tt_assert(evutil_timercmp(&tv[i], &tv[i+1], <));
1477c43e99fdSEd Maste evutil_timersub(&tv[i+1], &tv[i], &diff);
1478c43e99fdSEd Maste tt_int_op(diff.tv_sec, ==, 0);
1479c43e99fdSEd Maste total_diff += diff.tv_usec;
1480c43e99fdSEd Maste TT_BLATHER(("Step difference = %d", (int)diff.tv_usec));
1481c43e99fdSEd Maste }
1482c43e99fdSEd Maste TT_BLATHER(("Average step difference = %d", total_diff / 9));
1483c43e99fdSEd Maste tt_int_op(total_diff/9, <, maxstep);
1484c43e99fdSEd Maste
1485c43e99fdSEd Maste end:
1486c43e99fdSEd Maste ;
1487c43e99fdSEd Maste }
1488c43e99fdSEd Maste
1489c43e99fdSEd Maste static void
create_tm_from_unix_epoch(struct tm * cur_p,const time_t t)1490c43e99fdSEd Maste create_tm_from_unix_epoch(struct tm *cur_p, const time_t t)
1491c43e99fdSEd Maste {
1492c43e99fdSEd Maste #ifdef _WIN32
1493c43e99fdSEd Maste struct tm *tmp = gmtime(&t);
1494c43e99fdSEd Maste if (!tmp) {
1495c43e99fdSEd Maste fprintf(stderr, "gmtime: %s (%i)", strerror(errno), (int)t);
1496c43e99fdSEd Maste exit(1);
1497c43e99fdSEd Maste }
1498c43e99fdSEd Maste *cur_p = *tmp;
1499c43e99fdSEd Maste #else
1500c43e99fdSEd Maste gmtime_r(&t, cur_p);
1501c43e99fdSEd Maste #endif
1502c43e99fdSEd Maste }
1503c43e99fdSEd Maste
1504c43e99fdSEd Maste static struct date_rfc1123_case {
1505c43e99fdSEd Maste time_t t;
1506c43e99fdSEd Maste char date[30];
1507c43e99fdSEd Maste } date_rfc1123_cases[] = {
1508c43e99fdSEd Maste { 0, "Thu, 01 Jan 1970 00:00:00 GMT"} /* UNIX time of zero */,
1509c43e99fdSEd Maste { 946684799, "Fri, 31 Dec 1999 23:59:59 GMT"} /* the last moment of the 20th century */,
1510c43e99fdSEd Maste { 946684800, "Sat, 01 Jan 2000 00:00:00 GMT"} /* the first moment of the 21st century */,
1511c43e99fdSEd Maste { 981072000, "Fri, 02 Feb 2001 00:00:00 GMT"},
1512c43e99fdSEd Maste { 1015113600, "Sun, 03 Mar 2002 00:00:00 GMT"},
1513c43e99fdSEd Maste { 1049414400, "Fri, 04 Apr 2003 00:00:00 GMT"},
1514c43e99fdSEd Maste { 1083715200, "Wed, 05 May 2004 00:00:00 GMT"},
1515c43e99fdSEd Maste { 1118016000, "Mon, 06 Jun 2005 00:00:00 GMT"},
1516c43e99fdSEd Maste { 1152230400, "Fri, 07 Jul 2006 00:00:00 GMT"},
1517c43e99fdSEd Maste { 1186531200, "Wed, 08 Aug 2007 00:00:00 GMT"},
1518c43e99fdSEd Maste { 1220918400, "Tue, 09 Sep 2008 00:00:00 GMT"},
1519c43e99fdSEd Maste { 1255132800, "Sat, 10 Oct 2009 00:00:00 GMT"},
1520c43e99fdSEd Maste { 1289433600, "Thu, 11 Nov 2010 00:00:00 GMT"},
1521c43e99fdSEd Maste { 1323648000, "Mon, 12 Dec 2011 00:00:00 GMT"},
1522c43e99fdSEd Maste #ifndef _WIN32
1523*b50261e2SCy Schubert #if EVENT__SIZEOF_TIME_T > 4
1524c43e99fdSEd Maste /** In win32 case we have max "23:59:59 January 18, 2038, UTC" for time32 */
1525c43e99fdSEd Maste { 4294967296, "Sun, 07 Feb 2106 06:28:16 GMT"} /* 2^32 */,
1526c43e99fdSEd Maste /** In win32 case we have max "23:59:59, December 31, 3000, UTC" for time64 */
1527c43e99fdSEd Maste {253402300799, "Fri, 31 Dec 9999 23:59:59 GMT"} /* long long future no one can imagine */,
1528*b50261e2SCy Schubert #endif /* time_t != 32bit */
1529c43e99fdSEd Maste { 1456704000, "Mon, 29 Feb 2016 00:00:00 GMT"} /* leap year */,
1530c43e99fdSEd Maste #endif
1531c43e99fdSEd Maste { 1435708800, "Wed, 01 Jul 2015 00:00:00 GMT"} /* leap second */,
1532c43e99fdSEd Maste { 1481866376, "Fri, 16 Dec 2016 05:32:56 GMT"} /* the time this test case is generated */,
1533c43e99fdSEd Maste {0, ""} /* end of test cases. */
1534c43e99fdSEd Maste };
1535c43e99fdSEd Maste
1536c43e99fdSEd Maste static void
test_evutil_date_rfc1123(void * arg)1537c43e99fdSEd Maste test_evutil_date_rfc1123(void *arg)
1538c43e99fdSEd Maste {
1539c43e99fdSEd Maste struct tm query;
1540c43e99fdSEd Maste char result[30];
1541c43e99fdSEd Maste size_t i = 0;
1542c43e99fdSEd Maste
1543c43e99fdSEd Maste /* Checks if too small buffers are safely accepted. */
1544c43e99fdSEd Maste {
1545c43e99fdSEd Maste create_tm_from_unix_epoch(&query, 0);
1546c43e99fdSEd Maste evutil_date_rfc1123(result, 8, &query);
1547c43e99fdSEd Maste tt_str_op(result, ==, "Thu, 01");
1548c43e99fdSEd Maste }
1549c43e99fdSEd Maste
1550c43e99fdSEd Maste /* Checks for testcases. */
1551c43e99fdSEd Maste for (i = 0; ; i++) {
1552c43e99fdSEd Maste struct date_rfc1123_case c = date_rfc1123_cases[i];
1553c43e99fdSEd Maste
1554c43e99fdSEd Maste if (strlen(c.date) == 0)
1555c43e99fdSEd Maste break;
1556c43e99fdSEd Maste
1557c43e99fdSEd Maste create_tm_from_unix_epoch(&query, c.t);
1558c43e99fdSEd Maste evutil_date_rfc1123(result, sizeof(result), &query);
1559c43e99fdSEd Maste tt_str_op(result, ==, c.date);
1560c43e99fdSEd Maste }
1561c43e99fdSEd Maste
1562c43e99fdSEd Maste end:
1563c43e99fdSEd Maste ;
1564c43e99fdSEd Maste }
1565c43e99fdSEd Maste
1566*b50261e2SCy Schubert static void
test_evutil_v4addr_is_local(void * arg)1567*b50261e2SCy Schubert test_evutil_v4addr_is_local(void *arg)
1568*b50261e2SCy Schubert {
1569*b50261e2SCy Schubert struct sockaddr_in sin;
1570*b50261e2SCy Schubert sin.sin_family = AF_INET;
1571*b50261e2SCy Schubert
1572*b50261e2SCy Schubert /* we use evutil_inet_pton() here to fill in network-byte order */
1573*b50261e2SCy Schubert #define LOCAL(str, yes) do { \
1574*b50261e2SCy Schubert tt_int_op(evutil_inet_pton(AF_INET, str, &sin.sin_addr), ==, 1); \
1575*b50261e2SCy Schubert tt_int_op(evutil_v4addr_is_local_(&sin.sin_addr), ==, yes); \
1576*b50261e2SCy Schubert } while (0)
1577*b50261e2SCy Schubert
1578*b50261e2SCy Schubert /** any */
1579*b50261e2SCy Schubert sin.sin_addr.s_addr = INADDR_ANY;
1580*b50261e2SCy Schubert tt_int_op(evutil_v4addr_is_local_(&sin.sin_addr), ==, 1);
1581*b50261e2SCy Schubert
1582*b50261e2SCy Schubert /** loopback */
1583*b50261e2SCy Schubert sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1584*b50261e2SCy Schubert tt_int_op(evutil_v4addr_is_local_(&sin.sin_addr), ==, 1);
1585*b50261e2SCy Schubert LOCAL("127.0.0.1", 1);
1586*b50261e2SCy Schubert LOCAL("127.255.255.255", 1);
1587*b50261e2SCy Schubert LOCAL("121.0.0.1", 0);
1588*b50261e2SCy Schubert
1589*b50261e2SCy Schubert /** link-local */
1590*b50261e2SCy Schubert LOCAL("169.254.0.1", 1);
1591*b50261e2SCy Schubert LOCAL("169.254.255.255", 1);
1592*b50261e2SCy Schubert LOCAL("170.0.0.0", 0);
1593*b50261e2SCy Schubert
1594*b50261e2SCy Schubert /** Multicast */
1595*b50261e2SCy Schubert LOCAL("224.0.0.0", 1);
1596*b50261e2SCy Schubert LOCAL("239.255.255.255", 1);
1597*b50261e2SCy Schubert LOCAL("240.0.0.0", 0);
1598*b50261e2SCy Schubert end:
1599*b50261e2SCy Schubert ;
1600*b50261e2SCy Schubert }
1601*b50261e2SCy Schubert
1602*b50261e2SCy Schubert static void
test_evutil_v6addr_is_local(void * arg)1603*b50261e2SCy Schubert test_evutil_v6addr_is_local(void *arg)
1604*b50261e2SCy Schubert {
1605*b50261e2SCy Schubert struct sockaddr_in6 sin6;
1606*b50261e2SCy Schubert struct in6_addr anyaddr = IN6ADDR_ANY_INIT;
1607*b50261e2SCy Schubert struct in6_addr loopback = IN6ADDR_LOOPBACK_INIT;
1608*b50261e2SCy Schubert
1609*b50261e2SCy Schubert sin6.sin6_family = AF_INET6;
1610*b50261e2SCy Schubert #define LOCAL6(str, yes) do { \
1611*b50261e2SCy Schubert tt_int_op(evutil_inet_pton(AF_INET6, str, &sin6.sin6_addr), ==, 1);\
1612*b50261e2SCy Schubert tt_int_op(evutil_v6addr_is_local_(&sin6.sin6_addr), ==, yes); \
1613*b50261e2SCy Schubert } while (0)
1614*b50261e2SCy Schubert
1615*b50261e2SCy Schubert /** any */
1616*b50261e2SCy Schubert tt_int_op(evutil_v6addr_is_local_(&anyaddr), ==, 1);
1617*b50261e2SCy Schubert LOCAL6("::0", 1);
1618*b50261e2SCy Schubert
1619*b50261e2SCy Schubert /** loopback */
1620*b50261e2SCy Schubert tt_int_op(evutil_v6addr_is_local_(&loopback), ==, 1);
1621*b50261e2SCy Schubert LOCAL6("::1", 1);
1622*b50261e2SCy Schubert
1623*b50261e2SCy Schubert /** IPV4 mapped */
1624*b50261e2SCy Schubert LOCAL6("::ffff:0:0", 1);
1625*b50261e2SCy Schubert /** IPv4 translated */
1626*b50261e2SCy Schubert LOCAL6("::ffff:0:0:0", 1);
1627*b50261e2SCy Schubert /** IPv4/IPv6 translation */
1628*b50261e2SCy Schubert LOCAL6("64:ff9b::", 0);
1629*b50261e2SCy Schubert /** Link-local */
1630*b50261e2SCy Schubert LOCAL6("fe80::", 1);
1631*b50261e2SCy Schubert /** Multicast */
1632*b50261e2SCy Schubert LOCAL6("ff00::", 1);
1633*b50261e2SCy Schubert /** Unspecified */
1634*b50261e2SCy Schubert LOCAL6("::", 1);
1635*b50261e2SCy Schubert
1636*b50261e2SCy Schubert /** Global Internet */
1637*b50261e2SCy Schubert LOCAL6("2001::", 0);
1638*b50261e2SCy Schubert LOCAL6("2001:4860:4802:32::1b", 0);
1639*b50261e2SCy Schubert end:
1640*b50261e2SCy Schubert ;
1641*b50261e2SCy Schubert }
1642*b50261e2SCy Schubert
1643c43e99fdSEd Maste struct testcase_t util_testcases[] = {
1644c43e99fdSEd Maste { "ipv4_parse", regress_ipv4_parse, 0, NULL, NULL },
1645c43e99fdSEd Maste { "ipv6_parse", regress_ipv6_parse, 0, NULL, NULL },
1646*b50261e2SCy Schubert { "ipv6_parse_scope", regress_ipv6_parse_scope, 0, NULL, NULL },
1647c43e99fdSEd Maste { "sockaddr_port_parse", regress_sockaddr_port_parse, 0, NULL, NULL },
1648c43e99fdSEd Maste { "sockaddr_port_format", regress_sockaddr_port_format, 0, NULL, NULL },
1649c43e99fdSEd Maste { "sockaddr_predicates", test_evutil_sockaddr_predicates, 0,NULL,NULL },
1650c43e99fdSEd Maste { "evutil_snprintf", test_evutil_snprintf, 0, NULL, NULL },
1651c43e99fdSEd Maste { "evutil_strtoll", test_evutil_strtoll, 0, NULL, NULL },
1652c43e99fdSEd Maste { "evutil_casecmp", test_evutil_casecmp, 0, NULL, NULL },
1653c43e99fdSEd Maste { "evutil_rtrim", test_evutil_rtrim, 0, NULL, NULL },
1654c43e99fdSEd Maste { "strlcpy", test_evutil_strlcpy, 0, NULL, NULL },
1655c43e99fdSEd Maste { "log", test_evutil_log, TT_FORK, NULL, NULL },
1656c43e99fdSEd Maste { "upcast", test_evutil_upcast, 0, NULL, NULL },
1657c43e99fdSEd Maste { "integers", test_evutil_integers, 0, NULL, NULL },
1658c43e99fdSEd Maste { "rand", test_evutil_rand, TT_FORK, NULL, NULL },
1659*b50261e2SCy Schubert { "EVUTIL_IS_", test_EVUTIL_IS_, 0, NULL, NULL },
1660c43e99fdSEd Maste { "getaddrinfo", test_evutil_getaddrinfo, TT_FORK, NULL, NULL },
1661c43e99fdSEd Maste { "getaddrinfo_live", test_evutil_getaddrinfo_live, TT_FORK|TT_OFF_BY_DEFAULT, NULL, NULL },
1662*b50261e2SCy Schubert { "getaddrinfo_AI_ADDRCONFIG", test_evutil_getaddrinfo_AI_ADDRCONFIG, TT_FORK|TT_OFF_BY_DEFAULT, NULL, NULL },
1663c43e99fdSEd Maste #ifdef _WIN32
1664c43e99fdSEd Maste { "loadsyslib", test_evutil_loadsyslib, TT_FORK, NULL, NULL },
1665c43e99fdSEd Maste #endif
1666c43e99fdSEd Maste { "mm_malloc", test_event_malloc, 0, NULL, NULL },
1667c43e99fdSEd Maste { "mm_calloc", test_event_calloc, 0, NULL, NULL },
1668c43e99fdSEd Maste { "mm_strdup", test_event_strdup, 0, NULL, NULL },
1669*b50261e2SCy Schubert { "usleep", test_evutil_usleep, TT_RETRIABLE, NULL, NULL },
1670c43e99fdSEd Maste { "monotonic_res", test_evutil_monotonic_res, 0, &basic_setup, (void*)"" },
1671c43e99fdSEd Maste { "monotonic_res_precise", test_evutil_monotonic_res, TT_OFF_BY_DEFAULT, &basic_setup, (void*)"precise" },
1672c43e99fdSEd Maste { "monotonic_res_fallback", test_evutil_monotonic_res, TT_OFF_BY_DEFAULT, &basic_setup, (void*)"fallback" },
1673c43e99fdSEd Maste { "monotonic_prc", test_evutil_monotonic_prc, 0, &basic_setup, (void*)"" },
1674*b50261e2SCy Schubert { "monotonic_prc_precise", test_evutil_monotonic_prc, TT_RETRIABLE, &basic_setup, (void*)"precise" },
1675c43e99fdSEd Maste { "monotonic_prc_fallback", test_evutil_monotonic_prc, 0, &basic_setup, (void*)"fallback" },
1676c43e99fdSEd Maste { "date_rfc1123", test_evutil_date_rfc1123, 0, NULL, NULL },
1677*b50261e2SCy Schubert { "evutil_v4addr_is_local", test_evutil_v4addr_is_local, 0, NULL, NULL },
1678*b50261e2SCy Schubert { "evutil_v6addr_is_local", test_evutil_v6addr_is_local, 0, NULL, NULL },
1679c43e99fdSEd Maste END_OF_TESTCASES,
1680c43e99fdSEd Maste };
1681c43e99fdSEd Maste
1682