blob: 3f8bf062c322f02ff7e50752814a7987d0d9dcba [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001#undef NDEBUG
2#include <stdio.h>
3#include <stdlib.h>
4#include <sys/types.h>
5#include <string.h>
6#include <assert.h>
7
8#include <Programming.h>
Lev Walkin650da772006-06-17 03:30:43 +00009#include <SeqWithMandatory.h>
10#include <SeqWithOptional.h>
Lev Walkinf15320b2004-06-03 03:38:44 +000011
12int
13main(int ac, char **av) {
14 Programming_t p;
Lev Walkin650da772006-06-17 03:30:43 +000015 SeqWithMandatory_t swm;
16 SeqWithOptional_t *swo = 0;
17 Error_t *err;
18 asn_enc_rval_t erv;
19 asn_dec_rval_t drv;
20 char buf[128];
Lev Walkinf15320b2004-06-03 03:38:44 +000021
Lev Walkind9bd7752004-06-05 08:17:50 +000022 (void)ac; /* Unused argument */
23 (void)av; /* Unused argument */
24
Lev Walkin650da772006-06-17 03:30:43 +000025 /*
26 * No plans to fill Programming_t up:
27 * just checking whether it compiles or not.
28 */
Lev Walkinf15320b2004-06-03 03:38:44 +000029 memset(&p, 0, sizeof(p));
30
31 /*
Lev Walkin650da772006-06-17 03:30:43 +000032 * Construct a dummy sequence:
33 * SeqWithMandatory ::= {
34 * seqOfMan [0] EXPLICIT SEQUENCE OF Error
35 * }
Lev Walkinf15320b2004-06-03 03:38:44 +000036 */
Lev Walkin650da772006-06-17 03:30:43 +000037 err = calloc(1, sizeof *err);
38 memset(&swm, 0, sizeof swm);
39 OCTET_STRING_fromBuf(&swm.someString, "Oley", 4);
40 ASN_SEQUENCE_ADD(&swm.seqOfMan, err);
41
42 /*
43 * Encode the sequence.
44 */
45 erv = der_encode_to_buffer(&asn_DEF_SeqWithMandatory,
Lev Walkinc46b7cb2006-08-18 02:27:55 +000046 &swm, buf, sizeof buf);
47 assert(erv.encoded > 0);
48 buf[erv.encoded] = '\0';
Lev Walkin650da772006-06-17 03:30:43 +000049
50 /*
51 * Try to decode it using a compatible type.
52 */
53 drv = ber_decode(0, &asn_DEF_SeqWithOptional, (void **)&swo,
Lev Walkinc46b7cb2006-08-18 02:27:55 +000054 buf, erv.encoded);
Lev Walkin650da772006-06-17 03:30:43 +000055 assert(drv.code == RC_OK);
Lev Walkinfb2a30b2014-01-14 02:11:37 -080056 assert((ssize_t)drv.consumed == erv.encoded);
Lev Walkin650da772006-06-17 03:30:43 +000057 assert(swo->seqOfOpt != 0);
58
59 xer_fprint(stderr, &asn_DEF_SeqWithOptional, swo);
Vasil Velichkovcef21e02017-10-09 23:40:17 +030060 void *tmp = swo->seqOfOpt;
Lev Walkin650da772006-06-17 03:30:43 +000061 swo->seqOfOpt = 0;
62
Lev Walkin650da772006-06-17 03:30:43 +000063 erv = der_encode_to_buffer(&asn_DEF_SeqWithOptional,
Lev Walkinc46b7cb2006-08-18 02:27:55 +000064 swo, buf, sizeof buf);
65 assert(erv.encoded > 0);
66 buf[erv.encoded] = '\0';
Lev Walkin650da772006-06-17 03:30:43 +000067
Vasil Velichkovcef21e02017-10-09 23:40:17 +030068 swo->seqOfOpt = tmp;
69 ASN_STRUCT_RESET(asn_DEF_SeqWithMandatory, &swm);
70 ASN_STRUCT_FREE(asn_DEF_SeqWithOptional, swo);
Lev Walkin650da772006-06-17 03:30:43 +000071 swo = 0;
Vasil Velichkovcef21e02017-10-09 23:40:17 +030072
Lev Walkin650da772006-06-17 03:30:43 +000073 drv = ber_decode(0, &asn_DEF_SeqWithMandatory, (void **)&swo,
Lev Walkinc46b7cb2006-08-18 02:27:55 +000074 buf, erv.encoded);
Lev Walkin650da772006-06-17 03:30:43 +000075 assert(drv.code != RC_OK);
Vasil Velichkovcef21e02017-10-09 23:40:17 +030076 ASN_STRUCT_FREE(asn_DEF_SeqWithOptional, swo);
Lev Walkin650da772006-06-17 03:30:43 +000077 swo = 0;
78 drv = ber_decode(0, &asn_DEF_SeqWithOptional, (void **)&swo,
Lev Walkinc46b7cb2006-08-18 02:27:55 +000079 buf, erv.encoded);
Lev Walkin650da772006-06-17 03:30:43 +000080 assert(drv.code == RC_OK);
Lev Walkinfb2a30b2014-01-14 02:11:37 -080081 assert((ssize_t)drv.consumed == erv.encoded);
Lev Walkin650da772006-06-17 03:30:43 +000082 assert(swo->seqOfOpt == 0);
83
84 xer_fprint(stderr, &asn_DEF_SeqWithOptional, swo);
Vasil Velichkovcef21e02017-10-09 23:40:17 +030085 ASN_STRUCT_FREE(asn_DEF_SeqWithOptional, swo);
Lev Walkin650da772006-06-17 03:30:43 +000086
87 printf("Finished\n");
Lev Walkinf15320b2004-06-03 03:38:44 +000088
89 return 0;
90}