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