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 /*
27  * Verify our ability to read sample files created by Solaris pax for
28  * a sparse file.
29  */
30 static void
test_compat_solaris_pax_sparse_1(void)31 test_compat_solaris_pax_sparse_1(void)
32 {
33 	char name[] = "test_compat_solaris_pax_sparse_1.pax.Z";
34 	struct archive_entry *ae;
35 	struct archive *a;
36 	int64_t offset, length;
37 	const void *buff;
38 	size_t bytes_read;
39 	char data[1024*8];
40 	int r;
41 
42 	assert((a = archive_read_new()) != NULL);
43 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
44 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
45 	extract_reference_file(name);
46 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, name, 10240));
47 
48 	/* Read first entry. */
49 	assertEqualIntA(a, ARCHIVE_OK, r = archive_read_next_header(a, &ae));
50 	if (r != ARCHIVE_OK) {
51 		archive_read_free(a);
52 		return;
53 	}
54 	assertEqualString("hole", archive_entry_pathname(ae));
55 	assertEqualInt(1310411683, archive_entry_mtime(ae));
56 	assertEqualInt(101, archive_entry_uid(ae));
57 	assertEqualString("cue", archive_entry_uname(ae));
58 	assertEqualInt(10, archive_entry_gid(ae));
59 	assertEqualString("staff", archive_entry_gname(ae));
60 	assertEqualInt(0100644, archive_entry_mode(ae));
61 
62 	/* Verify the sparse information. */
63 	failure("This sparse file should have tree data blocks");
64 	assertEqualInt(3, archive_entry_sparse_reset(ae));
65 	assertEqualInt(ARCHIVE_OK,
66 	    archive_entry_sparse_next(ae, &offset, &length));
67 	assertEqualInt(0, offset);
68 	assertEqualInt(131072, length);
69 	assertEqualInt(ARCHIVE_OK,
70 	    archive_entry_sparse_next(ae, &offset, &length));
71 	assertEqualInt(393216, offset);
72 	assertEqualInt(131072, length);
73 	assertEqualInt(ARCHIVE_OK,
74 	    archive_entry_sparse_next(ae, &offset, &length));
75 	assertEqualInt(786432, offset);
76 	assertEqualInt(32775, length);
77 	while (ARCHIVE_OK ==
78 	    archive_read_data_block(a, &buff, &bytes_read, &offset)) {
79 		failure("The data blocks should not include the hole");
80 		assert((offset >= 0 && offset + bytes_read <= 131072) ||
81 		       (offset >= 393216 && offset + bytes_read <= 393216+131072) ||
82 		       (offset >= 786432 && offset + bytes_read <= 786432+32775));
83 		if (offset == 0 && bytes_read >= 1024*8) {
84 			memset(data, 'a', sizeof(data));
85 			failure("First data block should be 8K bytes of 'a'");
86 			assertEqualMem(buff, data, sizeof(data));
87 		} else if (offset + bytes_read == 819207 && bytes_read >= 7) {
88 			const char *last = buff;
89 			last += bytes_read - 7;
90 			memset(data, 'c', 7);
91 			failure("Last seven bytes should be all 'c'");
92 			assertEqualMem(last, data, 7);
93 		}
94 	}
95 
96 	/* Verify the end-of-archive. */
97 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
98 
99 	/* Verify that the format detection worked. */
100 	assertEqualInt(archive_filter_code(a, 0), ARCHIVE_FILTER_COMPRESS);
101 	assertEqualInt(archive_format(a), ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE);
102 
103 	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
104 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
105 }
106 
107 /*
108  * Verify our ability to read sample files created by Solaris pax for
109  * a sparse file which begin with hole.
110  */
111 static void
test_compat_solaris_pax_sparse_2(void)112 test_compat_solaris_pax_sparse_2(void)
113 {
114 	char name[] = "test_compat_solaris_pax_sparse_2.pax.Z";
115 	struct archive_entry *ae;
116 	struct archive *a;
117 	int64_t offset, length;
118 	const void *buff;
119 	size_t bytes_read;
120 	int r;
121 
122 	assert((a = archive_read_new()) != NULL);
123 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
124 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
125 	extract_reference_file(name);
126 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, name, 10240));
127 
128 	/* Read first entry. */
129 	assertEqualIntA(a, ARCHIVE_OK, r = archive_read_next_header(a, &ae));
130 	if (r != ARCHIVE_OK) {
131 		archive_read_free(a);
132 		return;
133 	}
134 	assertEqualString("hole", archive_entry_pathname(ae));
135 	assertEqualInt(1310416789, archive_entry_mtime(ae));
136 	assertEqualInt(101, archive_entry_uid(ae));
137 	assertEqualString("cue", archive_entry_uname(ae));
138 	assertEqualInt(10, archive_entry_gid(ae));
139 	assertEqualString("staff", archive_entry_gname(ae));
140 	assertEqualInt(0100644, archive_entry_mode(ae));
141 
142 	/* Verify the sparse information. */
143 	failure("This sparse file should have two data blocks");
144 	assertEqualInt(2, archive_entry_sparse_reset(ae));
145 	assertEqualInt(ARCHIVE_OK,
146 	    archive_entry_sparse_next(ae, &offset, &length));
147 	assertEqualInt(393216, offset);
148 	assertEqualInt(131072, length);
149 	assertEqualInt(ARCHIVE_OK,
150 	    archive_entry_sparse_next(ae, &offset, &length));
151 	assertEqualInt(786432, offset);
152 	assertEqualInt(32799, length);
153 	while (ARCHIVE_OK ==
154 	    archive_read_data_block(a, &buff, &bytes_read, &offset)) {
155 		failure("The data blocks should not include the hole");
156 		assert((offset >= 393216 && offset + bytes_read <= 393216+131072) ||
157 		       (offset >= 786432 && offset + bytes_read <= 786432+32799));
158 		if (offset + bytes_read == 819231 && bytes_read >= 31) {
159 			char data[32];
160 			const char *last = buff;
161 			last += bytes_read - 31;
162 			memset(data, 'c', 31);
163 			failure("Last 31 bytes should be all 'c'");
164 			assertEqualMem(last, data, 31);
165 		}
166 	}
167 
168 	/* Verify the end-of-archive. */
169 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
170 
171 	/* Verify that the format detection worked. */
172 	assertEqualInt(archive_filter_code(a, 0), ARCHIVE_FILTER_COMPRESS);
173 	assertEqualInt(archive_format(a), ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE);
174 
175 	assertEqualInt(ARCHIVE_OK, archive_read_close(a));
176 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
177 }
178 
179 
DEFINE_TEST(test_compat_solaris_pax_sparse)180 DEFINE_TEST(test_compat_solaris_pax_sparse)
181 {
182 	test_compat_solaris_pax_sparse_1();
183 	test_compat_solaris_pax_sparse_2();
184 }
185 
186 
187