1 /*-
2  * Copyright (c) 2003-2009 Tim Kientzle
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 #if defined(__FreeBSD__) && __FreeBSD__ > 4
28 #include <sys/extattr.h>
29 #endif
30 
31 /*
32  * Verify extended attribute restore-to-disk.  This test is FreeBSD-specific.
33  */
34 
DEFINE_TEST(test_extattr_freebsd)35 DEFINE_TEST(test_extattr_freebsd)
36 {
37 #if !defined(__FreeBSD__)
38 	skipping("FreeBSD-specific extattr restore test");
39 #elif __FreeBSD__ < 5
40 	skipping("extattr restore supported only on FreeBSD 5.0 and later");
41 #else
42 	char buff[64];
43 	const char *xname;
44 	const void *xval;
45 	size_t xsize;
46 	struct stat st;
47 	struct archive *a;
48 	struct archive_entry *ae;
49 	ssize_t n;
50 	int fd;
51 
52 	/*
53 	 * First, do a quick manual set/read of an extended attribute
54 	 * to verify that the local filesystem does support it.  If it
55 	 * doesn't, we'll simply skip the remaining tests.
56 	 */
57 	/* Create a test file and try to set an ACL on it. */
58 	fd = open("pretest", O_RDWR | O_CREAT, 0777);
59 	failure("Could not create test file?!");
60 	if (!assert(fd >= 0))
61 		return;
62 
63 	errno = 0;
64 	n = extattr_set_fd(fd, EXTATTR_NAMESPACE_USER, "testattr", "1234", 4);
65 	if (n != 4 && errno == EOPNOTSUPP) {
66 		close(fd);
67 		skipping("extattr tests require that extattr support be enabled on the filesystem");
68 		return;
69 	}
70 	failure("extattr_set_fd(): errno=%d (%s)", errno, strerror(errno));
71 	assertEqualInt(4, n);
72 	close(fd);
73 
74 	/* Create a write-to-disk object. */
75 	assert(NULL != (a = archive_write_disk_new()));
76 	archive_write_disk_set_options(a,
77 	    ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_XATTR);
78 
79 	/* Populate an archive entry with an extended attribute. */
80 	ae = archive_entry_new();
81 	assert(ae != NULL);
82 	archive_entry_set_pathname(ae, "test0");
83 	archive_entry_set_mtime(ae, 123456, 7890);
84 	archive_entry_set_size(ae, 0);
85 	archive_entry_set_mode(ae, 0755);
86 	archive_entry_xattr_add_entry(ae, "user.foo", "12345", 5);
87 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
88 	assertEqualIntA(a, ARCHIVE_OK, archive_write_finish_entry(a));
89 	archive_entry_free(ae);
90 
91 	/* Another entry; similar but with mode = 0. */
92 	ae = archive_entry_new();
93 	assert(ae != NULL);
94 	archive_entry_set_pathname(ae, "test1");
95 	archive_entry_set_mtime(ae, 12345678, 7890);
96 	archive_entry_set_size(ae, 0);
97 	archive_entry_set_mode(ae, 0);
98 	archive_entry_xattr_add_entry(ae, "user.bar", "123456", 6);
99 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
100 	archive_entry_free(ae);
101 
102 	/* Close the archive. */
103 	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
104 
105 	/* Verify the data on disk. */
106 	assertEqualInt(0, stat("test0", &st));
107 	assertEqualInt(st.st_mtime, 123456);
108 	assertEqualInt(st.st_mode & 0777, 0755);
109 	/* Verify extattr */
110 	n = extattr_get_file("test0", EXTATTR_NAMESPACE_USER,
111 	    "foo", buff, sizeof(buff));
112 	if (assertEqualInt(n, 5)) {
113 		buff[n] = '\0';
114 		assertEqualString(buff, "12345");
115 	}
116 
117 	/* Verify the data on disk. */
118 	assertEqualInt(0, stat("test1", &st));
119 	assertEqualInt(st.st_mtime, 12345678);
120 	assertEqualInt(st.st_mode & 0777, 0);
121 	/*
122 	 * If we are not root, we have to make test1 user readable
123 	 * or extattr_get_file() will fail
124 	 */
125 	if (geteuid() != 0) {
126 		chmod("test1", S_IRUSR);
127 	}
128 	/* Verify extattr */
129 	n = extattr_get_file("test1", EXTATTR_NAMESPACE_USER,
130 	    "bar", buff, sizeof(buff));
131 	if (assertEqualInt(n, 6)) {
132 		buff[n] = '\0';
133 		assertEqualString(buff, "123456");
134 	}
135 
136 	/* Use libarchive APIs to read the file back into an entry and
137 	 * verify that the extattr was read correctly. */
138 	assert((a = archive_read_disk_new()) != NULL);
139 	assert((ae = archive_entry_new()) != NULL);
140 	archive_entry_set_pathname(ae, "test0");
141 	assertEqualInt(ARCHIVE_OK,
142 	    archive_read_disk_entry_from_file(a, ae, -1, NULL));
143 	assertEqualInt(1, archive_entry_xattr_reset(ae));
144 	assertEqualInt(ARCHIVE_OK,
145 	    archive_entry_xattr_next(ae, &xname, &xval, &xsize));
146 	assertEqualString(xname, "user.foo");
147 	assertEqualInt(xsize, 5);
148 	assertEqualMem(xval, "12345", xsize);
149 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
150 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
151 	archive_entry_free(ae);
152 #endif
153 }
154