Debian Codegraph @

skeletons/tests/check-OCTET_STRING.c

raw symbols SCIP
1 #include <stdio.h>
2 #include <assert.h>
3 #include <time.h>
4 #include <sys/time.h>
5
6 #include <OCTET_STRING.h>
7 #include <BIT_STRING.h>
8
9 enum encoding_type { HEX, BINARY, UTF8 };
10
11 static void
12 check(enum encoding_type type, char *tagname, char *xmlbuf, char *verify) {
13 int xmllen = strlen(xmlbuf);
14 int verlen = verify ? strlen(verify) : 0;
15 asn_TYPE_descriptor_t *td = &asn_DEF_OCTET_STRING;
16 OCTET_STRING_t *st = 0;
17 OCTET_STRING_t **stp = &st;
18 asn_dec_rval_t rc;
19 xer_type_decoder_f *decoder = 0;
20
21 switch(type) {
22 case HEX:
23 decoder = OCTET_STRING_decode_xer_hex;
24 break;
25 case BINARY:
26 td = &asn_DEF_BIT_STRING;
27 decoder = OCTET_STRING_decode_xer_binary;
28 break;
29 case UTF8:
30 decoder = OCTET_STRING_decode_xer_utf8;
31 break;
32 }
33
34 rc = decoder(0, td, (void **)stp, tagname, xmlbuf, xmllen);
35 printf("[%s] => [%s]:%d vs [%s]:%d, code %d\n",
36 xmlbuf,
37 st ? (const char *)st->buf : "", st ? st->size : 0,
38 verify ? verify : "", verlen, rc.code);
39
40 if(verify) {
41 assert(rc.code == RC_OK);
42 assert(st);
43 assert(st->buf);
44 assert(st->size == verlen);
45 assert(!memcmp(st->buf, verify, verlen));
46 } else {
47 assert(rc.code != RC_OK);
48 }
49 }
50
51 static char buf[1024];
52
53 static int
54 write_buf(const void *buffer, size_t size, void *key) {
55 size_t *off = key;
56 assert(*off + size < sizeof(buf));
57 memcpy(buf + *off, buffer, size);
58 *off += size;
59 return 0;
60 }
61
62 static void
63 encode(char *orig, char *encoded) {
64 OCTET_STRING_t os;
65 size_t written = 0;
66 asn_enc_rval_t er;
67
68 memset(&os, 0, sizeof(os));
69
70 OCTET_STRING_fromString(&os, orig);
71
72 er = OCTET_STRING_encode_xer_utf8(&asn_DEF_OCTET_STRING, &os,
73 0, 0, write_buf, &written);
74 assert(er.encoded >= 0);
75 buf[er.encoded] = '\0';
76 printf("Orig: [%s], encoded: [%s], check [%s]\n",
77 orig, buf, encoded);
78 assert(strcmp(buf, encoded) == 0);
79 }
80
81 int
82 main() {
83
84 check(HEX, 0, "<OCTET_STRING>41424</OCTET_STRING>",
85 "AB@");
86
87 check(HEX, 0, "<!--comment--><OCTET_STRING>\n"
88 "<!--comment-->41424</OCTET_STRING>",
89 "AB@");
90
91 check(HEX, 0, "<OCTET_STRING blah blah> 4 1 4 2 4 5 44 </OCTET_STRING>",
92 "ABED");
93
94 /* Some hard cases */
95 check(HEX, "z", "<z><!-- < -->40</z>", "@");
96 check(HEX, "z", "<z><!-- <-->40</z>", "@");
97 check(HEX, "z", "<z><!-- -->>40</z>", 0);
98 check(HEX, "z", "<z><!-- <some <sometag>-->40</z>", "@");
99 check(HEX, "z", "<z><!-- <some <sometag-->>40</z>", 0);
100
101 check(HEX, "z", "ignored<z>40</z>stuff", "@");
102
103 check(HEX, "tag", "<tag>4</tag>", "@");
104 check(HEX, "a-z", "<a-z>7 375 73 6c6 9<!--/-->6 b</a-z>", "suslik");
105
106 /* This one has a comment in a not-yet-supported place */
107 /* check(HEX, "a-z", "<a-z>73 75 73 6c 6<!--/-->9 6b</a-z>",
108 "suslik"); */
109
110 check(BINARY, "tag", "<tag/>", "");
111 check(BINARY, "tag", "<tag>blah</tag>", 0);
112 check(BINARY, "tag", "<tag>01000001</tag>", "A");
113 check(BINARY, "tag", "<tag>01000<!--blah--> 00 101 00001</tag>", "AB");
114
115 check(UTF8, 0, "<OCTET_STRING>one, two, three</OCTET_STRING>",
116 "one, two, three");
117
118 check(UTF8, "z", "<z></z>", "");
119 check(UTF8, "z", "<z z z>&lt;&amp;&gt;</z z z>", "<&>");
120 check(UTF8, "z", "<z z z>a&lt;b&amp;c&gt;d</z z z>", "a<b&c>d");
121 check(UTF8, "z", "<z z z>a&lt</z z z>", "a&lt");
122 check(UTF8, "z", "<z z z>a&sdfsdfsdf;b</z z z>", "a&sdfsdfsdf;b");
123 check(UTF8, "z", "<z z z>a&#x20;b</z z z>", "a b");
124 check(UTF8, "z", "<z z z>a&#32;b</z z z>", "a b");
125 check(UTF8, "z", "<z>a&#32323;b</z>", "a\347\271\203b");
126 check(UTF8, "z", "<z>a&#3000000000;b</z>", "a&#3000000000;b");
127 check(UTF8, "z", "<z>a&#5000000000;b</z>", "a&#5000000000;b");
128 check(UTF8, "z", "<z>a&#300</z>", "a&#300");
129 check(UTF8, "z", "<z>a&#-300;</z>", "a&#-300;");
130 check(UTF8, "z", "<z>a<ff/>b</z>", "a\014b");
131 check(UTF8, "z", "<z>a<soh/>b</z>", "a\001b");
132 check(UTF8, "z", "<z>a<bel/></z>", "a\007");
133
134 encode("", "");
135 encode("a", "a");
136 encode("a\nb", "a\nb");
137 encode("a\bc", "a<bs/>c");
138 encode("ab\01c\ndef\r\n", "ab<soh/>c\ndef\r\n");
139
140 return 0;
141 }
142