2 * Copyright (c) 2003-2010 Tim Kientzle
3 * Copyright (c) 2017 Martin Matuska
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 __FBSDID("$FreeBSD$");
29 DEFINE_TEST(test_xattr_platform)
31 #if !ARCHIVE_XATTR_SUPPORT
32 skipping("Extended attributes are not supported on this platform");
33 #else /* ARCHIVE_XATTR_SUPPORT */
35 struct archive_entry *ae;
40 const char *attrname = "user.libarchive.test";
41 const char *readval = "readval";
42 const char *writeval = "writeval";
44 assertMakeFile("readtest", 0644, "a");
46 if (!setXattr("readtest", attrname, readval, strlen(readval) + 1)) {
47 skipping("Extended attributes are not supported on this "
53 assert(NULL != (a = archive_read_disk_new()));
54 ae = archive_entry_new();
56 archive_entry_set_pathname(ae, "readtest");
57 assertEqualInt(ARCHIVE_OK,
58 archive_read_disk_entry_from_file(a, ae, -1, NULL));
59 e = archive_entry_xattr_reset(ae);
63 while (archive_entry_xattr_next(ae, &name, &value,
64 &size) == ARCHIVE_OK) {
65 if (name != NULL && value != NULL && size > 0 &&
66 strcmp(name, attrname) == 0) {
67 failure("Attribute value does not match");
68 assertEqualString((const char *)value, readval);
73 failure("Attribute not found: %s", attrname);
76 archive_entry_free(ae);
77 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
79 assert(NULL != (a = archive_write_disk_new()));
80 archive_write_disk_set_options(a, ARCHIVE_EXTRACT_TIME |
81 ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_XATTR);
84 ae = archive_entry_new();
86 archive_entry_set_pathname(ae, "writetest");
87 archive_entry_set_filetype(ae, AE_IFREG);
88 archive_entry_set_perm(ae, 0654);
89 archive_entry_set_mtime(ae, 123456, 7890);
90 archive_entry_set_size(ae, 0);
91 archive_entry_xattr_add_entry(ae, attrname, writeval,
92 strlen(writeval) + 1);
93 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
94 archive_entry_free(ae);
95 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
96 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
98 value = getXattr("writetest", attrname, &insize);
99 if (assertEqualInt(insize, strlen(writeval) + 1) != 0)
100 assertEqualMem(value, writeval, insize);