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