Merge branch 'master' of ssh://git.indexdata.com/home/git/pub/yaz
[yaz-moved-to-github.git] / client / bertorture.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 Index Data
3  * See the file LICENSE for details.
4  */
5
6 #include <signal.h>
7 #if HAVE_SYS_TYPES_H
8 #include <sys/types.h>
9 #endif
10 #if HAVE_SYS_STAT_H
11 #include <sys/stat.h>
12 #endif
13 #include <fcntl.h>
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17
18 #include <stdlib.h>
19 #include <stdio.h>
20
21 #include <yaz/yaz-util.h>
22 #include <yaz/proto.h>
23 #include <yaz/comstack.h>
24
25 #define PACKET_SIZE 64
26
27 static int stop = 0;
28
29 /*
30 static int send_packet(const char *host)
31 {
32     char buf[PACKET_SIZE];
33     int i;
34
35     void *add;
36
37     COMSTACK cs = cs_create_host(host, 1, &add);
38
39     if (!cs)
40         return -1;
41
42     if (cs_connect(cs, add) < 0)
43         return -1;
44
45     for (i = 0; i<sizeof(buf); i++)
46         buf[i] = 233;
47 #if 0
48         buf[i] = rand() & 0xff;
49 #endif
50     cs_put(cs, buf, sizeof(buf));
51
52     cs_close(cs);
53     return 0;
54 }
55 */
56
57 /*
58 static void test_file(const char *fname)
59 {
60     Z_GDU *req;
61     ODR odr = odr_createmem(ODR_DECODE);
62     char buf[PACKET_SIZE];
63     int off = 0;
64     int fd =open(fname, O_RDONLY, 0666);
65     if (fd == -1)
66     {
67         yaz_log(YLOG_ERRNO|YLOG_FATAL, "open %s", fname);
68         exit (1);
69     }
70     while (off < sizeof(buf))
71     {
72         ssize_t rd;
73         rd = read(fd, buf+off, sizeof(buf)-off);
74         if (rd == -1) {
75             yaz_log(YLOG_ERRNO|YLOG_FATAL, "read %s", fname);
76             exit (1);
77         }
78         if (rd == 0)
79             break;
80         off += rd;
81     }
82     if (close(fd) == -1)
83     {
84         yaz_log(YLOG_ERRNO|YLOG_FATAL, "close %s", fname);
85         exit (1);
86     }
87     odr_setbuf(odr, buf, off, 0);
88     z_GDU(odr, &req, 0, 0);
89
90     odr_destroy(odr);
91 }
92 */
93
94 static void test_random(int run, const char *fname, const char *fname2,
95                         int *estat)
96 {
97     FILE *dumpfile = 0;
98     char buf[PACKET_SIZE];
99     int i, j;
100
101     if (fname2)
102     {
103         if (!strcmp(fname2, "-"))
104             dumpfile = stdout;
105         else
106             dumpfile = fopen(fname2, "w");
107     }
108
109     for (i = 0; i<sizeof(buf); i++)
110         buf[i] = rand() & 0xff;
111
112     for (j = 0; j<sizeof(buf)-1; j++)
113     {
114         Z_GDU *req;
115         char *mbuf;
116         ODR odr;
117
118         odr = odr_createmem(ODR_DECODE);
119         if (fname)
120         {
121             int off = 0;
122             int fd =open(fname, O_TRUNC|O_CREAT|O_WRONLY, 0666);
123             if (fd == -1)
124             {
125                 yaz_log(YLOG_ERRNO|YLOG_FATAL, "open %s", fname);
126                 exit (1);
127             }
128             while (sizeof(buf)-j-off > 0)
129             {
130                 ssize_t wrote;
131                 wrote = write(fd, buf+off+j, sizeof(buf)-j-off);
132                 if (wrote <= 0) {
133                     yaz_log(YLOG_ERRNO|YLOG_FATAL, "write %s", fname);
134                     exit (1);
135                 }
136                 off += wrote;
137             }
138             if (close(fd) == -1)
139             {
140                 yaz_log(YLOG_ERRNO|YLOG_FATAL, "close %s", fname);
141                 exit (1);
142             }
143         }
144         mbuf = malloc(sizeof(buf)-j);
145         memcpy(mbuf, buf+j, sizeof(buf)-j);
146         odr_setbuf(odr, mbuf, sizeof(buf)-j, 0);
147         if (z_GDU(odr, &req, 0, 0))
148             estat[99]++;
149         else
150         {
151             int ex;
152             odr_geterrorx(odr, &ex);
153             estat[ex]++;
154         }
155         if (dumpfile)
156             odr_dumpBER(dumpfile, buf+j, sizeof(buf)-j);
157         free(mbuf);
158         odr_reset(odr);
159         odr_destroy(odr);
160     }
161     if (dumpfile && dumpfile != stdout)
162         fclose(dumpfile);
163 }
164
165 void sigint_handler(int x)
166 {
167     stop = 1;
168 }
169
170 int main(int argc, char **argv)
171 {
172     int start = 0, end = 10000000, ret, i, estat[100];
173     char *arg;
174     char *ber_fname = 0;
175     char *packet_fname = 0;
176
177     signal(SIGINT, sigint_handler);
178     signal(SIGTERM, sigint_handler);
179     for (i = 0; i<sizeof(estat)/sizeof(*estat); i++)
180         estat[i] = 0;
181
182     while ((ret = options("s:e:b:p:", argv, argc, &arg)) != -2)
183     {
184         switch (ret)
185         {
186         case 's':
187             start = atoi(arg);
188             break;
189         case 'e':
190             end = atoi(arg);
191             break;
192         case 'b':
193             ber_fname = arg;
194             break;
195         case 'p':
196             packet_fname = arg;
197             break;
198         case 0:
199             if (!strcmp(arg, "random"))
200             {
201                 i = start;
202                 while(!stop && (end == 0 || i < end))
203                 {
204                     srand(i*5111+1);
205                     if ((i % 50) == 0)
206                         printf ("\r[%d]", i); fflush(stdout);
207                     test_random(i, packet_fname, ber_fname, estat);
208                     i++;
209                 }
210             }
211             break;
212         default:
213             fprintf (stderr, "usage\n");
214             fprintf (stderr, " [-s start] [-e end] [-b berdump] [-p packetdump] random\n");
215             exit(1);
216         }
217     }
218     printf ("\n");
219     for (i = 0; i < sizeof(estat)/sizeof(*estat); i++)
220         if (estat[i])
221             printf ("%3d %9d\n", i, estat[i]);
222     exit(0);
223 }
224 /*
225  * Local variables:
226  * c-basic-offset: 4
227  * c-file-style: "Stroustrup"
228  * indent-tabs-mode: nil
229  * End:
230  * vim: shiftwidth=4 tabstop=8 expandtab
231  */
232