xref: /freebsd/contrib/libarchive/libarchive/test/test_write_format_zip_windows_path.c (revision bd66c1b43e33540205dbc1187c2f2a15c58b57ba)
1 /*-
2  * Copyright (c) 2024 Yang Zhou
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  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "test.h"
28 
29 static void
test_with_hdrcharset(const char * charset)30 test_with_hdrcharset(const char *charset)
31 {
32 	static const char *raw_path = "dir_stored\\dir1/file";
33 	static const char *replaced = "dir_stored/dir1/file";
34 	struct archive *a;
35 	size_t used;
36 	size_t buffsize = 1000000;
37 	char *buff;
38 
39 	buff = malloc(buffsize);
40 
41 	/* Create a new archive in memory. */
42 	assert((a = archive_write_new()) != NULL);
43 	assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a));
44 	assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
45 	if (charset != NULL) {
46 		assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_option(a, "zip", "hdrcharset", charset));
47 	}
48 	assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, buffsize, &used));
49 
50 	/*
51 	 * Write a file with mixed '/' and '\'
52 	 */
53 	struct archive_entry *ae;
54 	assert((ae = archive_entry_new()) != NULL);
55 	archive_entry_set_mtime(ae, 1, 10);
56 	archive_entry_copy_pathname(ae, raw_path);
57 	archive_entry_set_mode(ae, AE_IFREG | 0755);
58 	archive_entry_set_size(ae, 0);
59 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
60 	archive_entry_free(ae);
61 
62 	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
63 	assertEqualIntA(a, ARCHIVE_OK, archive_write_free(a));
64 	dumpfile("constructed.zip", buff, used);
65 
66 	/*
67 	 * Check if the generated archive contains and only contains expected path.
68 	 * Intentionally avoid using `archive_read_XXX` functions because it silently replaces '\' with '/',
69 	 * making it difficult to get the exact path written in the archive.
70 	 */
71 #if defined(_WIN32) && !defined(__CYGWIN__)
72 	const char *expected = replaced;
73 	const char *unexpected = raw_path;
74 #else
75 	const char *expected = raw_path;
76 	const char *unexpected = replaced;
77 #endif
78 	int expected_found = 0;
79 	int unexpected_found = 0;
80 	size_t len = strlen(raw_path);
81 	for (char *ptr = buff; ptr < (buff + used - len); ptr++) {
82 		if (memcmp(ptr, expected, len) == 0)
83 			++expected_found;
84 		if (memcmp(ptr, unexpected, len) == 0)
85 			++unexpected_found;
86 	}
87 	failure("should find expected path in both local and central header (charset=%s)", charset);
88 	assertEqualInt(2, expected_found);
89 	failure("should not find unexpected path in anywhere (charset=%s)", charset);
90 	assertEqualInt(0, unexpected_found);
91 }
92 
DEFINE_TEST(test_write_format_zip_windows_path)93 DEFINE_TEST(test_write_format_zip_windows_path)
94 {
95 	test_with_hdrcharset(NULL);
96 #if defined(_WIN32) && !defined(__CYGWIN__) || HAVE_ICONV
97 	test_with_hdrcharset("ISO-8859-1");
98 	test_with_hdrcharset("UTF-8");
99 #endif
100 }
101