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