1 /*-
2 * Copyright (c) 2003-2007 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
27 /*
28 * Exercise various lengths of filenames in tar archives,
29 * especially around the magic sizes where ustar breaks
30 * filenames into prefix/suffix.
31 */
32
33 static void
test_filename(const char * prefix,int dlen,int flen)34 test_filename(const char *prefix, int dlen, int flen)
35 {
36 char buff[8192];
37 char filename[400];
38 char dirname[400];
39 struct archive_entry *ae;
40 struct archive *a;
41 size_t used;
42 char *p;
43 int i;
44
45 p = filename;
46 if (prefix) {
47 strcpy(filename, prefix);
48 p += strlen(p);
49 }
50 if (dlen > 0) {
51 for (i = 0; i < dlen; i++)
52 *p++ = 'a';
53 *p++ = '/';
54 }
55 for (i = 0; i < flen; i++)
56 *p++ = 'b';
57 *p = '\0';
58
59 strcpy(dirname, filename);
60
61 /* Create a new archive in memory. */
62 assert((a = archive_write_new()) != NULL);
63 assertA(0 == archive_write_set_format_pax_restricted(a));
64 assertA(0 == archive_write_add_filter_none(a));
65 assertA(0 == archive_write_set_bytes_per_block(a,0));
66 assertA(0 == archive_write_open_memory(a, buff, sizeof(buff), &used));
67
68 /*
69 * Write a file to it.
70 */
71 assert((ae = archive_entry_new()) != NULL);
72 archive_entry_copy_pathname(ae, filename);
73 archive_entry_set_mode(ae, S_IFREG | 0755);
74 failure("Pathname %d/%d", dlen, flen);
75 assertA(0 == archive_write_header(a, ae));
76 archive_entry_free(ae);
77
78 /*
79 * Write a dir to it (without trailing '/').
80 */
81 assert((ae = archive_entry_new()) != NULL);
82 archive_entry_copy_pathname(ae, dirname);
83 archive_entry_set_mode(ae, S_IFDIR | 0755);
84 failure("Dirname %d/%d", dlen, flen);
85 assertA(0 == archive_write_header(a, ae));
86 archive_entry_free(ae);
87
88 /* Tar adds a '/' to directory names. */
89 strcat(dirname, "/");
90
91 /*
92 * Write a dir to it (with trailing '/').
93 */
94 assert((ae = archive_entry_new()) != NULL);
95 archive_entry_copy_pathname(ae, dirname);
96 archive_entry_set_mode(ae, S_IFDIR | 0755);
97 failure("Dirname %d/%d", dlen, flen);
98 assertA(0 == archive_write_header(a, ae));
99 archive_entry_free(ae);
100
101 /* Close out the archive. */
102 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
103 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
104
105 /*
106 * Now, read the data back.
107 */
108 assert((a = archive_read_new()) != NULL);
109 assertA(0 == archive_read_support_format_all(a));
110 assertA(0 == archive_read_support_filter_all(a));
111 assertA(0 == archive_read_open_memory(a, buff, used));
112
113 /* Read the file and check the filename. */
114 assertA(0 == archive_read_next_header(a, &ae));
115 assertEqualString(filename, archive_entry_pathname(ae));
116 assertEqualInt((S_IFREG | 0755), archive_entry_mode(ae));
117
118 /*
119 * Read the two dirs and check the names.
120 *
121 * Both dirs should read back with the same name, since
122 * tar should add a trailing '/' to any dir that doesn't
123 * already have one. We only report the first such failure
124 * here.
125 */
126 assertA(0 == archive_read_next_header(a, &ae));
127 assertEqualString(dirname, archive_entry_pathname(ae));
128 assert((S_IFDIR | 0755) == archive_entry_mode(ae));
129
130 assertA(0 == archive_read_next_header(a, &ae));
131 assertEqualString(dirname, archive_entry_pathname(ae));
132 assert((S_IFDIR | 0755) == archive_entry_mode(ae));
133
134 /* Verify the end of the archive. */
135 assert(1 == archive_read_next_header(a, &ae));
136 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
137 assertEqualInt(ARCHIVE_OK, archive_read_free(a));
138 }
139
DEFINE_TEST(test_tar_filenames)140 DEFINE_TEST(test_tar_filenames)
141 {
142 int dlen, flen;
143
144 /* Repeat the following for a variety of dir/file lengths. */
145 for (dlen = 45; dlen < 55; dlen++) {
146 for (flen = 45; flen < 55; flen++) {
147 test_filename(NULL, dlen, flen);
148 test_filename("/", dlen, flen);
149 }
150 }
151
152 for (dlen = 0; dlen < 140; dlen += 10) {
153 for (flen = 98; flen < 102; flen++) {
154 test_filename(NULL, dlen, flen);
155 test_filename("/", dlen, flen);
156 }
157 }
158
159 for (dlen = 140; dlen < 160; dlen++) {
160 for (flen = 95; flen < 105; flen++) {
161 test_filename(NULL, dlen, flen);
162 test_filename("/", dlen, flen);
163 }
164 }
165 }
166