xref: /freebsd/contrib/libarchive/libarchive/test/test_write_format_tar_empty_dirname.c (revision 185becb1e1bd2657c156f78aeb52edac05ba5fb5)
1 /*-
2  * Copyright (c) 2026 Tobias Stoeckmann
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 static void
test_format(int (* set_format)(struct archive *))28 test_format(int (*set_format)(struct archive *))
29 {
30 	struct archive *a;
31 	struct archive_entry *entry;
32 	char buff[2048];
33 	size_t used;
34 
35 	/* Create a new archive in memory. */
36 	assert((a = archive_write_new()) != NULL);
37 	assertA(0 == set_format(a));
38 	assertA(0 == archive_write_add_filter_none(a));
39 	assertA(0 == archive_write_set_bytes_per_block(a, 512));
40 	assertA(0 == archive_write_set_bytes_in_last_block(a, 512));
41 	assertA(0 == archive_write_open_memory(a, buff, sizeof(buff), &used));
42 
43 	/* Write directory with empty wide character name */
44 	assert((entry = archive_entry_new()) != NULL);
45 	archive_entry_copy_pathname_w(entry, L"");
46 	archive_entry_set_mode(entry, S_IFDIR | 0755);
47 	archive_entry_set_uid(entry, 0);
48 	archive_entry_set_gid(entry, 0);
49 	assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, entry));
50 
51 	/* Close out the archive. */
52 	archive_entry_free(entry);
53 	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
54 	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
55 
56 	/*
57 	 * Now, read the data back.
58 	 */
59 	assert((a = archive_read_new()) != NULL);
60 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a));
61 	assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a));
62 	assertEqualIntA(a, ARCHIVE_OK,
63 	    archive_read_open_memory(a, buff, used));
64 	assertEqualIntA(a, ARCHIVE_OK,
65 	    archive_read_next_header(a, &entry));
66 	assertEqualString("", archive_entry_pathname(entry));
67 
68 	/* Verify the end of the archive. */
69 	assertEqualIntA(a, ARCHIVE_EOF,
70 	    archive_read_next_header(a, &entry));
71 	assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
72 	assertEqualInt(ARCHIVE_OK, archive_read_free(a));
73 
74 }
75 
DEFINE_TEST(test_write_format_tar_empty_dirname)76 DEFINE_TEST(test_write_format_tar_empty_dirname)
77 {
78 	test_format(archive_write_set_format_gnutar);
79 	test_format(archive_write_set_format_pax);
80 	test_format(archive_write_set_format_ustar);
81 	test_format(archive_write_set_format_v7tar);
82 }
83