blob: 98d5c3e370f92de70718e831f26cfbda7583c28b [file] [log] [blame]
vlm53cd87b2004-08-19 13:27:57 +00001#include <stdio.h>
2#include <assert.h>
3
4#include <ber_tlv_tag.c>
5
6struct tag_control_s {
7 int taglen;
8 uint8_t tagbuf[8];
9
10 int correctly_decodable;
11
12 int tclass; /* Tag class */
13 ber_tlv_tag_t tvalue; /* Tag value */
14 int constr; /* Is it constructed? */
15} control[] = {
16 { 1, { 0x2 << 6 }, 1, ASN_TAG_CLASS_CONTEXT, 0, 0 },
17 { 1, { 0x2 << 6 | 32 | 1 }, 1, ASN_TAG_CLASS_CONTEXT, 1, 1 },
18 { 1, { 0x3 << 6 | 30 }, 1, ASN_TAG_CLASS_PRIVATE, 30, 0 },
19 { 1, { 29 }, 1, ASN_TAG_CLASS_UNIVERSAL, 29, 0 },
20 { 1, { 0xbf, 31 }, 0, ASN_TAG_CLASS_CONTEXT, 31, 1 },
21 { 2, { 0xbf, 31 }, 1, ASN_TAG_CLASS_CONTEXT, 31, 1 },
vlm6a644392004-08-19 14:47:37 +000022 { 2, { 0xbf, 83 }, 1, ASN_TAG_CLASS_CONTEXT, 83, 1 },
vlm53cd87b2004-08-19 13:27:57 +000023 { 2, { 0xbf, 127 }, 1, ASN_TAG_CLASS_CONTEXT, 127, 1 },
24 { 2, { 0xbf, 129 }, 0, ASN_TAG_CLASS_CONTEXT, 127, 1 },
25 { 3, { 0xbf, 129, 0 }, 1, ASN_TAG_CLASS_CONTEXT, 128, 1 },
26 { 3, { 0xbf, 129, 1 }, 1, ASN_TAG_CLASS_CONTEXT, 129, 1 },
27 { 3, { 0xbf, 130, 0 }, 1, ASN_TAG_CLASS_CONTEXT, 256, 1 },
28 { 3, { 0xbf, 130, 1 }, 1, ASN_TAG_CLASS_CONTEXT, 257, 1 },
29 { 3, { 0xbf, 130, 0x81 }, 0, 0, 0, 0 },
30 { 4, { 0xbf, 130, 0x81, 2 }, 1, ASN_TAG_CLASS_CONTEXT, 32898, 1 },
31 { 4, { 0xbf, 130, 0x81, 0x82 }, 0, ASN_TAG_CLASS_CONTEXT, 32898, 1 },
32 { 5, { 0x1f, 130, 0x81, 0x82, 1 }, 1, 0, 4210945, 0 },
33 { 5, { 0x1f, 130, 0x81, 0x82, 2 }, 1, 0, 4210946, 0 },
34 { 5, { 0x1f, 0xff, 0x81, 0x82, 2 }, 1, 0, 266354946, 0 },
35 { 6, { 0x1f, 0xff, 0xff, 0x82, 0x80, 1 }, -1, 0, 266354946, 0 },
36 { 7, { 0x1f, 0x8E, 0x87, 0xAA, 0x95, 0x99, 3}, -1, 0, 4000000000UL, 0 },
37};
38
39
40static void check_decode(struct tag_control_s *ctrl);
41static void check_encode(struct tag_control_s *ctrl);
42
43int
44main() {
45 size_t i;
46
47 for(i = 0; i < sizeof(control) / sizeof(control[0]); i++) {
48 check_decode(&control[i]);
49 check_encode(&control[i]);
50 }
51
52 return 0;
53}
54
55static void
56check_decode(struct tag_control_s *ctrl) {
57 ber_tlv_tag_t tag = 123;
58 ber_tlv_tag_t tag1 = 124;
59 ber_tlv_tag_t tag2 = 125;
60 ssize_t size;
61
62 if(ctrl->correctly_decodable < 1) {
63 size = ber_fetch_tag(ctrl->tagbuf, ctrl->taglen, &tag1);
64 assert(size == ctrl->correctly_decodable);
65 return;
66 }
67
68 printf("Expecting ");
69 tag = (ctrl->tvalue << 2) | ctrl->tclass;
70 ber_tlv_tag_fwrite(tag, stdout);
71 printf(", got ");
72
73 size = ber_fetch_tag(ctrl->tagbuf, 0, &tag1);
74 assert(size == 0);
75
76 size = ber_fetch_tag(ctrl->tagbuf, ctrl->taglen, &tag1);
77 assert(size == ctrl->taglen);
78
79 size = ber_fetch_tag(ctrl->tagbuf, ctrl->taglen + 10, &tag2);
80 assert(size == ctrl->taglen);
81
82 ber_tlv_tag_fwrite(tag1, stdout);
83 printf("\n");
84
85 assert(tag1 == tag2);
86 assert(tag == tag1);
87
88 assert(ctrl->constr == BER_TLV_CONSTRUCTED(ctrl->tagbuf));
89}
90
91
92
93static void
94check_encode(struct tag_control_s *ctrl) {
95 uint8_t buf[16];
96 ber_tlv_tag_t tag;
97 int Filler = 0xDA;
98 ssize_t size;
99 ssize_t i;
100
101 tag = ctrl->tvalue << 2 | ctrl->tclass;
102
103 /*
104 * Testing buffer overruns.
105 */
106 for(i = 0; i < (int)sizeof(buf); i++) {
107 int j;
108
109 memset(buf, Filler, sizeof(buf));
110
111 size = ber_tlv_tag_serialize(tag, buf, i);
112 assert(size < (int)sizeof(buf));
113
114 if(size <= i) {
115 for(j = 0; j < size; j++) assert(buf[j] != Filler);
116 } else {
117 j = i;
118 }
119 for(; j < (int)sizeof(buf); j++) assert(buf[j] == Filler);
120 }
121
122 memset(buf, Filler, sizeof(buf));
123
124 size = ber_tlv_tag_serialize(tag, buf, sizeof(buf));
125 assert(size < (int)sizeof(buf));
126
127 for(i = 0; i < size; i++) assert(buf[i] != Filler);
128 for(; i < (int)sizeof(buf); i++) assert(buf[i] == Filler);
129
130 if(ctrl->correctly_decodable == 1) {
131 assert(size == ctrl->taglen);
132 }
133 if(ctrl->constr) *buf |= 0x20;
134
135 ber_tlv_tag_fwrite(tag, stdout);
136
137 printf(":");
138
139 for(i = 0; i < size; i++) {
140 printf(" %02x", buf[i]);
141 if(ctrl->correctly_decodable == 1) {
142 assert(ctrl->tagbuf[i] == buf[i]);
143 }
144 }
145 printf("\n");
146}
147