Blame libarchive/test/test_extattr_freebsd.c

Packit Service 1d0348
/*-
Packit Service 1d0348
 * Copyright (c) 2003-2009 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_extattr_freebsd.c 201247 2009-12-30 05:59:21Z kientzle $");
Packit Service 1d0348
Packit Service 1d0348
#if defined(__FreeBSD__) && __FreeBSD__ > 4
Packit Service 1d0348
#include <sys/extattr.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Verify extended attribute restore-to-disk.  This test is FreeBSD-specific.
Packit Service 1d0348
 */
Packit Service 1d0348
Packit Service 1d0348
DEFINE_TEST(test_extattr_freebsd)
Packit Service 1d0348
{
Packit Service 1d0348
#if !defined(__FreeBSD__)
Packit Service 1d0348
	skipping("FreeBSD-specific extattr restore test");
Packit Service 1d0348
#elif __FreeBSD__ < 5
Packit Service 1d0348
	skipping("extattr restore supported only on FreeBSD 5.0 and later");
Packit Service 1d0348
#else
Packit Service 1d0348
	char buff[64];
Packit Service 1d0348
	const char *xname;
Packit Service 1d0348
	const void *xval;
Packit Service 1d0348
	size_t xsize;
Packit Service 1d0348
	struct stat st;
Packit Service 1d0348
	struct archive *a;
Packit Service 1d0348
	struct archive_entry *ae;
Packit Service 1d0348
	int n, fd;
Packit Service 1d0348
	int extattr_privilege_bug = 0;
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * First, do a quick manual set/read of an extended attribute
Packit Service 1d0348
	 * to verify that the local filesystem does support it.  If it
Packit Service 1d0348
	 * doesn't, we'll simply skip the remaining tests.
Packit Service 1d0348
	 */
Packit Service 1d0348
	/* Create a test file and try to set an ACL on it. */
Packit Service 1d0348
	fd = open("pretest", O_RDWR | O_CREAT, 0777);
Packit Service 1d0348
	failure("Could not create test file?!");
Packit Service 1d0348
	if (!assert(fd >= 0))
Packit Service 1d0348
		return;
Packit Service 1d0348
Packit Service 1d0348
	errno = 0;
Packit Service 1d0348
	n = extattr_set_fd(fd, EXTATTR_NAMESPACE_USER, "testattr", "1234", 4);
Packit Service 1d0348
	if (n != 4 && errno == EOPNOTSUPP) {
Packit Service 1d0348
		close(fd);
Packit Service 1d0348
		skipping("extattr tests require that extattr support be enabled on the filesystem");
Packit Service 1d0348
		return;
Packit Service 1d0348
	}
Packit Service 1d0348
	failure("extattr_set_fd(): errno=%d (%s)", errno, strerror(errno));
Packit Service 1d0348
	assertEqualInt(4, n);
Packit Service 1d0348
	close(fd);
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Repeat the above, but with file permissions set to 0000.
Packit Service 1d0348
	 * This should work (extattr_set_fd() should follow fd
Packit Service 1d0348
	 * permissions, not file permissions), but is known broken on
Packit Service 1d0348
	 * some versions of FreeBSD.
Packit Service 1d0348
	 */
Packit Service 1d0348
	fd = open("pretest2", O_RDWR | O_CREAT, 00000);
Packit Service 1d0348
	failure("Could not create test file?!");
Packit Service 1d0348
	if (!assert(fd >= 0))
Packit Service 1d0348
		return;
Packit Service 1d0348
Packit Service 1d0348
	n = extattr_set_fd(fd, EXTATTR_NAMESPACE_USER, "testattr", "1234", 4);
Packit Service 1d0348
	if (n != 4) {
Packit Service 1d0348
		skipping("Restoring xattr to an unwritable file seems to be broken on this platform");
Packit Service 1d0348
		extattr_privilege_bug = 1;
Packit Service 1d0348
	}
Packit Service 1d0348
	close(fd);
Packit Service 1d0348
Packit Service 1d0348
	/* Create a write-to-disk object. */
Packit Service 1d0348
	assert(NULL != (a = archive_write_disk_new()));
Packit Service 1d0348
	archive_write_disk_set_options(a,
Packit Service 1d0348
	    ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_XATTR);
Packit Service 1d0348
Packit Service 1d0348
	/* Populate an archive entry with an extended attribute. */
Packit Service 1d0348
	ae = archive_entry_new();
Packit Service 1d0348
	assert(ae != NULL);
Packit Service 1d0348
	archive_entry_set_pathname(ae, "test0");
Packit Service 1d0348
	archive_entry_set_mtime(ae, 123456, 7890);
Packit Service 1d0348
	archive_entry_set_size(ae, 0);
Packit Service 1d0348
	archive_entry_set_mode(ae, 0755);
Packit Service 1d0348
	archive_entry_xattr_add_entry(ae, "user.foo", "12345", 5);
Packit Service 1d0348
	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
Packit Service 1d0348
	assertEqualIntA(a, ARCHIVE_OK, archive_write_finish_entry(a));
Packit Service 1d0348
	archive_entry_free(ae);
Packit Service 1d0348
Packit Service 1d0348
	/* Another entry; similar but with mode = 0. */
Packit Service 1d0348
	ae = archive_entry_new();
Packit Service 1d0348
	assert(ae != NULL);
Packit Service 1d0348
	archive_entry_set_pathname(ae, "test1");
Packit Service 1d0348
	archive_entry_set_mtime(ae, 12345678, 7890);
Packit Service 1d0348
	archive_entry_set_size(ae, 0);
Packit Service 1d0348
	archive_entry_set_mode(ae, 0);
Packit Service 1d0348
	archive_entry_xattr_add_entry(ae, "user.bar", "123456", 6);
Packit Service 1d0348
	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
Packit Service 1d0348
	archive_entry_free(ae);
Packit Service 1d0348
Packit Service 1d0348
	/* Close the archive. */
Packit Service 1d0348
	if (extattr_privilege_bug)
Packit Service 1d0348
		/* If the bug is here, write_close will return warning. */
Packit Service 1d0348
		assertEqualIntA(a, ARCHIVE_WARN, archive_write_close(a));
Packit Service 1d0348
	else
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
	/* Verify the data on disk. */
Packit Service 1d0348
	assertEqualInt(0, stat("test0", &st);;
Packit Service 1d0348
	assertEqualInt(st.st_mtime, 123456);
Packit Service 1d0348
	/* Verify extattr */
Packit Service 1d0348
	n = extattr_get_file("test0", EXTATTR_NAMESPACE_USER,
Packit Service 1d0348
	    "foo", buff, sizeof(buff));
Packit Service 1d0348
	if (assertEqualInt(n, 5)) {
Packit Service 1d0348
		buff[n] = '\0';
Packit Service 1d0348
		assertEqualString(buff, "12345");
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/* Verify the data on disk. */
Packit Service 1d0348
	assertEqualInt(0, stat("test1", &st);;
Packit Service 1d0348
	assertEqualInt(st.st_mtime, 12345678);
Packit Service 1d0348
	/* Verify extattr */
Packit Service 1d0348
	n = extattr_get_file("test1", EXTATTR_NAMESPACE_USER,
Packit Service 1d0348
	    "bar", buff, sizeof(buff));
Packit Service 1d0348
	if (extattr_privilege_bug) {
Packit Service 1d0348
		/* If we have the bug, the extattr won't have been written. */
Packit Service 1d0348
		assertEqualInt(n, -1);
Packit Service 1d0348
	} else {
Packit Service 1d0348
		if (assertEqualInt(n, 6)) {
Packit Service 1d0348
			buff[n] = '\0';
Packit Service 1d0348
			assertEqualString(buff, "123456");
Packit Service 1d0348
		}
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/* Use libarchive APIs to read the file back into an entry and
Packit Service 1d0348
	 * verify that the extattr was read correctly. */
Packit Service 1d0348
	assert((a = archive_read_disk_new()) != NULL);
Packit Service 1d0348
	assert((ae = archive_entry_new()) != NULL);
Packit Service 1d0348
	archive_entry_set_pathname(ae, "test0");
Packit Service 1d0348
	assertEqualInt(ARCHIVE_OK,
Packit Service 1d0348
	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
Packit Service 1d0348
	assertEqualInt(1, archive_entry_xattr_reset(ae));
Packit Service 1d0348
	assertEqualInt(ARCHIVE_OK,
Packit Service 1d0348
	    archive_entry_xattr_next(ae, &xname, &xval, &xsize));
Packit Service 1d0348
	assertEqualString(xname, "user.foo");
Packit Service 1d0348
	assertEqualInt(xsize, 5);
Packit Service 1d0348
	assertEqualMem(xval, "12345", xsize);
Packit Service 1d0348
	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
Packit Service 1d0348
	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
Packit Service 1d0348
	archive_entry_free(ae);
Packit Service 1d0348
#endif
Packit Service 1d0348
}