1/*-
2 * Copyright (c) 2011 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 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25#include "test.h"
26__FBSDID("$FreeBSD$");
27
28/*
29 * Verify our ability to read sample files created by Solaris pax for
30 * a sparse file.
31 */
32static void
33test_compat_solaris_pax_sparse_1(void)
34{
35	char name[] = "test_compat_solaris_pax_sparse_1.pax.Z";
36	struct archive_entry *ae;
37	struct archive *a;
38	int64_t offset, length;
39	const void *buff;
40	size_t bytes_read;
41	char data[1024*8];
42	int r;
43
44	assert((a = archive_read_new()) != NULL);
45	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
46	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
47	extract_reference_file(name);
48	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, name, 10240));
49
50	/* Read first entry. */
51	assertEqualIntA(a, ARCHIVE_OK, r = archive_read_next_header(a, &ae));
52	if (r != ARCHIVE_OK) {
53		archive_read_free(a);
54		return;
55	}
56	assertEqualString("hole", archive_entry_pathname(ae));
57	assertEqualInt(1310411683, archive_entry_mtime(ae));
58	assertEqualInt(101, archive_entry_uid(ae));
59	assertEqualString("cue", archive_entry_uname(ae));
60	assertEqualInt(10, archive_entry_gid(ae));
61	assertEqualString("staff", archive_entry_gname(ae));
62	assertEqualInt(0100644, archive_entry_mode(ae));
63
64	/* Verify the sparse information. */
65	failure("This sparse file should have tree data blocks");
66	assertEqualInt(3, archive_entry_sparse_reset(ae));
67	assertEqualInt(ARCHIVE_OK,
68	    archive_entry_sparse_next(ae, &offset, &length));
69	assertEqualInt(0, offset);
70	assertEqualInt(131072, length);
71	assertEqualInt(ARCHIVE_OK,
72	    archive_entry_sparse_next(ae, &offset, &length));
73	assertEqualInt(393216, offset);
74	assertEqualInt(131072, length);
75	assertEqualInt(ARCHIVE_OK,
76	    archive_entry_sparse_next(ae, &offset, &length));
77	assertEqualInt(786432, offset);
78	assertEqualInt(32775, length);
79	while (ARCHIVE_OK ==
80	    archive_read_data_block(a, &buff, &bytes_read, &offset)) {
81		failure("The data blocks should not include the hole");
82		assert((offset >= 0 && offset + bytes_read <= 131072) ||
83		       (offset >= 393216 && offset + bytes_read <= 393216+131072) ||
84		       (offset >= 786432 && offset + bytes_read <= 786432+32775));
85		if (offset == 0 && bytes_read >= 1024*8) {
86			memset(data, 'a', sizeof(data));
87			failure("First data block should be 8K bytes of 'a'");
88			assertEqualMem(buff, data, sizeof(data));
89		} else if (offset + bytes_read == 819207 && bytes_read >= 7) {
90			const char *last = buff;
91			last += bytes_read - 7;
92			memset(data, 'c', 7);
93			failure("Last seven bytes should be all 'c'");
94			assertEqualMem(last, data, 7);
95		}
96	}
97
98	/* Verify the end-of-archive. */
99	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
100
101	/* Verify that the format detection worked. */
102	assertEqualInt(archive_filter_code(a, 0), ARCHIVE_FILTER_COMPRESS);
103	assertEqualInt(archive_format(a), ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE);
104
105	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
106	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
107}
108
109/*
110 * Verify our ability to read sample files created by Solaris pax for
111 * a sparse file which begin with hole.
112 */
113static void
114test_compat_solaris_pax_sparse_2(void)
115{
116	char name[] = "test_compat_solaris_pax_sparse_2.pax.Z";
117	struct archive_entry *ae;
118	struct archive *a;
119	int64_t offset, length;
120	const void *buff;
121	size_t bytes_read;
122	int r;
123
124	assert((a = archive_read_new()) != NULL);
125	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
126	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
127	extract_reference_file(name);
128	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, name, 10240));
129
130	/* Read first entry. */
131	assertEqualIntA(a, ARCHIVE_OK, r = archive_read_next_header(a, &ae));
132	if (r != ARCHIVE_OK) {
133		archive_read_free(a);
134		return;
135	}
136	assertEqualString("hole", archive_entry_pathname(ae));
137	assertEqualInt(1310416789, archive_entry_mtime(ae));
138	assertEqualInt(101, archive_entry_uid(ae));
139	assertEqualString("cue", archive_entry_uname(ae));
140	assertEqualInt(10, archive_entry_gid(ae));
141	assertEqualString("staff", archive_entry_gname(ae));
142	assertEqualInt(0100644, archive_entry_mode(ae));
143
144	/* Verify the sparse information. */
145	failure("This sparse file should have two data blocks");
146	assertEqualInt(2, archive_entry_sparse_reset(ae));
147	assertEqualInt(ARCHIVE_OK,
148	    archive_entry_sparse_next(ae, &offset, &length));
149	assertEqualInt(393216, offset);
150	assertEqualInt(131072, length);
151	assertEqualInt(ARCHIVE_OK,
152	    archive_entry_sparse_next(ae, &offset, &length));
153	assertEqualInt(786432, offset);
154	assertEqualInt(32799, length);
155	while (ARCHIVE_OK ==
156	    archive_read_data_block(a, &buff, &bytes_read, &offset)) {
157		failure("The data blocks should not include the hole");
158		assert((offset >= 393216 && offset + bytes_read <= 393216+131072) ||
159		       (offset >= 786432 && offset + bytes_read <= 786432+32799));
160		if (offset + bytes_read == 819231 && bytes_read >= 31) {
161			char data[32];
162			const char *last = buff;
163			last += bytes_read - 31;
164			memset(data, 'c', 31);
165			failure("Last 31 bytes should be all 'c'");
166			assertEqualMem(last, data, 31);
167		}
168	}
169
170	/* Verify the end-of-archive. */
171	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
172
173	/* Verify that the format detection worked. */
174	assertEqualInt(archive_filter_code(a, 0), ARCHIVE_FILTER_COMPRESS);
175	assertEqualInt(archive_format(a), ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE);
176
177	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
178	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
179}
180
181
182DEFINE_TEST(test_compat_solaris_pax_sparse)
183{
184	test_compat_solaris_pax_sparse_1();
185	test_compat_solaris_pax_sparse_2();
186}
187
188
189