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: releng/11.0/contrib/libarchive/libarchive/test/test_write_format_mtree.c 299529 2016-05-12 10:16:16Z mm $");
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};
51299529Smmstatic struct {
52299529Smm  const char  *path;
53299529Smm  mode_t     mode;
54299529Smm  time_t     mtime;
55299529Smm  uid_t    uid;
56299529Smm  gid_t    gid;
57299529Smm} entries2[] = {
58299529Smm  { "COPYING",    S_IFREG | 0644, 1231975636, 1001, 1001 },
59299529Smm  { "Makefile",   S_IFREG | 0644, 1233041050, 1001, 1001 },
60299529Smm  { "NEWS",     S_IFREG | 0644, 1231975636, 1001, 1001 },
61299529Smm  { "PROJECTS",   S_IFREG | 0644, 1231975636, 1001, 1001 },
62299529Smm  { "README",   S_IFREG | 0644, 1231975636, 1001, 1001 },
63299529Smm  { "subdir",   S_IFDIR | 0755, 1233504586, 1001, 1001 },
64299529Smm  { "subdir/README",  S_IFREG | 0664, 1231975636, 1002, 1001 },
65299529Smm  { "subdir/config",  S_IFREG | 0664, 1232266273, 1003, 1003 },
66299529Smm  { "subdir2",    S_IFDIR | 0755, 1233504586, 1001, 1001 },
67299529Smm  { "subdir3",    S_IFDIR | 0755, 1233504586, 1001, 1001 },
68299529Smm  { "subdir3/mtree",  S_IFREG | 0664, 1232266273, 1003, 1003 },
69299529Smm  { NULL, 0, 0, 0, 0 }
70299529Smm};
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
159299529Smmstatic void
160299529Smmtest_write_format_mtree_sub2(int use_set, int dironly)
161299529Smm{
162299529Smm  struct archive_entry *ae;
163299529Smm  struct archive* a;
164299529Smm  size_t used;
165299529Smm  int i;
166299529Smm  char str[32];
167299529Smm
168299529Smm  /* Create a mtree format archive. */
169299529Smm  assert((a = archive_write_new()) != NULL);
170299529Smm  assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_mtree(a));
171299529Smm  if (use_set)
172299529Smm    assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_option(a, NULL, "use-set", "1"));
173299529Smm  if (dironly)
174299529Smm    assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_option(a, NULL, "dironly", "1"));
175299529Smm  assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff)-1, &used));
176299529Smm
177299529Smm  /* Write entries2 */
178299529Smm  for (i = 0; entries2[i].path != NULL; i++) {
179299529Smm    assert((ae = archive_entry_new()) != NULL);
180299529Smm    archive_entry_set_mtime(ae, entries2[i].mtime, 0);
181299529Smm    assert(entries2[i].mtime == archive_entry_mtime(ae));
182299529Smm    archive_entry_set_mode(ae, entries2[i].mode);
183299529Smm    assert(entries2[i].mode == archive_entry_mode(ae));
184299529Smm    archive_entry_set_uid(ae, entries2[i].uid);
185299529Smm    assert(entries2[i].uid == archive_entry_uid(ae));
186299529Smm    archive_entry_set_gid(ae, entries2[i].gid);
187299529Smm    assert(entries2[i].gid == archive_entry_gid(ae));
188299529Smm    archive_entry_copy_pathname(ae, entries2[i].path);
189299529Smm    if ((entries2[i].mode & AE_IFMT) != S_IFDIR)
190299529Smm      archive_entry_set_size(ae, 8);
191299529Smm    assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
192299529Smm    if ((entries2[i].mode & AE_IFMT) != S_IFDIR)
193299529Smm      assertEqualIntA(a, 8,
194299529Smm          archive_write_data(a, "Hello012", 15));
195299529Smm    archive_entry_free(ae);
196299529Smm  }
197299529Smm  assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
198299529Smm        assertEqualInt(ARCHIVE_OK, archive_write_free(a));
199299529Smm
200299529Smm  if (use_set) {
201299529Smm    const char *p;
202299529Smm
203299529Smm    buff[used] = '\0';
204299529Smm    assert(NULL != (p = strstr(buff, "\n/set ")));
205299529Smm    if (p != NULL) {
206299529Smm      char *r;
207299529Smm      const char *o;
208299529Smm      p++;
209299529Smm      r = strchr(p, '\n');
210299529Smm      if (r != NULL)
211299529Smm        *r = '\0';
212299529Smm      if (dironly)
213299529Smm        o = "/set type=dir uid=1001 gid=1001 mode=755";
214299529Smm      else
215299529Smm        o = "/set type=file uid=1001 gid=1001 mode=644";
216299529Smm      assertEqualString(o, p);
217299529Smm      if (r != NULL)
218299529Smm        *r = '\n';
219299529Smm    }
220299529Smm  }
221299529Smm
222299529Smm  /*
223299529Smm   * Read the data and check it.
224299529Smm   */
225299529Smm  assert((a = archive_read_new()) != NULL);
226299529Smm  assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
227299529Smm  assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
228299529Smm  assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used));
229299529Smm
230299529Smm  /* Read entries2 */
231299529Smm  memset(str, 0, sizeof(str));
232299529Smm  strcpy(str, "./");
233299529Smm  for (i = 0; entries2[i].path != NULL; i++) {
234299529Smm    if (dironly && (entries2[i].mode & AE_IFMT) != S_IFDIR)
235299529Smm      continue;
236299529Smm    assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
237299529Smm    assertEqualInt(entries2[i].mtime, archive_entry_mtime(ae));
238299529Smm    assertEqualInt(entries2[i].mode, archive_entry_mode(ae));
239299529Smm    assertEqualInt(entries2[i].uid, archive_entry_uid(ae));
240299529Smm    assertEqualInt(entries2[i].gid, archive_entry_gid(ae));
241299529Smm    strcpy(str + 2, entries2[i].path);
242299529Smm    assertEqualString(str, archive_entry_pathname(ae));
243299529Smm    if ((entries2[i].mode & AE_IFMT) != S_IFDIR)
244299529Smm      assertEqualInt(8, archive_entry_size(ae));
245299529Smm  }
246299529Smm  assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
247299529Smm  assertEqualInt(ARCHIVE_OK, archive_read_free(a));
248299529Smm}
249299529Smm
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}
261299529Smm
262299529SmmDEFINE_TEST(test_write_format_mtree_no_leading_dotslash)
263299529Smm{
264299529Smm  /* Default setting */
265299529Smm  test_write_format_mtree_sub2(0, 0);
266299529Smm  /* Directory only */
267299529Smm  test_write_format_mtree_sub2(0, 1);
268299529Smm  /* Use /set keyword */
269299529Smm  test_write_format_mtree_sub2(1, 0);
270299529Smm  /* Use /set keyword with directory only */
271299529Smm  test_write_format_mtree_sub2(1, 1);
272299529Smm}
273