From b2efa1743e712eb8d713ff1b934a09cc19b35a55 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Tue, 6 May 2003 10:08:30 +0000 Subject: [PATCH] New test for ODR. Demonstrates the use of small ASN.1 spec too. --- odr/Makefile.am | 15 ++++- odr/test.c | 165 ------------------------------------------------------- odr/tstodr.asn | 16 ++++++ odr/tstodr.c | 61 ++++++++++++++++++++ 4 files changed, 91 insertions(+), 166 deletions(-) delete mode 100644 odr/test.c create mode 100644 odr/tstodr.asn create mode 100644 odr/tstodr.c diff --git a/odr/Makefile.am b/odr/Makefile.am index 988eedc..47d8321 100644 --- a/odr/Makefile.am +++ b/odr/Makefile.am @@ -1,9 +1,22 @@ -## $Id: Makefile.am,v 1.7 2002-09-11 21:25:57 adam Exp $ +## $Id: Makefile.am,v 1.8 2003-05-06 10:08:30 adam Exp $ noinst_LTLIBRARIES = libodr.la AM_CPPFLAGS=-I$(top_srcdir)/include +TESTS = tstodr + +EXTRA_PROGRAMS = tstodr + +EXTRA_DIST = tstodrcodec.c tstodrcodec.h tstodr.asn + +tstodr_LDADD = libodr.la ../util/libutil.la +tstodr_SOURCES = tstodr.c tstodrcodec.c tstodrcodec.h + +# Rule for generating codecs for our small ASN.1 spec +tstodrcodec.c tstodrcodec.h: $(srcdir)/tstodr.asn + cd $(srcdir); $(top_srcdir)/util/yaz-comp tstodr.asn + libodr_la_SOURCES = odr_bool.c ber_bool.c ber_len.c ber_tag.c odr_util.c \ odr_null.c ber_null.c odr_int.c ber_int.c odr_tag.c odr_cons.c \ odr_seq.c odr_oct.c ber_oct.c odr_bit.c ber_bit.c odr_oid.c \ diff --git a/odr/test.c b/odr/test.c deleted file mode 100644 index 6a3feef..0000000 --- a/odr/test.c +++ /dev/null @@ -1,165 +0,0 @@ -#include -#include - -#if 0 - -typedef Odr_bitmask Z_ReferenceId; - -typedef struct Z_InitRequest -{ - Z_ReferenceId *referenceId; /* OPTIONAL */ - Odr_bitmask *options; - Odr_bitmask *protocolVersion; - int *preferredMessageSize; - int *maximumRecordSize; - char *idAuthentication; /* OPTIONAL */ - char *implementationId; /* OPTIONAL */ - char *implementationName; /* OPTIONAL */ - char *implementationVersion; /* OPTIONAL */ -} Z_InitRequest; - -int z_ReferenceId(ODR o, Z_ReferenceId **p, int opt) -{ - return odr_implicit(o, odr_octetstring, (Odr_oct**) p, ODR_CONTEXT, 2, opt); -} - -int z_InitRequest(ODR o, Z_InitRequest **p, int opt) -{ - Z_InitRequest *pp; - - if (!odr_sequence_begin(o, p, sizeof(Z_InitRequest))) - return opt; - pp = *p; - return - z_ReferenceId(o, &pp->referenceId, 1) && - odr_implicit(o, odr_bitstring, &pp->protocolVersion, ODR_CONTEXT, - 3, 0) && - odr_implicit(o, odr_bitstring, &pp->options, ODR_CONTEXT, 4, 0) && - odr_implicit(o, odr_integer, &pp->preferredMessageSize, ODR_CONTEXT, - 5, 0) && - odr_implicit(o, odr_integer, &pp->maximumRecordSize, ODR_CONTEXT, - 6, 0) && - odr_implicit(o, odr_visiblestring, &pp->idAuthentication, ODR_CONTEXT, - 7, 1) && - odr_implicit(o, odr_visiblestring, &pp->implementationId, ODR_CONTEXT, - 110, 1) && - odr_implicit(o, odr_visiblestring, &pp->implementationName, ODR_CONTEXT, - 111, 1) && - odr_implicit(o, odr_visiblestring, &pp->implementationVersion, - ODR_CONTEXT, 112, 1) && - odr_sequence_end(o); -} - -struct A -{ - int which; - union - { - int *b; /* integer */ - char *c; /* visstring */ - } u; -}; - -int f_A(ODR o, struct A **p, int opt) -{ - int res; - Odr_arm arm[] = - { - { -1, -1, -1, 0, (Odr_fun) odr_integer }, - { ODR_IMPLICIT, ODR_CONTEXT, 200, 1, (Odr_fun) odr_visiblestring }, - { -1, -1, -1, -1, 0 } - }; - - if (o->direction == ODR_DECODE && !*p) - *p = odr_malloc(o, sizeof(**p)); - res = odr_choice(o, arm, &(*p)->u, &(*p)->which); - if (!res) - { - *p = 0; - return opt; - } - return 1; -} - -#if 0 -int main() -{ - int i; - unsigned char buf[4048]; - struct odr o; - Z_InitRequest ireq, *ireqp, *ireq2p; - Odr_bitmask options, protocolVersion; - char *iId = "YAZ", *iName = "Yet Another Z39.50 Implementation", - *iVersion = "0.1"; - int maximumRS = 4096, preferredMS = 2048; - static Odr_oid oid[] = {1, 2, 3, 4, -1}, *oidp1, *oidp2; - - oidp1 = oid; - - ODR_MASK_ZERO(&protocolVersion); - ODR_MASK_SET(&protocolVersion, 0); - ODR_MASK_SET(&protocolVersion, 1); - - ODR_MASK_ZERO(&options); - ODR_MASK_SET(&options, 0); - ODR_MASK_SET(&options, 1); - ODR_MASK_SET(&options, 2); - - ireq.referenceId = 0; - ireq.protocolVersion = &protocolVersion; - ireq.options = &options; - ireq.preferredMessageSize = &preferredMS; - ireq.maximumRecordSize = &maximumRS; - ireq.idAuthentication = 0; - ireq.implementationId = iId; - ireq.implementationName = iName; - ireq.implementationVersion = iVersion; - ireqp = &ireq; - - o.buf = buf; - o.bp=o.buf; - o.left = o.buflen = 1024; - o.direction = ODR_PRINT; - o.print = stdout; - o.t_class = -1; - - odr_oid(&o, &oidp1, 0); - - exit(0); - - o.direction = ODR_DECODE; - o.bp = o.buf; - - odr_oid(&o, &oidp2, 0); -} -#endif - -#endif - -int main() -{ - Odr_bitmask a; - char command; - int val; - char line[100]; - - ODR_MASK_ZERO(&a); - while (gets(line)) - { - int i; - - sscanf(line, "%c %d", &command, &val); - switch (command) - { - case 's': ODR_MASK_SET(&a, val); break; - case 'c': ODR_MASK_CLEAR(&a, val); break; - case 'g': printf("%d\n", ODR_MASK_GET(&a, val)); break; - case 'l': break; - default: printf("enter c or s or l\n"); continue; - } - printf("top is %d\n", a.top); - for (i = 0; i <= a.top; i++) - printf("%2.2x ", a.bits[i] ); - printf("\n"); - } -} diff --git a/odr/tstodr.asn b/odr/tstodr.asn new file mode 100644 index 0000000..7a3f309 --- /dev/null +++ b/odr/tstodr.asn @@ -0,0 +1,16 @@ +-- Small ASN.1 spec for tstodr test +-- $Id: tstodr.asn,v 1.1 2003-05-06 10:08:30 adam Exp $ +tstodrcodec +{tstodrcodec} DEFINITIONS ::= +BEGIN +MySequence ::= SEQUENCE { + first [1] IMPLICIT INTEGER, + second [2] IMPLICIT OCTET STRING, + third [3] IMPLICIT BOOLEAN, + fourth [4] IMPLICIT NULL, + fifth [5] IMPLICIT INTEGER { + enum1(0), + enum2(1) + } +} +END diff --git a/odr/tstodr.c b/odr/tstodr.c new file mode 100644 index 0000000..f302b20 --- /dev/null +++ b/odr/tstodr.c @@ -0,0 +1,61 @@ +/* + * Copyright (c) 1995-2003, Index Data + * See the file LICENSE for details. + * + * $Id: tstodr.c,v 1.1 2003-05-06 10:08:30 adam Exp $ + * + */ +#include +#include +#include "tstodrcodec.h" + +void tst_MySequence(ODR encode, ODR decode) +{ + char *ber_buf; + int ber_len; + Yc_MySequence *s = odr_malloc(encode, sizeof(*s)); + Yc_MySequence *t; + + s->first = odr_intdup(encode, 12345); + s->second = odr_malloc(encode, sizeof(*s->second)); + s->second->buf = "hello"; + s->second->len = 5; + s->second->size = 0; + s->third = odr_intdup(encode, 1); + s->fourth = odr_nullval(); + s->fifth = odr_intdup(encode, YC_MySequence_enum1); + + if (!yc_MySequence(encode, &s, 0, 0)) + exit(1); + + ber_buf = odr_getbuf(encode, &ber_len, 0); + + odr_setbuf(decode, ber_buf, ber_len, 1); + + if (!yc_MySequence(decode, &t, 0, 0)) + exit(2); + if (!t->first || *t->first != 12345) + exit(3); + if (!t->second || !t->second->buf || t->second->len != 5) + exit(4); + if (memcmp(t->second->buf, "hello", t->second->len)) + exit(5); + if (!t->third || *t->third != 1) + exit(6); + if (!t->fourth) + exit(7); + if (!t->fifth || *t->fifth != YC_MySequence_enum1) + exit(8); +} + +int main(int argc, char **argv) +{ + ODR odr_encode = odr_createmem(ODR_ENCODE); + ODR odr_decode = odr_createmem(ODR_DECODE); + + tst_MySequence(odr_encode, odr_decode); + + odr_destroy(odr_encode); + odr_destroy(odr_decode); + exit(0); +} -- 1.7.10.4