1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * Copyright (c) 2011 Michihiro NAKAJIMA
4  * Copyright (c) 2019 Mike Frysinger
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 #include "test.h"
28 
29 #include <locale.h>
30 
31 static void
32 verify(struct archive *a) {
33 	struct archive_entry *ae;
34 	const char *p;
35 
36 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
37 	assert((p = archive_entry_pathname_utf8(ae)) != NULL);
38 	assertEqualUTF8String(p, "File 1.txt");
39 
40 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
41 	assert((p = archive_entry_pathname_utf8(ae)) != NULL);
42 #if defined(__APPLE__)
43 	/* Compare NFD string. */
44 	assertEqualUTF8String(p, "File 2 - o\xCC\x88.txt");
45 #else
46 	/* Compare NFC string. */
47 	assertEqualUTF8String(p, "File 2 - \xC3\xB6.txt");
48 #endif
49 
50 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
51 	assert((p = archive_entry_pathname_utf8(ae)) != NULL);
52 #if defined(__APPLE__)
53 	/* Compare NFD string. */
54 	assertEqualUTF8String(p, "File 3 - a\xCC\x88.txt");
55 #else
56 	/* Compare NFC string. */
57 	assertEqualUTF8String(p, "File 3 - \xC3\xA4.txt");
58 #endif
59 
60 	/* The CRC of the filename fails, so fall back to CDE. */
61 	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
62 	assert((p = archive_entry_pathname_utf8(ae)) != NULL);
63 	assertEqualUTF8String(p, "File 4 - xx.txt");
64 
65 	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
66 }
67 
68 DEFINE_TEST(test_read_format_zip_utf8_paths)
69 {
70 	const char *refname = "test_read_format_zip_7075_utf8_paths.zip";
71 	struct archive *a;
72 	char *p;
73 	size_t s;
74 
75 	extract_reference_file(refname);
76 
77 	if (NULL == setlocale(LC_ALL, "en_US.UTF-8")) {
78 		skipping("en_US.UTF-8 locale not available on this system.");
79 		return;
80 	}
81 
82 	/* Verify with seeking reader. */
83 	assert((a = archive_read_new()) != NULL);
84 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
85 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
86 	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 10240));
87 	verify(a);
88 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
89 	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
90 
91 	/* Verify with streaming reader. */
92 	p = slurpfile(&s, "%s", refname);
93 	assert((a = archive_read_new()) != NULL);
94 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
95 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
96 	assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, p, s, 31));
97 	verify(a);
98 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
99 	assertEqualIntA(a, ARCHIVE_OK, archive_free(a));
100 	free(p);
101 }
102