1 /*-
2  * Copyright (c) 2016 Tim Kientzle
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 /*
29  * Inspired by Github issue #682, which reported that gnutar filenames
30  * of exactly 512 bytes weren't getting written correctly.
31  *
32  * This writes a filename of every length from 1 to 2000 bytes and
33  * reads back to verify it.
34  */
35 
36 static char filename[2048];
37 
38 DEFINE_TEST(test_write_format_gnutar_filenames)
39 {
40 	size_t buffsize = 1000000;
41 	char *buff;
42 	struct archive_entry *ae, *template;
43 	struct archive *a;
44 	size_t used;
45 	int i;
46 
47 	buff = malloc(buffsize); /* million bytes of work area */
48 	assert(buff != NULL);
49 
50 	/* Create a template entry. */
51 	assert((template = archive_entry_new()) != NULL);
52 	archive_entry_set_atime(template, 2, 20);
53 	archive_entry_set_birthtime(template, 3, 30);
54 	archive_entry_set_ctime(template, 4, 40);
55 	archive_entry_set_mtime(template, 5, 50);
56 	archive_entry_set_mode(template, S_IFREG | 0755);
57 	archive_entry_set_size(template, 8);
58 
59 	for (i = 0; i < 2000; ++i) {
60 		filename[i] = 'a';
61 		filename[i + 1] = '\0';
62 		archive_entry_copy_pathname(template, filename);
63 
64 		/* Write a one-item gnutar format archive. */
65 		assert((a = archive_write_new()) != NULL);
66 		assertA(0 == archive_write_set_format_gnutar(a));
67 		assertA(0 == archive_write_add_filter_none(a));
68 		assertA(0 == archive_write_open_memory(a, buff, buffsize, &used));
69 		assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, template));
70 		assertEqualIntA(a, 8, archive_write_data(a, "12345678", 9));
71 		assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
72 		assertEqualIntA(a, ARCHIVE_OK, archive_write_free(a));
73 
74 
75 		/* Read back and verify the filename. */
76 		assert((a = archive_read_new()) != NULL);
77 		assertEqualIntA(a, 0, archive_read_support_format_all(a));
78 		assertEqualIntA(a, 0, archive_read_support_filter_all(a));
79 		assertEqualIntA(a, 0, archive_read_open_memory(a, buff, used));
80 
81 		assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
82 		assertEqualString(filename, archive_entry_pathname(ae));
83 		assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
84 		assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
85 		assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
86 	}
87 
88 	archive_entry_free(template);
89 
90 	free(buff);
91 }
92 
93 
94 DEFINE_TEST(test_write_format_gnutar_linknames)
95 {
96 	size_t buffsize = 1000000;
97 	char *buff;
98 	struct archive_entry *ae, *template;
99 	struct archive *a;
100 	size_t used;
101 	int i;
102 
103 #ifdef S_IFLNK
104 	assertEqualInt(S_IFLNK, AE_IFLNK);
105 #endif
106 
107 	buff = malloc(buffsize); /* million bytes of work area */
108 	assert(buff != NULL);
109 
110 	/* Create a template entry. */
111 	assert((template = archive_entry_new()) != NULL);
112 	archive_entry_set_atime(template, 2, 20);
113 	archive_entry_set_birthtime(template, 3, 30);
114 	archive_entry_set_ctime(template, 4, 40);
115 	archive_entry_set_mtime(template, 5, 50);
116 	archive_entry_set_mode(template, AE_IFLNK | 0755);
117 	archive_entry_copy_pathname(template, "link");
118 
119 	for (i = 0; i < 2000; ++i) {
120 		filename[i] = 'a';
121 		filename[i + 1] = '\0';
122 		archive_entry_copy_symlink(template, filename);
123 
124 		/* Write a one-item gnutar format archive. */
125 		assert((a = archive_write_new()) != NULL);
126 		assertA(0 == archive_write_set_format_gnutar(a));
127 		assertA(0 == archive_write_add_filter_none(a));
128 		assertA(0 == archive_write_open_memory(a, buff, buffsize, &used));
129 		assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, template));
130 		assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
131 		assertEqualIntA(a, ARCHIVE_OK, archive_write_free(a));
132 
133 
134 		/* Read back and verify the filename. */
135 		assert((a = archive_read_new()) != NULL);
136 		assertEqualIntA(a, 0, archive_read_support_format_all(a));
137 		assertEqualIntA(a, 0, archive_read_support_filter_all(a));
138 		assertEqualIntA(a, 0, archive_read_open_memory(a, buff, used));
139 
140 		assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
141 		assertEqualString("link", archive_entry_pathname(ae));
142 		assertEqualString(filename, archive_entry_symlink(ae));
143 		assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
144 		assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
145 		assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
146 	}
147 
148 	archive_entry_free(template);
149 
150 	free(buff);
151 }
152