maintainer-clean
[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
5m4_dnl This file is part of adns, which is Copyright (C) 1997, 1998 Ian Jackson
6m4_dnl
7m4_dnl This program is free software; you can redistribute it and/or modify
8m4_dnl it under the terms of the GNU General Public License as published by
9m4_dnl the Free Software Foundation; either version 2, or (at your option)
10m4_dnl any later version.
11m4_dnl
12m4_dnl This program is distributed in the hope that it will be useful,
13m4_dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
14m4_dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15m4_dnl GNU General Public License for more details.
16m4_dnl
17m4_dnl You should have received a copy of the GNU General Public License
18m4_dnl along with this program; if not, write to the Free Software Foundation,
19m4_dnl Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
21m4_include(hmacros.i4)
22
23#include <assert.h>
24#include <string.h>
25#include <fcntl.h>
26#include <errno.h>
27#include <stdlib.h>
28
29#include <sys/socket.h>
30#include <netinet/in.h>
31#include <arpa/inet.h>
32
33#include "harness.h"
34
12c5b204 35static FILE *Tinputfile, *Treportfile;
70a778e5 36static vbuf vb2;
37
12c5b204 38static void Tensurereportfile(void) {
39 const char *fdstr;
40 int fd;
41
42 Treportfile= stderr;
43 fdstr= getenv("ADNS_TEST_REPORT_FD"); if (!fdstr) return;
44 fd= atoi(fdstr);
45 Treportfile= fdopen(fd,"a"); if (!Treportfile) Tfailed("fdopen ADNS_TEST_REPORT_FD");
46}
47
70a778e5 48static void Tensureinputfile(void) {
49 const char *fdstr;
50 int fd;
51
52 Tinputfile= stdin;
53 fdstr= getenv("ADNS_TEST_IN_FD"); if (!fdstr) return;
54 fd= atoi(fdstr);
55 Tinputfile= fdopen(fd,"r"); if (!Tinputfile) Tfailed("fdopen ADNS_TEST_IN_FD");
56}
57
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
68static void Parg(const char *argname) {
69 int l;
70
71 if (vb2.buf[vb2.used++] != hm_squote hm_squote) Psyntax("not a space before argument");
72 l= strlen(argname);
73 if (memcmp(vb2.buf+vb2.used,argname,l)) Psyntax("argument name wrong");
74 vb2.used+= l;
75 if (vb2.buf[vb2.used++] != hm_squote=hm_squote) Psyntax("not = after argument name");
76}
77
78static int Perrno(const char *stuff) {
79 const struct Terrno *te;
80 int r;
81 char *ep;
82
83 for (te= Terrnos; te->n && strcmp(te->n,stuff); te++);
84 if (te->n) return te->v;
85 r= strtoul(stuff+1,&ep,10);
86 if (ep) Psyntax("errno value not recognised, not numeric");
87 return r;
88}
89
90static struct timeval begin;
91
92static void Ptimevalabs(struct timeval *tvr) {
93 int store, chars;
94 unsigned long sec, usec;
95 struct timeval rv;
96
97 if (vb2.buf[vb2.used]==hm_squote+hm_squote) {
98 vb2.used++;
99 rv= begin;
100 store= 0;
101 } else {
102 rv.tv_sec= 0;
103 rv.tv_usec= 0;
104 store= 1;
105 }
106 chars= -1;
107 sscanf(vb2.buf+vb2.used,"%lu.%lu%n",&sec,&usec,&chars);
108 if (chars==-1) Psyntax("timeval syntax error");
109 rv.tv_sec += sec;
110 rv.tv_usec += usec;
111 if (begin.tv_usec > 1000000) {
112 rv.tv_sec++;
113 rv.tv_usec -= 1000000;
114 }
115 *tvr= rv;
116 if (store) begin= rv;
117
118 vb2.used += chars;
119}
120
121static void Pfdset(fd_set *set, int max) {
122 int r, c;
123 char *ep;
124
125 if (vb2.buf[vb2.used++] != hm_squote[hm_squote) Psyntax("fd set start not [");
126 FD_ZERO(set);
127 for (;;) {
128 r= strtoul(vb2.buf+vb2.used,&ep,10);
129 if (r>=max) Psyntax("fd set member > max");
130 FD_SET(r,set);
131 vb2.used= ep - (char*)vb2.buf;
132 c= vb2.buf[vb2.used++];
133 if (c == hm_squote]hm_squote) break;
134 if (c != hm_squote,hm_squote) Psyntax("fd set separator not ,");
135 }
136}
137
138static void Paddr(struct sockaddr *addr, int *lenr) {
139 struct sockaddr_in *sa= (struct sockaddr_in*)addr;
140 char *p, *ep;
141 long ul;
142
143 assert(*lenr >= sizeof(*sa));
144 p= strchr(vb2.buf+vb2.used,':');
145 if (!p) Psyntax("no port on address");
146 *p++= 0;
147 memset(sa,0,sizeof(*sa));
148 sa->sin_family= AF_INET;
149 if (!inet_aton(vb2.buf+vb2.used,&sa->sin_addr)) Psyntax("invalid address");
150 ul= strtoul(p,&ep,10);
151 if (*ep && *ep != hm_squote hm_squote) Psyntax("invalid port (bad syntax)");
152 if (ul >= 65536) Psyntax("port too large");
153 sa->sin_port= htons(ul);
154 *lenr= sizeof(*sa);
155
156 vb2.used= ep - (char*)vb2.buf;
157}
158
159static int Pbytes(byte *buf, int maxlen) {
160 static const char hexdigits[]= "0123456789abcdef";
161
162 int c, v, done;
163 const char *pf;
164
165 done= 0;
166 for (;;) {
167 c= getc(Tinputfile); Pcheckinput();
168 if (c=='\n' || c==' ' || c=='\t') continue;
169 if (c=='.') break;
170 pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid first hex digit");
171 v= (pf-hexdigits)<<4;
172 c= getc(Tinputfile); Pcheckinput();
173 pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid second hex digit");
174 v |= (pf-hexdigits);
175 if (maxlen<=0) Psyntax("buffer overflow in bytes");
176 *buf++= v;
177 maxlen--; done++;
178 }
179 for (;;) {
180 c= getc(Tinputfile); Pcheckinput();
181 if (c=='\n') return done;
182 }
183}
184
185void Q_vb(void) {
186 int r;
187
188 Tensureinputfile();
189 if (!adns__vbuf_ensure(&vb2,vb.used+2)) Tnomem();
190 r= fread(vb2.buf,1,vb.used+2,Tinputfile);
191 if (feof(Tinputfile)) {
192 fprintf(stderr,"adns test harness: input ends prematurely; program did:\n %.*s\n",
193 vb.used,vb.buf);
194 exit(-1);
195 }
196 Pcheckinput();
197 if (vb2.buf[0] != hm_squote hm_squote) Psyntax("not space before call");
198 if (memcmp(vb.buf,vb2.buf+1,vb.used) ||
199 vb2.buf[vb.used+1] != hm_squote\nhm_squote) {
200 fprintf(stderr,
201 "adns test harness: program did unexpected:\n %.*s\n"
202 "was expecting:\n %.*s\n",
203 vb.used,vb.buf, vb.used,vb2.buf+1);
204 exit(1);
205 }
206}
207
208m4_define(`hm_syscall', `
209 hm_create_proto_h
210int H$1(hm_args_massage($3,void)) {
211 int r;
212 m4_define(`hm_rv_fd',`char *ep;')
213 m4_define(`hm_rv_any',`char *ep;')
214 m4_define(`hm_rv_len',`')
215 m4_define(`hm_rv_must',`')
216 m4_define(`hm_rv_succfail',`')
217 $2
218
219 hm_create_hqcall_vars
220 $3
221
222 hm_create_hqcall_init($1)
223 $3
224
225 hm_create_hqcall_args
226 Q$1(hm_args_massage($3));
227
228 m4_define(`hm_r_offset',`m4_len(` $1=')')
229 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
230 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
12c5b204 231
232 Tensurereportfile();
233 fprintf(Treportfile,"syscallr %s",vb2.buf);
70a778e5 234 vb2.avail= strlen(vb2.buf);
235 if (vb.avail<=0 || vb2.buf[--vb2.avail]!=hm_squote\nhm_squote)
236 Psyntax("badly formed line");
237 vb2.buf[vb2.avail]= 0;
238 if (memcmp(vb2.buf," $1=",hm_r_offset)) Psyntax("syscall reply mismatch");
239
240 if (vb2.buf[hm_r_offset] == hm_squoteEhm_squote) {
241 errno= Perrno(vb2.buf+hm_r_offset);
242 return -1;
243 }
244
245 m4_define(`hm_rv_succfail',`
246 if (memcmp(vb2.buf+hm_r_offset,"OK",2)) Psyntax("success/fail not E* or OK");
247 vb2.used= hm_r_offset+2;
248 r= 0;
249 ')
250 m4_define(`hm_rv_len',`hm_rv_succfail')
251 m4_define(`hm_rv_must',`hm_rv_succfail')
252 m4_define(`hm_rv_any',`
253 r= strtoul(vb2.buf+hm_r_offset,&ep,10);
254 if (*ep && *ep!=hm_squote hm_squote) Psyntax("return value not E* or positive number");
255 vb2.used= ep - (char*)vb2.buf;
256 ')
257 m4_define(`hm_rv_fd',`hm_rv_any')
258 $2
259
260 hm_create_nothing
261 m4_define(`hm_arg_fdset_io',`Parg("$'`1"); Pfdset($'`1,$'`2);')
262 m4_define(`hm_arg_addr_out',`Parg("$'`1"); Paddr($'`1,$'`2);')
263 m4_define(`hm_arg_timeval_out_abs',`Parg("$'`1"); Ptimevalabs($'`1);')
264 $3
265 if (vb2.used != vb2.avail) Psyntax("junk at end of line");
266
267 hm_create_nothing
268 m4_define(`hm_arg_bytes_out',`r= Pbytes($'`2,$'`4);')
269 $3
270
271 return r;
272}
273')
274
275m4_include(`hsyscalls.i4')