1/*-
2 * Copyright (c) 2012 Michihiro NAKAJIMA
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 *    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.
14 *
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.
25 */
26
27#include "test.h"
28
29static char buff[4096];
30static struct {
31	const char	*path;
32	mode_t		 mode;
33	int		 nlink;
34	time_t		 mtime;
35	uid_t	 	 uid;
36	gid_t		 gid;
37} entries[] = {
38	{ ".",			S_IFDIR | 0755, 3, 1231975636, 1001, 1001 },
39	{ "./COPYING",		S_IFREG | 0644, 1, 1231975636, 1001, 1001 },
40	{ "./Makefile", 	S_IFREG | 0644, 1, 1233041050, 1001, 1001 },
41	{ "./NEWS", 		S_IFREG | 0644, 1, 1231975636, 1001, 1001 },
42	{ "./PROJECTS", 	S_IFREG | 0644, 1, 1231975636, 1001, 1001 },
43	{ "./README",		S_IFREG | 0644, 1, 1231975636, 1001, 1001 },
44	{ "./subdir",		S_IFDIR | 0755, 3, 1233504586, 1001, 1001 },
45	{ "./subdir/README",	S_IFREG | 0664, 1, 1231975636, 1002, 1001 },
46	{ "./subdir/config",	S_IFREG | 0664, 1, 1232266273, 1003, 1003 },
47	{ "./subdir2",		S_IFDIR | 0755, 3, 1233504586, 1001, 1001 },
48	{ "./subdir3",		S_IFDIR | 0755, 3, 1233504586, 1001, 1001 },
49	{ "./subdir3/mtree",	S_IFREG | 0664, 2, 1232266273, 1003, 1003 },
50	{ NULL, 0, 0, 0, 0, 0 }
51};
52
53static const char image [] = {
54"#mtree\n"
55"\n"
56"# .\n"
57"/set type=file uid=1001 gid=1001 mode=644\n"
58".               time=1231975636.0 mode=755 type=dir\n"
59"    COPYING         time=1231975636.0 size=8\n"
60"    Makefile        time=1233041050.0 size=8\n"
61"    NEWS            time=1231975636.0 size=8\n"
62"    PROJECTS        time=1231975636.0 size=8\n"
63"    README          time=1231975636.0 size=8\n"
64"\n"
65"# ./subdir\n"
66"/set mode=664\n"
67"    subdir          time=1233504586.0 mode=755 type=dir\n"
68"        README          time=1231975636.0 uid=1002 size=8\n"
69"        config          time=1232266273.0 gid=1003 uid=1003 size=8\n"
70"    # ./subdir\n"
71"    ..\n"
72"\n"
73"\n"
74"# ./subdir2\n"
75"    subdir2         time=1233504586.0 mode=755 type=dir\n"
76"    # ./subdir2\n"
77"    ..\n"
78"\n"
79"\n"
80"# ./subdir3\n"
81"    subdir3         time=1233504586.0 mode=755 type=dir\n"
82"        mtree           nlink=2 time=1232266273.0 gid=1003 uid=1003 size=8\n"
83"    # ./subdir3\n"
84"    ..\n"
85"\n"
86"..\n\n"
87};
88
89static const char image_dironly [] = {
90"#mtree\n"
91"# .\n"
92"/set type=dir uid=1001 gid=1001 mode=755\n"
93".               time=1231975636.0\n"
94"# ./subdir\n"
95"    subdir          time=1233504586.0\n"
96"    # ./subdir\n"
97"    ..\n"
98"# ./subdir2\n"
99"    subdir2         time=1233504586.0\n"
100"    # ./subdir2\n"
101"    ..\n"
102"# ./subdir3\n"
103"    subdir3         time=1233504586.0\n"
104"    # ./subdir3\n"
105"    ..\n"
106"..\n"
107};
108
109static void
110test_write_format_mtree_sub(int dironly)
111{
112	struct archive_entry *ae;
113	struct archive* a;
114	size_t used;
115	int i;
116
117	/* Create a mtree format archive. */
118	assert((a = archive_write_new()) != NULL);
119	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_mtree_classic(a));
120	assertEqualIntA(a, ARCHIVE_OK,
121		archive_write_set_format_option(a, NULL, "indent", "1"));
122	if (dironly)
123		assertEqualIntA(a, ARCHIVE_OK,
124			archive_write_set_format_option(a, NULL, "dironly", "1"));
125	assertEqualIntA(a, ARCHIVE_OK,
126		archive_write_open_memory(a, buff, sizeof(buff)-1, &used));
127
128	/* Write entries */
129	for (i = 0; entries[i].path != NULL; i++) {
130		assert((ae = archive_entry_new()) != NULL);
131		archive_entry_set_nlink(ae, entries[i].nlink);
132		assertEqualInt(entries[i].nlink, archive_entry_nlink(ae));
133		archive_entry_set_mtime(ae, entries[i].mtime, 0);
134		assertEqualInt(entries[i].mtime, archive_entry_mtime(ae));
135		archive_entry_set_mode(ae, entries[i].mode);
136		assertEqualInt(entries[i].mode, archive_entry_mode(ae));
137		archive_entry_set_uid(ae, entries[i].uid);
138		assertEqualInt(entries[i].uid, archive_entry_uid(ae));
139		archive_entry_set_gid(ae, entries[i].gid);
140		assertEqualInt(entries[i].gid, archive_entry_gid(ae));
141		archive_entry_copy_pathname(ae, entries[i].path);
142		if ((entries[i].mode & AE_IFMT) != S_IFDIR)
143			archive_entry_set_size(ae, 8);
144		assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
145		if ((entries[i].mode & AE_IFMT) != S_IFDIR)
146			assertEqualIntA(a, 8,
147			    archive_write_data(a, "Hello012", 15));
148		archive_entry_free(ae);
149	}
150	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
151        assertEqualInt(ARCHIVE_OK, archive_write_free(a));
152
153	buff[used] = '\0';
154	if (dironly)
155		assertEqualString(buff, image_dironly);
156	else
157		assertEqualString(buff, image);
158
159	/*
160	 * Read the data and check it.
161	 */
162	assert((a = archive_read_new()) != NULL);
163	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
164	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
165	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used));
166
167	/* Read entries */
168	for (i = 0; entries[i].path != NULL; i++) {
169		if (dironly && (entries[i].mode & AE_IFMT) != S_IFDIR)
170			continue;
171		assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
172		assertEqualInt(entries[i].mtime, archive_entry_mtime(ae));
173		assertEqualInt(entries[i].mode, archive_entry_mode(ae));
174		assertEqualInt(entries[i].uid, archive_entry_uid(ae));
175		assertEqualInt(entries[i].gid, archive_entry_gid(ae));
176		if (i > 0)
177			assertEqualString(entries[i].path + 2,
178				archive_entry_pathname(ae));
179		else
180			assertEqualString(entries[i].path,
181				archive_entry_pathname(ae));
182		if ((entries[i].mode & AE_IFMT) != S_IFDIR)
183			assertEqualInt(8, archive_entry_size(ae));
184	}
185	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
186	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
187}
188
189DEFINE_TEST(test_write_format_mtree_classic_indent)
190{
191	/* Generate classic format. */
192	test_write_format_mtree_sub(0);
193	/* Generate classic format and Write directory only. */
194	test_write_format_mtree_sub(1);
195}
196