1/*-
2 * Copyright (c) 2009 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__FBSDID("$FreeBSD$");
29
30static char buff[4096];
31static struct {
32	const char	*path;
33	mode_t		 mode;
34	time_t		 mtime;
35	uid_t	 	 uid;
36	gid_t		 gid;
37} entries[] = {
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 },
49	{ NULL, 0, 0, 0, 0 }
50};
51static struct {
52  const char  *path;
53  mode_t     mode;
54  time_t     mtime;
55  uid_t    uid;
56  gid_t    gid;
57} entries2[] = {
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 },
69  { NULL, 0, 0, 0, 0 }
70};
71
72static void
73test_write_format_mtree_sub(int use_set, int dironly)
74{
75	struct archive_entry *ae;
76	struct archive* a;
77	size_t used;
78	int i;
79
80	/* Create a mtree format archive. */
81	assert((a = archive_write_new()) != NULL);
82	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_mtree(a));
83	if (use_set)
84		assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_option(a, NULL, "use-set", "1"));
85	if (dironly)
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));
88
89	/* Write entries */
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);
108	}
109	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
110        assertEqualInt(ARCHIVE_OK, archive_write_free(a));
111
112	if (use_set) {
113		const char *p;
114
115		buff[used] = '\0';
116		assert(NULL != (p = strstr(buff, "\n/set ")));
117		if (p != NULL) {
118			char *r;
119			const char *o;
120			p++;
121			r = strchr(p, '\n');
122			if (r != NULL)
123				*r = '\0';
124			if (dironly)
125				o = "/set type=dir uid=1001 gid=1001 mode=755";
126			else
127				o = "/set type=file uid=1001 gid=1001 mode=644";
128			assertEqualString(o, p);
129			if (r != NULL)
130				*r = '\n';
131		}
132	}
133
134	/*
135	 * Read the data and check it.
136	 */
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));
141
142	/* Read entries */
143	for (i = 0; entries[i].path != NULL; i++) {
144		if (dironly && (entries[i].mode & AE_IFMT) != S_IFDIR)
145			continue;
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));
154	}
155	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
156	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
157}
158
159static void
160test_write_format_mtree_sub2(int use_set, int dironly)
161{
162  struct archive_entry *ae;
163  struct archive* a;
164  size_t used;
165  int i;
166  char str[32];
167
168  /* Create a mtree format archive. */
169  assert((a = archive_write_new()) != NULL);
170  assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_mtree(a));
171  if (use_set)
172    assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_option(a, NULL, "use-set", "1"));
173  if (dironly)
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));
176
177  /* Write entries2 */
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);
196  }
197  assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
198        assertEqualInt(ARCHIVE_OK, archive_write_free(a));
199
200  if (use_set) {
201    const char *p;
202
203    buff[used] = '\0';
204    assert(NULL != (p = strstr(buff, "\n/set ")));
205    if (p != NULL) {
206      char *r;
207      const char *o;
208      p++;
209      r = strchr(p, '\n');
210      if (r != NULL)
211        *r = '\0';
212      if (dironly)
213        o = "/set type=dir uid=1001 gid=1001 mode=755";
214      else
215        o = "/set type=file uid=1001 gid=1001 mode=644";
216      assertEqualString(o, p);
217      if (r != NULL)
218        *r = '\n';
219    }
220  }
221
222  /*
223   * Read the data and check it.
224   */
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));
229
230  /* Read entries2 */
231  memset(str, 0, sizeof(str));
232  strcpy(str, "./");
233  for (i = 0; entries2[i].path != NULL; i++) {
234    if (dironly && (entries2[i].mode & AE_IFMT) != S_IFDIR)
235      continue;
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));
245  }
246  assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
247  assertEqualInt(ARCHIVE_OK, archive_read_free(a));
248}
249
250DEFINE_TEST(test_write_format_mtree)
251{
252	/* Default setting */
253	test_write_format_mtree_sub(0, 0);
254	/* Directory only */
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);
260}
261
262DEFINE_TEST(test_write_format_mtree_no_leading_dotslash)
263{
264  /* Default setting */
265  test_write_format_mtree_sub2(0, 0);
266  /* Directory only */
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);
272}
273