blob: e03ecf51473a100b4c8b38e921ee9633b7aecc82 [file] [log] [blame]
vlm46ffdf62007-07-23 06:48:26 +00001#include <stdio.h>
2#include <assert.h>
3#include <time.h>
vlm7950eab2004-10-20 15:54:02 +00004#include <sys/time.h>
5
vlm46ffdf62007-07-23 06:48:26 +00006#define EMIT_ASN_DEBUG 1
7#include <OCTET_STRING.h>
8#include <BIT_STRING.h>
9
vlmedb12c62005-02-22 05:58:18 +000010enum encoding_type { HEX, BINARY, UTF8 };
vlm7950eab2004-10-20 15:54:02 +000011
12static void
vlmedb12c62005-02-22 05:58:18 +000013check(enum encoding_type type, char *tagname, char *xmlbuf, char *verify) {
vlm7950eab2004-10-20 15:54:02 +000014 int xmllen = strlen(xmlbuf);
15 int verlen = verify ? strlen(verify) : 0;
16 asn_TYPE_descriptor_t *td = &asn_DEF_OCTET_STRING;
17 OCTET_STRING_t *st = 0;
vlm170e42c2006-07-27 11:46:25 +000018 OCTET_STRING_t **stp = &st;
vlm7950eab2004-10-20 15:54:02 +000019 asn_dec_rval_t rc;
20 xer_type_decoder_f *decoder = 0;
21
22 switch(type) {
vlmedb12c62005-02-22 05:58:18 +000023 case HEX:
24 decoder = OCTET_STRING_decode_xer_hex;
25 break;
vlm7950eab2004-10-20 15:54:02 +000026 case BINARY:
27 td = &asn_DEF_BIT_STRING;
vlmedb12c62005-02-22 05:58:18 +000028 decoder = OCTET_STRING_decode_xer_binary;
29 break;
30 case UTF8:
31 decoder = OCTET_STRING_decode_xer_utf8;
32 break;
vlm7950eab2004-10-20 15:54:02 +000033 }
34
vlm170e42c2006-07-27 11:46:25 +000035 rc = decoder(0, td, (void **)stp, tagname, xmlbuf, xmllen);
vlm7950eab2004-10-20 15:54:02 +000036 printf("[%s] => [%s]:%d vs [%s]:%d, code %d\n",
37 xmlbuf,
vlm7958f112004-10-21 05:44:11 +000038 st ? (const char *)st->buf : "", st ? st->size : 0,
vlm9daadb12004-10-20 15:57:55 +000039 verify ? verify : "", verlen, rc.code);
vlm7950eab2004-10-20 15:54:02 +000040
41 if(verify) {
42 assert(rc.code == RC_OK);
43 assert(st);
44 assert(st->buf);
45 assert(st->size == verlen);
46 assert(!memcmp(st->buf, verify, verlen));
47 } else {
48 assert(rc.code != RC_OK);
49 }
50}
51
vlm19c9c1b2005-08-27 03:20:20 +000052static char buf[1024];
53
54static int
55write_buf(const void *buffer, size_t size, void *key) {
56 size_t *off = key;
57 assert(*off + size < sizeof(buf));
58 memcpy(buf + *off, buffer, size);
59 *off += size;
60 return 0;
61}
62
63static void
64encode(char *orig, char *encoded) {
65 OCTET_STRING_t os;
66 size_t written = 0;
67 asn_enc_rval_t er;
68
69 memset(&os, 0, sizeof(os));
70
71 OCTET_STRING_fromString(&os, orig);
72
73 er = OCTET_STRING_encode_xer_utf8(&asn_DEF_OCTET_STRING, &os,
74 0, 0, write_buf, &written);
75 assert(er.encoded >= 0);
76 buf[er.encoded] = '\0';
77 printf("Orig: [%s], encoded: [%s], check [%s]\n",
78 orig, buf, encoded);
79 assert(strcmp(buf, encoded) == 0);
80}
81
vlm7950eab2004-10-20 15:54:02 +000082int
83main() {
84
85 check(HEX, 0, "<OCTET_STRING>41424</OCTET_STRING>",
86 "AB@");
87
88 check(HEX, 0, "<!--comment--><OCTET_STRING>\n"
89 "<!--comment-->41424</OCTET_STRING>",
90 "AB@");
91
92 check(HEX, 0, "<OCTET_STRING blah blah> 4 1 4 2 4 5 44 </OCTET_STRING>",
93 "ABED");
94
95 /* Some hard cases */
96 check(HEX, "z", "<z><!-- < -->40</z>", "@");
97 check(HEX, "z", "<z><!-- <-->40</z>", "@");
98 check(HEX, "z", "<z><!-- -->>40</z>", 0);
99 check(HEX, "z", "<z><!-- <some <sometag>-->40</z>", "@");
100 check(HEX, "z", "<z><!-- <some <sometag-->>40</z>", 0);
101
vlm7958f112004-10-21 05:44:11 +0000102 check(HEX, "z", "ignored<z>40</z>stuff", "@");
103
vlm7950eab2004-10-20 15:54:02 +0000104 check(HEX, "tag", "<tag>4</tag>", "@");
vlmedb12c62005-02-22 05:58:18 +0000105 check(HEX, "a-z", "<a-z>7 375 73 6c6 9<!--/-->6 b</a-z>", "suslik");
106
107 /* This one has a comment in a not-yet-supported place */
108 /* check(HEX, "a-z", "<a-z>73 75 73 6c 6<!--/-->9 6b</a-z>",
109 "suslik"); */
vlm7950eab2004-10-20 15:54:02 +0000110
111 check(BINARY, "tag", "<tag/>", "");
112 check(BINARY, "tag", "<tag>blah</tag>", 0);
113 check(BINARY, "tag", "<tag>01000001</tag>", "A");
114 check(BINARY, "tag", "<tag>01000<!--blah--> 00 101 00001</tag>", "AB");
115
116 check(UTF8, 0, "<OCTET_STRING>one, two, three</OCTET_STRING>",
117 "one, two, three");
118
vlme9a652d2004-10-23 10:16:07 +0000119 check(UTF8, "z", "<z></z>", "");
vlm7950eab2004-10-20 15:54:02 +0000120 check(UTF8, "z", "<z z z>&lt;&amp;&gt;</z z z>", "<&>");
121 check(UTF8, "z", "<z z z>a&lt;b&amp;c&gt;d</z z z>", "a<b&c>d");
122 check(UTF8, "z", "<z z z>a&lt</z z z>", "a&lt");
123 check(UTF8, "z", "<z z z>a&sdfsdfsdf;b</z z z>", "a&sdfsdfsdf;b");
124 check(UTF8, "z", "<z z z>a&#x20;b</z z z>", "a b");
125 check(UTF8, "z", "<z z z>a&#32;b</z z z>", "a b");
126 check(UTF8, "z", "<z>a&#32323;b</z>", "a\347\271\203b");
127 check(UTF8, "z", "<z>a&#3000000000;b</z>", "a&#3000000000;b");
vlm6c593842004-10-26 09:03:31 +0000128 check(UTF8, "z", "<z>a&#5000000000;b</z>", "a&#5000000000;b");
vlm7950eab2004-10-20 15:54:02 +0000129 check(UTF8, "z", "<z>a&#300</z>", "a&#300");
130 check(UTF8, "z", "<z>a&#-300;</z>", "a&#-300;");
131 check(UTF8, "z", "<z>a<ff/>b</z>", "a\014b");
132 check(UTF8, "z", "<z>a<soh/>b</z>", "a\001b");
133 check(UTF8, "z", "<z>a<bel/></z>", "a\007");
134
vlm19c9c1b2005-08-27 03:20:20 +0000135 encode("", "");
136 encode("a", "a");
137 encode("a\nb", "a\nb");
138 encode("a\bc", "a<bs/>c");
139 encode("ab\01c\ndef\r\n", "ab<soh/>c\ndef\r\n");
140
vlm7950eab2004-10-20 15:54:02 +0000141 return 0;
142}
143