2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 __FBSDID("$FreeBSD$");
29 * Exercise various lengths of filenames in ustar archives.
33 test_filename(const char *prefix, int dlen, int flen)
38 struct archive_entry *ae;
45 strcpy(filename, prefix);
46 i = (int)strlen(prefix);
54 for (; i < dlen + flen + separator; i++)
58 strcpy(dirname, filename);
60 /* Create a new archive in memory. */
61 assert((a = archive_write_new()) != NULL);
62 assertA(0 == archive_write_set_format_ustar(a));
63 assertA(0 == archive_write_set_compression_none(a));
64 assertA(0 == archive_write_set_bytes_per_block(a,0));
65 assertA(0 == archive_write_open_memory(a, buff, sizeof(buff), &used));
70 assert((ae = archive_entry_new()) != NULL);
71 archive_entry_copy_pathname(ae, filename);
72 archive_entry_set_mode(ae, S_IFREG | 0755);
73 failure("dlen=%d, flen=%d", dlen, flen);
75 assertEqualIntA(a, ARCHIVE_FAILED, archive_write_header(a, ae));
77 assertEqualIntA(a, 0, archive_write_header(a, ae));
79 archive_entry_free(ae);
82 * Write a dir to it (without trailing '/').
84 assert((ae = archive_entry_new()) != NULL);
85 archive_entry_copy_pathname(ae, dirname);
86 archive_entry_set_mode(ae, S_IFDIR | 0755);
87 failure("dlen=%d, flen=%d", dlen, flen);
89 assertEqualIntA(a, ARCHIVE_FAILED, archive_write_header(a, ae));
91 assertEqualIntA(a, 0, archive_write_header(a, ae));
93 archive_entry_free(ae);
95 /* Tar adds a '/' to directory names. */
99 * Write a dir to it (with trailing '/').
101 assert((ae = archive_entry_new()) != NULL);
102 archive_entry_copy_pathname(ae, dirname);
103 archive_entry_set_mode(ae, S_IFDIR | 0755);
104 failure("dlen=%d, flen=%d", dlen, flen);
106 assertEqualIntA(a, ARCHIVE_FAILED, archive_write_header(a, ae));
108 assertEqualIntA(a, 0, archive_write_header(a, ae));
110 archive_entry_free(ae);
112 /* Close out the archive. */
113 assertA(0 == archive_write_close(a));
114 #if ARCHIVE_VERSION_NUMBER < 2000000
115 archive_write_finish(a);
117 assertEqualInt(0, archive_write_finish(a));
121 * Now, read the data back.
123 assert((a = archive_read_new()) != NULL);
124 assertA(0 == archive_read_support_format_all(a));
125 assertA(0 == archive_read_support_compression_all(a));
126 assertA(0 == archive_read_open_memory(a, buff, used));
129 /* Read the file and check the filename. */
130 assertA(0 == archive_read_next_header(a, &ae));
131 failure("dlen=%d, flen=%d", dlen, flen);
132 assertEqualString(filename, archive_entry_pathname(ae));
133 assertEqualInt((S_IFREG | 0755), archive_entry_mode(ae));
137 * Read the two dirs and check the names.
139 * Both dirs should read back with the same name, since
140 * tar should add a trailing '/' to any dir that doesn't
144 assertA(0 == archive_read_next_header(a, &ae));
145 assert((S_IFDIR | 0755) == archive_entry_mode(ae));
146 failure("dlen=%d, flen=%d", dlen, flen);
147 assertEqualString(dirname, archive_entry_pathname(ae));
151 assertA(0 == archive_read_next_header(a, &ae));
152 assert((S_IFDIR | 0755) == archive_entry_mode(ae));
153 assertEqualString(dirname, archive_entry_pathname(ae));
156 /* Verify the end of the archive. */
157 failure("This fails if entries were written that should not have been written. dlen=%d, flen=%d", dlen, flen);
158 assertEqualInt(1, archive_read_next_header(a, &ae));
159 assert(0 == archive_read_close(a));
160 #if ARCHIVE_VERSION_NUMBER < 2000000
161 archive_read_finish(a);
163 assertEqualInt(0, archive_read_finish(a));
167 DEFINE_TEST(test_ustar_filenames)
171 /* Try a bunch of different file/dir lengths that add up
172 * to just a little less or a little more than 100 bytes.
173 * This exercises the code that splits paths between ustar
174 * filename and prefix fields.
176 for (dlen = 5; dlen < 70; dlen += 5) {
177 for (flen = 100 - dlen - 5; flen < 100 - dlen + 5; flen++) {
178 test_filename(NULL, dlen, flen);
179 test_filename("/", dlen, flen);
183 /* Probe the 100-char limit for paths with no '/'. */
184 for (flen = 90; flen < 110; flen++) {
185 test_filename(NULL, 0, flen);
186 test_filename("/", dlen, flen);
189 /* XXXX TODO Probe the 100-char limit with a dir prefix. */
190 /* XXXX TODO Probe the 255-char total limit. */