Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | #include "../INTEGER.c" |
| 2 | #include "../ber_decoder.c" |
| 3 | #include "../ber_tlv_length.c" |
| 4 | #include "../ber_tlv_tag.c" |
| 5 | #include "../der_encoder.c" |
| 6 | #include "../constraints.c" |
| 7 | |
Lev Walkin | db13f51 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 8 | static char *shared_scratch_start; |
| 9 | |
| 10 | static int _print2buf(const void *buf, size_t size, void *key) { |
| 11 | (void)key; |
| 12 | memcpy(shared_scratch_start, buf, size); |
| 13 | shared_scratch_start += size; |
| 14 | *shared_scratch_start = '\0'; /* 0-termination */ |
| 15 | return 0; |
| 16 | } |
| 17 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 18 | static void |
| 19 | check(uint8_t *buf, int size, long check_long, int check_ret) { |
Lev Walkin | db13f51 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 20 | char scratch[128]; |
| 21 | char verify[32]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 22 | INTEGER_t val; |
Lev Walkin | db13f51 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 23 | uint8_t *buf_end = buf + size; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 24 | int ret; |
| 25 | long rlong = 123; |
| 26 | |
| 27 | assert(buf); |
| 28 | assert(size >= 0); |
| 29 | |
| 30 | val.buf = buf; |
| 31 | val.size = size; |
| 32 | |
Lev Walkin | db13f51 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 33 | printf("Testing: ["); |
| 34 | for(; buf < buf_end; buf++) { |
| 35 | if(buf != val.buf) printf(":"); |
| 36 | printf("%02x", *buf); |
| 37 | } |
| 38 | printf("]: "); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 39 | |
| 40 | ret = asn1_INTEGER2long(&val, &rlong); |
Lev Walkin | db13f51 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 41 | printf(" (%ld, %d) vs (%ld, %d)\n", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 42 | rlong, ret, check_long, check_ret); |
| 43 | assert(ret == check_ret); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 44 | assert(rlong == check_long); |
Lev Walkin | db13f51 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 45 | |
| 46 | shared_scratch_start = scratch; |
| 47 | ret = INTEGER_print(&asn1_DEF_INTEGER, &val, 0, _print2buf, scratch); |
| 48 | assert(shared_scratch_start < scratch + sizeof(scratch)); |
| 49 | assert(ret == 0); |
| 50 | ret = snprintf(verify, sizeof(verify), "%ld", check_long); |
| 51 | assert(ret < sizeof(verify)); |
| 52 | ret = strcmp(scratch, verify); |
| 53 | printf(" [%s] vs [%s]: %d%s\n", |
| 54 | scratch, verify, ret, |
| 55 | (check_ret == -1)?" (expected to fail)":"" |
| 56 | ); |
| 57 | if(check_ret == -1) { |
| 58 | assert(strcmp(scratch, verify)); |
| 59 | } else { |
| 60 | assert(strcmp(scratch, verify) == 0); |
| 61 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | int |
| 65 | main(int ac, char **av) { |
| 66 | uint8_t buf1[] = { 1 }; |
| 67 | uint8_t buf2[] = { 0xff }; |
| 68 | uint8_t buf3[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; |
| 69 | uint8_t buf4[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0 }; |
| 70 | uint8_t buf5[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1 }; |
| 71 | uint8_t buf6[] = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; |
| 72 | uint8_t buf7[] = { 0xff, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; |
| 73 | uint8_t buf8[] = { 0x7f, 0x7e, 0x7d, 0x7c }; |
| 74 | uint8_t buf9[] = { 0, 0x7f, 0x7e, 0x7d, 0x7c }; |
| 75 | uint8_t buf10[] = { 0, 0, 0, 0, 0, 0, 0x7f, 0x7e, 0x7d, 0x7c }; |
| 76 | |
| 77 | #define CHECK(buf, val, ret) check(buf, sizeof(buf), val, ret) |
| 78 | |
| 79 | CHECK(buf1, 1, 0); |
| 80 | CHECK(buf2, -1, 0); |
| 81 | CHECK(buf3, -1, 0); |
| 82 | CHECK(buf4, -16, 0); |
| 83 | CHECK(buf5, 257, 0); |
| 84 | CHECK(buf6, 123, -1); |
| 85 | CHECK(buf7, 123, -1); |
| 86 | CHECK(buf8, 0x7F7E7D7C, 0); |
| 87 | CHECK(buf9, 0x7F7E7D7C, 0); |
| 88 | CHECK(buf10, 0x7F7E7D7C, 0); |
| 89 | |
| 90 | return 0; |
| 91 | } |