1248590Smm/*-
2248590Smm * Copyright (c) 2012 Michihiro NAKAJIMA
3248590Smm * All rights reserved.
4248590Smm *
5248590Smm * Redistribution and use in source and binary forms, with or without
6248590Smm * modification, are permitted provided that the following conditions
7248590Smm * are met:
8248590Smm * 1. Redistributions of source code must retain the above copyright
9248590Smm *    notice, this list of conditions and the following disclaimer
10248590Smm *    in this position and unchanged.
11248590Smm * 2. Redistributions in binary form must reproduce the above copyright
12248590Smm *    notice, this list of conditions and the following disclaimer in the
13248590Smm *    documentation and/or other materials provided with the distribution.
14248590Smm *
15248590Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16248590Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17248590Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18248590Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19248590Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20248590Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21248590Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22248590Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23248590Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24248590Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25248590Smm */
26248590Smm
27248590Smm#include "test.h"
28248590Smm__FBSDID("$FreeBSD$");
29248590Smm
30248590Smmstatic char buff[4096];
31248590Smmstatic struct {
32248590Smm	const char	*path;
33248590Smm	mode_t		 mode;
34248590Smm	int		 nlink;
35248590Smm	time_t		 mtime;
36248590Smm	uid_t	 	 uid;
37248590Smm	gid_t		 gid;
38248590Smm} entries[] = {
39248590Smm	{ ".",			S_IFDIR | 0755, 3, 1231975636, 1001, 1001 },
40248590Smm	{ "./COPYING",		S_IFREG | 0644, 1, 1231975636, 1001, 1001 },
41248590Smm	{ "./Makefile", 	S_IFREG | 0644, 1, 1233041050, 1001, 1001 },
42248590Smm	{ "./NEWS", 		S_IFREG | 0644, 1, 1231975636, 1001, 1001 },
43248590Smm	{ "./PROJECTS", 	S_IFREG | 0644, 1, 1231975636, 1001, 1001 },
44248590Smm	{ "./README",		S_IFREG | 0644, 1, 1231975636, 1001, 1001 },
45248590Smm	{ "./subdir",		S_IFDIR | 0755, 3, 1233504586, 1001, 1001 },
46248590Smm	{ "./subdir/README",	S_IFREG | 0664, 1, 1231975636, 1002, 1001 },
47248590Smm	{ "./subdir/config",	S_IFREG | 0664, 1, 1232266273, 1003, 1003 },
48248590Smm	{ "./subdir2",		S_IFDIR | 0755, 3, 1233504586, 1001, 1001 },
49248590Smm	{ "./subdir3",		S_IFDIR | 0755, 3, 1233504586, 1001, 1001 },
50248590Smm	{ "./subdir3/mtree",	S_IFREG | 0664, 2, 1232266273, 1003, 1003 },
51248590Smm	{ NULL, 0, 0, 0, 0, 0 }
52248590Smm};
53248590Smm
54248590Smmstatic const char image [] = {
55248590Smm"#mtree\n"
56248590Smm"\n"
57248590Smm"# .\n"
58248590Smm"/set type=file uid=1001 gid=1001 mode=644\n"
59248590Smm".               time=1231975636.0 mode=755 type=dir\n"
60248590Smm"    COPYING         time=1231975636.0 size=8\n"
61248590Smm"    Makefile        time=1233041050.0 size=8\n"
62248590Smm"    NEWS            time=1231975636.0 size=8\n"
63248590Smm"    PROJECTS        time=1231975636.0 size=8\n"
64248590Smm"    README          time=1231975636.0 size=8\n"
65248590Smm"\n"
66248590Smm"# ./subdir\n"
67248590Smm"/set mode=664\n"
68248590Smm"    subdir          time=1233504586.0 mode=755 type=dir\n"
69248590Smm"        README          time=1231975636.0 uid=1002 size=8\n"
70248590Smm"        config          time=1232266273.0 gid=1003 uid=1003 size=8\n"
71248590Smm"    # ./subdir\n"
72248590Smm"    ..\n"
73248590Smm"\n"
74248590Smm"\n"
75248590Smm"# ./subdir2\n"
76248590Smm"    subdir2         time=1233504586.0 mode=755 type=dir\n"
77248590Smm"    # ./subdir2\n"
78248590Smm"    ..\n"
79248590Smm"\n"
80248590Smm"\n"
81248590Smm"# ./subdir3\n"
82248590Smm"    subdir3         time=1233504586.0 mode=755 type=dir\n"
83248590Smm"        mtree           nlink=2 time=1232266273.0 gid=1003 uid=1003 size=8\n"
84248590Smm"    # ./subdir3\n"
85248590Smm"    ..\n"
86248590Smm"\n"
87248590Smm"..\n\n"
88248590Smm};
89248590Smm
90248590Smmstatic const char image_dironly [] = {
91248590Smm"#mtree\n"
92248590Smm"# .\n"
93248590Smm"/set type=dir uid=1001 gid=1001 mode=755\n"
94248590Smm".               time=1231975636.0\n"
95248590Smm"# ./subdir\n"
96248590Smm"    subdir          time=1233504586.0\n"
97248590Smm"    # ./subdir\n"
98248590Smm"    ..\n"
99248590Smm"# ./subdir2\n"
100248590Smm"    subdir2         time=1233504586.0\n"
101248590Smm"    # ./subdir2\n"
102248590Smm"    ..\n"
103248590Smm"# ./subdir3\n"
104248590Smm"    subdir3         time=1233504586.0\n"
105248590Smm"    # ./subdir3\n"
106248590Smm"    ..\n"
107248590Smm"..\n"
108248590Smm};
109248590Smm
110248590Smmstatic void
111248590Smmtest_write_format_mtree_sub(int dironly)
112248590Smm{
113248590Smm	struct archive_entry *ae;
114248590Smm	struct archive* a;
115248590Smm	size_t used;
116248590Smm	int i;
117248590Smm
118248590Smm	/* Create a mtree format archive. */
119248590Smm	assert((a = archive_write_new()) != NULL);
120248590Smm	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_mtree_classic(a));
121248590Smm	assertEqualIntA(a, ARCHIVE_OK,
122248590Smm		archive_write_set_format_option(a, NULL, "indent", "1"));
123248590Smm	if (dironly)
124248590Smm		assertEqualIntA(a, ARCHIVE_OK,
125248590Smm			archive_write_set_format_option(a, NULL, "dironly", "1"));
126248590Smm	assertEqualIntA(a, ARCHIVE_OK,
127248590Smm		archive_write_open_memory(a, buff, sizeof(buff)-1, &used));
128248590Smm
129248590Smm	/* Write entries */
130248590Smm	for (i = 0; entries[i].path != NULL; i++) {
131248590Smm		assert((ae = archive_entry_new()) != NULL);
132248590Smm		archive_entry_set_nlink(ae, entries[i].nlink);
133248590Smm		assertEqualInt(entries[i].nlink, archive_entry_nlink(ae));
134248590Smm		archive_entry_set_mtime(ae, entries[i].mtime, 0);
135248590Smm		assertEqualInt(entries[i].mtime, archive_entry_mtime(ae));
136248590Smm		archive_entry_set_mode(ae, entries[i].mode);
137248590Smm		assertEqualInt(entries[i].mode, archive_entry_mode(ae));
138248590Smm		archive_entry_set_uid(ae, entries[i].uid);
139248590Smm		assertEqualInt(entries[i].uid, archive_entry_uid(ae));
140248590Smm		archive_entry_set_gid(ae, entries[i].gid);
141248590Smm		assertEqualInt(entries[i].gid, archive_entry_gid(ae));
142248590Smm		archive_entry_copy_pathname(ae, entries[i].path);
143248590Smm		if ((entries[i].mode & AE_IFMT) != S_IFDIR)
144248590Smm			archive_entry_set_size(ae, 8);
145248590Smm		assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
146248590Smm		if ((entries[i].mode & AE_IFMT) != S_IFDIR)
147248590Smm			assertEqualIntA(a, 8,
148248590Smm			    archive_write_data(a, "Hello012", 15));
149248590Smm		archive_entry_free(ae);
150248590Smm	}
151248590Smm	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
152248590Smm        assertEqualInt(ARCHIVE_OK, archive_write_free(a));
153248590Smm
154248590Smm	buff[used] = '\0';
155248590Smm	if (dironly)
156248590Smm		assertEqualString(buff, image_dironly);
157248590Smm	else
158248590Smm		assertEqualString(buff, image);
159248590Smm
160248590Smm	/*
161248590Smm	 * Read the data and check it.
162248590Smm	 */
163248590Smm	assert((a = archive_read_new()) != NULL);
164248590Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
165248590Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
166248590Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used));
167248590Smm
168248590Smm	/* Read entries */
169248590Smm	for (i = 0; entries[i].path != NULL; i++) {
170248590Smm		if (dironly && (entries[i].mode & AE_IFMT) != S_IFDIR)
171248590Smm			continue;
172248590Smm		assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
173248590Smm		assertEqualInt(entries[i].mtime, archive_entry_mtime(ae));
174248590Smm		assertEqualInt(entries[i].mode, archive_entry_mode(ae));
175248590Smm		assertEqualInt(entries[i].uid, archive_entry_uid(ae));
176248590Smm		assertEqualInt(entries[i].gid, archive_entry_gid(ae));
177248590Smm		if (i > 0)
178248590Smm			assertEqualString(entries[i].path + 2,
179248590Smm				archive_entry_pathname(ae));
180248590Smm		else
181248590Smm			assertEqualString(entries[i].path,
182248590Smm				archive_entry_pathname(ae));
183248590Smm		if ((entries[i].mode & AE_IFMT) != S_IFDIR)
184248590Smm			assertEqualInt(8, archive_entry_size(ae));
185248590Smm	}
186248590Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
187248590Smm	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
188248590Smm}
189248590Smm
190248590SmmDEFINE_TEST(test_write_format_mtree_classic_indent)
191248590Smm{
192248590Smm	/* Generate classic format. */
193248590Smm	test_write_format_mtree_sub(0);
194248590Smm	/* Generate classic format and Write directory only. */
195248590Smm	test_write_format_mtree_sub(1);
196248590Smm}
197