blob: a885d6c5b415258d073b1bf608ac55966435960f [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 <T.h>
9
10uint8_t buf1[] = {
11 32 | ((2 << 6) + 5), /* [5], constructed */
12 17, /* L */
13 32 | 16, /* [UNIVERSAL 16], constructed */
14 15, /* L */
15 /* INTEGER a */
16 2, /* [UNIVERSAL 2] */
17 2, /* L */
18 150,
19 70,
20 /* INTEGER b */
21 ((2 << 6) + 0), /* [0] */
22 1, /* L */
23 123,
24 /* INTEGER c */
25 ((2 << 6) + 1), /* [1] */
26 1, /* L */
27 123,
28 /* INTEGER d */
29 32 | ((2 << 6) + 5), /* [5], constructed */
30 3,
31 2,
32 1, /* L */
33 123,
34};
35
36static void
Lev Walkin1715b322013-03-28 04:02:13 -070037check(int is_ok, uint8_t *buf, size_t size, size_t consumed) {
Lev Walkinf15320b2004-06-03 03:38:44 +000038 T_t t, *tp;
Lev Walkin90bf7ae2004-10-20 15:46:56 +000039 asn_dec_rval_t rval;
Lev Walkinf15320b2004-06-03 03:38:44 +000040
41 tp = memset(&t, 0, sizeof(t));
42
43 fprintf(stderr, "Buf %p\n", buf);
Lev Walkinc7400c52004-09-29 13:14:36 +000044 rval = ber_decode(0, &asn_DEF_T, (void **)&tp, buf, size);
Lev Walkin1715b322013-03-28 04:02:13 -070045 fprintf(stderr, "Returned code %d, consumed %zd\n",
46 (int)rval.code, rval.consumed);
Lev Walkinf15320b2004-06-03 03:38:44 +000047
48 if(is_ok) {
49 assert(rval.code == RC_OK);
50 assert(rval.consumed == consumed);
51 } else {
52 if(rval.code == RC_OK) {
53 assert(t.a.size != 2
54 || (!t.b || t.b->size != 1)
55 || (!t.c || t.c->size != 1)
56 || t.d.size != 1
57 );
58 }
59 assert(rval.consumed <= consumed);
60 }
Vasil Velichkovcef21e02017-10-09 23:40:17 +030061
62 ASN_STRUCT_RESET(asn_DEF_T, tp);
Lev Walkinf15320b2004-06-03 03:38:44 +000063}
64
65static void
Lev Walkin1715b322013-03-28 04:02:13 -070066try_corrupt(uint8_t *buf, size_t size) {
Lev Walkina4f8e942017-10-08 19:28:20 -070067 uint8_t tmp[size];
Lev Walkinf15320b2004-06-03 03:38:44 +000068
69 fprintf(stderr, "\nCorrupting...\n");
70
Lev Walkina4f8e942017-10-08 19:28:20 -070071 for(int i = 0; i < 1000; i++) {
Lev Walkinf15320b2004-06-03 03:38:44 +000072 int loc;
73 memcpy(tmp, buf, size);
74
75 /* Corrupt random _non-value_ location. */
76 do { loc = random() % size; } while(tmp[loc] >= 70);
77 do { tmp[loc] ^= random(); } while(tmp[loc] == buf[loc]);
78
79 fprintf(stderr, "\nTry %d: corrupting byte %d (%d->%d)\n",
80 i, loc, buf[loc], tmp[loc]);
81
82 check(0, tmp, size, size);
83 }
84}
85
86int
87main(int ac, char **av) {
88
Lev Walkind9bd7752004-06-05 08:17:50 +000089 (void)ac; /* Unused argument */
90 (void)av; /* Unused argument */
91
Lev Walkinf15320b2004-06-03 03:38:44 +000092 check(1, buf1, sizeof(buf1), sizeof(buf1));
93 try_corrupt(buf1, sizeof(buf1));
94
95 return 0;
96}