From 5cb28dc91a2e23e6f8f7e875e2ea725634526dfe Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sat, 14 Dec 2019 01:03:02 +0100 Subject: Add a test case for reading binary xattrs from pax/schily headers Signed-off-by: David Oberhollenzer --- tests/Makemodule.am | 8 +++- tests/tar/CREDITS | 5 +++ tests/tar/xattr/xattr-schily-binary.tar | Bin 0 -> 3072 bytes tests/tar_xattr_schily_bin.c | 72 ++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 tests/tar/xattr/xattr-schily-binary.tar create mode 100644 tests/tar_xattr_schily_bin.c (limited to 'tests') diff --git a/tests/Makemodule.am b/tests/Makemodule.am index 492744d..ce91ad3 100644 --- a/tests/Makemodule.am +++ b/tests/Makemodule.am @@ -81,6 +81,11 @@ test_tar_xattr_schily_LDADD = libtar.a libcompat.a test_tar_xattr_schily_CPPFLAGS = $(AM_CPPFLAGS) test_tar_xattr_schily_CPPFLAGS += -DTESTPATH=$(top_srcdir)/tests/tar +test_tar_xattr_schily_bin_SOURCES = tests/tar_xattr_schily_bin.c +test_tar_xattr_schily_bin_LDADD = libtar.a libcompat.a +test_tar_xattr_schily_bin_CPPFLAGS = $(AM_CPPFLAGS) +test_tar_xattr_schily_bin_CPPFLAGS += -DTESTPATH=$(top_srcdir)/tests/tar + fstree_fuzz_SOURCES = tests/fstree_fuzz.c fstree_fuzz_LDADD = libfstree.a libcompat.a @@ -93,6 +98,7 @@ check_PROGRAMS += test_get_path test_fstree_sort test_fstree_from_file check_PROGRAMS += test_fstree_init test_tar_ustar test_tar_pax test_tar_gnu check_PROGRAMS += test_tar_sparse_gnu test_tar_sparse_gnu1 test_tar_sparse_gnu2 check_PROGRAMS += test_tar_xattr_bsd test_tar_xattr_schily +check_PROGRAMS += test_tar_xattr_schily_bin check_SCRIPTS += tests/cantrbry.sh noinst_PROGRAMS += fstree_fuzz tar_fuzz @@ -103,7 +109,7 @@ TESTS += test_add_by_path test_get_path test_fstree_sort test_fstree_from_file TESTS += test_fstree_init test_tar_ustar test_tar_pax TESTS += test_tar_gnu test_tar_sparse_gnu test_tar_sparse_gnu1 TESTS += test_tar_sparse_gnu2 test_tar_xattr_bsd test_tar_xattr_schily -TESTS += tests/cantrbry.sh +TESTS += test_tar_xattr_schily_bin tests/cantrbry.sh endif EXTRA_DIST += $(top_srcdir)/tests/tar $(top_srcdir)/tests/words.txt diff --git a/tests/tar/CREDITS b/tests/tar/CREDITS index e4b19b4..6d55a59 100644 --- a/tests/tar/CREDITS +++ b/tests/tar/CREDITS @@ -23,3 +23,8 @@ for those features: In addition to that, the files in "file-size" are truncated, since we are only interested in parsing the header. + +The following addtional files have been added: + - xattr/xattr-shily-binary.tar + Created from xattr/xattr-shily.tar by manually patching in a capability + xattr key/value pair. diff --git a/tests/tar/xattr/xattr-schily-binary.tar b/tests/tar/xattr/xattr-schily-binary.tar new file mode 100644 index 0000000..0312807 Binary files /dev/null and b/tests/tar/xattr/xattr-schily-binary.tar differ diff --git a/tests/tar_xattr_schily_bin.c b/tests/tar_xattr_schily_bin.c new file mode 100644 index 0000000..d8d5161 --- /dev/null +++ b/tests/tar_xattr_schily_bin.c @@ -0,0 +1,72 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * tar_xattr_schily_bin.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#include "config.h" + +#include "tar.h" + +#include +#include +#include +#include + +#define STR(x) #x +#define STRVALUE(x) STR(x) + +#define TEST_PATH STRVALUE(TESTPATH) + +static FILE *open_read(const char *path) +{ + FILE *fp = fopen(path, "rb"); + + if (fp == NULL) { + perror(path); + exit(EXIT_FAILURE); + } + + return fp; +} + +static const uint8_t value[] = { + 0x00, 0x00, 0x00, 0x02, + 0x00, 0x30, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, +}; + +int main(void) +{ + tar_header_decoded_t hdr; + char buffer[6]; + FILE *fp; + + assert(chdir(TEST_PATH) == 0); + + fp = open_read("xattr/xattr-schily-binary.tar"); + assert(read_header(fp, &hdr) == 0); + assert(hdr.sb.st_mode == (S_IFREG | 0644)); + assert(hdr.sb.st_uid == 01750); + assert(hdr.sb.st_gid == 01750); + assert(hdr.sb.st_size == 5); + assert(hdr.sb.st_mtime == 1543094477); + assert(hdr.mtime == 1543094477); + assert(strcmp(hdr.name, "input.txt") == 0); + assert(!hdr.unknown_record); + assert(read_retry("data0", fp, buffer, 5) == 0); + buffer[5] = '\0'; + assert(strcmp(buffer, "test\n") == 0); + + assert(hdr.xattr != NULL); + assert(strcmp(hdr.xattr->key, "security.capability") == 0); + assert(hdr.xattr->value_len == sizeof(value)); + assert(memcmp(hdr.xattr->value, value, sizeof(value)) == 0); + assert(hdr.xattr->next == NULL); + + clear_header(&hdr); + fclose(fp); + return EXIT_SUCCESS; +} -- cgit v1.2.3