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