| 1 | #include <string.h> |
| 2 | #include <errno.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <stdarg.h> |
| 5 | #include <sys/types.h> |
| 6 | #include <sys/socket.h> |
| 7 | #include <netinet/in.h> |
| 8 | #include <arpa/inet.h> |
| 9 | #include <unistd.h> |
| 10 | #include <fcntl.h> |
| 11 | #include "harness.h" |
| 12 | #include "internal.h" |
| 13 | vbuf vb; |
| 14 | FILE *Toutputfile= 0; |
| 15 | struct timeval currenttime; |
| 16 | const struct Terrno Terrnos[]= { |
| 17 | { "EBADF", EBADF }, |
| 18 | { "EAGAIN", EAGAIN }, |
| 19 | { "EINPROGRESS", EINPROGRESS }, |
| 20 | { "EINTR", EINTR }, |
| 21 | { "EINVAL", EINVAL }, |
| 22 | { "EMSGSIZE", EMSGSIZE }, |
| 23 | { "ENOBUFS", ENOBUFS }, |
| 24 | { "ENOENT", ENOENT }, |
| 25 | { "ENOPROTOOPT", ENOPROTOOPT }, |
| 26 | { "ENOSPC", ENOSPC }, |
| 27 | { "EWOULDBLOCK", EWOULDBLOCK }, |
| 28 | { "EHOSTUNREACH", EHOSTUNREACH }, |
| 29 | { "ECONNRESET", ECONNRESET }, |
| 30 | { "ECONNREFUSED", ECONNREFUSED }, |
| 31 | { "EPIPE", EPIPE }, |
| 32 | { "ENOTSOCK", ENOTSOCK }, |
| 33 | { 0, 0 } |
| 34 | }; |
| 35 | static vbuf vbw; |
| 36 | int Hgettimeofday(struct timeval *tv, struct timezone *tz) { |
| 37 | Tensurerecordfile(); |
| 38 | Tmust("gettimeofday","tz",!tz); |
| 39 | *tv= currenttime; |
| 40 | return 0; |
| 41 | } |
| 42 | int Hwritev(int fd, const struct iovec *vector, size_t count) { |
| 43 | size_t i; |
| 44 | vbw.used= 0; |
| 45 | for (i=0; i<count; i++, vector++) { |
| 46 | if (!adns__vbuf_append(&vbw,vector->iov_base,vector->iov_len)) Tnomem(); |
| 47 | } |
| 48 | return Hwrite(fd,vbw.buf,vbw.used); |
| 49 | } |
| 50 | void Qselect( int max , const fd_set *rfds , const fd_set *wfds , const fd_set *efds , struct timeval *to ) { |
| 51 | vb.used= 0; |
| 52 | Tvba("select"); |
| 53 | Tvbf(" max=%d",max); |
| 54 | Tvbf(" rfds="); Tvbfdset(max,rfds); |
| 55 | Tvbf(" wfds="); Tvbfdset(max,wfds); |
| 56 | Tvbf(" efds="); Tvbfdset(max,efds); |
| 57 | if (to) Tvbf(" to=%ld.%06ld",(long)to->tv_sec,(long)to->tv_usec); |
| 58 | else Tvba(" to=null"); |
| 59 | Q_vb(); |
| 60 | } |
| 61 | #ifdef HAVE_POLL |
| 62 | void Qpoll( const struct pollfd *fds , int nfds , int timeout ) { |
| 63 | vb.used= 0; |
| 64 | Tvba("poll"); |
| 65 | Tvbf(" fds="); Tvbpollfds(fds,nfds); |
| 66 | Tvbf(" timeout=%d",timeout); |
| 67 | Q_vb(); |
| 68 | } |
| 69 | #endif |
| 70 | void Qsocket( int type ) { |
| 71 | vb.used= 0; |
| 72 | Tvba("socket"); |
| 73 | Tvbf(type==SOCK_STREAM ? " type=SOCK_STREAM" : " type=SOCK_DGRAM"); |
| 74 | Q_vb(); |
| 75 | } |
| 76 | void Qfcntl( int fd , int cmd , long arg ) { |
| 77 | vb.used= 0; |
| 78 | Tvba("fcntl"); |
| 79 | Tvbf(" fd=%d",fd); |
| 80 | if (cmd == F_SETFL) { |
| 81 | Tvbf(" cmd=F_SETFL %s",arg & O_NONBLOCK ? "O_NONBLOCK|..." : "~O_NONBLOCK&..."); |
| 82 | } else if (cmd == F_GETFL) { |
| 83 | Tvba(" cmd=F_GETFL"); |
| 84 | } else { |
| 85 | Tmust("cmd","F_GETFL/F_SETFL",0); |
| 86 | } |
| 87 | Q_vb(); |
| 88 | } |
| 89 | void Qconnect( int fd , const struct sockaddr *addr , int addrlen ) { |
| 90 | vb.used= 0; |
| 91 | Tvba("connect"); |
| 92 | Tvbf(" fd=%d",fd); |
| 93 | Tvba(" addr="); Tvbaddr(addr,addrlen); |
| 94 | Q_vb(); |
| 95 | } |
| 96 | void Qbind( int fd , const struct sockaddr *addr , int addrlen ) { |
| 97 | vb.used= 0; |
| 98 | Tvba("bind"); |
| 99 | Tvbf(" fd=%d",fd); |
| 100 | Tvba(" addr="); Tvbaddr(addr,addrlen); |
| 101 | Q_vb(); |
| 102 | } |
| 103 | void Qlisten( int fd , int backlog ) { |
| 104 | vb.used= 0; |
| 105 | Tvba("listen"); |
| 106 | Tvbf(" fd=%d",fd); |
| 107 | Tvbf(" backlog=%d",backlog); |
| 108 | Q_vb(); |
| 109 | } |
| 110 | void Qclose( int fd ) { |
| 111 | vb.used= 0; |
| 112 | Tvba("close"); |
| 113 | Tvbf(" fd=%d",fd); |
| 114 | Q_vb(); |
| 115 | } |
| 116 | void Qsendto( int fd , const void *msg , int msglen , const struct sockaddr *addr , int addrlen ) { |
| 117 | vb.used= 0; |
| 118 | Tvba("sendto"); |
| 119 | Tvbf(" fd=%d",fd); |
| 120 | Tvba(" addr="); Tvbaddr(addr,addrlen); |
| 121 | Tvbbytes(msg,msglen); |
| 122 | Q_vb(); |
| 123 | } |
| 124 | void Qrecvfrom( int fd , int buflen , int addrlen ) { |
| 125 | vb.used= 0; |
| 126 | Tvba("recvfrom"); |
| 127 | Tvbf(" fd=%d",fd); |
| 128 | Tvbf(" buflen=%lu",(unsigned long)buflen); |
| 129 | Tvbf(" *addrlen=%d",addrlen); |
| 130 | Q_vb(); |
| 131 | } |
| 132 | void Qread( int fd , size_t buflen ) { |
| 133 | vb.used= 0; |
| 134 | Tvba("read"); |
| 135 | Tvbf(" fd=%d",fd); |
| 136 | Tvbf(" buflen=%lu",(unsigned long)buflen); |
| 137 | Q_vb(); |
| 138 | } |
| 139 | void Qwrite( int fd , const void *buf , size_t len ) { |
| 140 | vb.used= 0; |
| 141 | Tvba("write"); |
| 142 | Tvbf(" fd=%d",fd); |
| 143 | Tvbbytes(buf,len); |
| 144 | Q_vb(); |
| 145 | } |
| 146 | void Tvbaddr(const struct sockaddr *addr, int len) { |
| 147 | const struct sockaddr_in *ai= (const struct sockaddr_in*)addr; |
| 148 | assert(len==sizeof(struct sockaddr_in)); |
| 149 | assert(ai->sin_family==AF_INET); |
| 150 | Tvbf("%s:%u",inet_ntoa(ai->sin_addr),htons(ai->sin_port)); |
| 151 | } |
| 152 | void Tvbbytes(const void *buf, int len) { |
| 153 | const byte *bp; |
| 154 | int i; |
| 155 | if (!len) { Tvba("\n ."); return; } |
| 156 | for (i=0, bp=buf; i<len; i++, bp++) { |
| 157 | if (!(i&31)) Tvba("\n "); |
| 158 | else if (!(i&3)) Tvba(" "); |
| 159 | Tvbf("%02x",*bp); |
| 160 | } |
| 161 | Tvba("."); |
| 162 | } |
| 163 | void Tvbfdset(int max, const fd_set *fds) { |
| 164 | int i; |
| 165 | const char *comma= ""; |
| 166 | Tvba("["); |
| 167 | for (i=0; i<max; i++) { |
| 168 | if (!FD_ISSET(i,fds)) continue; |
| 169 | Tvba(comma); |
| 170 | Tvbf("%d",i); |
| 171 | comma= ","; |
| 172 | } |
| 173 | Tvba("]"); |
| 174 | } |
| 175 | static void Tvbpollevents(int events) { |
| 176 | const char *delim= ""; |
| 177 | events &= (POLLIN|POLLOUT|POLLPRI); |
| 178 | if (!events) { Tvba("0"); return; } |
| 179 | if (events & POLLIN) { Tvba("POLLIN"); delim= "|"; } |
| 180 | if (events & POLLOUT) { Tvba(delim); Tvba("POLLOUT"); delim= "|"; } |
| 181 | if (events & POLLPRI) { Tvba(delim); Tvba("POLLPRI"); } |
| 182 | } |
| 183 | void Tvbpollfds(const struct pollfd *fds, int nfds) { |
| 184 | const char *comma= ""; |
| 185 | Tvba("["); |
| 186 | while (nfds>0) { |
| 187 | Tvba(comma); |
| 188 | Tvbf("{fd=%d, events=",fds->fd); |
| 189 | Tvbpollevents(fds->events); |
| 190 | Tvba(", revents="); |
| 191 | Tvbpollevents(fds->revents); |
| 192 | Tvba("}"); |
| 193 | comma= ", "; |
| 194 | nfds--; fds++; |
| 195 | } |
| 196 | Tvba("]"); |
| 197 | } |
| 198 | void Tvberrno(int e) { |
| 199 | const struct Terrno *te; |
| 200 | for (te= Terrnos; te->n && te->v != e; te++); |
| 201 | assert(te->n); |
| 202 | Tvba(te->n); |
| 203 | } |
| 204 | void Tvba(const char *str) { |
| 205 | if (!adns__vbuf_appendstr(&vb,str)) Tnomem(); |
| 206 | } |
| 207 | void Tvbvf(const char *fmt, va_list al) { |
| 208 | char buf[1000]; |
| 209 | buf[sizeof(buf)-2]= '\t'; |
| 210 | vsnprintf(buf,sizeof(buf),fmt,al); |
| 211 | assert(buf[sizeof(buf)-2] == '\t'); |
| 212 | Tvba(buf); |
| 213 | } |
| 214 | void Tvbf(const char *fmt, ...) { |
| 215 | va_list al; |
| 216 | va_start(al,fmt); |
| 217 | Tvbvf(fmt,al); |
| 218 | va_end(al); |
| 219 | } |
| 220 | void Tmust(const char *call, const char *arg, int cond) { |
| 221 | if (cond) return; |
| 222 | fprintf(stderr,"adns test harness: case not handled: system call %s, arg %s",call,arg); |
| 223 | exit(-1); |
| 224 | } |
| 225 | void Tfailed(const char *why) { |
| 226 | fprintf(stderr,"adns test harness: failure: %s: %s\n",why,strerror(errno)); |
| 227 | exit(-1); |
| 228 | } |
| 229 | void Tnomem(void) { |
| 230 | Tfailed("unable to malloc/realloc"); |
| 231 | } |
| 232 | void Toutputerr(void) { |
| 233 | Tfailed("write error on test harness output"); |
| 234 | } |
| 235 | struct malloced { |
| 236 | struct malloced *next, *back; |
| 237 | size_t sz; |
| 238 | unsigned long count; |
| 239 | struct { double d; long ul; void *p; void (*fp)(void); } data; |
| 240 | }; |
| 241 | static unsigned long malloccount, mallocfailat; |
| 242 | static struct { struct malloced *head, *tail; } mallocedlist; |
| 243 | #define MALLOCHSZ ((char*)&mallocedlist.head->data - (char*)mallocedlist.head) |
| 244 | void *Hmalloc(size_t sz) { |
| 245 | struct malloced *newnode; |
| 246 | const char *mfavar; |
| 247 | char *ep; |
| 248 | assert(sz); |
| 249 | newnode= malloc(MALLOCHSZ + sz); if (!newnode) Tnomem(); |
| 250 | LIST_LINK_TAIL(mallocedlist,newnode); |
| 251 | newnode->sz= sz; |
| 252 | newnode->count= ++malloccount; |
| 253 | if (!mallocfailat) { |
| 254 | mfavar= getenv("ADNS_REGRESS_MALLOCFAILAT"); |
| 255 | if (mfavar) { |
| 256 | mallocfailat= strtoul(mfavar,&ep,10); |
| 257 | if (!mallocfailat || *ep) Tfailed("ADNS_REGRESS_MALLOCFAILAT bad value"); |
| 258 | } else { |
| 259 | mallocfailat= ~0UL; |
| 260 | } |
| 261 | } |
| 262 | assert(newnode->count != mallocfailat); |
| 263 | memset(&newnode->data,0xc7,sz); |
| 264 | return &newnode->data; |
| 265 | } |
| 266 | void Hfree(void *ptr) { |
| 267 | struct malloced *oldnode; |
| 268 | if (!ptr) return; |
| 269 | oldnode= (void*)((char*)ptr - MALLOCHSZ); |
| 270 | LIST_UNLINK(mallocedlist,oldnode); |
| 271 | memset(&oldnode->data,0x38,oldnode->sz); |
| 272 | free(oldnode); |
| 273 | } |
| 274 | void *Hrealloc(void *op, size_t nsz) { |
| 275 | struct malloced *oldnode; |
| 276 | void *np; |
| 277 | size_t osz; |
| 278 | if (op) { oldnode= (void*)((char*)op - MALLOCHSZ); osz= oldnode->sz; } else { osz= 0; } |
| 279 | np= Hmalloc(nsz); |
| 280 | memcpy(np,op, osz>nsz ? nsz : osz); |
| 281 | Hfree(op); |
| 282 | return np; |
| 283 | } |
| 284 | void Hexit(int rv) { |
| 285 | struct malloced *loopnode; |
| 286 | Tshutdown(); |
| 287 | adns__vbuf_free(&vb); |
| 288 | adns__vbuf_free(&vbw); |
| 289 | if (mallocedlist.head) { |
| 290 | fprintf(stderr,"adns test harness: memory leaked:"); |
| 291 | for (loopnode=mallocedlist.head; loopnode; loopnode=loopnode->next) |
| 292 | fprintf(stderr," %lu",loopnode->count); |
| 293 | putc('\n',stderr); |
| 294 | if (ferror(stderr)) exit(-1); |
| 295 | } |
| 296 | exit(rv); |
| 297 | } |
| 298 | pid_t Hgetpid(void) { |
| 299 | return 2264; /* just some number */ |
| 300 | } |