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
28DEFINE_TEST(test_xattr_platform)
29{
30#if !ARCHIVE_XATTR_SUPPORT
31	skipping("Extended attributes are not supported on this platform");
32#else /* ARCHIVE_XATTR_SUPPORT */
33	struct archive *a;
34	struct archive_entry *ae;
35	const char *name;
36	const void *value;
37	void *rvalue;
38	size_t size, insize;
39	int e, r;
40	const char *attrname = "user.libarchive.test";
41	const char *readval = "readval";
42	const char *writeval = "writeval";
43
44	assertMakeFile("readtest", 0644, "a");
45
46	if (!setXattr("readtest", attrname, readval, strlen(readval) + 1)) {
47		skipping("Extended attributes are not supported on this "
48		    "filesystem");
49		return;
50	}
51
52	/* Read test */
53	assert(NULL != (a = archive_read_disk_new()));
54	ae = archive_entry_new();
55	assert(ae != NULL);
56	archive_entry_set_pathname(ae, "readtest");
57	assertEqualInt(ARCHIVE_OK,
58		archive_read_disk_entry_from_file(a, ae, -1, NULL));
59	e = archive_entry_xattr_reset(ae);
60	assert(e > 0);
61
62	r = 0;
63	while (archive_entry_xattr_next(ae, &name, &value,
64	    &size) == ARCHIVE_OK) {
65		if (name != NULL && value != NULL && size > 0 &&
66		    strcmp(name, attrname) == 0) {
67			failure("Attribute value does not match");
68			assertEqualString((const char *)value, readval);
69			r = 1;
70			break;
71		}
72	}
73	failure("Attribute not found: %s", attrname);
74	assertEqualInt(r, 1);
75
76	archive_entry_free(ae);
77	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
78
79	assert(NULL != (a = archive_write_disk_new()));
80	archive_write_disk_set_options(a, ARCHIVE_EXTRACT_TIME |
81	    ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_XATTR);
82
83	/* Write test */
84	ae = archive_entry_new();
85	assert(ae != NULL);
86	archive_entry_set_pathname(ae, "writetest");
87	archive_entry_set_filetype(ae, AE_IFREG);
88	archive_entry_set_perm(ae, 0654);
89	archive_entry_set_mtime(ae, 123456, 7890);
90	archive_entry_set_size(ae, 0);
91	archive_entry_xattr_add_entry(ae, attrname, writeval,
92	    strlen(writeval) + 1);
93	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
94	archive_entry_free(ae);
95	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
96	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
97
98	rvalue = getXattr("writetest", attrname, &insize);
99	if (assertEqualInt(insize, strlen(writeval) + 1) != 0)
100		assertEqualMem(rvalue, writeval, insize);
101	free(rvalue);
102#endif
103}
104