]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/libarchive/libarchive/test/test_write_format_tar_v7tar.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / libarchive / libarchive / test / test_write_format_tar_v7tar.c
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * Copyright (c) 2012 Michihiro NAKAJIMA
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 #include "test.h"
27 __FBSDID("$FreeBSD$");
28
29 static int
30 is_null(const char *p, size_t l)
31 {
32         while (l > 0) {
33                 if (*p != '\0')
34                         return (0);
35                 --l;
36                 ++p;
37         }
38         return (1);
39 }
40
41 /* Verify the contents, then erase them to NUL bytes. */
42 /* Tar requires all "unused" bytes be set to NUL; this allows us
43  * to easily verify that by invoking is_null() over the entire header
44  * after verifying each field. */
45 #define myAssertEqualMem(a,b,s) assertEqualMem(a, b, s); memset(a, 0, s)
46
47 /*
48  * Detailed verification that 'v7tar' archives are written with
49  * the correct format.
50  */
51 DEFINE_TEST(test_write_format_tar_v7tar)
52 {
53         struct archive *a;
54         struct archive_entry *entry;
55         char *buff, *e;
56         size_t buffsize = 100000;
57         size_t used;
58         int i;
59         char f99[100];
60         char f100[101];
61
62         for (i = 0; i < 99; ++i)
63                 f99[i] = 'a' + i % 26;
64         f99[99] = '\0';
65
66         for (i = 0; i < 100; ++i)
67                 f100[i] = 'A' + i % 26;
68         f100[100] = '\0';
69
70         buff = malloc(buffsize);
71
72         /* Create a new archive in memory. */
73         assert((a = archive_write_new()) != NULL);
74         assertEqualIntA(a, ARCHIVE_OK,
75             archive_write_set_format_v7tar(a));
76         assertEqualIntA(a, ARCHIVE_OK,
77             archive_write_add_filter_none(a));
78         assertEqualIntA(a, ARCHIVE_OK,
79             archive_write_open_memory(a, buff, buffsize, &used));
80
81         /*
82          * Add various files to it.
83          * TODO: Extend this to cover more filetypes.
84          */
85
86         /* "file" with 10 bytes of content */
87         assert((entry = archive_entry_new()) != NULL);
88         archive_entry_set_mtime(entry, 1, 10);
89         archive_entry_set_pathname(entry, "file");
90         archive_entry_set_mode(entry, S_IFREG | 0664);
91         archive_entry_set_size(entry, 10);
92         archive_entry_set_uid(entry, 80);
93         archive_entry_set_gid(entry, 90);
94         archive_entry_set_dev(entry, 12);
95         archive_entry_set_ino(entry, 89);
96         archive_entry_set_nlink(entry, 2);
97         assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, entry));
98         archive_entry_free(entry);
99         assertEqualIntA(a, 10, archive_write_data(a, "1234567890", 10));
100
101         /* Hardlink to "file" with 10 bytes of content */
102         assert((entry = archive_entry_new()) != NULL);
103         archive_entry_set_mtime(entry, 1, 10);
104         archive_entry_set_pathname(entry, "linkfile");
105         archive_entry_set_hardlink(entry, "file");
106         archive_entry_set_mode(entry, S_IFREG | 0664);
107         archive_entry_set_size(entry, 10);
108         archive_entry_set_uid(entry, 80);
109         archive_entry_set_gid(entry, 90);
110         archive_entry_set_dev(entry, 12);
111         archive_entry_set_ino(entry, 89);
112         archive_entry_set_nlink(entry, 2);
113         assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, entry));
114         archive_entry_free(entry);
115         /* Write of data to dir should fail == zero bytes get written. */
116         assertEqualIntA(a, 0, archive_write_data(a, "1234567890", 10));
117
118         /* "dir" */
119         assert((entry = archive_entry_new()) != NULL);
120         archive_entry_set_mtime(entry, 2, 20);
121         archive_entry_set_pathname(entry, "dir");
122         archive_entry_set_mode(entry, S_IFDIR | 0775);
123         archive_entry_set_size(entry, 10);
124         archive_entry_set_nlink(entry, 2);
125         assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, entry));
126         archive_entry_free(entry);
127         /* Write of data to dir should fail == zero bytes get written. */
128         assertEqualIntA(a, 0, archive_write_data(a, "1234567890", 10));
129
130         /* "symlink" pointing to "file" */
131         assert((entry = archive_entry_new()) != NULL);
132         archive_entry_set_mtime(entry, 3, 30);
133         archive_entry_set_pathname(entry, "symlink");
134         archive_entry_set_mode(entry, 0664);
135         archive_entry_set_filetype(entry, AE_IFLNK);
136         archive_entry_set_symlink(entry,"file");
137         archive_entry_set_size(entry, 0);
138         archive_entry_set_uid(entry, 88);
139         archive_entry_set_gid(entry, 98);
140         archive_entry_set_dev(entry, 12);
141         archive_entry_set_ino(entry, 90);
142         archive_entry_set_nlink(entry, 1);
143         assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, entry));
144         archive_entry_free(entry);
145
146         /* file with 99-char filename. */
147         assert((entry = archive_entry_new()) != NULL);
148         archive_entry_set_mtime(entry, 1, 10);
149         archive_entry_set_pathname(entry, f99);
150         archive_entry_set_mode(entry, S_IFREG | 0664);
151         archive_entry_set_size(entry, 0);
152         archive_entry_set_uid(entry, 82);
153         archive_entry_set_gid(entry, 93);
154         archive_entry_set_dev(entry, 102);
155         archive_entry_set_ino(entry, 7);
156         archive_entry_set_nlink(entry, 1);
157         assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, entry));
158         archive_entry_free(entry);
159
160         /* file with 100-char filename. */
161         assert((entry = archive_entry_new()) != NULL);
162         archive_entry_set_mtime(entry, 1, 10);
163         archive_entry_set_pathname(entry, f100);
164         archive_entry_set_mode(entry, S_IFREG | 0664);
165         archive_entry_set_size(entry, 0);
166         archive_entry_set_uid(entry, 82);
167         archive_entry_set_gid(entry, 93);
168         archive_entry_set_dev(entry, 102);
169         archive_entry_set_ino(entry, 7);
170         archive_entry_set_nlink(entry, 1);
171         failure("100-char filename should be rejected");
172         assertEqualIntA(a, ARCHIVE_FAILED, archive_write_header(a, entry));
173         archive_entry_free(entry);
174
175         /* Close out the archive. */
176         assertEqualInt(ARCHIVE_OK, archive_write_free(a));
177
178         /*
179          * Verify the archive format.
180          */
181         e = buff;
182
183         /* "file" */
184         myAssertEqualMem(e + 0, "file", 5); /* Filename */
185         myAssertEqualMem(e + 100, "000664 ", 8); /* mode */
186         myAssertEqualMem(e + 108, "000120 ", 8); /* uid */
187         myAssertEqualMem(e + 116, "000132 ", 8); /* gid */
188         myAssertEqualMem(e + 124, "00000000012 ", 12); /* size */
189         myAssertEqualMem(e + 136, "00000000001 ", 12); /* mtime */
190         myAssertEqualMem(e + 148, "005335\0 ", 8); /* checksum */
191         myAssertEqualMem(e + 156, "", 1); /* linkflag */
192         myAssertEqualMem(e + 157, "", 1); /* linkname */
193         assert(is_null(e + 0, 512));
194         myAssertEqualMem(e + 512, "1234567890", 10);
195         assert(is_null(e + 512, 512));
196         e += 1024;
197
198         /* hardlink to "file" */
199         myAssertEqualMem(e + 0, "linkfile", 9); /* Filename */
200         myAssertEqualMem(e + 100, "000664 ", 8); /* mode */
201         myAssertEqualMem(e + 108, "000120 ", 8); /* uid */
202         myAssertEqualMem(e + 116, "000132 ", 8); /* gid */
203         myAssertEqualMem(e + 124, "00000000000 ", 12); /* size */
204         myAssertEqualMem(e + 136, "00000000001 ", 12); /* mtime */
205         myAssertEqualMem(e + 148, "007131\0 ", 8); /* checksum */
206         myAssertEqualMem(e + 156, "1", 1); /* linkflag */
207         myAssertEqualMem(e + 157, "file", 5); /* linkname */
208         assert(is_null(e + 0, 512));
209         e += 512;
210
211         /* "dir" */
212         myAssertEqualMem(e + 0, "dir/", 4); /* Filename */
213         myAssertEqualMem(e + 100, "000775 ", 8); /* mode */
214         myAssertEqualMem(e + 108, "000000 ", 8); /* uid */
215         myAssertEqualMem(e + 116, "000000 ", 8); /* gid */
216         myAssertEqualMem(e + 124, "00000000000 ", 12); /* size */
217         myAssertEqualMem(e + 136, "00000000002 ", 12); /* mtime */
218         myAssertEqualMem(e + 148, "005243\0 ", 8); /* checksum */
219         myAssertEqualMem(e + 156, "", 1); /* typeflag */
220         myAssertEqualMem(e + 157, "", 1); /* linkname */
221         assert(is_null(e + 0, 512));
222         e += 512;
223
224         /* "symlink" pointing to "file" */
225         myAssertEqualMem(e + 0, "symlink", 8); /* Filename */
226         myAssertEqualMem(e + 100, "000664 ", 8); /* mode */
227         myAssertEqualMem(e + 108, "000130 ", 8); /* uid */
228         myAssertEqualMem(e + 116, "000142 ", 8); /* gid */
229         myAssertEqualMem(e + 124, "00000000000 ", 12); /* size */
230         myAssertEqualMem(e + 136, "00000000003 ", 12); /* mtime */
231         myAssertEqualMem(e + 148, "007027\0 ", 8); /* checksum */
232         myAssertEqualMem(e + 156, "2", 1); /* linkflag */
233         myAssertEqualMem(e + 157, "file", 5); /* linkname */
234         assert(is_null(e + 0, 512));
235         e += 512;
236
237         /* File with 99-char filename */
238         myAssertEqualMem(e + 0, f99, 100); /* Filename */
239         myAssertEqualMem(e + 100, "000664 ", 8); /* mode */
240         myAssertEqualMem(e + 108, "000122 ", 8); /* uid */
241         myAssertEqualMem(e + 116, "000135 ", 8); /* gid */
242         myAssertEqualMem(e + 124, "00000000000 ", 12); /* size */
243         myAssertEqualMem(e + 136, "00000000001 ", 12); /* mtime */
244         myAssertEqualMem(e + 148, "031543\0 ", 8); /* checksum */
245         myAssertEqualMem(e + 156, "", 1); /* linkflag */
246         myAssertEqualMem(e + 157, "", 1); /* linkname */
247         assert(is_null(e + 0, 512));
248         e += 512;
249
250         /* TODO: Verify other types of entries. */
251
252         /* Last entry is end-of-archive marker. */
253         assert(is_null(e, 1024));
254         e += 1024;
255
256         assertEqualInt((int)used, e - buff);
257
258         free(buff);
259 }