8fe42a4578a9b2850d339d70f35431372941ee8c
[com/asn1c.git] / tests / tests-c-compiler / check-src / check-22.-fwide-types.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 <T1.h>
9
10 uint8_t buf1[] = {
11         32 | 16,                /* [UNIVERSAL 16], constructed */
12         12,     /* L */
13         /* INTEGER a */
14         ((2 << 6) + 0),         /* [0], primitive */
15         2,      /* L */
16   150,
17   70,
18         /* b [1] EXPLICIT CHOICE */
19         32 | ((2 << 6) + 1),    /* [1] */
20         3,      /* L */
21         ((2 << 6) + 1),         /* [1] */
22         1,
23   'i',
24         /* UTF8String c */
25         ((2 << 6) + 2),         /* [2], primitive */
26         1,      /* L */
27   'x'
28 };
29
30 uint8_t buf2[128];
31 int buf2_pos;
32
33 static int
34 buf2_fill(const void *buffer, size_t size, void *app_key) {
35
36         (void)app_key;
37
38         if(buf2_pos + size > sizeof(buf2))
39                 return -1;
40
41         memcpy(buf2 + buf2_pos, buffer, size);
42         buf2_pos += size;
43
44         return 0;
45 }
46
47 static void
48 check(int is_ok, uint8_t *buf, size_t size, size_t consumed) {
49         T1_t t, *tp;
50         void *tpp = &tp;
51         asn_dec_rval_t rval;
52         asn_enc_rval_t erval;
53         int ret;
54         int i;
55
56         tp = memset(&t, 0, sizeof(t));
57
58         fprintf(stderr, "Buf %p\n", buf);
59         rval = ber_decode(0, &asn_DEF_T1, (void **)tpp, buf, size);
60         fprintf(stderr, "Returned code %d, consumed %d\n",
61                 (int)rval.code, (int)rval.consumed);
62
63         if(is_ok) {
64                 assert(rval.code == RC_OK);
65                 assert(rval.consumed == (size_t)consumed);
66                 assert(t.a.size == 2);
67                 assert(t.b.present == b_PR_n);
68                 assert(t.b.choice.n.size == 1);
69                 assert(t.b.choice.n.buf[0] == 'i');
70                 assert(t.c.size == 1);
71                 assert(t.c.buf[0] == 'x');
72
73         } else {
74                 if(rval.code == RC_OK) {
75                         assert(t.a.size != 2
76                                 || t.b.present != b_PR_n
77                                 || t.b.choice.n.size != 1
78                                 || t.c.size != 1
79                         );
80                 }
81                 assert(rval.consumed <= (size_t)consumed);
82                 ASN_STRUCT_RESET(asn_DEF_T1, tp);
83                 return;
84         }
85
86         fprintf(stderr, "=> Re-creating using DER encoder <=\n");
87
88         /*
89          * Try to re-create using DER encoding.
90          */
91         buf2_pos = 0;
92         erval = der_encode(&asn_DEF_T1, tp, buf2_fill, 0);
93         assert(erval.encoded != -1);
94         if(erval.encoded != sizeof(buf1)) {
95                 printf("%d != %d\n", (int)erval.encoded, (int)sizeof(buf1));
96         }
97         assert(erval.encoded == (ssize_t)sizeof(buf1));
98         for(i = 0; i < (ssize_t)sizeof(buf1); i++) {
99                 if(buf1[i] != buf2[i]) {
100                         fprintf(stderr, "Recreated buffer content mismatch:\n");
101                         fprintf(stderr, "Byte %d, %x != %x (%d != %d)\n",
102                                 i,
103                                 buf1[i], buf2[i],
104                                 buf1[i], buf2[i]
105                         );
106                 }
107                 assert(buf1[i] == buf2[i]);
108         }
109
110         fprintf(stderr, "=== asn_fprint() ===\n");
111         ret = asn_fprint(stderr, &asn_DEF_T1, tp);
112         assert(ret == 0);
113         fprintf(stderr, "=== xer_fprint() ===\n");
114         ret = xer_fprint(stderr, &asn_DEF_T1, tp);
115         assert(ret == 0);
116         fprintf(stderr, "=== EOF ===\n");
117
118         ASN_STRUCT_RESET(asn_DEF_T1, tp);
119 }
120
121 static void
122 try_corrupt(uint8_t *buf, size_t size) {
123         uint8_t tmp[size];
124
125         fprintf(stderr, "\nCorrupting...\n");
126
127         for(int i = 0; i < 1000; i++) {
128                 int loc;
129                 memcpy(tmp, buf, size);
130
131                 /* Corrupt random _non-value_ location. */
132                 do { loc = random() % size; } while(tmp[loc] >= 70);
133                 do { tmp[loc] ^= random(); } while(tmp[loc] == buf[loc]);
134
135                 fprintf(stderr, "\nTry %d: corrupting byte %d (%d->%d)\n",
136                         i, loc, buf[loc], tmp[loc]);
137
138                 check(0, tmp, size, size);
139         }
140 }
141
142 int
143 main(int ac, char **av) {
144
145         (void)ac;       /* Unused argument */
146         (void)av;       /* Unused argument */
147
148         check(1, buf1, sizeof(buf1), sizeof(buf1));
149         try_corrupt(buf1, sizeof(buf1));
150         check(1, buf1, sizeof(buf1) + 10, sizeof(buf1));
151
152         return 0;
153 }