Home | History | Annotate | Line # | Download | only in test
      1 /*-
      2  * SPDX-License-Identifier: BSD-2-Clause
      3  *
      4  * Copyright (c) 2017 Martin Matuska
      5  * All rights reserved.
      6  */
      7 #include "test.h"
      8 
      9 DEFINE_TEST(test_option_xattrs)
     10 {
     11 #if !ARCHIVE_XATTR_SUPPORT
     12         skipping("Extended attributes are not supported on this platform");
     13 #else	/* ARCHIVE_XATTR_SUPPORT */
     14 
     15 	const char *testattr = "user.libarchive.test";
     16 	const char *testval = "testval";
     17 	void *readval;
     18 	size_t size;
     19 	int r;
     20 
     21 	/* Create a file. */
     22 	assertMakeFile("f", 0644, "a");
     23 
     24 	if (!setXattr("f", "user.libarchive.test", testval,
     25 	    strlen(testval) + 1)) {
     26 		skipping("Can't set user extended attributes on this "
     27 		    "filesystem");
     28 		return;
     29 	}
     30 
     31 	/* Archive with xattrs */
     32 	r = systemf("%s -c --no-mac-metadata --xattrs -f xattrs.tar f >xattrs.out 2>xattrs.err", testprog);
     33 	assertEqualInt(r, 0);
     34 
     35 	/* Archive without xattrs */
     36 	r = systemf("%s -c --no-mac-metadata --no-xattrs -f noxattrs.tar f >noxattrs.out 2>noxattrs.err", testprog);
     37 	assertEqualInt(r, 0);
     38 
     39 	/* Extract xattrs with xattrs */
     40 	assertMakeDir("xattrs_xattrs", 0755);
     41 	r = systemf("%s -x -C xattrs_xattrs --no-same-permissions --xattrs -f xattrs.tar >xattrs_xattrs.out 2>xattrs_xattrs.err", testprog);
     42 	assertEqualInt(r, 0);
     43 	readval = getXattr("xattrs_xattrs/f", testattr, &size);
     44 	if(assertEqualInt(size, strlen(testval) + 1) != 0)
     45 		assertEqualMem(readval, testval, size);
     46 	free(readval);
     47 
     48 	/* Extract xattrs without xattrs */
     49 	assertMakeDir("xattrs_noxattrs", 0755);
     50 	r = systemf("%s -x -C xattrs_noxattrs -p --no-xattrs -f xattrs.tar >xattrs_noxattrs.out 2>xattrs_noxattrs.err", testprog);
     51 	assertEqualInt(r, 0);
     52 	readval = getXattr("xattrs_noxattrs/f", testattr, &size);
     53 	assert(readval == NULL);
     54 
     55 	/* Extract noxattrs with xattrs */
     56 	assertMakeDir("noxattrs_xattrs", 0755);
     57 	r = systemf("%s -x -C noxattrs_xattrs --no-same-permissions --xattrs -f noxattrs.tar >noxattrs_xattrs.out 2>noxattrs_xattrs.err", testprog);
     58 	assertEqualInt(r, 0);
     59 	readval = getXattr("noxattrs_xattrs/f", testattr, &size);
     60 	assert(readval == NULL);
     61 
     62 	/* Extract noxattrs with noxattrs */
     63 	assertMakeDir("noxattrs_noxattrs", 0755);
     64 	r = systemf("%s -x -C noxattrs_noxattrs -p --no-xattrs -f noxattrs.tar >noxattrs_noxattrs.out 2>noxattrs_noxattrs.err", testprog);
     65 	assertEqualInt(r, 0);
     66 	readval = getXattr("noxattrs_noxattrs/f", testattr, &size);
     67 	assert(readval == NULL);
     68 #endif	/* ARCHIVE_XATTR_SUPPORT */
     69 }
     70