4 #include <asn_application.h>
5 #include <NativeEnumerated.h>
7 #define CHECK_DECODE(code, a, b, c) check_decode(__LINE__, code, a, b, c)
8 #define CHECK_ROUNDTRIP(a) check_roundtrip(__LINE__, a);
11 check_decode(int lineno, enum asn_dec_rval_code_e code, long control, const char *buf, size_t size) {
12 static char *code_s[] = { "RC_OK", "RC_WMORE", "RC_FAIL", "<error>" };
14 fprintf(stderr, "\n%d: OER decode (control %ld)\n", lineno, control);
17 long *value_ptr = &value;
20 fprintf(stderr, "%d: buf[%zu]={%d, %d, ...}\n", lineno, size,
21 size <= 0 ? -1 : ((const uint8_t *)buf)[0],
22 size <= 1 ? -1 : ((const uint8_t *)buf)[1]);
24 ret = NativeEnumerated_decode_oer(NULL, &asn_DEF_NativeEnumerated, NULL,
25 (void **)&value_ptr, buf, size);
26 if(ret.code != RC_OK) {
27 /* Basic OER decode does not work */
28 fprintf(stderr, "%d: Failed oer_decode(ctl=%ld, size=%zu)\n",
29 lineno, control, size);
30 if(ret.code == code) {
31 fprintf(stderr, " (That was expected)\n");
35 stderr, " Unexpected return code %s (%d) expected %s\n",
36 code_s[(unsigned)ret.code <= RC_FAIL ? RC_FAIL : (RC_FAIL + 1)],
37 (int)ret.code, code_s[code]);
38 assert(ret.code == code);
42 if(outcome != control) {
43 /* Decoded value is wrong */
45 "%d: Decode result %ld is not expected %ld\n",
46 lineno, outcome, control);
47 assert(outcome == control);
51 fprintf(stderr, "%d: Decode result %ld\n", lineno, control);
55 dump_data(int lineno, const uint8_t *buf, size_t size) {
56 const uint8_t *p = buf;
57 const uint8_t *end = buf + size;
59 fprintf(stderr, "%d: Encoded: [", lineno);
62 fprintf(stderr, "\\x%02x", *(const unsigned char *)p);
64 fprintf(stderr, "] (%zu bytes)\n", size);
68 check_roundtrip(int lineno, long control) {
74 fprintf(stderr, "\n%d: OER round-trip value %ld\n", lineno, control);
76 long value_out = control;
78 long *value_in_ptr = &value_in;
80 er = oer_encode_to_buffer(&asn_DEF_NativeEnumerated, NULL,
81 &value_out, tmpbuf, sizeof(tmpbuf));
82 if(er.encoded == -1) {
83 fprintf(stderr, "%d: OER encode failed for %s\n", lineno,
84 er.failed_type ? er.failed_type->name : "<none>");
85 assert(er.encoded != -1);
87 tmpbuf_size = er.encoded;
89 dump_data(lineno, tmpbuf, tmpbuf_size);
91 ret = asn_DEF_NativeEnumerated.op->oer_decoder(0, &asn_DEF_NativeEnumerated,
92 NULL, (void **)&value_in_ptr,
94 if(ret.code != RC_OK) {
95 /* Basic OER decode does not work */
96 fprintf(stderr, "%d: Failed oer_decode(value=%ld, size=%zu)\n",
97 lineno, control, tmpbuf_size);
98 assert(ret.code == 0);
100 long outcome = value_in;
101 if(outcome != control) {
102 /* Decoded value is wrong */
104 "%d: Decode result %ld is not expected %ld\n",
105 lineno, outcome, control);
106 assert(outcome == control);
110 fprintf(stderr, "%d: Decode result %ld\n", lineno, control);
115 CHECK_DECODE(RC_WMORE, 0, "", 0);
116 CHECK_DECODE(RC_OK, 0, "\x00", 1);
117 CHECK_DECODE(RC_FAIL, 0, "\x00", 1);
118 CHECK_DECODE(RC_WMORE, 0, "", 0);
119 CHECK_DECODE(RC_WMORE, 0, "", 0);
120 CHECK_DECODE(RC_OK, 0, "\x00", 1);
121 CHECK_DECODE(RC_OK, 0, "\x00", 1);
123 CHECK_DECODE(RC_OK, 0, "\x00", 1);
124 CHECK_DECODE(RC_OK, 1, "\x01", 1);
125 CHECK_DECODE(RC_FAIL, 0, "\xff", 1);
126 CHECK_DECODE(RC_FAIL, 0, "\x89", 1);
127 CHECK_DECODE(RC_WMORE, 0, "\x84", 1);
128 CHECK_DECODE(RC_WMORE, 0, "\x84\x00", 3);
129 CHECK_DECODE(RC_WMORE, 0, "\x84\x00\x00", 3);
130 CHECK_DECODE(RC_WMORE, 0, "\x84\x00\x00\x00", 4);
131 CHECK_DECODE(RC_OK, 0, "\x84\x00\x00\x00\x00", 5);
132 CHECK_DECODE(RC_OK, 1, "\x84\x00\x00\x00\x01", 5);
133 CHECK_DECODE(RC_OK, 127, "\x7f", 1);
134 CHECK_DECODE(RC_OK, 127, "\x81\x7f", 2);
135 CHECK_DECODE(RC_OK, 255, "\x82\x00\xff", 3);
136 CHECK_DECODE(RC_OK, -1, "\x81\xff", 2);
137 CHECK_DECODE(RC_OK, -1, "\x82\xff\xff", 3);
139 CHECK_DECODE(RC_OK, 1, "\x01\x00", 2);
140 CHECK_DECODE(RC_OK, 1, "\x01\x01", 2);
141 CHECK_DECODE(RC_OK, -1, "\x81\xff", 2);
142 CHECK_DECODE(RC_WMORE, -1, "\x82\x00\xff", 2);
147 CHECK_ROUNDTRIP(-127);
148 CHECK_ROUNDTRIP(-128);
149 CHECK_ROUNDTRIP(-129);
150 CHECK_ROUNDTRIP(126);
151 CHECK_ROUNDTRIP(127);
152 CHECK_ROUNDTRIP(128);
153 CHECK_ROUNDTRIP(-65000);
154 CHECK_ROUNDTRIP(65000);
155 CHECK_ROUNDTRIP(65535);
156 CHECK_ROUNDTRIP(-65535);
157 CHECK_ROUNDTRIP(-65536);
158 CHECK_ROUNDTRIP(65536);
159 CHECK_ROUNDTRIP(32000);
161 for(size_t i = 0; i < 8 * sizeof(long) - 1; i++) {
162 long value = (long)1 << i;
163 CHECK_ROUNDTRIP(value);
165 CHECK_ROUNDTRIP(value);
168 CHECK_ROUNDTRIP(LONG_MIN);
169 CHECK_ROUNDTRIP(LONG_MAX);