blob: ea686b434507dcfcbe8fc3921f23f187d88a570a [file] [log] [blame]
vlmd3da8512004-08-19 13:26:54 +00001#include <INTEGER.c>
vlm19c3a192004-10-21 11:19:51 +00002#include <asn_codecs_prim.c>
vlmd3da8512004-08-19 13:26:54 +00003#include <ber_decoder.c>
4#include <ber_tlv_length.c>
5#include <ber_tlv_tag.c>
6#include <der_encoder.c>
vlm19c3a192004-10-21 11:19:51 +00007#include <xer_decoder.c>
8#include <xer_support.c>
vlmd3da8512004-08-19 13:26:54 +00009#include <constraints.c>
vlmfa67ddc2004-06-03 03:38:44 +000010
vlm7d278c42004-07-19 17:30:25 +000011static char *shared_scratch_start;
12
13static 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
vlmfa67ddc2004-06-03 03:38:44 +000021static void
22check(uint8_t *buf, int size, long check_long, int check_ret) {
vlm7d278c42004-07-19 17:30:25 +000023 char scratch[128];
24 char verify[32];
vlmfa67ddc2004-06-03 03:38:44 +000025 INTEGER_t val;
vlm7d278c42004-07-19 17:30:25 +000026 uint8_t *buf_end = buf + size;
vlmfa67ddc2004-06-03 03:38:44 +000027 int ret;
28 long rlong = 123;
29
30 assert(buf);
31 assert(size >= 0);
32
33 val.buf = buf;
34 val.size = size;
35
vlm7d278c42004-07-19 17:30:25 +000036 printf("Testing: [");
37 for(; buf < buf_end; buf++) {
38 if(buf != val.buf) printf(":");
39 printf("%02x", *buf);
40 }
41 printf("]: ");
vlmfa67ddc2004-06-03 03:38:44 +000042
vlmef6355b2004-09-29 13:26:15 +000043 ret = asn_INTEGER2long(&val, &rlong);
vlm7d278c42004-07-19 17:30:25 +000044 printf(" (%ld, %d) vs (%ld, %d)\n",
vlmfa67ddc2004-06-03 03:38:44 +000045 rlong, ret, check_long, check_ret);
46 assert(ret == check_ret);
vlmfa67ddc2004-06-03 03:38:44 +000047 assert(rlong == check_long);
vlm7d278c42004-07-19 17:30:25 +000048
vlm19c3a192004-10-21 11:19:51 +000049 if(check_ret == 0) {
50 INTEGER_t val2;
51 long rlong2;
52 val2.buf = 0;
53 val2.size = 0;
54 ret = asn_long2INTEGER(&val2, rlong);
55 assert(ret == 0);
56 assert(val2.buf);
57 assert(val2.size <= val.size); /* At least as compact */
58 ret = asn_INTEGER2long(&val, &rlong2);
59 assert(ret == 0);
60 assert(rlong == rlong2);
61 }
62
vlm7d278c42004-07-19 17:30:25 +000063 shared_scratch_start = scratch;
vlmef6355b2004-09-29 13:26:15 +000064 ret = INTEGER_print(&asn_DEF_INTEGER, &val, 0, _print2buf, scratch);
vlm7d278c42004-07-19 17:30:25 +000065 assert(shared_scratch_start < scratch + sizeof(scratch));
66 assert(ret == 0);
67 ret = snprintf(verify, sizeof(verify), "%ld", check_long);
68 assert(ret < sizeof(verify));
69 ret = strcmp(scratch, verify);
70 printf(" [%s] vs [%s]: %d%s\n",
71 scratch, verify, ret,
72 (check_ret == -1)?" (expected to fail)":""
73 );
74 if(check_ret == -1) {
75 assert(strcmp(scratch, verify));
76 } else {
77 assert(strcmp(scratch, verify) == 0);
78 }
vlmfa67ddc2004-06-03 03:38:44 +000079}
80
81int
82main(int ac, char **av) {
83 uint8_t buf1[] = { 1 };
84 uint8_t buf2[] = { 0xff };
85 uint8_t buf3[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
86 uint8_t buf4[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0 };
87 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 };
88 uint8_t buf6[] = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
89 uint8_t buf7[] = { 0xff, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
90 uint8_t buf8[] = { 0x7f, 0x7e, 0x7d, 0x7c };
91 uint8_t buf9[] = { 0, 0x7f, 0x7e, 0x7d, 0x7c };
92 uint8_t buf10[] = { 0, 0, 0, 0, 0, 0, 0x7f, 0x7e, 0x7d, 0x7c };
vlm19c3a192004-10-21 11:19:51 +000093 uint8_t buf11[] = { 0x80, 0, 0, 0 };
94 uint8_t buf12[] = { 0x80, 0 };
95 uint8_t buf13[] = { 0x80 };
vlmfa67ddc2004-06-03 03:38:44 +000096
97#define CHECK(buf, val, ret) check(buf, sizeof(buf), val, ret)
98
99 CHECK(buf1, 1, 0);
100 CHECK(buf2, -1, 0);
101 CHECK(buf3, -1, 0);
102 CHECK(buf4, -16, 0);
103 CHECK(buf5, 257, 0);
104 CHECK(buf6, 123, -1);
105 CHECK(buf7, 123, -1);
106 CHECK(buf8, 0x7F7E7D7C, 0);
107 CHECK(buf9, 0x7F7E7D7C, 0);
108 CHECK(buf10, 0x7F7E7D7C, 0);
vlm19c3a192004-10-21 11:19:51 +0000109 CHECK(buf11, 0x80000000, 0);
110 CHECK(buf12, -32768, 0);
111 CHECK(buf13, -128, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000112
113 return 0;
114}