Blame tar/test/test_basic.c

Packit Service 1d0348
/*-
Packit Service 1d0348
 * Copyright (c) 2003-2007 Tim Kientzle
Packit Service 1d0348
 * All rights reserved.
Packit Service 1d0348
 *
Packit Service 1d0348
 * Redistribution and use in source and binary forms, with or without
Packit Service 1d0348
 * modification, are permitted provided that the following conditions
Packit Service 1d0348
 * are met:
Packit Service 1d0348
 * 1. Redistributions of source code must retain the above copyright
Packit Service 1d0348
 *    notice, this list of conditions and the following disclaimer.
Packit Service 1d0348
 * 2. Redistributions in binary form must reproduce the above copyright
Packit Service 1d0348
 *    notice, this list of conditions and the following disclaimer in the
Packit Service 1d0348
 *    documentation and/or other materials provided with the distribution.
Packit Service 1d0348
 *
Packit Service 1d0348
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
Packit Service 1d0348
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit Service 1d0348
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Packit Service 1d0348
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit Service 1d0348
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit Service 1d0348
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit Service 1d0348
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit Service 1d0348
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit Service 1d0348
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Packit Service 1d0348
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 1d0348
 */
Packit Service 1d0348
#include "test.h"
Packit Service 1d0348
__FBSDID("$FreeBSD: src/usr.bin/tar/test/test_basic.c,v 1.2 2008/05/26 17:10:10 kientzle Exp $");
Packit Service 1d0348
Packit Service 1d0348
static const char *
Packit Service 1d0348
make_files(void)
Packit Service 1d0348
{
Packit Service 1d0348
	FILE *f;
Packit Service 1d0348
Packit Service 1d0348
	/* File with 10 bytes content. */
Packit Service 1d0348
	f = fopen("file", "wb");
Packit Service 1d0348
	assert(f != NULL);
Packit Service 1d0348
	assertEqualInt(10, fwrite("123456789", 1, 10, f));
Packit Service 1d0348
	fclose(f);
Packit Service 1d0348
Packit Service 1d0348
	/* hardlink to above file. */
Packit Service 1d0348
	assertMakeHardlink("linkfile", "file");
Packit Service 1d0348
	assertIsHardlink("file", "linkfile");
Packit Service 1d0348
Packit Service 1d0348
	/* Symlink to above file. */
Packit Service 1d0348
	if (canSymlink())
Packit Service 1d0348
		assertMakeSymlink("symlink", "file");
Packit Service 1d0348
Packit Service 1d0348
	/* Directory. */
Packit Service 1d0348
	assertMakeDir("dir", 0775);
Packit Service 1d0348
Packit Service 1d0348
	return canSymlink()
Packit Service 1d0348
	    ? "file linkfile symlink dir"
Packit Service 1d0348
	    : "file linkfile dir";
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static void
Packit Service 1d0348
verify_files(const char *target)
Packit Service 1d0348
{
Packit Service 1d0348
	assertChdir(target);
Packit Service 1d0348
Packit Service 1d0348
	/* Regular file with 2 links. */
Packit Service 1d0348
	failure("%s", target);
Packit Service 1d0348
	assertIsReg("file", -1);
Packit Service 1d0348
	failure("%s", target);
Packit Service 1d0348
	assertFileSize("file", 10);
Packit Service 1d0348
	failure("%s", target);
Packit Service 1d0348
	assertFileContents("123456789", 10, "file");
Packit Service 1d0348
	failure("%s", target);
Packit Service 1d0348
	assertFileNLinks("file", 2);
Packit Service 1d0348
Packit Service 1d0348
	/* Another name for the same file. */
Packit Service 1d0348
	failure("%s", target);
Packit Service 1d0348
	assertIsReg("linkfile", -1);
Packit Service 1d0348
	failure("%s", target);
Packit Service 1d0348
	assertFileSize("linkfile", 10);
Packit Service 1d0348
	assertFileContents("123456789", 10, "linkfile");
Packit Service 1d0348
	assertFileNLinks("linkfile", 2);
Packit Service 1d0348
	assertIsHardlink("file", "linkfile");
Packit Service 1d0348
Packit Service 1d0348
	/* Symlink */
Packit Service 1d0348
	if (canSymlink())
Packit Service 1d0348
		assertIsSymlink("symlink", "file");
Packit Service 1d0348
Packit Service 1d0348
	/* dir */
Packit Service 1d0348
	failure("%s", target);
Packit Service 1d0348
	assertIsDir("dir", 0775);
Packit Service 1d0348
	assertChdir("..");
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static void
Packit Service 1d0348
run_tar(const char *target, const char *pack_options,
Packit Service 1d0348
    const char *unpack_options, const char *flist)
Packit Service 1d0348
{
Packit Service 1d0348
	int r;
Packit Service 1d0348
Packit Service 1d0348
	assertMakeDir(target, 0775);
Packit Service 1d0348
Packit Service 1d0348
	/* Use the tar program to create an archive. */
Packit Service 1d0348
	r = systemf("%s cf - %s %s >%s/archive 2>%s/pack.err", testprog, pack_options, flist, target, target);
Packit Service 1d0348
	failure("Error invoking %s cf -", testprog, pack_options);
Packit Service 1d0348
	assertEqualInt(r, 0);
Packit Service 1d0348
Packit Service 1d0348
	assertChdir(target);
Packit Service 1d0348
Packit Service 1d0348
	/* Verify that nothing went to stderr. */
Packit Service 1d0348
	assertEmptyFile("pack.err");
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Use tar to unpack the archive into another directory.
Packit Service 1d0348
	 */
Packit Service 1d0348
	r = systemf("%s xf archive %s >unpack.out 2>unpack.err",
Packit Service 1d0348
	    testprog, unpack_options);
Packit Service 1d0348
	failure("Error invoking %s xf archive %s", testprog, unpack_options);
Packit Service 1d0348
	assertEqualInt(r, 0);
Packit Service 1d0348
Packit Service 1d0348
	/* Verify that nothing went to stderr. */
Packit Service 1d0348
	assertEmptyFile("unpack.err");
Packit Service 1d0348
	assertChdir("..");
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
DEFINE_TEST(test_basic)
Packit Service 1d0348
{
Packit Service 1d0348
	const char *flist;
Packit Service 1d0348
Packit Service 1d0348
	assertUmask(0);
Packit Service 1d0348
	flist = make_files();
Packit Service 1d0348
	/* Archive/dearchive with a variety of options. */
Packit Service 1d0348
	run_tar("copy", "", "", flist);
Packit Service 1d0348
	verify_files("copy");
Packit Service 1d0348
Packit Service 1d0348
	run_tar("copy_ustar", "--format=ustar", "", flist);
Packit Service 1d0348
	verify_files("copy_ustar");
Packit Service 1d0348
Packit Service 1d0348
	/* tar doesn't handle cpio symlinks correctly */
Packit Service 1d0348
	/* run_tar("copy_odc", "--format=odc", ""); */
Packit Service 1d0348
}