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