6 #include <sys/socket.h>
7 #include <netinet/in.h>
15 struct timeval currenttime;
16 const struct Terrno Terrnos[]= {
19 { "EINPROGRESS", EINPROGRESS },
22 { "EMSGSIZE", EMSGSIZE },
23 { "ENOBUFS", ENOBUFS },
25 { "ENOPROTOOPT", ENOPROTOOPT },
27 { "EWOULDBLOCK", EWOULDBLOCK },
28 { "EHOSTUNREACH", EHOSTUNREACH },
29 { "ECONNRESET", ECONNRESET },
30 { "ECONNREFUSED", ECONNREFUSED },
32 { "ENOTSOCK", ENOTSOCK },
36 int Hgettimeofday(struct timeval *tv, struct timezone *tz) {
38 Tmust("gettimeofday","tz",!tz);
42 int Hwritev(int fd, const struct iovec *vector, size_t count) {
45 for (i=0; i<count; i++, vector++) {
46 if (!adns__vbuf_append(&vbw,vector->iov_base,vector->iov_len)) Tnomem();
48 return Hwrite(fd,vbw.buf,vbw.used);
50 void Qselect( int max , const fd_set *rfds , const fd_set *wfds , const fd_set *efds , struct timeval *to ) {
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");
62 void Qpoll( const struct pollfd *fds , int nfds , int timeout ) {
65 Tvbf(" fds="); Tvbpollfds(fds,nfds);
66 Tvbf(" timeout=%d",timeout);
70 void Qsocket( int type ) {
73 Tvbf(type==SOCK_STREAM ? " type=SOCK_STREAM" : " type=SOCK_DGRAM");
76 void Qfcntl( int fd , int cmd , long arg ) {
81 Tvbf(" cmd=F_SETFL %s",arg & O_NONBLOCK ? "O_NONBLOCK|..." : "~O_NONBLOCK&...");
82 } else if (cmd == F_GETFL) {
85 Tmust("cmd","F_GETFL/F_SETFL",0);
89 void Qconnect( int fd , const struct sockaddr *addr , int addrlen ) {
93 Tvba(" addr="); Tvbaddr(addr,addrlen);
96 void Qbind( int fd , const struct sockaddr *addr , int addrlen ) {
100 Tvba(" addr="); Tvbaddr(addr,addrlen);
103 void Qlisten( int fd , int backlog ) {
107 Tvbf(" backlog=%d",backlog);
110 void Qclose( int fd ) {
116 void Qsendto( int fd , const void *msg , int msglen , const struct sockaddr *addr , int addrlen ) {
120 Tvba(" addr="); Tvbaddr(addr,addrlen);
121 Tvbbytes(msg,msglen);
124 void Qrecvfrom( int fd , int buflen , int addrlen ) {
128 Tvbf(" buflen=%lu",(unsigned long)buflen);
131 void Qread( int fd , size_t buflen ) {
135 Tvbf(" buflen=%lu",(unsigned long)buflen);
138 void Qwrite( int fd , const void *buf , size_t len ) {
145 void Tvbaddr(const struct sockaddr *addr, int len) {
146 const struct sockaddr_in *ai= (const struct sockaddr_in*)addr;
147 assert(len==sizeof(struct sockaddr_in));
148 assert(ai->sin_family==AF_INET);
149 Tvbf("%s:%u",inet_ntoa(ai->sin_addr),htons(ai->sin_port));
151 void Tvbbytes(const void *buf, int len) {
154 if (!len) { Tvba("\n ."); return; }
155 for (i=0, bp=buf; i<len; i++, bp++) {
156 if (!(i&31)) Tvba("\n ");
157 else if (!(i&3)) Tvba(" ");
162 void Tvbfdset(int max, const fd_set *fds) {
164 const char *comma= "";
166 for (i=0; i<max; i++) {
167 if (!FD_ISSET(i,fds)) continue;
174 static void Tvbpollevents(int events) {
175 const char *delim= "";
176 events &= (POLLIN|POLLOUT|POLLPRI);
177 if (!events) { Tvba("0"); return; }
178 if (events & POLLIN) { Tvba("POLLIN"); delim= "|"; }
179 if (events & POLLOUT) { Tvba(delim); Tvba("POLLOUT"); delim= "|"; }
180 if (events & POLLPRI) { Tvba(delim); Tvba("POLLPRI"); }
182 void Tvbpollfds(const struct pollfd *fds, int nfds) {
183 const char *comma= "";
187 Tvbf("{fd=%d, events=",fds->fd);
188 Tvbpollevents(fds->events);
190 Tvbpollevents(fds->revents);
197 void Tvberrno(int e) {
198 const struct Terrno *te;
199 for (te= Terrnos; te->n && te->v != e; te++);
203 void Tvba(const char *str) {
204 if (!adns__vbuf_appendstr(&vb,str)) Tnomem();
206 void Tvbvf(const char *fmt, va_list al) {
208 buf[sizeof(buf)-2]= '\t';
209 vsnprintf(buf,sizeof(buf),fmt,al);
210 assert(buf[sizeof(buf)-2] == '\t');
213 void Tvbf(const char *fmt, ...) {
219 void Tmust(const char *call, const char *arg, int cond) {
221 fprintf(stderr,"adns test harness: case not handled: system call %s, arg %s",call,arg);
224 void Tfailed(const char *why) {
225 fprintf(stderr,"adns test harness: failure: %s: %s\n",why,strerror(errno));
229 Tfailed("unable to malloc/realloc");
231 void Toutputerr(void) {
232 Tfailed("write error on test harness output");
235 struct malloced *next, *back;
238 struct { double d; long ul; void *p; void (*fp)(void); } data;
240 static unsigned long malloccount, mallocfailat;
241 static struct { struct malloced *head, *tail; } mallocedlist;
242 #define MALLOCHSZ ((char*)&mallocedlist.head->data - (char*)mallocedlist.head)
243 void *Hmalloc(size_t sz) {
244 struct malloced *newnode;
248 newnode= malloc(MALLOCHSZ + sz); if (!newnode) Tnomem();
249 LIST_LINK_TAIL(mallocedlist,newnode);
251 newnode->count= ++malloccount;
253 mfavar= getenv("ADNS_REGRESS_MALLOCFAILAT");
255 mallocfailat= strtoul(mfavar,&ep,10);
256 if (!mallocfailat || *ep) Tfailed("ADNS_REGRESS_MALLOCFAILAT bad value");
261 assert(newnode->count != mallocfailat);
262 memset(&newnode->data,0xc7,sz);
263 return &newnode->data;
265 void Hfree(void *ptr) {
266 struct malloced *oldnode;
268 oldnode= (void*)((char*)ptr - MALLOCHSZ);
269 LIST_UNLINK(mallocedlist,oldnode);
270 memset(&oldnode->data,0x38,oldnode->sz);
273 void *Hrealloc(void *op, size_t nsz) {
274 struct malloced *oldnode;
277 if (op) { oldnode= (void*)((char*)op - MALLOCHSZ); osz= oldnode->sz; } else { osz= 0; }
279 memcpy(np,op, osz>nsz ? nsz : osz);
284 struct malloced *loopnode;
286 adns__vbuf_free(&vb);
287 adns__vbuf_free(&vbw);
288 if (mallocedlist.head) {
289 fprintf(stderr,"adns test harness: memory leaked:");
290 for (loopnode=mallocedlist.head; loopnode; loopnode=loopnode->next)
291 fprintf(stderr," %lu",loopnode->count);
293 if (ferror(stderr)) exit(-1);
297 pid_t Hgetpid(void) {
298 return 2264; /* just some number */