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