blob: a2021808200d9086416ca8ab846d0b9b1784f91b [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 Walkin54f34512017-07-10 05:27:46 -07008
Lev Walkine4d8c922017-07-10 20:29:33 -07009static const intmax_t NoBound = -4200024;
10
11static void
12check_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) {
13 static char *code_s[] = { "RC_OK", "RC_WMORE", "RC_FAIL", "<error>" };
14
15 fprintf(stderr, "%d: buf[%d]={%d, %d, ...}\n", lineno, size,
16 ((const uint8_t *)buf)[0],
17 ((const uint8_t *)buf)[1]);
Lev Walkin54f34512017-07-10 05:27:46 -070018
19 INTEGER_t *st = NULL;
20 asn_dec_rval_t ret;
Lev Walkine4d8c922017-07-10 20:29:33 -070021 struct asn_oer_constraints_s empty_constraints = {{0,0,0}, {0,0,0}};
22 struct asn_oer_constraints_s *constraints = &empty_constraints;
23 struct asn_oer_constraint_s *ct_value = &constraints->value;
Lev Walkin54f34512017-07-10 05:27:46 -070024
Lev Walkine4d8c922017-07-10 20:29:33 -070025 /* Setup integer constraints as requested */
26 if(lower_bound == NoBound && upper_bound == NoBound) {
27 constraints = NULL;
28 } else {
29 if(lower_bound != NoBound) {
30 ct_value->flags |= AOC_HAS_LOWER_BOUND;
31 ct_value->lower_bound = lower_bound;
32 }
33 if(upper_bound != NoBound) {
34 ct_value->flags |= AOC_HAS_UPPER_BOUND;
35 ct_value->upper_bound = upper_bound;
36 }
37 }
38
39 (void)dummy;
40
41 ret = asn_DEF_INTEGER.oer_decoder(0, &asn_DEF_INTEGER, constraints,
42 (void **)&st, buf, size);
Lev Walkin54f34512017-07-10 05:27:46 -070043 if(ret.code != RC_OK) {
44 /* Basic OER decode does not work */
45 fprintf(stderr, "%d: Failed oer_decode(ctl=%" PRIdMAX ", size=%zu)\n",
46 lineno, control, size);
Lev Walkine4d8c922017-07-10 20:29:33 -070047 if(ret.code == code) {
48 fprintf(stderr, " (That was expected)\n");
49 return;
50 } else {
51 fprintf(
52 stderr, " Unexpected return code %s (%d) expected %s\n",
53 code_s[(unsigned)ret.code <= RC_FAIL ? RC_FAIL : (RC_FAIL + 1)],
54 (int)ret.code, code_s[code]);
55 assert(ret.code == code);
56 }
Lev Walkin54f34512017-07-10 05:27:46 -070057 } else {
58 intmax_t outcome;
59 if(asn_INTEGER2imax(st, &outcome) != 0) {
60 /* Result of decode is structurally incorrect */
Lev Walkine4d8c922017-07-10 20:29:33 -070061 fprintf(stderr,
62 "%d: Failed to convert INTEGER 2 imax; structurally "
63 "incorrect INTEGER\n",
Lev Walkin54f34512017-07-10 05:27:46 -070064 lineno);
65 assert(!"Unreachable");
66 } else if(outcome != control) {
67 /* Decoded value is wrong */
68 fprintf(stderr,
69 "%d: Decode result %" PRIdMAX " is not expected %" PRIdMAX
70 "\n",
71 lineno, outcome, control);
72 assert(outcome == control);
73 }
74 }
75
76 fprintf(stderr, "%d: Decode result %" PRIdMAX "\n", lineno, control);
Lev Walkined3a4ae2017-07-07 10:09:51 -070077}
78
79int
80main() {
Lev Walkine4d8c922017-07-10 20:29:33 -070081 CHECK_DECODE(RC_WMORE, 0, "", 0, "bounds=", NoBound, NoBound);
82 CHECK_DECODE(RC_FAIL, 0, "\x00", 1, "bounds=", NoBound, NoBound);
83 CHECK_DECODE(RC_WMORE, 0, "", 0, "bounds=", 0, 0);
84 CHECK_DECODE(RC_WMORE, 0, "", 0, "bounds=", 0, 1);
85 CHECK_DECODE(RC_OK, 0, "\x00", 1, "bounds=", 0, 1);
86 CHECK_DECODE(RC_OK, 0, "\x00", 1, "bounds=", -1, 1);
87
88 CHECK_DECODE(RC_OK, 0, "\x00", 1, "bounds=", -1, 1);
89 CHECK_DECODE(RC_OK, 1, "\x01", 1, "bounds=", -1, 1);
90 CHECK_DECODE(RC_OK, -1, "\xff", 1, "bounds=", -1, 1);
91 CHECK_DECODE(RC_OK, -1, "\xff", 1, "bounds=", -1, 1);
92 CHECK_DECODE(RC_OK, 127, "\x7f", 1, "bounds=", 0, 127);
93 CHECK_DECODE(RC_OK, 255, "\xff", 1, "bounds=", 0, 127);
94 CHECK_DECODE(RC_OK, 255, "\xff", 1, "bounds=", 0, 255);
95 CHECK_DECODE(RC_WMORE, 0, "\xff", 1, "bounds=", 0, 256);
96 CHECK_DECODE(RC_OK, 65535, "\xff\xff", 2, "bounds=", 0, 256);
97
98 CHECK_DECODE(RC_OK, 0, "\x01\x00", 2, "bounds=", NoBound, 1);
99 CHECK_DECODE(RC_OK, 1, "\x01\x01", 2, "bounds=", NoBound, 1);
100 CHECK_DECODE(RC_OK, -1, "\x1\xff", 2, "bounds=", NoBound, 1);
101 CHECK_DECODE(RC_OK, -1, "\x1\xff", 2, "bounds=", NoBound, 1);
102 CHECK_DECODE(RC_OK, -1, "\x1\xff", 2, "bounds=", NoBound, 255);
103 CHECK_DECODE(RC_WMORE, -1, "\x02\x00\xff", 2, "bounds=", NoBound, 200);
104 CHECK_DECODE(RC_OK, 255, "\x02\x00\xff", 3, "bounds=", NoBound, 200);
Lev Walkined3a4ae2017-07-07 10:09:51 -0700105}