1228753Smm/*-
2228753Smm * Copyright (c) 2009 Michihiro NAKAJIMA
3228753Smm * All rights reserved.
4228753Smm *
5228753Smm * Redistribution and use in source and binary forms, with or without
6228753Smm * modification, are permitted provided that the following conditions
7228753Smm * are met:
8228753Smm * 1. Redistributions of source code must retain the above copyright
9228753Smm *    notice, this list of conditions and the following disclaimer
10228753Smm *    in this position and unchanged.
11228753Smm * 2. Redistributions in binary form must reproduce the above copyright
12228753Smm *    notice, this list of conditions and the following disclaimer in the
13228753Smm *    documentation and/or other materials provided with the distribution.
14228753Smm *
15228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25228753Smm */
26228753Smm
27228753Smm#include "test.h"
28228763Smm__FBSDID("$FreeBSD$");
29228753Smm
30228753Smmstatic char buff[4096];
31228753Smmstatic struct {
32228753Smm	const char	*path;
33228753Smm	mode_t		 mode;
34228753Smm	time_t		 mtime;
35228753Smm	uid_t	 	 uid;
36228753Smm	gid_t		 gid;
37228753Smm} entries[] = {
38248616Smm	{ "./COPYING",		S_IFREG | 0644, 1231975636, 1001, 1001 },
39228753Smm	{ "./Makefile", 	S_IFREG | 0644, 1233041050, 1001, 1001 },
40228753Smm	{ "./NEWS", 		S_IFREG | 0644, 1231975636, 1001, 1001 },
41228753Smm	{ "./PROJECTS", 	S_IFREG | 0644, 1231975636, 1001, 1001 },
42228753Smm	{ "./README",		S_IFREG | 0644, 1231975636, 1001, 1001 },
43228753Smm	{ "./subdir",		S_IFDIR | 0755, 1233504586, 1001, 1001 },
44228753Smm	{ "./subdir/README",	S_IFREG | 0664, 1231975636, 1002, 1001 },
45228753Smm	{ "./subdir/config",	S_IFREG | 0664, 1232266273, 1003, 1003 },
46228753Smm	{ "./subdir2",		S_IFDIR | 0755, 1233504586, 1001, 1001 },
47228753Smm	{ "./subdir3",		S_IFDIR | 0755, 1233504586, 1001, 1001 },
48228753Smm	{ "./subdir3/mtree",	S_IFREG | 0664, 1232266273, 1003, 1003 },
49228753Smm	{ NULL, 0, 0, 0, 0 }
50228753Smm};
51302001Smmstatic struct {
52302001Smm  const char  *path;
53302001Smm  mode_t     mode;
54302001Smm  time_t     mtime;
55302001Smm  uid_t    uid;
56302001Smm  gid_t    gid;
57302001Smm} entries2[] = {
58302001Smm  { "COPYING",    S_IFREG | 0644, 1231975636, 1001, 1001 },
59302001Smm  { "Makefile",   S_IFREG | 0644, 1233041050, 1001, 1001 },
60302001Smm  { "NEWS",     S_IFREG | 0644, 1231975636, 1001, 1001 },
61302001Smm  { "PROJECTS",   S_IFREG | 0644, 1231975636, 1001, 1001 },
62302001Smm  { "README",   S_IFREG | 0644, 1231975636, 1001, 1001 },
63302001Smm  { "subdir",   S_IFDIR | 0755, 1233504586, 1001, 1001 },
64302001Smm  { "subdir/README",  S_IFREG | 0664, 1231975636, 1002, 1001 },
65302001Smm  { "subdir/config",  S_IFREG | 0664, 1232266273, 1003, 1003 },
66302001Smm  { "subdir2",    S_IFDIR | 0755, 1233504586, 1001, 1001 },
67302001Smm  { "subdir3",    S_IFDIR | 0755, 1233504586, 1001, 1001 },
68302001Smm  { "subdir3/mtree",  S_IFREG | 0664, 1232266273, 1003, 1003 },
69302001Smm  { NULL, 0, 0, 0, 0 }
70302001Smm};
71228753Smm
72228753Smmstatic void
73228753Smmtest_write_format_mtree_sub(int use_set, int dironly)
74228753Smm{
75228753Smm	struct archive_entry *ae;
76228753Smm	struct archive* a;
77228753Smm	size_t used;
78228753Smm	int i;
79228753Smm
80228753Smm	/* Create a mtree format archive. */
81228753Smm	assert((a = archive_write_new()) != NULL);
82232153Smm	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_mtree(a));
83228753Smm	if (use_set)
84232153Smm		assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_option(a, NULL, "use-set", "1"));
85228753Smm	if (dironly)
86232153Smm		assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_option(a, NULL, "dironly", "1"));
87232153Smm	assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff)-1, &used));
88228753Smm
89228753Smm	/* Write entries */
90228753Smm	for (i = 0; entries[i].path != NULL; i++) {
91228753Smm		assert((ae = archive_entry_new()) != NULL);
92228753Smm		archive_entry_set_mtime(ae, entries[i].mtime, 0);
93228753Smm		assert(entries[i].mtime == archive_entry_mtime(ae));
94228753Smm		archive_entry_set_mode(ae, entries[i].mode);
95228753Smm		assert(entries[i].mode == archive_entry_mode(ae));
96228753Smm		archive_entry_set_uid(ae, entries[i].uid);
97228753Smm		assert(entries[i].uid == archive_entry_uid(ae));
98228753Smm		archive_entry_set_gid(ae, entries[i].gid);
99228753Smm		assert(entries[i].gid == archive_entry_gid(ae));
100228753Smm		archive_entry_copy_pathname(ae, entries[i].path);
101228753Smm		if ((entries[i].mode & AE_IFMT) != S_IFDIR)
102228753Smm			archive_entry_set_size(ae, 8);
103232153Smm		assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
104228753Smm		if ((entries[i].mode & AE_IFMT) != S_IFDIR)
105232153Smm			assertEqualIntA(a, 8,
106232153Smm			    archive_write_data(a, "Hello012", 15));
107228753Smm		archive_entry_free(ae);
108228753Smm	}
109232153Smm	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
110232153Smm        assertEqualInt(ARCHIVE_OK, archive_write_free(a));
111232153Smm
112228753Smm	if (use_set) {
113228753Smm		const char *p;
114228753Smm
115228753Smm		buff[used] = '\0';
116228753Smm		assert(NULL != (p = strstr(buff, "\n/set ")));
117228753Smm		if (p != NULL) {
118228753Smm			char *r;
119228753Smm			const char *o;
120228753Smm			p++;
121228753Smm			r = strchr(p, '\n');
122228753Smm			if (r != NULL)
123228753Smm				*r = '\0';
124228753Smm			if (dironly)
125228753Smm				o = "/set type=dir uid=1001 gid=1001 mode=755";
126228753Smm			else
127228753Smm				o = "/set type=file uid=1001 gid=1001 mode=644";
128228753Smm			assertEqualString(o, p);
129228753Smm			if (r != NULL)
130228753Smm				*r = '\n';
131228753Smm		}
132228753Smm	}
133228753Smm
134228753Smm	/*
135228753Smm	 * Read the data and check it.
136228753Smm	 */
137228753Smm	assert((a = archive_read_new()) != NULL);
138228753Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
139232153Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
140228753Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used));
141228753Smm
142228753Smm	/* Read entries */
143228753Smm	for (i = 0; entries[i].path != NULL; i++) {
144228753Smm		if (dironly && (entries[i].mode & AE_IFMT) != S_IFDIR)
145228753Smm			continue;
146228753Smm		assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
147228753Smm		assertEqualInt(entries[i].mtime, archive_entry_mtime(ae));
148228753Smm		assertEqualInt(entries[i].mode, archive_entry_mode(ae));
149228753Smm		assertEqualInt(entries[i].uid, archive_entry_uid(ae));
150228753Smm		assertEqualInt(entries[i].gid, archive_entry_gid(ae));
151228753Smm		assertEqualString(entries[i].path, archive_entry_pathname(ae));
152228753Smm		if ((entries[i].mode & AE_IFMT) != S_IFDIR)
153228753Smm			assertEqualInt(8, archive_entry_size(ae));
154228753Smm	}
155232153Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
156232153Smm	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
157228753Smm}
158228753Smm
159302001Smmstatic void
160302001Smmtest_write_format_mtree_sub2(int use_set, int dironly)
161302001Smm{
162302001Smm  struct archive_entry *ae;
163302001Smm  struct archive* a;
164302001Smm  size_t used;
165302001Smm  int i;
166302001Smm  char str[32];
167302001Smm
168302001Smm  /* Create a mtree format archive. */
169302001Smm  assert((a = archive_write_new()) != NULL);
170302001Smm  assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_mtree(a));
171302001Smm  if (use_set)
172302001Smm    assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_option(a, NULL, "use-set", "1"));
173302001Smm  if (dironly)
174302001Smm    assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_option(a, NULL, "dironly", "1"));
175302001Smm  assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff)-1, &used));
176302001Smm
177302001Smm  /* Write entries2 */
178302001Smm  for (i = 0; entries2[i].path != NULL; i++) {
179302001Smm    assert((ae = archive_entry_new()) != NULL);
180302001Smm    archive_entry_set_mtime(ae, entries2[i].mtime, 0);
181302001Smm    assert(entries2[i].mtime == archive_entry_mtime(ae));
182302001Smm    archive_entry_set_mode(ae, entries2[i].mode);
183302001Smm    assert(entries2[i].mode == archive_entry_mode(ae));
184302001Smm    archive_entry_set_uid(ae, entries2[i].uid);
185302001Smm    assert(entries2[i].uid == archive_entry_uid(ae));
186302001Smm    archive_entry_set_gid(ae, entries2[i].gid);
187302001Smm    assert(entries2[i].gid == archive_entry_gid(ae));
188302001Smm    archive_entry_copy_pathname(ae, entries2[i].path);
189302001Smm    if ((entries2[i].mode & AE_IFMT) != S_IFDIR)
190302001Smm      archive_entry_set_size(ae, 8);
191302001Smm    assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
192302001Smm    if ((entries2[i].mode & AE_IFMT) != S_IFDIR)
193302001Smm      assertEqualIntA(a, 8,
194302001Smm          archive_write_data(a, "Hello012", 15));
195302001Smm    archive_entry_free(ae);
196302001Smm  }
197302001Smm  assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
198302001Smm        assertEqualInt(ARCHIVE_OK, archive_write_free(a));
199302001Smm
200302001Smm  if (use_set) {
201302001Smm    const char *p;
202302001Smm
203302001Smm    buff[used] = '\0';
204302001Smm    assert(NULL != (p = strstr(buff, "\n/set ")));
205302001Smm    if (p != NULL) {
206302001Smm      char *r;
207302001Smm      const char *o;
208302001Smm      p++;
209302001Smm      r = strchr(p, '\n');
210302001Smm      if (r != NULL)
211302001Smm        *r = '\0';
212302001Smm      if (dironly)
213302001Smm        o = "/set type=dir uid=1001 gid=1001 mode=755";
214302001Smm      else
215302001Smm        o = "/set type=file uid=1001 gid=1001 mode=644";
216302001Smm      assertEqualString(o, p);
217302001Smm      if (r != NULL)
218302001Smm        *r = '\n';
219302001Smm    }
220302001Smm  }
221302001Smm
222302001Smm  /*
223302001Smm   * Read the data and check it.
224302001Smm   */
225302001Smm  assert((a = archive_read_new()) != NULL);
226302001Smm  assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
227302001Smm  assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
228302001Smm  assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used));
229302001Smm
230302001Smm  /* Read entries2 */
231302001Smm  memset(str, 0, sizeof(str));
232302001Smm  strcpy(str, "./");
233302001Smm  for (i = 0; entries2[i].path != NULL; i++) {
234302001Smm    if (dironly && (entries2[i].mode & AE_IFMT) != S_IFDIR)
235302001Smm      continue;
236302001Smm    assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
237302001Smm    assertEqualInt(entries2[i].mtime, archive_entry_mtime(ae));
238302001Smm    assertEqualInt(entries2[i].mode, archive_entry_mode(ae));
239302001Smm    assertEqualInt(entries2[i].uid, archive_entry_uid(ae));
240302001Smm    assertEqualInt(entries2[i].gid, archive_entry_gid(ae));
241302001Smm    strcpy(str + 2, entries2[i].path);
242302001Smm    assertEqualString(str, archive_entry_pathname(ae));
243302001Smm    if ((entries2[i].mode & AE_IFMT) != S_IFDIR)
244302001Smm      assertEqualInt(8, archive_entry_size(ae));
245302001Smm  }
246302001Smm  assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
247302001Smm  assertEqualInt(ARCHIVE_OK, archive_read_free(a));
248302001Smm}
249302001Smm
250228753SmmDEFINE_TEST(test_write_format_mtree)
251228753Smm{
252228753Smm	/* Default setting */
253228753Smm	test_write_format_mtree_sub(0, 0);
254228753Smm	/* Directory only */
255228753Smm	test_write_format_mtree_sub(0, 1);
256228753Smm	/* Use /set keyword */
257228753Smm	test_write_format_mtree_sub(1, 0);
258228753Smm	/* Use /set keyword with directory only */
259228753Smm	test_write_format_mtree_sub(1, 1);
260228753Smm}
261302001Smm
262302001SmmDEFINE_TEST(test_write_format_mtree_no_leading_dotslash)
263302001Smm{
264302001Smm  /* Default setting */
265302001Smm  test_write_format_mtree_sub2(0, 0);
266302001Smm  /* Directory only */
267302001Smm  test_write_format_mtree_sub2(0, 1);
268302001Smm  /* Use /set keyword */
269302001Smm  test_write_format_mtree_sub2(1, 0);
270302001Smm  /* Use /set keyword with directory only */
271302001Smm  test_write_format_mtree_sub2(1, 1);
272302001Smm}
273