NativeEnumerated.c vars NULL init and check
[com/asn1c.git] / tests / tests-c-compiler / check-src / check-32.c
1 #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>
9 #include <SeqWithMandatory.h>
10 #include <SeqWithOptional.h>
11
12 int
13 main(int ac, char **av) {
14         Programming_t p;
15         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];
21
22         (void)ac;       /* Unused argument */
23         (void)av;       /* Unused argument */
24
25         /*
26          * No plans to fill Programming_t up:
27          * just checking whether it compiles or not.
28          */
29         memset(&p, 0, sizeof(p));
30
31         /*
32          * Construct a dummy sequence:
33          * SeqWithMandatory ::= {
34          *      seqOfMan [0] EXPLICIT SEQUENCE OF Error
35          * }
36          */
37         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,
46                         &swm, buf, sizeof buf);
47         assert(erv.encoded > 0);
48         buf[erv.encoded] = '\0';
49
50         /*
51          * Try to decode it using a compatible type.
52          */
53         drv = ber_decode(0, &asn_DEF_SeqWithOptional, (void **)&swo,
54                         buf, erv.encoded);
55         assert(drv.code == RC_OK);
56         assert((ssize_t)drv.consumed == erv.encoded);
57         assert(swo->seqOfOpt != 0);
58
59         xer_fprint(stderr, &asn_DEF_SeqWithOptional, swo);
60         void *tmp = swo->seqOfOpt;
61         swo->seqOfOpt = 0;
62
63         erv = der_encode_to_buffer(&asn_DEF_SeqWithOptional,
64                         swo, buf, sizeof buf);
65         assert(erv.encoded > 0);
66         buf[erv.encoded] = '\0';
67
68         swo->seqOfOpt = tmp;
69         ASN_STRUCT_RESET(asn_DEF_SeqWithMandatory, &swm);
70         ASN_STRUCT_FREE(asn_DEF_SeqWithOptional, swo);
71         swo = 0;
72
73         drv = ber_decode(0, &asn_DEF_SeqWithMandatory, (void **)&swo,
74                         buf, erv.encoded);
75         assert(drv.code != RC_OK);
76         ASN_STRUCT_FREE(asn_DEF_SeqWithOptional, swo);
77         swo = 0;
78         drv = ber_decode(0, &asn_DEF_SeqWithOptional, (void **)&swo,
79                         buf, erv.encoded);
80         assert(drv.code == RC_OK);
81         assert((ssize_t)drv.consumed == erv.encoded);
82         assert(swo->seqOfOpt == 0);
83
84         xer_fprint(stderr, &asn_DEF_SeqWithOptional, swo);
85         ASN_STRUCT_FREE(asn_DEF_SeqWithOptional, swo);
86
87         printf("Finished\n");
88
89         return 0;
90 }