Lev Walkin | ed3a4ae | 2017-07-07 10:09:51 -0700 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <assert.h> |
| 3 | |
Lev Walkin | e4d8c92 | 2017-07-10 20:29:33 -0700 | [diff] [blame^] | 4 | #include <asn_codecs.h> |
Lev Walkin | ed3a4ae | 2017-07-07 10:09:51 -0700 | [diff] [blame] | 5 | #include <INTEGER.h> |
Lev Walkin | ed3a4ae | 2017-07-07 10:09:51 -0700 | [diff] [blame] | 6 | |
Lev Walkin | e4d8c92 | 2017-07-10 20:29:33 -0700 | [diff] [blame^] | 7 | #define CHECK_DECODE(code, a, b, c, d, e, f) check_decode(__LINE__, code, a, b, c, d, e, f) |
Lev Walkin | 54f3451 | 2017-07-10 05:27:46 -0700 | [diff] [blame] | 8 | |
Lev Walkin | e4d8c92 | 2017-07-10 20:29:33 -0700 | [diff] [blame^] | 9 | static const intmax_t NoBound = -4200024; |
| 10 | |
| 11 | static void |
| 12 | check_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 Walkin | 54f3451 | 2017-07-10 05:27:46 -0700 | [diff] [blame] | 18 | |
| 19 | INTEGER_t *st = NULL; |
| 20 | asn_dec_rval_t ret; |
Lev Walkin | e4d8c92 | 2017-07-10 20:29:33 -0700 | [diff] [blame^] | 21 | 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 Walkin | 54f3451 | 2017-07-10 05:27:46 -0700 | [diff] [blame] | 24 | |
Lev Walkin | e4d8c92 | 2017-07-10 20:29:33 -0700 | [diff] [blame^] | 25 | /* 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 Walkin | 54f3451 | 2017-07-10 05:27:46 -0700 | [diff] [blame] | 43 | 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 Walkin | e4d8c92 | 2017-07-10 20:29:33 -0700 | [diff] [blame^] | 47 | 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 Walkin | 54f3451 | 2017-07-10 05:27:46 -0700 | [diff] [blame] | 57 | } else { |
| 58 | intmax_t outcome; |
| 59 | if(asn_INTEGER2imax(st, &outcome) != 0) { |
| 60 | /* Result of decode is structurally incorrect */ |
Lev Walkin | e4d8c92 | 2017-07-10 20:29:33 -0700 | [diff] [blame^] | 61 | fprintf(stderr, |
| 62 | "%d: Failed to convert INTEGER 2 imax; structurally " |
| 63 | "incorrect INTEGER\n", |
Lev Walkin | 54f3451 | 2017-07-10 05:27:46 -0700 | [diff] [blame] | 64 | 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 Walkin | ed3a4ae | 2017-07-07 10:09:51 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | int |
| 80 | main() { |
Lev Walkin | e4d8c92 | 2017-07-10 20:29:33 -0700 | [diff] [blame^] | 81 | 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 Walkin | ed3a4ae | 2017-07-07 10:09:51 -0700 | [diff] [blame] | 105 | } |