1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
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 Execute the following command to rebuild the data for this program:
30    tail -n +32 test_read_format_isozisofs_bz2.c | /bin/sh
31 
32 rm -rf /tmp/iso /tmp/ziso
33 mkdir /tmp/iso
34 mkdir /tmp/iso/dir
35 echo "hello" >/tmp/iso/file
36 dd if=/dev/zero count=1 bs=12345678 >>/tmp/iso/file
37 ln /tmp/iso/file /tmp/iso/hardlink
38 (cd /tmp/iso; ln -s file symlink)
39 (cd /tmp/iso; ln -s /tmp/ symlink2)
40 (cd /tmp/iso; ln -s /tmp/../ symlink3)
41 (cd /tmp/iso; ln -s .././../tmp/ symlink4)
42 TZ=utc touch -afhm -t 197001020000.01 /tmp/iso /tmp/iso/file /tmp/iso/dir
43 TZ=utc touch -afhm -t 197001030000.02 /tmp/iso/symlink
44 mkzftree /tmp/iso /tmp/ziso
45 TZ=utc touch -afhm -t 197001020000.01 /tmp/ziso /tmp/ziso/file /tmp/ziso/dir
46 TZ=utc touch -afhm -t 197001030000.02 /tmp/ziso/symlink
47 F=test_read_format_iso_zisofs.iso.Z
48 mkhybrid -R -uid 1 -gid 2 -z /tmp/ziso | compress > $F
49 uuencode $F $F > $F.uu
50 exit 1
51 
52  */
53 
54 DEFINE_TEST(test_read_format_isozisofs_bz2)
55 {
56 	const char *refname = "test_read_format_iso_zisofs.iso.Z";
57 	struct archive_entry *ae;
58 	struct archive *a;
59 	const void *p;
60 	size_t size;
61 	int64_t offset;
62 	int i;
63 
64 	extract_reference_file(refname);
65 	assert((a = archive_read_new()) != NULL);
66 	assertEqualInt(0, archive_read_support_filter_all(a));
67 	assertEqualInt(0, archive_read_support_format_all(a));
68 	assertEqualInt(ARCHIVE_OK,
69 	    archive_read_open_filename(a, refname, 10240));
70 
71 	/* Retrieve each of the 8 files on the ISO image and
72 	 * verify that each one is what we expect. */
73 	for (i = 0; i < 8; ++i) {
74 		assertEqualInt(0, archive_read_next_header(a, &ae));
75 
76 		assertEqualInt(archive_entry_is_encrypted(ae), 0);
77 		assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
78 
79 		if (strcmp(".", archive_entry_pathname(ae)) == 0) {
80 			/* '.' root directory. */
81 			assertEqualInt(AE_IFDIR, archive_entry_filetype(ae));
82 			assertEqualInt(2048, archive_entry_size(ae));
83 			/* Now, we read timestamp recorded by RRIP "TF". */
84 			assertEqualInt(86401, archive_entry_mtime(ae));
85 			assertEqualInt(0, archive_entry_mtime_nsec(ae));
86 			/* Now, we read links recorded by RRIP "PX". */
87 			assertEqualInt(3, archive_entry_stat(ae)->st_nlink);
88 			assertEqualInt(1, archive_entry_uid(ae));
89 			assertEqualIntA(a, ARCHIVE_EOF,
90 			    archive_read_data_block(a, &p, &size, &offset));
91 			assertEqualInt((int)size, 0);
92 		} else if (strcmp("dir", archive_entry_pathname(ae)) == 0) {
93 			/* A directory. */
94 			assertEqualString("dir", archive_entry_pathname(ae));
95 			assertEqualInt(AE_IFDIR, archive_entry_filetype(ae));
96 			assertEqualInt(2048, archive_entry_size(ae));
97 			assertEqualInt(86401, archive_entry_mtime(ae));
98 			assertEqualInt(86401, archive_entry_atime(ae));
99 			assertEqualInt(2, archive_entry_stat(ae)->st_nlink);
100 			assertEqualInt(1, archive_entry_uid(ae));
101 			assertEqualInt(2, archive_entry_gid(ae));
102 		} else if (strcmp("file", archive_entry_pathname(ae)) == 0) {
103 			int r;
104 			/* A regular file. */
105 			assertEqualString("file", archive_entry_pathname(ae));
106 			assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
107 			assertEqualInt(12345684, archive_entry_size(ae));
108 			r = archive_read_data_block(a, &p, &size, &offset);
109 			if (r == ARCHIVE_FAILED) {
110 			  skipping("Can't read body of ZISOFS entry.");
111 			} else {
112 			  assertEqualInt(ARCHIVE_OK, r);
113 			  assertEqualInt(0, offset);
114 			  assertEqualMem(p, "hello\n", 6);
115 			}
116 			assertEqualInt(86401, archive_entry_mtime(ae));
117 			assertEqualInt(86401, archive_entry_atime(ae));
118 			assertEqualInt(2, archive_entry_stat(ae)->st_nlink);
119 			assertEqualInt(1, archive_entry_uid(ae));
120 			assertEqualInt(2, archive_entry_gid(ae));
121 		} else if (strcmp("hardlink", archive_entry_pathname(ae)) == 0) {
122 			/* A hardlink to the regular file. */
123 			/* Note: If "hardlink" gets returned before "file",
124 			 * then "hardlink" will get returned as a regular file
125 			 * and "file" will get returned as the hardlink.
126 			 * This test should tolerate that, since it's a
127 			 * perfectly permissible thing for libarchive to do. */
128 			assertEqualString("hardlink", archive_entry_pathname(ae));
129 			assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
130 			assertEqualString("file", archive_entry_hardlink(ae));
131 			assertEqualInt(0, archive_entry_size_is_set(ae));
132 			assertEqualInt(0, archive_entry_size(ae));
133 			assertEqualInt(86401, archive_entry_mtime(ae));
134 			assertEqualInt(2, archive_entry_stat(ae)->st_nlink);
135 			assertEqualInt(1, archive_entry_uid(ae));
136 			assertEqualInt(2, archive_entry_gid(ae));
137 		} else if (strcmp("symlink", archive_entry_pathname(ae)) == 0) {
138 			/* A symlink to the regular file. */
139 			assertEqualInt(AE_IFLNK, archive_entry_filetype(ae));
140 			assertEqualString("file", archive_entry_symlink(ae));
141 			assertEqualInt(0, archive_entry_size(ae));
142 			assertEqualInt(172802, archive_entry_mtime(ae));
143 			assertEqualInt(172802, archive_entry_atime(ae));
144 			assertEqualInt(1, archive_entry_stat(ae)->st_nlink);
145 			assertEqualInt(1, archive_entry_uid(ae));
146 			assertEqualInt(2, archive_entry_gid(ae));
147 		} else if (strcmp("symlink2", archive_entry_pathname(ae)) == 0) {
148 			/* A symlink to /tmp (an absolute path) */
149 			assertEqualInt(AE_IFLNK, archive_entry_filetype(ae));
150 			assertEqualString("/tmp", archive_entry_symlink(ae));
151 			assertEqualInt(0, archive_entry_size(ae));
152 			assertEqualInt(1, archive_entry_stat(ae)->st_nlink);
153 			assertEqualInt(1, archive_entry_uid(ae));
154 			assertEqualInt(2, archive_entry_gid(ae));
155 		} else if (strcmp("symlink3", archive_entry_pathname(ae)) == 0) {
156 			/* A symlink to /tmp/.. (with a ".." component) */
157 			assertEqualInt(AE_IFLNK, archive_entry_filetype(ae));
158 			assertEqualString("/tmp/..", archive_entry_symlink(ae));
159 			assertEqualInt(0, archive_entry_size(ae));
160 			assertEqualInt(1, archive_entry_stat(ae)->st_nlink);
161 			assertEqualInt(1, archive_entry_uid(ae));
162 			assertEqualInt(2, archive_entry_gid(ae));
163 		} else if (strcmp("symlink4", archive_entry_pathname(ae)) == 0) {
164 			/* A symlink to a path with ".." and "." components */
165 			assertEqualInt(AE_IFLNK, archive_entry_filetype(ae));
166 			assertEqualString(".././../tmp",
167 			    archive_entry_symlink(ae));
168 			assertEqualInt(0, archive_entry_size(ae));
169 			assertEqualInt(1, archive_entry_stat(ae)->st_nlink);
170 			assertEqualInt(1, archive_entry_uid(ae));
171 			assertEqualInt(2, archive_entry_gid(ae));
172 		} else {
173 			failure("Saw a file that shouldn't have been there");
174 			assertEqualString(archive_entry_pathname(ae), "");
175 		}
176 	}
177 
178 	/* End of archive. */
179 	assertEqualInt(ARCHIVE_EOF, archive_read_next_header(a, &ae));
180 
181 	/* Verify archive format. */
182 	assertEqualInt(archive_filter_code(a, 0), ARCHIVE_FILTER_COMPRESS);
183 	assertEqualInt(archive_format(a), ARCHIVE_FORMAT_ISO9660_ROCKRIDGE);
184 
185 	/* Close the archive. */
186 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
187 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
188 }
189 
190 
191