]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/libarchive/libarchive/test/test_write_format_iso9660_empty.c
Upgrade to 1.6.16
[FreeBSD/FreeBSD.git] / contrib / libarchive / libarchive / test / test_write_format_iso9660_empty.c
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
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
62         /* ISO9660 format: Create a new archive in memory. */
63         assert((a = archive_write_new()) != NULL);
64         assertA(0 == archive_write_set_format_iso9660(a));
65         assertA(0 == archive_write_set_compression_none(a));
66         assertA(0 == archive_write_set_bytes_per_block(a, 1));
67         assertA(0 == archive_write_set_bytes_in_last_block(a, 1));
68         assertA(0 == archive_write_open_memory(a, buff, buffsize, &used));
69
70         /* Add "." entry which must be ignored. */ 
71         assert((ae = archive_entry_new()) != NULL);
72         archive_entry_set_atime(ae, 2, 0);
73         archive_entry_set_ctime(ae, 4, 0);
74         archive_entry_set_mtime(ae, 5, 0);
75         archive_entry_copy_pathname(ae, ".");
76         archive_entry_set_mode(ae, S_IFDIR | 0755);
77         assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
78         archive_entry_free(ae);
79
80         /* Add ".." entry which must be ignored. */ 
81         assert((ae = archive_entry_new()) != NULL);
82         archive_entry_set_atime(ae, 2, 0);
83         archive_entry_set_ctime(ae, 4, 0);
84         archive_entry_set_mtime(ae, 5, 0);
85         archive_entry_copy_pathname(ae, "..");
86         archive_entry_set_mode(ae, S_IFDIR | 0755);
87         assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
88         archive_entry_free(ae);
89
90         /* Add "/" entry which must be ignored. */ 
91         assert((ae = archive_entry_new()) != NULL);
92         archive_entry_set_atime(ae, 2, 0);
93         archive_entry_set_ctime(ae, 4, 0);
94         archive_entry_set_mtime(ae, 5, 0);
95         archive_entry_copy_pathname(ae, "/");
96         archive_entry_set_mode(ae, S_IFDIR | 0755);
97         assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
98         archive_entry_free(ae);
99
100         /* Add "../" entry which must be ignored. */ 
101         assert((ae = archive_entry_new()) != NULL);
102         archive_entry_set_atime(ae, 2, 0);
103         archive_entry_set_ctime(ae, 4, 0);
104         archive_entry_set_mtime(ae, 5, 0);
105         archive_entry_copy_pathname(ae, "../");
106         archive_entry_set_mode(ae, S_IFDIR | 0755);
107         assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
108         archive_entry_free(ae);
109
110         /* Add "../../." entry which must be ignored. */ 
111         assert((ae = archive_entry_new()) != NULL);
112         archive_entry_set_atime(ae, 2, 0);
113         archive_entry_set_ctime(ae, 4, 0);
114         archive_entry_set_mtime(ae, 5, 0);
115         archive_entry_copy_pathname(ae, "../../.");
116         archive_entry_set_mode(ae, S_IFDIR | 0755);
117         assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
118         archive_entry_free(ae);
119
120         /* Add "..//.././" entry which must be ignored. */ 
121         assert((ae = archive_entry_new()) != NULL);
122         archive_entry_set_atime(ae, 2, 0);
123         archive_entry_set_ctime(ae, 4, 0);
124         archive_entry_set_mtime(ae, 5, 0);
125         archive_entry_copy_pathname(ae, "..//.././");
126         archive_entry_set_mode(ae, S_IFDIR | 0755);
127         assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
128         archive_entry_free(ae);
129
130         /* Close out the archive. */
131         assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
132         assertEqualIntA(a, ARCHIVE_OK, archive_write_free(a));
133
134         assert(used == 2048 * 181);
135         /* Check System Area. */
136         for (i = 0; i < 2048 * 16; i++) {
137                 failure("System Area should be all nulls.");
138                 assert(buff[i] == 0);
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         /* Supplementary Volume. */
149         failure("Supplementary Volume(Joliet) Descriptor "
150             "should be in 17 Logical Sector.");
151         assertEqualMem(buff+2048*17, supplementary_id, 8);
152         assertEqualMem(buff+2048*17+0x28, volumeidu16, 32);
153         assertEqualMem(buff+2048*17+0x50, volumesize, 8);
154         failure("Date and Time of Primary Volume and "
155             "Date and Time of Supplementary Volume "
156             "must be the same.");
157         assertEqualMem(buff+2048*16+0x32d, buff+2048*17+0x32d, 0x44);
158
159         /* Terminator. */
160         failure("Volume Descriptor Set Terminator "
161             "should be in 18 Logical Sector.");
162         assertEqualMem(buff+2048*18, terminator_id, 8);
163         for (i = 8; i < 2048; i++) {
164                 failure("Body of Volume Descriptor Set Terminator "
165                     "should be all nulls.");
166                 assert(buff[2048*18+i] == 0);
167         }
168
169         /* Padding data. */
170         for (i = 0; i < 2048*150; i++) {
171                 failure("Padding data should be all nulls.");
172                 assert(buff[2048*31+i] == 0);
173         }
174
175         /*
176          * Read ISO image.
177          */
178         assert((a = archive_read_new()) != NULL);
179         assertEqualIntA(a, 0, archive_read_support_format_all(a));
180         assertEqualIntA(a, 0, archive_read_support_filter_all(a));
181         assertEqualIntA(a, 0, archive_read_open_memory(a, buff, used));
182
183         /*
184          * Read Root Directory
185          * Root Directory entry must be in ISO image.
186          */
187         assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
188         assertEqualInt(archive_entry_atime(ae), archive_entry_ctime(ae));
189         assertEqualInt(archive_entry_atime(ae), archive_entry_mtime(ae));
190         assertEqualString(".", archive_entry_pathname(ae));
191         assert((S_IFDIR | 0555) == archive_entry_mode(ae));
192         assertEqualInt(2048, archive_entry_size(ae));
193
194         /*
195          * Verify the end of the archive.
196          */
197         assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
198         assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
199         assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
200
201         free(buff);
202 }