Commit | Line | Data |
---|---|---|
bb7ba677 | 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 | ||
ae8cc977 | 5 | m4_dnl This file is part of adns, which is |
26e1c3d6 | 6 | m4_dnl Copyright (C) 1997-2000,2003,2006,2014 Ian Jackson |
17cd4f48 | 7 | m4_dnl Copyright (C) 2014 Mark Wooding |
ae8cc977 | 8 | m4_dnl Copyright (C) 1999-2000,2003,2006 Tony Finch |
9 | m4_dnl Copyright (C) 1991 Massachusetts Institute of Technology | |
10 | m4_dnl (See the file INSTALL for full details.) | |
bb7ba677 | 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 | |
7f8bbe29 | 14 | m4_dnl the Free Software Foundation; either version 3, or (at your option) |
bb7ba677 | 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 | |
8c09a4c6 | 23 | m4_dnl along with this program; if not, write to the Free Software Foundation. |
bb7ba677 | 24 | |
25 | m4_include(hmacros.i4) | |
a4f094dd | 26 | |
7d251914 | 27 | #include <string.h> |
28 | #include <errno.h> | |
914a5ff5 | 29 | #include <stdlib.h> |
4e25ff4e | 30 | #include <stdarg.h> |
7d251914 | 31 | |
bbede9a3 | 32 | #include <sys/types.h> |
7d251914 | 33 | #include <sys/socket.h> |
34 | #include <netinet/in.h> | |
35 | #include <arpa/inet.h> | |
36 | ||
bbede9a3 | 37 | #include <unistd.h> |
38 | #include <fcntl.h> | |
39 | ||
a4f094dd | 40 | #include "harness.h" |
41 | #include "internal.h" | |
42 | ||
7d251914 | 43 | vbuf vb; |
44 | FILE *Toutputfile= 0; | |
98b6d5b4 | 45 | struct timeval currenttime; |
7d251914 | 46 | |
47 | const struct Terrno Terrnos[]= { | |
a80db454 | 48 | { "EBADF", EBADF }, |
7d251914 | 49 | { "EAGAIN", EAGAIN }, |
50 | { "EINPROGRESS", EINPROGRESS }, | |
51 | { "EINTR", EINTR }, | |
52 | { "EINVAL", EINVAL }, | |
53 | { "EMSGSIZE", EMSGSIZE }, | |
54 | { "ENOBUFS", ENOBUFS }, | |
55 | { "ENOENT", ENOENT }, | |
56 | { "ENOPROTOOPT", ENOPROTOOPT }, | |
57 | { "ENOSPC", ENOSPC }, | |
58 | { "EWOULDBLOCK", EWOULDBLOCK }, | |
a00dbcb2 | 59 | { "EHOSTUNREACH", EHOSTUNREACH }, |
fb901bf5 | 60 | { "ECONNRESET", ECONNRESET }, |
a00dbcb2 | 61 | { "ECONNREFUSED", ECONNREFUSED }, |
62 | { "EPIPE", EPIPE }, | |
87dd6345 | 63 | { "ENOTSOCK", ENOTSOCK }, |
7d251914 | 64 | { 0, 0 } |
65 | }; | |
a4f094dd | 66 | |
70a778e5 | 67 | static vbuf vbw; |
7d251914 | 68 | |
98b6d5b4 | 69 | int Hgettimeofday(struct timeval *tv, struct timezone *tz) { |
87dd6345 | 70 | Tensurerecordfile(); |
98b6d5b4 | 71 | Tmust("gettimeofday","tz",!tz); |
72 | *tv= currenttime; | |
73 | return 0; | |
74 | } | |
75 | ||
70a778e5 | 76 | int Hwritev(int fd, const struct iovec *vector, size_t count) { |
77 | size_t i; | |
78 | ||
79 | vbw.used= 0; | |
80 | for (i=0; i<count; i++, vector++) { | |
81 | if (!adns__vbuf_append(&vbw,vector->iov_base,vector->iov_len)) Tnomem(); | |
82 | } | |
83 | return Hwrite(fd,vbw.buf,vbw.used); | |
84 | } | |
7d251914 | 85 | |
bb7ba677 | 86 | m4_define(`hm_syscall', ` |
87 | hm_create_proto_q | |
88 | void Q$1(hm_args_massage($3,void)) { | |
89 | ||
90 | vb.used= 0; | |
91 | Tvba("$1"); | |
92 | m4_define(`hm_na',`') | |
93 | m4_define(`hm_arg_nullptr',`') | |
94 | m4_define(`hm_arg_int', `Tvbf(" $'`1=%d",$'`1);') | |
95 | m4_define(`hm_arg_fdset_io', `Tvbf(" $'`1="); Tvbfdset($'`2,$'`1);') | |
8b1171e6 | 96 | m4_define(`hm_arg_pollfds_io', `Tvbf(" $'`1="); Tvbpollfds($'`1,$'`2);') |
bb7ba677 | 97 | m4_define(`hm_arg_timeval_in_rel_null', ` |
98 | if ($'`1) Tvbf(" $'`1=%ld.%06ld",(long)$'`1->tv_sec,(long)$'`1->tv_usec); | |
99 | else Tvba(" $'`1=null");') | |
100 | m4_define(`hm_arg_must', `') | |
101 | m4_define(`hm_arg_socktype', ` | |
102 | Tvbf($'`1==SOCK_STREAM ? " $'`1=SOCK_STREAM" : " $'`1=SOCK_DGRAM");') | |
5c6cd9e0 | 103 | m4_define(`hm_arg_addrfam', ` |
9ec92889 IJ |
104 | Tvbf($'`1==AF_INET ? " $'`1=AF_INET" : |
105 | $'`1==AF_INET6 ? " $'`1=AF_INET6" : | |
5c6cd9e0 | 106 | " $'`1=AF_???");') |
bb7ba677 | 107 | m4_define(`hm_arg_ign', `') |
108 | m4_define(`hm_arg_fd', `Tvbf(" $'`1=%d",$'`1);') | |
109 | m4_define(`hm_arg_fcntl_cmd_arg', ` | |
110 | if ($'`1 == F_SETFL) { | |
73c4c523 | 111 | Tvbf(" $'`1=F_SETFL %s",arg & O_NONBLOCK ? "O_NONBLOCK|..." : "~O_NONBLOCK&..."); |
112 | } else if ($'`1 == F_GETFL) { | |
bb7ba677 | 113 | Tvba(" $'`1=F_GETFL"); |
73c4c523 | 114 | } else { |
115 | Tmust("$'`1","F_GETFL/F_SETFL",0); | |
bb7ba677 | 116 | }') |
70a778e5 | 117 | m4_define(`hm_arg_addr_in', `Tvba(" $'`1="); Tvbaddr($'`1,$'`2);') |
118 | m4_define(`hm_arg_bytes_in', `') | |
bb7ba677 | 119 | m4_define(`hm_arg_bytes_out', `Tvbf(" $'`4=%lu",(unsigned long)$'`4);') |
4bc76b9c | 120 | m4_define(`hm_arg_addr_out', `') |
bb7ba677 | 121 | $3 |
7d251914 | 122 | |
70a778e5 | 123 | hm_create_nothing |
124 | m4_define(`hm_arg_bytes_in', `Tvbbytes($'`2,$'`4);') | |
125 | $3 | |
126 | ||
7d251914 | 127 | Q_vb(); |
128 | } | |
bb7ba677 | 129 | ') |
7d251914 | 130 | |
0ebff22d | 131 | m4_define(`hm_specsyscall', `') |
7d251914 | 132 | |
0ebff22d | 133 | m4_include(`hsyscalls.i4') |
7d251914 | 134 | |
135 | void Tvbaddr(const struct sockaddr *addr, int len) { | |
84ada014 MW |
136 | char buf[ADNS_ADDR2TEXT_BUFLEN]; |
137 | int err, port; | |
138 | int sz= sizeof(buf); | |
139 | ||
140 | err= adns_addr2text(addr, 0, buf,&sz, &port); | |
141 | assert(!err); | |
142 | ||
143 | Tvbf(strchr(buf, ':') ? "[%s]:%d" : "%s:%d", buf,port); | |
7d251914 | 144 | } |
145 | ||
146 | void Tvbbytes(const void *buf, int len) { | |
147 | const byte *bp; | |
148 | int i; | |
149 | ||
68000374 | 150 | if (!len) { Tvba("\n ."); return; } |
7d251914 | 151 | for (i=0, bp=buf; i<len; i++, bp++) { |
152 | if (!(i&31)) Tvba("\n "); | |
153 | else if (!(i&3)) Tvba(" "); | |
154 | Tvbf("%02x",*bp); | |
155 | } | |
70a778e5 | 156 | Tvba("."); |
7d251914 | 157 | } |
158 | ||
159 | void Tvbfdset(int max, const fd_set *fds) { | |
160 | int i; | |
161 | const char *comma= ""; | |
162 | ||
bb7ba677 | 163 | Tvba("["); |
7d251914 | 164 | for (i=0; i<max; i++) { |
165 | if (!FD_ISSET(i,fds)) continue; | |
166 | Tvba(comma); | |
167 | Tvbf("%d",i); | |
168 | comma= ","; | |
169 | } | |
170 | Tvba("]"); | |
171 | } | |
172 | ||
8b1171e6 | 173 | static void Tvbpollevents(int events) { |
174 | const char *delim= ""; | |
175 | ||
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"); } | |
181 | } | |
182 | ||
183 | void Tvbpollfds(const struct pollfd *fds, int nfds) { | |
184 | const char *comma= ""; | |
185 | ||
186 | Tvba("["); | |
187 | while (nfds>0) { | |
188 | Tvba(comma); | |
189 | Tvbf("{fd=%d, events=",fds->fd); | |
190 | Tvbpollevents(fds->events); | |
191 | Tvba(", revents="); | |
192 | Tvbpollevents(fds->revents); | |
193 | Tvba("}"); | |
194 | comma= ", "; | |
195 | nfds--; fds++; | |
196 | } | |
197 | Tvba("]"); | |
198 | } | |
199 | ||
7d251914 | 200 | void Tvberrno(int e) { |
201 | const struct Terrno *te; | |
a4f094dd | 202 | |
7d251914 | 203 | for (te= Terrnos; te->n && te->v != e; te++); |
43bb811f | 204 | assert(te->n); |
205 | Tvba(te->n); | |
a4f094dd | 206 | } |
207 | ||
7d251914 | 208 | void Tvba(const char *str) { |
209 | if (!adns__vbuf_appendstr(&vb,str)) Tnomem(); | |
210 | } | |
a4f094dd | 211 | |
7d251914 | 212 | void Tvbvf(const char *fmt, va_list al) { |
213 | char buf[1000]; | |
214 | buf[sizeof(buf)-2]= '\t'; | |
215 | vsnprintf(buf,sizeof(buf),fmt,al); | |
216 | assert(buf[sizeof(buf)-2] == '\t'); | |
a4f094dd | 217 | |
7d251914 | 218 | Tvba(buf); |
219 | } | |
220 | ||
221 | void Tvbf(const char *fmt, ...) { | |
222 | va_list al; | |
223 | va_start(al,fmt); | |
224 | Tvbvf(fmt,al); | |
225 | va_end(al); | |
226 | } | |
227 | ||
228 | ||
bb7ba677 | 229 | void Tmust(const char *call, const char *arg, int cond) { |
230 | if (cond) return; | |
231 | fprintf(stderr,"adns test harness: case not handled: system call %s, arg %s",call,arg); | |
232 | exit(-1); | |
233 | } | |
234 | ||
7d251914 | 235 | void Tfailed(const char *why) { |
236 | fprintf(stderr,"adns test harness: failure: %s: %s\n",why,strerror(errno)); | |
237 | exit(-1); | |
238 | } | |
239 | ||
240 | void Tnomem(void) { | |
241 | Tfailed("unable to malloc/realloc"); | |
242 | } | |
243 | ||
244 | void Toutputerr(void) { | |
245 | Tfailed("write error on test harness output"); | |
246 | } | |
914a5ff5 | 247 | |
248 | struct malloced { | |
249 | struct malloced *next, *back; | |
250 | size_t sz; | |
251 | unsigned long count; | |
252 | struct { double d; long ul; void *p; void (*fp)(void); } data; | |
253 | }; | |
254 | ||
255 | static unsigned long malloccount, mallocfailat; | |
256 | static struct { struct malloced *head, *tail; } mallocedlist; | |
257 | ||
258 | #define MALLOCHSZ ((char*)&mallocedlist.head->data - (char*)mallocedlist.head) | |
259 | ||
260 | void *Hmalloc(size_t sz) { | |
261 | struct malloced *newnode; | |
262 | const char *mfavar; | |
263 | char *ep; | |
264 | ||
265 | assert(sz); | |
266 | ||
267 | newnode= malloc(MALLOCHSZ + sz); if (!newnode) Tnomem(); | |
268 | ||
269 | LIST_LINK_TAIL(mallocedlist,newnode); | |
270 | newnode->sz= sz; | |
271 | newnode->count= ++malloccount; | |
272 | if (!mallocfailat) { | |
273 | mfavar= getenv("ADNS_REGRESS_MALLOCFAILAT"); | |
274 | if (mfavar) { | |
275 | mallocfailat= strtoul(mfavar,&ep,10); | |
276 | if (!mallocfailat || *ep) Tfailed("ADNS_REGRESS_MALLOCFAILAT bad value"); | |
277 | } else { | |
278 | mallocfailat= ~0UL; | |
279 | } | |
280 | } | |
281 | assert(newnode->count != mallocfailat); | |
282 | memset(&newnode->data,0xc7,sz); | |
283 | return &newnode->data; | |
284 | } | |
285 | ||
286 | void Hfree(void *ptr) { | |
287 | struct malloced *oldnode; | |
288 | ||
289 | if (!ptr) return; | |
290 | ||
291 | oldnode= (void*)((char*)ptr - MALLOCHSZ); | |
292 | LIST_UNLINK(mallocedlist,oldnode); | |
293 | memset(&oldnode->data,0x38,oldnode->sz); | |
294 | free(oldnode); | |
295 | } | |
296 | ||
297 | void *Hrealloc(void *op, size_t nsz) { | |
298 | struct malloced *oldnode; | |
299 | void *np; | |
300 | size_t osz; | |
301 | ||
302 | if (op) { oldnode= (void*)((char*)op - MALLOCHSZ); osz= oldnode->sz; } else { osz= 0; } | |
303 | np= Hmalloc(nsz); | |
304 | memcpy(np,op, osz>nsz ? nsz : osz); | |
305 | Hfree(op); | |
306 | return np; | |
307 | } | |
308 | ||
309 | void Hexit(int rv) { | |
310 | struct malloced *loopnode; | |
311 | ||
312 | Tshutdown(); | |
313 | adns__vbuf_free(&vb); | |
314 | adns__vbuf_free(&vbw); | |
315 | if (mallocedlist.head) { | |
316 | fprintf(stderr,"adns test harness: memory leaked:"); | |
317 | for (loopnode=mallocedlist.head; loopnode; loopnode=loopnode->next) | |
cb0a827b | 318 | fprintf(stderr," %lu",loopnode->count); |
914a5ff5 | 319 | putc('\n',stderr); |
320 | if (ferror(stderr)) exit(-1); | |
321 | } | |
322 | exit(rv); | |
323 | } | |
0e45654b | 324 | |
325 | pid_t Hgetpid(void) { | |
326 | return 2264; /* just some number */ | |
327 | } | |
328 |