vlm | d3da851 | 2004-08-19 13:26:54 +0000 | [diff] [blame] | 1 | #include <INTEGER.c> |
vlm | 19c3a19 | 2004-10-21 11:19:51 +0000 | [diff] [blame] | 2 | #include <asn_codecs_prim.c> |
vlm | d3da851 | 2004-08-19 13:26:54 +0000 | [diff] [blame] | 3 | #include <ber_decoder.c> |
| 4 | #include <ber_tlv_length.c> |
| 5 | #include <ber_tlv_tag.c> |
| 6 | #include <der_encoder.c> |
vlm | 19c3a19 | 2004-10-21 11:19:51 +0000 | [diff] [blame] | 7 | #include <xer_decoder.c> |
| 8 | #include <xer_support.c> |
vlm | d3da851 | 2004-08-19 13:26:54 +0000 | [diff] [blame] | 9 | #include <constraints.c> |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 10 | |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 11 | static char *shared_scratch_start; |
| 12 | |
| 13 | static int _print2buf(const void *buf, size_t size, void *key) { |
| 14 | (void)key; |
| 15 | memcpy(shared_scratch_start, buf, size); |
| 16 | shared_scratch_start += size; |
| 17 | *shared_scratch_start = '\0'; /* 0-termination */ |
| 18 | return 0; |
| 19 | } |
| 20 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 21 | static void |
| 22 | check(uint8_t *buf, int size, long check_long, int check_ret) { |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 23 | char scratch[128]; |
| 24 | char verify[32]; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 25 | INTEGER_t val; |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 26 | uint8_t *buf_end = buf + size; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 27 | int ret; |
| 28 | long rlong = 123; |
| 29 | |
| 30 | assert(buf); |
| 31 | assert(size >= 0); |
| 32 | |
| 33 | val.buf = buf; |
| 34 | val.size = size; |
| 35 | |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 36 | printf("Testing: ["); |
| 37 | for(; buf < buf_end; buf++) { |
| 38 | if(buf != val.buf) printf(":"); |
| 39 | printf("%02x", *buf); |
| 40 | } |
| 41 | printf("]: "); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 42 | |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 43 | ret = asn_INTEGER2long(&val, &rlong); |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 44 | printf(" (%ld, %d) vs (%ld, %d)\n", |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 45 | rlong, ret, check_long, check_ret); |
| 46 | assert(ret == check_ret); |
vlm | 6c59384 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 47 | printf("%ld %ld\n", rlong, check_long); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 48 | assert(rlong == check_long); |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 49 | |
vlm | 19c3a19 | 2004-10-21 11:19:51 +0000 | [diff] [blame] | 50 | if(check_ret == 0) { |
| 51 | INTEGER_t val2; |
| 52 | long rlong2; |
| 53 | val2.buf = 0; |
| 54 | val2.size = 0; |
| 55 | ret = asn_long2INTEGER(&val2, rlong); |
| 56 | assert(ret == 0); |
| 57 | assert(val2.buf); |
| 58 | assert(val2.size <= val.size); /* At least as compact */ |
| 59 | ret = asn_INTEGER2long(&val, &rlong2); |
| 60 | assert(ret == 0); |
| 61 | assert(rlong == rlong2); |
| 62 | } |
| 63 | |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 64 | shared_scratch_start = scratch; |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 65 | ret = INTEGER_print(&asn_DEF_INTEGER, &val, 0, _print2buf, scratch); |
vlm | 7d278c4 | 2004-07-19 17:30:25 +0000 | [diff] [blame] | 66 | assert(shared_scratch_start < scratch + sizeof(scratch)); |
| 67 | assert(ret == 0); |
| 68 | ret = snprintf(verify, sizeof(verify), "%ld", check_long); |
| 69 | assert(ret < sizeof(verify)); |
| 70 | ret = strcmp(scratch, verify); |
| 71 | printf(" [%s] vs [%s]: %d%s\n", |
| 72 | scratch, verify, ret, |
| 73 | (check_ret == -1)?" (expected to fail)":"" |
| 74 | ); |
| 75 | if(check_ret == -1) { |
| 76 | assert(strcmp(scratch, verify)); |
| 77 | } else { |
| 78 | assert(strcmp(scratch, verify) == 0); |
| 79 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 80 | } |
| 81 | |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 82 | static void |
| 83 | check_xer(int tofail, char *xmldata, long orig_value) { |
| 84 | INTEGER_t *st = 0; |
| 85 | asn_dec_rval_t rc; |
| 86 | long value; |
| 87 | int ret; |
| 88 | |
vlm | cdc58f4 | 2004-10-21 12:23:41 +0000 | [diff] [blame] | 89 | printf("[%s] vs %ld: ", xmldata, orig_value); |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 90 | |
| 91 | rc = xer_decode(0, &asn_DEF_INTEGER, (void *)&st, |
| 92 | xmldata, strlen(xmldata)); |
| 93 | if(rc.code != RC_OK) { |
| 94 | assert(tofail); |
| 95 | printf("\tfailed, as expected\n"); |
| 96 | return; |
| 97 | } |
| 98 | assert(!tofail); |
| 99 | |
| 100 | ret = asn_INTEGER2long(st, &value); |
| 101 | assert(ret == 0); |
| 102 | |
| 103 | printf("\t%ld\n", value); |
| 104 | |
| 105 | assert(value == orig_value); |
| 106 | |
| 107 | asn_DEF_INTEGER.free_struct(&asn_DEF_INTEGER, st, 0); |
| 108 | } |
| 109 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 110 | int |
| 111 | main(int ac, char **av) { |
| 112 | uint8_t buf1[] = { 1 }; |
| 113 | uint8_t buf2[] = { 0xff }; |
| 114 | uint8_t buf3[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; |
| 115 | uint8_t buf4[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0 }; |
| 116 | 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 }; |
| 117 | uint8_t buf6[] = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; |
| 118 | uint8_t buf7[] = { 0xff, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; |
| 119 | uint8_t buf8[] = { 0x7f, 0x7e, 0x7d, 0x7c }; |
| 120 | uint8_t buf9[] = { 0, 0x7f, 0x7e, 0x7d, 0x7c }; |
| 121 | uint8_t buf10[] = { 0, 0, 0, 0, 0, 0, 0x7f, 0x7e, 0x7d, 0x7c }; |
vlm | 19c3a19 | 2004-10-21 11:19:51 +0000 | [diff] [blame] | 122 | uint8_t buf11[] = { 0x80, 0, 0, 0 }; |
| 123 | uint8_t buf12[] = { 0x80, 0 }; |
| 124 | uint8_t buf13[] = { 0x80 }; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 125 | |
| 126 | #define CHECK(buf, val, ret) check(buf, sizeof(buf), val, ret) |
| 127 | |
| 128 | CHECK(buf1, 1, 0); |
| 129 | CHECK(buf2, -1, 0); |
| 130 | CHECK(buf3, -1, 0); |
| 131 | CHECK(buf4, -16, 0); |
| 132 | CHECK(buf5, 257, 0); |
| 133 | CHECK(buf6, 123, -1); |
| 134 | CHECK(buf7, 123, -1); |
| 135 | CHECK(buf8, 0x7F7E7D7C, 0); |
| 136 | CHECK(buf9, 0x7F7E7D7C, 0); |
| 137 | CHECK(buf10, 0x7F7E7D7C, 0); |
vlm | 6c59384 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 138 | CHECK(buf11, -2147483648, 0); /* 0x80000000 */ |
vlm | 19c3a19 | 2004-10-21 11:19:51 +0000 | [diff] [blame] | 139 | CHECK(buf12, -32768, 0); |
| 140 | CHECK(buf13, -128, 0); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 141 | |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 142 | check_xer(-1, "", 0); |
| 143 | check_xer(-1, "<INTEGER></INTEGER>", 0); |
vlm | cdc58f4 | 2004-10-21 12:23:41 +0000 | [diff] [blame] | 144 | check_xer(-1, "<INTEGER> </INTEGER>", 0); |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 145 | check_xer(-1, "<INTEGER>-</INTEGER>", 0); |
| 146 | check_xer(-1, "<INTEGER>+</INTEGER>", 0); |
| 147 | check_xer(-1, "<INTEGER>+-</INTEGER>", 0); |
| 148 | check_xer(0, "<INTEGER>+0</INTEGER>", 0); |
| 149 | check_xer(0, "<INTEGER>-0</INTEGER>", 0); |
| 150 | check_xer(0, "<INTEGER>+1</INTEGER>", 1); |
| 151 | check_xer(0, "<INTEGER>-1</INTEGER>", -1); |
| 152 | check_xer(0, "<INTEGER>1</INTEGER>", 1); |
| 153 | check_xer(0, "<INTEGER>-15</INTEGER>", -15); |
| 154 | check_xer(0, "<INTEGER>+15</INTEGER>", 15); |
| 155 | check_xer(0, "<INTEGER>15</INTEGER>", 15); |
| 156 | check_xer(0, "<INTEGER> 15</INTEGER>", 15); |
| 157 | check_xer(0, "<INTEGER> 15 </INTEGER>", 15); |
| 158 | check_xer(0, "<INTEGER>15 </INTEGER>", 15); |
| 159 | check_xer(0, "<INTEGER> +15 </INTEGER>", 15); |
| 160 | check_xer(-1, "<INTEGER> +15 -</INTEGER>", 0); |
| 161 | check_xer(-1, "<INTEGER> +15 1</INTEGER>", 0); |
| 162 | check_xer(-1, "<INTEGER>+ 15</INTEGER>", 0); |
| 163 | check_xer(-1, "<INTEGER>12<z>34</INTEGER>", 0); |
| 164 | check_xer(0, "<INTEGER>1234</INTEGER>", 1234); |
| 165 | check_xer(-1, "<INTEGER>1234 5678</INTEGER>", 0); |
| 166 | check_xer(0, "<INTEGER>-2147483647</INTEGER>", -2147483647); |
| 167 | check_xer(0, "<INTEGER>-2147483648</INTEGER>", -2147483648); |
| 168 | check_xer(0, "<INTEGER>+2147483647</INTEGER>", 2147483647); |
| 169 | check_xer(0, "<INTEGER>2147483647</INTEGER>", 2147483647); |
vlm | 1069dac | 2004-10-23 10:17:02 +0000 | [diff] [blame] | 170 | if(sizeof(long) == 4) { |
| 171 | check_xer(-1, "<INTEGER>2147483648</INTEGER>", 0); |
| 172 | check_xer(-1, "<INTEGER>2147483649</INTEGER>", 0); |
| 173 | check_xer(-1, "<INTEGER>3147483649</INTEGER>", 0); |
| 174 | check_xer(-1, "<INTEGER>4147483649</INTEGER>", 0); |
| 175 | check_xer(-1, "<INTEGER>5147483649</INTEGER>", 0); /* special */ |
| 176 | check_xer(-1, "<INTEGER>9147483649</INTEGER>", 0); |
| 177 | check_xer(-1, "<INTEGER>9999999999</INTEGER>", 0); |
| 178 | } |
vlm | e4f9cac | 2004-10-21 12:11:57 +0000 | [diff] [blame] | 179 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 180 | return 0; |
| 181 | } |