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/*
30Execute the following command to rebuild the data for this program:
31   tail -n +32 test_read_format_iso_xorriso.c | /bin/sh
32#
33rm -rf /tmp/iso
34mkdir /tmp/iso
35mkdir /tmp/iso/dir
36mkdir /tmp/iso/dir2
37echo "hello" >/tmp/iso/file
38ln /tmp/iso/file /tmp/iso/hardlink
39(cd /tmp/iso; ln -s file symlink)
40TZ=utc touch -afm -t 197001020000.01  /tmp/iso/empty
41echo "hello2" >/tmp/iso/dir/file2
42echo "hello3" >/tmp/iso/dir/file3
43echo "hello4" >/tmp/iso/dir2/file4
44
45TZ=utc touch -afhm -t 197001020000.01 /tmp/iso/dir/file2 /tmp/iso/dir/file3
46TZ=utc touch -afhm -t 197001020000.01 /tmp/iso/dir2/file4
47TZ=utc touch -afhm -t 197001020000.01 /tmp/iso /tmp/iso/file /tmp/iso/dir
48TZ=utc touch -afhm -t 197001020000.01 /tmp/iso/dir2
49TZ=utc touch -afhm -t 197001030000.02 /tmp/iso/symlink
50F=test_read_format_iso_xorriso.iso
51xorriso -outdev - -map /tmp/iso / > $F
52compress $F
53uuencode $F.Z $F.Z > $F.Z.uu
54rm $F.Z
55exit 1
56 */
57
58/*
59 * A test for the iso images made by xorriso which versions are
60 * from 0.6.5 to 1.0.1.
61 * The xorriso set 0 to the location of empty files(include symlink
62 * files) that caused our iso reader could not read following directory
63 * entries at all.
64 *
65 */
66
67DEFINE_TEST(test_read_format_iso_xorriso)
68{
69	const char *refname = "test_read_format_iso_xorriso.iso.Z";
70	struct archive_entry *ae;
71	struct archive *a;
72	const void *p;
73	size_t size;
74	int64_t offset;
75	int i;
76
77	extract_reference_file(refname);
78	assert((a = archive_read_new()) != NULL);
79	assertEqualInt(0, archive_read_support_filter_all(a));
80	assertEqualInt(0, archive_read_support_format_all(a));
81	assertEqualInt(ARCHIVE_OK,
82	    archive_read_open_filename(a, refname, 10240));
83
84	/* Retrieve each of the 10 files on the ISO image and
85	 * verify that each one is what we expect. */
86	for (i = 0; i < 10; ++i) {
87		assertEqualInt(0, archive_read_next_header(a, &ae));
88
89		if (strcmp(".", archive_entry_pathname(ae)) == 0) {
90			/* '.' root directory. */
91			assertEqualInt(AE_IFDIR, archive_entry_filetype(ae));
92			assertEqualInt(2048, archive_entry_size(ae));
93			/* Now, we read timestamp recorded by RRIP "TF". */
94			assertEqualInt(86401, archive_entry_mtime(ae));
95			assertEqualInt(0, archive_entry_mtime_nsec(ae));
96			/* Now, we read links recorded by RRIP "PX". */
97			assertEqualInt(4, archive_entry_nlink(ae));
98			assertEqualIntA(a, ARCHIVE_EOF,
99			    archive_read_data_block(a, &p, &size, &offset));
100			assertEqualInt((int)size, 0);
101		} else if (strcmp("./dir", archive_entry_pathname(ae)) == 0) {
102			/* A directory. */
103			assertEqualInt(AE_IFDIR, archive_entry_filetype(ae));
104			assertEqualInt(2048, archive_entry_size(ae));
105			assertEqualInt(86401, archive_entry_mtime(ae));
106			assertEqualInt(86401, archive_entry_atime(ae));
107			assertEqualInt(2, archive_entry_nlink(ae));
108		} else if (strcmp("./dir2", archive_entry_pathname(ae)) == 0) {
109			/* A directory. */
110			assertEqualInt(AE_IFDIR, archive_entry_filetype(ae));
111			assertEqualInt(2048, archive_entry_size(ae));
112			assertEqualInt(86401, archive_entry_mtime(ae));
113			assertEqualInt(86401, archive_entry_atime(ae));
114			assertEqualInt(2, archive_entry_nlink(ae));
115		} else if (strcmp("./file",
116		    archive_entry_pathname(ae)) == 0) {
117			/* A regular file. */
118			assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
119			assertEqualInt(6, archive_entry_size(ae));
120			assertEqualInt(0,
121			    archive_read_data_block(a, &p, &size, &offset));
122			assertEqualInt(0, offset);
123			assertEqualMem(p, "hello\n", 6);
124			assertEqualInt(86401, archive_entry_mtime(ae));
125			assertEqualInt(86401, archive_entry_atime(ae));
126			assertEqualInt(2, archive_entry_nlink(ae));
127		} else if (strcmp("./hardlink",
128		    archive_entry_pathname(ae)) == 0) {
129			/* A hardlink to the regular file. */
130			/* Note: If "hardlink" gets returned before "file",
131			 * then "hardlink" will get returned as a regular file
132			 * and "file" will get returned as the hardlink.
133			 * This test should tolerate that, since it's a
134			 * perfectly permissible thing for libarchive to do. */
135			assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
136			assertEqualString("./file", archive_entry_hardlink(ae));
137			assertEqualInt(0, archive_entry_size_is_set(ae));
138			assertEqualInt(0, archive_entry_size(ae));
139			assertEqualInt(86401, archive_entry_mtime(ae));
140			assertEqualInt(86401, archive_entry_atime(ae));
141			assertEqualInt(2, archive_entry_stat(ae)->st_nlink);
142		} else if (strcmp("./symlink",
143		    archive_entry_pathname(ae)) == 0) {
144			/* A symlink to the regular file. */
145			assertEqualInt(AE_IFLNK, archive_entry_filetype(ae));
146			assertEqualString("file", archive_entry_symlink(ae));
147			assertEqualInt(0, archive_entry_size(ae));
148			assertEqualInt(172802, archive_entry_mtime(ae));
149			assertEqualInt(172802, archive_entry_atime(ae));
150			assertEqualInt(1, archive_entry_stat(ae)->st_nlink);
151		} else if (strcmp("./empty",
152		    archive_entry_pathname(ae)) == 0) {
153			/* A empty file. */
154			assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
155			assertEqualInt(0, archive_entry_size(ae));
156			assertEqualInt(86401, archive_entry_mtime(ae));
157			assertEqualInt(86401, archive_entry_atime(ae));
158			assertEqualInt(1, archive_entry_nlink(ae));
159		} else if (strcmp("./dir/file2",
160		    archive_entry_pathname(ae)) == 0) {
161			/* A regular file. */
162			assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
163			assertEqualInt(7, archive_entry_size(ae));
164			assertEqualInt(0,
165			    archive_read_data_block(a, &p, &size, &offset));
166			assertEqualInt(0, offset);
167			assertEqualMem(p, "hello2\n", 7);
168			assertEqualInt(86401, archive_entry_mtime(ae));
169			assertEqualInt(86401, archive_entry_atime(ae));
170			assertEqualInt(1, archive_entry_nlink(ae));
171		} else if (strcmp("./dir/file3",
172		    archive_entry_pathname(ae)) == 0) {
173			/* A regular file. */
174			assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
175			assertEqualInt(7, archive_entry_size(ae));
176			assertEqualInt(0,
177			    archive_read_data_block(a, &p, &size, &offset));
178			assertEqualInt(0, offset);
179			assertEqualMem(p, "hello3\n", 7);
180			assertEqualInt(86401, archive_entry_mtime(ae));
181			assertEqualInt(86401, archive_entry_atime(ae));
182			assertEqualInt(1, archive_entry_nlink(ae));
183		} else if (strcmp("./dir2/file4",
184		    archive_entry_pathname(ae)) == 0) {
185			/* A regular file. */
186			assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
187			assertEqualInt(7, archive_entry_size(ae));
188			assertEqualInt(0,
189			    archive_read_data_block(a, &p, &size, &offset));
190			assertEqualInt(0, offset);
191			assertEqualMem(p, "hello4\n", 7);
192			assertEqualInt(86401, archive_entry_mtime(ae));
193			assertEqualInt(86401, archive_entry_atime(ae));
194			assertEqualInt(1, archive_entry_nlink(ae));
195		} else {
196			failure("Saw a file that shouldn't have been there");
197			assertEqualString(archive_entry_pathname(ae), "");
198		}
199	}
200
201	/* End of archive. */
202	assertEqualInt(ARCHIVE_EOF, archive_read_next_header(a, &ae));
203
204	/* Verify archive format. */
205	assertEqualInt(archive_filter_code(a, 0), ARCHIVE_FILTER_COMPRESS);
206	assertEqualInt(archive_format(a), ARCHIVE_FORMAT_ISO9660_ROCKRIDGE);
207
208	/* Close the archive. */
209	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
210	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
211}
212
213
214