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