bfc237d6a45bc5d106c5fb6b1ba2f48a1fadbaa3
[yaz-moved-to-github.git] / test / test_odr.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 #include <stdlib.h>
9 #include <stdio.h>
10 #include <yaz/oid_util.h>
11 #include "test_odrcodec.h"
12
13 #include <yaz/test.h>
14
15 #define MYOID  "1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19"
16
17 void tst_MySequence1(ODR encode, ODR decode)
18 {
19     int ret;
20     char *ber_buf;
21     int ber_len;
22     Yc_MySequence *s = (Yc_MySequence *) odr_malloc(encode, sizeof(*s));
23     Yc_MySequence *t;
24
25     YAZ_CHECK(s);
26     s->first = odr_intdup(encode, 12345);
27     s->second = (Odr_oct *) odr_malloc(encode, sizeof(*s->second));
28     s->second->buf = (unsigned char *) "hello";
29     s->second->len = 5;
30     s->second->size = 0;
31     s->third = odr_booldup(encode, 1);
32     s->fourth = odr_nullval();
33     s->fifth = odr_intdup(encode, YC_MySequence_enum1);
34
35     s->myoid = odr_getoidbystr(decode, MYOID);
36
37     ret = yc_MySequence(encode, &s, 0, 0);
38     YAZ_CHECK(ret);
39     if (!ret)
40         return;
41
42     ber_buf = odr_getbuf(encode, &ber_len, 0);
43
44     odr_setbuf(decode, ber_buf, ber_len, 0);
45
46     ret = yc_MySequence(decode, &t, 0, 0);
47     YAZ_CHECK(ret);
48     if (!ret)
49         return;
50
51     YAZ_CHECK(t);
52
53     YAZ_CHECK(t->first && *t->first == 12345);
54
55     YAZ_CHECK(t->second && t->second->buf && t->second->len == 5);
56
57     YAZ_CHECK(t->second && t->second->buf && t->second->len == 5 &&
58               memcmp(t->second->buf, "hello", t->second->len) == 0);
59
60     YAZ_CHECK(t->third && *t->third == 1);
61
62     YAZ_CHECK(t->fourth);
63
64     YAZ_CHECK(t->fifth && *t->fifth == YC_MySequence_enum1);
65
66     YAZ_CHECK(t->myoid);
67     if (t->myoid)
68     {
69         Odr_oid *myoid = odr_getoidbystr(decode, MYOID);
70
71         YAZ_CHECK(oid_oidcmp(myoid, t->myoid) == 0);
72     }
73 }
74
75 void tst_MySequence2(ODR encode, ODR decode)
76 {
77     int ret;
78     Yc_MySequence *s = (Yc_MySequence *) odr_malloc(encode, sizeof(*s));
79
80     YAZ_CHECK(s);
81     s->first = 0;  /* deliberately miss this .. */
82     s->second = (Odr_oct *) odr_malloc(encode, sizeof(*s->second));
83     s->second->buf = (unsigned char *) "hello";
84     s->second->len = 5;
85     s->second->size = 0;
86     s->third = odr_booldup(encode, 1);
87     s->fourth = odr_nullval();
88     s->fifth = odr_intdup(encode, YC_MySequence_enum1);
89     s->myoid = odr_getoidbystr(encode, MYOID);
90
91     ret = yc_MySequence(encode, &s, 0, 0); /* should fail */
92     YAZ_CHECK(!ret);
93
94     YAZ_CHECK(odr_geterror(encode) == OREQUIRED);
95
96     YAZ_CHECK(strcmp(odr_getelement(encode), "first") == 0);
97     odr_reset(encode);
98
99     YAZ_CHECK(odr_geterror(encode) == ONONE);
100
101     YAZ_CHECK(strcmp(odr_getelement(encode), "") == 0);
102 }
103
104 void tst_MySequence3(ODR encode, ODR decode)
105 {
106     char buf[40];
107     int i;
108     Yc_MySequence *t;
109
110     srand(123);
111     for (i = 0; i < 1000; i++)
112     {
113         int j;
114         for (j = 0; j < (int) sizeof(buf); j++)
115             buf[j] = rand();
116
117         for (j = 1; j < (int) sizeof(buf); j++)
118         {
119             odr_setbuf(decode, buf, j, 0);
120             yc_MySequence(decode, &t, 0, 0);
121             odr_reset(decode);
122         }
123     }
124 }
125
126 static void tst_berint32(ODR encode, ODR decode)
127 {
128     char *buf = 0;
129     int len = 0;
130     Odr_int val;
131     Odr_int ret_val;
132     int r;
133
134     val = 0;
135     odr_reset(encode);
136     r = ber_integer(encode, &val);
137     YAZ_CHECK_EQ(r, 1);
138     buf = odr_getbuf(encode, &len, 0);
139     YAZ_CHECK(buf);
140     YAZ_CHECK_EQ(len, 2);
141     YAZ_CHECK_EQ(buf[0], 1);
142     YAZ_CHECK_EQ(buf[1], 0);
143
144     odr_reset(decode);
145     odr_setbuf(decode, buf, len, 0);
146     ber_integer(decode, &ret_val);
147     YAZ_CHECK_EQ(ret_val, 0);
148
149     val = 1;
150     odr_reset(encode);
151     r = ber_integer(encode, &val);
152     YAZ_CHECK_EQ(r, 1);
153     buf = odr_getbuf(encode, &len, 0);
154     YAZ_CHECK(buf);
155     YAZ_CHECK_EQ(len, 2);
156     YAZ_CHECK_EQ(buf[0], 1);
157     YAZ_CHECK_EQ(buf[1], 1);
158
159     odr_reset(decode);
160     odr_setbuf(decode, buf, len, 0);
161     ber_integer(decode, &ret_val);
162     YAZ_CHECK_EQ(ret_val, 1);
163
164     val = -1;
165     odr_reset(encode);
166     r = ber_integer(encode, &val);
167     YAZ_CHECK_EQ(r, 1);
168     buf = odr_getbuf(encode, &len, 0);
169     YAZ_CHECK(buf);
170     YAZ_CHECK_EQ(len, 2);
171     YAZ_CHECK_EQ(buf[0], 1);
172     YAZ_CHECK_EQ((unsigned char) buf[1], 255);
173
174     odr_reset(decode);
175     odr_setbuf(decode, buf, len, 0);
176     ber_integer(decode, &ret_val);
177     YAZ_CHECK_EQ(ret_val, -1);
178
179     val = 127;
180     odr_reset(encode);
181     r = ber_integer(encode, &val);
182     YAZ_CHECK_EQ(r, 1);
183     buf = odr_getbuf(encode, &len, 0);
184     YAZ_CHECK(buf);
185     YAZ_CHECK_EQ(len, 2);
186     YAZ_CHECK_EQ(buf[0], 1);
187     YAZ_CHECK_EQ(buf[1], 127);
188
189     odr_reset(decode);
190     odr_setbuf(decode, buf, len, 0);
191     ber_integer(decode, &ret_val);
192     YAZ_CHECK_EQ(ret_val, 127);
193
194
195     val = 128;
196     odr_reset(encode);
197     r = ber_integer(encode, &val);
198     YAZ_CHECK_EQ(r, 1);
199     buf = odr_getbuf(encode, &len, 0);
200     YAZ_CHECK(buf);
201     YAZ_CHECK_EQ(len, 3);
202     YAZ_CHECK_EQ(buf[0], 2);
203     YAZ_CHECK_EQ(buf[1], 0);
204     YAZ_CHECK_EQ(((unsigned char *) buf)[2], 128);
205
206     odr_reset(decode);
207     odr_setbuf(decode, buf, len, 0);
208     ber_integer(decode, &ret_val);
209     YAZ_CHECK_EQ(ret_val, 128);
210
211     val = 2147483647; /* 2^31-1 */
212     odr_reset(encode);
213     r = ber_integer(encode, &val);
214     YAZ_CHECK_EQ(r, 1);
215     buf = odr_getbuf(encode, &len, 0);
216     YAZ_CHECK(buf);
217     YAZ_CHECK_EQ(len, 5);
218     YAZ_CHECK_EQ(buf[0], 4);
219     YAZ_CHECK_EQ(buf[1], 127);
220     YAZ_CHECK_EQ((unsigned char) buf[2], 255);
221     YAZ_CHECK_EQ((unsigned char) buf[3], 255);
222     YAZ_CHECK_EQ((unsigned char) buf[4], 255);
223
224     odr_reset(decode);
225     odr_setbuf(decode, buf, len, 0);
226     ber_integer(decode, &ret_val);
227     YAZ_CHECK_EQ(ret_val, 2147483647);
228
229     val = -2147483647L -1; /* -2^31 */
230     odr_reset(encode);
231     r = ber_integer(encode, &val);
232     YAZ_CHECK_EQ(r, 1);
233     buf = odr_getbuf(encode, &len, 0);
234     YAZ_CHECK(buf);
235     YAZ_CHECK_EQ(len, 5);
236     YAZ_CHECK_EQ(buf[0], 4);
237     YAZ_CHECK_EQ((unsigned char) buf[1], 128);
238     YAZ_CHECK_EQ(buf[2], 0);
239     YAZ_CHECK_EQ(buf[3], 0);
240     YAZ_CHECK_EQ(buf[4], 0);
241
242     odr_reset(decode);
243     odr_setbuf(decode, buf, len, 0);
244     ber_integer(decode, &ret_val);
245     YAZ_CHECK_EQ(ret_val, (Odr_int) -2147483647L -1);
246 }
247
248 static void tst_berint64(ODR encode, ODR decode)
249 {
250 #if NMEM_64
251     char *buf = 0;
252     int len = 0;
253     Odr_int val;
254     Odr_int ret_val;
255     int r;
256
257     val = (Odr_int) 2 * 2147483648UL; /* 2^32 */
258     odr_reset(encode);
259     r = ber_integer(encode, &val);
260     YAZ_CHECK_EQ(r, 1);
261     buf = odr_getbuf(encode, &len, 0);
262     YAZ_CHECK(buf);
263     YAZ_CHECK_EQ(len, 6);
264     YAZ_CHECK_EQ(buf[0], 5);
265     YAZ_CHECK_EQ(buf[1], 1);
266     YAZ_CHECK_EQ(buf[2], 0);
267     YAZ_CHECK_EQ(buf[3], 0);
268     YAZ_CHECK_EQ(buf[4], 0);
269     YAZ_CHECK_EQ(buf[5], 0);
270
271     odr_reset(decode);
272     odr_setbuf(decode, buf, len, 0);
273     ber_integer(decode, &ret_val);
274     YAZ_CHECK_EQ(ret_val, val);
275
276     val = (Odr_int) -2 * 2147483648UL; /* -2^32 */
277     odr_reset(encode);
278     r = ber_integer(encode, &val);
279     YAZ_CHECK_EQ(r, 1);
280     buf = odr_getbuf(encode, &len, 0);
281     YAZ_CHECK(buf);
282     YAZ_CHECK_EQ(len, 6);
283     YAZ_CHECK_EQ(buf[0], 5);
284     YAZ_CHECK_EQ((unsigned char) buf[1], 255);
285     YAZ_CHECK_EQ(buf[2], 0);
286     YAZ_CHECK_EQ(buf[3], 0);
287     YAZ_CHECK_EQ(buf[4], 0);
288     YAZ_CHECK_EQ(buf[5], 0);
289
290     odr_reset(decode);
291     odr_setbuf(decode, buf, len, 0);
292     ber_integer(decode, &ret_val);
293     YAZ_CHECK_EQ(ret_val, val);
294
295     val = (Odr_int) 1000 * 1000000000L; /* 10^12 */
296     odr_reset(encode);
297     r = ber_integer(encode, &val);
298     YAZ_CHECK_EQ(r, 1);
299     buf = odr_getbuf(encode, &len, 0);
300     YAZ_CHECK(buf);
301     YAZ_CHECK_EQ(len, 7);
302     YAZ_CHECK_EQ(buf[0], 6);
303     YAZ_CHECK_EQ(buf[1], 0);
304     YAZ_CHECK_EQ((unsigned char) buf[2], 232);
305     YAZ_CHECK_EQ((unsigned char) buf[3], 212);
306     YAZ_CHECK_EQ((unsigned char) buf[4], 165);
307     YAZ_CHECK_EQ(buf[5], 16);
308     YAZ_CHECK_EQ(buf[6], 0);
309
310     odr_reset(decode);
311     odr_setbuf(decode, buf, len, 0);
312     ber_integer(decode, &ret_val);
313     YAZ_CHECK_EQ(ret_val, val);
314 #endif
315 }
316
317 static void tst(void)
318 {
319     ODR odr_encode = odr_createmem(ODR_ENCODE);
320     ODR odr_decode = odr_createmem(ODR_DECODE);
321
322     YAZ_CHECK(odr_encode);
323     YAZ_CHECK(odr_decode);
324
325     tst_MySequence1(odr_encode, odr_decode);
326     tst_MySequence2(odr_encode, odr_decode);
327     tst_MySequence3(odr_encode, odr_decode);
328
329     tst_berint32(odr_encode, odr_decode);
330     tst_berint64(odr_encode, odr_decode);
331
332     odr_destroy(odr_encode);
333     odr_destroy(odr_decode);
334 }
335
336 /* example from documentation.. 'Using Odr' */
337 void do_nothing_useful(Odr_int value)
338 {
339     ODR encode, decode;
340     Odr_int *valp, *resvalp;
341     char *bufferp;
342     int len;
343
344     /* allocate streams */
345     if (!(encode = odr_createmem(ODR_ENCODE)))
346         return;
347     if (!(decode = odr_createmem(ODR_DECODE)))
348         return;
349
350     valp = &value;
351     if (odr_integer(encode, &valp, 0, 0) == 0)
352     {
353         printf("encoding went bad\n");
354         return;
355     }
356     bufferp = odr_getbuf(encode, &len, 0);
357     printf("length of encoded data is %d\n", len);
358
359     /* now let's decode the thing again */
360     odr_setbuf(decode, bufferp, len, 0);
361     if (odr_integer(decode, &resvalp, 0, 0) == 0)
362     {
363         printf("decoding went bad\n");
364         return;
365     }
366     /* ODR_INT_PRINTF format for printf (such as %d) */
367     printf("the value is " ODR_INT_PRINTF "\n", *resvalp);
368
369     /* clean up */
370     odr_destroy(encode);
371     odr_destroy(decode);
372 }
373
374 int main(int argc, char **argv)
375 {
376     YAZ_CHECK_INIT(argc, argv);
377     tst();
378     YAZ_CHECK_TERM;
379 }
380
381 /*
382  * Local variables:
383  * c-basic-offset: 4
384  * c-file-style: "Stroustrup"
385  * indent-tabs-mode: nil
386  * End:
387  * vim: shiftwidth=4 tabstop=8 expandtab
388  */
389