]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/libarchive/libarchive/test/test_read_format_tar_filename.c
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.git] / contrib / libarchive / libarchive / test / test_read_format_tar_filename.c
1 /*-
2  * Copyright (c) 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 __FBSDID("$FreeBSD");
27
28 #include <locale.h>
29
30 /*
31  * The sample tar file was made in LANG=KOI8-R and it contains two
32  * files the charset of which are different.
33  * - the filename of first file is stored in BINARY mode.
34  * - the filename of second file is stored in UTF-8.
35  *
36  * Whenever hdrcharset option is specified, we will correctly read the
37  * filename of second file, which is stored in UTF-8 by default.
38  */
39
40 static void
41 test_read_format_tar_filename_KOI8R_CP866(const char *refname)
42 {
43         struct archive *a;
44         struct archive_entry *ae;
45
46         /*
47         * Read filename in ru_RU.CP866 with "hdrcharset=KOI8-R" option.
48         * We should correctly read two filenames.
49         */
50         if (NULL == setlocale(LC_ALL, "Russian_Russia.866") &&
51             NULL == setlocale(LC_ALL, "ru_RU.CP866")) {
52                 skipping("ru_RU.CP866 locale not available on this system.");
53                 return;
54         }
55
56         /* Test if the platform can convert from UTF-8. */
57         assert((a = archive_read_new()) != NULL);
58         assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_tar(a));
59         if (ARCHIVE_OK != archive_read_set_options(a, "hdrcharset=UTF-8")) {
60                 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
61                 skipping("This system cannot convert character-set"
62                     " from UTF-8 to CP866.");
63                 return;
64         }
65         assertEqualInt(ARCHIVE_OK, archive_read_free(a));
66
67         assert((a = archive_read_new()) != NULL);
68         assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
69         assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
70         if (ARCHIVE_OK != archive_read_set_options(a, "hdrcharset=KOI8-R")) {
71                 skipping("This system cannot convert character-set"
72                     " from KOI8-R to CP866.");
73                 goto next_test;
74         }
75         assertEqualIntA(a, ARCHIVE_OK,
76             archive_read_open_filename(a, refname, 10240));
77
78         /* Verify regular first file. */
79         assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
80         assertEqualString("\x8f\x90\x88\x82\x85\x92",
81             archive_entry_pathname(ae));
82         assertEqualInt(6, archive_entry_size(ae));
83
84         /* Verify regular second file. */
85         assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
86         assertEqualString("\xaf\xe0\xa8\xa2\xa5\xe2",
87             archive_entry_pathname(ae));
88         assertEqualInt(6, archive_entry_size(ae));
89
90
91         /* End of archive. */
92         assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
93
94         /* Verify archive format. */
95         assertEqualIntA(a, ARCHIVE_FILTER_COMPRESS, archive_filter_code(a, 0));
96         assertEqualIntA(a, ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE,
97             archive_format(a));
98
99         /* Close the archive. */
100         assertEqualInt(ARCHIVE_OK, archive_read_close(a));
101 next_test:
102         assertEqualInt(ARCHIVE_OK, archive_read_free(a));
103
104
105         /*
106          * Read filename in ru_RU.CP866 without "hdrcharset=KOI8-R" option.
107          * The filename we can properly read is only second file.
108          */
109
110         assert((a = archive_read_new()) != NULL);
111         assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
112         assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
113         assertEqualIntA(a, ARCHIVE_OK,
114             archive_read_open_filename(a, refname, 10240));
115
116         /*
117          * Verify regular first file.
118          * The filename is not translated to CP866 because hdrcharset
119          * attribute is BINARY and there is not way to know its charset.
120          */
121         assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
122         /* A filename is in KOI8-R. */
123         assertEqualString("\xf0\xf2\xe9\xf7\xe5\xf4",
124             archive_entry_pathname(ae));
125         assertEqualInt(6, archive_entry_size(ae));
126
127         /*
128          * Verify regular second file.
129          * The filename is translated from UTF-8 to CP866
130          */
131         assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
132         assertEqualString("\xaf\xe0\xa8\xa2\xa5\xe2",
133             archive_entry_pathname(ae));
134         assertEqualInt(6, archive_entry_size(ae));
135
136
137         /* End of archive. */
138         assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
139
140         /* Verify archive format. */
141         assertEqualIntA(a, ARCHIVE_FILTER_COMPRESS, archive_filter_code(a, 0));
142         assertEqualIntA(a, ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE,
143             archive_format(a));
144
145         /* Close the archive. */
146         assertEqualInt(ARCHIVE_OK, archive_read_close(a));
147         assertEqualInt(ARCHIVE_OK, archive_read_free(a));
148 }
149
150 static void
151 test_read_format_tar_filename_KOI8R_UTF8(const char *refname)
152 {
153         struct archive *a;
154         struct archive_entry *ae;
155
156         /*
157          * Read filename in en_US.UTF-8 with "hdrcharset=KOI8-R" option.
158          * We should correctly read two filenames.
159          */
160         if (NULL == setlocale(LC_ALL, "en_US.UTF-8")) {
161                 skipping("en_US.UTF-8 locale not available on this system.");
162                 return;
163         }
164
165         assert((a = archive_read_new()) != NULL);
166         assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
167         assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
168         if (ARCHIVE_OK != archive_read_set_options(a, "hdrcharset=KOI8-R")) {
169                 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
170                 skipping("This system cannot convert character-set"
171                     " from KOI8-R to UTF-8.");
172                 return;
173         }
174         assertEqualIntA(a, ARCHIVE_OK,
175             archive_read_open_filename(a, refname, 10240));
176
177         /* Verify regular file. */
178         assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
179         assertEqualString("\xd0\x9f\xd0\xa0\xd0\x98\xd0\x92\xd0\x95\xd0\xa2",
180             archive_entry_pathname(ae));
181         assertEqualInt(6, archive_entry_size(ae));
182
183         /* Verify regular file. */
184         assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
185         assertEqualString("\xd0\xbf\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82",
186             archive_entry_pathname(ae));
187         assertEqualInt(6, archive_entry_size(ae));
188
189
190         /* End of archive. */
191         assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
192
193         /* Verify archive format. */
194         assertEqualIntA(a, ARCHIVE_FILTER_COMPRESS, archive_filter_code(a, 0));
195         assertEqualIntA(a, ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE,
196             archive_format(a));
197
198         /* Close the archive. */
199         assertEqualInt(ARCHIVE_OK, archive_read_close(a));
200         assertEqualInt(ARCHIVE_OK, archive_read_free(a));
201
202         /*
203          * Read filename in en_US.UTF-8 without "hdrcharset=KOI8-R" option.
204          * The filename we can properly read is only second file.
205          */
206
207         assert((a = archive_read_new()) != NULL);
208         assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
209         assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
210         assertEqualIntA(a, ARCHIVE_OK,
211             archive_read_open_filename(a, refname, 10240));
212
213         /*
214          * Verify regular first file.
215          * The filename is not translated to UTF-8 because hdrcharset
216          * attribute is BINARY and there is not way to know its charset.
217          */
218         assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
219         /* A filename is in KOI8-R. */
220         assertEqualString("\xf0\xf2\xe9\xf7\xe5\xf4",
221             archive_entry_pathname(ae));
222         assertEqualInt(6, archive_entry_size(ae));
223
224         /*
225          * Verify regular second file.
226          */
227         assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
228         assertEqualString("\xd0\xbf\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82",
229             archive_entry_pathname(ae));
230         assertEqualInt(6, archive_entry_size(ae));
231
232
233         /* End of archive. */
234         assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
235
236         /* Verify archive format. */
237         assertEqualIntA(a, ARCHIVE_FILTER_COMPRESS, archive_filter_code(a, 0));
238         assertEqualIntA(a, ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE,
239             archive_format(a));
240
241         /* Close the archive. */
242         assertEqualInt(ARCHIVE_OK, archive_read_close(a));
243         assertEqualInt(ARCHIVE_OK, archive_read_free(a));
244 }
245
246 static void
247 test_read_format_tar_filename_KOI8R_CP1251(const char *refname)
248 {
249         struct archive *a;
250         struct archive_entry *ae;
251
252         /*
253         * Read filename in CP1251 with "hdrcharset=KOI8-R" option.
254         * We should correctly read two filenames.
255         */
256         if (NULL == setlocale(LC_ALL, "Russian_Russia") &&
257             NULL == setlocale(LC_ALL, "ru_RU.CP1251")) {
258                 skipping("CP1251 locale not available on this system.");
259                 return;
260         }
261
262         /* Test if the platform can convert from UTF-8. */
263         assert((a = archive_read_new()) != NULL);
264         assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_tar(a));
265         if (ARCHIVE_OK != archive_read_set_options(a, "hdrcharset=UTF-8")) {
266                 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
267                 skipping("This system cannot convert character-set"
268                     " from UTF-8 to CP1251.");
269                 return;
270         }
271         assertEqualInt(ARCHIVE_OK, archive_read_free(a));
272
273         assert((a = archive_read_new()) != NULL);
274         assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
275         assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
276         if (ARCHIVE_OK != archive_read_set_options(a, "hdrcharset=KOI8-R")) {
277                 skipping("This system cannot convert character-set"
278                     " from KOI8-R to CP1251.");
279                 goto next_test;
280         }
281         assertEqualIntA(a, ARCHIVE_OK,
282             archive_read_open_filename(a, refname, 10240));
283
284         /* Verify regular first file. */
285         assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
286         assertEqualString("\xcf\xd0\xc8\xc2\xc5\xd2",
287             archive_entry_pathname(ae));
288         assertEqualInt(6, archive_entry_size(ae));
289
290         /* Verify regular second file. */
291         assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
292         assertEqualString("\xef\xf0\xe8\xe2\xe5\xf2",
293             archive_entry_pathname(ae));
294         assertEqualInt(6, archive_entry_size(ae));
295
296
297         /* End of archive. */
298         assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
299
300         /* Verify archive format. */
301         assertEqualIntA(a, ARCHIVE_FILTER_COMPRESS, archive_filter_code(a, 0));
302         assertEqualIntA(a, ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE,
303             archive_format(a));
304
305         /* Close the archive. */
306         assertEqualInt(ARCHIVE_OK, archive_read_close(a));
307 next_test:
308         assertEqualInt(ARCHIVE_OK, archive_read_free(a));
309
310         /*
311          * Read filename in CP1251 without "hdrcharset=KOI8-R" option.
312          * The filename we can properly read is only second file.
313          */
314
315         assert((a = archive_read_new()) != NULL);
316         assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
317         assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
318         assertEqualIntA(a, ARCHIVE_OK,
319             archive_read_open_filename(a, refname, 10240));
320
321         /*
322          * Verify regular first file.
323          * The filename is not translated to CP1251 because hdrcharset
324          * attribute is BINARY and there is not way to know its charset.
325          */
326         assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
327         /* A filename is in KOI8-R. */
328         assertEqualString("\xf0\xf2\xe9\xf7\xe5\xf4",
329             archive_entry_pathname(ae));
330         assertEqualInt(6, archive_entry_size(ae));
331
332         /*
333          * Verify regular second file.
334          */
335         assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
336         assertEqualString("\xef\xf0\xe8\xe2\xe5\xf2",
337             archive_entry_pathname(ae));
338         assertEqualInt(6, archive_entry_size(ae));
339
340
341         /* End of archive. */
342         assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
343
344         /* Verify archive format. */
345         assertEqualIntA(a, ARCHIVE_FILTER_COMPRESS, archive_filter_code(a, 0));
346         assertEqualIntA(a, ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE,
347             archive_format(a));
348
349         /* Close the archive. */
350         assertEqualInt(ARCHIVE_OK, archive_read_close(a));
351         assertEqualInt(ARCHIVE_OK, archive_read_free(a));
352 }
353
354
355 DEFINE_TEST(test_read_format_tar_filename)
356 {
357         const char *refname = "test_read_format_tar_filename_koi8r.tar.Z";
358
359         extract_reference_file(refname);
360         test_read_format_tar_filename_KOI8R_CP866(refname);
361         test_read_format_tar_filename_KOI8R_UTF8(refname);
362         test_read_format_tar_filename_KOI8R_CP1251(refname);
363 }