]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/libarchive/libarchive/test/test_write_format_iso9660_boot.c
MFC r299529,r299540,r299576,r299896:
[FreeBSD/stable/10.git] / contrib / libarchive / libarchive / test / test_write_format_iso9660_boot.c
1 /*-
2  * Copyright (c) 2009 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 a "bootable CD" 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     0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x26
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 boot_id[] = {
44     0x00, 0x43, 0x44, 0x30, 0x30, 0x31, 0x01, 0x45,
45     0x4c, 0x20, 0x54, 0x4f, 0x52, 0x49, 0x54, 0x4f,
46     0x20, 0x53, 0x50, 0x45, 0x43, 0x49, 0x46, 0x49,
47     0x43, 0x41, 0x54, 0x49, 0x4f, 0x4e, 0x00, 0x00,
48     0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49 };
50
51 static const unsigned char supplementary_id[] = {
52     0x02, 0x43, 0x44, 0x30, 0x30, 0x31, 0x01, 0x00
53 };
54 static const unsigned char terminator_id[] = {
55     0xff, 0x43, 0x44, 0x30, 0x30, 0x31, 0x01, 0x00
56 };
57
58 static const unsigned char boot_catalog[] = {
59     0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62     0x00, 0x00, 0x00, 0x00, 0xaa, 0x55, 0x55, 0xaa,
63     0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00,
64     0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
65     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
66     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
67 };
68
69 static const unsigned char info_table[] = {
70     0x10, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00,
71     0x00, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
73     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
74     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
75     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
77 };
78
79 static const unsigned char el_torito_signature[] = {
80     "ER\355\001\012T\207\001RRIP_1991ATHE ROCK RIDGE "
81     "INTERCHANGE PROTOCOL PROVIDES SUPPORT FOR POSIX "
82     "FILE SYSTEM SEMANTICSPLEASE CONTACT DISC PUBLISHER "
83     "FOR SPECIFICATION SOURCE.  SEE PUBLISHER IDENTIFIER "
84     "IN PRIMARY VOLUME DESCRIPTOR FOR CONTACT INFORMATION."
85 };
86
87 static char buff2[1024];
88
89 static void
90 _test_write_format_iso9660_boot(int write_info_tbl)
91 {
92         unsigned char nullb[2048];
93         struct archive *a;
94         struct archive_entry *ae;
95         unsigned char *buff;
96         size_t buffsize = 39 * 2048;
97         size_t used;
98         unsigned int i;
99
100         memset(nullb, 0, sizeof(nullb));
101         buff = malloc(buffsize);
102         assert(buff != NULL);
103
104         /* ISO9660 format: Create a new archive in memory. */
105         assert((a = archive_write_new()) != NULL);
106         assertA(0 == archive_write_set_format_iso9660(a));
107         assertA(0 == archive_write_add_filter_none(a));
108         assertA(0 == archive_write_set_option(a, NULL, "boot", "boot.img"));
109         if (write_info_tbl)
110                 assertA(0 == archive_write_set_option(a, NULL, "boot-info-table", "1"));
111         assertA(0 == archive_write_set_option(a, NULL, "pad", NULL));
112         assertA(0 == archive_write_open_memory(a, buff, buffsize, &used));
113
114         /*
115          * "boot.img" has a bunch of attributes and 10K bytes of null data.
116          */
117         assert((ae = archive_entry_new()) != NULL);
118         archive_entry_set_atime(ae, 2, 20);
119         archive_entry_set_birthtime(ae, 3, 30);
120         archive_entry_set_ctime(ae, 4, 40);
121         archive_entry_set_mtime(ae, 5, 50);
122         archive_entry_copy_pathname(ae, "boot.img");
123         archive_entry_set_mode(ae, S_IFREG | 0755);
124         archive_entry_set_nlink(ae, 1);
125         archive_entry_set_size(ae, 10*1024);
126         assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
127         archive_entry_free(ae);
128         for (i = 0; i < 10; i++)
129                 assertEqualIntA(a, 1024, archive_write_data(a, nullb, 1024));
130
131         /* Close out the archive. */
132         assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
133         assertEqualIntA(a, ARCHIVE_OK, archive_write_free(a));
134
135         assert(used == 2048 * 38);
136         /* Check System Area. */
137         for (i = 0; i < 2048 * 16; i += 2048) {
138                 assertEqualMem(buff+i, nullb, 2048);
139         }
140
141         /* Primary Volume. */
142         failure("Primary Volume Descriptor should be in 16 Logical Sector.");
143         assertEqualMem(buff+2048*16, primary_id, 8);
144         assertEqualMem(buff+2048*16+0x28,
145             "CDROM                           ", 32);
146         assertEqualMem(buff+2048*16+0x50, volumesize, 8);
147
148         /* Boot Volume. */
149         failure("Boot Volume Descriptor should be in 17 Logical Sector.");
150         assertEqualMem(buff+2048*17, boot_id, sizeof(boot_id));
151         for (i = 0x27; i <= 0x46; i++) {
152                 failure("Unused area must be all nulls.");
153                 assert(buff[2048*17+i] == 0);
154         }
155         /* First sector of Boot Catalog. */
156         assert(buff[2048*17+0x47] == 0x20);
157         assert(buff[2048*17+0x48] == 0x00);
158         assert(buff[2048*17+0x49] == 0x00);
159         assert(buff[2048*17+0x4a] == 0x00);
160         for (i = 0x4a; i <= 0x7ff; i++) {
161                 failure("Unused area must be all nulls.");
162                 assert(buff[2048*17+i] == 0);
163         }
164
165         /* Supplementary Volume. */
166         failure("Supplementary Volume(Joliet) Descriptor "
167             "should be in 18 Logical Sector.");
168         assertEqualMem(buff+2048*18, supplementary_id, 8);
169         assertEqualMem(buff+2048*18+0x28, volumeidu16, 32);
170         assertEqualMem(buff+2048*18+0x50, volumesize, 8);
171         failure("Date and Time of Primary Volume and "
172             "Date and Time of Supplementary Volume "
173             "must be the same.");
174         assertEqualMem(buff+2048*16+0x32d, buff+2048*18+0x32d, 0x44);
175
176         /* Terminator. */
177         failure("Volume Descriptor Set Terminator "
178             "should be in 19 Logical Sector.");
179         assertEqualMem(buff+2048*19, terminator_id, 8);
180         for (i = 8; i < 2048; i++) {
181                 failure("Body of Volume Descriptor Set Terminator "
182                     "should be all nulls.");
183                 assert(buff[2048*19+i] == 0);
184         }
185
186         /* Check signature of El-Torito. */
187         assertEqualMem(buff+2048*31, el_torito_signature, 237);
188         assertEqualMem(buff+2048*31+237, nullb, 2048-237);
189
190         /* Check contents of "boot.catalog". */
191         assertEqualMem(buff+2048*32, boot_catalog, 64);
192         assertEqualMem(buff+2048*32+64, nullb, 2048-64);
193
194         /* Check contents of "boot.img". */
195         if (write_info_tbl) {
196                 assertEqualMem(buff+2048*33, nullb, 8);
197                 assertEqualMem(buff+2048*33+8, info_table, 56);
198                 assertEqualMem(buff+2048*33+64, nullb, 2048-64);
199         } else {
200                 assertEqualMem(buff+2048*33, nullb, 2048);
201         }
202         for (i = 2048*34; i < 2048*38; i += 2048) {
203                 assertEqualMem(buff+i, nullb, 2048);
204         }
205
206         /*
207          * Read ISO image.
208          */
209         assert((a = archive_read_new()) != NULL);
210         assertEqualIntA(a, 0, archive_read_support_format_all(a));
211         assertEqualIntA(a, 0, archive_read_support_filter_all(a));
212         assertEqualIntA(a, 0, archive_read_open_memory(a, buff, used));
213
214         /*
215          * Read Root Directory
216          * Root Directory entry must be in ISO image.
217          */
218         assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
219         assertEqualInt(archive_entry_atime(ae), archive_entry_ctime(ae));
220         assertEqualInt(archive_entry_atime(ae), archive_entry_mtime(ae));
221         assertEqualString(".", archive_entry_pathname(ae));
222         assert((S_IFDIR | 0555) == archive_entry_mode(ae));
223         assertEqualInt(2048, archive_entry_size(ae));
224
225         /*
226          * Read "boot.catalog".
227          */
228         assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
229         assertEqualString("boot.catalog", archive_entry_pathname(ae));
230 #if !defined(_WIN32) && !defined(__CYGWIN__)
231         assert((S_IFREG | 0444) == archive_entry_mode(ae));
232 #else
233         /* On Windows and CYGWIN, always set all exec bit ON by default. */ 
234         assert((S_IFREG | 0555) == archive_entry_mode(ae));
235 #endif
236         assertEqualInt(1, archive_entry_nlink(ae));
237         assertEqualInt(2*1024, archive_entry_size(ae));
238         assertEqualIntA(a, 1024, archive_read_data(a, buff2, 1024));
239         assertEqualMem(buff2, boot_catalog, 64);
240
241         /*
242          * Read "boot.img".
243          */
244         assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
245         assertEqualInt(2, archive_entry_atime(ae));
246         assertEqualInt(3, archive_entry_birthtime(ae));
247         assertEqualInt(4, archive_entry_ctime(ae));
248         assertEqualInt(5, archive_entry_mtime(ae));
249         assertEqualString("boot.img", archive_entry_pathname(ae));
250         assert((S_IFREG | 0555) == archive_entry_mode(ae));
251         assertEqualInt(1, archive_entry_nlink(ae));
252         assertEqualInt(10*1024, archive_entry_size(ae));
253         assertEqualIntA(a, 1024, archive_read_data(a, buff2, 1024));
254         if (write_info_tbl) {
255                 assertEqualMem(buff2, nullb, 8);
256                 assertEqualMem(buff2+8, info_table, 56);
257                 assertEqualMem(buff2+64, nullb, 1024-64);
258         } else
259                 assertEqualMem(buff2, nullb, 1024);
260
261         /*
262          * Verify the end of the archive.
263          */
264         assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
265         assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
266         assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
267
268         free(buff);
269 }
270
271 DEFINE_TEST(test_write_format_iso9660_boot)
272 {
273         _test_write_format_iso9660_boot(0);
274         /* Use 'boot-info-table' option. */
275         _test_write_format_iso9660_boot(1);
276 }