]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/libarchive/libarchive/test/test_write_format_gnutar.c
MFC r299529,r299540,r299576,r299896:
[FreeBSD/stable/10.git] / contrib / libarchive / libarchive / test / test_write_format_gnutar.c
1 /*-
2  * Copyright (c) 2010 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 static char buff2[64];
29
30 /* Some names 1026 characters long */
31 static const char *longfilename = "abcdefghijklmnopqrstuvwxyz"
32     "12345678901234567890123456789012345678901234567890"
33     "12345678901234567890123456789012345678901234567890"
34     "12345678901234567890123456789012345678901234567890"
35     "12345678901234567890123456789012345678901234567890"
36     "12345678901234567890123456789012345678901234567890"
37     "12345678901234567890123456789012345678901234567890"
38     "12345678901234567890123456789012345678901234567890"
39     "12345678901234567890123456789012345678901234567890"
40     "12345678901234567890123456789012345678901234567890"
41     "12345678901234567890123456789012345678901234567890"
42     "12345678901234567890123456789012345678901234567890"
43     "12345678901234567890123456789012345678901234567890"
44     "12345678901234567890123456789012345678901234567890"
45     "12345678901234567890123456789012345678901234567890"
46     "12345678901234567890123456789012345678901234567890"
47     "12345678901234567890123456789012345678901234567890"
48     "12345678901234567890123456789012345678901234567890"
49     "12345678901234567890123456789012345678901234567890"
50     "12345678901234567890123456789012345678901234567890"
51     "12345678901234567890123456789012345678901234567890";
52
53 static const char *longlinkname = "Xabcdefghijklmnopqrstuvwxyz"
54     "12345678901234567890123456789012345678901234567890"
55     "12345678901234567890123456789012345678901234567890"
56     "12345678901234567890123456789012345678901234567890"
57     "12345678901234567890123456789012345678901234567890"
58     "12345678901234567890123456789012345678901234567890"
59     "12345678901234567890123456789012345678901234567890"
60     "12345678901234567890123456789012345678901234567890"
61     "12345678901234567890123456789012345678901234567890"
62     "12345678901234567890123456789012345678901234567890"
63     "12345678901234567890123456789012345678901234567890"
64     "12345678901234567890123456789012345678901234567890"
65     "12345678901234567890123456789012345678901234567890"
66     "12345678901234567890123456789012345678901234567890"
67     "12345678901234567890123456789012345678901234567890"
68     "12345678901234567890123456789012345678901234567890"
69     "12345678901234567890123456789012345678901234567890"
70     "12345678901234567890123456789012345678901234567890"
71     "12345678901234567890123456789012345678901234567890"
72     "12345678901234567890123456789012345678901234567890"
73     "12345678901234567890123456789012345678901234567890";
74
75 static const char *longhardlinkname = "Yabcdefghijklmnopqrstuvwxyz"
76     "12345678901234567890123456789012345678901234567890"
77     "12345678901234567890123456789012345678901234567890"
78     "12345678901234567890123456789012345678901234567890"
79     "12345678901234567890123456789012345678901234567890"
80     "12345678901234567890123456789012345678901234567890"
81     "12345678901234567890123456789012345678901234567890"
82     "12345678901234567890123456789012345678901234567890"
83     "12345678901234567890123456789012345678901234567890"
84     "12345678901234567890123456789012345678901234567890"
85     "12345678901234567890123456789012345678901234567890"
86     "12345678901234567890123456789012345678901234567890"
87     "12345678901234567890123456789012345678901234567890"
88     "12345678901234567890123456789012345678901234567890"
89     "12345678901234567890123456789012345678901234567890"
90     "12345678901234567890123456789012345678901234567890"
91     "12345678901234567890123456789012345678901234567890"
92     "12345678901234567890123456789012345678901234567890"
93     "12345678901234567890123456789012345678901234567890"
94     "12345678901234567890123456789012345678901234567890"
95     "12345678901234567890123456789012345678901234567890";
96
97
98 DEFINE_TEST(test_write_format_gnutar)
99 {
100         size_t buffsize = 1000000;
101         char *buff;
102         struct archive_entry *ae;
103         struct archive *a;
104         size_t used;
105
106         buff = malloc(buffsize); /* million bytes of work area */
107         assert(buff != NULL);
108
109         /* Create a new archive in memory. */
110         assert((a = archive_write_new()) != NULL);
111         assertA(0 == archive_write_set_format_gnutar(a));
112         assertA(0 == archive_write_add_filter_none(a));
113         assertA(0 == archive_write_open_memory(a, buff, buffsize, &used));
114
115         /*
116          * "file" has a bunch of attributes and 8 bytes of data.
117          */
118         assert((ae = archive_entry_new()) != NULL);
119         archive_entry_set_atime(ae, 2, 20);
120         archive_entry_set_birthtime(ae, 3, 30);
121         archive_entry_set_ctime(ae, 4, 40);
122         archive_entry_set_mtime(ae, 5, 50);
123         archive_entry_copy_pathname(ae, "file");
124         archive_entry_set_mode(ae, S_IFREG | 0755);
125         archive_entry_set_size(ae, 8);
126         assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
127         archive_entry_free(ae);
128         assertEqualIntA(a, 8, archive_write_data(a, "12345678", 9));
129
130         /*
131          * A file with a very long name
132          */
133         assert((ae = archive_entry_new()) != NULL);
134         archive_entry_copy_pathname(ae, longfilename);
135         archive_entry_set_mode(ae, S_IFREG | 0755);
136         archive_entry_set_size(ae, 8);
137         assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
138         archive_entry_free(ae);
139         assertEqualIntA(a, 8, archive_write_data(a, "abcdefgh", 9));
140
141         /*
142          * A hardlink to the above file.
143          */
144         assert((ae = archive_entry_new()) != NULL);
145         archive_entry_copy_pathname(ae, longhardlinkname);
146         archive_entry_copy_hardlink(ae, longfilename);
147         archive_entry_set_mode(ae, S_IFREG | 0755);
148         archive_entry_set_size(ae, 8);
149         assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
150         archive_entry_free(ae);
151
152         /*
153          * A symlink to the above file.
154          */
155         assert((ae = archive_entry_new()) != NULL);
156         archive_entry_copy_pathname(ae, longlinkname);
157         archive_entry_copy_symlink(ae, longfilename);
158         archive_entry_set_mode(ae, AE_IFLNK | 0755);
159         assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
160         archive_entry_free(ae);
161
162         /*
163          * A file with large UID/GID that overflow octal encoding.
164          */
165         assert((ae = archive_entry_new()) != NULL);
166         archive_entry_copy_pathname(ae, "large_uid_gid");
167         archive_entry_set_mode(ae, S_IFREG | 0755);
168         archive_entry_set_size(ae, 8);
169         archive_entry_set_uid(ae, 123456789);
170         archive_entry_set_gid(ae, 987654321);
171         assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae));
172         archive_entry_free(ae);
173         assertEqualIntA(a, 8, archive_write_data(a, "abcdefgh", 9));
174
175         /* TODO: support GNU tar sparse format and test it here. */
176         /* See test_write_format_pax for an example of testing sparse files. */
177
178         /* Close out the archive. */
179         assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
180         assertEqualIntA(a, ARCHIVE_OK, archive_write_free(a));
181
182         /*
183          * Some basic verification of the low-level format.
184          */
185
186         /* Verify GNU tar magic/version fields */
187         assertEqualMem(buff + 257, "ustar  \0", 8);
188
189         assertEqualInt(15360, used);
190
191         /*
192          *
193          * Now, read the data back.
194          *
195          */
196         assert((a = archive_read_new()) != NULL);
197         assertEqualIntA(a, 0, archive_read_support_format_all(a));
198         assertEqualIntA(a, 0, archive_read_support_filter_all(a));
199         assertEqualIntA(a, 0, archive_read_open_memory(a, buff, used));
200
201         /*
202          * Read "file"
203          */
204         assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
205         assert(!archive_entry_atime_is_set(ae));
206         assert(!archive_entry_birthtime_is_set(ae));
207         assert(!archive_entry_ctime_is_set(ae));
208         assertEqualInt(5, archive_entry_mtime(ae));
209         assertEqualInt(0, archive_entry_mtime_nsec(ae));
210         assertEqualString("file", archive_entry_pathname(ae));
211         assertEqualInt(S_IFREG | 0755, archive_entry_mode(ae));
212         assertEqualInt(8, archive_entry_size(ae));
213         assertEqualIntA(a, 8, archive_read_data(a, buff2, 10));
214         assertEqualMem(buff2, "12345678", 8);
215
216         /*
217          * Read file with very long name.
218          */
219         assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
220         assertEqualString(longfilename, archive_entry_pathname(ae));
221         assertEqualInt(S_IFREG | 0755, archive_entry_mode(ae));
222         assertEqualInt(8, archive_entry_size(ae));
223         assertEqualIntA(a, 8, archive_read_data(a, buff2, 10));
224         assertEqualMem(buff2, "abcdefgh", 8);
225
226         /*
227          * Read hardlink.
228          */
229         assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
230         assertEqualString(longhardlinkname, archive_entry_pathname(ae));
231         assertEqualString(longfilename, archive_entry_hardlink(ae));
232
233         /*
234          * Read symlink.
235          */
236         assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
237         assertEqualString(longlinkname, archive_entry_pathname(ae));
238         assertEqualString(longfilename, archive_entry_symlink(ae));
239         assertEqualInt(AE_IFLNK | 0755, archive_entry_mode(ae));
240
241         /*
242          * Read file with large UID/GID.
243          */
244         assertEqualIntA(a, 0, archive_read_next_header(a, &ae));
245         assertEqualInt(123456789, archive_entry_uid(ae));
246         assertEqualInt(987654321, archive_entry_gid(ae));
247         assertEqualString("large_uid_gid", archive_entry_pathname(ae));
248         assertEqualInt(S_IFREG | 0755, archive_entry_mode(ae));
249
250         /*
251          * Verify the end of the archive.
252          */
253         assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae));
254         assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a));
255         assertEqualIntA(a, ARCHIVE_OK, archive_read_free(a));
256
257         free(buff);
258 }