2 * Copyright (c) 2009 Michihiro NAKAJIMA
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
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.
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.
28 __FBSDID("$FreeBSD$");
30 static char buff[4096];
38 { "./COPYING", S_IFREG | 0644, 1231975636, 1001, 1001 },
39 { "./Makefile", S_IFREG | 0644, 1233041050, 1001, 1001 },
40 { "./NEWS", S_IFREG | 0644, 1231975636, 1001, 1001 },
41 { "./PROJECTS", S_IFREG | 0644, 1231975636, 1001, 1001 },
42 { "./README", S_IFREG | 0644, 1231975636, 1001, 1001 },
43 { "./subdir", S_IFDIR | 0755, 1233504586, 1001, 1001 },
44 { "./subdir/README", S_IFREG | 0664, 1231975636, 1002, 1001 },
45 { "./subdir/config", S_IFREG | 0664, 1232266273, 1003, 1003 },
46 { "./subdir2", S_IFDIR | 0755, 1233504586, 1001, 1001 },
47 { "./subdir3", S_IFDIR | 0755, 1233504586, 1001, 1001 },
48 { "./subdir3/mtree", S_IFREG | 0664, 1232266273, 1003, 1003 },
58 { "COPYING", S_IFREG | 0644, 1231975636, 1001, 1001 },
59 { "Makefile", S_IFREG | 0644, 1233041050, 1001, 1001 },
60 { "NEWS", S_IFREG | 0644, 1231975636, 1001, 1001 },
61 { "PROJECTS", S_IFREG | 0644, 1231975636, 1001, 1001 },
62 { "README", S_IFREG | 0644, 1231975636, 1001, 1001 },
63 { "subdir", S_IFDIR | 0755, 1233504586, 1001, 1001 },
64 { "subdir/README", S_IFREG | 0664, 1231975636, 1002, 1001 },
65 { "subdir/config", S_IFREG | 0664, 1232266273, 1003, 1003 },
66 { "subdir2", S_IFDIR | 0755, 1233504586, 1001, 1001 },
67 { "subdir3", S_IFDIR | 0755, 1233504586, 1001, 1001 },
68 { "subdir3/mtree", S_IFREG | 0664, 1232266273, 1003, 1003 },
73 test_write_format_mtree_sub(int use_set, int dironly)
75 struct archive_entry *ae;
80 /* Create a mtree format archive. */
81 assert((a = archive_write_new()) != NULL);
82 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_mtree(a));
84 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_option(a, NULL, "use-set", "1"));
86 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_option(a, NULL, "dironly", "1"));
87 assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff)-1, &used));
90 for (i = 0; entries[i].path != NULL; i++) {
91 assert((ae = archive_entry_new()) != NULL);
92 archive_entry_set_mtime(ae, entries[i].mtime, 0);
93 assert(entries[i].mtime == archive_entry_mtime(ae));
94 archive_entry_set_mode(ae, entries[i].mode);
95 assert(entries[i].mode == archive_entry_mode(ae));
96 archive_entry_set_uid(ae, entries[i].uid);
97 assert(entries[i].uid == archive_entry_uid(ae));
98 archive_entry_set_gid(ae, entries[i].gid);
99 assert(entries[i].gid == archive_entry_gid(ae));
100 archive_entry_copy_pathname(ae, entries[i].path);
101 if ((entries[i].mode & AE_IFMT) != S_IFDIR)
102 archive_entry_set_size(ae, 8);
103 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
104 if ((entries[i].mode & AE_IFMT) != S_IFDIR)
105 assertEqualIntA(a, 8,
106 archive_write_data(a, "Hello012", 15));
107 archive_entry_free(ae);
109 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
110 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
116 assert(NULL != (p = strstr(buff, "\n/set ")));
125 o = "/set type=dir uid=1001 gid=1001 mode=755";
127 o = "/set type=file uid=1001 gid=1001 mode=644";
128 assertEqualString(o, p);
135 * Read the data and check it.
137 assert((a = archive_read_new()) != NULL);
138 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
139 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
140 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used));
143 for (i = 0; entries[i].path != NULL; i++) {
144 if (dironly && (entries[i].mode & AE_IFMT) != S_IFDIR)
146 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
147 assertEqualInt(entries[i].mtime, archive_entry_mtime(ae));
148 assertEqualInt(entries[i].mode, archive_entry_mode(ae));
149 assertEqualInt(entries[i].uid, archive_entry_uid(ae));
150 assertEqualInt(entries[i].gid, archive_entry_gid(ae));
151 assertEqualString(entries[i].path, archive_entry_pathname(ae));
152 if ((entries[i].mode & AE_IFMT) != S_IFDIR)
153 assertEqualInt(8, archive_entry_size(ae));
155 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
156 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
160 test_write_format_mtree_sub2(int use_set, int dironly)
162 struct archive_entry *ae;
168 /* Create a mtree format archive. */
169 assert((a = archive_write_new()) != NULL);
170 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_mtree(a));
172 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_option(a, NULL, "use-set", "1"));
174 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_option(a, NULL, "dironly", "1"));
175 assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff)-1, &used));
178 for (i = 0; entries2[i].path != NULL; i++) {
179 assert((ae = archive_entry_new()) != NULL);
180 archive_entry_set_mtime(ae, entries2[i].mtime, 0);
181 assert(entries2[i].mtime == archive_entry_mtime(ae));
182 archive_entry_set_mode(ae, entries2[i].mode);
183 assert(entries2[i].mode == archive_entry_mode(ae));
184 archive_entry_set_uid(ae, entries2[i].uid);
185 assert(entries2[i].uid == archive_entry_uid(ae));
186 archive_entry_set_gid(ae, entries2[i].gid);
187 assert(entries2[i].gid == archive_entry_gid(ae));
188 archive_entry_copy_pathname(ae, entries2[i].path);
189 if ((entries2[i].mode & AE_IFMT) != S_IFDIR)
190 archive_entry_set_size(ae, 8);
191 assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
192 if ((entries2[i].mode & AE_IFMT) != S_IFDIR)
193 assertEqualIntA(a, 8,
194 archive_write_data(a, "Hello012", 15));
195 archive_entry_free(ae);
197 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
198 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
204 assert(NULL != (p = strstr(buff, "\n/set ")));
213 o = "/set type=dir uid=1001 gid=1001 mode=755";
215 o = "/set type=file uid=1001 gid=1001 mode=644";
216 assertEqualString(o, p);
223 * Read the data and check it.
225 assert((a = archive_read_new()) != NULL);
226 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
227 assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
228 assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used));
231 memset(str, 0, sizeof(str));
233 for (i = 0; entries2[i].path != NULL; i++) {
234 if (dironly && (entries2[i].mode & AE_IFMT) != S_IFDIR)
236 assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
237 assertEqualInt(entries2[i].mtime, archive_entry_mtime(ae));
238 assertEqualInt(entries2[i].mode, archive_entry_mode(ae));
239 assertEqualInt(entries2[i].uid, archive_entry_uid(ae));
240 assertEqualInt(entries2[i].gid, archive_entry_gid(ae));
241 strcpy(str + 2, entries2[i].path);
242 assertEqualString(str, archive_entry_pathname(ae));
243 if ((entries2[i].mode & AE_IFMT) != S_IFDIR)
244 assertEqualInt(8, archive_entry_size(ae));
246 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
247 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
250 DEFINE_TEST(test_write_format_mtree)
252 /* Default setting */
253 test_write_format_mtree_sub(0, 0);
255 test_write_format_mtree_sub(0, 1);
256 /* Use /set keyword */
257 test_write_format_mtree_sub(1, 0);
258 /* Use /set keyword with directory only */
259 test_write_format_mtree_sub(1, 1);
262 DEFINE_TEST(test_write_format_mtree_no_leading_dotslash)
264 /* Default setting */
265 test_write_format_mtree_sub2(0, 0);
267 test_write_format_mtree_sub2(0, 1);
268 /* Use /set keyword */
269 test_write_format_mtree_sub2(1, 0);
270 /* Use /set keyword with directory only */
271 test_write_format_mtree_sub2(1, 1);