xref: /freebsd/contrib/libarchive/tar/test/test_option_xattrs.c (revision ebacd8013fe5f7fdf9f6a5b286f6680dd2891036)
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 __FBSDID("$FreeBSD$");
27 
28 DEFINE_TEST(test_option_xattrs)
29 {
30 #if !ARCHIVE_XATTR_SUPPORT
31         skipping("Extended attributes are not supported on this platform");
32 #else	/* ARCHIVE_XATTR_SUPPORT */
33 
34 	const char *testattr = "user.libarchive.test";
35 	const char *testval = "testval";
36 	void *readval;
37 	size_t size;
38 	int r;
39 
40 	/* Create a file. */
41 	assertMakeFile("f", 0644, "a");
42 
43 	if (!setXattr("f", "user.libarchive.test", testval,
44 	    strlen(testval) + 1)) {
45 		skipping("Can't set user extended attributes on this "
46 		    "filesystem");
47 		return;
48 	}
49 
50 	/* Archive with xattrs */
51 	r = systemf("%s -c --no-mac-metadata --xattrs -f xattrs.tar f >xattrs.out 2>xattrs.err", testprog);
52 	assertEqualInt(r, 0);
53 
54 	/* Archive without xattrs */
55 	r = systemf("%s -c --no-mac-metadata --no-xattrs -f noxattrs.tar f >noxattrs.out 2>noxattrs.err", testprog);
56 	assertEqualInt(r, 0);
57 
58 	/* Extract xattrs with xattrs */
59 	assertMakeDir("xattrs_xattrs", 0755);
60 	r = systemf("%s -x -C xattrs_xattrs --no-same-permissions --xattrs -f xattrs.tar >xattrs_xattrs.out 2>xattrs_xattrs.err", testprog);
61 	assertEqualInt(r, 0);
62 	readval = getXattr("xattrs_xattrs/f", testattr, &size);
63 	if(assertEqualInt(size, strlen(testval) + 1) != 0)
64 		assertEqualMem(readval, testval, size);
65 	free(readval);
66 
67 	/* Extract xattrs without xattrs */
68 	assertMakeDir("xattrs_noxattrs", 0755);
69 	r = systemf("%s -x -C xattrs_noxattrs -p --no-xattrs -f xattrs.tar >xattrs_noxattrs.out 2>xattrs_noxattrs.err", testprog);
70 	assertEqualInt(r, 0);
71 	readval = getXattr("xattrs_noxattrs/f", testattr, &size);
72 	assert(readval == NULL);
73 
74 	/* Extract noxattrs with xattrs */
75 	assertMakeDir("noxattrs_xattrs", 0755);
76 	r = systemf("%s -x -C noxattrs_xattrs --no-same-permissions --xattrs -f noxattrs.tar >noxattrs_xattrs.out 2>noxattrs_xattrs.err", testprog);
77 	assertEqualInt(r, 0);
78 	readval = getXattr("noxattrs_xattrs/f", testattr, &size);
79 	assert(readval == NULL);
80 
81 	/* Extract noxattrs with noxattrs */
82 	assertMakeDir("noxattrs_noxattrs", 0755);
83 	r = systemf("%s -x -C noxattrs_noxattrs -p --no-xattrs -f noxattrs.tar >noxattrs_noxattrs.out 2>noxattrs_noxattrs.err", testprog);
84 	assertEqualInt(r, 0);
85 	readval = getXattr("noxattrs_noxattrs/f", testattr, &size);
86 	assert(readval == NULL);
87 #endif	/* ARCHIVE_XATTR_SUPPORT */
88 }
89