+ Bugfixes:
[adns] / regress / hplayback.c.m4
CommitLineData
70a778e5 1m4_dnl hplayback.c.m4
2m4_dnl (part of complex test harness, not of the library)
3m4_dnl - playback routines
4
d942707d 5m4_dnl This file is
3d5cde09 6m4_dnl Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
d942707d 7m4_dnl
8m4_dnl It is part of adns, which is
3d5cde09 9m4_dnl Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
d942707d 10m4_dnl Copyright (C) 1999 Tony Finch <dot@dotat.at>
70a778e5 11m4_dnl
12m4_dnl This program is free software; you can redistribute it and/or modify
13m4_dnl it under the terms of the GNU General Public License as published by
14m4_dnl the Free Software Foundation; either version 2, or (at your option)
15m4_dnl any later version.
16m4_dnl
17m4_dnl This program is distributed in the hope that it will be useful,
18m4_dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
19m4_dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20m4_dnl GNU General Public License for more details.
21m4_dnl
22m4_dnl You should have received a copy of the GNU General Public License
23m4_dnl along with this program; if not, write to the Free Software Foundation,
24m4_dnl Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
26m4_include(hmacros.i4)
27
28#include <assert.h>
29#include <string.h>
70a778e5 30#include <errno.h>
31#include <stdlib.h>
32
bbede9a3 33#include <sys/types.h>
70a778e5 34#include <sys/socket.h>
35#include <netinet/in.h>
36#include <arpa/inet.h>
37
bbede9a3 38#include <unistd.h>
39#include <fcntl.h>
40
70a778e5 41#include "harness.h"
42
12c5b204 43static FILE *Tinputfile, *Treportfile;
70a778e5 44static vbuf vb2;
45
914a5ff5 46extern void Tshutdown(void) {
47 adns__vbuf_free(&vb2);
48}
49
12c5b204 50static 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 61static 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
66static void Pcheckinput(void) {
67 if (ferror(Tinputfile)) Tfailed("read test log input file");
68 if (feof(Tinputfile)) Psyntax("eof at syscall reply");
69}
70
98b6d5b4 71static void Tensureinputfile(void) {
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 96static 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 106static 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
115static void Pstring(const char *string, const char *emsg) {
116 if (Pstring_maybe(string)) return;
117 Psyntax(emsg);
118}
119
70a778e5 120static 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 132static 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
150static 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);
156 for (;;) {
157 r= strtoul(vb2.buf+vb2.used,&ep,10);
158 if (r>=max) Psyntax("fd set member > max");
159 FD_SET(r,set);
160 vb2.used= ep - (char*)vb2.buf;
161 c= vb2.buf[vb2.used++];
162 if (c == hm_squote]hm_squote) break;
163 if (c != hm_squote,hm_squote) Psyntax("fd set separator not ,");
164 }
165}
166
636b69b1 167#ifdef HAVE_POLL
8b1171e6 168static int Ppollfdevents(void) {
169 int events;
170
171 if (Pstring_maybe("0")) return 0;
172 events= 0;
173
174 if (Pstring_maybe("POLLIN")) {
175 events |= POLLIN;
176 if (!Pstring_maybe("|")) return events;
177 }
178
179 if (Pstring_maybe("POLLOUT")) {
180 events |= POLLOUT;
181 if (!Pstring_maybe("|")) return events;
182 }
183
184 Pstring("POLLPRI","pollfdevents PRI?");
185 return events;
186}
187
188static void Ppollfds(struct pollfd *fds, int nfds) {
189 int i;
190 char *ep;
191 const char *comma= "";
192
193 if (vb2.buf[vb2.used++] != hm_squote[hm_squote) Psyntax("pollfds start not [");
194 for (i=0; i<nfds; i++) {
195 Pstring("{fd=","{fd= in pollfds");
196 fds->fd= strtoul(vb2.buf+vb2.used,&ep,10);
197 vb2.used= ep - (char*)vb2.buf;
198 Pstring(", events=",", events= in pollfds");
199 fds->events= Ppollfdevents();
200 Pstring(", revents=",", revents= in pollfds");
201 fds->revents= Ppollfdevents();
202 Pstring("}","} in pollfds");
203 Pstring(comma,"separator in pollfds");
204 comma= ", ";
205 }
206 if (vb2.buf[vb2.used++] != hm_squote]hm_squote) Psyntax("pollfds end not ]");
207}
636b69b1 208#endif
8b1171e6 209
70a778e5 210static void Paddr(struct sockaddr *addr, int *lenr) {
211 struct sockaddr_in *sa= (struct sockaddr_in*)addr;
212 char *p, *ep;
213 long ul;
214
215 assert(*lenr >= sizeof(*sa));
216 p= strchr(vb2.buf+vb2.used,':');
217 if (!p) Psyntax("no port on address");
218 *p++= 0;
219 memset(sa,0,sizeof(*sa));
220 sa->sin_family= AF_INET;
221 if (!inet_aton(vb2.buf+vb2.used,&sa->sin_addr)) Psyntax("invalid address");
222 ul= strtoul(p,&ep,10);
223 if (*ep && *ep != hm_squote hm_squote) Psyntax("invalid port (bad syntax)");
224 if (ul >= 65536) Psyntax("port too large");
225 sa->sin_port= htons(ul);
226 *lenr= sizeof(*sa);
227
228 vb2.used= ep - (char*)vb2.buf;
229}
230
231static int Pbytes(byte *buf, int maxlen) {
232 static const char hexdigits[]= "0123456789abcdef";
233
234 int c, v, done;
235 const char *pf;
236
237 done= 0;
238 for (;;) {
239 c= getc(Tinputfile); Pcheckinput();
240 if (c=='\n' || c==' ' || c=='\t') continue;
241 if (c=='.') break;
242 pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid first hex digit");
243 v= (pf-hexdigits)<<4;
244 c= getc(Tinputfile); Pcheckinput();
245 pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid second hex digit");
246 v |= (pf-hexdigits);
247 if (maxlen<=0) Psyntax("buffer overflow in bytes");
248 *buf++= v;
249 maxlen--; done++;
250 }
251 for (;;) {
252 c= getc(Tinputfile); Pcheckinput();
253 if (c=='\n') return done;
254 }
255}
256
257void Q_vb(void) {
258 int r;
fc938da6 259 const char *nl;
70a778e5 260
261 Tensureinputfile();
262 if (!adns__vbuf_ensure(&vb2,vb.used+2)) Tnomem();
263 r= fread(vb2.buf,1,vb.used+2,Tinputfile);
264 if (feof(Tinputfile)) {
265 fprintf(stderr,"adns test harness: input ends prematurely; program did:\n %.*s\n",
266 vb.used,vb.buf);
267 exit(-1);
268 }
269 Pcheckinput();
270 if (vb2.buf[0] != hm_squote hm_squote) Psyntax("not space before call");
271 if (memcmp(vb.buf,vb2.buf+1,vb.used) ||
272 vb2.buf[vb.used+1] != hm_squote\nhm_squote) {
273 fprintf(stderr,
274 "adns test harness: program did unexpected:\n %.*s\n"
275 "was expecting:\n %.*s\n",
276 vb.used,vb.buf, vb.used,vb2.buf+1);
277 exit(1);
278 }
fc938da6 279 Tensurereportfile();
280 nl= memchr(vb.buf,'\n',vb.used);
281 fprintf(Treportfile," %.*s\n", (int)(nl ? nl - (const char*)vb.buf : vb.used), vb.buf);
70a778e5 282}
283
284m4_define(`hm_syscall', `
285 hm_create_proto_h
286int H$1(hm_args_massage($3,void)) {
68000374 287 int r, amtread;
70a778e5 288 m4_define(`hm_rv_fd',`char *ep;')
289 m4_define(`hm_rv_any',`char *ep;')
290 m4_define(`hm_rv_len',`')
291 m4_define(`hm_rv_must',`')
292 m4_define(`hm_rv_succfail',`')
73c4c523 293 m4_define(`hm_rv_fcntl',`')
70a778e5 294 $2
295
296 hm_create_hqcall_vars
297 $3
298
299 hm_create_hqcall_init($1)
300 $3
301
302 hm_create_hqcall_args
303 Q$1(hm_args_massage($3));
304
305 m4_define(`hm_r_offset',`m4_len(` $1=')')
306 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
307 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
12c5b204 308
309 Tensurereportfile();
fc938da6 310 fprintf(Treportfile,"%s",vb2.buf);
68000374 311 amtread= strlen(vb2.buf);
312 if (amtread<=0 || vb2.buf[--amtread]!=hm_squote\nhm_squote)
70a778e5 313 Psyntax("badly formed line");
68000374 314 vb2.buf[amtread]= 0;
70a778e5 315 if (memcmp(vb2.buf," $1=",hm_r_offset)) Psyntax("syscall reply mismatch");
316
317 if (vb2.buf[hm_r_offset] == hm_squoteEhm_squote) {
98b6d5b4 318 int e;
319 e= Perrno(vb2.buf+hm_r_offset);
320 P_updatetime();
321 errno= e;
70a778e5 322 return -1;
323 }
324
325 m4_define(`hm_rv_succfail',`
326 if (memcmp(vb2.buf+hm_r_offset,"OK",2)) Psyntax("success/fail not E* or OK");
327 vb2.used= hm_r_offset+2;
328 r= 0;
329 ')
330 m4_define(`hm_rv_len',`hm_rv_succfail')
331 m4_define(`hm_rv_must',`hm_rv_succfail')
332 m4_define(`hm_rv_any',`
333 r= strtoul(vb2.buf+hm_r_offset,&ep,10);
334 if (*ep && *ep!=hm_squote hm_squote) Psyntax("return value not E* or positive number");
335 vb2.used= ep - (char*)vb2.buf;
336 ')
337 m4_define(`hm_rv_fd',`hm_rv_any')
73c4c523 338 m4_define(`hm_rv_fcntl',`
339 r= 0;
340 if (cmd == F_GETFL) {
341 if (!memcmp(vb2.buf+hm_r_offset,"O_NONBLOCK|...",14)) {
342 r= O_NONBLOCK;
343 vb2.used= hm_r_offset+14;
344 } else if (!memcmp(vb2.buf+hm_r_offset,"~O_NONBLOCK&...",15)) {
345 vb2.used= hm_r_offset+15;
346 } else {
347 Psyntax("fcntl flags not O_NONBLOCK|... or ~O_NONBLOCK&...");
348 }
349 } else if (cmd == F_SETFL) {
350 hm_rv_succfail
351 } else {
352 Psyntax("fcntl not F_GETFL or F_SETFL");
353 }
354 ')
70a778e5 355 $2
356
357 hm_create_nothing
358 m4_define(`hm_arg_fdset_io',`Parg("$'`1"); Pfdset($'`1,$'`2);')
8b1171e6 359 m4_define(`hm_arg_pollfds_io',`Parg("$'`1"); Ppollfds($'`1,$'`2);')
70a778e5 360 m4_define(`hm_arg_addr_out',`Parg("$'`1"); Paddr($'`1,$'`2);')
70a778e5 361 $3
68000374 362 assert(vb2.used <= amtread);
363 if (vb2.used != amtread) Psyntax("junk at end of line");
70a778e5 364
365 hm_create_nothing
366 m4_define(`hm_arg_bytes_out',`r= Pbytes($'`2,$'`4);')
367 $3
368
98b6d5b4 369 P_updatetime();
70a778e5 370 return r;
371}
372')
373
374m4_include(`hsyscalls.i4')