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