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/*
30231200SmmExecute the following command to rebuild the data for this program:
31231200Smm   tail -n +32 test_read_format_iso_xorriso.c | /bin/sh
32231200Smm#
33231200Smmrm -rf /tmp/iso
34231200Smmmkdir /tmp/iso
35231200Smmmkdir /tmp/iso/dir
36231200Smmmkdir /tmp/iso/dir2
37231200Smmecho "hello" >/tmp/iso/file
38231200Smmln /tmp/iso/file /tmp/iso/hardlink
39231200Smm(cd /tmp/iso; ln -s file symlink)
40231200SmmTZ=utc touch -afm -t 197001020000.01  /tmp/iso/empty
41231200Smmecho "hello2" >/tmp/iso/dir/file2
42231200Smmecho "hello3" >/tmp/iso/dir/file3
43231200Smmecho "hello4" >/tmp/iso/dir2/file4
44231200Smm
45231200SmmTZ=utc touch -afhm -t 197001020000.01 /tmp/iso/dir/file2 /tmp/iso/dir/file3
46231200SmmTZ=utc touch -afhm -t 197001020000.01 /tmp/iso/dir2/file4
47231200SmmTZ=utc touch -afhm -t 197001020000.01 /tmp/iso /tmp/iso/file /tmp/iso/dir
48231200SmmTZ=utc touch -afhm -t 197001020000.01 /tmp/iso/dir2
49231200SmmTZ=utc touch -afhm -t 197001030000.02 /tmp/iso/symlink
50231200SmmF=test_read_format_iso_xorriso.iso
51231200Smmxorriso -outdev - -map /tmp/iso / > $F
52231200Smmcompress $F
53231200Smmuuencode $F.Z $F.Z > $F.Z.uu
54231200Smmrm $F.Z
55231200Smmexit 1
56231200Smm */
57231200Smm
58231200Smm/*
59231200Smm * A test for the iso images made by xorriso which versions are
60231200Smm * from 0.6.5 to 1.0.1.
61231200Smm * The xorriso set 0 to the location of empty files(include symlink
62231200Smm * files) that caused our iso reader could not read following directory
63231200Smm * entries at all.
64231200Smm *
65231200Smm */
66231200Smm
67231200SmmDEFINE_TEST(test_read_format_iso_xorriso)
68231200Smm{
69231200Smm	const char *refname = "test_read_format_iso_xorriso.iso.Z";
70231200Smm	struct archive_entry *ae;
71231200Smm	struct archive *a;
72231200Smm	const void *p;
73231200Smm	size_t size;
74231200Smm	int64_t offset;
75231200Smm	int i;
76231200Smm
77231200Smm	extract_reference_file(refname);
78231200Smm	assert((a = archive_read_new()) != NULL);
79231200Smm	assertEqualInt(0, archive_read_support_filter_all(a));
80231200Smm	assertEqualInt(0, archive_read_support_format_all(a));
81231200Smm	assertEqualInt(ARCHIVE_OK,
82231200Smm	    archive_read_open_filename(a, refname, 10240));
83231200Smm
84231200Smm	/* Retrieve each of the 10 files on the ISO image and
85231200Smm	 * verify that each one is what we expect. */
86231200Smm	for (i = 0; i < 10; ++i) {
87231200Smm		assertEqualInt(0, archive_read_next_header(a, &ae));
88231200Smm
89299529Smm		assertEqualInt(archive_entry_is_encrypted(ae), 0);
90299529Smm		assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
91299529Smm
92231200Smm		if (strcmp(".", archive_entry_pathname(ae)) == 0) {
93231200Smm			/* '.' root directory. */
94231200Smm			assertEqualInt(AE_IFDIR, archive_entry_filetype(ae));
95231200Smm			assertEqualInt(2048, archive_entry_size(ae));
96231200Smm			/* Now, we read timestamp recorded by RRIP "TF". */
97231200Smm			assertEqualInt(86401, archive_entry_mtime(ae));
98231200Smm			assertEqualInt(0, archive_entry_mtime_nsec(ae));
99231200Smm			/* Now, we read links recorded by RRIP "PX". */
100231200Smm			assertEqualInt(4, archive_entry_nlink(ae));
101231200Smm			assertEqualIntA(a, ARCHIVE_EOF,
102231200Smm			    archive_read_data_block(a, &p, &size, &offset));
103231200Smm			assertEqualInt((int)size, 0);
104231200Smm		} else if (strcmp("./dir", archive_entry_pathname(ae)) == 0) {
105231200Smm			/* A directory. */
106231200Smm			assertEqualInt(AE_IFDIR, archive_entry_filetype(ae));
107231200Smm			assertEqualInt(2048, archive_entry_size(ae));
108231200Smm			assertEqualInt(86401, archive_entry_mtime(ae));
109231200Smm			assertEqualInt(86401, archive_entry_atime(ae));
110231200Smm			assertEqualInt(2, archive_entry_nlink(ae));
111231200Smm		} else if (strcmp("./dir2", archive_entry_pathname(ae)) == 0) {
112231200Smm			/* A directory. */
113231200Smm			assertEqualInt(AE_IFDIR, archive_entry_filetype(ae));
114231200Smm			assertEqualInt(2048, archive_entry_size(ae));
115231200Smm			assertEqualInt(86401, archive_entry_mtime(ae));
116231200Smm			assertEqualInt(86401, archive_entry_atime(ae));
117231200Smm			assertEqualInt(2, archive_entry_nlink(ae));
118231200Smm		} else if (strcmp("./file",
119231200Smm		    archive_entry_pathname(ae)) == 0) {
120231200Smm			/* A regular file. */
121231200Smm			assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
122231200Smm			assertEqualInt(6, archive_entry_size(ae));
123231200Smm			assertEqualInt(0,
124231200Smm			    archive_read_data_block(a, &p, &size, &offset));
125231200Smm			assertEqualInt(0, offset);
126231200Smm			assertEqualMem(p, "hello\n", 6);
127231200Smm			assertEqualInt(86401, archive_entry_mtime(ae));
128231200Smm			assertEqualInt(86401, archive_entry_atime(ae));
129231200Smm			assertEqualInt(2, archive_entry_nlink(ae));
130231200Smm		} else if (strcmp("./hardlink",
131231200Smm		    archive_entry_pathname(ae)) == 0) {
132231200Smm			/* A hardlink to the regular file. */
133231200Smm			/* Note: If "hardlink" gets returned before "file",
134231200Smm			 * then "hardlink" will get returned as a regular file
135231200Smm			 * and "file" will get returned as the hardlink.
136231200Smm			 * This test should tolerate that, since it's a
137231200Smm			 * perfectly permissible thing for libarchive to do. */
138231200Smm			assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
139231200Smm			assertEqualString("./file", archive_entry_hardlink(ae));
140231200Smm			assertEqualInt(0, archive_entry_size_is_set(ae));
141231200Smm			assertEqualInt(0, archive_entry_size(ae));
142231200Smm			assertEqualInt(86401, archive_entry_mtime(ae));
143231200Smm			assertEqualInt(86401, archive_entry_atime(ae));
144231200Smm			assertEqualInt(2, archive_entry_stat(ae)->st_nlink);
145231200Smm		} else if (strcmp("./symlink",
146231200Smm		    archive_entry_pathname(ae)) == 0) {
147231200Smm			/* A symlink to the regular file. */
148231200Smm			assertEqualInt(AE_IFLNK, archive_entry_filetype(ae));
149231200Smm			assertEqualString("file", archive_entry_symlink(ae));
150231200Smm			assertEqualInt(0, archive_entry_size(ae));
151231200Smm			assertEqualInt(172802, archive_entry_mtime(ae));
152231200Smm			assertEqualInt(172802, archive_entry_atime(ae));
153231200Smm			assertEqualInt(1, archive_entry_stat(ae)->st_nlink);
154231200Smm		} else if (strcmp("./empty",
155231200Smm		    archive_entry_pathname(ae)) == 0) {
156231200Smm			/* A empty file. */
157231200Smm			assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
158231200Smm			assertEqualInt(0, archive_entry_size(ae));
159231200Smm			assertEqualInt(86401, archive_entry_mtime(ae));
160231200Smm			assertEqualInt(86401, archive_entry_atime(ae));
161231200Smm			assertEqualInt(1, archive_entry_nlink(ae));
162231200Smm		} else if (strcmp("./dir/file2",
163231200Smm		    archive_entry_pathname(ae)) == 0) {
164231200Smm			/* A regular file. */
165231200Smm			assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
166231200Smm			assertEqualInt(7, archive_entry_size(ae));
167231200Smm			assertEqualInt(0,
168231200Smm			    archive_read_data_block(a, &p, &size, &offset));
169231200Smm			assertEqualInt(0, offset);
170231200Smm			assertEqualMem(p, "hello2\n", 7);
171231200Smm			assertEqualInt(86401, archive_entry_mtime(ae));
172231200Smm			assertEqualInt(86401, archive_entry_atime(ae));
173231200Smm			assertEqualInt(1, archive_entry_nlink(ae));
174231200Smm		} else if (strcmp("./dir/file3",
175231200Smm		    archive_entry_pathname(ae)) == 0) {
176231200Smm			/* A regular file. */
177231200Smm			assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
178231200Smm			assertEqualInt(7, archive_entry_size(ae));
179231200Smm			assertEqualInt(0,
180231200Smm			    archive_read_data_block(a, &p, &size, &offset));
181231200Smm			assertEqualInt(0, offset);
182231200Smm			assertEqualMem(p, "hello3\n", 7);
183231200Smm			assertEqualInt(86401, archive_entry_mtime(ae));
184231200Smm			assertEqualInt(86401, archive_entry_atime(ae));
185231200Smm			assertEqualInt(1, archive_entry_nlink(ae));
186231200Smm		} else if (strcmp("./dir2/file4",
187231200Smm		    archive_entry_pathname(ae)) == 0) {
188231200Smm			/* A regular file. */
189231200Smm			assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
190231200Smm			assertEqualInt(7, archive_entry_size(ae));
191231200Smm			assertEqualInt(0,
192231200Smm			    archive_read_data_block(a, &p, &size, &offset));
193231200Smm			assertEqualInt(0, offset);
194231200Smm			assertEqualMem(p, "hello4\n", 7);
195231200Smm			assertEqualInt(86401, archive_entry_mtime(ae));
196231200Smm			assertEqualInt(86401, archive_entry_atime(ae));
197231200Smm			assertEqualInt(1, archive_entry_nlink(ae));
198231200Smm		} else {
199231200Smm			failure("Saw a file that shouldn't have been there");
200231200Smm			assertEqualString(archive_entry_pathname(ae), "");
201231200Smm		}
202231200Smm	}
203231200Smm
204231200Smm	/* End of archive. */
205231200Smm	assertEqualInt(ARCHIVE_EOF, archive_read_next_header(a, &ae));
206231200Smm
207231200Smm	/* Verify archive format. */
208248616Smm	assertEqualInt(archive_filter_code(a, 0), ARCHIVE_FILTER_COMPRESS);
209231200Smm	assertEqualInt(archive_format(a), ARCHIVE_FORMAT_ISO9660_ROCKRIDGE);
210231200Smm
211231200Smm	/* Close the archive. */
212231200Smm	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
213231200Smm	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
214231200Smm}
215231200Smm
216231200Smm
217