70a778e5 |
1 | m4_dnl hplayback.c.m4 |
2 | m4_dnl (part of complex test harness, not of the library) |
3 | m4_dnl - playback routines |
4 | |
ae8cc977 |
5 | m4_dnl This file is part of adns, which is |
6 | m4_dnl Copyright (C) 1997-2000,2003,2006 Ian Jackson |
7 | m4_dnl Copyright (C) 1999-2000,2003,2006 Tony Finch |
8 | m4_dnl Copyright (C) 1991 Massachusetts Institute of Technology |
9 | m4_dnl (See the file INSTALL for full details.) |
70a778e5 |
10 | m4_dnl |
11 | m4_dnl This program is free software; you can redistribute it and/or modify |
12 | m4_dnl it under the terms of the GNU General Public License as published by |
13 | m4_dnl the Free Software Foundation; either version 2, or (at your option) |
14 | m4_dnl any later version. |
15 | m4_dnl |
16 | m4_dnl This program is distributed in the hope that it will be useful, |
17 | m4_dnl but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | m4_dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | m4_dnl GNU General Public License for more details. |
20 | m4_dnl |
21 | m4_dnl You should have received a copy of the GNU General Public License |
22 | m4_dnl along with this program; if not, write to the Free Software Foundation, |
23 | m4_dnl Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
24 | |
25 | m4_include(hmacros.i4) |
26 | |
27 | #include <assert.h> |
28 | #include <string.h> |
70a778e5 |
29 | #include <errno.h> |
30 | #include <stdlib.h> |
31 | |
bbede9a3 |
32 | #include <sys/types.h> |
70a778e5 |
33 | #include <sys/socket.h> |
34 | #include <netinet/in.h> |
35 | #include <arpa/inet.h> |
a069d77f |
36 | #include <sys/time.h> |
70a778e5 |
37 | |
bbede9a3 |
38 | #include <unistd.h> |
39 | #include <fcntl.h> |
40 | |
70a778e5 |
41 | #include "harness.h" |
42 | |
12c5b204 |
43 | static FILE *Tinputfile, *Treportfile; |
70a778e5 |
44 | static vbuf vb2; |
45 | |
914a5ff5 |
46 | extern void Tshutdown(void) { |
47 | adns__vbuf_free(&vb2); |
48 | } |
49 | |
12c5b204 |
50 | static void Tensurereportfile(void) { |
51 | const char *fdstr; |
52 | int fd; |
53 | |
68000374 |
54 | if (Treportfile) return; |
12c5b204 |
55 | Treportfile= stderr; |
56 | fdstr= getenv("ADNS_TEST_REPORT_FD"); if (!fdstr) return; |
57 | fd= atoi(fdstr); |
58 | Treportfile= fdopen(fd,"a"); if (!Treportfile) Tfailed("fdopen ADNS_TEST_REPORT_FD"); |
59 | } |
60 | |
70a778e5 |
61 | static void Psyntax(const char *where) { |
62 | fprintf(stderr,"adns test harness: syntax error in test log input file: %s\n",where); |
63 | exit(-1); |
64 | } |
65 | |
66 | static void Pcheckinput(void) { |
67 | if (ferror(Tinputfile)) Tfailed("read test log input file"); |
68 | if (feof(Tinputfile)) Psyntax("eof at syscall reply"); |
69 | } |
70 | |
87dd6345 |
71 | void Tensurerecordfile(void) { |
98b6d5b4 |
72 | const char *fdstr; |
73 | int fd; |
74 | int chars; |
75 | unsigned long sec, usec; |
76 | |
77 | if (Tinputfile) return; |
78 | Tinputfile= stdin; |
79 | fdstr= getenv("ADNS_TEST_IN_FD"); |
80 | if (fdstr) { |
81 | fd= atoi(fdstr); |
82 | Tinputfile= fdopen(fd,"r"); if (!Tinputfile) Tfailed("fdopen ADNS_TEST_IN_FD"); |
83 | } |
4fad263d |
84 | setvbuf(Tinputfile,0,_IONBF,0); |
98b6d5b4 |
85 | |
86 | if (!adns__vbuf_ensure(&vb2,1000)) Tnomem(); |
87 | fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput(); |
88 | chars= -1; |
89 | sscanf(vb2.buf," start %lu.%lu%n",&sec,&usec,&chars); |
90 | if (chars==-1) Psyntax("start time invalid"); |
91 | currenttime.tv_sec= sec; |
92 | currenttime.tv_usec= usec; |
93 | if (vb2.buf[chars] != hm_squote\nhm_squote) Psyntax("not newline after start time"); |
94 | } |
95 | |
70a778e5 |
96 | static void Parg(const char *argname) { |
97 | int l; |
98 | |
99 | if (vb2.buf[vb2.used++] != hm_squote hm_squote) Psyntax("not a space before argument"); |
100 | l= strlen(argname); |
101 | if (memcmp(vb2.buf+vb2.used,argname,l)) Psyntax("argument name wrong"); |
102 | vb2.used+= l; |
103 | if (vb2.buf[vb2.used++] != hm_squote=hm_squote) Psyntax("not = after argument name"); |
104 | } |
105 | |
8b1171e6 |
106 | static int Pstring_maybe(const char *string) { |
107 | int l; |
108 | |
109 | l= strlen(string); |
110 | if (memcmp(vb2.buf+vb2.used,string,l)) return 0; |
111 | vb2.used+= l; |
112 | return 1; |
113 | } |
114 | |
115 | static void Pstring(const char *string, const char *emsg) { |
116 | if (Pstring_maybe(string)) return; |
117 | Psyntax(emsg); |
118 | } |
119 | |
70a778e5 |
120 | static int Perrno(const char *stuff) { |
121 | const struct Terrno *te; |
122 | int r; |
123 | char *ep; |
124 | |
125 | for (te= Terrnos; te->n && strcmp(te->n,stuff); te++); |
126 | if (te->n) return te->v; |
79833e62 |
127 | r= strtoul(stuff+2,&ep,10); |
128 | if (*ep) Psyntax("errno value not recognised, not numeric"); |
70a778e5 |
129 | return r; |
130 | } |
131 | |
98b6d5b4 |
132 | static void P_updatetime(void) { |
133 | int chars; |
70a778e5 |
134 | unsigned long sec, usec; |
98b6d5b4 |
135 | |
136 | if (!adns__vbuf_ensure(&vb2,1000)) Tnomem(); |
137 | fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput(); |
70a778e5 |
138 | chars= -1; |
98b6d5b4 |
139 | sscanf(vb2.buf," +%lu.%lu%n",&sec,&usec,&chars); |
140 | if (chars==-1) Psyntax("update time invalid"); |
141 | currenttime.tv_sec+= sec; |
142 | currenttime.tv_usec+= usec; |
143 | if (currenttime.tv_usec > 1000000) { |
144 | currenttime.tv_sec++; |
145 | currenttime.tv_usec -= 1000000; |
70a778e5 |
146 | } |
98b6d5b4 |
147 | if (vb2.buf[chars] != hm_squote\nhm_squote) Psyntax("not newline after update time"); |
70a778e5 |
148 | } |
149 | |
150 | static void Pfdset(fd_set *set, int max) { |
151 | int r, c; |
152 | char *ep; |
153 | |
154 | if (vb2.buf[vb2.used++] != hm_squote[hm_squote) Psyntax("fd set start not ["); |
155 | FD_ZERO(set); |
fe950039 |
156 | if (vb2.buf[vb2.used] == hm_squote]hm_squote) { vb2.used++; return; } |
70a778e5 |
157 | for (;;) { |
158 | r= strtoul(vb2.buf+vb2.used,&ep,10); |
159 | if (r>=max) Psyntax("fd set member > max"); |
fe950039 |
160 | if (ep == (char*)vb2.buf+vb2.used) Psyntax("empty entry in fd set"); |
70a778e5 |
161 | FD_SET(r,set); |
162 | vb2.used= ep - (char*)vb2.buf; |
163 | c= vb2.buf[vb2.used++]; |
164 | if (c == hm_squote]hm_squote) break; |
165 | if (c != hm_squote,hm_squote) Psyntax("fd set separator not ,"); |
166 | } |
167 | } |
168 | |
636b69b1 |
169 | #ifdef HAVE_POLL |
8b1171e6 |
170 | static int Ppollfdevents(void) { |
171 | int events; |
172 | |
173 | if (Pstring_maybe("0")) return 0; |
174 | events= 0; |
175 | |
176 | if (Pstring_maybe("POLLIN")) { |
177 | events |= POLLIN; |
178 | if (!Pstring_maybe("|")) return events; |
179 | } |
180 | |
181 | if (Pstring_maybe("POLLOUT")) { |
182 | events |= POLLOUT; |
183 | if (!Pstring_maybe("|")) return events; |
184 | } |
185 | |
186 | Pstring("POLLPRI","pollfdevents PRI?"); |
187 | return events; |
188 | } |
189 | |
190 | static void Ppollfds(struct pollfd *fds, int nfds) { |
191 | int i; |
192 | char *ep; |
193 | const char *comma= ""; |
194 | |
195 | if (vb2.buf[vb2.used++] != hm_squote[hm_squote) Psyntax("pollfds start not ["); |
196 | for (i=0; i<nfds; i++) { |
197 | Pstring("{fd=","{fd= in pollfds"); |
198 | fds->fd= strtoul(vb2.buf+vb2.used,&ep,10); |
199 | vb2.used= ep - (char*)vb2.buf; |
200 | Pstring(", events=",", events= in pollfds"); |
201 | fds->events= Ppollfdevents(); |
202 | Pstring(", revents=",", revents= in pollfds"); |
203 | fds->revents= Ppollfdevents(); |
204 | Pstring("}","} in pollfds"); |
205 | Pstring(comma,"separator in pollfds"); |
206 | comma= ", "; |
207 | } |
208 | if (vb2.buf[vb2.used++] != hm_squote]hm_squote) Psyntax("pollfds end not ]"); |
209 | } |
636b69b1 |
210 | #endif |
8b1171e6 |
211 | |
70a778e5 |
212 | static void Paddr(struct sockaddr *addr, int *lenr) { |
213 | struct sockaddr_in *sa= (struct sockaddr_in*)addr; |
214 | char *p, *ep; |
215 | long ul; |
216 | |
217 | assert(*lenr >= sizeof(*sa)); |
218 | p= strchr(vb2.buf+vb2.used,':'); |
219 | if (!p) Psyntax("no port on address"); |
220 | *p++= 0; |
221 | memset(sa,0,sizeof(*sa)); |
222 | sa->sin_family= AF_INET; |
223 | if (!inet_aton(vb2.buf+vb2.used,&sa->sin_addr)) Psyntax("invalid address"); |
224 | ul= strtoul(p,&ep,10); |
225 | if (*ep && *ep != hm_squote hm_squote) Psyntax("invalid port (bad syntax)"); |
226 | if (ul >= 65536) Psyntax("port too large"); |
227 | sa->sin_port= htons(ul); |
228 | *lenr= sizeof(*sa); |
229 | |
230 | vb2.used= ep - (char*)vb2.buf; |
231 | } |
232 | |
233 | static int Pbytes(byte *buf, int maxlen) { |
234 | static const char hexdigits[]= "0123456789abcdef"; |
235 | |
236 | int c, v, done; |
237 | const char *pf; |
238 | |
239 | done= 0; |
240 | for (;;) { |
241 | c= getc(Tinputfile); Pcheckinput(); |
242 | if (c=='\n' || c==' ' || c=='\t') continue; |
243 | if (c=='.') break; |
244 | pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid first hex digit"); |
245 | v= (pf-hexdigits)<<4; |
246 | c= getc(Tinputfile); Pcheckinput(); |
247 | pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid second hex digit"); |
248 | v |= (pf-hexdigits); |
249 | if (maxlen<=0) Psyntax("buffer overflow in bytes"); |
250 | *buf++= v; |
251 | maxlen--; done++; |
252 | } |
253 | for (;;) { |
254 | c= getc(Tinputfile); Pcheckinput(); |
255 | if (c=='\n') return done; |
256 | } |
257 | } |
258 | |
259 | void Q_vb(void) { |
fc938da6 |
260 | const char *nl; |
70a778e5 |
261 | |
87dd6345 |
262 | Tensurerecordfile(); |
70a778e5 |
263 | if (!adns__vbuf_ensure(&vb2,vb.used+2)) Tnomem(); |
faa52594 |
264 | fread(vb2.buf,1,vb.used+2,Tinputfile); |
70a778e5 |
265 | if (feof(Tinputfile)) { |
266 | fprintf(stderr,"adns test harness: input ends prematurely; program did:\n %.*s\n", |
267 | vb.used,vb.buf); |
268 | exit(-1); |
269 | } |
270 | Pcheckinput(); |
271 | if (vb2.buf[0] != hm_squote hm_squote) Psyntax("not space before call"); |
272 | if (memcmp(vb.buf,vb2.buf+1,vb.used) || |
273 | vb2.buf[vb.used+1] != hm_squote\nhm_squote) { |
274 | fprintf(stderr, |
275 | "adns test harness: program did unexpected:\n %.*s\n" |
276 | "was expecting:\n %.*s\n", |
277 | vb.used,vb.buf, vb.used,vb2.buf+1); |
278 | exit(1); |
279 | } |
fc938da6 |
280 | Tensurereportfile(); |
281 | nl= memchr(vb.buf,'\n',vb.used); |
282 | fprintf(Treportfile," %.*s\n", (int)(nl ? nl - (const char*)vb.buf : vb.used), vb.buf); |
70a778e5 |
283 | } |
284 | |
285 | m4_define(`hm_syscall', ` |
286 | hm_create_proto_h |
287 | int H$1(hm_args_massage($3,void)) { |
68000374 |
288 | int r, amtread; |
70a778e5 |
289 | m4_define(`hm_rv_fd',`char *ep;') |
290 | m4_define(`hm_rv_any',`char *ep;') |
291 | m4_define(`hm_rv_len',`') |
292 | m4_define(`hm_rv_must',`') |
293 | m4_define(`hm_rv_succfail',`') |
73c4c523 |
294 | m4_define(`hm_rv_fcntl',`') |
70a778e5 |
295 | $2 |
296 | |
297 | hm_create_hqcall_vars |
298 | $3 |
299 | |
300 | hm_create_hqcall_init($1) |
301 | $3 |
302 | |
303 | hm_create_hqcall_args |
304 | Q$1(hm_args_massage($3)); |
305 | |
306 | m4_define(`hm_r_offset',`m4_len(` $1=')') |
307 | if (!adns__vbuf_ensure(&vb2,1000)) Tnomem(); |
308 | fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput(); |
12c5b204 |
309 | |
310 | Tensurereportfile(); |
fc938da6 |
311 | fprintf(Treportfile,"%s",vb2.buf); |
68000374 |
312 | amtread= strlen(vb2.buf); |
313 | if (amtread<=0 || vb2.buf[--amtread]!=hm_squote\nhm_squote) |
70a778e5 |
314 | Psyntax("badly formed line"); |
68000374 |
315 | vb2.buf[amtread]= 0; |
70a778e5 |
316 | if (memcmp(vb2.buf," $1=",hm_r_offset)) Psyntax("syscall reply mismatch"); |
317 | |
318 | if (vb2.buf[hm_r_offset] == hm_squoteEhm_squote) { |
98b6d5b4 |
319 | int e; |
320 | e= Perrno(vb2.buf+hm_r_offset); |
321 | P_updatetime(); |
322 | errno= e; |
70a778e5 |
323 | return -1; |
324 | } |
325 | |
326 | m4_define(`hm_rv_succfail',` |
327 | if (memcmp(vb2.buf+hm_r_offset,"OK",2)) Psyntax("success/fail not E* or OK"); |
328 | vb2.used= hm_r_offset+2; |
329 | r= 0; |
330 | ') |
331 | m4_define(`hm_rv_len',`hm_rv_succfail') |
332 | m4_define(`hm_rv_must',`hm_rv_succfail') |
333 | m4_define(`hm_rv_any',` |
334 | r= strtoul(vb2.buf+hm_r_offset,&ep,10); |
335 | if (*ep && *ep!=hm_squote hm_squote) Psyntax("return value not E* or positive number"); |
336 | vb2.used= ep - (char*)vb2.buf; |
337 | ') |
338 | m4_define(`hm_rv_fd',`hm_rv_any') |
73c4c523 |
339 | m4_define(`hm_rv_fcntl',` |
340 | r= 0; |
341 | if (cmd == F_GETFL) { |
342 | if (!memcmp(vb2.buf+hm_r_offset,"O_NONBLOCK|...",14)) { |
343 | r= O_NONBLOCK; |
344 | vb2.used= hm_r_offset+14; |
345 | } else if (!memcmp(vb2.buf+hm_r_offset,"~O_NONBLOCK&...",15)) { |
346 | vb2.used= hm_r_offset+15; |
347 | } else { |
348 | Psyntax("fcntl flags not O_NONBLOCK|... or ~O_NONBLOCK&..."); |
349 | } |
350 | } else if (cmd == F_SETFL) { |
351 | hm_rv_succfail |
352 | } else { |
353 | Psyntax("fcntl not F_GETFL or F_SETFL"); |
354 | } |
355 | ') |
70a778e5 |
356 | $2 |
357 | |
358 | hm_create_nothing |
359 | m4_define(`hm_arg_fdset_io',`Parg("$'`1"); Pfdset($'`1,$'`2);') |
8b1171e6 |
360 | m4_define(`hm_arg_pollfds_io',`Parg("$'`1"); Ppollfds($'`1,$'`2);') |
70a778e5 |
361 | m4_define(`hm_arg_addr_out',`Parg("$'`1"); Paddr($'`1,$'`2);') |
70a778e5 |
362 | $3 |
68000374 |
363 | assert(vb2.used <= amtread); |
364 | if (vb2.used != amtread) Psyntax("junk at end of line"); |
70a778e5 |
365 | |
366 | hm_create_nothing |
367 | m4_define(`hm_arg_bytes_out',`r= Pbytes($'`2,$'`4);') |
368 | $3 |
369 | |
98b6d5b4 |
370 | P_updatetime(); |
70a778e5 |
371 | return r; |
372 | } |
373 | ') |
374 | |
0ebff22d |
375 | m4_define(`hm_specsyscall', `') |
376 | |
70a778e5 |
377 | m4_include(`hsyscalls.i4') |