From: Zhaofeng Li Date: Sat, 24 May 2025 22:40:08 +0000 (-0600) Subject: Add test for macOS metadata reading in tar archives X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F2636%2Fhead;p=thirdparty%2Flibarchive.git Add test for macOS metadata reading in tar archives Signed-off-by: Zhaofeng Li --- diff --git a/Makefile.am b/Makefile.am index 74cb6e14b..d159f2970 100644 --- a/Makefile.am +++ b/Makefile.am @@ -531,6 +531,7 @@ libarchive_test_SOURCES= \ libarchive/test/test_read_format_tar_empty_with_gnulabel.c \ libarchive/test/test_read_format_tar_filename.c \ libarchive/test/test_read_format_tar_invalid_pax_size.c \ + libarchive/test/test_read_format_tar_mac_metadata.c \ libarchive/test/test_read_format_tar_pax_g_large.c \ libarchive/test/test_read_format_tar_pax_large_attr.c \ libarchive/test/test_read_format_tbz.c \ @@ -975,6 +976,7 @@ libarchive_test_EXTRA_DIST=\ libarchive/test/test_read_format_tar_empty_pax.tar.Z.uu \ libarchive/test/test_read_format_tar_filename_koi8r.tar.Z.uu \ libarchive/test/test_read_format_tar_invalid_pax_size.tar.uu \ + libarchive/test/test_read_format_tar_mac_metadata_1.tar.uu \ libarchive/test/test_read_format_tar_pax_g_large.tar.uu \ libarchive/test/test_read_format_tar_pax_large_attr.tar.Z.uu \ libarchive/test/test_read_format_ustar_filename_cp866.tar.Z.uu \ diff --git a/libarchive/test/CMakeLists.txt b/libarchive/test/CMakeLists.txt index ffdb5b4ef..2fa8b01c8 100644 --- a/libarchive/test/CMakeLists.txt +++ b/libarchive/test/CMakeLists.txt @@ -174,6 +174,7 @@ IF(ENABLE_TEST) test_read_format_tar_empty_pax.c test_read_format_tar_filename.c test_read_format_tar_invalid_pax_size.c + test_read_format_tar_mac_metadata.c test_read_format_tar_pax_g_large.c test_read_format_tar_pax_large_attr.c test_read_format_tbz.c diff --git a/libarchive/test/test_read_format_tar_mac_metadata.c b/libarchive/test/test_read_format_tar_mac_metadata.c new file mode 100644 index 000000000..b4745a22e --- /dev/null +++ b/libarchive/test/test_read_format_tar_mac_metadata.c @@ -0,0 +1,85 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2025 Zhaofeng Li + * All rights reserved. + */ +#include "test.h" + +DEFINE_TEST(test_read_format_tar_mac_metadata) +{ + /* + This test tar file is crafted with two files in a specific order: + + 1. A ._-prefixed file with pax header containing the path attribute. + 2. A file with a pax header but without the path attribute. + + It's designed to trigger the case encountered in: + + + GNU tar is required to reproduce this tar file: + + ```sh + NAME1="._101_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + NAME2="goodname" + OUT="test_read_format_tar_mac_metadata_1.tar" + + echo "content of badname" >"${NAME1}" + echo "content of goodname" >"${NAME2}" + + rm -f "${OUT}" + gnutar \ + --mtime="@0" \ + --owner=0 --group=0 --numeric-owner \ + --pax-option=exthdr.name=%d/PaxHeaders/%f,atime:=0,ctime:=0,foo:=bar \ + --format=pax \ + -cf "${OUT}" \ + "${NAME1}" \ + "${NAME2}" + uuencode "${OUT}" "${OUT}" >"${OUT}.uu" + + sha256sum "${OUT}" + sha256sum "${OUT}.uu" + ``` + */ + const char *refname = "test_read_format_tar_mac_metadata_1.tar"; + char *p; + size_t s; + struct archive *a; + struct archive_entry *ae; + + /* + * This is not a valid AppleDouble metadata file. It is merely to test that + * the correct bytes are read. + */ + const unsigned char appledouble[] = { + 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x20, 0x6f, 0x66, 0x20, 0x62, + 0x61, 0x64, 0x6e, 0x61, 0x6d, 0x65, 0x0a + }; + + extract_reference_file(refname); + p = slurpfile(&s, "%s", refname); + + assert((a = archive_read_new()) != NULL); + assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_tar(a)); + assertEqualIntA(a, ARCHIVE_OK, archive_read_set_option(a, "tar", "mac-ext", "1")); + assertEqualIntA(a, ARCHIVE_OK, read_open_memory_seek(a, p, s, 1)); + + assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); + + /* Correct name and metadata bytes */ + assertEqualString("goodname", archive_entry_pathname(ae)); + + const void *metadata = archive_entry_mac_metadata(ae, &s); + if (assert(metadata != NULL)) { + assertEqualMem(metadata, appledouble, + sizeof(appledouble)); + } + + /* ... and nothing else */ + assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); + assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); + assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a)); + + free(p); +} diff --git a/libarchive/test/test_read_format_tar_mac_metadata_1.tar.uu b/libarchive/test/test_read_format_tar_mac_metadata_1.tar.uu new file mode 100644 index 000000000..20b2cf5ef --- /dev/null +++ b/libarchive/test/test_read_format_tar_mac_metadata_1.tar.uu @@ -0,0 +1,231 @@ +begin 644 test_read_format_tar_mac_metadata_1.tar +M+B]087A(96%D97)S+RY?,3`Q7V%A86%A86%A86%A86%A86%A86%A86%A86%A +M86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A86%A +M86%A86%A86%A83`P,#`V-#0`,#`P,#`P,``P,#`P,#`P`#`P,#`P,#`P,C(P +M`#`P,#`P,#`P,#`P`#`S,#,Q,0`@>``````````````````````````````` +M```````````````````````````````````````````````````````````` +M``````````````````````````````````````````!U$AE861E