]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - lib/libarchive/test/test_write_format_cpio_odc.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / lib / libarchive / test / test_write_format_cpio_odc.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 static int
30 is_octal(const char *p, size_t l)
31 {
32         while (l > 0) {
33                 if (*p < '0' || *p > '7')
34                         return (0);
35                 --l;
36                 ++p;
37         }
38         return (1);
39 }
40
41 /*
42  * Detailed verification that cpio 'odc' archives are written with
43  * the correct format.
44  */
45 DEFINE_TEST(test_write_format_cpio_odc)
46 {
47         struct archive *a;
48         struct archive_entry *entry;
49         char *buff, *e;
50         size_t buffsize = 100000;
51         size_t used;
52
53         buff = malloc(buffsize);
54
55         /* Create a new archive in memory. */
56         assert((a = archive_write_new()) != NULL);
57         assertEqualIntA(a, 0, archive_write_set_format_cpio(a));
58         assertEqualIntA(a, 0, archive_write_set_compression_none(a));
59         assertEqualIntA(a, 0, archive_write_open_memory(a, buff, buffsize, &used));
60
61         /*
62          * Add various files to it.
63          * TODO: Extend this to cover more filetypes.
64          */
65
66         /* "file" with 10 bytes of content */
67         assert((entry = archive_entry_new()) != NULL);
68         archive_entry_set_mtime(entry, 1, 10);
69         archive_entry_set_pathname(entry, "file");
70         archive_entry_set_mode(entry, S_IFREG | 0664);
71         archive_entry_set_size(entry, 10);
72         archive_entry_set_uid(entry, 80);
73         archive_entry_set_gid(entry, 90);
74         archive_entry_set_dev(entry, 12);
75         archive_entry_set_ino(entry, 89);
76         archive_entry_set_nlink(entry, 2);
77         assertEqualIntA(a, 0, archive_write_header(a, entry));
78         archive_entry_free(entry);
79         assertEqualIntA(a, 10, archive_write_data(a, "1234567890", 10));
80
81         /* Hardlink to "file" with 10 bytes of content */
82         assert((entry = archive_entry_new()) != NULL);
83         archive_entry_set_mtime(entry, 1, 10);
84         archive_entry_set_pathname(entry, "linkfile");
85         archive_entry_set_mode(entry, S_IFREG | 0664);
86         archive_entry_set_size(entry, 10);
87         archive_entry_set_uid(entry, 80);
88         archive_entry_set_gid(entry, 90);
89         archive_entry_set_dev(entry, 12);
90         archive_entry_set_ino(entry, 89);
91         archive_entry_set_nlink(entry, 2);
92         assertEqualIntA(a, 0, archive_write_header(a, entry));
93         archive_entry_free(entry);
94         assertEqualIntA(a, 10, archive_write_data(a, "1234567890", 10));
95
96         /* "dir" */
97         assert((entry = archive_entry_new()) != NULL);
98         archive_entry_set_mtime(entry, 2, 20);
99         archive_entry_set_pathname(entry, "dir");
100         archive_entry_set_mode(entry, S_IFDIR | 0775);
101         archive_entry_set_size(entry, 10);
102         archive_entry_set_nlink(entry, 2);
103         assertEqualIntA(a, 0, archive_write_header(a, entry));
104         archive_entry_free(entry);
105         /* Write of data to dir should fail == zero bytes get written. */
106         assertEqualIntA(a, 0, archive_write_data(a, "1234567890", 10));
107
108         /* "symlink" pointing to "file" */
109         assert((entry = archive_entry_new()) != NULL);
110         archive_entry_set_mtime(entry, 3, 30);
111         archive_entry_set_pathname(entry, "symlink");
112         archive_entry_set_mode(entry, S_IFLNK | 0664);
113         archive_entry_set_symlink(entry,"file");
114         archive_entry_set_size(entry, 0);
115         archive_entry_set_uid(entry, 88);
116         archive_entry_set_gid(entry, 98);
117         archive_entry_set_dev(entry, 12);
118         archive_entry_set_ino(entry, 90);
119         archive_entry_set_nlink(entry, 1);
120         assertEqualIntA(a, 0, archive_write_header(a, entry));
121         archive_entry_free(entry);
122         /* Write of data to symlink should fail == zero bytes get written. */
123         assertEqualIntA(a, 0, archive_write_data(a, "1234567890", 10));
124
125 #if ARCHIVE_VERSION_NUMBER < 2000000
126         archive_write_finish(a);
127 #else
128         assert(0 == archive_write_finish(a));
129 #endif
130
131         /*
132          * Verify the archive format.
133          */
134         e = buff;
135
136         /* "file" */
137         assert(is_octal(e, 76)); /* Entire header is octal digits. */
138         assertEqualMem(e + 0, "070707", 6); /* Magic */
139         assertEqualMem(e + 6, "000014", 6); /* dev */
140         assertEqualMem(e + 12, "000131", 6); /* ino */
141         assertEqualMem(e + 18, "100664", 6); /* Mode */
142         assertEqualMem(e + 24, "000120", 6); /* uid */
143         assertEqualMem(e + 30, "000132", 6); /* gid */
144         assertEqualMem(e + 36, "000002", 6); /* nlink */
145         assertEqualMem(e + 42, "000000", 6); /* rdev */
146         assertEqualMem(e + 48, "00000000001", 11); /* mtime */
147         assertEqualMem(e + 59, "000005", 6); /* Name size */
148         assertEqualMem(e + 65, "00000000012", 11); /* File size */
149         assertEqualMem(e + 76, "file\0", 5); /* Name contents */
150         assertEqualMem(e + 81, "1234567890", 10); /* File contents */
151         e += 91;
152
153         /* hardlink to "file" */
154         assert(is_octal(e, 76)); /* Entire header is octal digits. */
155         assertEqualMem(e + 0, "070707", 6); /* Magic */
156         assertEqualMem(e + 6, "000014", 6); /* dev */
157         assertEqualMem(e + 12, "000131", 6); /* ino */
158         assertEqualMem(e + 18, "100664", 6); /* Mode */
159         assertEqualMem(e + 24, "000120", 6); /* uid */
160         assertEqualMem(e + 30, "000132", 6); /* gid */
161         assertEqualMem(e + 36, "000002", 6); /* nlink */
162         assertEqualMem(e + 42, "000000", 6); /* rdev */
163         assertEqualMem(e + 48, "00000000001", 11); /* mtime */
164         assertEqualMem(e + 59, "000011", 6); /* Name size */
165         assertEqualMem(e + 65, "00000000012", 11); /* File size */
166         assertEqualMem(e + 76, "linkfile\0", 9); /* Name contents */
167         assertEqualMem(e + 85, "1234567890", 10); /* File contents */
168         e += 95;
169
170         /* "dir" */
171         assert(is_octal(e, 76));
172         assertEqualMem(e + 0, "070707", 6); /* Magic */
173         assertEqualMem(e + 6, "000000", 6); /* dev */
174         assertEqualMem(e + 12, "000000", 6); /* ino */
175         assertEqualMem(e + 18, "040775", 6); /* Mode */
176         assertEqualMem(e + 24, "000000", 6); /* uid */
177         assertEqualMem(e + 30, "000000", 6); /* gid */
178         assertEqualMem(e + 36, "000002", 6); /* Nlink */
179         assertEqualMem(e + 42, "000000", 6); /* rdev */
180         assertEqualMem(e + 48, "00000000002", 11); /* mtime */
181         assertEqualMem(e + 59, "000004", 6); /* Name size */
182         assertEqualMem(e + 65, "00000000000", 11); /* File size */
183         assertEqualMem(e + 76, "dir\0", 4); /* name */
184         e += 80;
185
186         /* "symlink" pointing to "file" */
187         assert(is_octal(e, 76)); /* Entire header is octal digits. */
188         assertEqualMem(e + 0, "070707", 6); /* Magic */
189         assertEqualMem(e + 6, "000014", 6); /* dev */
190         assertEqualMem(e + 12, "000132", 6); /* ino */
191         assertEqualMem(e + 18, "120664", 6); /* Mode */
192         assertEqualMem(e + 24, "000130", 6); /* uid */
193         assertEqualMem(e + 30, "000142", 6); /* gid */
194         assertEqualMem(e + 36, "000001", 6); /* nlink */
195         assertEqualMem(e + 42, "000000", 6); /* rdev */
196         assertEqualMem(e + 48, "00000000003", 11); /* mtime */
197         assertEqualMem(e + 59, "000010", 6); /* Name size */
198         assertEqualMem(e + 65, "00000000004", 11); /* File size */
199         assertEqualMem(e + 76, "symlink\0", 8); /* Name contents */
200         assertEqualMem(e + 84, "file", 4); /* File contents == link target */
201         e += 88;
202
203         /* TODO: Verify other types of entries. */
204
205         /* Last entry is end-of-archive marker. */
206         assert(is_octal(e, 76));
207         assertEqualMem(e + 0, "070707", 6); /* Magic */
208         assertEqualMem(e + 6, "000000", 6); /* dev */
209         assertEqualMem(e + 12, "000000", 6); /* ino */
210         assertEqualMem(e + 18, "000000", 6); /* Mode */
211         assertEqualMem(e + 24, "000000", 6); /* uid */
212         assertEqualMem(e + 30, "000000", 6); /* gid */
213         assertEqualMem(e + 36, "000001", 6); /* Nlink */
214         assertEqualMem(e + 42, "000000", 6); /* rdev */
215         assertEqualMem(e + 48, "00000000000", 11); /* mtime */
216         assertEqualMem(e + 59, "000013", 6); /* Name size */
217         assertEqualMem(e + 65, "00000000000", 11); /* File size */
218         assertEqualMem(e + 76, "TRAILER!!!\0", 11); /* Name */
219         e += 87;
220
221         assertEqualInt((int)used, e - buff);
222
223         free(buff);
224 }