Blame libarchive/test/test_xattr_platform.c

Packit Service 1d0348
/*-
Packit Service 1d0348
 * Copyright (c) 2003-2010 Tim Kientzle
Packit Service 1d0348
 * Copyright (c) 2017 Martin Matuska
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$");
Packit Service 1d0348
Packit Service 1d0348
DEFINE_TEST(test_xattr_platform)
Packit Service 1d0348
{
Packit Service 1d0348
#if !ARCHIVE_XATTR_SUPPORT
Packit Service 1d0348
	skipping("Extended attributes are not supported on this platform");
Packit Service 1d0348
#else /* ARCHIVE_XATTR_SUPPORT */
Packit Service 1d0348
	struct archive *a;
Packit Service 1d0348
	struct archive_entry *ae;
Packit Service 1d0348
	const char *name;
Packit Service 1d0348
	const void *value;
Packit Service 1d0348
	void *rvalue;
Packit Service 1d0348
	size_t size, insize;
Packit Service 1d0348
	int e, r;
Packit Service 1d0348
	const char *attrname = "user.libarchive.test";
Packit Service 1d0348
	const char *readval = "readval";
Packit Service 1d0348
	const char *writeval = "writeval";
Packit Service 1d0348
Packit Service 1d0348
	assertMakeFile("readtest", 0644, "a");
Packit Service 1d0348
Packit Service 1d0348
	if (!setXattr("readtest", attrname, readval, strlen(readval) + 1)) {
Packit Service 1d0348
		skipping("Extended attributes are not supported on this "
Packit Service 1d0348
		    "filesystem");
Packit Service 1d0348
		return;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/* Read test */
Packit Service 1d0348
	assert(NULL != (a = archive_read_disk_new()));
Packit Service 1d0348
	ae = archive_entry_new();
Packit Service 1d0348
	assert(ae != NULL);
Packit Service 1d0348
	archive_entry_set_pathname(ae, "readtest");
Packit Service 1d0348
	assertEqualInt(ARCHIVE_OK,
Packit Service 1d0348
		archive_read_disk_entry_from_file(a, ae, -1, NULL));
Packit Service 1d0348
	e = archive_entry_xattr_reset(ae);
Packit Service 1d0348
	assert(e > 0);
Packit Service 1d0348
Packit Service 1d0348
	r = 0;
Packit Service 1d0348
	while (archive_entry_xattr_next(ae, &name, &value,
Packit Service 1d0348
	    &size) == ARCHIVE_OK) {
Packit Service 1d0348
		if (name != NULL && value != NULL && size > 0 &&
Packit Service 1d0348
		    strcmp(name, attrname) == 0) {
Packit Service 1d0348
			failure("Attribute value does not match");
Packit Service 1d0348
			assertEqualString((const char *)value, readval);
Packit Service 1d0348
			r = 1;
Packit Service 1d0348
			break;
Packit Service 1d0348
		}
Packit Service 1d0348
	}
Packit Service 1d0348
	failure("Attribute not found: %s", attrname);
Packit Service 1d0348
	assertEqualInt(r, 1);
Packit Service 1d0348
Packit Service 1d0348
	archive_entry_free(ae);
Packit Service 1d0348
	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
Packit Service 1d0348
Packit Service 1d0348
	assert(NULL != (a = archive_write_disk_new()));
Packit Service 1d0348
	archive_write_disk_set_options(a, ARCHIVE_EXTRACT_TIME |
Packit Service 1d0348
	    ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_XATTR);
Packit Service 1d0348
Packit Service 1d0348
	/* Write test */
Packit Service 1d0348
	ae = archive_entry_new();
Packit Service 1d0348
	assert(ae != NULL);
Packit Service 1d0348
	archive_entry_set_pathname(ae, "writetest");
Packit Service 1d0348
	archive_entry_set_filetype(ae, AE_IFREG);
Packit Service 1d0348
	archive_entry_set_perm(ae, 0654);
Packit Service 1d0348
	archive_entry_set_mtime(ae, 123456, 7890);
Packit Service 1d0348
	archive_entry_set_size(ae, 0);
Packit Service 1d0348
	archive_entry_xattr_add_entry(ae, attrname, writeval,
Packit Service 1d0348
	    strlen(writeval) + 1);
Packit Service 1d0348
	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
Packit Service 1d0348
	archive_entry_free(ae);
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
	rvalue = getXattr("writetest", attrname, &insize);
Packit Service 1d0348
	if (assertEqualInt(insize, strlen(writeval) + 1) != 0)
Packit Service 1d0348
		assertEqualMem(rvalue, writeval, insize);
Packit Service 1d0348
	free(rvalue);
Packit Service 1d0348
#endif
Packit Service 1d0348
}