First record and playback works.
[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
35static FILE *Tinputfile;
36static vbuf vb2;
37
38static void Tensureinputfile(void) {
39 const char *fdstr;
40 int fd;
41
42 Tinputfile= stdin;
43 fdstr= getenv("ADNS_TEST_IN_FD"); if (!fdstr) return;
44 fd= atoi(fdstr);
45 Tinputfile= fdopen(fd,"r"); if (!Tinputfile) Tfailed("fdopen ADNS_TEST_IN_FD");
46}
47
48static void Psyntax(const char *where) {
49 fprintf(stderr,"adns test harness: syntax error in test log input file: %s\n",where);
50 exit(-1);
51}
52
53static void Pcheckinput(void) {
54 if (ferror(Tinputfile)) Tfailed("read test log input file");
55 if (feof(Tinputfile)) Psyntax("eof at syscall reply");
56}
57
58static void Parg(const char *argname) {
59 int l;
60
61 if (vb2.buf[vb2.used++] != hm_squote hm_squote) Psyntax("not a space before argument");
62 l= strlen(argname);
63 if (memcmp(vb2.buf+vb2.used,argname,l)) Psyntax("argument name wrong");
64 vb2.used+= l;
65 if (vb2.buf[vb2.used++] != hm_squote=hm_squote) Psyntax("not = after argument name");
66}
67
68static int Perrno(const char *stuff) {
69 const struct Terrno *te;
70 int r;
71 char *ep;
72
73 for (te= Terrnos; te->n && strcmp(te->n,stuff); te++);
74 if (te->n) return te->v;
75 r= strtoul(stuff+1,&ep,10);
76 if (ep) Psyntax("errno value not recognised, not numeric");
77 return r;
78}
79
80static struct timeval begin;
81
82static void Ptimevalabs(struct timeval *tvr) {
83 int store, chars;
84 unsigned long sec, usec;
85 struct timeval rv;
86
87 if (vb2.buf[vb2.used]==hm_squote+hm_squote) {
88 vb2.used++;
89 rv= begin;
90 store= 0;
91 } else {
92 rv.tv_sec= 0;
93 rv.tv_usec= 0;
94 store= 1;
95 }
96 chars= -1;
97 sscanf(vb2.buf+vb2.used,"%lu.%lu%n",&sec,&usec,&chars);
98 if (chars==-1) Psyntax("timeval syntax error");
99 rv.tv_sec += sec;
100 rv.tv_usec += usec;
101 if (begin.tv_usec > 1000000) {
102 rv.tv_sec++;
103 rv.tv_usec -= 1000000;
104 }
105 *tvr= rv;
106 if (store) begin= rv;
107
108 vb2.used += chars;
109}
110
111static void Pfdset(fd_set *set, int max) {
112 int r, c;
113 char *ep;
114
115 if (vb2.buf[vb2.used++] != hm_squote[hm_squote) Psyntax("fd set start not [");
116 FD_ZERO(set);
117 for (;;) {
118 r= strtoul(vb2.buf+vb2.used,&ep,10);
119 if (r>=max) Psyntax("fd set member > max");
120 FD_SET(r,set);
121 vb2.used= ep - (char*)vb2.buf;
122 c= vb2.buf[vb2.used++];
123 if (c == hm_squote]hm_squote) break;
124 if (c != hm_squote,hm_squote) Psyntax("fd set separator not ,");
125 }
126}
127
128static void Paddr(struct sockaddr *addr, int *lenr) {
129 struct sockaddr_in *sa= (struct sockaddr_in*)addr;
130 char *p, *ep;
131 long ul;
132
133 assert(*lenr >= sizeof(*sa));
134 p= strchr(vb2.buf+vb2.used,':');
135 if (!p) Psyntax("no port on address");
136 *p++= 0;
137 memset(sa,0,sizeof(*sa));
138 sa->sin_family= AF_INET;
139 if (!inet_aton(vb2.buf+vb2.used,&sa->sin_addr)) Psyntax("invalid address");
140 ul= strtoul(p,&ep,10);
141 if (*ep && *ep != hm_squote hm_squote) Psyntax("invalid port (bad syntax)");
142 if (ul >= 65536) Psyntax("port too large");
143 sa->sin_port= htons(ul);
144 *lenr= sizeof(*sa);
145
146 vb2.used= ep - (char*)vb2.buf;
147}
148
149static int Pbytes(byte *buf, int maxlen) {
150 static const char hexdigits[]= "0123456789abcdef";
151
152 int c, v, done;
153 const char *pf;
154
155 done= 0;
156 for (;;) {
157 c= getc(Tinputfile); Pcheckinput();
158 if (c=='\n' || c==' ' || c=='\t') continue;
159 if (c=='.') break;
160 pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid first hex digit");
161 v= (pf-hexdigits)<<4;
162 c= getc(Tinputfile); Pcheckinput();
163 pf= strchr(hexdigits,c); if (!pf) Psyntax("invalid second hex digit");
164 v |= (pf-hexdigits);
165 if (maxlen<=0) Psyntax("buffer overflow in bytes");
166 *buf++= v;
167 maxlen--; done++;
168 }
169 for (;;) {
170 c= getc(Tinputfile); Pcheckinput();
171 if (c=='\n') return done;
172 }
173}
174
175void Q_vb(void) {
176 int r;
177
178 Tensureinputfile();
179 if (!adns__vbuf_ensure(&vb2,vb.used+2)) Tnomem();
180 r= fread(vb2.buf,1,vb.used+2,Tinputfile);
181 if (feof(Tinputfile)) {
182 fprintf(stderr,"adns test harness: input ends prematurely; program did:\n %.*s\n",
183 vb.used,vb.buf);
184 exit(-1);
185 }
186 Pcheckinput();
187 if (vb2.buf[0] != hm_squote hm_squote) Psyntax("not space before call");
188 if (memcmp(vb.buf,vb2.buf+1,vb.used) ||
189 vb2.buf[vb.used+1] != hm_squote\nhm_squote) {
190 fprintf(stderr,
191 "adns test harness: program did unexpected:\n %.*s\n"
192 "was expecting:\n %.*s\n",
193 vb.used,vb.buf, vb.used,vb2.buf+1);
194 exit(1);
195 }
196}
197
198m4_define(`hm_syscall', `
199 hm_create_proto_h
200int H$1(hm_args_massage($3,void)) {
201 int r;
202 m4_define(`hm_rv_fd',`char *ep;')
203 m4_define(`hm_rv_any',`char *ep;')
204 m4_define(`hm_rv_len',`')
205 m4_define(`hm_rv_must',`')
206 m4_define(`hm_rv_succfail',`')
207 $2
208
209 hm_create_hqcall_vars
210 $3
211
212 hm_create_hqcall_init($1)
213 $3
214
215 hm_create_hqcall_args
216 Q$1(hm_args_massage($3));
217
218 m4_define(`hm_r_offset',`m4_len(` $1=')')
219 if (!adns__vbuf_ensure(&vb2,1000)) Tnomem();
220 fgets(vb2.buf,vb2.avail,Tinputfile); Pcheckinput();
221fprintf(stderr,"syscallr %s",vb2.buf);
222 vb2.avail= strlen(vb2.buf);
223 if (vb.avail<=0 || vb2.buf[--vb2.avail]!=hm_squote\nhm_squote)
224 Psyntax("badly formed line");
225 vb2.buf[vb2.avail]= 0;
226 if (memcmp(vb2.buf," $1=",hm_r_offset)) Psyntax("syscall reply mismatch");
227
228 if (vb2.buf[hm_r_offset] == hm_squoteEhm_squote) {
229 errno= Perrno(vb2.buf+hm_r_offset);
230 return -1;
231 }
232
233 m4_define(`hm_rv_succfail',`
234 if (memcmp(vb2.buf+hm_r_offset,"OK",2)) Psyntax("success/fail not E* or OK");
235 vb2.used= hm_r_offset+2;
236 r= 0;
237 ')
238 m4_define(`hm_rv_len',`hm_rv_succfail')
239 m4_define(`hm_rv_must',`hm_rv_succfail')
240 m4_define(`hm_rv_any',`
241 r= strtoul(vb2.buf+hm_r_offset,&ep,10);
242 if (*ep && *ep!=hm_squote hm_squote) Psyntax("return value not E* or positive number");
243 vb2.used= ep - (char*)vb2.buf;
244 ')
245 m4_define(`hm_rv_fd',`hm_rv_any')
246 $2
247
248 hm_create_nothing
249 m4_define(`hm_arg_fdset_io',`Parg("$'`1"); Pfdset($'`1,$'`2);')
250 m4_define(`hm_arg_addr_out',`Parg("$'`1"); Paddr($'`1,$'`2);')
251 m4_define(`hm_arg_timeval_out_abs',`Parg("$'`1"); Ptimevalabs($'`1);')
252 $3
253 if (vb2.used != vb2.avail) Psyntax("junk at end of line");
254
255 hm_create_nothing
256 m4_define(`hm_arg_bytes_out',`r= Pbytes($'`2,$'`4);')
257 $3
258
259 return r;
260}
261')
262
263m4_include(`hsyscalls.i4')