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__FBSDID("$FreeBSD: stable/11/contrib/libarchive/libarchive/test/test_read_format_zip_7075_utf8_paths.c 358088 2020-02-19 01:50:47Z mm $");
29
30#include <locale.h>
31
32static void
33verify(struct archive *a) {
34	struct archive_entry *ae;
35	const char *p;
36
37	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
38	assert((p = archive_entry_pathname_utf8(ae)) != NULL);
39	assertEqualUTF8String(p, "File 1.txt");
40
41	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
42	assert((p = archive_entry_pathname_utf8(ae)) != NULL);
43#if defined(__APPLE__)
44	/* Compare NFD string. */
45	assertEqualUTF8String(p, "File 2 - o\xCC\x88.txt");
46#else
47	/* Compare NFC string. */
48	assertEqualUTF8String(p, "File 2 - \xC3\xB6.txt");
49#endif
50
51	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
52	assert((p = archive_entry_pathname_utf8(ae)) != NULL);
53#if defined(__APPLE__)
54	/* Compare NFD string. */
55	assertEqualUTF8String(p, "File 3 - a\xCC\x88.txt");
56#else
57	/* Compare NFC string. */
58	assertEqualUTF8String(p, "File 3 - \xC3\xA4.txt");
59#endif
60
61	/* The CRC of the filename fails, so fall back to CDE. */
62	assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae));
63	assert((p = archive_entry_pathname_utf8(ae)) != NULL);
64	assertEqualUTF8String(p, "File 4 - xx.txt");
65
66	assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
67}
68
69DEFINE_TEST(test_read_format_zip_utf8_paths)
70{
71	const char *refname = "test_read_format_zip_7075_utf8_paths.zip";
72	struct archive *a;
73	char *p;
74	size_t s;
75
76	extract_reference_file(refname);
77
78	if (NULL == setlocale(LC_ALL, "en_US.UTF-8")) {
79		skipping("en_US.UTF-8 locale not available on this system.");
80		return;
81	}
82
83	/* Verify with seeking reader. */
84	assert((a = archive_read_new()) != NULL);
85	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
86	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
87	assertEqualIntA(a, ARCHIVE_OK, archive_read_open_filename(a, refname, 10240));
88	verify(a);
89	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
90	assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
91
92	/* Verify with streaming reader. */
93	p = slurpfile(&s, "%s", refname);
94	assert((a = archive_read_new()) != NULL);
95	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
96	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
97	assertEqualIntA(a, ARCHIVE_OK, read_open_memory(a, p, s, 31));
98	verify(a);
99	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
100	assertEqualIntA(a, ARCHIVE_OK, archive_free(a));
101	free(p);
102}
103