bb7ba677 |
1 | m4_dnl hcommon.c |
2 | m4_dnl (part of complex test harness, not of the library) |
3 | m4_dnl - routines used for both record and playback |
4 | |
d942707d |
5 | m4_dnl This file is |
6 | m4_dnl Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk> |
7 | m4_dnl |
8 | m4_dnl It is part of adns, which is |
9 | m4_dnl Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk> |
10 | m4_dnl Copyright (C) 1999 Tony Finch <dot@dotat.at> |
bb7ba677 |
11 | m4_dnl |
12 | m4_dnl This program is free software; you can redistribute it and/or modify |
13 | m4_dnl it under the terms of the GNU General Public License as published by |
14 | m4_dnl the Free Software Foundation; either version 2, or (at your option) |
15 | m4_dnl any later version. |
16 | m4_dnl |
17 | m4_dnl This program is distributed in the hope that it will be useful, |
18 | m4_dnl but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | m4_dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 | m4_dnl GNU General Public License for more details. |
21 | m4_dnl |
22 | m4_dnl You should have received a copy of the GNU General Public License |
23 | m4_dnl along with this program; if not, write to the Free Software Foundation, |
24 | m4_dnl Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | |
26 | m4_include(hmacros.i4) |
a4f094dd |
27 | |
7d251914 |
28 | #include <fcntl.h> |
29 | #include <string.h> |
30 | #include <errno.h> |
31 | |
32 | #include <sys/socket.h> |
33 | #include <netinet/in.h> |
34 | #include <arpa/inet.h> |
35 | |
a4f094dd |
36 | #include "harness.h" |
37 | #include "internal.h" |
38 | |
7d251914 |
39 | vbuf vb; |
40 | FILE *Toutputfile= 0; |
98b6d5b4 |
41 | struct timeval currenttime; |
7d251914 |
42 | |
43 | const struct Terrno Terrnos[]= { |
44 | { "EAGAIN", EAGAIN }, |
45 | { "EINPROGRESS", EINPROGRESS }, |
46 | { "EINTR", EINTR }, |
47 | { "EINVAL", EINVAL }, |
48 | { "EMSGSIZE", EMSGSIZE }, |
49 | { "ENOBUFS", ENOBUFS }, |
50 | { "ENOENT", ENOENT }, |
51 | { "ENOPROTOOPT", ENOPROTOOPT }, |
52 | { "ENOSPC", ENOSPC }, |
53 | { "EWOULDBLOCK", EWOULDBLOCK }, |
a00dbcb2 |
54 | { "EHOSTUNREACH", EHOSTUNREACH }, |
55 | { "ECONNREFUSED", ECONNREFUSED }, |
56 | { "EPIPE", EPIPE }, |
7d251914 |
57 | { 0, 0 } |
58 | }; |
a4f094dd |
59 | |
70a778e5 |
60 | static vbuf vbw; |
7d251914 |
61 | |
98b6d5b4 |
62 | int Hgettimeofday(struct timeval *tv, struct timezone *tz) { |
63 | Tmust("gettimeofday","tz",!tz); |
64 | *tv= currenttime; |
65 | return 0; |
66 | } |
67 | |
70a778e5 |
68 | int Hwritev(int fd, const struct iovec *vector, size_t count) { |
69 | size_t i; |
70 | |
71 | vbw.used= 0; |
72 | for (i=0; i<count; i++, vector++) { |
73 | if (!adns__vbuf_append(&vbw,vector->iov_base,vector->iov_len)) Tnomem(); |
74 | } |
75 | return Hwrite(fd,vbw.buf,vbw.used); |
76 | } |
7d251914 |
77 | |
bb7ba677 |
78 | m4_define(`hm_syscall', ` |
79 | hm_create_proto_q |
80 | void Q$1(hm_args_massage($3,void)) { |
81 | |
82 | vb.used= 0; |
83 | Tvba("$1"); |
84 | m4_define(`hm_na',`') |
85 | m4_define(`hm_arg_nullptr',`') |
86 | m4_define(`hm_arg_int', `Tvbf(" $'`1=%d",$'`1);') |
87 | m4_define(`hm_arg_fdset_io', `Tvbf(" $'`1="); Tvbfdset($'`2,$'`1);') |
8b1171e6 |
88 | m4_define(`hm_arg_pollfds_io', `Tvbf(" $'`1="); Tvbpollfds($'`1,$'`2);') |
bb7ba677 |
89 | m4_define(`hm_arg_timeval_in_rel_null', ` |
90 | if ($'`1) Tvbf(" $'`1=%ld.%06ld",(long)$'`1->tv_sec,(long)$'`1->tv_usec); |
91 | else Tvba(" $'`1=null");') |
92 | m4_define(`hm_arg_must', `') |
93 | m4_define(`hm_arg_socktype', ` |
94 | Tvbf($'`1==SOCK_STREAM ? " $'`1=SOCK_STREAM" : " $'`1=SOCK_DGRAM");') |
95 | m4_define(`hm_arg_ign', `') |
96 | m4_define(`hm_arg_fd', `Tvbf(" $'`1=%d",$'`1);') |
97 | m4_define(`hm_arg_fcntl_cmd_arg', ` |
98 | if ($'`1 == F_SETFL) { |
73c4c523 |
99 | Tvbf(" $'`1=F_SETFL %s",arg & O_NONBLOCK ? "O_NONBLOCK|..." : "~O_NONBLOCK&..."); |
100 | } else if ($'`1 == F_GETFL) { |
bb7ba677 |
101 | Tvba(" $'`1=F_GETFL"); |
73c4c523 |
102 | } else { |
103 | Tmust("$'`1","F_GETFL/F_SETFL",0); |
bb7ba677 |
104 | }') |
70a778e5 |
105 | m4_define(`hm_arg_addr_in', `Tvba(" $'`1="); Tvbaddr($'`1,$'`2);') |
106 | m4_define(`hm_arg_bytes_in', `') |
bb7ba677 |
107 | m4_define(`hm_arg_bytes_out', `Tvbf(" $'`4=%lu",(unsigned long)$'`4);') |
108 | m4_define(`hm_arg_addr_out', `Tvbf(" *$'`2=%d",$'`2);') |
109 | $3 |
7d251914 |
110 | |
70a778e5 |
111 | hm_create_nothing |
112 | m4_define(`hm_arg_bytes_in', `Tvbbytes($'`2,$'`4);') |
113 | $3 |
114 | |
7d251914 |
115 | Q_vb(); |
116 | } |
bb7ba677 |
117 | ') |
7d251914 |
118 | |
bb7ba677 |
119 | m4_include(`hsyscalls.i4') |
7d251914 |
120 | |
121 | |
122 | void Tvbaddr(const struct sockaddr *addr, int len) { |
123 | const struct sockaddr_in *ai= (const struct sockaddr_in*)addr; |
124 | |
125 | assert(len==sizeof(struct sockaddr_in)); |
126 | assert(ai->sin_family==AF_INET); |
bb7ba677 |
127 | Tvbf("%s:%u",inet_ntoa(ai->sin_addr),htons(ai->sin_port)); |
7d251914 |
128 | } |
129 | |
130 | void Tvbbytes(const void *buf, int len) { |
131 | const byte *bp; |
132 | int i; |
133 | |
134 | if (!len) { Tvba(" empty"); return; } |
135 | for (i=0, bp=buf; i<len; i++, bp++) { |
136 | if (!(i&31)) Tvba("\n "); |
137 | else if (!(i&3)) Tvba(" "); |
138 | Tvbf("%02x",*bp); |
139 | } |
70a778e5 |
140 | Tvba("."); |
7d251914 |
141 | } |
142 | |
143 | void Tvbfdset(int max, const fd_set *fds) { |
144 | int i; |
145 | const char *comma= ""; |
146 | |
bb7ba677 |
147 | Tvba("["); |
7d251914 |
148 | for (i=0; i<max; i++) { |
149 | if (!FD_ISSET(i,fds)) continue; |
150 | Tvba(comma); |
151 | Tvbf("%d",i); |
152 | comma= ","; |
153 | } |
154 | Tvba("]"); |
155 | } |
156 | |
8b1171e6 |
157 | static void Tvbpollevents(int events) { |
158 | const char *delim= ""; |
159 | |
160 | events &= (POLLIN|POLLOUT|POLLPRI); |
161 | if (!events) { Tvba("0"); return; } |
162 | if (events & POLLIN) { Tvba("POLLIN"); delim= "|"; } |
163 | if (events & POLLOUT) { Tvba(delim); Tvba("POLLOUT"); delim= "|"; } |
164 | if (events & POLLPRI) { Tvba(delim); Tvba("POLLPRI"); } |
165 | } |
166 | |
167 | void Tvbpollfds(const struct pollfd *fds, int nfds) { |
168 | const char *comma= ""; |
169 | |
170 | Tvba("["); |
171 | while (nfds>0) { |
172 | Tvba(comma); |
173 | Tvbf("{fd=%d, events=",fds->fd); |
174 | Tvbpollevents(fds->events); |
175 | Tvba(", revents="); |
176 | Tvbpollevents(fds->revents); |
177 | Tvba("}"); |
178 | comma= ", "; |
179 | nfds--; fds++; |
180 | } |
181 | Tvba("]"); |
182 | } |
183 | |
7d251914 |
184 | void Tvberrno(int e) { |
185 | const struct Terrno *te; |
a4f094dd |
186 | |
7d251914 |
187 | for (te= Terrnos; te->n && te->v != e; te++); |
43bb811f |
188 | assert(te->n); |
189 | Tvba(te->n); |
a4f094dd |
190 | } |
191 | |
7d251914 |
192 | void Tvba(const char *str) { |
193 | if (!adns__vbuf_appendstr(&vb,str)) Tnomem(); |
194 | } |
a4f094dd |
195 | |
7d251914 |
196 | void Tvbvf(const char *fmt, va_list al) { |
197 | char buf[1000]; |
198 | buf[sizeof(buf)-2]= '\t'; |
199 | vsnprintf(buf,sizeof(buf),fmt,al); |
200 | assert(buf[sizeof(buf)-2] == '\t'); |
a4f094dd |
201 | |
7d251914 |
202 | Tvba(buf); |
203 | } |
204 | |
205 | void Tvbf(const char *fmt, ...) { |
206 | va_list al; |
207 | va_start(al,fmt); |
208 | Tvbvf(fmt,al); |
209 | va_end(al); |
210 | } |
211 | |
212 | |
bb7ba677 |
213 | void Tmust(const char *call, const char *arg, int cond) { |
214 | if (cond) return; |
215 | fprintf(stderr,"adns test harness: case not handled: system call %s, arg %s",call,arg); |
216 | exit(-1); |
217 | } |
218 | |
7d251914 |
219 | void Tfailed(const char *why) { |
220 | fprintf(stderr,"adns test harness: failure: %s: %s\n",why,strerror(errno)); |
221 | exit(-1); |
222 | } |
223 | |
224 | void Tnomem(void) { |
225 | Tfailed("unable to malloc/realloc"); |
226 | } |
227 | |
228 | void Toutputerr(void) { |
229 | Tfailed("write error on test harness output"); |
230 | } |