| 1 | m4_dnl hcommon.c |
| 2 | m4_dnl (part of complex test harness, not of the library) |
| 3 | m4_dnl - routines used for both record and playback |
| 4 | |
| 5 | m4_dnl This file is |
| 6 | m4_dnl Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk> |
| 7 | m4_dnl |
| 8 | m4_dnl It is part of adns, which is |
| 9 | m4_dnl Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk> |
| 10 | m4_dnl Copyright (C) 1999-2000 Tony Finch <dot@dotat.at> |
| 11 | m4_dnl |
| 12 | m4_dnl This program is free software; you can redistribute it and/or modify |
| 13 | m4_dnl it under the terms of the GNU General Public License as published by |
| 14 | m4_dnl the Free Software Foundation; either version 2, or (at your option) |
| 15 | m4_dnl any later version. |
| 16 | m4_dnl |
| 17 | m4_dnl This program is distributed in the hope that it will be useful, |
| 18 | m4_dnl but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | m4_dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | m4_dnl GNU General Public License for more details. |
| 21 | m4_dnl |
| 22 | m4_dnl You should have received a copy of the GNU General Public License |
| 23 | m4_dnl along with this program; if not, write to the Free Software Foundation, |
| 24 | m4_dnl Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | |
| 26 | m4_include(hmacros.i4) |
| 27 | |
| 28 | #include <string.h> |
| 29 | #include <errno.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <stdarg.h> |
| 32 | |
| 33 | #include <sys/types.h> |
| 34 | #include <sys/socket.h> |
| 35 | #include <netinet/in.h> |
| 36 | #include <arpa/inet.h> |
| 37 | |
| 38 | #include <unistd.h> |
| 39 | #include <fcntl.h> |
| 40 | |
| 41 | #include "harness.h" |
| 42 | #include "internal.h" |
| 43 | |
| 44 | vbuf vb; |
| 45 | FILE *Toutputfile= 0; |
| 46 | struct timeval currenttime; |
| 47 | |
| 48 | const struct Terrno Terrnos[]= { |
| 49 | { "EBADF", EBADF }, |
| 50 | { "EAGAIN", EAGAIN }, |
| 51 | { "EINPROGRESS", EINPROGRESS }, |
| 52 | { "EINTR", EINTR }, |
| 53 | { "EINVAL", EINVAL }, |
| 54 | { "EMSGSIZE", EMSGSIZE }, |
| 55 | { "ENOBUFS", ENOBUFS }, |
| 56 | { "ENOENT", ENOENT }, |
| 57 | { "ENOPROTOOPT", ENOPROTOOPT }, |
| 58 | { "ENOSPC", ENOSPC }, |
| 59 | { "EWOULDBLOCK", EWOULDBLOCK }, |
| 60 | { "EHOSTUNREACH", EHOSTUNREACH }, |
| 61 | { "ECONNRESET", ECONNRESET }, |
| 62 | { "ECONNREFUSED", ECONNREFUSED }, |
| 63 | { "EPIPE", EPIPE }, |
| 64 | { 0, 0 } |
| 65 | }; |
| 66 | |
| 67 | static vbuf vbw; |
| 68 | |
| 69 | int Hgettimeofday(struct timeval *tv, struct timezone *tz) { |
| 70 | Tmust("gettimeofday","tz",!tz); |
| 71 | *tv= currenttime; |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | int Hwritev(int fd, const struct iovec *vector, size_t count) { |
| 76 | size_t i; |
| 77 | |
| 78 | vbw.used= 0; |
| 79 | for (i=0; i<count; i++, vector++) { |
| 80 | if (!adns__vbuf_append(&vbw,vector->iov_base,vector->iov_len)) Tnomem(); |
| 81 | } |
| 82 | return Hwrite(fd,vbw.buf,vbw.used); |
| 83 | } |
| 84 | |
| 85 | m4_define(`hm_syscall', ` |
| 86 | hm_create_proto_q |
| 87 | void Q$1(hm_args_massage($3,void)) { |
| 88 | |
| 89 | vb.used= 0; |
| 90 | Tvba("$1"); |
| 91 | m4_define(`hm_na',`') |
| 92 | m4_define(`hm_arg_nullptr',`') |
| 93 | m4_define(`hm_arg_int', `Tvbf(" $'`1=%d",$'`1);') |
| 94 | m4_define(`hm_arg_fdset_io', `Tvbf(" $'`1="); Tvbfdset($'`2,$'`1);') |
| 95 | m4_define(`hm_arg_pollfds_io', `Tvbf(" $'`1="); Tvbpollfds($'`1,$'`2);') |
| 96 | m4_define(`hm_arg_timeval_in_rel_null', ` |
| 97 | if ($'`1) Tvbf(" $'`1=%ld.%06ld",(long)$'`1->tv_sec,(long)$'`1->tv_usec); |
| 98 | else Tvba(" $'`1=null");') |
| 99 | m4_define(`hm_arg_must', `') |
| 100 | m4_define(`hm_arg_socktype', ` |
| 101 | Tvbf($'`1==SOCK_STREAM ? " $'`1=SOCK_STREAM" : " $'`1=SOCK_DGRAM");') |
| 102 | m4_define(`hm_arg_ign', `') |
| 103 | m4_define(`hm_arg_fd', `Tvbf(" $'`1=%d",$'`1);') |
| 104 | m4_define(`hm_arg_fcntl_cmd_arg', ` |
| 105 | if ($'`1 == F_SETFL) { |
| 106 | Tvbf(" $'`1=F_SETFL %s",arg & O_NONBLOCK ? "O_NONBLOCK|..." : "~O_NONBLOCK&..."); |
| 107 | } else if ($'`1 == F_GETFL) { |
| 108 | Tvba(" $'`1=F_GETFL"); |
| 109 | } else { |
| 110 | Tmust("$'`1","F_GETFL/F_SETFL",0); |
| 111 | }') |
| 112 | m4_define(`hm_arg_addr_in', `Tvba(" $'`1="); Tvbaddr($'`1,$'`2);') |
| 113 | m4_define(`hm_arg_bytes_in', `') |
| 114 | m4_define(`hm_arg_bytes_out', `Tvbf(" $'`4=%lu",(unsigned long)$'`4);') |
| 115 | m4_define(`hm_arg_addr_out', `Tvbf(" *$'`2=%d",$'`2);') |
| 116 | $3 |
| 117 | |
| 118 | hm_create_nothing |
| 119 | m4_define(`hm_arg_bytes_in', `Tvbbytes($'`2,$'`4);') |
| 120 | $3 |
| 121 | |
| 122 | Q_vb(); |
| 123 | } |
| 124 | ') |
| 125 | |
| 126 | m4_define(`hm_specsyscall', `') |
| 127 | |
| 128 | m4_include(`hsyscalls.i4') |
| 129 | |
| 130 | void Tvbaddr(const struct sockaddr *addr, int len) { |
| 131 | const struct sockaddr_in *ai= (const struct sockaddr_in*)addr; |
| 132 | |
| 133 | assert(len==sizeof(struct sockaddr_in)); |
| 134 | assert(ai->sin_family==AF_INET); |
| 135 | Tvbf("%s:%u",inet_ntoa(ai->sin_addr),htons(ai->sin_port)); |
| 136 | } |
| 137 | |
| 138 | void Tvbbytes(const void *buf, int len) { |
| 139 | const byte *bp; |
| 140 | int i; |
| 141 | |
| 142 | if (!len) { Tvba("\n ."); return; } |
| 143 | for (i=0, bp=buf; i<len; i++, bp++) { |
| 144 | if (!(i&31)) Tvba("\n "); |
| 145 | else if (!(i&3)) Tvba(" "); |
| 146 | Tvbf("%02x",*bp); |
| 147 | } |
| 148 | Tvba("."); |
| 149 | } |
| 150 | |
| 151 | void Tvbfdset(int max, const fd_set *fds) { |
| 152 | int i; |
| 153 | const char *comma= ""; |
| 154 | |
| 155 | Tvba("["); |
| 156 | for (i=0; i<max; i++) { |
| 157 | if (!FD_ISSET(i,fds)) continue; |
| 158 | Tvba(comma); |
| 159 | Tvbf("%d",i); |
| 160 | comma= ","; |
| 161 | } |
| 162 | Tvba("]"); |
| 163 | } |
| 164 | |
| 165 | static void Tvbpollevents(int events) { |
| 166 | const char *delim= ""; |
| 167 | |
| 168 | events &= (POLLIN|POLLOUT|POLLPRI); |
| 169 | if (!events) { Tvba("0"); return; } |
| 170 | if (events & POLLIN) { Tvba("POLLIN"); delim= "|"; } |
| 171 | if (events & POLLOUT) { Tvba(delim); Tvba("POLLOUT"); delim= "|"; } |
| 172 | if (events & POLLPRI) { Tvba(delim); Tvba("POLLPRI"); } |
| 173 | } |
| 174 | |
| 175 | void Tvbpollfds(const struct pollfd *fds, int nfds) { |
| 176 | const char *comma= ""; |
| 177 | |
| 178 | Tvba("["); |
| 179 | while (nfds>0) { |
| 180 | Tvba(comma); |
| 181 | Tvbf("{fd=%d, events=",fds->fd); |
| 182 | Tvbpollevents(fds->events); |
| 183 | Tvba(", revents="); |
| 184 | Tvbpollevents(fds->revents); |
| 185 | Tvba("}"); |
| 186 | comma= ", "; |
| 187 | nfds--; fds++; |
| 188 | } |
| 189 | Tvba("]"); |
| 190 | } |
| 191 | |
| 192 | void Tvberrno(int e) { |
| 193 | const struct Terrno *te; |
| 194 | |
| 195 | for (te= Terrnos; te->n && te->v != e; te++); |
| 196 | assert(te->n); |
| 197 | Tvba(te->n); |
| 198 | } |
| 199 | |
| 200 | void Tvba(const char *str) { |
| 201 | if (!adns__vbuf_appendstr(&vb,str)) Tnomem(); |
| 202 | } |
| 203 | |
| 204 | void Tvbvf(const char *fmt, va_list al) { |
| 205 | char buf[1000]; |
| 206 | buf[sizeof(buf)-2]= '\t'; |
| 207 | vsnprintf(buf,sizeof(buf),fmt,al); |
| 208 | assert(buf[sizeof(buf)-2] == '\t'); |
| 209 | |
| 210 | Tvba(buf); |
| 211 | } |
| 212 | |
| 213 | void Tvbf(const char *fmt, ...) { |
| 214 | va_list al; |
| 215 | va_start(al,fmt); |
| 216 | Tvbvf(fmt,al); |
| 217 | va_end(al); |
| 218 | } |
| 219 | |
| 220 | |
| 221 | void Tmust(const char *call, const char *arg, int cond) { |
| 222 | if (cond) return; |
| 223 | fprintf(stderr,"adns test harness: case not handled: system call %s, arg %s",call,arg); |
| 224 | exit(-1); |
| 225 | } |
| 226 | |
| 227 | void Tfailed(const char *why) { |
| 228 | fprintf(stderr,"adns test harness: failure: %s: %s\n",why,strerror(errno)); |
| 229 | exit(-1); |
| 230 | } |
| 231 | |
| 232 | void Tnomem(void) { |
| 233 | Tfailed("unable to malloc/realloc"); |
| 234 | } |
| 235 | |
| 236 | void Toutputerr(void) { |
| 237 | Tfailed("write error on test harness output"); |
| 238 | } |
| 239 | |
| 240 | struct malloced { |
| 241 | struct malloced *next, *back; |
| 242 | size_t sz; |
| 243 | unsigned long count; |
| 244 | struct { double d; long ul; void *p; void (*fp)(void); } data; |
| 245 | }; |
| 246 | |
| 247 | static unsigned long malloccount, mallocfailat; |
| 248 | static struct { struct malloced *head, *tail; } mallocedlist; |
| 249 | |
| 250 | #define MALLOCHSZ ((char*)&mallocedlist.head->data - (char*)mallocedlist.head) |
| 251 | |
| 252 | void *Hmalloc(size_t sz) { |
| 253 | struct malloced *newnode; |
| 254 | const char *mfavar; |
| 255 | char *ep; |
| 256 | |
| 257 | assert(sz); |
| 258 | |
| 259 | newnode= malloc(MALLOCHSZ + sz); if (!newnode) Tnomem(); |
| 260 | |
| 261 | LIST_LINK_TAIL(mallocedlist,newnode); |
| 262 | newnode->sz= sz; |
| 263 | newnode->count= ++malloccount; |
| 264 | if (!mallocfailat) { |
| 265 | mfavar= getenv("ADNS_REGRESS_MALLOCFAILAT"); |
| 266 | if (mfavar) { |
| 267 | mallocfailat= strtoul(mfavar,&ep,10); |
| 268 | if (!mallocfailat || *ep) Tfailed("ADNS_REGRESS_MALLOCFAILAT bad value"); |
| 269 | } else { |
| 270 | mallocfailat= ~0UL; |
| 271 | } |
| 272 | } |
| 273 | assert(newnode->count != mallocfailat); |
| 274 | memset(&newnode->data,0xc7,sz); |
| 275 | return &newnode->data; |
| 276 | } |
| 277 | |
| 278 | void Hfree(void *ptr) { |
| 279 | struct malloced *oldnode; |
| 280 | |
| 281 | if (!ptr) return; |
| 282 | |
| 283 | oldnode= (void*)((char*)ptr - MALLOCHSZ); |
| 284 | LIST_UNLINK(mallocedlist,oldnode); |
| 285 | memset(&oldnode->data,0x38,oldnode->sz); |
| 286 | free(oldnode); |
| 287 | } |
| 288 | |
| 289 | void *Hrealloc(void *op, size_t nsz) { |
| 290 | struct malloced *oldnode; |
| 291 | void *np; |
| 292 | size_t osz; |
| 293 | |
| 294 | if (op) { oldnode= (void*)((char*)op - MALLOCHSZ); osz= oldnode->sz; } else { osz= 0; } |
| 295 | np= Hmalloc(nsz); |
| 296 | memcpy(np,op, osz>nsz ? nsz : osz); |
| 297 | Hfree(op); |
| 298 | return np; |
| 299 | } |
| 300 | |
| 301 | void Hexit(int rv) { |
| 302 | struct malloced *loopnode; |
| 303 | |
| 304 | Tshutdown(); |
| 305 | adns__vbuf_free(&vb); |
| 306 | adns__vbuf_free(&vbw); |
| 307 | if (mallocedlist.head) { |
| 308 | fprintf(stderr,"adns test harness: memory leaked:"); |
| 309 | for (loopnode=mallocedlist.head; loopnode; loopnode=loopnode->next) |
| 310 | fprintf(stderr," %lu(%lu)",loopnode->count,(unsigned long)loopnode->sz); |
| 311 | putc('\n',stderr); |
| 312 | if (ferror(stderr)) exit(-1); |
| 313 | } |
| 314 | exit(rv); |
| 315 | } |