Blame tar/test/test_copy.c

Packit 08bd4c
/*-
Packit 08bd4c
 * Copyright (c) 2003-2007 Tim Kientzle
Packit 08bd4c
 * All rights reserved.
Packit 08bd4c
 *
Packit 08bd4c
 * Redistribution and use in source and binary forms, with or without
Packit 08bd4c
 * modification, are permitted provided that the following conditions
Packit 08bd4c
 * are met:
Packit 08bd4c
 * 1. Redistributions of source code must retain the above copyright
Packit 08bd4c
 *    notice, this list of conditions and the following disclaimer.
Packit 08bd4c
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 08bd4c
 *    notice, this list of conditions and the following disclaimer in the
Packit 08bd4c
 *    documentation and/or other materials provided with the distribution.
Packit 08bd4c
 *
Packit 08bd4c
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
Packit 08bd4c
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit 08bd4c
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Packit 08bd4c
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit 08bd4c
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit 08bd4c
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 08bd4c
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 08bd4c
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 08bd4c
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Packit 08bd4c
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 08bd4c
 */
Packit 08bd4c
#include "test.h"
Packit 08bd4c
__FBSDID("$FreeBSD: src/usr.bin/tar/test/test_copy.c,v 1.3 2008/08/15 06:12:02 kientzle Exp $");
Packit 08bd4c
Packit 08bd4c
#if defined(__CYGWIN__)
Packit 08bd4c
# include <limits.h>
Packit 08bd4c
# include <sys/cygwin.h>
Packit 08bd4c
#endif
Packit 08bd4c
#if defined(_WIN32) && !defined(__CYGWIN__)
Packit 08bd4c
# include <direct.h>
Packit 08bd4c
#endif
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * Try to figure out how deep we can go in our tests.  Assumes that
Packit 08bd4c
 * the first call to this function has the longest starting cwd (which
Packit 08bd4c
 * is currently "<testdir>/original").  This is mostly to work around
Packit 08bd4c
 * limits in our Win32 support.
Packit 08bd4c
 *
Packit 08bd4c
 * Background: On Posix systems, PATH_MAX is merely a limit on the
Packit 08bd4c
 * length of the string passed into a system call.  By repeatedly
Packit 08bd4c
 * calling chdir(), you can work with arbitrarily long paths on such
Packit 08bd4c
 * systems.  In contrast, Win32 APIs apply PATH_MAX limits to the full
Packit 08bd4c
 * absolute path, so the permissible length of a system call argument
Packit 08bd4c
 * varies with the cwd. Some APIs actually enforce limits
Packit 08bd4c
 * significantly less than PATH_MAX to ensure that you can create
Packit 08bd4c
 * files within the current working directory.  The Win32 limits also
Packit 08bd4c
 * apply to Cygwin before 1.7.
Packit 08bd4c
 *
Packit 08bd4c
 * Someday, I want to convert the Win32 support to use newer
Packit 08bd4c
 * wide-character paths with '\\?\' prefix, which has a 32k PATH_MAX
Packit 08bd4c
 * instead of the rather anemic 260 character limit of the older
Packit 08bd4c
 * system calls.  Then we can drop this mess (unless we want to
Packit 08bd4c
 * continue to special-case Cygwin 1.5 and earlier).
Packit 08bd4c
 */
