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