New bsdtar test harness. Still rather skimpy, but a lot easier
to run and maintain than the old scripts that used to be here.
This commit is contained in:
parent
a0a88a1e98
commit
c40b056519
61
usr.bin/tar/test/Makefile
Normal file
61
usr.bin/tar/test/Makefile
Normal file
@ -0,0 +1,61 @@
|
||||
# $FreeBSD$
|
||||
|
||||
# Where to find the tar sources (for the internal unit tests)
|
||||
TAR_SRCDIR=${.CURDIR}/..
|
||||
.PATH: ${TAR_SRCDIR}
|
||||
|
||||
# Some tar sources are pulled in for white-box tests
|
||||
TAR_SRCS= \
|
||||
../getdate.y
|
||||
|
||||
TESTS= \
|
||||
test_0.c \
|
||||
test_basic.c \
|
||||
test_copy.c \
|
||||
test_getdate.c \
|
||||
test_help.c \
|
||||
test_option_T.c \
|
||||
test_stdio.c \
|
||||
test_version.c
|
||||
|
||||
# Build the test program
|
||||
SRCS= ${TAR_SRCS} \
|
||||
${TESTS} \
|
||||
list.h \
|
||||
main.c
|
||||
|
||||
CLEANFILES+= list.h
|
||||
|
||||
NO_MAN=yes
|
||||
|
||||
PROG=bsdtar_test
|
||||
DPADD=${LIBARCHIVE} ${LIBBZ2} ${LIBZ}
|
||||
CFLAGS+= -DPLATFORM_CONFIG_H=\"config_freebsd.h\"
|
||||
CFLAGS+= -I..
|
||||
LDADD= -larchive -lz -lbz2
|
||||
CFLAGS+= -static -g -O2 -Wall
|
||||
CFLAGS+= -I${.OBJDIR}
|
||||
CFLAGS+= -I${TAR_SRCDIR}
|
||||
|
||||
# Uncomment to link against dmalloc
|
||||
#LDADD+= -L/usr/local/lib -ldmalloc
|
||||
#CFLAGS+= -I/usr/local/include -DUSE_DMALLOC
|
||||
WARNS=6
|
||||
|
||||
check test: bsdtar_test
|
||||
./bsdtar_test -p ${.OBJDIR}/../bsdtar -r ${.CURDIR}
|
||||
|
||||
list.h: ${TESTS} Makefile
|
||||
(cd ${.CURDIR}; cat ${TESTS}) | grep DEFINE_TEST > list.h
|
||||
|
||||
clean:
|
||||
rm -f *.out
|
||||
rm -f *.o
|
||||
rm -f *.core
|
||||
rm -f *~
|
||||
rm -f list.h
|
||||
rm -f archive.h ../archive.h
|
||||
-chmod -R +w /tmp/bsdtar_test.*
|
||||
rm -rf /tmp/bsdtar_test.*
|
||||
|
||||
.include <bsd.prog.mk>
|
1012
usr.bin/tar/test/main.c
Normal file
1012
usr.bin/tar/test/main.c
Normal file
File diff suppressed because it is too large
Load Diff
151
usr.bin/tar/test/test.h
Normal file
151
usr.bin/tar/test/test.h
Normal file
@ -0,0 +1,151 @@
|
||||
/*
|
||||
* Copyright (c) 2003-2006 Tim Kientzle
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* $FreeBSD$
|
||||
*/
|
||||
|
||||
/* Every test program should #include "test.h" as the first thing. */
|
||||
|
||||
/*
|
||||
* 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
|
||||
#endif
|
||||
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#ifndef _WIN32
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include <wchar.h>
|
||||
|
||||
#ifdef USE_DMALLOC
|
||||
#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 */
|
||||
#else
|
||||
#define __FBSDID(a) /* null */
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Redefine DEFINE_TEST for use in defining the test functions.
|
||||
*/
|
||||
#undef DEFINE_TEST
|
||||
#define DEFINE_TEST(name) void name(void); void name(void)
|
||||
|
||||
/* An implementation of the standard assert() macro */
|
||||
#define assert(e) test_assert(__FILE__, __LINE__, (e), #e, NULL)
|
||||
|
||||
/* Assert two integers are the same. Reports value of each one if not. */
|
||||
#define assertEqualInt(v1,v2) \
|
||||
test_assert_equal_int(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
|
||||
|
||||
/* Assert two strings are the same. Reports value of each one if not. */
|
||||
#define assertEqualString(v1,v2) \
|
||||
test_assert_equal_string(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
|
||||
/* As above, but v1 and v2 are wchar_t * */
|
||||
#define assertEqualWString(v1,v2) \
|
||||
test_assert_equal_wstring(__FILE__, __LINE__, (v1), #v1, (v2), #v2, NULL)
|
||||
/* As above, but raw blocks of bytes. */
|
||||
#define assertEqualMem(v1, v2, l) \
|
||||
test_assert_equal_mem(__FILE__, __LINE__, (v1), #v1, (v2), #v2, (l), #l, NULL)
|
||||
/* Assert two files are the same; allow printf-style expansion of second name.
|
||||
* See below for comments about variable arguments here...
|
||||
*/
|
||||
#define assertEqualFile \
|
||||
test_setup(__FILE__, __LINE__);test_assert_equal_file
|
||||
/* 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
|
||||
|
||||
/*
|
||||
* This would be simple with C99 variadic macros, but I don't want to
|
||||
* require that. Instead, I insert a function call before each
|
||||
* skipping() call to pass the file and line information down. Crude,
|
||||
* but effective.
|
||||
*/
|
||||
#define skipping \
|
||||
test_setup(__FILE__, __LINE__);test_skipping
|
||||
|
||||
/* Function declarations. These are defined in test_utility.c. */
|
||||
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 *);
|
||||
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 *, ...);
|
||||
int test_assert_file_exists(const char *, ...);
|
||||
int test_assert_file_not_exists(const char *, ...);
|
||||
|
||||
/* Like sprintf, then system() */
|
||||
int systemf(const char * fmt, ...);
|
||||
|
||||
/* Suck file into string allocated via malloc(). Call free() when done. */
|
||||
/* Supports printf-style args: slurpfile(NULL, "%s/myfile", refdir); */
|
||||
char *slurpfile(size_t *, const char *fmt, ...);
|
||||
|
||||
/* Extracts named reference file to the current directory. */
|
||||
void extract_reference_file(const char *);
|
||||
|
||||
/*
|
||||
* Special interfaces for program test harness.
|
||||
*/
|
||||
|
||||
/* Pathname of exe to be tested. */
|
||||
char *testprog;
|
62
usr.bin/tar/test/test_0.c
Normal file
62
usr.bin/tar/test/test_0.c
Normal file
@ -0,0 +1,62 @@
|
||||
/*-
|
||||
* Copyright (c) 2003-2007 Tim Kientzle
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "test.h"
|
||||
__FBSDID("$FreeBSD$");
|
||||
|
||||
/*
|
||||
* This first test does basic sanity checks on the environment. For
|
||||
* most of these, we just exit on failure.
|
||||
*/
|
||||
|
||||
DEFINE_TEST(test_0)
|
||||
{
|
||||
struct stat st;
|
||||
|
||||
failure("File %s does not exist?!", testprog);
|
||||
if (!assertEqualInt(0, stat(testprog, &st)))
|
||||
exit(1);
|
||||
|
||||
failure("%s is not executable?!", testprog);
|
||||
if (!assert((st.st_mode & 0111) != 0))
|
||||
exit(1);
|
||||
|
||||
/*
|
||||
* Try to succesfully run the program; this requires that
|
||||
* we know some option that will succeed.
|
||||
*/
|
||||
if (0 == systemf("%s --version >/dev/null", testprog)) {
|
||||
/* This worked. */
|
||||
} else if (0 == systemf("%s -W version >/dev/null", testprog)) {
|
||||
/* This worked. */
|
||||
} else {
|
||||
failure("Unable to successfully run any of the following:\n"
|
||||
" * %s --version\n"
|
||||
" * %s -W version\n",
|
||||
testprog, testprog);
|
||||
assert(0);
|
||||
}
|
||||
|
||||
/* TODO: Ensure that our reference files are available. */
|
||||
}
|
158
usr.bin/tar/test/test_basic.c
Normal file
158
usr.bin/tar/test/test_basic.c
Normal file
@ -0,0 +1,158 @@
|
||||
/*-
|
||||
* Copyright (c) 2003-2007 Tim Kientzle
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "test.h"
|
||||
__FBSDID("$FreeBSD$");
|
||||
|
||||
|
||||
static void
|
||||
basic_tar(const char *target, const char *pack_options, const char *unpack_options)
|
||||
{
|
||||
struct stat st, st2;
|
||||
char buff[128];
|
||||
int r;
|
||||
|
||||
assertEqualInt(0, mkdir(target, 0775));
|
||||
|
||||
/* Use the tar program to create an archive. */
|
||||
r = systemf("%s cf - %s `cat filelist` >%s/archive 2>%s/pack.err", testprog, pack_options, target, target);
|
||||
failure("Error invoking %s cf -", testprog, pack_options);
|
||||
assertEqualInt(r, 0);
|
||||
|
||||
chdir(target);
|
||||
|
||||
/* Verify that nothing went to stderr. */
|
||||
assertEmptyFile("pack.err");
|
||||
|
||||
/*
|
||||
* Use tar to unpack the archive into another directory.
|
||||
*/
|
||||
r = systemf("%s xf archive %s >unpack.out 2>unpack.err", testprog, unpack_options);
|
||||
failure("Error invoking %s xf archive %s", testprog, unpack_options);
|
||||
assertEqualInt(r, 0);
|
||||
|
||||
/* Verify that nothing went to stderr. */
|
||||
assertEmptyFile("unpack.err");
|
||||
|
||||
/*
|
||||
* Verify unpacked files.
|
||||
*/
|
||||
|
||||
/* Regular file with 2 links. */
|
||||
r = lstat("file", &st);
|
||||
failure("Failed to stat file %s/file, errno=%d", target, errno);
|
||||
assertEqualInt(r, 0);
|
||||
if (r == 0) {
|
||||
assert(S_ISREG(st.st_mode));
|
||||
assertEqualInt(0644, st.st_mode & 0777);
|
||||
assertEqualInt(10, st.st_size);
|
||||
failure("file %s/file", target);
|
||||
assertEqualInt(2, st.st_nlink);
|
||||
}
|
||||
|
||||
/* Another name for the same file. */
|
||||
r = lstat("linkfile", &st2);
|
||||
failure("Failed to stat file %s/linkfile, errno=%d", target, errno);
|
||||
assertEqualInt(r, 0);
|
||||
if (r == 0) {
|
||||
assert(S_ISREG(st2.st_mode));
|
||||
assertEqualInt(0644, st2.st_mode & 0777);
|
||||
assertEqualInt(10, st2.st_size);
|
||||
failure("file %s/linkfile", target);
|
||||
assertEqualInt(2, st2.st_nlink);
|
||||
/* Verify that the two are really hardlinked. */
|
||||
assertEqualInt(st.st_dev, st2.st_dev);
|
||||
failure("%s/linkfile and %s/file aren't really hardlinks", target, target);
|
||||
assertEqualInt(st.st_ino, st2.st_ino);
|
||||
}
|
||||
|
||||
/* Symlink */
|
||||
r = lstat("symlink", &st);
|
||||
failure("Failed to stat file %s/symlink, errno=%d", target, errno);
|
||||
assertEqualInt(r, 0);
|
||||
if (r == 0) {
|
||||
failure("symlink should be a symlink; actual mode is %o",
|
||||
st.st_mode);
|
||||
assert(S_ISLNK(st.st_mode));
|
||||
if (S_ISLNK(st.st_mode)) {
|
||||
r = readlink("symlink", buff, sizeof(buff));
|
||||
assertEqualInt(r, 4);
|
||||
buff[r] = '\0';
|
||||
assertEqualString(buff, "file");
|
||||
}
|
||||
}
|
||||
|
||||
/* dir */
|
||||
r = lstat("dir", &st);
|
||||
if (r == 0) {
|
||||
assertEqualInt(r, 0);
|
||||
assert(S_ISDIR(st.st_mode));
|
||||
assertEqualInt(0775, st.st_mode & 0777);
|
||||
}
|
||||
|
||||
chdir("..");
|
||||
}
|
||||
|
||||
DEFINE_TEST(test_basic)
|
||||
{
|
||||
int fd;
|
||||
int filelist;
|
||||
int oldumask;
|
||||
|
||||
oldumask = umask(0);
|
||||
|
||||
/*
|
||||
* Create an assortment of files on disk.
|
||||
*/
|
||||
filelist = open("filelist", O_CREAT | O_WRONLY, 0644);
|
||||
|
||||
/* File with 10 bytes content. */
|
||||
fd = open("file", O_CREAT | O_WRONLY, 0644);
|
||||
assert(fd >= 0);
|
||||
assertEqualInt(10, write(fd, "123456789", 10));
|
||||
close(fd);
|
||||
write(filelist, "file\n", 5);
|
||||
|
||||
/* hardlink to above file. */
|
||||
assertEqualInt(0, link("file", "linkfile"));
|
||||
write(filelist, "linkfile\n", 9);
|
||||
|
||||
/* Symlink to above file. */
|
||||
assertEqualInt(0, symlink("file", "symlink"));
|
||||
write(filelist, "symlink\n", 8);
|
||||
|
||||
/* Directory. */
|
||||
assertEqualInt(0, mkdir("dir", 0775));
|
||||
write(filelist, "dir\n", 4);
|
||||
/* All done. */
|
||||
close(filelist);
|
||||
|
||||
/* Archive/dearchive with a variety of options. */
|
||||
basic_tar("copy", "", "");
|
||||
/* tar doesn't handle cpio symlinks correctly */
|
||||
/* basic_tar("copy_odc", "--format=odc", ""); */
|
||||
basic_tar("copy_ustar", "--format=ustar", "");
|
||||
|
||||
umask(oldumask);
|
||||
}
|
300
usr.bin/tar/test/test_copy.c
Normal file
300
usr.bin/tar/test/test_copy.c
Normal file
@ -0,0 +1,300 @@
|
||||
/*-
|
||||
* Copyright (c) 2003-2007 Tim Kientzle
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "test.h"
|
||||
__FBSDID("$FreeBSD$");
|
||||
|
||||
static void
|
||||
create_tree(void)
|
||||
{
|
||||
char buff[260];
|
||||
char buff2[260];
|
||||
int i;
|
||||
int fd;
|
||||
|
||||
assertEqualInt(0, mkdir("original", 0775));
|
||||
chdir("original");
|
||||
|
||||
buff[0] = 'f';
|
||||
buff[1] = '_';
|
||||
for (i = 0; i < 200; i++) {
|
||||
/* Create a file named "f_abcdef..." */
|
||||
buff[i + 2] = 'a' + (i % 26);
|
||||
buff[i + 3] = '\0';
|
||||
fd = open(buff, O_CREAT | O_WRONLY, 0644);
|
||||
assert(fd >= 0);
|
||||
assertEqualInt(i + 3, write(fd, buff, strlen(buff)));
|
||||
close(fd);
|
||||
|
||||
/* Create a link named "l_abcdef..." to the above. */
|
||||
strcpy(buff2, buff);
|
||||
buff2[0] = 'l';
|
||||
assertEqualInt(0, link(buff, buff2));
|
||||
|
||||
/* Create a link named "m_abcdef..." to the above. */
|
||||
strcpy(buff2, buff);
|
||||
buff2[0] = 'm';
|
||||
assertEqualInt(0, link(buff, buff2));
|
||||
|
||||
/* Create a symlink named "s_abcdef..." to the above. */
|
||||
buff2[0] = 's';
|
||||
assertEqualInt(0, symlink(buff, buff2));
|
||||
|
||||
/* Create a dir named "d_abcdef...". */
|
||||
buff2[0] = 'd';
|
||||
assertEqualInt(0, mkdir(buff2, 0775));
|
||||
}
|
||||
|
||||
chdir("..");
|
||||
}
|
||||
|
||||
#define LIMIT_NONE 0
|
||||
#define LIMIT_USTAR 1
|
||||
|
||||
static void
|
||||
verify_tree(int limit)
|
||||
{
|
||||
struct stat st, st2;
|
||||
char filename[260];
|
||||
char contents[260];
|
||||
int i, r;
|
||||
int fd;
|
||||
int len;
|
||||
const char *p;
|
||||
DIR *d;
|
||||
struct dirent *de;
|
||||
|
||||
/* Generate the names we know should be there and verify them. */
|
||||
for (i = 0; i < 200; i++) {
|
||||
/* Verify a file named "f_abcdef..." */
|
||||
filename[0] = 'f';
|
||||
filename[1] = '_';
|
||||
filename[i + 2] = 'a' + (i % 26);
|
||||
filename[i + 3] = '\0';
|
||||
if (limit != LIMIT_USTAR || strlen(filename) <= 100) {
|
||||
fd = open(filename, O_RDONLY);
|
||||
failure("Couldn't open \"%s\": %s",
|
||||
filename, strerror(errno));
|
||||
if (assert(fd >= 0)) {
|
||||
len = read(fd, contents, i + 10);
|
||||
close(fd);
|
||||
assertEqualInt(len, i + 3);
|
||||
/* Verify contents of 'contents' */
|
||||
contents[len] = '\0';
|
||||
failure("Each test file contains its own name");
|
||||
assertEqualString(filename, contents);
|
||||
/* stat() file and get dev/ino for next check */
|
||||
assertEqualInt(0, lstat(filename, &st));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* ustar allows 100 chars for links, and we have
|
||||
* "original/" as part of the name, so the link
|
||||
* names here can't exceed 91 chars.
|
||||
*/
|
||||
if (limit != LIMIT_USTAR || strlen(filename) <= 91) {
|
||||
/* Verify hardlink "l_abcdef..." */
|
||||
filename[0] = 'l';
|
||||
assertEqualInt(0, (r = lstat(filename, &st2)));
|
||||
if (r == 0) {
|
||||
assertEqualInt(st2.st_dev, st.st_dev);
|
||||
assertEqualInt(st2.st_ino, st.st_ino);
|
||||
}
|
||||
|
||||
/* Verify hardlink "m_abcdef..." */
|
||||
filename[0] = 'm';
|
||||
assertEqualInt(0, (r = lstat(filename, &st2)));
|
||||
if (r == 0) {
|
||||
assertEqualInt(st2.st_dev, st.st_dev);
|
||||
assertEqualInt(st2.st_ino, st.st_ino);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Symlink text doesn't include the 'original/' prefix,
|
||||
* so the limit here is 100 characters.
|
||||
*/
|
||||
/* Verify symlink "s_abcdef..." */
|
||||
filename[0] = 's';
|
||||
if (limit != LIMIT_USTAR || strlen(filename) <= 100) {
|
||||
/* This is a symlink. */
|
||||
failure("Couldn't stat %s (length %d)",
|
||||
filename, strlen(filename));
|
||||
if (assertEqualInt(0, lstat(filename, &st2))) {
|
||||
assert(S_ISLNK(st2.st_mode));
|
||||
/* This is a symlink to the file above. */
|
||||
failure("Couldn't stat %s", filename);
|
||||
if (assertEqualInt(0, stat(filename, &st2))) {
|
||||
assertEqualInt(st2.st_dev, st.st_dev);
|
||||
assertEqualInt(st2.st_ino, st.st_ino);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Verify dir "d_abcdef...". */
|
||||
filename[0] = 'd';
|
||||
if (limit != LIMIT_USTAR || strlen(filename) < 100) {
|
||||
/* This is a dir. */
|
||||
failure("Couldn't stat %s (length %d)",
|
||||
filename, strlen(filename));
|
||||
if (assertEqualInt(0, lstat(filename, &st2))) {
|
||||
if (assert(S_ISDIR(st2.st_mode))) {
|
||||
/* TODO: opendir/readdir this
|
||||
* directory and make sure
|
||||
* it's empty.
|
||||
*/
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Now make sure nothing is there that shouldn't be. */
|
||||
d = opendir(".");
|
||||
while ((de = readdir(d)) != NULL) {
|
||||
p = de->d_name;
|
||||
switch(p[0]) {
|
||||
case 'l': case 'm':
|
||||
if (limit == LIMIT_USTAR) {
|
||||
failure("strlen(p) = %d", strlen(p));
|
||||
assert(strlen(p) < 92);
|
||||
}
|
||||
case 'd':
|
||||
if (limit == LIMIT_USTAR) {
|
||||
failure("strlen(p)=%d", strlen(p));
|
||||
assert(strlen(p) < 100);
|
||||
}
|
||||
case 'f': case 's':
|
||||
if (limit == LIMIT_USTAR) {
|
||||
failure("strlen(p)=%d", strlen(p));
|
||||
assert(strlen(p) < 101);
|
||||
}
|
||||
/* Our files have very particular filename patterns. */
|
||||
assert(p[1] == '_' && p[2] == 'a');
|
||||
assert(p[2] == 'a');
|
||||
p += 2;
|
||||
for (i = 0; p[i] != '\0' && i < 200; i++)
|
||||
assert(p[i] == 'a' + (i % 26));
|
||||
assert(p[i] == '\0');
|
||||
break;
|
||||
case '.':
|
||||
assert(p[1] == '\0' || (p[1] == '.' && p[2] == '\0'));
|
||||
break;
|
||||
default:
|
||||
failure("File %s shouldn't be here", p);
|
||||
assert(0);
|
||||
}
|
||||
|
||||
}
|
||||
closedir(d);
|
||||
}
|
||||
|
||||
static void
|
||||
copy_basic(void)
|
||||
{
|
||||
int r;
|
||||
|
||||
assertEqualInt(0, mkdir("plain", 0775));
|
||||
chdir("plain");
|
||||
|
||||
/*
|
||||
* Use the tar program to create an archive.
|
||||
*/
|
||||
r = systemf("%s cf archive -C .. original >pack.out 2>pack.err",
|
||||
testprog);
|
||||
failure("Error invoking \"%s cf\"", testprog);
|
||||
assertEqualInt(r, 0);
|
||||
|
||||
/* Verify that nothing went to stdout or stderr. */
|
||||
assertEmptyFile("pack.err");
|
||||
assertEmptyFile("pack.out");
|
||||
|
||||
/*
|
||||
* Use tar to unpack the archive into another directory.
|
||||
*/
|
||||
r = systemf("%s xf archive >unpack.out 2>unpack.err", testprog);
|
||||
failure("Error invoking %s xf archive", testprog);
|
||||
assertEqualInt(r, 0);
|
||||
|
||||
/* Verify that nothing went to stdout or stderr. */
|
||||
assertEmptyFile("unpack.err");
|
||||
assertEmptyFile("unpack.out");
|
||||
|
||||
chdir("original");
|
||||
verify_tree(LIMIT_NONE);
|
||||
chdir("../..");
|
||||
}
|
||||
|
||||
static void
|
||||
copy_ustar(void)
|
||||
{
|
||||
const char *target = "ustar";
|
||||
int r;
|
||||
|
||||
assertEqualInt(0, mkdir(target, 0775));
|
||||
chdir(target);
|
||||
|
||||
/*
|
||||
* Use the tar program to create an archive.
|
||||
*/
|
||||
r = systemf("%s cf archive --format=ustar -C .. original >pack.out 2>pack.err",
|
||||
testprog);
|
||||
failure("Error invoking \"%s cf archive --format=ustar\"", testprog);
|
||||
assertEqualInt(r, 0);
|
||||
|
||||
/* Verify that nothing went to stdout. */
|
||||
assertEmptyFile("pack.out");
|
||||
|
||||
/*
|
||||
* Use tar to unpack the archive into another directory.
|
||||
*/
|
||||
r = systemf("%s xf archive >unpack.out 2>unpack.err", testprog);
|
||||
failure("Error invoking %s xf archive", testprog);
|
||||
assertEqualInt(r, 0);
|
||||
|
||||
/* Verify that nothing went to stdout or stderr. */
|
||||
assertEmptyFile("unpack.err");
|
||||
assertEmptyFile("unpack.out");
|
||||
|
||||
chdir("original");
|
||||
verify_tree(LIMIT_USTAR);
|
||||
chdir("../..");
|
||||
}
|
||||
|
||||
DEFINE_TEST(test_copy)
|
||||
{
|
||||
int oldumask;
|
||||
|
||||
oldumask = umask(0);
|
||||
|
||||
create_tree(); /* Create sample files in "original" dir. */
|
||||
|
||||
/* Test simple "tar -c | tar -x" pipeline copy. */
|
||||
copy_basic();
|
||||
|
||||
/* Same, but constrain to ustar format. */
|
||||
copy_ustar();
|
||||
|
||||
umask(oldumask);
|
||||
}
|
38
usr.bin/tar/test/test_getdate.c
Normal file
38
usr.bin/tar/test/test_getdate.c
Normal file
@ -0,0 +1,38 @@
|
||||
/*-
|
||||
* Copyright (c) 2003-2007 Tim Kientzle
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "test.h"
|
||||
__FBSDID("$FreeBSD$");
|
||||
|
||||
/*
|
||||
* Verify that the getdate() function works.
|
||||
*/
|
||||
|
||||
time_t get_date(const char *);
|
||||
|
||||
DEFINE_TEST(test_getdate)
|
||||
{
|
||||
assertEqualInt(0, get_date("Jan 1, 1970 UTC"));
|
||||
/* TODO: Lots more tests here. */
|
||||
}
|
81
usr.bin/tar/test/test_help.c
Normal file
81
usr.bin/tar/test/test_help.c
Normal file
@ -0,0 +1,81 @@
|
||||
/*-
|
||||
* Copyright (c) 2003-2007 Tim Kientzle
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "test.h"
|
||||
__FBSDID("$FreeBSD$");
|
||||
|
||||
/*
|
||||
* Test that "--help", "-h", and "-W help" options all work and
|
||||
* generate reasonable output.
|
||||
*/
|
||||
|
||||
static int
|
||||
in_first_line(const char *p, const char *substring)
|
||||
{
|
||||
size_t l = strlen(substring);
|
||||
|
||||
while (*p != '\0' && *p != '\n') {
|
||||
if (memcmp(p, substring, l) == 0)
|
||||
return (1);
|
||||
++p;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
DEFINE_TEST(test_help)
|
||||
{
|
||||
int r;
|
||||
char *p;
|
||||
size_t plen;
|
||||
|
||||
/* Exercise --help option. */
|
||||
r = systemf("%s --help >help.stdout 2>help.stderr", testprog);
|
||||
failure("--help should generate nothing to stderr.");
|
||||
assertEmptyFile("help.stderr");
|
||||
/* Help message should start with name of program. */
|
||||
p = slurpfile(&plen, "help.stdout");
|
||||
failure("Help output should be long enough.");
|
||||
assert(plen >= 6);
|
||||
failure("First line of help output should contain 'bsdtar': %s", p);
|
||||
assert(in_first_line(p, "bsdtar"));
|
||||
/*
|
||||
* TODO: Extend this check to further verify that --help output
|
||||
* looks approximately right.
|
||||
*/
|
||||
free(p);
|
||||
|
||||
/* -h option should generate the same output. */
|
||||
r = systemf("%s -h >h.stdout 2>h.stderr", testprog);
|
||||
failure("-h should generate nothing to stderr.");
|
||||
assertEmptyFile("h.stderr");
|
||||
failure("stdout should be same for -h and --help");
|
||||
assertEqualFile("h.stdout", "help.stdout");
|
||||
|
||||
/* -W help should be another synonym. */
|
||||
r = systemf("%s -W help >Whelp.stdout 2>Whelp.stderr", testprog);
|
||||
failure("-W help should generate nothing to stderr.");
|
||||
assertEmptyFile("Whelp.stderr");
|
||||
failure("stdout should be same for -W help and --help");
|
||||
assertEqualFile("Whelp.stdout", "help.stdout");
|
||||
}
|
123
usr.bin/tar/test/test_option_T.c
Normal file
123
usr.bin/tar/test/test_option_T.c
Normal file
@ -0,0 +1,123 @@
|
||||
/*-
|
||||
* Copyright (c) 2003-2008 Tim Kientzle
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "test.h"
|
||||
__FBSDID("$FreeBSD$");
|
||||
|
||||
static int
|
||||
touch(const char *fn)
|
||||
{
|
||||
int fd = open(fn, O_RDWR | O_CREAT);
|
||||
failure("Couldn't create file '%s', fd=%d, errno=%d (%s)\n",
|
||||
fn, fd, errno, strerror(errno));
|
||||
if (!assert(fd > 0))
|
||||
return (0); /* Failure. */
|
||||
close(fd);
|
||||
return (1); /* Success */
|
||||
}
|
||||
|
||||
DEFINE_TEST(test_option_T)
|
||||
{
|
||||
FILE *f;
|
||||
|
||||
/* Create a simple dir heirarchy; bail if anything fails. */
|
||||
if (!assertEqualInt(0, mkdir("d1", 0755))) return;
|
||||
if (!assertEqualInt(0, mkdir("d1/d2", 0755))) return;
|
||||
if (!touch("d1/f1")) return;
|
||||
if (!touch("d1/f2")) return;
|
||||
if (!touch("d1/d2/f3")) return;
|
||||
if (!touch("d1/d2/f4")) return;
|
||||
if (!touch("d1/d2/f5")) return;
|
||||
|
||||
/* Populate a file list */
|
||||
f = fopen("filelist", "w+");
|
||||
if (!assert(f != NULL))
|
||||
return;
|
||||
fprintf(f, "d1/f1\n");
|
||||
fprintf(f, "d1/d2/f4\n");
|
||||
fclose(f);
|
||||
|
||||
/* Populate a second file list */
|
||||
f = fopen("filelist2", "w+");
|
||||
if (!assert(f != NULL))
|
||||
return;
|
||||
fprintf(f, "d1/d2/f3\n");
|
||||
fprintf(f, "d1/d2/f5\n");
|
||||
fclose(f);
|
||||
|
||||
/* Use -c -T to archive up the files. */
|
||||
systemf("%s -c -f test1.tar -T filelist > test1.out 2> test1.err",
|
||||
testprog);
|
||||
assertEmptyFile("test1.out");
|
||||
assertEmptyFile("test1.err");
|
||||
|
||||
/* Use -x -T to dearchive the files */
|
||||
if (!assertEqualInt(0, mkdir("test1", 0755))) return;
|
||||
systemf("%s -x -f test1.tar -T filelist -C test1"
|
||||
" > test1b.out 2> test1b.err", testprog);
|
||||
assertEmptyFile("test1b.out");
|
||||
assertEmptyFile("test1b.err");
|
||||
|
||||
/* Verify the files were extracted. */
|
||||
assertFileExists("test1/d1/f1");
|
||||
assertFileNotExists("test1/d1/f2");
|
||||
assertFileNotExists("test1/d1/d2/f3");
|
||||
assertFileExists("test1/d1/d2/f4");
|
||||
assertFileNotExists("test1/d1/d2/f5");
|
||||
|
||||
/* Use -r -T to add more files to the archive. */
|
||||
systemf("%s -r -f test1.tar -T filelist2 > test2.out 2> test2.err",
|
||||
testprog);
|
||||
assertEmptyFile("test2.out");
|
||||
assertEmptyFile("test2.err");
|
||||
|
||||
/* Use -x without -T to dearchive the files (ensure -r worked) */
|
||||
if (!assertEqualInt(0, mkdir("test3", 0755))) return;
|
||||
systemf("%s -x -f test1.tar -C test3"
|
||||
" > test3.out 2> test3.err", testprog);
|
||||
assertEmptyFile("test3.out");
|
||||
assertEmptyFile("test3.err");
|
||||
/* Verify the files were extracted.*/
|
||||
assertFileExists("test3/d1/f1");
|
||||
assertFileNotExists("test3/d1/f2");
|
||||
assertFileExists("test3/d1/d2/f3");
|
||||
assertFileExists("test3/d1/d2/f4");
|
||||
assertFileExists("test3/d1/d2/f5");
|
||||
|
||||
/* Use -x -T to dearchive the files (verify -x -T together) */
|
||||
if (!assertEqualInt(0, mkdir("test2", 0755))) return;
|
||||
systemf("%s -x -f test1.tar -T filelist -C test2"
|
||||
" > test2b.out 2> test2b.err", testprog);
|
||||
assertEmptyFile("test2b.out");
|
||||
assertEmptyFile("test2b.err");
|
||||
/* Verify the files were extracted.*/
|
||||
assertFileExists("test2/d1/f1");
|
||||
assertFileNotExists("test2/d1/f2");
|
||||
assertFileNotExists("test2/d1/d2/f3");
|
||||
assertFileExists("test2/d1/d2/f4");
|
||||
assertFileNotExists("test2/d1/d2/f5");
|
||||
|
||||
/* TODO: Include some use of -C directory-changing within the filelist. */
|
||||
/* I'm pretty sure -C within the filelist is broken on extract. */
|
||||
}
|
124
usr.bin/tar/test/test_stdio.c
Normal file
124
usr.bin/tar/test/test_stdio.c
Normal file
@ -0,0 +1,124 @@
|
||||
/*-
|
||||
* Copyright (c) 2003-2007 Tim Kientzle
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "test.h"
|
||||
__FBSDID("$FreeBSD$");
|
||||
|
||||
DEFINE_TEST(test_stdio)
|
||||
{
|
||||
int fd;
|
||||
int filelist;
|
||||
int oldumask;
|
||||
int r;
|
||||
|
||||
oldumask = umask(0);
|
||||
|
||||
/*
|
||||
* Create a couple of files on disk.
|
||||
*/
|
||||
filelist = open("filelist", O_CREAT | O_WRONLY, 0644);
|
||||
/* File */
|
||||
fd = open("f", O_CREAT | O_WRONLY, 0644);
|
||||
assert(fd >= 0);
|
||||
write(fd, "f\n", 2);
|
||||
close(fd);
|
||||
write(filelist, "f\n", 2);
|
||||
/* Link to above file. */
|
||||
assertEqualInt(0, link("f", "l"));
|
||||
write(filelist, "l\n", 2);
|
||||
close(filelist);
|
||||
|
||||
/*
|
||||
* Archive/dearchive with a variety of options, verifying
|
||||
* stdio paths.
|
||||
*/
|
||||
|
||||
/* 'cf' should generate no output unless there's an error. */
|
||||
r = systemf("%s cf archive f l >cf.out 2>cf.err", testprog);
|
||||
assertEqualInt(r, 0);
|
||||
assertEmptyFile("cf.out");
|
||||
assertEmptyFile("cf.err");
|
||||
|
||||
/* 'cvf' should generate file list on stderr, empty stdout. */
|
||||
r = systemf("%s cvf archive f l >cvf.out 2>cvf.err", testprog);
|
||||
assertEqualInt(r, 0);
|
||||
failure("'cv' writes filenames to stderr, nothing to stdout (SUSv2)\n"
|
||||
"Note that GNU tar writes the file list to stdout by default.");
|
||||
assertEmptyFile("cvf.out");
|
||||
/* TODO: Verify cvf.err has file list in SUSv2-prescribed format. */
|
||||
|
||||
/* 'cvf -' should generate file list on stderr, archive on stdout. */
|
||||
r = systemf("%s cvf - f l >cvf-.out 2>cvf-.err", testprog);
|
||||
assertEqualInt(r, 0);
|
||||
failure("cvf - should write archive to stdout");
|
||||
/* TODO: Verify cvf-.out has archive. */
|
||||
failure("cvf - should write file list to stderr (SUSv2)");
|
||||
/* TODO: Verify cvf-.err has verbose file list. */
|
||||
|
||||
/* 'tf' should generate file list on stdout, empty stderr. */
|
||||
r = systemf("%s tf archive >tf.out 2>tf.err", testprog);
|
||||
assertEqualInt(r, 0);
|
||||
assertEmptyFile("tf.err");
|
||||
failure("'t' mode should write results to stdout");
|
||||
/* TODO: Verify tf.out has file list. */
|
||||
|
||||
/* 'tvf' should generate file list on stdout, empty stderr. */
|
||||
r = systemf("%s tvf archive >tvf.out 2>tvf.err", testprog);
|
||||
assertEqualInt(r, 0);
|
||||
assertEmptyFile("tvf.err");
|
||||
failure("'tv' mode should write results to stdout");
|
||||
/* TODO: Verify tvf.out has file list. */
|
||||
|
||||
/* 'tvf -' uses stdin, file list on stdout, empty stderr. */
|
||||
r = systemf("%s tvf - < archive >tvf-.out 2>tvf-.err", testprog);
|
||||
assertEqualInt(r, 0);
|
||||
assertEmptyFile("tvf-.err");
|
||||
/* TODO: Verify tvf-.out has file list. */
|
||||
|
||||
/* Basic 'xf' should generate no output on stdout or stderr. */
|
||||
r = systemf("%s xf archive >xf.out 2>xf.err", testprog);
|
||||
assertEqualInt(r, 0);
|
||||
assertEmptyFile("xf.err");
|
||||
assertEmptyFile("xf.out");
|
||||
|
||||
/* 'xvf' should generate list on stderr, empty stdout. */
|
||||
r = systemf("%s xvf archive >xvf.out 2>xvf.err", testprog);
|
||||
assertEqualInt(r, 0);
|
||||
assertEmptyFile("xvf.out");
|
||||
/* TODO: Verify xvf.err */
|
||||
|
||||
/* 'xvOf' should generate list on stderr, file contents on stdout. */
|
||||
r = systemf("%s xvOf archive >xvOf.out 2>xvOf.err", testprog);
|
||||
assertEqualInt(r, 0);
|
||||
/* TODO: Verify xvOf.out */
|
||||
/* TODO: Verify xvf.err */
|
||||
|
||||
/* 'xvf -' should generate list on stderr, empty stdout. */
|
||||
r = systemf("%s xvf - < archive >xvf-.out 2>xvf-.err", testprog);
|
||||
assertEqualInt(r, 0);
|
||||
assertEmptyFile("xvf-.out");
|
||||
/* TODO: Verify xvf-.err */
|
||||
|
||||
umask(oldumask);
|
||||
}
|
93
usr.bin/tar/test/test_version.c
Normal file
93
usr.bin/tar/test/test_version.c
Normal file
@ -0,0 +1,93 @@
|
||||
/*-
|
||||
* Copyright (c) 2003-2007 Tim Kientzle
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "test.h"
|
||||
__FBSDID("$FreeBSD$");
|
||||
|
||||
/*
|
||||
* Test that --version option works and generates reasonable output.
|
||||
*/
|
||||
|
||||
DEFINE_TEST(test_version)
|
||||
{
|
||||
int r;
|
||||
char *p, *q;
|
||||
size_t s;
|
||||
|
||||
|
||||
r = systemf("%s --version >version.stdout 2>version.stderr", testprog);
|
||||
if (r != 0)
|
||||
r = systemf("%s -W version >version.stdout 2>version.stderr",
|
||||
testprog);
|
||||
failure("Unable to run either %s --version or %s -W version",
|
||||
testprog, testprog);
|
||||
if (!assert(r == 0))
|
||||
return;
|
||||
|
||||
/* --version should generate nothing to stdout. */
|
||||
assertEmptyFile("version.stderr");
|
||||
/* Verify format of version message. */
|
||||
q = p = slurpfile(&s, "version.stdout");
|
||||
/* Version message should start with name of program, then space. */
|
||||
assert(s > 6);
|
||||
failure("Version: %s", p);
|
||||
assertEqualMem(q, "bsdtar ", 7);
|
||||
q += 7; s -= 7;
|
||||
/* Version number is a series of digits and periods. */
|
||||
while (s > 0 && (*q == '.' || (*q >= '0' && *q <= '9'))) {
|
||||
++q;
|
||||
--s;
|
||||
}
|
||||
/* Version number terminated by space. */
|
||||
failure("Version: %s", p);
|
||||
assert(s > 1);
|
||||
/* Skip a single trailing a,b,c, or d. */
|
||||
if (*q == 'a' || *q == 'b' || *q == 'c' || *q == 'd')
|
||||
++q;
|
||||
failure("Version: %s", p);
|
||||
assert(*q == ' ');
|
||||
++q; --s;
|
||||
/* Separator. */
|
||||
failure("Version: %s", p);
|
||||
assertEqualMem(q, "- ", 2);
|
||||
q += 2; s -= 2;
|
||||
/* libarchive name and version number */
|
||||
failure("Version: %s", p);
|
||||
assert(s > 11);
|
||||
failure("Version: %s", p);
|
||||
assertEqualMem(q, "libarchive ", 11);
|
||||
q += 11; s -= 11;
|
||||
/* Version number is a series of digits and periods. */
|
||||
while (s > 0 && (*q == '.' || (*q >= '0' && *q <= '9'))) {
|
||||
++q;
|
||||
--s;
|
||||
}
|
||||
/* Skip a single trailing a,b,c, or d. */
|
||||
if (*q == 'a' || *q == 'b' || *q == 'c' || *q == 'd')
|
||||
++q;
|
||||
/* All terminated by a newline. */
|
||||
assert(s >= 1);
|
||||
assertEqualMem(q, "\n", 1);
|
||||
free(p);
|
||||
}
|
Loading…
Reference in New Issue
Block a user