Commit edfc7e7f authored by v0-e's avatar v0-e

copy: Add tests

parent c19e515f
...@@ -187,6 +187,16 @@ compile_and_test() { ...@@ -187,6 +187,16 @@ compile_and_test() {
return 3 return 3
fi fi
echo "Checking random data copy"
copy_check_cmd="${ASAN_ENV_FLAGS} ./random-test-driver -s ${rmax} ${encodings} -y"
echo "(${reproduce_make} && ${copy_check_cmd})" > .test-reproduce
if eval "$copy_check_cmd"; then
echo "Copy test OK"
else
{ echo "RETRY:"; cat .test-reproduce ; }
return 3
fi
echo "Generating new random data" echo "Generating new random data"
rm -rf random-data rm -rf random-data
cmd="${ASAN_ENV_FLAGS} UBSAN_OPTIONS=print_stacktrace=1" cmd="${ASAN_ENV_FLAGS} UBSAN_OPTIONS=print_stacktrace=1"
......
...@@ -327,12 +327,85 @@ check_random_roundtrip(enum asn_transfer_syntax syntax, size_t max_random_value_ ...@@ -327,12 +327,85 @@ check_random_roundtrip(enum asn_transfer_syntax syntax, size_t max_random_value_
enc.name); enc.name);
} }
static void
check_copy_op(enum asn_transfer_syntax syntax, size_t max_random_value_size, int iterations, int debug) {
struct encoding_map enc;
for(size_t i = 0; i < sizeof(encodings)/sizeof(encodings[0]); i++) {
enc = encodings[i];
if(enc.syntax == syntax) {
fprintf(stderr, "Testing %d iterations of round-trip for %s\n",
iterations, enc.name);
break;
}
}
for(int i = 0; i < iterations; i++) {
T_t *structure = 0;
T_t *new_structure = 0;
if(asn_random_fill(&asn_DEF_T, (void **)&structure,
max_random_value_size)
== -1) {
assert(structure == 0);
fprintf(stderr, "Can't generate %d'th value, skipping\n", i);
continue;
}
assert(structure != 0);
if(debug) {
fprintf(stderr, "Random structure %s:\n",
sizeof(ASN1_STR) > 60 ? "T" : ASN1_STR);
asn_fprint(stderr, &asn_DEF_T, structure);
xer_fprint(stderr, &asn_DEF_T, structure);
}
int cr = asn_DEF_T.op->copy_struct(&asn_DEF_T, (void**)&new_structure,
structure);
if(cr != 0) {
fprintf(stderr, "Copying structure %s:\n",
sizeof(ASN1_STR) > 60 ? "T" : ASN1_STR);
asn_fprint(stderr, &asn_DEF_T, structure);
assert(cr == 0);
exit(EX_SOFTWARE);
}
assert(new_structure != 0);
/*
* Confirm that we decoded the same data.
*/
int cmp = asn_DEF_T.op->compare_struct(&asn_DEF_T, structure,
new_structure);
if(cmp != 0 || debug) {
fprintf(stderr, "Random %s value:\n", ASN1_STR);
asn_fprint(stderr, &asn_DEF_T, structure);
xer_fprint(stderr, &asn_DEF_T, structure);
fprintf(stderr, "Copied %s value:\n", ASN1_STR);
asn_fprint(stderr, &asn_DEF_T, new_structure);
xer_fprint(stderr, &asn_DEF_T, new_structure);
assert(cmp == 0);
}
ASN_STRUCT_FREE(asn_DEF_T, structure);
ASN_STRUCT_FREE(asn_DEF_T, new_structure);
if(i < 5) {
fprintf(stderr, "[%03d] copy OK\n", i);
} else if(i == 5) {
fprintf(stderr, "... and so on\n");
}
}
fprintf(stderr, "OK %d iterations of copy-op for %s\n", iterations,
enc.name);
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
uint32_t enabled_encodings = 0; uint32_t enabled_encodings = 0;
enum { enum {
MODE_UNKNOWN, MODE_UNKNOWN,
MODE_GENERATE_RANDOM_DATA, MODE_GENERATE_RANDOM_DATA,
MODE_CHECK_RANDOM_ROUNDTRIP MODE_CHECK_RANDOM_ROUNDTRIP,
MODE_CHECK_COPY_OP
} mode = MODE_UNKNOWN; } mode = MODE_UNKNOWN;
const char *generate_into_dir = NULL; const char *generate_into_dir = NULL;
int iterations = 100; int iterations = 100;
...@@ -340,7 +413,7 @@ int main(int argc, char **argv) { ...@@ -340,7 +413,7 @@ int main(int argc, char **argv) {
int debug = 0; int debug = 0;
int c; int c;
while((c = getopt(argc, argv, "cde:g:hn:s:")) != -1) { while((c = getopt(argc, argv, "cde:g:hn:s:y")) != -1) {
switch(c) { switch(c) {
case 'c': case 'c':
mode = MODE_CHECK_RANDOM_ROUNDTRIP; mode = MODE_CHECK_RANDOM_ROUNDTRIP;
...@@ -377,6 +450,9 @@ int main(int argc, char **argv) { ...@@ -377,6 +450,9 @@ int main(int argc, char **argv) {
} }
max_random_value_size = atoi(optarg); max_random_value_size = atoi(optarg);
break; break;
case 'y':
mode = MODE_CHECK_COPY_OP;
break;
default: default:
usage(argv[0]); usage(argv[0]);
exit(2); exit(2);
...@@ -408,6 +484,10 @@ int main(int argc, char **argv) { ...@@ -408,6 +484,10 @@ int main(int argc, char **argv) {
check_random_roundtrip(syntax, max_random_value_size, check_random_roundtrip(syntax, max_random_value_size,
iterations, debug); iterations, debug);
break; break;
case MODE_CHECK_COPY_OP:
check_copy_op(syntax, max_random_value_size,
iterations, debug);
break;
} }
} }
} }
......
...@@ -4,6 +4,7 @@ check_PROGRAMS = \ ...@@ -4,6 +4,7 @@ check_PROGRAMS = \
check-ber_tlv_tag \ check-ber_tlv_tag \
check-length \ check-length \
check-bits \ check-bits \
check-copy \
check-OIDs \ check-OIDs \
check-GeneralizedTime \ check-GeneralizedTime \
check-OCTET_STRING \ check-OCTET_STRING \
...@@ -27,6 +28,7 @@ check_PROGRAMS += \ ...@@ -27,6 +28,7 @@ check_PROGRAMS += \
check-32-ber_tlv_tag \ check-32-ber_tlv_tag \
check-32-length \ check-32-length \
check-32-bits \ check-32-bits \
check-32-copy \
check-32-OIDs \ check-32-OIDs \
check-32-GeneralizedTime \ check-32-GeneralizedTime \
check-32-OCTET_STRING \ check-32-OCTET_STRING \
...@@ -54,6 +56,9 @@ check_32_length_SOURCES=check-length.c ...@@ -54,6 +56,9 @@ check_32_length_SOURCES=check-length.c
check_32_bits_CFLAGS=$(CFLAGS_M32) check_32_bits_CFLAGS=$(CFLAGS_M32)
check_32_bits_LDADD=$(LDADD_32) check_32_bits_LDADD=$(LDADD_32)
check_32_bits_SOURCES=check-bits.c check_32_bits_SOURCES=check-bits.c
check_32_copy_CFLAGS=$(CFLAGS_M32)
check_32_copy_LDADD=$(LDADD_32)
check_32_copy_SOURCES=check-copy.c
check_32_OIDs_CFLAGS=$(CFLAGS_M32) check_32_OIDs_CFLAGS=$(CFLAGS_M32)
check_32_OIDs_LDADD=$(LDADD_32) check_32_OIDs_LDADD=$(LDADD_32)
check_32_OIDs_SOURCES=check-OIDs.c check_32_OIDs_SOURCES=check-OIDs.c
......
#include <stdio.h>
#include <assert.h>
#include <OCTET_STRING.c>
#include <BIT_STRING.c>
#include <INTEGER.c>
#include <BOOLEAN.c>
#include <NULL.c>
#include <REAL.c>
#define check(td, a, b) test(__LINE__, td, a, b)
static int
copy(asn_TYPE_descriptor_t *td, void **a, const void *b) {
return asn_copy(td, a, b) != 0;
}
static int
compare(asn_TYPE_descriptor_t *td, void *a, void *b) {
return td->op->compare_struct(td, a, b) != 0;
}
static int
test(int lineno, asn_TYPE_descriptor_t *td, void **a, void *b) {
int rv;
rv = copy(td, a, b);
if(rv) {
fprintf(stderr, "%03d: copy() failed\n", lineno);
assert(rv == 0);
}
rv = compare(td, *a, b);
if(rv) {
fprintf(stderr, "%03d: compare() failed\n", lineno);
assert(rv == 0);
}
ASN_STRUCT_FREE(*td, *a);
ASN_STRUCT_FREE_CONTENTS_ONLY(*td, b);
return 0;
}
int
main(int ac, char **av) {
(void)ac;
(void)av;
/* OCTET STRING */
{
OCTET_STRING_t b = { 0 };
OCTET_STRING_fromBuf(&b, "Hello", 5);
OCTET_STRING_t* a = NULL;
check(&asn_DEF_OCTET_STRING, (void**)&a, &b);
}
/* INTEGER */
{
INTEGER_t b = { 0 };
asn_ulong2INTEGER(&b, 123);
INTEGER_t* a = NULL;
check(&asn_DEF_INTEGER, (void**)&a, &b);
}
{
INTEGER_t b = { 0 };
INTEGER_t* a = NULL;
check(&asn_DEF_INTEGER, (void**)&a, &b);
}
/* BIT STRING */
{
BIT_STRING_t b = { 0 };
b.buf = MALLOC(2);
b.size = 2;
b.buf[0] = 0x80;
b.buf[1] = 0x02;
b.bits_unused = 1;
BIT_STRING_t* a = NULL;
check(&asn_DEF_BIT_STRING, (void**)&a, &b);
}
/* BOOLEAN */
{
BOOLEAN_t b = { 0 };
b = 1;
BIT_STRING_t* a = NULL;
check(&asn_DEF_BOOLEAN, (void**)&a, &b);
}
/* NULL */
{
NULL_t b = { 0 };
NULL_t* a = NULL;
check(&asn_DEF_NULL, (void**)&a, &b);
}
/* REAL */
{
REAL_t b = { 0 };
asn_double2REAL(&b, 123.456);
REAL_t* a = NULL;
check(&asn_DEF_REAL, (void**)&a, &b);
}
return 0;
}
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