xref: /freebsd/contrib/libarchive/libarchive/test/test_write_format_iso9660_empty.c (revision 552311f4bb98c81b1b9e0e81d74e0262fc12110b)
1 /*-
2  * Copyright (c) 2009-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 /*
28  * Check that an "empty" ISO 9660 image is correctly created.
29  */
30 
31 static const unsigned char primary_id[] = {
32     0x01, 0x43, 0x44, 0x30, 0x30, 0x31, 0x01, 0x00
33 };
34 static const unsigned char volumesize[] = {
35     0xb5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb5
36 };
37 static const unsigned char volumeidu16[] = {
38     0x00, 0x43, 0x00, 0x44, 0x00, 0x52, 0x00, 0x4f,
39     0x00, 0x4d, 0x00, 0x20, 0x00, 0x20, 0x00, 0x20,
40     0x00, 0x20, 0x00, 0x20, 0x00, 0x20, 0x00, 0x20,
41     0x00, 0x20, 0x00, 0x20, 0x00, 0x20, 0x00, 0x20
42 };
43 static const unsigned char supplementary_id[] = {
44     0x02, 0x43, 0x44, 0x30, 0x30, 0x31, 0x01, 0x00
45 };
46 static const unsigned char terminator_id[] = {
47     0xff, 0x43, 0x44, 0x30, 0x30, 0x31, 0x01, 0x00
48 };
49 
DEFINE_TEST(test_write_format_iso9660_empty)50 DEFINE_TEST(test_write_format_iso9660_empty)
51 {
52 	struct archive *a;
53 	struct archive_entry *ae;
54 	unsigned char *buff;
55 	size_t buffsize = 190 * 2048;
56 	size_t used;
57 	unsigned int i;
58 
59 	buff = malloc(buffsize);
60 	assert(buff != NULL);
61 	if (buff == NULL)
62 		return;
63 
64 	/* ISO9660 format: Create a new archive in memory. */
65 	assert((a = archive_write_new()) != NULL);
66 	assertA(0 == archive_write_set_format_iso9660(a));
67 	assertA(0 == archive_write_add_filter_none(a));
68 	assertA(0 == archive_write_set_bytes_per_block(a, 1));
69 	assertA(0 == archive_write_set_bytes_in_last_block(a, 1));
70 	assertA(0 == archive_write_open_memory(a, buff, buffsize, &used));
71 
72 	/* Add "." entry which must be ignored. */
73 	assert((ae = archive_entry_new()) != NULL);
74 	archive_entry_set_atime(ae, 2, 0);
75 	archive_entry_set_ctime(ae, 4, 0);
76 	archive_entry_set_mtime(ae, 5, 0);
77 	archive_entry_copy_pathname(ae, ".");
78 	archive_entry_set_mode(ae, S_IFDIR | 0755);
79 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
80 	archive_entry_free(ae);
81 
82 	/* Add ".." entry which must be ignored. */
83 	assert((ae = archive_entry_new()) != NULL);
84 	archive_entry_set_atime(ae, 2, 0);
85 	archive_entry_set_ctime(ae, 4, 0);
86 	archive_entry_set_mtime(ae, 5, 0);
87 	archive_entry_copy_pathname(ae, "..");
88 	archive_entry_set_mode(ae, S_IFDIR | 0755);
89 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
90 	archive_entry_free(ae);
91 
92 	/* Add "/" entry which must be ignored. */
93 	assert((ae = archive_entry_new()) != NULL);
94 	archive_entry_set_atime(ae, 2, 0);
95 	archive_entry_set_ctime(ae, 4, 0);
96 	archive_entry_set_mtime(ae, 5, 0);
97 	archive_entry_copy_pathname(ae, "/");
98 	archive_entry_set_mode(ae, S_IFDIR | 0755);
99 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
100 	archive_entry_free(ae);
101 
102 	/* Add "../" entry which must be ignored. */
103 	assert((ae = archive_entry_new()) != NULL);
104 	archive_entry_set_atime(ae, 2, 0);
105 	archive_entry_set_ctime(ae, 4, 0);
106 	archive_entry_set_mtime(ae, 5, 0);
107 	archive_entry_copy_pathname(ae, "../");
108 	archive_entry_set_mode(ae, S_IFDIR | 0755);
109 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
110 	archive_entry_free(ae);
111 
112 	/* Add "../../." entry which must be ignored. */
113 	assert((ae = archive_entry_new()) != NULL);
114 	archive_entry_set_atime(ae, 2, 0);
115 	archive_entry_set_ctime(ae, 4, 0);
116 	archive_entry_set_mtime(ae, 5, 0);
117 	archive_entry_copy_pathname(ae, "../../.");
118 	archive_entry_set_mode(ae, S_IFDIR | 0755);
119 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
120 	archive_entry_free(ae);
121 
122 	/* Add "..//.././" entry which must be ignored. */
123 	assert((ae = archive_entry_new()) != NULL);
124 	archive_entry_set_atime(ae, 2, 0);
125 	archive_entry_set_ctime(ae, 4, 0);
126 	archive_entry_set_mtime(ae, 5, 0);
127 	archive_entry_copy_pathname(ae, "..//.././");
128 	archive_entry_set_mode(ae, S_IFDIR | 0755);
129 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
130 	archive_entry_free(ae);
131 
132 	/* Close out the archive. */
133 	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
134 	assertEqualIntA(a, ARCHIVE_OK, archive_write_free(a));
135 
136 	assert(used == 2048 * 181);
137 	/* Check System Area. */
138 	for (i = 0; i < 2048 * 16; i++) {
139 		failure("System Area should be all nulls.");
140 		assert(buff[i] == 0);
141 	}
142 
143 	/* Primary Volume. */
144 	failure("Primary Volume Descriptor should be in 16 Logical Sector.");
145 	assertEqualMem(buff+2048*16, primary_id, 8);
146 	assertEqualMem(buff+2048*16+0x28,
147 	    "CDROM                           ", 32);
148 	assertEqualMem(buff+2048*16+0x50, volumesize, 8);
149 
150 	/* Supplementary Volume. */
151 	failure("Supplementary Volume(Joliet) Descriptor "
152 	    "should be in 17 Logical Sector.");
153 	assertEqualMem(buff+2048*17, supplementary_id, 8);
154 	assertEqualMem(buff+2048*17+0x28, volumeidu16, 32);
155 	assertEqualMem(buff+2048*17+0x50, volumesize, 8);
156 	failure("Date and Time of Primary Volume and "
157 	    "Date and Time of Supplementary Volume "
158 	    "must be the same.");
159 	assertEqualMem(buff+2048*16+0x32d, buff+2048*17+0x32d, 0x44);
160 
161 	/* Terminator. */
162 	failure("Volume Descriptor Set Terminator "
163 	    "should be in 18 Logical Sector.");
164 	assertEqualMem(buff+2048*18, terminator_id, 8);
165 	for (i = 8; i < 2048; i++) {
166 		failure("Body of Volume Descriptor Set Terminator "
167 		    "should be all nulls.");
168 		assert(buff[2048*18+i] == 0);
169 	}
170 
171 	/* Padding data. */
172 	for (i = 0; i < 2048*150; i++) {
173 		failure("Padding data should be all nulls.");
174 		assert(buff[2048*31+i] == 0);
175 	}
176 
177 	/*
178 	 * Read ISO image.
179 	 */
180 	assert((a = archive_read_new()) != NULL);
181 	assertEqualIntA(a, 0, archive_read_support_format_all(a));
182 	assertEqualIntA(a, 0, archive_read_support_filter_all(a));
183 	assertEqualIntA(a, 0, archive_read_open_memory(a, buff, used));
184 
185 	/*
186 	 * Read Root Directory
187 	 * Root Directory entry must be in ISO image.
188 	 */
189 	assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
190 	assertEqualInt(archive_entry_atime(ae), archive_entry_ctime(ae));
191 	assertEqualInt(archive_entry_atime(ae), archive_entry_mtime(ae));
192 	assertEqualString(".", archive_entry_pathname(ae));
193 	assert((S_IFDIR | 0555) == archive_entry_mode(ae));
194 	assertEqualInt(2048, archive_entry_size(ae));
195 
196 	/*
197 	 * Verify the end of the archive.
198 	 */
199 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
200 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
201 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
202 
203 	free(buff);
204 }
205