1 /*-
2  * Copyright (c) 2003-2010 Tim Kientzle
3  * Copyright (c) 2017 Martin Matuska
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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.
25  */
26 #include "test.h"
27 __FBSDID("$FreeBSD$");
28 
DEFINE_TEST(test_xattr_platform)29 DEFINE_TEST(test_xattr_platform)
30 {
31 #if !ARCHIVE_XATTR_SUPPORT
32 	skipping("Extended attributes are not supported on this platform");
33 #else /* ARCHIVE_XATTR_SUPPORT */
34 	struct archive *a;
35 	struct archive_entry *ae;
36 	const char *name;
37 	const void *value;
38 	void *rvalue;
39 	size_t size, insize;
40 	int e, r;
41 	const char *attrname = "user.libarchive.test";
42 	const char *readval = "readval";
43 	const char *writeval = "writeval";
44 
45 	assertMakeFile("readtest", 0644, "a");
46 
47 	if (!setXattr("readtest", attrname, readval, strlen(readval) + 1)) {
48 		skipping("Extended attributes are not supported on this "
49 		    "filesystem");
50 		return;
51 	}
52 
53 	/* Read test */
54 	assert(NULL != (a = archive_read_disk_new()));
55 	ae = archive_entry_new();
56 	assert(ae != NULL);
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);
61 	assert(e > 0);
62 
63 	r = 0;
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);
70 			r = 1;
71 			break;
72 		}
73 	}
74 	failure("Attribute not found: %s", attrname);
75 	assertEqualInt(r, 1);
76 
77 	archive_entry_free(ae);
78 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
79 
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);
83 
84 	/* Write test */
85 	ae = archive_entry_new();
86 	assert(ae != NULL);
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));
98 
99 	rvalue = getXattr("writetest", attrname, &insize);
100 	if (assertEqualInt(insize, strlen(writeval) + 1) != 0)
101 		assertEqualMem(rvalue, writeval, insize);
102 	free(rvalue);
103 #endif
104 }
105