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