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