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