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