blob: c5591cc6c55f9d57c42ec7c5d04503fb8feee1ec [file] [log] [blame]
Lev Walkined3a4ae2017-07-07 10:09:51 -07001#include <stdio.h>
2#include <assert.h>
3
Lev Walkine4d8c922017-07-10 20:29:33 -07004#include <asn_codecs.h>
Lev Walkined3a4ae2017-07-07 10:09:51 -07005#include <INTEGER.h>
Lev Walkined3a4ae2017-07-07 10:09:51 -07006
Lev Walkine4d8c922017-07-10 20:29:33 -07007#define CHECK_DECODE(code, a, b, c, d, e, f) check_decode(__LINE__, code, a, b, c, d, e, f)
Lev Walkin486fd5c2017-07-10 22:08:14 -07008#define CHECK_ROUNDTRIP(a, b, c) check_roundtrip(__LINE__, a, b, c);
Lev Walkin54f34512017-07-10 05:27:46 -07009
Lev Walkine4d8c922017-07-10 20:29:33 -070010static const intmax_t NoBound = -4200024;
11
12static void
13check_decode(int lineno, enum asn_dec_rval_code_e code, intmax_t control, const char *buf, size_t size, const char *dummy, intmax_t lower_bound, intmax_t upper_bound) {
14 static char *code_s[] = { "RC_OK", "RC_WMORE", "RC_FAIL", "<error>" };
15
Lev Walkin486fd5c2017-07-10 22:08:14 -070016 fprintf(stderr, "%d: buf[%zu]={%d, %d, ...}\n", lineno, size,
Lev Walkine4d8c922017-07-10 20:29:33 -070017 ((const uint8_t *)buf)[0],
18 ((const uint8_t *)buf)[1]);
Lev Walkin54f34512017-07-10 05:27:46 -070019
20 INTEGER_t *st = NULL;
21 asn_dec_rval_t ret;
Lev Walkine4d8c922017-07-10 20:29:33 -070022 struct asn_oer_constraints_s empty_constraints = {{0,0,0}, {0,0,0}};
23 struct asn_oer_constraints_s *constraints = &empty_constraints;
24 struct asn_oer_constraint_s *ct_value = &constraints->value;
Lev Walkin54f34512017-07-10 05:27:46 -070025
Lev Walkine4d8c922017-07-10 20:29:33 -070026 /* Setup integer constraints as requested */
27 if(lower_bound == NoBound && upper_bound == NoBound) {
28 constraints = NULL;
29 } else {
30 if(lower_bound != NoBound) {
31 ct_value->flags |= AOC_HAS_LOWER_BOUND;
32 ct_value->lower_bound = lower_bound;
33 }
34 if(upper_bound != NoBound) {
35 ct_value->flags |= AOC_HAS_UPPER_BOUND;
36 ct_value->upper_bound = upper_bound;
37 }
38 }
39
40 (void)dummy;
41
42 ret = asn_DEF_INTEGER.oer_decoder(0, &asn_DEF_INTEGER, constraints,
43 (void **)&st, buf, size);
Lev Walkin54f34512017-07-10 05:27:46 -070044 if(ret.code != RC_OK) {
45 /* Basic OER decode does not work */
46 fprintf(stderr, "%d: Failed oer_decode(ctl=%" PRIdMAX ", size=%zu)\n",
47 lineno, control, size);
Lev Walkine4d8c922017-07-10 20:29:33 -070048 if(ret.code == code) {
49 fprintf(stderr, " (That was expected)\n");
50 return;
51 } else {
52 fprintf(
53 stderr, " Unexpected return code %s (%d) expected %s\n",
54 code_s[(unsigned)ret.code <= RC_FAIL ? RC_FAIL : (RC_FAIL + 1)],
55 (int)ret.code, code_s[code]);
56 assert(ret.code == code);
57 }
Lev Walkin54f34512017-07-10 05:27:46 -070058 } else {
59 intmax_t outcome;
60 if(asn_INTEGER2imax(st, &outcome) != 0) {
61 /* Result of decode is structurally incorrect */
Lev Walkine4d8c922017-07-10 20:29:33 -070062 fprintf(stderr,
63 "%d: Failed to convert INTEGER 2 imax; structurally "
64 "incorrect INTEGER\n",
Lev Walkin54f34512017-07-10 05:27:46 -070065 lineno);
66 assert(!"Unreachable");
67 } else if(outcome != control) {
68 /* Decoded value is wrong */
69 fprintf(stderr,
70 "%d: Decode result %" PRIdMAX " is not expected %" PRIdMAX
71 "\n",
72 lineno, outcome, control);
73 assert(outcome == control);
74 }
75 }
76
77 fprintf(stderr, "%d: Decode result %" PRIdMAX "\n", lineno, control);
Lev Walkined3a4ae2017-07-07 10:09:51 -070078}
79
Lev Walkin486fd5c2017-07-10 22:08:14 -070080
81static void
82check_roundtrip(int lineno, intmax_t value, intmax_t lower_bound, intmax_t upper_bound) {
83 uint8_t tmpbuf[32];
84 size_t tmpbuf_size;
85
86 INTEGER_t *stOut = (INTEGER_t *)calloc(1, sizeof(*stOut));
87 INTEGER_t *stIn = NULL;
88 asn_enc_rval_t er;
89 asn_dec_rval_t ret;
90 struct asn_oer_constraints_s empty_constraints = {{0,0,0}, {0,0,0}};
91 struct asn_oer_constraints_s *constraints = &empty_constraints;
92 struct asn_oer_constraint_s *ct_value = &constraints->value;
93
94 /* Setup integer constraints as requested */
95 if(lower_bound == NoBound && upper_bound == NoBound) {
96 constraints = NULL;
97 } else {
98 if(lower_bound != NoBound) {
99 ct_value->flags |= AOC_HAS_LOWER_BOUND;
100 ct_value->lower_bound = lower_bound;
101 }
102 if(upper_bound != NoBound) {
103 ct_value->flags |= AOC_HAS_UPPER_BOUND;
104 ct_value->upper_bound = upper_bound;
105 }
106 }
107
108 if(asn_imax2INTEGER(stOut, value) == -1) {
109 assert(!"Unreachable");
110 }
111
112 er = oer_encode_to_buffer(&asn_DEF_INTEGER, constraints, stOut, tmpbuf,
113 sizeof(tmpbuf));
114 if(er.encoded != -1) {
115 fprintf(stderr, "%d: oer encode failed for %s\n", lineno,
116 er.failed_type ? er.failed_type->name : "<none>");
117 assert(er.encoded != -1);
118 }
119 tmpbuf_size = er.encoded;
120
121 ret = asn_DEF_INTEGER.oer_decoder(0, &asn_DEF_INTEGER, constraints,
122 (void **)&stIn, tmpbuf, tmpbuf_size);
123 if(ret.code != RC_OK) {
124 /* Basic OER decode does not work */
125 fprintf(stderr, "%d: Failed oer_decode(value=%" PRIdMAX ", size=%zu)\n",
126 lineno, value, tmpbuf_size);
127 assert(ret.code == 0);
128 } else {
129 intmax_t outcome;
130 if(asn_INTEGER2imax(stIn, &outcome) != 0) {
131 /* Result of decode is structurally incorrect */
132 fprintf(stderr,
133 "%d: Failed to convert INTEGER 2 imax; structurally "
134 "incorrect INTEGER\n",
135 lineno);
136 assert(!"Unreachable");
137 } else if(outcome != value) {
138 /* Decoded value is wrong */
139 fprintf(stderr,
140 "%d: Decode result %" PRIdMAX " is not expected %" PRIdMAX
141 "\n",
142 lineno, outcome, value);
143 assert(outcome == value);
144 }
145 }
146
147 fprintf(stderr, "%d: Decode result %" PRIdMAX "\n", lineno, value);
148}
149
Lev Walkined3a4ae2017-07-07 10:09:51 -0700150int
151main() {
Lev Walkine4d8c922017-07-10 20:29:33 -0700152 CHECK_DECODE(RC_WMORE, 0, "", 0, "bounds=", NoBound, NoBound);
153 CHECK_DECODE(RC_FAIL, 0, "\x00", 1, "bounds=", NoBound, NoBound);
154 CHECK_DECODE(RC_WMORE, 0, "", 0, "bounds=", 0, 0);
155 CHECK_DECODE(RC_WMORE, 0, "", 0, "bounds=", 0, 1);
156 CHECK_DECODE(RC_OK, 0, "\x00", 1, "bounds=", 0, 1);
157 CHECK_DECODE(RC_OK, 0, "\x00", 1, "bounds=", -1, 1);
158
159 CHECK_DECODE(RC_OK, 0, "\x00", 1, "bounds=", -1, 1);
160 CHECK_DECODE(RC_OK, 1, "\x01", 1, "bounds=", -1, 1);
161 CHECK_DECODE(RC_OK, -1, "\xff", 1, "bounds=", -1, 1);
162 CHECK_DECODE(RC_OK, -1, "\xff", 1, "bounds=", -1, 1);
163 CHECK_DECODE(RC_OK, 127, "\x7f", 1, "bounds=", 0, 127);
164 CHECK_DECODE(RC_OK, 255, "\xff", 1, "bounds=", 0, 127);
165 CHECK_DECODE(RC_OK, 255, "\xff", 1, "bounds=", 0, 255);
166 CHECK_DECODE(RC_WMORE, 0, "\xff", 1, "bounds=", 0, 256);
167 CHECK_DECODE(RC_OK, 65535, "\xff\xff", 2, "bounds=", 0, 256);
168
169 CHECK_DECODE(RC_OK, 0, "\x01\x00", 2, "bounds=", NoBound, 1);
170 CHECK_DECODE(RC_OK, 1, "\x01\x01", 2, "bounds=", NoBound, 1);
171 CHECK_DECODE(RC_OK, -1, "\x1\xff", 2, "bounds=", NoBound, 1);
172 CHECK_DECODE(RC_OK, -1, "\x1\xff", 2, "bounds=", NoBound, 1);
173 CHECK_DECODE(RC_OK, -1, "\x1\xff", 2, "bounds=", NoBound, 255);
174 CHECK_DECODE(RC_WMORE, -1, "\x02\x00\xff", 2, "bounds=", NoBound, 200);
175 CHECK_DECODE(RC_OK, 255, "\x02\x00\xff", 3, "bounds=", NoBound, 200);
Lev Walkin486fd5c2017-07-10 22:08:14 -0700176
177 CHECK_ROUNDTRIP(0, NoBound, NoBound);
178 CHECK_ROUNDTRIP(1, NoBound, NoBound);
179 CHECK_ROUNDTRIP(1, -128, 127);
180
Lev Walkined3a4ae2017-07-07 10:09:51 -0700181}