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