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};
51228753Smm
52228753Smmstatic void
53228753Smmtest_write_format_mtree_sub(int use_set, int dironly)
54228753Smm{
55228753Smm	struct archive_entry *ae;
56228753Smm	struct archive* a;
57228753Smm	size_t used;
58228753Smm	int i;
59228753Smm
60228753Smm	/* Create a mtree format archive. */
61228753Smm	assert((a = archive_write_new()) != NULL);
62232153Smm	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_mtree(a));
63228753Smm	if (use_set)
64232153Smm		assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_option(a, NULL, "use-set", "1"));
65228753Smm	if (dironly)
66232153Smm		assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_option(a, NULL, "dironly", "1"));
67232153Smm	assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff)-1, &used));
68228753Smm
69228753Smm	/* Write entries */
70228753Smm	for (i = 0; entries[i].path != NULL; i++) {
71228753Smm		assert((ae = archive_entry_new()) != NULL);
72228753Smm		archive_entry_set_mtime(ae, entries[i].mtime, 0);
73228753Smm		assert(entries[i].mtime == archive_entry_mtime(ae));
74228753Smm		archive_entry_set_mode(ae, entries[i].mode);
75228753Smm		assert(entries[i].mode == archive_entry_mode(ae));
76228753Smm		archive_entry_set_uid(ae, entries[i].uid);
77228753Smm		assert(entries[i].uid == archive_entry_uid(ae));
78228753Smm		archive_entry_set_gid(ae, entries[i].gid);
79228753Smm		assert(entries[i].gid == archive_entry_gid(ae));
80228753Smm		archive_entry_copy_pathname(ae, entries[i].path);
81228753Smm		if ((entries[i].mode & AE_IFMT) != S_IFDIR)
82228753Smm			archive_entry_set_size(ae, 8);
83232153Smm		assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
84228753Smm		if ((entries[i].mode & AE_IFMT) != S_IFDIR)
85232153Smm			assertEqualIntA(a, 8,
86232153Smm			    archive_write_data(a, "Hello012", 15));
87228753Smm		archive_entry_free(ae);
88228753Smm	}
89232153Smm	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
90232153Smm        assertEqualInt(ARCHIVE_OK, archive_write_free(a));
91232153Smm
92228753Smm	if (use_set) {
93228753Smm		const char *p;
94228753Smm
95228753Smm		buff[used] = '\0';
96228753Smm		assert(NULL != (p = strstr(buff, "\n/set ")));
97228753Smm		if (p != NULL) {
98228753Smm			char *r;
99228753Smm			const char *o;
100228753Smm			p++;
101228753Smm			r = strchr(p, '\n');
102228753Smm			if (r != NULL)
103228753Smm				*r = '\0';
104228753Smm			if (dironly)
105228753Smm				o = "/set type=dir uid=1001 gid=1001 mode=755";
106228753Smm			else
107228753Smm				o = "/set type=file uid=1001 gid=1001 mode=644";
108228753Smm			assertEqualString(o, p);
109228753Smm			if (r != NULL)
110228753Smm				*r = '\n';
111228753Smm		}
112228753Smm	}
113228753Smm
114228753Smm	/*
115228753Smm	 * Read the data and check it.
116228753Smm	 */
117228753Smm	assert((a = archive_read_new()) != NULL);
118228753Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
119232153Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
120228753Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, used));
121228753Smm
122228753Smm	/* Read entries */
123228753Smm	for (i = 0; entries[i].path != NULL; i++) {
124228753Smm		if (dironly && (entries[i].mode & AE_IFMT) != S_IFDIR)
125228753Smm			continue;
126228753Smm		assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
127228753Smm		assertEqualInt(entries[i].mtime, archive_entry_mtime(ae));
128228753Smm		assertEqualInt(entries[i].mode, archive_entry_mode(ae));
129228753Smm		assertEqualInt(entries[i].uid, archive_entry_uid(ae));
130228753Smm		assertEqualInt(entries[i].gid, archive_entry_gid(ae));
131228753Smm		assertEqualString(entries[i].path, archive_entry_pathname(ae));
132228753Smm		if ((entries[i].mode & AE_IFMT) != S_IFDIR)
133228753Smm			assertEqualInt(8, archive_entry_size(ae));
134228753Smm	}
135232153Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
136232153Smm	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
137228753Smm}
138228753Smm
139228753SmmDEFINE_TEST(test_write_format_mtree)
140228753Smm{
141228753Smm	/* Default setting */
142228753Smm	test_write_format_mtree_sub(0, 0);
143228753Smm	/* Directory only */
144228753Smm	test_write_format_mtree_sub(0, 1);
145228753Smm	/* Use /set keyword */
146228753Smm	test_write_format_mtree_sub(1, 0);
147228753Smm	/* Use /set keyword with directory only */
148228753Smm	test_write_format_mtree_sub(1, 1);
149228753Smm}
150