chiark / gitweb /
Correct error messages for qname CNAME foo, foo CNAME bar.
[adns] / regress / hcommon.c.m4
CommitLineData
078c4fd0 1m4_dnl hcommon.c
2m4_dnl (part of complex test harness, not of the library)
3m4_dnl - routines used for both record and playback
4
3ff64957 5m4_dnl This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson
078c4fd0 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)
74cf211e 22
4fb3d854 23#include <fcntl.h>
24#include <string.h>
25#include <errno.h>
26
27#include <sys/socket.h>
28#include <netinet/in.h>
29#include <arpa/inet.h>
30
74cf211e 31#include "harness.h"
32#include "internal.h"
33
4fb3d854 34vbuf vb;
35FILE *Toutputfile= 0;
3c5796f7 36struct timeval currenttime;
4fb3d854 37
38const struct Terrno Terrnos[]= {
39 { "EAGAIN", EAGAIN },
40 { "EINPROGRESS", EINPROGRESS },
41 { "EINTR", EINTR },
42 { "EINVAL", EINVAL },
43 { "EMSGSIZE", EMSGSIZE },
44 { "ENOBUFS", ENOBUFS },
45 { "ENOENT", ENOENT },
46 { "ENOPROTOOPT", ENOPROTOOPT },
47 { "ENOSPC", ENOSPC },
48 { "EWOULDBLOCK", EWOULDBLOCK },
49 { 0, 0 }
50};
74cf211e 51
9ba9f156 52static vbuf vbw;
4fb3d854 53
3c5796f7 54int Hgettimeofday(struct timeval *tv, struct timezone *tz) {
55 Tmust("gettimeofday","tz",!tz);
56 *tv= currenttime;
57 return 0;
58}
59
9ba9f156 60int Hwritev(int fd, const struct iovec *vector, size_t count) {
61 size_t i;
62
63 vbw.used= 0;
64 for (i=0; i<count; i++, vector++) {
65 if (!adns__vbuf_append(&vbw,vector->iov_base,vector->iov_len)) Tnomem();
66 }
67 return Hwrite(fd,vbw.buf,vbw.used);
68}
4fb3d854 69
078c4fd0 70m4_define(`hm_syscall', `
71 hm_create_proto_q
72void Q$1(hm_args_massage($3,void)) {
73
74 vb.used= 0;
75 Tvba("$1");
76 m4_define(`hm_na',`')
77 m4_define(`hm_arg_nullptr',`')
78 m4_define(`hm_arg_int', `Tvbf(" $'`1=%d",$'`1);')
79 m4_define(`hm_arg_fdset_io', `Tvbf(" $'`1="); Tvbfdset($'`2,$'`1);')
2d5cce62 80 m4_define(`hm_arg_pollfds_io', `Tvbf(" $'`1="); Tvbpollfds($'`1,$'`2);')
078c4fd0 81 m4_define(`hm_arg_timeval_in_rel_null', `
82 if ($'`1) Tvbf(" $'`1=%ld.%06ld",(long)$'`1->tv_sec,(long)$'`1->tv_usec);
83 else Tvba(" $'`1=null");')
84 m4_define(`hm_arg_must', `')
85 m4_define(`hm_arg_socktype', `
86 Tvbf($'`1==SOCK_STREAM ? " $'`1=SOCK_STREAM" : " $'`1=SOCK_DGRAM");')
87 m4_define(`hm_arg_ign', `')
88 m4_define(`hm_arg_fd', `Tvbf(" $'`1=%d",$'`1);')
89 m4_define(`hm_arg_fcntl_cmd_arg', `
90 if ($'`1 == F_SETFL) {
09a06b84 91 Tvbf(" $'`1=F_SETFL %s",arg & O_NONBLOCK ? "O_NONBLOCK|..." : "~O_NONBLOCK&...");
92 } else if ($'`1 == F_GETFL) {
078c4fd0 93 Tvba(" $'`1=F_GETFL");
09a06b84 94 } else {
95 Tmust("$'`1","F_GETFL/F_SETFL",0);
078c4fd0 96 }')
9ba9f156 97 m4_define(`hm_arg_addr_in', `Tvba(" $'`1="); Tvbaddr($'`1,$'`2);')
98 m4_define(`hm_arg_bytes_in', `')
078c4fd0 99 m4_define(`hm_arg_bytes_out', `Tvbf(" $'`4=%lu",(unsigned long)$'`4);')
100 m4_define(`hm_arg_addr_out', `Tvbf(" *$'`2=%d",$'`2);')
101 $3
4fb3d854 102
9ba9f156 103 hm_create_nothing
104 m4_define(`hm_arg_bytes_in', `Tvbbytes($'`2,$'`4);')
105 $3
106
4fb3d854 107 Q_vb();
108}
078c4fd0 109')
4fb3d854 110
078c4fd0 111m4_include(`hsyscalls.i4')
4fb3d854 112
113
114void Tvbaddr(const struct sockaddr *addr, int len) {
115 const struct sockaddr_in *ai= (const struct sockaddr_in*)addr;
116
117 assert(len==sizeof(struct sockaddr_in));
118 assert(ai->sin_family==AF_INET);
078c4fd0 119 Tvbf("%s:%u",inet_ntoa(ai->sin_addr),htons(ai->sin_port));
4fb3d854 120}
121
122void Tvbbytes(const void *buf, int len) {
123 const byte *bp;
124 int i;
125
126 if (!len) { Tvba(" empty"); return; }
127 for (i=0, bp=buf; i<len; i++, bp++) {
128 if (!(i&31)) Tvba("\n ");
129 else if (!(i&3)) Tvba(" ");
130 Tvbf("%02x",*bp);
131 }
9ba9f156 132 Tvba(".");
4fb3d854 133}
134
135void Tvbfdset(int max, const fd_set *fds) {
136 int i;
137 const char *comma= "";
138
078c4fd0 139 Tvba("[");
4fb3d854 140 for (i=0; i<max; i++) {
141 if (!FD_ISSET(i,fds)) continue;
142 Tvba(comma);
143 Tvbf("%d",i);
144 comma= ",";
145 }
146 Tvba("]");
147}
148
2d5cce62 149static void Tvbpollevents(int events) {
150 const char *delim= "";
151
152 events &= (POLLIN|POLLOUT|POLLPRI);
153 if (!events) { Tvba("0"); return; }
154 if (events & POLLIN) { Tvba("POLLIN"); delim= "|"; }
155 if (events & POLLOUT) { Tvba(delim); Tvba("POLLOUT"); delim= "|"; }
156 if (events & POLLPRI) { Tvba(delim); Tvba("POLLPRI"); }
157}
158
159void Tvbpollfds(const struct pollfd *fds, int nfds) {
160 const char *comma= "";
161
162 Tvba("[");
163 while (nfds>0) {
164 Tvba(comma);
165 Tvbf("{fd=%d, events=",fds->fd);
166 Tvbpollevents(fds->events);
167 Tvba(", revents=");
168 Tvbpollevents(fds->revents);
169 Tvba("}");
170 comma= ", ";
171 nfds--; fds++;
172 }
173 Tvba("]");
174}
175
4fb3d854 176void Tvberrno(int e) {
177 const struct Terrno *te;
74cf211e 178
4fb3d854 179 for (te= Terrnos; te->n && te->v != e; te++);
180 if (te->n) Tvba(te->n);
181 else Tvbf("E#%d",e);
74cf211e 182}
183
4fb3d854 184void Tvba(const char *str) {
185 if (!adns__vbuf_appendstr(&vb,str)) Tnomem();
186}
74cf211e 187
4fb3d854 188void Tvbvf(const char *fmt, va_list al) {
189 char buf[1000];
190 buf[sizeof(buf)-2]= '\t';
191 vsnprintf(buf,sizeof(buf),fmt,al);
192 assert(buf[sizeof(buf)-2] == '\t');
74cf211e 193
4fb3d854 194 Tvba(buf);
195}
196
197void Tvbf(const char *fmt, ...) {
198 va_list al;
199 va_start(al,fmt);
200 Tvbvf(fmt,al);
201 va_end(al);
202}
203
204
078c4fd0 205void Tmust(const char *call, const char *arg, int cond) {
206 if (cond) return;
207 fprintf(stderr,"adns test harness: case not handled: system call %s, arg %s",call,arg);
208 exit(-1);
209}
210
4fb3d854 211void Tfailed(const char *why) {
212 fprintf(stderr,"adns test harness: failure: %s: %s\n",why,strerror(errno));
213 exit(-1);
214}
215
216void Tnomem(void) {
217 Tfailed("unable to malloc/realloc");
218}
219
220void Toutputerr(void) {
221 Tfailed("write error on test harness output");
222}