1231200Smm/*-
2231200Smm * Copyright (c) 2011 Michihiro NAKAJIMA
3231200Smm * All rights reserved.
4231200Smm *
5231200Smm * Redistribution and use in source and binary forms, with or without
6231200Smm * modification, are permitted provided that the following conditions
7231200Smm * are met:
8231200Smm * 1. Redistributions of source code must retain the above copyright
9231200Smm *    notice, this list of conditions and the following disclaimer.
10231200Smm * 2. Redistributions in binary form must reproduce the above copyright
11231200Smm *    notice, this list of conditions and the following disclaimer in the
12231200Smm *    documentation and/or other materials provided with the distribution.
13231200Smm *
14231200Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15231200Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16231200Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17231200Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18231200Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19231200Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20231200Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21231200Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22231200Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23231200Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24231200Smm */
25231200Smm#include "test.h"
26231200Smm__FBSDID("$FreeBSD$");
27231200Smm
28231200Smm/*
29231200Smm * Verify our ability to read sample files created by Solaris pax for
30231200Smm * a sparse file.
31231200Smm */
32231200Smmstatic void
33231200Smmtest_compat_solaris_pax_sparse_1(void)
34231200Smm{
35231200Smm	char name[] = "test_compat_solaris_pax_sparse_1.pax.Z";
36231200Smm	struct archive_entry *ae;
37231200Smm	struct archive *a;
38231200Smm	int64_t offset, length;
39231200Smm	const void *buff;
40231200Smm	size_t bytes_read;
41231200Smm	char data[1024*8];
42231200Smm	int r;
43231200Smm
44231200Smm	assert((a = archive_read_new()) != NULL);
45231200Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
46231200Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
47231200Smm	extract_reference_file(name);
48231200Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, name, 10240));
49231200Smm
50231200Smm	/* Read first entry. */
51231200Smm	assertEqualIntA(a, ARCHIVE_OK, r = archive_read_next_header(a, &ae));
52231200Smm	if (r != ARCHIVE_OK) {
53231200Smm		archive_read_free(a);
54231200Smm		return;
55231200Smm	}
56231200Smm	assertEqualString("hole", archive_entry_pathname(ae));
57231200Smm	assertEqualInt(1310411683, archive_entry_mtime(ae));
58231200Smm	assertEqualInt(101, archive_entry_uid(ae));
59231200Smm	assertEqualString("cue", archive_entry_uname(ae));
60231200Smm	assertEqualInt(10, archive_entry_gid(ae));
61231200Smm	assertEqualString("staff", archive_entry_gname(ae));
62231200Smm	assertEqualInt(0100644, archive_entry_mode(ae));
63231200Smm
64231200Smm	/* Verify the sparse information. */
65231200Smm	failure("This sparse file should have tree data blocks");
66231200Smm	assertEqualInt(3, archive_entry_sparse_reset(ae));
67231200Smm	assertEqualInt(ARCHIVE_OK,
68231200Smm	    archive_entry_sparse_next(ae, &offset, &length));
69231200Smm	assertEqualInt(0, offset);
70231200Smm	assertEqualInt(131072, length);
71231200Smm	assertEqualInt(ARCHIVE_OK,
72231200Smm	    archive_entry_sparse_next(ae, &offset, &length));
73231200Smm	assertEqualInt(393216, offset);
74231200Smm	assertEqualInt(131072, length);
75231200Smm	assertEqualInt(ARCHIVE_OK,
76231200Smm	    archive_entry_sparse_next(ae, &offset, &length));
77231200Smm	assertEqualInt(786432, offset);
78231200Smm	assertEqualInt(32775, length);
79231200Smm	while (ARCHIVE_OK ==
80231200Smm	    archive_read_data_block(a, &buff, &bytes_read, &offset)) {
81231200Smm		failure("The data blocks should not include the hole");
82231200Smm		assert((offset >= 0 && offset + bytes_read <= 131072) ||
83231200Smm		       (offset >= 393216 && offset + bytes_read <= 393216+131072) ||
84231200Smm		       (offset >= 786432 && offset + bytes_read <= 786432+32775));
85231200Smm		if (offset == 0 && bytes_read >= 1024*8) {
86231200Smm			memset(data, 'a', sizeof(data));
87231200Smm			failure("First data block should be 8K bytes of 'a'");
88231200Smm			assertEqualMem(buff, data, sizeof(data));
89231200Smm		} else if (offset + bytes_read == 819207 && bytes_read >= 7) {
90231200Smm			const char *last = buff;
91231200Smm			last += bytes_read - 7;
92231200Smm			memset(data, 'c', 7);
93231200Smm			failure("Last seven bytes should be all 'c'");
94231200Smm			assertEqualMem(last, data, 7);
95231200Smm		}
96231200Smm	}
97231200Smm
98231200Smm	/* Verify the end-of-archive. */
99231200Smm	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
100231200Smm
101231200Smm	/* Verify that the format detection worked. */
102231200Smm	assertEqualInt(archive_filter_code(a, 0), ARCHIVE_FILTER_COMPRESS);
103231200Smm	assertEqualInt(archive_format(a), ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE);
104231200Smm
105231200Smm	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
106231200Smm	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
107231200Smm}
108231200Smm
109231200Smm/*
110231200Smm * Verify our ability to read sample files created by Solaris pax for
111231200Smm * a sparse file which begin with hole.
112231200Smm */
113231200Smmstatic void
114231200Smmtest_compat_solaris_pax_sparse_2(void)
115231200Smm{
116231200Smm	char name[] = "test_compat_solaris_pax_sparse_2.pax.Z";
117231200Smm	struct archive_entry *ae;
118231200Smm	struct archive *a;
119231200Smm	int64_t offset, length;
120231200Smm	const void *buff;
121231200Smm	size_t bytes_read;
122231200Smm	int r;
123231200Smm
124231200Smm	assert((a = archive_read_new()) != NULL);
125231200Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
126231200Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
127231200Smm	extract_reference_file(name);
128231200Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, name, 10240));
129231200Smm
130231200Smm	/* Read first entry. */
131231200Smm	assertEqualIntA(a, ARCHIVE_OK, r = archive_read_next_header(a, &ae));
132231200Smm	if (r != ARCHIVE_OK) {
133231200Smm		archive_read_free(a);
134231200Smm		return;
135231200Smm	}
136231200Smm	assertEqualString("hole", archive_entry_pathname(ae));
137231200Smm	assertEqualInt(1310416789, archive_entry_mtime(ae));
138231200Smm	assertEqualInt(101, archive_entry_uid(ae));
139231200Smm	assertEqualString("cue", archive_entry_uname(ae));
140231200Smm	assertEqualInt(10, archive_entry_gid(ae));
141231200Smm	assertEqualString("staff", archive_entry_gname(ae));
142231200Smm	assertEqualInt(0100644, archive_entry_mode(ae));
143231200Smm
144231200Smm	/* Verify the sparse information. */
145231200Smm	failure("This sparse file should have two data blocks");
146231200Smm	assertEqualInt(2, archive_entry_sparse_reset(ae));
147231200Smm	assertEqualInt(ARCHIVE_OK,
148231200Smm	    archive_entry_sparse_next(ae, &offset, &length));
149231200Smm	assertEqualInt(393216, offset);
150231200Smm	assertEqualInt(131072, length);
151231200Smm	assertEqualInt(ARCHIVE_OK,
152231200Smm	    archive_entry_sparse_next(ae, &offset, &length));
153231200Smm	assertEqualInt(786432, offset);
154231200Smm	assertEqualInt(32799, length);
155231200Smm	while (ARCHIVE_OK ==
156231200Smm	    archive_read_data_block(a, &buff, &bytes_read, &offset)) {
157231200Smm		failure("The data blocks should not include the hole");
158231200Smm		assert((offset >= 393216 && offset + bytes_read <= 393216+131072) ||
159231200Smm		       (offset >= 786432 && offset + bytes_read <= 786432+32799));
160231200Smm		if (offset + bytes_read == 819231 && bytes_read >= 31) {
161231200Smm			char data[32];
162231200Smm			const char *last = buff;
163231200Smm			last += bytes_read - 31;
164231200Smm			memset(data, 'c', 31);
165231200Smm			failure("Last 31 bytes should be all 'c'");
166231200Smm			assertEqualMem(last, data, 31);
167231200Smm		}
168231200Smm	}
169231200Smm
170231200Smm	/* Verify the end-of-archive. */
171231200Smm	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
172231200Smm
173231200Smm	/* Verify that the format detection worked. */
174231200Smm	assertEqualInt(archive_filter_code(a, 0), ARCHIVE_FILTER_COMPRESS);
175231200Smm	assertEqualInt(archive_format(a), ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE);
176231200Smm
177231200Smm	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
178231200Smm	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
179231200Smm}
180231200Smm
181231200Smm
182231200SmmDEFINE_TEST(test_compat_solaris_pax_sparse)
183231200Smm{
184231200Smm	test_compat_solaris_pax_sparse_1();
185231200Smm	test_compat_solaris_pax_sparse_2();
186231200Smm}
187231200Smm
188231200Smm
189