MfP4: test harness improvements.

This commit is contained in:
Tim Kientzle 2008-06-15 10:35:22 +00:00
parent 8d86da5715
commit c8390967f2
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=179798
6 changed files with 99 additions and 34 deletions

View file

@ -27,13 +27,13 @@
* Various utility routines useful for test programs.
* Each test program is linked against this file.
*/
#include "test.h"
#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.
*
@ -540,6 +540,48 @@ test_assert_equal_file(const char *f1, const char *f2pattern, ...)
return (0);
}
int
test_assert_file_exists(const char *fpattern, ...)
{
char f[1024];
va_list ap;
va_start(ap, fpattern);
vsprintf(f, fpattern, ap);
va_end(ap);
if (!access(f, F_OK))
return (1);
if (!previous_failures(test_filename, test_line)) {
fprintf(stderr, "%s:%d: File doesn't exist\n",
test_filename, test_line);
fprintf(stderr, " file=\"%s\"\n", f);
report_failure(test_extra);
}
return (0);
}
int
test_assert_file_not_exists(const char *fpattern, ...)
{
char f[1024];
va_list ap;
va_start(ap, fpattern);
vsprintf(f, fpattern, ap);
va_end(ap);
if (access(f, F_OK))
return (1);
if (!previous_failures(test_filename, test_line)) {
fprintf(stderr, "%s:%d: File exists and shouldn't\n",
test_filename, test_line);
fprintf(stderr, " file=\"%s\"\n", f);
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, ...)
@ -669,8 +711,11 @@ static int test_run(int i, const char *tmpdir)
{
int failures_before = failures;
if (!quiet_flag)
if (!quiet_flag) {
printf("%d: %s\n", i, tests[i].name);
fflush(stdout);
}
/*
* Always explicitly chdir() in case the last test moved us to
* a strange place.

View file

@ -31,8 +31,18 @@
* The goal of this file (and the matching test.c) is to
* simplify the very repetitive test-*.c test programs.
*/
#ifndef _FILE_OFFSET_BITS
#define _FILE_OFFSET_BITS 64
#if defined(HAVE_CONFIG_H)
/* Most POSIX platforms use the 'configure' script to build config.h */
#include "../../config.h"
#elif defined(__FreeBSD__)
/* Building as part of FreeBSD system requires a pre-built config.h. */
#include "../config_freebsd.h"
#elif defined(_WIN32)
/* Win32 can't run the 'configure' script. */
#include "../config_windows.h"
#else
/* Warn if the library hasn't been (automatically or manually) configured. */
#error Oops: No config.h and no pre-built configuration in test.h.
#endif
#include <dirent.h>
@ -51,20 +61,6 @@
#include <dmalloc.h>
#endif
#if defined(HAVE_CONFIG_H)
/* Most POSIX platforms use the 'configure' script to build config.h */
#include "../../config.h"
#elif defined(__FreeBSD__)
/* Building as part of FreeBSD system requires a pre-built config.h. */
#include "../config_freebsd.h"
#elif defined(_WIN32)
/* Win32 can't run the 'configure' script. */
#include "../config_windows.h"
#else
/* Warn if the library hasn't been (automatically or manually) configured. */
#error Oops: No config.h and no pre-built configuration in test.h.
#endif
/* No non-FreeBSD platform will have __FBSDID, so just define it here. */
#ifdef __FreeBSD__
#include <sys/cdefs.h> /* For __FBSDID */
@ -102,6 +98,15 @@
/* Assert that a file is empty; supports printf-style arguments. */
#define assertEmptyFile \
test_setup(__FILE__, __LINE__);test_assert_empty_file
/* Assert that a file exists; supports printf-style arguments. */
#define assertFileExists \
test_setup(__FILE__, __LINE__);test_assert_file_exists
/* Assert that a file exists; supports printf-style arguments. */
#define assertFileNotExists \
test_setup(__FILE__, __LINE__);test_assert_file_not_exists
/* Assert that file contents match a string; supports printf-style arguments. */
#define assertFileContents \
test_setup(__FILE__, __LINE__);test_assert_file_contents
/*
* This would be simple with C99 variadic macros, but I don't want to
@ -124,6 +129,8 @@ int test_assert_equal_string(const char *, int, const char *v1, const char *, co
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 *, ...);
int test_assert_file_exists(const char *, ...);
int test_assert_file_not_exists(const char *, ...);
/* Like sprintf, then system() */
int systemf(const char * fmt, ...);

View file

@ -135,12 +135,18 @@ DEFINE_TEST(test_read_extract)
#endif
/* Test the entries on disk. */
/* This first entry was extracted with ARCHIVE_EXTRACT_PERM,
* so the permissions should have been restored exactly,
* including resetting the gid bit on those platforms
* where gid is inherited by subdirs. */
assert(0 == stat("dir_0775", &st));
failure("This was 0775 in archive, and should be 0775 on disk");
assert(st.st_mode == (S_IFDIR | 0775));
assertEqualInt(st.st_mode, S_IFDIR | 0775);
/* Everything else was extracted without ARCHIVE_EXTRACT_PERM,
* so there may be some sloppiness about gid bits on directories. */
assert(0 == stat("file", &st));
failure("st.st_mode=%o should be %o", st.st_mode, S_IFREG | 0755);
assert(st.st_mode == (S_IFREG | 0755));
assertEqualInt(st.st_mode, S_IFREG | 0755);
failure("The file extracted to disk is the wrong size.");
assert(st.st_size == FILE_BUFF_SIZE);
fd = open("file", O_RDONLY);
@ -153,23 +159,26 @@ DEFINE_TEST(test_read_extract)
assert(memcmp(buff, file_buff, FILE_BUFF_SIZE) == 0);
assert(0 == stat("dir", &st));
failure("This was 0777 in archive, but umask should make it 0755");
assert(st.st_mode == (S_IFDIR | 0755));
/* If EXTRACT_PERM wasn't used, be careful to ignore sgid bit
* when checking dir modes, as some systems inherit sgid bit
* from the parent dir. */
assertEqualInt(0755, st.st_mode & 0777);
assert(0 == stat("dir/file", &st));
assert(st.st_mode == (S_IFREG | 0700));
assert(0 == stat("dir2", &st));
assert(st.st_mode == (S_IFDIR | 0755));
assertEqualInt(0755, st.st_mode & 0777);
assert(0 == stat("dir2/file", &st));
assert(st.st_mode == (S_IFREG | 0000));
assert(0 == stat("dir3", &st));
assert(st.st_mode == (S_IFDIR | 0710));
assertEqualInt(0710, st.st_mode & 0777);
assert(0 == stat("dir4", &st));
assert(st.st_mode == (S_IFDIR | 0755));
assertEqualInt(0755, st.st_mode & 0777);
assert(0 == stat("dir4/a", &st));
assert(st.st_mode == (S_IFDIR | 0755));
assertEqualInt(0755, st.st_mode & 0777);
assert(0 == stat("dir4/b", &st));
assert(st.st_mode == (S_IFDIR | 0755));
assertEqualInt(0755, st.st_mode & 0777);
assert(0 == stat("dir4/c", &st));
assert(st.st_mode == (S_IFDIR | 0711));
assertEqualInt(0711, st.st_mode & 0777);
assert(0 == lstat("symlink", &st));
assert(S_ISLNK(st.st_mode));
#if HAVE_LCHMOD

View file

@ -48,7 +48,11 @@ static void create(struct archive_entry *ae, const char *msg)
assert(0 == stat(archive_entry_pathname(ae), &st));
failure("st.st_mode=%o archive_entry_mode(ae)=%o",
st.st_mode, archive_entry_mode(ae));
assert(st.st_mode == (archive_entry_mode(ae) & ~UMASK));
/* When verifying a dir, ignore the S_ISGID bit, as some systems set
* that automatically. */
if (archive_entry_filetype(ae) == AE_IFDIR)
st.st_mode &= ~S_ISGID;
assertEqualInt(st.st_mode, archive_entry_mode(ae) & ~UMASK);
}
static void create_reg_file(struct archive_entry *ae, const char *msg)

View file

@ -186,7 +186,7 @@ DEFINE_TEST(test_write_disk_perms)
/* Check original perms. */
assert(0 == stat("dir_overwrite_0744", &st));
failure("dir_overwrite_0744: st.st_mode=%o", st.st_mode);
assert((st.st_mode & 07777) == 0744);
assert((st.st_mode & 0777) == 0744);
/* Overwrite shouldn't edit perms. */
assert((ae = archive_entry_new()) != NULL);
archive_entry_copy_pathname(ae, "dir_overwrite_0744");
@ -197,7 +197,7 @@ DEFINE_TEST(test_write_disk_perms)
/* Make sure they're unchanged. */
assert(0 == stat("dir_overwrite_0744", &st));
failure("dir_overwrite_0744: st.st_mode=%o", st.st_mode);
assert((st.st_mode & 07777) == 0744);
assert((st.st_mode & 0777) == 0744);
/* Write a regular file with SUID bit, but don't use _EXTRACT_PERM. */
assert((ae = archive_entry_new()) != NULL);
@ -385,7 +385,7 @@ DEFINE_TEST(test_write_disk_perms)
assert(0 == stat("dir_overwrite_0744", &st));
failure("dir_overwrite_0744: st.st_mode=%o", st.st_mode);
assert((st.st_mode & 07777) == 0744);
assert((st.st_mode & 0777) == 0744);
assert(0 == stat("file_no_suid", &st));
failure("file_0755: st.st_mode=%o", st.st_mode);

View file

@ -115,7 +115,7 @@ DEFINE_TEST(test_write_disk_secure)
/* Test the entries on disk. */
assert(0 == lstat("dir", &st));
failure("dir: st.st_mode=%o", st.st_mode);
assert((st.st_mode & 07777) == 0755);
assert((st.st_mode & 0777) == 0755);
assert(0 == lstat("link_to_dir", &st));
failure("link_to_dir: st.st_mode=%o", st.st_mode);
@ -137,7 +137,7 @@ DEFINE_TEST(test_write_disk_secure)
failure("link_to_dir2 should have been re-created as a true dir");
assert(S_ISDIR(st.st_mode));
failure("link_to_dir2: Implicit dir creation should obey umask, but st.st_mode=%o", st.st_mode);
assert((st.st_mode & 07777) == 0755);
assert((st.st_mode & 0777) == 0755);
assert(0 == lstat("link_to_dir2/filec", &st));
assert(S_ISREG(st.st_mode));