Commit 1e609cfb authored by Lev Walkin's avatar Lev Walkin

ANY/OCTET STRING/BIT STRING testing, plus numerous test cases

parent 0cb0fc52
......@@ -11,7 +11,12 @@ else
TESTS = check-*.c
endif
EXTRA_DIST = $(check_SCRIPTS) check-*.c*
EXTRA_DIST = $(check_SCRIPTS) check-*.c* data-*/*.*
clean:
for t in test-*; do rm -rf $$t; done
rm -rf test-*
SUFFIXES = .xbr .ber
.xbr.ber:
../enber $< > $@
......@@ -160,10 +160,12 @@ check_SCRIPTS = check-assembly.sh
TESTS_ENVIRONMENT = CFLAGS="${CFLAGS}" CXXFLAGS="${CXXFLAGS}" ./check-assembly.sh
@CPLUSPLUS_FOUND_FALSE@TESTS = check-*.c
@CPLUSPLUS_FOUND_TRUE@TESTS = check-*.c check-*.cc ## Include C++ test filed (.cc)
EXTRA_DIST = $(check_SCRIPTS) check-*.c*
EXTRA_DIST = $(check_SCRIPTS) check-*.c* data-*/*.*
SUFFIXES = .xbr .ber
all: all-am
.SUFFIXES:
.SUFFIXES: .xbr .ber
$(srcdir)/Makefile.in: @MAINTAINER_MODE_TRUE@ $(srcdir)/Makefile.am $(am__configure_deps)
@for dep in $?; do \
case '$(am__configure_deps)' in \
......@@ -284,6 +286,7 @@ check-TESTS: $(TESTS)
else :; fi
distdir: $(DISTFILES)
$(mkdir_p) $(distdir)/data-*
@srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`; \
topsrcdirstrip=`echo "$(top_srcdir)" | sed 's|.|.|g'`; \
list='$(DISTFILES)'; for file in $$list; do \
......@@ -396,7 +399,10 @@ uninstall-am: uninstall-info-am
clean:
for t in test-*; do rm -rf $$t; done
rm -rf test-*
.xbr.ber:
../enber $< > $@
# Tell versions [3.59,3.63) of GNU make to not export all variables.
# Otherwise a system limit (for SysV at least) may be exceeded.
.NOEXPORT:
#undef NDEBUG
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h> /* for chdir(2) */
#include <string.h>
#include <dirent.h>
#include <assert.h>
#include <errno.h>
#include <T.h>
enum expectation {
EXP_OK, /* Encoding/decoding must succeed */
EXP_BROKEN, /* Decoding must fail */
EXP_RECLESS, /* Reconstruction is allowed to yield less data */
};
static unsigned char buf[4096];
static int buf_offset;
static int
_buf_writer(const void *buffer, size_t size, void *app_key) {
unsigned char *b, *bend;
(void)app_key;
assert(buf_offset + size < sizeof(buf));
memcpy(buf + buf_offset, buffer, size);
b = buf + buf_offset;
bend = b + size;
printf("=> [");
for(; b < bend; b++)
printf(" %02X", *b);
printf("]:%ld\n", (long)size);
buf_offset += size;
return 0;
}
static int
save_object(T_t *st) {
asn_enc_rval_t rval; /* Return value */
buf_offset = 0;
rval = der_encode(&asn1_DEF_T, st, _buf_writer, 0);
if (rval.encoded == -1) {
fprintf(stderr,
"Cannot encode %s: %s\n",
rval.failed_type->name, strerror(errno));
assert(rval.encoded != -1);
return -1; /* JIC */
}
fprintf(stderr, "SAVED OBJECT IN SIZE %d\n", buf_offset);
return 0;
}
static T_t *
load_object(enum expectation expectation, char *fbuf, int size) {
ber_dec_rval_t rval;
T_t *st = 0;
int csize;
fprintf(stderr, "LOADING OBJECT OF SIZE %d\n", size);
/* Perform multiple iterations with multiple chunks sizes */
for(csize = 1; csize < 20; csize += 1) {
int fbuf_offset = 0;
int fbuf_left = size;
int fbuf_chunk = csize;
if(st) asn1_DEF_T.free_struct(&asn1_DEF_T, st, 0);
st = 0;
do {
fprintf(stderr, "Decoding from %d with %d (left %d)\n",
fbuf_offset, fbuf_chunk, fbuf_left);
rval = ber_decode(&asn1_DEF_T, (void **)&st,
fbuf + fbuf_offset,
fbuf_chunk < fbuf_left
? fbuf_chunk : fbuf_left);
fbuf_offset += rval.consumed;
fbuf_left -= rval.consumed;
if(rval.code == RC_WMORE)
fbuf_chunk += 1; /* Give little more */
else
fbuf_chunk = csize; /* Back off */
} while(fbuf_left && rval.code == RC_WMORE);
if(expectation != EXP_BROKEN) {
assert(rval.code == RC_OK);
} else {
assert(rval.code != RC_OK);
fprintf(stderr, "Failed, but this was expected\n");
asn1_DEF_T.free_struct(&asn1_DEF_T, st, 0);
st = 0; /* ignore leak for now */
}
}
if(st) asn_fprint(stderr, &asn1_DEF_T, st);
return st;
}
static void
process_data(enum expectation expectation, char *fbuf, int size) {
T_t *st;
int ret;
st = load_object(expectation, fbuf, size);
if(!st) return;
ret = save_object(st);
assert(buf_offset < sizeof(buf));
assert(ret == 0);
if(expectation == EXP_RECLESS) {
assert(buf_offset > 0 && buf_offset < size);
assert(memcmp(buf + 2, fbuf + 2, buf_offset - 2) == 0);
} else {
assert(buf_offset == size);
assert(memcmp(buf, fbuf, buf_offset) == 0);
}
asn1_DEF_T.free_struct(&asn1_DEF_T, st, 0);
}
/*
* Decode the .der files and try to regenerate them.
*/
static int
process(const char *fname) {
char fbuf[4096];
char *ext = strrchr(fname, '.');
enum expectation expectation;
int ret;
int rd;
FILE *fp;
if(ext == 0 || strcmp(ext, ".ber"))
return 0;
switch(ext[-1]) {
case 'B': /* The file is intentionally broken */
expectation = EXP_BROKEN; break;
case 'L': /* Extensions are present */
expectation = EXP_RECLESS; break;
default:
expectation = EXP_OK; break;
}
fprintf(stderr, "\nProcessing file [../%s]\n", fname);
ret = chdir("../data-62");
assert(ret == 0);
fp = fopen(fname, "r");
ret = chdir("../test-check-62");
assert(ret == 0);
assert(fp);
rd = fread(fbuf, 1, sizeof(fbuf), fp);
fclose(fp);
assert(rd < sizeof(fbuf)); /* expect small files */
process_data(expectation, fbuf, rd);
return 1;
}
int
main() {
DIR *dir;
struct dirent *dent;
int processed_files = 0;
dir = opendir("../data-62");
assert(dir);
while((dent = readdir(dir))) {
if(strncmp(dent->d_name, "data-62-", 8))
continue;
if(process(dent->d_name))
processed_files++;
}
assert(processed_files);
closedir(dir);
return 0;
}
0 test
\ No newline at end of file
<C T="[UNIVERSAL 16]" TL="2" V="11">
<P T="[UNIVERSAL 2]" TL="2" V="1">&x05;</P>
<C T="[1]" TL="2" V="6">
<P T="[UNIVERSAL 19]" TL="2" V="4">test</P>
</C>
</C>
0
test
\ No newline at end of file
<C T="[UNIVERSAL 16]" TL="2" V="10">
<P T="[UNIVERSAL 2]" TL="2" V="1">&x05;</P>
<C T="[1]" TL="2" V="6">
<P T="[UNIVERSAL 19]" TL="2" V="4">test</P>
</C>
</C>
0 test
\ No newline at end of file
<C T="[UNIVERSAL 16]" TL="2" V="9">
<P T="[UNIVERSAL 2]" TL="2" V="1">&x05;</P>
<C T="[1]" TL="2" V="6">
<P T="[UNIVERSAL 19]" TL="2" V="4">test</P>
</C>
</C>
0test
\ No newline at end of file
<C T="[UNIVERSAL 16]" TL="2" V="8">
<P T="[UNIVERSAL 2]" TL="2" V="1">&x05;</P>
<C T="[1]" TL="2" V="6">
<P T="[UNIVERSAL 19]" TL="2" V="4">test</P>
</C>
</C>
0test
\ No newline at end of file
<C T="[UNIVERSAL 16]" TL="2" V="7">
<P T="[UNIVERSAL 2]" TL="2" V="1">&x05;</P>
<C T="[1]" TL="2" V="6">
<P T="[UNIVERSAL 19]" TL="2" V="4">test</P>
</C>
</C>
0test
\ No newline at end of file
<C T="[UNIVERSAL 16]" TL="2" V="6">
<P T="[UNIVERSAL 2]" TL="2" V="1">&x05;</P>
<C T="[1]" TL="2" V="6">
<P T="[UNIVERSAL 19]" TL="2" V="4">test</P>
</C>
</C>
0 test
\ No newline at end of file
<C T="[UNIVERSAL 16]" TL="2" V="12">
<P T="[UNIVERSAL 2]" TL="2" V="1">&x05;</P>
<C T="[1]" TL="2" V="6">
<P T="[UNIVERSAL 19]" TL="2" V="4">test</P>
</C>
</C>
<C T="[UNIVERSAL 16]" TL="2" V="13">
<P T="[UNIVERSAL 2]" TL="2" V="1">&x05;</P>
<C T="[1]" TL="2" V="6">
<P T="[UNIVERSAL 19]" TL="2" V="4">test</P>
</C>
<P T="[UNIVERSAL 5]" TL="2" V="0"></P> -- extra component
</C>
<C T="[UNIVERSAL 16]" TL="2" V="16">
<P T="[UNIVERSAL 2]" TL="2" V="1">&x05;</P>
<C T="[1]" TL="2" V="6">
<P T="[UNIVERSAL 19]" TL="2" V="4">test</P>
</C>
<P T="[UNIVERSAL 5]" TL="2" V="0"></P> -- extra component
<P T="[UNIVERSAL 1]" TL="2" V="1">&xff;</P> -- another extra component
</C>
<C T="[UNIVERSAL 16]" TL="2" V="7">
<C T="[1]" TL="2" V="5">
<I T="[APPLICATION 123]" TL="3" V="Indefinite">
</I>
</C>
</C>
<C T="[UNIVERSAL 16]" TL="2" V="10">
<C T="[1]" TL="2" V="8">
<I T="[1]" TL="2">
</I>
<I T="[1]" TL="2">
</I>
</C>
</C>
0
\ No newline at end of file
<C T="[UNIVERSAL 16]" TL="2" V="6">
<C T="[1]" TL="2" V="2">
<I T="[1]" TL="2">
-- Do not terminate: no end-of-content octets is intended
</C>
</C>
<C T="[UNIVERSAL 16]" TL="2" V="8">
<C T="[1]" TL="2" V="6">
<I T="[PRIVATE 1]" TL="2">
</I>
<I T="[PRIVATE 2]" TL="2">
-- Do not terminate: no end-of-content octets is intended.
</C>
</C>
<C T="[UNIVERSAL 16]" TL="2" V="14">
<C T="[1]" TL="2" V="12">
<I T="[PRIVATE 1]" TL="2">
<I T="[PRIVATE 2]" TL="2">
</I>
<I T="[PRIVATE 3]" TL="2">
</I>
</I>
</C>
</C>
<C T="[UNIVERSAL 16]" TL="2" V="12">
<C T="[1]" TL="2" V="10">
<I T="[PRIVATE 1]" TL="2">
<I T="[PRIVATE 2]" TL="2">
</I>
<I T="[PRIVATE 3]" TL="2">
-- missing EOC
</I>
</C>
</C>
<C T="[UNIVERSAL 16]" TL="2" V="12">
<C T="[1]" TL="2" V="10">
<I T="[PRIVATE 1]" TL="2">
<P T="[UNIVERSAL 19]" TL="2" V="4">test</P>
</I>
</C>
</C>
<C T="[UNIVERSAL 16]" TL="2" V="12">
<C T="[1]" TL="2" V="10">
<I T="[PRIVATE 1]" TL="2">
<P T="[UNIVERSAL 19]" TL="2" V="5">test2</P>
</I>
</C>
</C>
<C T="[UNIVERSAL 16]" TL="2" V="13">
<C T="[1]" TL="2" V="11">
<I T="[PRIVATE 1]" TL="2">
<P T="[UNIVERSAL 19]" TL="2" V="4">test</P>
</I>
</C>
</C>
<C T="[UNIVERSAL 16]" TL="2" V="12">
<C T="[1]" TL="2" V="11">
<I T="[PRIVATE 1]" TL="2">
<P T="[UNIVERSAL 19]" TL="2" V="4">test</P>
</I>
</C>
</C>
<C T="[UNIVERSAL 16]" TL="2" V="9">
<C T="[1]" TL="2" V="7">
<I T="[PRIVATE 1]" TL="2">
<C T="[123]" TL="3" V="0">
</C>
</I>
</C>
</C>
<C T="[UNIVERSAL 16]" TL="2" V="9">
<C T="[1]" TL="2" V="7">
<I T="[PRIVATE 1]" TL="2">
<C T="[123]" TL="3" V="0">
<C T="[23]" TL="2" V="0">
<P T="[UNIVERSAL 5]" TL="2" V="0"></P>
</C>
</C>
</I>
</C>
</C>
<C T="[UNIVERSAL 16]" TL="2" V="13">
<C T="[1]" TL="2" V="11">
<I T="[PRIVATE 1]" TL="2">
<C T="[123]" TL="3" V="4">
<C T="[23]" TL="2" V="2">
<P T="[UNIVERSAL 5]" TL="2" V="0"></P>
</C>
</C>
</I>
</C>
</C>
<C T="[UNIVERSAL 16]" TL="2" V="13">
<C T="[1]" TL="2" V="11">
<I T="[PRIVATE 1]" TL="2">
<C T="[123]" TL="3" V="5">
<C T="[23]" TL="2" V="2">
<P T="[UNIVERSAL 5]" TL="2" V="0"></P>
</C>
</C>
</I>
</C>
</C>
<I T="[UNIVERSAL 16]" TL="2" V="Indefinite">
<C T="[1]" TL="2" V="14">
<I T="[PRIVATE 1]" TL="2">
<C T="[123]" TL="3" V="5">
<C T="[23]" TL="2" V="3">
<P T="[UNIVERSAL 19]" TL="2" V="1">z</P>
</C>
</C>
</I>
<P T="[UNIVERSAL 0]" TL="2" V="0"></P>
</C>
</I>
<C T="[UNIVERSAL 16]" TL="2" V="14">
<C T="[1]" TL="2" V="12">
<I T="[PRIVATE 1]" TL="2">
<C T="[123]" TL="3" V="5">
<C T="[23]" TL="2" V="3">
<P T="[UNIVERSAL 19]" TL="2" V="1">z</P>
</C>
</C>
</I>
</C>
</C>
<I T="[UNIVERSAL 16]" TL="2" V="Indefinite">
<C T="[1]" TL="2" V="14">
<I T="[PRIVATE 1]" TL="2">
<C T="[123]" TL="3" V="5">
<C T="[23]" TL="2" V="3">
<P T="[UNIVERSAL 19]" TL="2" V="1">z</P>
</C>
</C>
</I>
</C>
</I>
<C T="[UNIVERSAL 16]" TL="2" V="2">
<C T="[1]" TL="2" V="0">
</C>
</C>
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