1/*-
2 * Copyright (c) 2019 Martin Matuska
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
27static struct archive_entry*
28create_archive_entry(void) {
29	struct archive_entry *ae;
30
31	assert((ae = archive_entry_new()) != NULL);
32        archive_entry_set_atime(ae, 2, 20);
33        archive_entry_set_ctime(ae, 4, 40);
34        archive_entry_set_mtime(ae, 5, 50);
35        archive_entry_copy_pathname(ae, "file");
36        archive_entry_set_mode(ae, AE_IFREG | 0755);
37        archive_entry_set_nlink(ae, 2);
38        archive_entry_set_size(ae, 8);
39        archive_entry_xattr_add_entry(ae, "user.data1", "ABCDEFG", 7);
40        archive_entry_xattr_add_entry(ae, "user.data2", "XYZ", 3);
41
42	return (ae);
43}
44
45DEFINE_TEST(test_pax_xattr_header)
46{
47	static const char *reffiles[] = {
48	    "test_pax_xattr_header_all.tar",
49	    "test_pax_xattr_header_libarchive.tar",
50	    "test_pax_xattr_header_schily.tar",
51	    NULL
52	};
53	struct archive *a;
54	struct archive_entry *ae;
55
56	extract_reference_files(reffiles);
57
58	/* First archive, no options */
59	assert((a = archive_write_new()) != NULL);
60	assertEqualIntA(a, 0, archive_write_set_format_pax(a));
61	assertEqualIntA(a, 0, archive_write_add_filter_none(a));
62	assertEqualInt(0,
63	    archive_write_open_filename(a, "test1.tar"));
64	ae = create_archive_entry();
65        assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
66        archive_entry_free(ae);
67        assertEqualIntA(a, 8, archive_write_data(a, "12345678", 9));
68
69	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
70	assertEqualIntA(a, ARCHIVE_OK, archive_write_free(a));
71
72	assertEqualFile("test1.tar","test_pax_xattr_header_all.tar");
73
74	/* Second archive, xattrheader=SCHILY */
75	assert((a = archive_write_new()) != NULL);
76	assertEqualIntA(a, 0, archive_write_set_format_pax(a));
77	assertEqualIntA(a, 0, archive_write_add_filter_none(a));
78	assertEqualIntA(a, 0, archive_write_set_options(a,
79	    "xattrheader=SCHILY"));
80	assertEqualInt(0,
81	    archive_write_open_filename(a, "test2.tar"));
82
83	ae = create_archive_entry();
84        assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
85        archive_entry_free(ae);
86        assertEqualIntA(a, 8, archive_write_data(a, "12345678", 9));
87
88	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
89	assertEqualIntA(a, ARCHIVE_OK, archive_write_free(a));
90
91	assertEqualFile("test2.tar","test_pax_xattr_header_schily.tar");
92
93	/* Third archive, xattrheader=LIBARCHIVE */
94	assert((a = archive_write_new()) != NULL);
95	assertEqualIntA(a, 0, archive_write_set_format_pax(a));
96	assertEqualIntA(a, 0, archive_write_add_filter_none(a));
97	assertEqualIntA(a, 0, archive_write_set_options(a,
98	    "xattrheader=LIBARCHIVE"));
99	assertEqualInt(0,
100	    archive_write_open_filename(a, "test3.tar"));
101
102	ae = create_archive_entry();
103        assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
104        archive_entry_free(ae);
105        assertEqualIntA(a, 8, archive_write_data(a, "12345678", 9));
106
107	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
108	assertEqualIntA(a, ARCHIVE_OK, archive_write_free(a));
109
110	assertEqualFile("test3.tar","test_pax_xattr_header_libarchive.tar");
111
112	/* Fourth archive, xattrheader=ALL */
113	assert((a = archive_write_new()) != NULL);
114	assertEqualIntA(a, 0, archive_write_set_format_pax(a));
115	assertEqualIntA(a, 0, archive_write_add_filter_none(a));
116	assertEqualIntA(a, 0, archive_write_set_options(a, "xattrheader=ALL"));
117	assertEqualInt(0,
118	    archive_write_open_filename(a, "test4.tar"));
119
120	ae = create_archive_entry();
121        assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
122        archive_entry_free(ae);
123        assertEqualIntA(a, 8, archive_write_data(a, "12345678", 9));
124
125	assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
126	assertEqualIntA(a, ARCHIVE_OK, archive_write_free(a));
127
128	assertEqualFile("test4.tar","test_pax_xattr_header_all.tar");
129}
130