maintainer-clean
[adns] / regress / hcommon.c.m4
CommitLineData
bb7ba677 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
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)
a4f094dd 22
7d251914 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
a4f094dd 31#include "harness.h"
32#include "internal.h"
33
7d251914 34vbuf vb;
35FILE *Toutputfile= 0;
36
37const struct Terrno Terrnos[]= {
38 { "EAGAIN", EAGAIN },
39 { "EINPROGRESS", EINPROGRESS },
40 { "EINTR", EINTR },
41 { "EINVAL", EINVAL },
42 { "EMSGSIZE", EMSGSIZE },
43 { "ENOBUFS", ENOBUFS },
44 { "ENOENT", ENOENT },
45 { "ENOPROTOOPT", ENOPROTOOPT },
46 { "ENOSPC", ENOSPC },
47 { "EWOULDBLOCK", EWOULDBLOCK },
48 { 0, 0 }
49};
a4f094dd 50
70a778e5 51static vbuf vbw;
7d251914 52
70a778e5 53int Hwritev(int fd, const struct iovec *vector, size_t count) {
54 size_t i;
55
56 vbw.used= 0;
57 for (i=0; i<count; i++, vector++) {
58 if (!adns__vbuf_append(&vbw,vector->iov_base,vector->iov_len)) Tnomem();
59 }
60 return Hwrite(fd,vbw.buf,vbw.used);
61}
7d251914 62
bb7ba677 63m4_define(`hm_syscall', `
64 hm_create_proto_q
65void Q$1(hm_args_massage($3,void)) {
66
67 vb.used= 0;
68 Tvba("$1");
69 m4_define(`hm_na',`')
70 m4_define(`hm_arg_nullptr',`')
71 m4_define(`hm_arg_int', `Tvbf(" $'`1=%d",$'`1);')
72 m4_define(`hm_arg_fdset_io', `Tvbf(" $'`1="); Tvbfdset($'`2,$'`1);')
73 m4_define(`hm_arg_timeval_in_rel_null', `
74 if ($'`1) Tvbf(" $'`1=%ld.%06ld",(long)$'`1->tv_sec,(long)$'`1->tv_usec);
75 else Tvba(" $'`1=null");')
76 m4_define(`hm_arg_must', `')
77 m4_define(`hm_arg_socktype', `
78 Tvbf($'`1==SOCK_STREAM ? " $'`1=SOCK_STREAM" : " $'`1=SOCK_DGRAM");')
79 m4_define(`hm_arg_ign', `')
80 m4_define(`hm_arg_fd', `Tvbf(" $'`1=%d",$'`1);')
81 m4_define(`hm_arg_fcntl_cmd_arg', `
82 if ($'`1 == F_SETFL) {
83 Tvbf(" $'`1=F_SETFL %ld",arg);
84 } else {
85 Tvba(" $'`1=F_GETFL");
86 }')
70a778e5 87 m4_define(`hm_arg_addr_in', `Tvba(" $'`1="); Tvbaddr($'`1,$'`2);')
88 m4_define(`hm_arg_bytes_in', `')
bb7ba677 89 m4_define(`hm_arg_bytes_out', `Tvbf(" $'`4=%lu",(unsigned long)$'`4);')
90 m4_define(`hm_arg_addr_out', `Tvbf(" *$'`2=%d",$'`2);')
91 $3
7d251914 92
70a778e5 93 hm_create_nothing
94 m4_define(`hm_arg_bytes_in', `Tvbbytes($'`2,$'`4);')
95 $3
96
7d251914 97 Q_vb();
98}
bb7ba677 99')
7d251914 100
bb7ba677 101m4_include(`hsyscalls.i4')
7d251914 102
103
104void Tvbaddr(const struct sockaddr *addr, int len) {
105 const struct sockaddr_in *ai= (const struct sockaddr_in*)addr;
106
107 assert(len==sizeof(struct sockaddr_in));
108 assert(ai->sin_family==AF_INET);
bb7ba677 109 Tvbf("%s:%u",inet_ntoa(ai->sin_addr),htons(ai->sin_port));
7d251914 110}
111
112void Tvbbytes(const void *buf, int len) {
113 const byte *bp;
114 int i;
115
116 if (!len) { Tvba(" empty"); return; }
117 for (i=0, bp=buf; i<len; i++, bp++) {
118 if (!(i&31)) Tvba("\n ");
119 else if (!(i&3)) Tvba(" ");
120 Tvbf("%02x",*bp);
121 }
70a778e5 122 Tvba(".");
7d251914 123}
124
125void Tvbfdset(int max, const fd_set *fds) {
126 int i;
127 const char *comma= "";
128
bb7ba677 129 Tvba("[");
7d251914 130 for (i=0; i<max; i++) {
131 if (!FD_ISSET(i,fds)) continue;
132 Tvba(comma);
133 Tvbf("%d",i);
134 comma= ",";
135 }
136 Tvba("]");
137}
138
139void Tvberrno(int e) {
140 const struct Terrno *te;
a4f094dd 141
7d251914 142 for (te= Terrnos; te->n && te->v != e; te++);
143 if (te->n) Tvba(te->n);
144 else Tvbf("E#%d",e);
a4f094dd 145}
146
7d251914 147void Tvba(const char *str) {
148 if (!adns__vbuf_appendstr(&vb,str)) Tnomem();
149}
a4f094dd 150
7d251914 151void Tvbvf(const char *fmt, va_list al) {
152 char buf[1000];
153 buf[sizeof(buf)-2]= '\t';
154 vsnprintf(buf,sizeof(buf),fmt,al);
155 assert(buf[sizeof(buf)-2] == '\t');
a4f094dd 156
7d251914 157 Tvba(buf);
158}
159
160void Tvbf(const char *fmt, ...) {
161 va_list al;
162 va_start(al,fmt);
163 Tvbvf(fmt,al);
164 va_end(al);
165}
166
167
bb7ba677 168void Tmust(const char *call, const char *arg, int cond) {
169 if (cond) return;
170 fprintf(stderr,"adns test harness: case not handled: system call %s, arg %s",call,arg);
171 exit(-1);
172}
173
7d251914 174void Tfailed(const char *why) {
175 fprintf(stderr,"adns test harness: failure: %s: %s\n",why,strerror(errno));
176 exit(-1);
177}
178
179void Tnomem(void) {
180 Tfailed("unable to malloc/realloc");
181}
182
183void Toutputerr(void) {
184 Tfailed("write error on test harness output");
185}