Update copyright dates
[adns] / regress / hcommon.c.m4
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
5 m4_dnl This file is part of adns, which is
6 m4_dnl Copyright (C) 1997-2000,2003,2006,2014-2016 Ian Jackson
7 m4_dnl Copyright (C) 2014 Mark Wooding
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.)
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
14 m4_dnl the Free Software Foundation; either version 3, or (at your option)
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
23 m4_dnl along with this program; if not, write to the Free Software Foundation.
24
25 m4_include(hmacros.i4)
26
27 #include <string.h>
28 #include <errno.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36
37 #include <unistd.h>
38 #include <fcntl.h>
39
40 #include "harness.h"
41 #include "internal.h"
42
43 vbuf vb;
44 FILE *Toutputfile= 0;
45 struct timeval currenttime;
46
47 const struct Terrno Terrnos[]= {
48 { "EBADF", EBADF },
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 },
59 { "EHOSTUNREACH", EHOSTUNREACH },
60 { "ECONNRESET", ECONNRESET },
61 { "ECONNREFUSED", ECONNREFUSED },
62 { "EPIPE", EPIPE },
63 { "ENOTSOCK", ENOTSOCK },
64 { 0, 0 }
65 };
66
67 static vbuf vbw;
68
69 int Hgettimeofday(struct timeval *tv, struct timezone *tz) {
70 Tensurerecordfile();
71 Tmust("gettimeofday","tz",!tz);
72 *tv= currenttime;
73 return 0;
74 }
75
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 }
85
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);')
96 m4_define(`hm_arg_pollfds_io', `Tvbf(" $'`1="); Tvbpollfds($'`1,$'`2);')
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");')
103 m4_define(`hm_arg_addrfam', `
104 Tvbf($'`1==AF_INET ? " $'`1=AF_INET" :
105 $'`1==AF_INET6 ? " $'`1=AF_INET6" :
106 " $'`1=AF_???");')
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) {
111 Tvbf(" $'`1=F_SETFL %s",arg & O_NONBLOCK ? "O_NONBLOCK|..." : "~O_NONBLOCK&...");
112 } else if ($'`1 == F_GETFL) {
113 Tvba(" $'`1=F_GETFL");
114 } else {
115 Tmust("$'`1","F_GETFL/F_SETFL",0);
116 }')
117 m4_define(`hm_arg_addr_in', `Tvba(" $'`1="); Tvbaddr($'`1,$'`2);')
118 m4_define(`hm_arg_bytes_in', `')
119 m4_define(`hm_arg_bytes_out', `Tvbf(" $'`4=%lu",(unsigned long)$'`4);')
120 m4_define(`hm_arg_addr_out', `')
121 $3
122
123 hm_create_nothing
124 m4_define(`hm_arg_bytes_in', `Tvbbytes($'`2,$'`4);')
125 $3
126
127 Q_vb();
128 }
129 ')
130
131 m4_define(`hm_specsyscall', `')
132
133 m4_include(`hsyscalls.i4')
134
135 void Tvbaddr(const struct sockaddr *addr, int len) {
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);
144 }
145
146 void Tvbbytes(const void *buf, int len) {
147 const byte *bp;
148 int i;
149
150 if (!len) { Tvba("\n ."); return; }
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 }
156 Tvba(".");
157 }
158
159 void Tvbfdset(int max, const fd_set *fds) {
160 int i;
161 const char *comma= "";
162
163 if (!fds) {
164 Tvba("null");
165 return;
166 }
167
168 Tvba("[");
169 for (i=0; i<max; i++) {
170 if (!FD_ISSET(i,fds)) continue;
171 Tvba(comma);
172 Tvbf("%d",i);
173 comma= ",";
174 }
175 Tvba("]");
176 }
177
178 static void Tvbpollevents(int events) {
179 const char *delim= "";
180
181 events &= (POLLIN|POLLOUT|POLLPRI);
182 if (!events) { Tvba("0"); return; }
183 if (events & POLLIN) { Tvba("POLLIN"); delim= "|"; }
184 if (events & POLLOUT) { Tvba(delim); Tvba("POLLOUT"); delim= "|"; }
185 if (events & POLLPRI) { Tvba(delim); Tvba("POLLPRI"); }
186 }
187
188 void Tvbpollfds(const struct pollfd *fds, int nfds) {
189 const char *comma= "";
190
191 Tvba("[");
192 while (nfds>0) {
193 Tvba(comma);
194 Tvbf("{fd=%d, events=",fds->fd);
195 Tvbpollevents(fds->events);
196 Tvba(", revents=");
197 Tvbpollevents(fds->revents);
198 Tvba("}");
199 comma= ", ";
200 nfds--; fds++;
201 }
202 Tvba("]");
203 }
204
205 void Tvberrno(int e) {
206 const struct Terrno *te;
207
208 for (te= Terrnos; te->n && te->v != e; te++);
209 assert(te->n);
210 Tvba(te->n);
211 }
212
213 void Tvba(const char *str) {
214 if (!adns__vbuf_appendstr(&vb,str)) Tnomem();
215 }
216
217 void Tvbvf(const char *fmt, va_list al) {
218 char buf[1000];
219 buf[sizeof(buf)-2]= '\t';
220 vsnprintf(buf,sizeof(buf),fmt,al);
221 assert(buf[sizeof(buf)-2] == '\t');
222
223 Tvba(buf);
224 }
225
226 void Tvbf(const char *fmt, ...) {
227 va_list al;
228 va_start(al,fmt);
229 Tvbvf(fmt,al);
230 va_end(al);
231 }
232
233
234 void Tmust(const char *call, const char *arg, int cond) {
235 if (cond) return;
236 fprintf(stderr,"adns test harness: case not handled: system call %s, arg %s",call,arg);
237 exit(-1);
238 }
239
240 void Tfailed(const char *why) {
241 fprintf(stderr,"adns test harness: failure: %s: %s\n",why,strerror(errno));
242 exit(-1);
243 }
244
245 void Tnomem(void) {
246 Tfailed("unable to malloc/realloc");
247 }
248
249 void Toutputerr(void) {
250 Tfailed("write error on test harness output");
251 }
252
253 struct malloced {
254 struct malloced *next, *back;
255 size_t sz;
256 unsigned long count;
257 struct { double d; long ul; void *p; void (*fp)(void); } data;
258 };
259
260 static unsigned long malloccount, mallocfailat;
261 static struct { struct malloced *head, *tail; } mallocedlist;
262
263 #define MALLOCHSZ ((char*)&mallocedlist.head->data - (char*)mallocedlist.head)
264
265 void *Hmalloc(size_t sz) {
266 struct malloced *newnode;
267 const char *mfavar;
268 char *ep;
269
270 assert(sz);
271
272 newnode= malloc(MALLOCHSZ + sz); if (!newnode) Tnomem();
273
274 LIST_LINK_TAIL(mallocedlist,newnode);
275 newnode->sz= sz;
276 newnode->count= ++malloccount;
277 if (!mallocfailat) {
278 mfavar= getenv("ADNS_REGRESS_MALLOCFAILAT");
279 if (mfavar) {
280 mallocfailat= strtoul(mfavar,&ep,10);
281 if (!mallocfailat || *ep) Tfailed("ADNS_REGRESS_MALLOCFAILAT bad value");
282 } else {
283 mallocfailat= ~0UL;
284 }
285 }
286 assert(newnode->count != mallocfailat);
287 memset(&newnode->data,0xc7,sz);
288 return &newnode->data;
289 }
290
291 void Hfree(void *ptr) {
292 struct malloced *oldnode;
293
294 if (!ptr) return;
295
296 oldnode= (void*)((char*)ptr - MALLOCHSZ);
297 LIST_UNLINK(mallocedlist,oldnode);
298 memset(&oldnode->data,0x38,oldnode->sz);
299 free(oldnode);
300 }
301
302 void *Hrealloc(void *op, size_t nsz) {
303 struct malloced *oldnode;
304 void *np;
305 size_t osz;
306
307 if (op) { oldnode= (void*)((char*)op - MALLOCHSZ); osz= oldnode->sz; } else { osz= 0; }
308 np= Hmalloc(nsz);
309 if (osz) memcpy(np,op, osz>nsz ? nsz : osz);
310 Hfree(op);
311 return np;
312 }
313
314 void Texit(int rv) {
315 struct malloced *loopnode;
316
317 Tshutdown();
318 adns__vbuf_free(&vb);
319 adns__vbuf_free(&vbw);
320 if (mallocedlist.head) {
321 fprintf(stderr,"adns test harness: memory leaked:");
322 for (loopnode=mallocedlist.head; loopnode; loopnode=loopnode->next)
323 fprintf(stderr," %lu",loopnode->count);
324 putc('\n',stderr);
325 if (ferror(stderr)) exit(-1);
326 }
327 exit(rv);
328 }
329
330 void Hexit(int rv) {
331 vb.used= 0;
332 Tvbf("exit %d", rv);
333 Q_vb();
334 Texit(0);
335 }
336
337 pid_t Hgetpid(void) {
338 return 2264; /* just some number */
339 }
340