1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
5  * Based on libarchive/test/test_read_format_isorr_bz2.c with
6  * bugs introduced by Andreas Henriksson <andreas@fatal.se> for
7  * testing ISO9660 image with Joliet extension.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #include "test.h"
30 __FBSDID("$FreeBSD$");
31 
32 /*
33 Execute the following to rebuild the data for this program:
34    tail -n +35 test_read_format_isojoliet_rr.c | /bin/sh
35 
36 rm -rf /tmp/iso
37 mkdir /tmp/iso
38 mkdir /tmp/iso/dir
39 file="long-joliet-file-name.textfile"
40 echo "hello" >/tmp/iso/$file
41 ln /tmp/iso/$file /tmp/iso/hardlink
42 (cd /tmp/iso; ln -s $file symlink)
43 if [ "$(uname -s)" = "Linux" ]; then # gnu coreutils touch doesn't have -h
44 TZ=utc touch -afm -t 197001020000.01 /tmp/iso/hardlink /tmp/iso/$file /tmp/iso/dir
45 TZ=utc touch -afm -t 197001030000.02 /tmp/iso/symlink
46 TZ=utc touch -afm -t 197001020000.01 /tmp/iso
47 else
48 TZ=utc touch -afhm -t 197001020000.01 /tmp/iso/hardlink /tmp/iso/$file /tmp/iso/dir
49 TZ=utc touch -afhm -t 197001030000.02 /tmp/iso/symlink
50 TZ=utc touch -afhm -t 197001020000.01 /tmp/iso
51 fi
52 F=test_read_format_iso_joliet_rockridge.iso.Z
53 mkhybrid -J -uid 1 -gid 2 /tmp/iso | compress > $F
54 uuencode $F $F > $F.uu
55 exit 1
56  */
57 
58 DEFINE_TEST(test_read_format_isojoliet_rr)
59 {
60 	const char *refname = "test_read_format_iso_joliet_rockridge.iso.Z";
61 	struct archive_entry *ae;
62 	struct archive *a;
63 	const void *p;
64 	size_t size;
65 	int64_t offset;
66 
67 	extract_reference_file(refname);
68 	assert((a = archive_read_new()) != NULL);
69 	assertEqualInt(0, archive_read_support_filter_all(a));
70 	assertEqualInt(0, archive_read_support_format_all(a));
71 	assertEqualInt(ARCHIVE_OK,
72 	    archive_read_open_filename(a, refname, 10240));
73 
74 	/* First entry is '.' root directory. */
75 	assertEqualInt(0, archive_read_next_header(a, &ae));
76 	assertEqualString(".", archive_entry_pathname(ae));
77 	assertEqualInt(AE_IFDIR, archive_entry_filetype(ae));
78 	assertEqualInt(2048, archive_entry_size(ae));
79 	assertEqualInt(86401, archive_entry_mtime(ae));
80 	assertEqualInt(0, archive_entry_mtime_nsec(ae));
81 	assertEqualInt(3, archive_entry_stat(ae)->st_nlink);
82 	assertEqualInt(1, archive_entry_uid(ae));
83 	assertEqualIntA(a, ARCHIVE_EOF,
84 	    archive_read_data_block(a, &p, &size, &offset));
85 	assertEqualInt((int)size, 0);
86 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
87 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
88 
89 	/* A directory. */
90 	assertEqualInt(0, archive_read_next_header(a, &ae));
91 	assertEqualString("dir", archive_entry_pathname(ae));
92 	assertEqualInt(AE_IFDIR, archive_entry_filetype(ae));
93 	assertEqualInt(2048, archive_entry_size(ae));
94 	assertEqualInt(86401, archive_entry_mtime(ae));
95 	assertEqualInt(86401, archive_entry_atime(ae));
96 	assertEqualInt(2, archive_entry_stat(ae)->st_nlink);
97 	assertEqualInt(1, archive_entry_uid(ae));
98 	assertEqualInt(2, archive_entry_gid(ae));
99 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
100 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
101 
102 	/* A regular file with two names ("hardlink" gets returned
103 	 * first, so it's not marked as a hardlink). */
104 	assertEqualInt(0, archive_read_next_header(a, &ae));
105 	assertEqualString("hardlink",
106 	    archive_entry_pathname(ae));
107 	assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
108 	assert(archive_entry_hardlink(ae) == NULL);
109 	assertEqualInt(6, archive_entry_size(ae));
110 	assertEqualInt(0, archive_read_data_block(a, &p, &size, &offset));
111 	assertEqualInt(6, (int)size);
112 	assertEqualInt(0, offset);
113 	assertEqualMem(p, "hello\n", 6);
114 	assertEqualInt(86401, archive_entry_mtime(ae));
115 	/* mkisofs records their access time. */
116 	/*assertEqualInt(86401, archive_entry_atime(ae));*/
117 	/* TODO: Actually, libarchive should be able to
118 	 * compute nlinks correctly even without RR
119 	 * extensions. See comments in libarchive source. */
120 	assertEqualInt(2, archive_entry_nlink(ae));
121 	assertEqualInt(1, archive_entry_uid(ae));
122 	assertEqualInt(2, archive_entry_gid(ae));
123 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
124 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
125 
126 	/* Second name for the same regular file (this happens to be
127 	 * returned second, so does get marked as a hardlink). */
128 	assertEqualInt(0, archive_read_next_header(a, &ae));
129 	assertEqualString("long-joliet-file-name.textfile",
130 	    archive_entry_pathname(ae));
131 	assertEqualInt(AE_IFREG, archive_entry_filetype(ae));
132 	assertEqualString("hardlink",
133 	    archive_entry_hardlink(ae));
134 	assert(!archive_entry_size_is_set(ae));
135 	assertEqualInt(86401, archive_entry_mtime(ae));
136 	assertEqualInt(86401, archive_entry_atime(ae));
137 	/* TODO: See above. */
138 	assertEqualInt(2, archive_entry_nlink(ae));
139 	assertEqualInt(1, archive_entry_uid(ae));
140 	assertEqualInt(2, archive_entry_gid(ae));
141 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
142 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
143 
144 	/* A symlink to the regular file. */
145 	assertEqualInt(0, archive_read_next_header(a, &ae));
146 	assertEqualString("symlink", archive_entry_pathname(ae));
147 	assertEqualInt(AE_IFLNK, archive_entry_filetype(ae));
148 	assertEqualString("long-joliet-file-name.textfile",
149 	    archive_entry_symlink(ae));
150 	assertEqualInt(0, archive_entry_size(ae));
151 	assertEqualInt(172802, archive_entry_mtime(ae));
152 	assertEqualInt(172802, archive_entry_atime(ae));
153 	assertEqualInt(1, archive_entry_nlink(ae));
154 	assertEqualInt(1, archive_entry_uid(ae));
155 	assertEqualInt(2, archive_entry_gid(ae));
156 	assertEqualInt(archive_entry_is_encrypted(ae), 0);
157 	assertEqualIntA(a, archive_read_has_encrypted_entries(a), ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED);
158 
159 	/* End of archive. */
160 	assertEqualInt(ARCHIVE_EOF, archive_read_next_header(a, &ae));
161 
162 	/* Verify archive format. */
163 	assertEqualInt(archive_filter_code(a, 0), ARCHIVE_FILTER_COMPRESS);
164 	assertEqualInt(archive_format(a), ARCHIVE_FORMAT_ISO9660_ROCKRIDGE);
165 
166 	/* Close the archive. */
167 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
168 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
169 }
170 
171