]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/libarchive/libarchive/test/test_extattr_freebsd.c
MFC r368207,368607:
[FreeBSD/stable/10.git] / contrib / libarchive / libarchive / test / test_extattr_freebsd.c
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         int extattr_privilege_bug = 0;
53
54         /*
55          * First, do a quick manual set/read of an extended attribute
56          * to verify that the local filesystem does support it.  If it
57          * doesn't, we'll simply skip the remaining tests.
58          */
59         /* Create a test file and try to set an ACL on it. */
60         fd = open("pretest", O_RDWR | O_CREAT, 0777);
61         failure("Could not create test file?!");
62         if (!assert(fd >= 0))
63                 return;
64
65         errno = 0;
66         n = extattr_set_fd(fd, EXTATTR_NAMESPACE_USER, "testattr", "1234", 4);
67         if (n != 4 && errno == EOPNOTSUPP) {
68                 close(fd);
69                 skipping("extattr tests require that extattr support be enabled on the filesystem");
70                 return;
71         }
72         failure("extattr_set_fd(): errno=%d (%s)", errno, strerror(errno));
73         assertEqualInt(4, n);
74         close(fd);
75
76         /*
77          * Repeat the above, but with file permissions set to 0000.
78          * This should work (extattr_set_fd() should follow fd
79          * permissions, not file permissions), but is known broken on
80          * some versions of FreeBSD.
81          */
82         fd = open("pretest2", O_RDWR | O_CREAT, 00000);
83         failure("Could not create test file?!");
84         if (!assert(fd >= 0))
85                 return;
86
87         n = extattr_set_fd(fd, EXTATTR_NAMESPACE_USER, "testattr", "1234", 4);
88         if (n != 4) {
89                 skipping("Restoring xattr to an unwritable file seems to be broken on this platform");
90                 extattr_privilege_bug = 1;
91         }
92         close(fd);
93
94         /* Create a write-to-disk object. */
95         assert(NULL != (a = archive_write_disk_new()));
96         archive_write_disk_set_options(a,
97             ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_XATTR);
98
99         /* Populate an archive entry with an extended attribute. */
100         ae = archive_entry_new();
101         assert(ae != NULL);
102         archive_entry_set_pathname(ae, "test0");
103         archive_entry_set_mtime(ae, 123456, 7890);
104         archive_entry_set_size(ae, 0);
105         archive_entry_set_mode(ae, 0755);
106         archive_entry_xattr_add_entry(ae, "user.foo", "12345", 5);
107         assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
108         assertEqualIntA(a, ARCHIVE_OK, archive_write_finish_entry(a));
109         archive_entry_free(ae);
110
111         /* Another entry; similar but with mode = 0. */
112         ae = archive_entry_new();
113         assert(ae != NULL);
114         archive_entry_set_pathname(ae, "test1");
115         archive_entry_set_mtime(ae, 12345678, 7890);
116         archive_entry_set_size(ae, 0);
117         archive_entry_set_mode(ae, 0);
118         archive_entry_xattr_add_entry(ae, "user.bar", "123456", 6);
119         assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
120         archive_entry_free(ae);
121
122         /* Close the archive. */
123         if (extattr_privilege_bug)
124                 /* If the bug is here, write_close will return warning. */
125                 assertEqualIntA(a, ARCHIVE_WARN, archive_write_close(a));
126         else
127                 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
128         assertEqualInt(ARCHIVE_OK, archive_write_free(a));
129
130         /* Verify the data on disk. */
131         assertEqualInt(0, stat("test0", &st));
132         assertEqualInt(st.st_mtime, 123456);
133         /* Verify extattr */
134         n = extattr_get_file("test0", EXTATTR_NAMESPACE_USER,
135             "foo", buff, sizeof(buff));
136         if (assertEqualInt(n, 5)) {
137                 buff[n] = '\0';
138                 assertEqualString(buff, "12345");
139         }
140
141         /* Verify the data on disk. */
142         assertEqualInt(0, stat("test1", &st));
143         assertEqualInt(st.st_mtime, 12345678);
144         /* Verify extattr */
145         n = extattr_get_file("test1", EXTATTR_NAMESPACE_USER,
146             "bar", buff, sizeof(buff));
147         if (extattr_privilege_bug) {
148                 /* If we have the bug, the extattr won't have been written. */
149                 assertEqualInt(n, -1);
150         } else {
151                 if (assertEqualInt(n, 6)) {
152                         buff[n] = '\0';
153                         assertEqualString(buff, "123456");
154                 }
155         }
156
157         /* Use libarchive APIs to read the file back into an entry and
158          * verify that the extattr was read correctly. */
159         assert((a = archive_read_disk_new()) != NULL);
160         assert((ae = archive_entry_new()) != NULL);
161         archive_entry_set_pathname(ae, "test0");
162         assertEqualInt(ARCHIVE_OK,
163             archive_read_disk_entry_from_file(a, ae, -1, NULL));
164         assertEqualInt(1, archive_entry_xattr_reset(ae));
165         assertEqualInt(ARCHIVE_OK,
166             archive_entry_xattr_next(ae, &xname, &xval, &xsize));
167         assertEqualString(xname, "user.foo");
168         assertEqualInt(xsize, 5);
169         assertEqualMem(xval, "12345", xsize);
170         assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
171         assertEqualInt(ARCHIVE_OK, archive_read_free(a));
172         archive_entry_free(ae);
173 #endif
174 }