Blame libarchive/test/test_read_extract.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: head/lib/libarchive/test/test_read_extract.c 201247 2009-12-30 05:59:21Z kientzle $");
Packit Service 1d0348
Packit Service 1d0348
#define BUFF_SIZE 1000000
Packit Service 1d0348
#define FILE_BUFF_SIZE 100000
Packit Service 1d0348
Packit Service 1d0348
DEFINE_TEST(test_read_extract)
Packit Service 1d0348
{
Packit Service 1d0348
	struct archive_entry *ae;
Packit Service 1d0348
	struct archive *a;
Packit Service 1d0348
	size_t used;
Packit Service 1d0348
	int i, numEntries = 0;
Packit Service 1d0348
	char *buff, *file_buff;
Packit Service 1d0348
Packit Service 1d0348
	buff = malloc(BUFF_SIZE);
Packit Service 1d0348
	file_buff = malloc(FILE_BUFF_SIZE);
Packit Service 1d0348
Packit Service 1d0348
	/* Force the umask to something predictable. */
Packit Service 1d0348
	assertUmask(022);
Packit Service 1d0348
Packit Service 1d0348
	/* Create a new archive in memory containing various types of entries. */
Packit Service 1d0348
	assert((a = archive_write_new()) != NULL);
Packit Service 1d0348
	assertA(0 == archive_write_set_format_ustar(a));
Packit Service 1d0348
	assertA(0 == archive_write_add_filter_none(a));
Packit Service 1d0348
	assertA(0 == archive_write_open_memory(a, buff, BUFF_SIZE, &used));
Packit Service 1d0348
	/* A directory to be restored with EXTRACT_PERM. */
Packit Service 1d0348
	++numEntries;
Packit Service 1d0348
	assert((ae = archive_entry_new()) != NULL);
Packit Service 1d0348
	archive_entry_copy_pathname(ae, "dir_0775");
Packit Service 1d0348
	archive_entry_set_mode(ae, S_IFDIR | 0775);
Packit Service 1d0348
	assertA(0 == archive_write_header(a, ae));
Packit Service 1d0348
	archive_entry_free(ae);
Packit Service 1d0348
	/* A regular file. */
Packit Service 1d0348
	++numEntries;
Packit Service 1d0348
	assert((ae = archive_entry_new()) != NULL);
Packit Service 1d0348
	archive_entry_copy_pathname(ae, "file");
Packit Service 1d0348
	archive_entry_set_mode(ae, S_IFREG | 0755);
Packit Service 1d0348
	for (i = 0; i < FILE_BUFF_SIZE; i++)
Packit Service 1d0348
		file_buff[i] = (unsigned char)rand();
Packit Service 1d0348
	archive_entry_set_size(ae, FILE_BUFF_SIZE);
Packit Service 1d0348
	assertA(0 == archive_write_header(a, ae));
Packit Service 1d0348
	assertA(FILE_BUFF_SIZE == archive_write_data(a, file_buff, FILE_BUFF_SIZE));
Packit Service 1d0348
	archive_entry_free(ae);
Packit Service 1d0348
	/* A directory that should obey umask when restored. */
Packit Service 1d0348
	++numEntries;
Packit Service 1d0348
	assert((ae = archive_entry_new()) != NULL);
Packit Service 1d0348
	archive_entry_copy_pathname(ae, "dir");
Packit Service 1d0348
	archive_entry_set_mode(ae, S_IFDIR | 0777);
Packit Service 1d0348
	assertA(0 == archive_write_header(a, ae));
Packit Service 1d0348
	archive_entry_free(ae);
Packit Service 1d0348
	/* A file in the directory. */
Packit Service 1d0348
	++numEntries;
Packit Service 1d0348
	assert((ae = archive_entry_new()) != NULL);
Packit Service 1d0348
	archive_entry_copy_pathname(ae, "dir/file");
Packit Service 1d0348
	archive_entry_set_mode(ae, S_IFREG | 0700);
Packit Service 1d0348
	assertA(0 == archive_write_header(a, ae));
Packit Service 1d0348
	archive_entry_free(ae);
Packit Service 1d0348
	/* A file in a dir that is not already in the archive. */
Packit Service 1d0348
	++numEntries;
Packit Service 1d0348
	assert((ae = archive_entry_new()) != NULL);
Packit Service 1d0348
	archive_entry_copy_pathname(ae, "dir2/file");
Packit Service 1d0348
	archive_entry_set_mode(ae, S_IFREG | 0000);
Packit Service 1d0348
	assertA(0 == archive_write_header(a, ae));
Packit Service 1d0348
	archive_entry_free(ae);
Packit Service 1d0348
	/* A dir with a trailing /. */
Packit Service 1d0348
	++numEntries;
Packit Service 1d0348
	assert((ae = archive_entry_new()) != NULL);
Packit Service 1d0348
	archive_entry_copy_pathname(ae, "dir3/.");
Packit Service 1d0348
	archive_entry_set_mode(ae, S_IFDIR | 0710);
Packit Service 1d0348
	assertA(0 == archive_write_header(a, ae));
Packit Service 1d0348
	archive_entry_free(ae);
Packit Service 1d0348
	/* Multiple dirs with a single entry. */
Packit Service 1d0348
	++numEntries;
Packit Service 1d0348
	assert((ae = archive_entry_new()) != NULL);
Packit Service 1d0348
	archive_entry_copy_pathname(ae, "dir4/a/../b/../c/");
Packit Service 1d0348
	archive_entry_set_mode(ae, S_IFDIR | 0711);
Packit Service 1d0348
	assertA(0 == archive_write_header(a, ae));
Packit Service 1d0348
	archive_entry_free(ae);
Packit Service 1d0348
	/* A symlink. */
Packit Service 1d0348
	if (canSymlink()) {
Packit Service 1d0348
		++numEntries;
Packit Service 1d0348
		assert((ae = archive_entry_new()) != NULL);
Packit Service 1d0348
		archive_entry_copy_pathname(ae, "symlink");
Packit Service 1d0348
		archive_entry_set_mode(ae, AE_IFLNK | 0755);
Packit Service 1d0348
		archive_entry_set_symlink(ae, "file");
Packit Service 1d0348
		assertA(0 == archive_write_header(a, ae));
Packit Service 1d0348
		archive_entry_free(ae);
Packit Service 1d0348
	}
Packit Service 1d0348
	/* Close out the archive. */
Packit Service 1d0348
	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
Packit Service 1d0348
	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
Packit Service 1d0348
Packit Service 1d0348
	/* Extract the entries to disk. */
Packit Service 1d0348
	assert((a = archive_read_new()) != NULL);
Packit Service 1d0348
	assertA(0 == archive_read_support_format_all(a));
Packit Service 1d0348
	assertA(0 == archive_read_support_filter_all(a));
Packit Service 1d0348
	assertA(0 == archive_read_open_memory(a, buff, BUFF_SIZE));
Packit Service 1d0348
	/* Restore first entry with _EXTRACT_PERM. */
Packit Service 1d0348
	failure("Error reading first entry", i);
Packit Service 1d0348
	assertA(0 == archive_read_next_header(a, &ae);;
Packit Service 1d0348
	assertA(0 == archive_read_extract(a, ae, ARCHIVE_EXTRACT_PERM));
Packit Service 1d0348
	/* Rest of entries get restored with no flags. */
Packit Service 1d0348
	for (i = 1; i < numEntries; i++) {
Packit Service 1d0348
		failure("Error reading entry %d", i);
Packit Service 1d0348
		assertA(0 == archive_read_next_header(a, &ae);;
Packit Service 1d0348
		failure("Failed to extract entry %d: %s", i,
Packit Service 1d0348
			archive_entry_pathname(ae));
Packit Service 1d0348
		assertA(0 == archive_read_extract(a, ae, 0));
Packit Service 1d0348
	}
Packit Service 1d0348
	assertA(ARCHIVE_EOF == archive_read_next_header(a, &ae);;
Packit Service 1d0348
	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
Packit Service 1d0348
	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
Packit Service 1d0348
Packit Service 1d0348
	/* Test the entries on disk. */
Packit Service 1d0348
	/* This first entry was extracted with ARCHIVE_EXTRACT_PERM,
Packit Service 1d0348
	 * so the permissions should have been restored exactly,
Packit Service 1d0348
	 * including resetting the gid bit on those platforms
Packit Service 1d0348
	 * where gid is inherited by subdirs. */
Packit Service 1d0348
	failure("This was 0775 in archive, and should be 0775 on disk");
Packit Service 1d0348
	assertIsDir("dir_0775", 0775);
Packit Service 1d0348
	/* Everything else was extracted without ARCHIVE_EXTRACT_PERM,
Packit Service 1d0348
	 * so there may be some sloppiness about gid bits on directories. */
Packit Service 1d0348
	assertIsReg("file", 0755);
Packit Service 1d0348
	assertFileSize("file", FILE_BUFF_SIZE);
Packit Service 1d0348
	assertFileContents(file_buff, FILE_BUFF_SIZE, "file");
Packit Service 1d0348
	/* If EXTRACT_PERM wasn't used, be careful to ignore sgid bit
Packit Service 1d0348
	 * when checking dir modes, as some systems inherit sgid bit
Packit Service 1d0348
	 * from the parent dir. */
Packit Service 1d0348
	failure("This was 0777 in archive, but umask should make it 0755");
Packit Service 1d0348
	assertIsDir("dir", 0755);
Packit Service 1d0348
	assertIsReg("dir/file", 0700);
Packit Service 1d0348
	assertIsDir("dir2", 0755);
Packit Service 1d0348
	assertIsReg("dir2/file", 0000);
Packit Service 1d0348
	assertIsDir("dir3", 0710);
Packit Service 1d0348
	assertIsDir("dir4", 0755);
Packit Service 1d0348
	assertIsDir("dir4/a", 0755);
Packit Service 1d0348
	assertIsDir("dir4/b", 0755);
Packit Service 1d0348
	assertIsDir("dir4/c", 0711);
Packit Service 1d0348
	if (canSymlink())
Packit Service 1d0348
		assertIsSymlink("symlink", "file");
Packit Service 1d0348
Packit Service 1d0348
	free(buff);
Packit Service 1d0348
	free(file_buff);
Packit Service 1d0348
}