blob: 85e9b1116e3478438f4953de057fd425e826bc51 [file] [log] [blame]
vlmd3da8512004-08-19 13:26:54 +00001#include <INTEGER.c>
vlm6678cb12004-09-26 13:10:40 +00002#include <ber_codec_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>
7#include <constraints.c>
vlmfa67ddc2004-06-03 03:38:44 +00008
vlm7d278c42004-07-19 17:30:25 +00009static char *shared_scratch_start;
10
11static int _print2buf(const void *buf, size_t size, void *key) {
12 (void)key;
13 memcpy(shared_scratch_start, buf, size);
14 shared_scratch_start += size;
15 *shared_scratch_start = '\0'; /* 0-termination */
16 return 0;
17}
18
vlmfa67ddc2004-06-03 03:38:44 +000019static void
20check(uint8_t *buf, int size, long check_long, int check_ret) {
vlm7d278c42004-07-19 17:30:25 +000021 char scratch[128];
22 char verify[32];
vlmfa67ddc2004-06-03 03:38:44 +000023 INTEGER_t val;
vlm7d278c42004-07-19 17:30:25 +000024 uint8_t *buf_end = buf + size;
vlmfa67ddc2004-06-03 03:38:44 +000025 int ret;
26 long rlong = 123;
27
28 assert(buf);
29 assert(size >= 0);
30
31 val.buf = buf;
32 val.size = size;
33
vlm7d278c42004-07-19 17:30:25 +000034 printf("Testing: [");
35 for(; buf < buf_end; buf++) {
36 if(buf != val.buf) printf(":");
37 printf("%02x", *buf);
38 }
39 printf("]: ");
vlmfa67ddc2004-06-03 03:38:44 +000040
vlmef6355b2004-09-29 13:26:15 +000041 ret = asn_INTEGER2long(&val, &rlong);
vlm7d278c42004-07-19 17:30:25 +000042 printf(" (%ld, %d) vs (%ld, %d)\n",
vlmfa67ddc2004-06-03 03:38:44 +000043 rlong, ret, check_long, check_ret);
44 assert(ret == check_ret);
vlmfa67ddc2004-06-03 03:38:44 +000045 assert(rlong == check_long);
vlm7d278c42004-07-19 17:30:25 +000046
47 shared_scratch_start = scratch;
vlmef6355b2004-09-29 13:26:15 +000048 ret = INTEGER_print(&asn_DEF_INTEGER, &val, 0, _print2buf, scratch);
vlm7d278c42004-07-19 17:30:25 +000049 assert(shared_scratch_start < scratch + sizeof(scratch));
50 assert(ret == 0);
51 ret = snprintf(verify, sizeof(verify), "%ld", check_long);
52 assert(ret < sizeof(verify));
53 ret = strcmp(scratch, verify);
54 printf(" [%s] vs [%s]: %d%s\n",
55 scratch, verify, ret,
56 (check_ret == -1)?" (expected to fail)":""
57 );
58 if(check_ret == -1) {
59 assert(strcmp(scratch, verify));
60 } else {
61 assert(strcmp(scratch, verify) == 0);
62 }
vlmfa67ddc2004-06-03 03:38:44 +000063}
64
65int
66main(int ac, char **av) {
67 uint8_t buf1[] = { 1 };
68 uint8_t buf2[] = { 0xff };
69 uint8_t buf3[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
70 uint8_t buf4[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0 };
71 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 };
72 uint8_t buf6[] = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
73 uint8_t buf7[] = { 0xff, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
74 uint8_t buf8[] = { 0x7f, 0x7e, 0x7d, 0x7c };
75 uint8_t buf9[] = { 0, 0x7f, 0x7e, 0x7d, 0x7c };
76 uint8_t buf10[] = { 0, 0, 0, 0, 0, 0, 0x7f, 0x7e, 0x7d, 0x7c };
77
78#define CHECK(buf, val, ret) check(buf, sizeof(buf), val, ret)
79
80 CHECK(buf1, 1, 0);
81 CHECK(buf2, -1, 0);
82 CHECK(buf3, -1, 0);
83 CHECK(buf4, -16, 0);
84 CHECK(buf5, 257, 0);
85 CHECK(buf6, 123, -1);
86 CHECK(buf7, 123, -1);
87 CHECK(buf8, 0x7F7E7D7C, 0);
88 CHECK(buf9, 0x7F7E7D7C, 0);
89 CHECK(buf10, 0x7F7E7D7C, 0);
90
91 return 0;
92}