Portability: Eliminate the need for uudecode by incorporating

uudecode into the main test driver and invoking it just-in-time
within the various tests.

Also, incorporate a number of improvements to the main test support
code that have proven useful on other projects where I've used this
framework.
This commit is contained in:
Tim Kientzle 2008-03-12 05:12:23 +00:00
parent bdb5bdf0b7
commit df4691b984
7 changed files with 231 additions and 81 deletions

View File

@ -7,17 +7,6 @@ LA_SRCDIR=${.CURDIR}/..
# Get a list of all libarchive source files
LA_SRCS!=make -f ${LA_SRCDIR}/Makefile -V SRCS
TESTFILES= \
test_compat_gtar_1.tgz \
test_compat_tar_hardlink_1.tar \
test_compat_zip_1.zip \
test_read_format_gtar_sparse_1_13.tgz \
test_read_format_gtar_sparse_1_17.tgz \
test_read_format_gtar_sparse_1_17_posix00.tgz \
test_read_format_gtar_sparse_1_17_posix01.tgz \
test_read_format_gtar_sparse_1_17_posix10.tgz \
test_read_format_gtar_sparse_1_17_posix10_modified.tar
TESTS= \
test_acl_basic.c \
test_acl_pax.c \
@ -104,13 +93,8 @@ CFLAGS+= -I/usr/local/include -DUSE_DMALLOC
WARNS=6
# Build libarchive_test and run it.
check test: libarchive_test ${TESTFILES}
./libarchive_test
.for f in ${TESTFILES}
${f}: ${f}.uu
uudecode -p ${.CURDIR}/${f}.uu >${f}
.endfor
check test: libarchive_test
./libarchive_test -k -r ${.CURDIR}
INCS=archive.h list.h

View File

