2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: bertorture.c,v 1.4 2005-06-25 15:46:01 adam Exp $
10 #include <sys/types.h>
23 #include <yaz/yaz-util.h>
24 #include <yaz/proto.h>
25 #include <yaz/comstack.h>
27 #define PACKET_SIZE 64
31 static int send_packet(const char *host)
33 char buf[PACKET_SIZE];
38 COMSTACK cs = cs_create_host(host, 1, &add);
43 if (cs_connect(cs, add) < 0)
46 for (i = 0; i<sizeof(buf); i++)
49 buf[i] = rand() & 0xff;
51 cs_put(cs, buf, sizeof(buf));
57 static void test_file(const char *fname)
60 ODR odr = odr_createmem(ODR_DECODE);
61 char buf[PACKET_SIZE];
63 int fd =open(fname, O_RDONLY, 0666);
66 yaz_log(LOG_ERRNO|LOG_FATAL, "open %s", fname);
69 while (off < sizeof(buf))
72 rd = read(fd, buf+off, sizeof(buf)-off);
74 yaz_log(LOG_ERRNO|LOG_FATAL, "read %s", fname);
83 yaz_log(LOG_ERRNO|LOG_FATAL, "close %s", fname);
86 odr_setbuf(odr, buf, off, 0);
87 z_GDU(odr, &req, 0, 0);
92 static void test_random(int run, const char *fname, const char *fname2,
96 char buf[PACKET_SIZE];
101 if (!strcmp(fname2, "-"))
104 dumpfile = fopen(fname2, "w");
107 for (i = 0; i<sizeof(buf); i++)
108 buf[i] = rand() & 0xff;
110 for (j = 0; j<sizeof(buf)-1; j++)
117 odr = odr_createmem(ODR_DECODE);
121 int fd =open(fname, O_TRUNC|O_CREAT|O_WRONLY, 0666);
124 yaz_log(LOG_ERRNO|LOG_FATAL, "open %s", fname);
127 while (sizeof(buf)-j-off > 0)
130 wrote = write(fd, buf+off+j, sizeof(buf)-j-off);
132 yaz_log(LOG_ERRNO|LOG_FATAL, "write %s", fname);
139 yaz_log(LOG_ERRNO|LOG_FATAL, "close %s", fname);
143 mbuf = malloc(sizeof(buf)-j);
144 memcpy(mbuf, buf+j, sizeof(buf)-j);
145 odr_setbuf(odr, mbuf, sizeof(buf)-j, 0);
146 if (z_GDU(odr, &req, 0, 0))
151 odr_geterrorx(odr, &ex);
155 odr_dumpBER(dumpfile, buf+j, sizeof(buf)-j);
161 if (dumpfile && dumpfile != stdout)
165 void sigint_handler(int x)
170 int main(int argc, char **argv)
172 int start = 0, end = 10000000, ret, i, estat[100];
175 char *packet_fname = 0;
177 signal(SIGINT, sigint_handler);
178 signal(SIGTERM, sigint_handler);
179 for (i = 0; i<sizeof(estat)/sizeof(*estat); i++)
182 while ((ret = options("s:e:b:p:", argv, argc, &arg)) != -2)
199 if (!strcmp(arg, "random"))
202 while(!stop && (end == 0 || i < end))
206 printf ("\r[%d]", i); fflush(stdout);
207 test_random(i, packet_fname, ber_fname, estat);
213 fprintf (stderr, "usage\n");
214 fprintf (stderr, " [-s start] [-e end] [-b berdump] [-p packetdump] random\n");
219 for (i = 0; i < sizeof(estat)/sizeof(*estat); i++)
221 printf ("%3d %9d\n", i, estat[i]);
227 * indent-tabs-mode: nil
229 * vim: shiftwidth=4 tabstop=8 expandtab