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