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 | } |
| 61 | } |
| 62 | |
| 63 | static void |
Lev Walkin | 1715b32 | 2013-03-28 04:02:13 -0700 | [diff] [blame] | 64 | try_corrupt(uint8_t *buf, size_t size) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 65 | uint8_t *tmp; |
| 66 | int i; |
| 67 | |
| 68 | fprintf(stderr, "\nCorrupting...\n"); |
| 69 | |
| 70 | tmp = alloca(size); |
| 71 | |
| 72 | for(i = 0; i < 1000; i++) { |
| 73 | int loc; |
| 74 | memcpy(tmp, buf, size); |
| 75 | |
| 76 | /* Corrupt random _non-value_ location. */ |
| 77 | do { loc = random() % size; } while(tmp[loc] >= 70); |
| 78 | do { tmp[loc] ^= random(); } while(tmp[loc] == buf[loc]); |
| 79 | |
| 80 | fprintf(stderr, "\nTry %d: corrupting byte %d (%d->%d)\n", |
| 81 | i, loc, buf[loc], tmp[loc]); |
| 82 | |
| 83 | check(0, tmp, size, size); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | int |
| 88 | main(int ac, char **av) { |
| 89 | |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 90 | (void)ac; /* Unused argument */ |
| 91 | (void)av; /* Unused argument */ |
| 92 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 93 | check(1, buf1, sizeof(buf1), sizeof(buf1)); |
| 94 | try_corrupt(buf1, sizeof(buf1)); |
| 95 | |
| 96 | return 0; |
| 97 | } |