Commit cc9b3aeb authored by Lev Walkin's avatar Lev Walkin

test OER encoding over the INTEGER value range

parent 527497f7
......@@ -145,7 +145,7 @@ asn_enc_rval_t
INTEGER_encode_oer(asn_TYPE_descriptor_t *td,
asn_oer_constraints_t *constraints, void *sptr,
asn_app_consume_bytes_f *cb, void *app_key) {
const INTEGER_t *st = *(const INTEGER_t **)sptr;
const INTEGER_t *st = sptr;
asn_enc_rval_t er;
asn_oer_constraint_t *ct;
const uint8_t *buf;
......@@ -170,19 +170,25 @@ INTEGER_encode_oer(asn_TYPE_descriptor_t *td,
sign = (buf && buf < end) ? buf[0] & 0x80 : 0;
if(encode_as_unsigned && sign) {
/* The value given is a signed value. Can't proceed. */
ASN__ENCODE_FAILED;
}
/* Ignore 9 leading zeroes or ones */
for(; buf + 1 < end; buf++) {
if(buf[0] == 0x0 && ((buf[1] & 0x80) == 0 || encode_as_unsigned)) {
continue;
} else if(buf[0] == 0xff && (buf[1] & 0x80) != 0) {
continue;
if(encode_as_unsigned) {
if(sign) {
/* The value given is a signed value. Can't proceed. */
ASN__ENCODE_FAILED;
}
/* Remove leading zeros. */
for(; buf + 1 < end; buf++) {
if(buf[0] != 0x0) break;
}
} else {
for(; buf + 1 < end; buf++) {
if(buf[0] == 0x0 && (buf[1] & 0x80) == 0) {
continue;
} else if(buf[0] == 0xff && (buf[1] & 0x80) != 0) {
continue;
}
break;
}
break;
}
useful_bytes = end - buf;
......
......@@ -58,13 +58,21 @@ oer_encode_to_buffer(struct asn_TYPE_descriptor_s *type_descriptor,
arg.buffer = buffer;
arg.left = buffer_size;
ec = type_descriptor->oer_encoder(
type_descriptor, constraints,
struct_ptr, /* Pointer to the destination structure */
encode_to_buffer_cb, &arg);
if(ec.encoded != -1) {
assert(ec.encoded == (ssize_t)(buffer_size - arg.left));
/* Return the encoded contents size */
if(type_descriptor->oer_encoder == NULL) {
ec.encoded = -1;
ec.failed_type = type_descriptor;
ec.structure_ptr = struct_ptr;
ASN_DEBUG("OER encoder is not defined for %s",
type_descriptor->name);
} else {
ec = type_descriptor->oer_encoder(
type_descriptor, constraints,
struct_ptr, /* Pointer to the destination structure */
encode_to_buffer_cb, &arg);
if(ec.encoded != -1) {
assert(ec.encoded == (ssize_t)(buffer_size - arg.left));
/* Return the encoded contents size */
}
}
return ec;
}
......@@ -5,26 +5,25 @@
#include <INTEGER.h>
#define CHECK_DECODE(code, a, b, c, d, e, f) check_decode(__LINE__, code, a, b, c, d, e, f)
#define CHECK_ROUNDTRIP(a, b, c) check_roundtrip(__LINE__, a, b, c);
#define CHECK_ROUNDTRIP(a, b, c) check_roundtrip(__LINE__, a, b, c);
#define CHECK_ENCODE_OK(a, b, c) check_encode(__LINE__, 0, a, b, c)
#define CHECK_ENCODE_BAD(a, b, c) check_encode(__LINE__, 1, a, b, c);
static const intmax_t NoBound = -4200024;
static void
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) {
static char *code_s[] = { "RC_OK", "RC_WMORE", "RC_FAIL", "<error>" };
fprintf(stderr, "%d: buf[%zu]={%d, %d, ...}\n", lineno, size,
((const uint8_t *)buf)[0],
((const uint8_t *)buf)[1]);
INTEGER_t *st = NULL;
asn_dec_rval_t ret;
struct asn_oer_constraints_s empty_constraints = {{0,0,0}, {0,0,0}};
static asn_oer_constraints_t *
setup_constraints(int lineno, const char *process, intmax_t lower_bound,
intmax_t upper_bound) {
static struct asn_oer_constraints_s empty_constraints;
struct asn_oer_constraints_s *constraints = &empty_constraints;
struct asn_oer_constraint_s *ct_value = &constraints->value;
memset(&empty_constraints, 0, sizeof(empty_constraints));
/* Setup integer constraints as requested */
if(lower_bound == NoBound && upper_bound == NoBound) {
fprintf(stderr, "%d: OER %s without constraints\n", lineno,
process);
constraints = NULL;
} else {
if(lower_bound != NoBound) {
......@@ -35,8 +34,41 @@ check_decode(int lineno, enum asn_dec_rval_code_e code, intmax_t control, const
ct_value->flags |= AOC_HAS_UPPER_BOUND;
ct_value->upper_bound = upper_bound;
}
if(lower_bound != NoBound && upper_bound != NoBound) {
fprintf(stderr,
"%d: OER %s constraints %" PRIdMAX
"..%" PRIdMAX "\n",
lineno, process, lower_bound, upper_bound);
} else if(lower_bound != NoBound) {
fprintf(stderr,
"%d: OER %s constraints %" PRIdMAX
"..MAX\n",
lineno, process, lower_bound);
} else if(upper_bound != NoBound) {
fprintf(stderr,
"%d: OER %s constraints MIN..%" PRIdMAX "\n",
lineno, process, upper_bound);
}
}
return constraints;
}
static void
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) {
static char *code_s[] = { "RC_OK", "RC_WMORE", "RC_FAIL", "<error>" };
fprintf(stderr, "\n%d: OER decode (control %" PRIdMAX ")\n", lineno, control);
INTEGER_t *st = NULL;
asn_dec_rval_t ret;
asn_oer_constraints_t *constraints =
setup_constraints(lineno, "decoding", lower_bound, upper_bound);
fprintf(stderr, "%d: buf[%zu]={%d, %d, ...}\n", lineno, size,
((const uint8_t *)buf)[0],
((const uint8_t *)buf)[1]);
(void)dummy;
ret = asn_DEF_INTEGER.oer_decoder(0, &asn_DEF_INTEGER, constraints,
......@@ -77,46 +109,51 @@ check_decode(int lineno, enum asn_dec_rval_code_e code, intmax_t control, const
fprintf(stderr, "%d: Decode result %" PRIdMAX "\n", lineno, control);
}
static void
dump_data(int lineno, const uint8_t *buf, size_t size) {
const uint8_t *p = buf;
const uint8_t *end = buf + size;
fprintf(stderr, "%d: Encoded: [", lineno);
for(; p < end; p++) {
fprintf(stderr, "\\x%02x", *(const unsigned char *)p);
}
fprintf(stderr, "] (%zu bytes)\n", size);
}
static void
check_roundtrip(int lineno, intmax_t value, intmax_t lower_bound, intmax_t upper_bound) {
uint8_t tmpbuf[32];
size_t tmpbuf_size;
fprintf(stderr, "\n%d: OER round-trip value %" PRIdMAX "\n", lineno, value);
INTEGER_t *stOut = (INTEGER_t *)calloc(1, sizeof(*stOut));
INTEGER_t *stIn = NULL;
asn_enc_rval_t er;
asn_dec_rval_t ret;
struct asn_oer_constraints_s empty_constraints = {{0,0,0}, {0,0,0}};
struct asn_oer_constraints_s *constraints = &empty_constraints;
struct asn_oer_constraint_s *ct_value = &constraints->value;
/* Setup integer constraints as requested */
if(lower_bound == NoBound && upper_bound == NoBound) {
constraints = NULL;
} else {
if(lower_bound != NoBound) {
ct_value->flags |= AOC_HAS_LOWER_BOUND;
ct_value->lower_bound = lower_bound;
}
if(upper_bound != NoBound) {
ct_value->flags |= AOC_HAS_UPPER_BOUND;
ct_value->upper_bound = upper_bound;
}
}
asn_oer_constraints_t *constraints =
setup_constraints(lineno, "encoding", lower_bound, upper_bound);
if(asn_imax2INTEGER(stOut, value) == -1) {
assert(!"Unreachable");
assert(!"Unreachable imax2INTEGER failure");
} else {
assert(stOut->buf != NULL);
assert(stOut->size != 0);
}
er = oer_encode_to_buffer(&asn_DEF_INTEGER, constraints, stOut, tmpbuf,
sizeof(tmpbuf));
if(er.encoded != -1) {
fprintf(stderr, "%d: oer encode failed for %s\n", lineno,
if(er.encoded == -1) {
fprintf(stderr, "%d: OER encode failed for %s\n", lineno,
er.failed_type ? er.failed_type->name : "<none>");
assert(er.encoded != -1);
}
tmpbuf_size = er.encoded;
ASN_STRUCT_FREE(asn_DEF_INTEGER, stOut);
dump_data(lineno, tmpbuf, tmpbuf_size);
ret = asn_DEF_INTEGER.oer_decoder(0, &asn_DEF_INTEGER, constraints,
(void **)&stIn, tmpbuf, tmpbuf_size);
......@@ -144,9 +181,46 @@ check_roundtrip(int lineno, intmax_t value, intmax_t lower_bound, intmax_t upper
}
}
ASN_STRUCT_FREE(asn_DEF_INTEGER, stIn);
fprintf(stderr, "%d: Decode result %" PRIdMAX "\n", lineno, value);
}
static void
check_encode(int lineno, int bad, intmax_t value, intmax_t lower_bound, intmax_t upper_bound) {
uint8_t tmpbuf[32];
fprintf(stderr, "\n%d: OER encode value %" PRIdMAX "\n", lineno, value);
INTEGER_t *stOut = (INTEGER_t *)calloc(1, sizeof(*stOut));
asn_enc_rval_t er;
asn_oer_constraints_t *constraints =
setup_constraints(lineno, "encoding", lower_bound, upper_bound);
if(asn_imax2INTEGER(stOut, value) == -1) {
assert(!"Unreachable imax2INTEGER failure");
} else {
assert(stOut->buf != NULL);
assert(stOut->size != 0);
}
er = oer_encode_to_buffer(&asn_DEF_INTEGER, constraints, stOut, tmpbuf,
sizeof(tmpbuf));
if(er.encoded == -1) {
fprintf(stderr, "%d: OER encode failed for %s%s\n", lineno,
er.failed_type ? er.failed_type->name : "<none>",
bad ? " (expected)" : "");
if(!bad) {
assert(er.encoded != -1);
}
} else {
dump_data(lineno, tmpbuf, er.encoded);
if(bad) {
assert(er.encoded == -1);
}
}
ASN_STRUCT_FREE(asn_DEF_INTEGER, stOut);
}
int
main() {
CHECK_DECODE(RC_WMORE, 0, "", 0, "bounds=", NoBound, NoBound);
......@@ -176,6 +250,69 @@ main() {
CHECK_ROUNDTRIP(0, NoBound, NoBound);
CHECK_ROUNDTRIP(1, NoBound, NoBound);
CHECK_ROUNDTRIP(-1, NoBound, NoBound);
CHECK_ROUNDTRIP(-65000, NoBound, NoBound);
CHECK_ROUNDTRIP(65000, NoBound, NoBound);
CHECK_ROUNDTRIP(65535, NoBound, NoBound);
CHECK_ROUNDTRIP(-65535, NoBound, NoBound);
CHECK_ROUNDTRIP(-65536, NoBound, NoBound);
CHECK_ROUNDTRIP(65536, NoBound, NoBound);
CHECK_ROUNDTRIP(0, -128, 127);
CHECK_ROUNDTRIP(1, -128, 127);
CHECK_ROUNDTRIP(-1, -128, 127);
CHECK_ROUNDTRIP(-127, -128, 127);
CHECK_ROUNDTRIP(-128, -128, 127);
CHECK_ROUNDTRIP(127, -128, 127);
CHECK_ROUNDTRIP(1, 32000, 32000); /* Sic! */
CHECK_ROUNDTRIP(32000, 0, 32000); /* Sic! */
CHECK_ROUNDTRIP(32000, -32000, 32000); /* Sic! */
CHECK_ROUNDTRIP(1, 65000, 65000); /* Sic! */
CHECK_ROUNDTRIP(65535, 0, 65000); /* Sic! */
CHECK_ROUNDTRIP(65535, -65000, 65000); /* Sic! */
CHECK_ENCODE_OK(0, 0, 255);
CHECK_ENCODE_OK(255, 0, 255);
CHECK_ENCODE_BAD(256, 0, 255);
CHECK_ENCODE_OK(0, -128, 127);
CHECK_ENCODE_OK(127, -128, 127);
CHECK_ENCODE_OK(-128, -128, 127);
CHECK_ENCODE_BAD(-129, -128, 127);
CHECK_ENCODE_BAD(128, -128, 127);
CHECK_ENCODE_OK(-4900000000, -5000000000, 5000000000);
CHECK_ENCODE_OK(4900000000, -5000000000, 5000000000);
CHECK_ENCODE_OK(-2000000000, -4000000000, 0);
CHECK_ENCODE_OK(-4000000000, -4000000000, 0);
CHECK_ENCODE_BAD(-4900000000, 0, 4000000000);
CHECK_ENCODE_BAD(-1, 0, 4000000000);
CHECK_ENCODE_BAD(4900000000, 0, 4000000000);
CHECK_ENCODE_OK(4100000000, 0, 4000000000); /* Sic! */
for(size_t i = 0; i < 7 ; i++) {
intmax_t value = (intmax_t)1 << i;
CHECK_ROUNDTRIP(value, 0, 255);
value = -value;
CHECK_ROUNDTRIP(value, -128, 127);
}
for(size_t i = 0; i < 16 ; i++) {
intmax_t value = (intmax_t)1 << i;
CHECK_ROUNDTRIP(value, 0, 65535);
value = -value;
CHECK_ROUNDTRIP(value, -32768, 32767);
}
for(size_t i = 0; i < 32 ; i++) {
intmax_t value = (intmax_t)1 << i;
CHECK_ROUNDTRIP(value, 0, 4294967296UL);
value = -value;
CHECK_ROUNDTRIP(value, -2147483648L, 2147483647L);
}
for(size_t i = 0; i < 8 * sizeof(intmax_t) ; i++) {
intmax_t value = (intmax_t)1 << i;
CHECK_ROUNDTRIP(value, NoBound, NoBound);
value = -value;
CHECK_ROUNDTRIP(value, NoBound, NoBound);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment