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;
41 const char *attrname = "user.libarchive.test";
42 const char *readval = "readval";
43 const char *writeval = "writeval";
45 assertMakeFile("readtest", 0644, "a");
47 if (!setXattr("readtest", attrname, readval, strlen(readval) + 1)) {
48 skipping("Extended attributes are not supported on this "
54 assert(NULL != (a = archive_read_disk_new()));
55 ae = archive_entry_new();
57 archive_entry_set_pathname(ae, "readtest");
58 assertEqualInt(ARCHIVE_OK,
59 archive_read_disk_entry_from_file(a, ae, -1, NULL));
60 e = archive_entry_xattr_reset(ae);
64 while (archive_entry_xattr_next(ae, &name, &value,
65 &size) == ARCHIVE_OK) {
66 if (name != NULL && value != NULL && size > 0 &&
67 strcmp(name, attrname) == 0) {
68 failure("Attribute value does not match");
69 assertEqualString((const char *)value, readval);
74 failure("Attribute not found: %s", attrname);
77 archive_entry_free(ae);
78 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
80 assert(NULL != (a = archive_write_disk_new()));
81 archive_write_disk_set_options(a, ARCHIVE_EXTRACT_TIME |
82 ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_XATTR);
85 ae = archive_entry_new();
87 archive_entry_set_pathname(ae, "writetest");
88 archive_entry_set_filetype(ae, AE_IFREG);
89 archive_entry_set_perm(ae, 0654);
90 archive_entry_set_mtime(ae, 123456, 7890);
91 archive_entry_set_size(ae, 0);
92 archive_entry_xattr_add_entry(ae, attrname, writeval,
93 strlen(writeval) + 1);
94 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
95 archive_entry_free(ae);
96 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
97 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
99 rvalue = getXattr("writetest", attrname, &insize);
100 if (assertEqualInt(insize, strlen(writeval) + 1) != 0)
101 assertEqualMem(rvalue, writeval, insize);