Packit 08bd4c
static int
Packit 08bd4c
compute_loop_max(void)
Packit 08bd4c
{
Packit 08bd4c
#if defined(_WIN32) && !defined(__CYGWIN__)
Packit 08bd4c
	static int LOOP_MAX = 0;
Packit 08bd4c
	char buf[MAX_PATH];
Packit 08bd4c
	size_t cwdlen;
Packit 08bd4c
Packit 08bd4c
	if (LOOP_MAX == 0) {
Packit 08bd4c
		assert(_getcwd(buf, MAX_PATH) != NULL);
Packit 08bd4c
		cwdlen = strlen(buf);
Packit 08bd4c
		/* 12 characters = length of 8.3 filename */
Packit 08bd4c
		/* 4 characters = length of "/../" used in symlink tests */
Packit 08bd4c
		/* 1 character = length of extra "/" separator */
Packit 08bd4c
		LOOP_MAX = MAX_PATH - (int)cwdlen - 12 - 4 - 1;
Packit 08bd4c
	}
Packit 08bd4c
	return LOOP_MAX;
Packit 08bd4c
#elif defined(__CYGWIN__) && !defined(HAVE_CYGWIN_CONV_PATH)
Packit 08bd4c
	static int LOOP_MAX = 0;
Packit 08bd4c
	if (LOOP_MAX == 0) {
Packit 08bd4c
		char wbuf[PATH_MAX];
Packit 08bd4c
		char pbuf[PATH_MAX];
Packit 08bd4c
		size_t wcwdlen;
Packit 08bd4c
		size_t pcwdlen;
Packit 08bd4c
	        size_t cwdlen;
Packit 08bd4c
		assert(getcwd(pbuf, PATH_MAX) != NULL);
Packit 08bd4c
		pcwdlen = strlen(pbuf);
Packit 08bd4c
		cygwin_conv_to_full_win32_path(pbuf, wbuf);
Packit 08bd4c
		wcwdlen = strlen(wbuf);
Packit 08bd4c
		cwdlen = ((wcwdlen > pcwdlen) ? wcwdlen : pcwdlen);
Packit 08bd4c
		/* Cygwin helper needs an extra few characters. */
Packit 08bd4c
		LOOP_MAX = PATH_MAX - (int)cwdlen - 12 - 4 - 4;
Packit 08bd4c
	}
Packit 08bd4c
	return LOOP_MAX;
Packit 08bd4c
#else
Packit 08bd4c
	/* cygwin-1.7 ends up here, along with "normal" unix */
Packit 08bd4c
	return 200; /* restore pre-r278 depth */
Packit 08bd4c
#endif
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
/* filenames[i] is a distinctive filename of length i. */
Packit 08bd4c
/* To simplify interpreting failures, each filename ends with a
Packit 08bd4c
 * decimal integer which is the length of the filename.  E.g., A
Packit 08bd4c
 * filename ending in "_92" is 92 characters long.  To detect errors
Packit 08bd4c
 * which drop or misplace characters, the filenames use a repeating
Packit 08bd4c
 * "abcdefghijklmnopqrstuvwxyz..." pattern. */
Packit 08bd4c
static char *filenames[201];
Packit 08bd4c
Packit 08bd4c
static void
Packit 08bd4c
compute_filenames(void)
Packit 08bd4c
{
Packit 08bd4c
	char buff[250];
Packit 08bd4c
	size_t i,j;
Packit 08bd4c
Packit 08bd4c
	filenames[0] = strdup("");
Packit 08bd4c
	filenames[1] = strdup("1");
Packit 08bd4c
	filenames[2] = strdup("a2");
Packit 08bd4c
	for (i = 3; i < sizeof(filenames)/sizeof(filenames[0]); ++i) {
Packit 08bd4c
		/* Fill with "abcdefghij..." */
Packit 08bd4c
		for (j = 0; j < i; ++j)
Packit 08bd4c
			buff[j] = 'a' + (j % 26);
Packit 08bd4c
		buff[j--] = '\0';
Packit 08bd4c
		/* Work from the end to fill in the number portion. */
Packit 08bd4c
		buff[j--] = '0' + (i % 10);
Packit 08bd4c
		if (i > 9) {
Packit 08bd4c
			buff[j--] = '0' + ((i / 10) % 10);
Packit 08bd4c
			if (i > 99)
Packit 08bd4c
				buff[j--] = '0' + (char)(i / 100);
Packit 08bd4c
		}
Packit 08bd4c
		buff[j] = '_';
Packit 08bd4c
		/* Guard against obvious screwups in the above code. */
Packit 08bd4c
		assertEqualInt(strlen(buff), i);
Packit 08bd4c
		filenames[i] = strdup(buff);
Packit 08bd4c
	}
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
static void
Packit 08bd4c
create_tree(void)
Packit 08bd4c
{
Packit 08bd4c
	char buff[260];
Packit 08bd4c
	char buff2[260];
Packit 08bd4c
	int i;
Packit 08bd4c
	int LOOP_MAX;
Packit 08bd4c
Packit 08bd4c
	compute_filenames();
Packit 08bd4c
Packit 08bd4c
	/* Log that we'll be omitting some checks. */
Packit 08bd4c
	if (!canSymlink()) {
Packit 08bd4c
		skipping("Symlink checks");
Packit 08bd4c
	}
Packit 08bd4c
Packit 08bd4c
	assertMakeDir("original", 0775);
Packit 08bd4c
	assertEqualInt(0, chdir("original"));
Packit 08bd4c
	LOOP_MAX = compute_loop_max();
Packit 08bd4c
Packit 08bd4c
	assertMakeDir("f", 0775);
Packit 08bd4c
	assertMakeDir("l", 0775);
Packit 08bd4c
	assertMakeDir("m", 0775);
Packit 08bd4c
	assertMakeDir("s", 0775);
Packit 08bd4c
	assertMakeDir("d", 0775);
Packit 08bd4c
Packit 08bd4c
	for (i = 1; i < LOOP_MAX; i++) {
Packit 08bd4c
		failure("Internal sanity check failed: i = %d", i);
Packit 08bd4c
		assert(filenames[i] != NULL);
Packit 08bd4c
Packit 08bd4c
		sprintf(buff, "f/%s", filenames[i]);
Packit 08bd4c
		assertMakeFile(buff, 0777, buff);
Packit 08bd4c
Packit 08bd4c
		/* Create a link named "l/abcdef..." to the above. */
Packit 08bd4c
		sprintf(buff2, "l/%s", filenames[i]);
Packit 08bd4c
		assertMakeHardlink(buff2, buff);
Packit 08bd4c
Packit 08bd4c
		/* Create a link named "m/abcdef..." to the above. */
Packit 08bd4c
		sprintf(buff2, "m/%s", filenames[i]);
Packit 08bd4c
		assertMakeHardlink(buff2, buff);
Packit 08bd4c
Packit 08bd4c
		if (canSymlink()) {
Packit 08bd4c
			/* Create a symlink named "s/abcdef..." to the above. */
Packit 08bd4c
			sprintf(buff, "s/%s", filenames[i]);
Packit 08bd4c
			sprintf(buff2, "../f/%s", filenames[i]);
Packit 08bd4c
			failure("buff=\"%s\" buff2=\"%s\"", buff, buff2);
Packit 08bd4c
			assertMakeSymlink(buff, buff2);
Packit 08bd4c
		}
Packit 08bd4c
		/* Create a dir named "d/abcdef...". */
Packit 08bd4c
		buff[0] = 'd';
Packit 08bd4c
		failure("buff=\"%s\"", buff);
Packit 08bd4c
		assertMakeDir(buff, 0775);
Packit 08bd4c
	}
Packit 08bd4c
Packit 08bd4c
	assertEqualInt(0, chdir(".."));
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
#define LIMIT_NONE 200
Packit 08bd4c
#define LIMIT_USTAR 100
Packit 08bd4c
Packit 08bd4c
static void
Packit 08bd4c
verify_tree(size_t limit)
Packit 08bd4c
{
Packit 08bd4c
	char name1[260];
Packit 08bd4c
	char name2[260];
Packit 08bd4c
	size_t i, LOOP_MAX;
Packit 08bd4c
Packit 08bd4c
	LOOP_MAX = compute_loop_max();
Packit 08bd4c
Packit 08bd4c
	/* Generate the names we know should be there and verify them. */
Packit 08bd4c
	for (i = 1; i < LOOP_MAX; i++) {
Packit 08bd4c
		/* Verify a file named "f/abcdef..." */
Packit 08bd4c
		sprintf(name1, "f/%s", filenames[i]);
Packit 08bd4c
		if (i <= limit) {
Packit 08bd4c
			assertFileExists(name1);
Packit 08bd4c
			assertFileContents(name1, (int)strlen(name1), name1);
Packit 08bd4c
		}
Packit 08bd4c
Packit 08bd4c
		sprintf(name2, "l/%s", filenames[i]);
Packit 08bd4c
		if (i + 2 <= limit) {
Packit 08bd4c
			/* Verify hardlink "l/abcdef..." */
Packit 08bd4c
			assertIsHardlink(name1, name2);
Packit 08bd4c
			/* Verify hardlink "m/abcdef..." */
Packit 08bd4c
			name2[0] = 'm';
Packit 08bd4c
			assertIsHardlink(name1, name2);
Packit 08bd4c
		}
Packit 08bd4c
Packit 08bd4c
		if (canSymlink()) {
Packit 08bd4c
			/* Verify symlink "s/abcdef..." */
Packit 08bd4c
			sprintf(name1, "s/%s", filenames[i]);
Packit 08bd4c
			sprintf(name2, "../f/%s", filenames[i]);
Packit 08bd4c
			if (strlen(name2) <= limit)
Packit 08bd4c
				assertIsSymlink(name1, name2);
Packit 08bd4c
		}
Packit 08bd4c
Packit 08bd4c
		/* Verify dir "d/abcdef...". */
Packit 08bd4c
		sprintf(name1, "d/%s", filenames[i]);
Packit 08bd4c
		if (i + 1 <= limit) { /* +1 for trailing slash */
Packit 08bd4c
			if (assertIsDir(name1, -1)) {
Packit 08bd4c
				/* TODO: opendir/readdir this
Packit 08bd4c
				 * directory and make sure
Packit 08bd4c
				 * it's empty.
Packit 08bd4c
				 */
Packit 08bd4c
			}
Packit 08bd4c
		}
Packit 08bd4c
	}
Packit 08bd4c
Packit 08bd4c
#if !defined(_WIN32) || defined(__CYGWIN__)
Packit 08bd4c
	{
Packit 08bd4c
		const char *dp;
Packit 08bd4c
		/* Now make sure nothing is there that shouldn't be. */
Packit 08bd4c
		for (dp = "dflms"; *dp != '\0'; ++dp) {
Packit 08bd4c
			DIR *d;
Packit 08bd4c
			struct dirent *de;
Packit 08bd4c
			char dir[2];
Packit 08bd4c
			dir[0] = *dp; dir[1] = '\0';
Packit 08bd4c
			d = opendir(dir);
Packit 08bd4c
			failure("Unable to open dir '%s'", dir);
Packit 08bd4c
			if (!assert(d != NULL))
Packit 08bd4c
				continue;
Packit 08bd4c
			while ((de = readdir(d)) != NULL) {
Packit 08bd4c
				char *p = de->d_name;
Packit 08bd4c
				if (p[0] == '.')
Packit 08bd4c
					continue;
Packit 08bd4c
				switch(dp[0]) {
Packit 08bd4c
				case 'l': case 'm': case 'd':
Packit 08bd4c
					failure("strlen(p)=%d", strlen(p));
Packit 08bd4c
					assert(strlen(p) < limit);
Packit 08bd4c
					assertEqualString(p,
Packit 08bd4c
					    filenames[strlen(p)]);
Packit 08bd4c
					break;
Packit 08bd4c
				case 'f': case 's':
Packit 08bd4c
					failure("strlen(p)=%d", strlen(p));
Packit 08bd4c
					assert(strlen(p) < limit + 1);
Packit 08bd4c
					assertEqualString(p,
Packit 08bd4c
					    filenames[strlen(p)]);
Packit 08bd4c
					break;
Packit 08bd4c
				default:
Packit 08bd4c
					failure("File %s shouldn't be here", p);
Packit 08bd4c
					assert(0);
Packit 08bd4c
				}
Packit 08bd4c
			}
Packit 08bd4c
			closedir(d);
Packit 08bd4c
		}
Packit 08bd4c
	}
Packit 08bd4c
#endif
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
static void
Packit 08bd4c
copy_basic(void)
Packit 08bd4c
{
Packit 08bd4c
	int r;
Packit 08bd4c
Packit 08bd4c
	/* NOTE: for proper operation on cygwin-1.5 and windows, the
Packit 08bd4c
	 * length of the name of the directory below, "plain", must be
Packit 08bd4c
	 * less than or equal to the length of the name of the original
Packit 08bd4c
	 * directory, "original"  This restriction derives from the
Packit 08bd4c
	 * extremely limited pathname lengths on those platforms.
Packit 08bd4c
	 */
Packit 08bd4c
	assertMakeDir("plain", 0775);
Packit 08bd4c
	assertEqualInt(0, chdir("plain"));
Packit 08bd4c
Packit 08bd4c
	/*
Packit 08bd4c
	 * Use the tar program to create an archive.
Packit 08bd4c
	 */
Packit 08bd4c
	r = systemf("%s cf archive -C ../original f d l m s >pack.out 2>pack.err",
Packit 08bd4c
	    testprog);
Packit 08bd4c
	failure("Error invoking \"%s cf\"", testprog);
Packit 08bd4c
	assertEqualInt(r, 0);
Packit 08bd4c
Packit 08bd4c
	/* Verify that nothing went to stdout or stderr. */
Packit 08bd4c
	assertEmptyFile("pack.err");
Packit 08bd4c
	assertEmptyFile("pack.out");
Packit 08bd4c
Packit 08bd4c
	/*
Packit 08bd4c
	 * Use tar to unpack the archive into another directory.
Packit 08bd4c
	 */
Packit 08bd4c
	r = systemf("%s xf archive >unpack.out 2>unpack.err", testprog);
Packit 08bd4c
	failure("Error invoking %s xf archive", testprog);
Packit 08bd4c
	assertEqualInt(r, 0);
Packit 08bd4c
Packit 08bd4c
	/* Verify that nothing went to stdout or stderr. */
Packit 08bd4c
	assertEmptyFile("unpack.err");
Packit 08bd4c
	assertEmptyFile("unpack.out");
Packit 08bd4c
Packit 08bd4c
	verify_tree(LIMIT_NONE);
Packit 08bd4c
	assertEqualInt(0, chdir(".."));
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
static void
Packit 08bd4c
copy_ustar(void)
Packit 08bd4c
{
Packit 08bd4c
	const char *target = "ustar";
Packit 08bd4c
	int r;
Packit 08bd4c
Packit 08bd4c
	/* NOTE: for proper operation on cygwin-1.5 and windows, the
Packit 08bd4c
	 * length of the name of the directory below, "ustar", must be
Packit 08bd4c
	 * less than or equal to the length of the name of the original
Packit 08bd4c
	 * directory, "original"  This restriction derives from the
Packit 08bd4c
	 * extremely limited pathname lengths on those platforms.
Packit 08bd4c
	 */
Packit 08bd4c
	assertMakeDir(target, 0775);
Packit 08bd4c
	assertEqualInt(0, chdir(target));
Packit 08bd4c
Packit 08bd4c
	/*
Packit 08bd4c
	 * Use the tar program to create an archive.
Packit 08bd4c
	 */
Packit 08bd4c
	r = systemf("%s cf archive --format=ustar -C ../original f d l m s >pack.out 2>pack.err",
Packit 08bd4c
	    testprog);
Packit 08bd4c
	failure("Error invoking \"%s cf archive --format=ustar\"", testprog);
Packit 08bd4c
	assertEqualInt(r, 0);
Packit 08bd4c
Packit 08bd4c
	/* Verify that nothing went to stdout. */
Packit 08bd4c
	assertEmptyFile("pack.out");
Packit 08bd4c
	/* Stderr is non-empty, since there are a bunch of files
Packit 08bd4c
	 * with filenames too long to archive. */
Packit 08bd4c
Packit 08bd4c
	/*
Packit 08bd4c
	 * Use tar to unpack the archive into another directory.
Packit 08bd4c
	 */
Packit 08bd4c
	r = systemf("%s xf archive >unpack.out 2>unpack.err", testprog);
Packit 08bd4c
	failure("Error invoking %s xf archive", testprog);
Packit 08bd4c
	assertEqualInt(r, 0);
Packit 08bd4c
Packit 08bd4c
	/* Verify that nothing went to stdout or stderr. */
Packit 08bd4c
	assertEmptyFile("unpack.err");
Packit 08bd4c
	assertEmptyFile("unpack.out");
Packit 08bd4c
Packit 08bd4c
	verify_tree(LIMIT_USTAR);
Packit 08bd4c
	assertEqualInt(0, chdir("../.."));
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
DEFINE_TEST(test_copy)
Packit 08bd4c
{
Packit 08bd4c
	assertUmask(0);
Packit 08bd4c
	create_tree(); /* Create sample files in "original" dir. */
Packit 08bd4c
Packit 08bd4c
	/* Test simple "tar -c | tar -x" pipeline copy. */
Packit 08bd4c
	copy_basic();
Packit 08bd4c
Packit 08bd4c
	/* Same, but constrain to ustar format. */
Packit 08bd4c
	copy_ustar();
Packit 08bd4c
}