xref: /freebsd/contrib/libarchive/tar/test/test_option_xattrs.c (revision e63d20b70ee1dbee9b075f29de6f30cdcfe1abe1)
1 /*-
2  * Copyright (c) 2017 Martin Matuska
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #include "test.h"
26 
27 DEFINE_TEST(test_option_xattrs)
28 {
29 #if !ARCHIVE_XATTR_SUPPORT
30         skipping("Extended attributes are not supported on this platform");
31 #else	/* ARCHIVE_XATTR_SUPPORT */
32 
33 	const char *testattr = "user.libarchive.test";
34 	const char *testval = "testval";
35 	void *readval;
36 	size_t size;
37 	int r;
38 
39 	/* Create a file. */
40 	assertMakeFile("f", 0644, "a");
41 
42 	if (!setXattr("f", "user.libarchive.test", testval,
43 	    strlen(testval) + 1)) {
44 		skipping("Can't set user extended attributes on this "
45 		    "filesystem");
46 		return;
47 	}
48 
49 	/* Archive with xattrs */
50 	r = systemf("%s -c --no-mac-metadata --xattrs -f xattrs.tar f >xattrs.out 2>xattrs.err", testprog);
51 	assertEqualInt(r, 0);
52 
53 	/* Archive without xattrs */
54 	r = systemf("%s -c --no-mac-metadata --no-xattrs -f noxattrs.tar f >noxattrs.out 2>noxattrs.err", testprog);
55 	assertEqualInt(r, 0);
56 
57 	/* Extract xattrs with xattrs */
58 	assertMakeDir("xattrs_xattrs", 0755);
59 	r = systemf("%s -x -C xattrs_xattrs --no-same-permissions --xattrs -f xattrs.tar >xattrs_xattrs.out 2>xattrs_xattrs.err", testprog);
60 	assertEqualInt(r, 0);
61 	readval = getXattr("xattrs_xattrs/f", testattr, &size);
62 	if(assertEqualInt(size, strlen(testval) + 1) != 0)
63 		assertEqualMem(readval, testval, size);
64 	free(readval);
65 
66 	/* Extract xattrs without xattrs */
67 	assertMakeDir("xattrs_noxattrs", 0755);
68 	r = systemf("%s -x -C xattrs_noxattrs -p --no-xattrs -f xattrs.tar >xattrs_noxattrs.out 2>xattrs_noxattrs.err", testprog);
69 	assertEqualInt(r, 0);
70 	readval = getXattr("xattrs_noxattrs/f", testattr, &size);
71 	assert(readval == NULL);
72 
73 	/* Extract noxattrs with xattrs */
74 	assertMakeDir("noxattrs_xattrs", 0755);
75 	r = systemf("%s -x -C noxattrs_xattrs --no-same-permissions --xattrs -f noxattrs.tar >noxattrs_xattrs.out 2>noxattrs_xattrs.err", testprog);
76 	assertEqualInt(r, 0);
77 	readval = getXattr("noxattrs_xattrs/f", testattr, &size);
78 	assert(readval == NULL);
79 
80 	/* Extract noxattrs with noxattrs */
81 	assertMakeDir("noxattrs_noxattrs", 0755);
82 	r = systemf("%s -x -C noxattrs_noxattrs -p --no-xattrs -f noxattrs.tar >noxattrs_noxattrs.out 2>noxattrs_noxattrs.err", testprog);
83 	assertEqualInt(r, 0);
84 	readval = getXattr("noxattrs_noxattrs/f", testattr, &size);
85 	assert(readval == NULL);
86 #endif	/* ARCHIVE_XATTR_SUPPORT */
87 }
88