@ -24,22 +24,25 @@
*/
/*
* This same file is used pretty much verbatim for all test harnesses.
*
* The next line is used to define various environment variables, etc.
*
* The tar and cpio test harnesses are identical except for this line;
* the libarchive test harness omits some code that is needed only for
* testing standalone executables.
* Various utility routines useful for test programs.
* Each test program is linked against this file.
*/
#define PROGRAM "LIBARCHIVE"
#include <errno.h>
#include <locale.h>
#include <stdarg.h>
#include <time.h>
#include "test.h"
/*
* This same file is used pretty much verbatim for all test harnesses.
*
* The next few lines are the only differences.
*/
#undef PROGRAM /* Testing a library, not a program. */
#define ENVBASE "LIBARCHIVE" /* Prefix for environment variables. */
#define EXTRA_DUMP(x) archive_error_string((struct archive *)(x))
#define EXTRA_VERSION archive_version()
__FBSDID("$FreeBSD$");
/*
@ -51,7 +54,7 @@ __FBSDID("$FreeBSD$");
* test functions.
*/
#undef DEFINE_TEST
#define DEFINE_TEST(name) void name(void);
#define DEFINE_TEST(name) void name(void);
#include "list.h"
/* Interix doesn't define these in a standard header. */
@ -71,6 +74,9 @@ static int skips = 0;
/* Cumulative count of assertions. */
static int assertions = 0;
/* Directory where uuencoded reference files can be found. */
static char *refdir;
/*
* My own implementation of the standard assert() macro emits the
* message in the same format as GCC (file:line: message).
@ -90,7 +96,7 @@ static char msg[4096];
* For each test source file, we remember how many times each
* failure was reported.
*/
static const char *failed_filename;
static const char *failed_filename = NULL;
static struct line {
int line;
int count;
@ -163,9 +169,13 @@ report_failure(void *extra)
fprintf(stderr, " Description: %s\n", msg);
msg[0] = '\0';
}
if (extra != NULL) {
fprintf(stderr, " archive error: %s\n", archive_error_string((struct archive *)extra));
}
#ifdef EXTRA_DUMP
if (extra != NULL)
fprintf(stderr, " detail: %s\n", EXTRA_DUMP(extra));
#else
(void)extra; /* UNUSED */
#endif
if (dump_on_failure) {
fprintf(stderr,
@ -241,27 +251,28 @@ test_assert(const char *file, int line, int value, const char *condition, void *
}
/* assertEqualInt() displays the values of the two integers. */
void
int
test_assert_equal_int(const char *file, int line,
int v1, const char *e1, int v2, const char *e2, void *extra)
{
++assertions;
if (v1 == v2) {
msg[0] = '\0';
return;
return (1);
}
failures ++;
if (previous_failures(file, line))
return;
return (0);
fprintf(stderr, "%s:%d: Assertion failed: Ints not equal\n",
file, line);
fprintf(stderr, " %s=%d\n", e1, v1);
fprintf(stderr, " %s=%d\n", e2, v2);
report_failure(extra);
return (0);
}
/* assertEqualString() displays the values of the two strings. */
void
int
test_assert_equal_string(const char *file, int line,
const char *v1, const char *e1,
const char *v2, const char *e2,
@ -271,24 +282,25 @@ test_assert_equal_string(const char *file, int line,
if (v1 == NULL || v2 == NULL) {
if (v1 == v2) {
msg[0] = '\0';
return;
return (1);
}
} else if (strcmp(v1, v2) == 0) {
msg[0] = '\0';
return;
return (1);
}
failures ++;
if (previous_failures(file, line))
return;
return (0);
fprintf(stderr, "%s:%d: Assertion failed: Strings not equal\n",
file, line);
fprintf(stderr, " %s = \"%s\"\n", e1, v1);
fprintf(stderr, " %s = \"%s\"\n", e2, v2);
report_failure(extra);
return (0);
}
/* assertEqualWString() displays the values of the two strings. */
void
int
test_assert_equal_wstring(const char *file, int line,
const wchar_t *v1, const char *e1,
const wchar_t *v2, const char *e2,
@ -297,16 +309,17 @@ test_assert_equal_wstring(const char *file, int line,
++assertions;
if (wcscmp(v1, v2) == 0) {
msg[0] = '\0';
return;
return (1);
}
failures ++;
if (previous_failures(file, line))
return;
return (0);
fprintf(stderr, "%s:%d: Assertion failed: Unicode strings not equal\n",
file, line);
fwprintf(stderr, L" %s = \"%ls\"\n", e1, v1);
fwprintf(stderr, L" %s = \"%ls\"\n", e2, v2);
report_failure(extra);
return (0);
}
/*
@ -326,7 +339,7 @@ hexdump(const char *p, const char *ref, size_t l, size_t offset)
for (j = 0; j < 16 && i + j < l; j++) {
if (ref != NULL && p[i + j] != ref[i + j])
sep = '_';
fprintf(stderr, "%c%02x", sep, p[i+j]);
fprintf(stderr, "%c%02x", sep, 0xff & (int)p[i+j]);
if (ref != NULL && p[i + j] == ref[i + j])
sep = ' ';
}
@ -348,7 +361,7 @@ hexdump(const char *p, const char *ref, size_t l, size_t offset)
/* assertEqualMem() displays the values of the two memory blocks. */
/* TODO: For long blocks, hexdump the first bytes that actually differ. */
void
int
test_assert_equal_mem(const char *file, int line,
const char *v1, const char *e1,
const char *v2, const char *e2,
@ -358,15 +371,15 @@ test_assert_equal_mem(const char *file, int line,
if (v1 == NULL || v2 == NULL) {
if (v1 == v2) {
msg[0] = '\0';
return;
return (1);
}
} else if (memcmp(v1, v2, l) == 0) {
msg[0] = '\0';
return;
return (1);
}
failures ++;
if (previous_failures(file, line))
return;
return (0);
fprintf(stderr, "%s:%d: Assertion failed: memory not equal\n",
file, line);
fprintf(stderr, " size %s = %d\n", ld, (int)l);
@ -376,14 +389,19 @@ test_assert_equal_mem(const char *file, int line,
hexdump(v2, v1, l < 32 ? l : 32, 0);
fprintf(stderr, "\n");
report_failure(extra);
return (0);
}
void
int
test_assert_empty_file(const char *f1fmt, ...)
{
char buff[1024];
char f1[1024];
struct stat st;
va_list ap;
ssize_t s;
int fd;
va_start(ap, f1fmt);
vsprintf(f1, f1fmt, ap);
@ -392,16 +410,32 @@ test_assert_empty_file(const char *f1fmt, ...)
if (stat(f1, &st) != 0) {
fprintf(stderr, "%s:%d: Could not stat: %s\n", test_filename, test_line, f1);
report_failure(NULL);
} else if (st.st_size > 0) {
fprintf(stderr, "%s:%d: File not empty: %s\n", test_filename, test_line, f1);
fprintf(stderr, " File size: %d\n", (int)st.st_size);
report_failure(NULL);
}
if (st.st_size == 0)
return (1);
failures ++;
if (previous_failures(test_filename, test_line))
return (0);
fprintf(stderr, "%s:%d: File not empty: %s\n", test_filename, test_line, f1);
fprintf(stderr, " File size: %d\n", (int)st.st_size);
fprintf(stderr, " Contents:\n");
fd = open(f1, O_RDONLY);
if (fd < 0) {
fprintf(stderr, " Unable to open %s\n", f1);
} else {
s = sizeof(buff) < st.st_size ? sizeof(buff) : st.st_size;
s = read(fd, buff, s);
hexdump(buff, NULL, s, 0);
}
report_failure(NULL);
return (0);
}
/* assertEqualFile() asserts that two files have the same contents. */
/* TODO: hexdump the first bytes that actually differ. */
void
int
test_assert_equal_file(const char *f1, const char *f2pattern, ...)
{
char f2[1024];
@ -423,16 +457,58 @@ test_assert_equal_file(const char *f1, const char *f2pattern, ...)
if (n1 != n2)
break;
if (n1 == 0 && n2 == 0)
return;
return (1);
if (memcmp(buff1, buff2, n1) != 0)
break;
}
fprintf(stderr, "%s:%d: Files are not identical\n", test_filename, test_line);
failures ++;
if (previous_failures(test_filename, test_line))
return (0);
fprintf(stderr, "%s:%d: Files are not identical\n",
test_filename, test_line);
fprintf(stderr, " file1=\"%s\"\n", f1);
fprintf(stderr, " file2=\"%s\"\n", f2);
report_failure(test_extra);
return (0);
}
/* assertFileContents() asserts the contents of a file. */
int
test_assert_file_contents(const void *buff, int s, const char *fpattern, ...)
{
char f[1024];
va_list ap;
char *contents;
int fd;
int n;
va_start(ap, fpattern);
vsprintf(f, fpattern, ap);
va_end(ap);
fd = open(f, O_RDONLY);
contents = malloc(s * 2);
n = read(fd, contents, s * 2);
if (n == s && memcmp(buff, contents, s) == 0) {
free(contents);
return (1);
}
failures ++;
if (!previous_failures(test_filename, test_line)) {
fprintf(stderr, "%s:%d: File contents don't match\n",
test_filename, test_line);
fprintf(stderr, " file=\"%s\"\n", f);
if (n > 0)
hexdump(contents, buff, n, 0);
else {
fprintf(stderr, " File empty, contents should be:\n");
hexdump(buff, NULL, s, 0);
}
report_failure(test_extra);
}
free(contents);
return (0);
}
/*
* Call standard system() call, but build up the command line using
@ -510,7 +586,7 @@ slurpfile(size_t * sizep, const char *fmt, ...)
* We reuse it here to define a list of all tests (functions and names).
*/
#undef DEFINE_TEST
#define DEFINE_TEST(n) { n, #n },
#define DEFINE_TEST(n) { n, #n },
struct { void (*func)(void); const char *name; } tests[] = {
#include "list.h"
};
@ -572,6 +648,10 @@ static void usage(const char *program)
printf("Options:\n");
printf(" -k Keep running after failures.\n");
printf(" Default: Core dump after any failure.\n");
#ifdef PROGRAM
printf(" -p <path> Path to executable to be tested.\n");
printf(" Default: path taken from " ENVBASE " environment variable.\n");
#endif
printf(" -q Quiet.\n");
printf(" -r <dir> Path to dir containing reference files.\n");
printf(" Default: Current directory.\n");
@ -581,6 +661,66 @@ static void usage(const char *program)
exit(1);
}
#define UUDECODE(c) (((c) - 0x20) & 0x3f)
void
extract_reference_file(const char *name)
{
char buff[1024];
FILE *in, *out;
sprintf(buff, "%s/%s.uu", refdir, name);
in = fopen(buff, "r");
failure("Couldn't open reference file %s", buff);
assert(in != NULL);
if (in == NULL)
return;
/* Read up to and including the 'begin' line. */
for (;;) {
if (fgets(buff, sizeof(buff), in) == NULL) {
/* TODO: This is a failure. */
return;
}
if (memcmp(buff, "begin ", 6) == 0)
break;
}
/* Now, decode the rest and write it. */
/* Not a lot of error checking here; the input better be right. */
out = fopen(name, "w");
while (fgets(buff, sizeof(buff), in) != NULL) {
char *p = buff;
int bytes;
if (memcmp(buff, "end", 3) == 0)
break;
bytes = UUDECODE(*p++);
while (bytes > 0) {
int n = 0;
/* Write out 1-3 bytes from that. */
if (bytes > 0) {
n = UUDECODE(*p++) << 18;
n |= UUDECODE(*p++) << 12;
fputc(n >> 16, out);
--bytes;
}
if (bytes > 0) {
n |= UUDECODE(*p++) << 6;
fputc((n >> 8) & 0xFF, out);
--bytes;
}
if (bytes > 0) {
n |= UUDECODE(*p++);
fputc(n & 0xFF, out);
--bytes;
}
}
}
fclose(out);
fclose(in);
}
int main(int argc, char **argv)
{
static const int limit = sizeof(tests) / sizeof(tests[0]);
@ -602,17 +742,33 @@ int main(int argc, char **argv)
++p;
}
#ifdef PROGRAM
/* Get the target program from environment, if available. */
testprog = getenv(ENVBASE);
#endif
/* Allow -k to be controlled through the environment. */
if (getenv(ENVBASE "_KEEP_GOING") != NULL)
dump_on_failure = 0;
/* Get the directory holding test files from environment. */
refdir = getenv(PROGRAM "_TEST_FILES");
refdir = getenv(ENVBASE "_TEST_FILES");
/*
* Parse options.
*/
while ((opt = getopt(argc, argv, "kqr:")) != -1) {
while ((opt = getopt(argc, argv, "kp:qr:")) != -1) {
switch (opt) {
case 'k':
dump_on_failure = 0;
break;
case 'p':
#ifdef PROGRAM
testprog = optarg;
#else
usage(progname);
#endif
break;
case 'q':
quiet_flag++;
break;
@ -627,6 +783,14 @@ int main(int argc, char **argv)
argc -= optind;
argv += optind;
/*
* Sanity-check that our options make sense.
*/
#ifdef PROGRAM
if (testprog == NULL)
usage(progname);
#endif
/*
* Create a temp directory for the following tests.
* Include the time the tests started as part of the name,
@ -667,7 +831,12 @@ int main(int argc, char **argv)
if (!quiet_flag) {
printf("Running tests in: %s\n", tmpdir);
printf("Reference files will be read from: %s\n", refdir);
printf("Exercising %s\n", archive_version());
#ifdef PROGRAM
printf("Running tests on: %s\n", testprog);
#endif
printf("Exercising: ");
fflush(stdout);
printf("%s\n", EXTRA_VERSION);
}
/*

View File

@ -35,6 +35,7 @@
#define _FILE_OFFSET_BITS 64
#endif
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
@ -116,12 +117,13 @@ void failure(const char *fmt, ...);
void test_setup(const char *, int);
void test_skipping(const char *fmt, ...);
int test_assert(const char *, int, int, const char *, void *);
void test_assert_empty_file(const char *, ...);
void test_assert_equal_file(const char *, const char *, ...);
void test_assert_equal_int(const char *, int, int, const char *, int, const char *, void *);
void test_assert_equal_string(const char *, int, const char *v1, const char *, const char *v2, const char *, void *);
void test_assert_equal_wstring(const char *, int, const wchar_t *v1, const char *, const wchar_t *v2, const char *, void *);
void test_assert_equal_mem(const char *, int, const char *, const char *, const char *, const char *, size_t, const char *, void *);
int test_assert_empty_file(const char *, ...);
int test_assert_equal_file(const char *, const char *, ...);
int test_assert_equal_int(const char *, int, int, const char *, int, const char *, void *);
int test_assert_equal_string(const char *, int, const char *v1, const char *, const char *v2, const char *, void *);
int test_assert_equal_wstring(const char *, int, const wchar_t *v1, const char *, const wchar_t *v2, const char *, void *);
int test_assert_equal_mem(const char *, int, const char *, const char *, const char *, const char *, size_t, const char *, void *);
int test_assert_file_contents(const void *, int, const char *, ...);
/* Like sprintf, then system() */
int systemf(const char * fmt, ...);
@ -130,12 +132,8 @@ int systemf(const char * fmt, ...);
/* Supports printf-style args: slurpfile(NULL, "%s/myfile", refdir); */
char *slurpfile(size_t *, const char *fmt, ...);
/*
* Global vars
*/
/* Directory holding reference files. */
char *refdir;
/* Extracts named reference file to the current directory. */
void extract_reference_file(const char *);
/*
* Special interfaces for libarchive test harness.

View File

@ -40,14 +40,14 @@ __FBSDID("$FreeBSD$");
static void
test_compat_gtar_1(void)
{
char name[1024];
char name[] = "test_compat_gtar_1.tgz";
struct archive_entry *ae;
struct archive *a;
assert((a = archive_read_new()) != NULL);
assertEqualIntA(a, ARCHIVE_OK, archive_read_support_compression_all(a));
assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
sprintf(name, "%s/test_compat_gtar_1.tgz", refdir);
extract_reference_file(name);
assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, name, 10240));
/* Read first entry. */

View File

@ -46,14 +46,14 @@ __FBSDID("$FreeBSD$");
static void
test_compat_tar_hardlink_1(void)
{
char name[1024];
char name[] = "test_compat_tar_hardlink_1.tar";
struct archive_entry *ae;
struct archive *a;
assert((a = archive_read_new()) != NULL);
assertEqualIntA(a, ARCHIVE_OK, archive_read_support_compression_all(a));
assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
sprintf(name, "%s/test_compat_tar_hardlink_1.tar", refdir);
extract_reference_file(name);
assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, name, 10240));
/* Read first entry, which is a regular file. */

View File

@ -29,14 +29,14 @@ __FBSDID("$FreeBSD$");
static void
test_compat_zip_1(void)
{
char name[1024];
char name[] = "test_compat_zip_1.zip";
struct archive_entry *ae;
struct archive *a;
assert((a = archive_read_new()) != NULL);
assertEqualIntA(a, ARCHIVE_OK, archive_read_support_compression_all(a));
assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
sprintf(name, "%s/test_compat_zip_1.zip", refdir);
extract_reference_file(name);
assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, name, 10240));
/* Read first entry. */

View File

@ -181,7 +181,6 @@ struct archive_contents {
static void
verify_archive_file(const char *name, struct archive_contents *ac)
{
char path[512];
struct archive_entry *ae;
int err;
/* data, size, offset of next expected block. */
@ -190,13 +189,13 @@ verify_archive_file(const char *name, struct archive_contents *ac)
struct contents actual;
struct archive *a;
sprintf(path, "%s/%s", refdir, name);
extract_reference_file(name);
assert((a = archive_read_new()) != NULL);
assert(0 == archive_read_support_compression_all(a));
assert(0 == archive_read_support_format_tar(a));
failure("Can't open %s", path);
assert(0 == archive_read_open_filename(a, path, 3));
failure("Can't open %s", name);
assert(0 == archive_read_open_filename(a, name, 3));
while (ac->filename != NULL) {
struct contents *cts = ac->contents;