Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 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 <T.h> |
| 9 | |
| 10 | uint8_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 | |
| 36 | static void |
Lev Walkin | 1715b32 | 2013-03-28 04:02:13 -0700 | [diff] [blame] | 37 | check(int is_ok, uint8_t *buf, size_t size, size_t consumed) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 38 | T_t t, *tp; |
Lev Walkin | 90bf7ae | 2004-10-20 15:46:56 +0000 | [diff] [blame] | 39 | asn_dec_rval_t rval; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 40 | |
| 41 | tp = memset(&t, 0, sizeof(t)); |
| 42 | |
| 43 | fprintf(stderr, "Buf %p\n", buf); |
Lev Walkin | c7400c5 | 2004-09-29 13:14:36 +0000 | [diff] [blame] | 44 | rval = ber_decode(0, &asn_DEF_T, (void **)&tp, buf, size); |
Lev Walkin | 1715b32 | 2013-03-28 04:02:13 -0700 | [diff] [blame] | 45 | fprintf(stderr, "Returned code %d, consumed %zd\n", |
| 46 | (int)rval.code, rval.consumed); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 47 | |
| 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 Velichkov | cef21e0 | 2017-10-09 23:40:17 +0300 | [diff] [blame] | 61 | |
| 62 | ASN_STRUCT_RESET(asn_DEF_T, tp); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static void |
Lev Walkin | 1715b32 | 2013-03-28 04:02:13 -0700 | [diff] [blame] | 66 | try_corrupt(uint8_t *buf, size_t size) { |
Lev Walkin | a4f8e94 | 2017-10-08 19:28:20 -0700 | [diff] [blame] | 67 | uint8_t tmp[size]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 68 | |
| 69 | fprintf(stderr, "\nCorrupting...\n"); |
| 70 | |
Lev Walkin | a4f8e94 | 2017-10-08 19:28:20 -0700 | [diff] [blame] | 71 | for(int i = 0; i < 1000; i++) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 72 | 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 | |
| 86 | int |
| 87 | main(int ac, char **av) { |
| 88 | |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 89 | (void)ac; /* Unused argument */ |
| 90 | (void)av; /* Unused argument */ |
| 91 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 92 | check(1, buf1, sizeof(buf1), sizeof(buf1)); |
| 93 | try_corrupt(buf1, sizeof(buf1)); |
| 94 | |
| 95 | return 0; |
| 96 | } |