1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
10 #include <yaz/oid_util.h>
11 #include "test_odrcodec.h"
15 #define MYOID "1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19"
17 void tst_MySequence1(ODR encode, ODR decode)
22 Yc_MySequence *s = (Yc_MySequence *) odr_malloc(encode, sizeof(*s));
26 s->first = odr_intdup(encode, 12345);
27 s->second = (Odr_oct *) odr_malloc(encode, sizeof(*s->second));
28 s->second->buf = (char *) "hello";
33 s->third = odr_booldup(encode, 1);
34 s->fourth = odr_nullval();
35 s->fifth = odr_intdup(encode, YC_MySequence_enum1);
37 s->myoid = odr_getoidbystr(decode, MYOID);
39 ret = yc_MySequence(encode, &s, 0, 0);
44 ber_buf = odr_getbuf(encode, &ber_len, 0);
46 odr_setbuf(decode, ber_buf, ber_len, 0);
48 ret = yc_MySequence(decode, &t, 0, 0);
55 YAZ_CHECK(t->first && *t->first == 12345);
57 YAZ_CHECK(t->second && t->second->buf && t->second->len == 5);
59 YAZ_CHECK(t->second && t->second->buf && t->second->len == 5 &&
60 memcmp(t->second->buf, "hello", t->second->len) == 0);
62 YAZ_CHECK(t->third && *t->third == 1);
66 YAZ_CHECK(t->fifth && *t->fifth == YC_MySequence_enum1);
71 Odr_oid *myoid = odr_getoidbystr(decode, MYOID);
73 YAZ_CHECK(oid_oidcmp(myoid, t->myoid) == 0);
77 void tst_MySequence2(ODR encode, ODR decode)
80 Yc_MySequence *s = (Yc_MySequence *) odr_malloc(encode, sizeof(*s));
83 s->first = 0; /* deliberately miss this .. */
84 s->second = (Odr_oct *) odr_malloc(encode, sizeof(*s->second));
85 s->second->buf = (char *) "hello";
90 s->third = odr_booldup(encode, 1);
91 s->fourth = odr_nullval();
92 s->fifth = odr_intdup(encode, YC_MySequence_enum1);
93 s->myoid = odr_getoidbystr(encode, MYOID);
95 ret = yc_MySequence(encode, &s, 0, 0); /* should fail */
98 YAZ_CHECK(odr_geterror(encode) == OREQUIRED);
100 YAZ_CHECK(strcmp(odr_getelement(encode), "first") == 0);
103 YAZ_CHECK(odr_geterror(encode) == ONONE);
105 YAZ_CHECK(strcmp(odr_getelement(encode), "") == 0);
108 void tst_MySequence3(ODR encode, ODR decode)
115 for (i = 0; i < 1000; i++)
118 for (j = 0; j < (int) sizeof(buf); j++)
121 for (j = 1; j < (int) sizeof(buf); j++)
123 odr_setbuf(decode, buf, j, 0);
124 yc_MySequence(decode, &t, 0, 0);
130 static void tst_berint32(ODR encode, ODR decode)
140 r = ber_integer(encode, &val);
142 buf = odr_getbuf(encode, &len, 0);
144 YAZ_CHECK_EQ(len, 2);
145 YAZ_CHECK_EQ(buf[0], 1);
146 YAZ_CHECK_EQ(buf[1], 0);
149 odr_setbuf(decode, buf, len, 0);
150 ber_integer(decode, &ret_val);
151 YAZ_CHECK_EQ(ret_val, 0);
155 r = ber_integer(encode, &val);
157 buf = odr_getbuf(encode, &len, 0);
159 YAZ_CHECK_EQ(len, 2);
160 YAZ_CHECK_EQ(buf[0], 1);
161 YAZ_CHECK_EQ(buf[1], 1);
164 odr_setbuf(decode, buf, len, 0);
165 ber_integer(decode, &ret_val);
166 YAZ_CHECK_EQ(ret_val, 1);
170 r = ber_integer(encode, &val);
172 buf = odr_getbuf(encode, &len, 0);
174 YAZ_CHECK_EQ(len, 2);
175 YAZ_CHECK_EQ(buf[0], 1);
176 YAZ_CHECK_EQ((unsigned char) buf[1], 255);
179 odr_setbuf(decode, buf, len, 0);
180 ber_integer(decode, &ret_val);
181 YAZ_CHECK_EQ(ret_val, -1);
185 r = ber_integer(encode, &val);
187 buf = odr_getbuf(encode, &len, 0);
189 YAZ_CHECK_EQ(len, 2);
190 YAZ_CHECK_EQ(buf[0], 1);
191 YAZ_CHECK_EQ(buf[1], 127);
194 odr_setbuf(decode, buf, len, 0);
195 ber_integer(decode, &ret_val);
196 YAZ_CHECK_EQ(ret_val, 127);
201 r = ber_integer(encode, &val);
203 buf = odr_getbuf(encode, &len, 0);
205 YAZ_CHECK_EQ(len, 3);
206 YAZ_CHECK_EQ(buf[0], 2);
207 YAZ_CHECK_EQ(buf[1], 0);
208 YAZ_CHECK_EQ(((unsigned char *) buf)[2], 128);
211 odr_setbuf(decode, buf, len, 0);
212 ber_integer(decode, &ret_val);
213 YAZ_CHECK_EQ(ret_val, 128);
215 val = 2147483647; /* 2^31-1 */
217 r = ber_integer(encode, &val);
219 buf = odr_getbuf(encode, &len, 0);
221 YAZ_CHECK_EQ(len, 5);
222 YAZ_CHECK_EQ(buf[0], 4);
223 YAZ_CHECK_EQ(buf[1], 127);
224 YAZ_CHECK_EQ((unsigned char) buf[2], 255);
225 YAZ_CHECK_EQ((unsigned char) buf[3], 255);
226 YAZ_CHECK_EQ((unsigned char) buf[4], 255);
229 odr_setbuf(decode, buf, len, 0);
230 ber_integer(decode, &ret_val);
231 YAZ_CHECK_EQ(ret_val, 2147483647);
233 val = -2147483647L -1; /* -2^31 */
235 r = ber_integer(encode, &val);
237 buf = odr_getbuf(encode, &len, 0);
239 YAZ_CHECK_EQ(len, 5);
240 YAZ_CHECK_EQ(buf[0], 4);
241 YAZ_CHECK_EQ((unsigned char) buf[1], 128);
242 YAZ_CHECK_EQ(buf[2], 0);
243 YAZ_CHECK_EQ(buf[3], 0);
244 YAZ_CHECK_EQ(buf[4], 0);
247 odr_setbuf(decode, buf, len, 0);
248 ber_integer(decode, &ret_val);
249 YAZ_CHECK_EQ(ret_val, (Odr_int) -2147483647L -1);
252 static void tst_berint64(ODR encode, ODR decode)
261 val = (Odr_int) 2 * 2147483648UL; /* 2^32 */
263 r = ber_integer(encode, &val);
265 buf = odr_getbuf(encode, &len, 0);
267 YAZ_CHECK_EQ(len, 6);
268 YAZ_CHECK_EQ(buf[0], 5);
269 YAZ_CHECK_EQ(buf[1], 1);
270 YAZ_CHECK_EQ(buf[2], 0);
271 YAZ_CHECK_EQ(buf[3], 0);
272 YAZ_CHECK_EQ(buf[4], 0);
273 YAZ_CHECK_EQ(buf[5], 0);
276 odr_setbuf(decode, buf, len, 0);
277 ber_integer(decode, &ret_val);
278 YAZ_CHECK_EQ(ret_val, val);
280 val = (Odr_int) -2 * 2147483648UL; /* -2^32 */
282 r = ber_integer(encode, &val);
284 buf = odr_getbuf(encode, &len, 0);
286 YAZ_CHECK_EQ(len, 6);
287 YAZ_CHECK_EQ(buf[0], 5);
288 YAZ_CHECK_EQ((unsigned char) buf[1], 255);
289 YAZ_CHECK_EQ(buf[2], 0);
290 YAZ_CHECK_EQ(buf[3], 0);
291 YAZ_CHECK_EQ(buf[4], 0);
292 YAZ_CHECK_EQ(buf[5], 0);
295 odr_setbuf(decode, buf, len, 0);
296 ber_integer(decode, &ret_val);
297 YAZ_CHECK_EQ(ret_val, val);
299 val = (Odr_int) 1000 * 1000000000L; /* 10^12 */
301 r = ber_integer(encode, &val);
303 buf = odr_getbuf(encode, &len, 0);
305 YAZ_CHECK_EQ(len, 7);
306 YAZ_CHECK_EQ(buf[0], 6);
307 YAZ_CHECK_EQ(buf[1], 0);
308 YAZ_CHECK_EQ((unsigned char) buf[2], 232);
309 YAZ_CHECK_EQ((unsigned char) buf[3], 212);
310 YAZ_CHECK_EQ((unsigned char) buf[4], 165);
311 YAZ_CHECK_EQ(buf[5], 16);
312 YAZ_CHECK_EQ(buf[6], 0);
315 odr_setbuf(decode, buf, len, 0);
316 ber_integer(decode, &ret_val);
317 YAZ_CHECK_EQ(ret_val, val);
321 static void tst(void)
323 ODR odr_encode = odr_createmem(ODR_ENCODE);
324 ODR odr_decode = odr_createmem(ODR_DECODE);
326 YAZ_CHECK(odr_encode);
327 YAZ_CHECK(odr_decode);
329 tst_MySequence1(odr_encode, odr_decode);
330 tst_MySequence2(odr_encode, odr_decode);
331 tst_MySequence3(odr_encode, odr_decode);
333 tst_berint32(odr_encode, odr_decode);
334 tst_berint64(odr_encode, odr_decode);
336 odr_destroy(odr_encode);
337 odr_destroy(odr_decode);
340 /* example from documentation.. 'Using Odr' */
341 void do_nothing_useful(Odr_int value)
344 Odr_int *valp, *resvalp;
348 /* allocate streams */
349 if (!(encode = odr_createmem(ODR_ENCODE)))
351 if (!(decode = odr_createmem(ODR_DECODE)))
355 if (odr_integer(encode, &valp, 0, 0) == 0)
357 printf("encoding went bad\n");
360 bufferp = odr_getbuf(encode, &len, 0);
361 printf("length of encoded data is %d\n", len);
363 /* now let's decode the thing again */
364 odr_setbuf(decode, bufferp, len, 0);
365 if (odr_integer(decode, &resvalp, 0, 0) == 0)
367 printf("decoding went bad\n");
370 /* ODR_INT_PRINTF format for printf (such as %d) */
371 printf("the value is " ODR_INT_PRINTF "\n", *resvalp);
378 int main(int argc, char **argv)
380 YAZ_CHECK_INIT(argc, argv);
388 * c-file-style: "Stroustrup"
389 * indent-tabs-mode: nil
391 * vim: shiftwidth=4 tabstop=8 expandtab