asn1c/asn1c/tests/check-70.c

284 lines
6.2 KiB
C
Raw Normal View History

2005-02-18 10:13:44 +00:00
/*
* Mode of operation:
* Each of the *.in files is XER-decoded, then converted into DER,
* then decoded from DER and encoded into XER again. The resulting
* stream is compared with the corresponding .out file.
*/
2005-02-14 17:20:23 +00:00
#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 <PDU.h>
enum expectation {
EXP_OK, /* Encoding/decoding must succeed */
EXP_BROKEN, /* Decoding must fail */
EXP_DIFFERENT, /* Reconstruction will yield different encoding */
};
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("%c", *b);
printf("]:%ld\n", (long)size);
buf_offset += size;
return 0;
}
2005-02-18 10:13:44 +00:00
enum der_or_xer {
AS_DER,
AS_XER,
};
static void
save_object_as(PDU_t *st, enum der_or_xer how) {
2005-02-14 17:20:23 +00:00
asn_enc_rval_t rval; /* Return value */
buf_offset = 0;
2005-02-18 10:13:44 +00:00
/*
* Save object using specified method.
*/
switch(how) {
case AS_DER:
rval = der_encode(&asn_DEF_PDU, st,
_buf_writer, 0);
break;
case AS_XER:
rval = xer_encode(&asn_DEF_PDU, st, XER_F_BASIC,
_buf_writer, 0);
break;
}
2005-02-14 17:20:23 +00:00
if (rval.encoded == -1) {
fprintf(stderr,
"Cannot encode %s: %s\n",
rval.failed_type->name, strerror(errno));
assert(rval.encoded != -1);
2005-02-18 10:13:44 +00:00
return;
2005-02-14 17:20:23 +00:00
}
fprintf(stderr, "SAVED OBJECT IN SIZE %d\n", buf_offset);
}
static PDU_t *
2005-02-18 10:13:44 +00:00
load_object_from(enum expectation expectation, char *fbuf, int size, enum der_or_xer how) {
2005-02-14 17:20:23 +00:00
asn_dec_rval_t rval;
2005-02-18 10:13:44 +00:00
asn_dec_rval_t (*zer_decode)(struct asn_codec_ctx_s *,
asn_TYPE_descriptor_t *, void **, void *, size_t);
2005-02-14 17:20:23 +00:00
PDU_t *st = 0;
int csize;
2005-02-18 10:13:44 +00:00
if(how == AS_DER)
zer_decode = ber_decode;
else
zer_decode = xer_decode;
2005-02-14 17:20:23 +00:00
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) asn_DEF_PDU.free_struct(&asn_DEF_PDU, st, 0);
st = 0;
do {
fprintf(stderr, "Decoding from %d with %d (left %d)\n",
fbuf_offset, fbuf_chunk, fbuf_left);
2005-02-18 10:13:44 +00:00
rval = zer_decode(0, &asn_DEF_PDU, (void **)&st,
2005-02-14 17:20:23 +00:00
fbuf + fbuf_offset,
fbuf_chunk < fbuf_left
2005-02-18 10:13:44 +00:00
? fbuf_chunk : fbuf_left);
2005-02-14 17:20:23 +00:00
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);
2005-02-18 10:13:44 +00:00
if(how == AS_DER) {
assert(fbuf_offset == size);
} else {
assert(fbuf_offset - size < 2
2005-02-14 17:20:23 +00:00
|| (fbuf_offset + 1 /* "\n" */ == size
&& fbuf[size - 1] == '\n')
|| (fbuf_offset + 2 /* "\r\n" */ == size
&& fbuf[size - 2] == '\r'
&& fbuf[size - 1] == '\n')
2005-02-18 10:13:44 +00:00
);
}
2005-02-14 17:20:23 +00:00
} else {
assert(rval.code != RC_OK);
fprintf(stderr, "Failed, but this was expected\n");
asn_DEF_PDU.free_struct(&asn_DEF_PDU, st, 0);
st = 0; /* ignore leak for now */
}
}
if(st) asn_fprint(stderr, &asn_DEF_PDU, st);
return st;
}
2005-02-18 10:13:44 +00:00
static int
xer_encoding_equal(char *obuf, size_t osize, char *nbuf, size_t nsize) {
char *oend = obuf + osize;
char *nend = nbuf + nsize;
if((osize && !nsize) || (!osize && nsize))
return 0; /* not equal apriori */
while(1) {
while(obuf < oend && isspace(*obuf)) obuf++;
while(nbuf < nend && isspace(*nbuf)) nbuf++;
if(obuf == oend || nbuf == nend) {
if(obuf == oend && nbuf == nend)
break;
fprintf(stderr, "%s data in reconstructed encoding\n",
(obuf == oend) ? "More" : "Less");
return 0;
}
if(*obuf != *nbuf) {
printf("%c%c != %c%c\n",
obuf[0], obuf[1],
nbuf[0], nbuf[1]);
return 0;
}
obuf++, nbuf++;
}
return 1;
}
2005-02-14 17:20:23 +00:00
static void
2005-02-18 10:13:44 +00:00
process_XER_data(enum expectation expectation, char *fbuf, int size) {
2005-02-14 17:20:23 +00:00
PDU_t *st;
int ret;
2005-02-18 10:13:44 +00:00
st = load_object_from(expectation, fbuf, size, AS_XER);
2005-02-14 17:20:23 +00:00
if(!st) return;
2005-02-18 10:13:44 +00:00
/* Save and re-load as DER */
save_object_as(st, AS_DER);
st = load_object_from(expectation, buf, buf_offset, AS_DER);
assert(st);
save_object_as(st, AS_XER);
fprintf(stderr, "=== original ===\n");
fwrite(fbuf, 1, size, stderr);
fprintf(stderr, "=== re-encoded ===\n");
fwrite(buf, 1, buf_offset, stderr);
fprintf(stderr, "=== end ===\n");
2005-02-14 17:20:23 +00:00
switch(expectation) {
case EXP_DIFFERENT:
2005-02-18 10:13:44 +00:00
assert(!xer_encoding_equal(fbuf, size, buf, buf_offset));
2005-02-14 17:20:23 +00:00
break;
case EXP_BROKEN:
2005-02-18 10:13:44 +00:00
assert(!xer_encoding_equal(fbuf, size, buf, buf_offset));
2005-02-14 17:20:23 +00:00
break;
case EXP_OK:
2005-02-18 10:13:44 +00:00
assert(xer_encoding_equal(fbuf, size, buf, buf_offset));
2005-02-14 17:20:23 +00:00
break;
}
asn_DEF_PDU.free_struct(&asn_DEF_PDU, 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, ".in"))
return 0;
switch(ext[-1]) {
case 'B': /* The file is intentionally broken */
expectation = EXP_BROKEN; break;
case 'D': /* Reconstructing should yield different data */
expectation = EXP_DIFFERENT; break;
default:
expectation = EXP_OK; break;
}
fprintf(stderr, "\nProcessing file [../%s]\n", fname);
ret = chdir("../data-70");
assert(ret == 0);
fp = fopen(fname, "r");
ret = chdir("../test-check-70");
assert(ret == 0);
assert(fp);
rd = fread(fbuf, 1, sizeof(fbuf), fp);
fclose(fp);
assert(rd < sizeof(fbuf)); /* expect small files */
2005-02-18 10:13:44 +00:00
process_XER_data(expectation, fbuf, rd);
2005-02-14 17:20:23 +00:00
return 1;
}
int
main() {
DIR *dir;
struct dirent *dent;
int processed_files = 0;
char *str;
/* Process a specific test file */
str = getenv("DATA_70_FILE");
if(str && strncmp(str, "data-70-", 8) == 0)
process(str);
2005-02-18 10:13:44 +00:00
dir = opendir("../data-70");
assert(dir);
/*
* Process each file in that directory.
*/
2005-02-14 17:20:23 +00:00
while((dent = readdir(dir))) {
if(strncmp(dent->d_name, "data-70-", 8) == 0)
if(process(dent->d_name))
processed_files++;
}
assert(processed_files);
closedir(dir);
return 0;
}