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: head/lib/libarchive/test/test_write_format_cpio_odc.c 201247 2009-12-30 05:59:21Z kientzle $");
30
is_octal(const char *p, size_t l)
33
if (*p < '0' || *p > '7')
42
* Detailed verification that cpio 'odc' archives are written with
45
DEFINE_TEST(test_write_format_cpio_odc)
48
struct archive_entry *entry;
49
char *buff, *e, *file;
50
size_t buffsize = 100000;
53
buff = malloc(buffsize);
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));
62
* Add various files to it.
63
* TODO: Extend this to cover more filetypes.
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));
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));
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));
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, 0664);
113
archive_entry_set_filetype(entry, AE_IFLNK);
114
archive_entry_set_symlink(entry,"file");
115
archive_entry_set_size(entry, 0);
116
archive_entry_set_uid(entry, 88);
117
archive_entry_set_gid(entry, 98);
118
archive_entry_set_dev(entry, 12);
119
archive_entry_set_ino(entry, 90);
120
archive_entry_set_nlink(entry, 1);
121
assertEqualIntA(a, 0, archive_write_header(a, entry));
122
archive_entry_free(entry);
123
/* Write of data to symlink should fail == zero bytes get written. */
124
assertEqualIntA(a, 0, archive_write_data(a, "1234567890", 10));
126
#if ARCHIVE_VERSION_NUMBER < 2000000
127
archive_write_finish(a);
129
assert(0 == archive_write_finish(a));
133
* Verify the archive format.
135
* Notes on the ino validation: cpio does not actually require
136
* that the ino values written to the archive match those read
137
* from disk. It really requires that:
138
* * matching non-zero ino values be written as matching
140
* * non-matching non-zero ino values be written as non-matching
142
* Libarchive further ensures that zero ino values get written
143
* as zeroes. This allows the cpio writer to generate
144
* synthetic ino values for the archive that may be different
145
* than those on disk in order to avoid problems due to truncation.
146
* This is especially needed for odc (POSIX format) that
147
* only supports 18-bit ino values.
152
file = e; /* Remember where this starts... */
153
assert(is_octal(e, 76)); /* Entire header is octal digits. */
154
assertEqualMem(e + 0, "070707", 6); /* Magic */
155
assertEqualMem(e + 6, "000014", 6); /* dev */
156
assert(memcmp(e + 12, "000000", 6) != 0); /* ino must be != 0 */
157
assertEqualMem(e + 18, "100664", 6); /* Mode */
158
assertEqualMem(e + 24, "000120", 6); /* uid */
159
assertEqualMem(e + 30, "000132", 6); /* gid */
160
assertEqualMem(e + 36, "000002", 6); /* nlink */
161
assertEqualMem(e + 42, "000000", 6); /* rdev */
162
assertEqualMem(e + 48, "00000000001", 11); /* mtime */
163
assertEqualMem(e + 59, "000005", 6); /* Name size */
164
assertEqualMem(e + 65, "00000000012", 11); /* File size */
165
assertEqualMem(e + 76, "file\0", 5); /* Name contents */
166
assertEqualMem(e + 81, "1234567890", 10); /* File contents */
169
/* hardlink to "file" */
170
assert(is_octal(e, 76)); /* Entire header is octal digits. */
171
assertEqualMem(e + 0, "070707", 6); /* Magic */
172
assertEqualMem(e + 6, "000014", 6); /* dev */
173
assertEqualMem(e + 12, file + 12, 6); /* ino must match above */
174
assertEqualMem(e + 18, "100664", 6); /* Mode */
175
assertEqualMem(e + 24, "000120", 6); /* uid */
176
assertEqualMem(e + 30, "000132", 6); /* gid */
177
assertEqualMem(e + 36, "000002", 6); /* nlink */
178
assertEqualMem(e + 42, "000000", 6); /* rdev */
179
assertEqualMem(e + 48, "00000000001", 11); /* mtime */
180
assertEqualMem(e + 59, "000011", 6); /* Name size */
181
assertEqualMem(e + 65, "00000000012", 11); /* File size */
182
assertEqualMem(e + 76, "linkfile\0", 9); /* Name contents */
183
assertEqualMem(e + 85, "1234567890", 10); /* File contents */
187
assert(is_octal(e, 76));
188
assertEqualMem(e + 0, "070707", 6); /* Magic */
189
assertEqualMem(e + 6, "000000", 6); /* dev */
190
assertEqualMem(e + 12, "000000", 6); /* ino */
191
assertEqualMem(e + 18, "040775", 6); /* Mode */
192
assertEqualMem(e + 24, "000000", 6); /* uid */
193
assertEqualMem(e + 30, "000000", 6); /* gid */
194
assertEqualMem(e + 36, "000002", 6); /* Nlink */
195
assertEqualMem(e + 42, "000000", 6); /* rdev */
196
assertEqualMem(e + 48, "00000000002", 11); /* mtime */
197
assertEqualMem(e + 59, "000004", 6); /* Name size */
198
assertEqualMem(e + 65, "00000000000", 11); /* File size */
199
assertEqualMem(e + 76, "dir\0", 4); /* name */
202
/* "symlink" pointing to "file" */
203
assert(is_octal(e, 76)); /* Entire header is octal digits. */
204
assertEqualMem(e + 0, "070707", 6); /* Magic */
205
assertEqualMem(e + 6, "000014", 6); /* dev */
206
assert(memcmp(e + 12, file + 12, 6) != 0); /* ino must != file ino */
207
assert(memcmp(e + 12, "000000", 6) != 0); /* ino must != 0 */
208
assertEqualMem(e + 18, "120664", 6); /* Mode */
209
assertEqualMem(e + 24, "000130", 6); /* uid */
210
assertEqualMem(e + 30, "000142", 6); /* gid */
211
assertEqualMem(e + 36, "000001", 6); /* nlink */
212
assertEqualMem(e + 42, "000000", 6); /* rdev */
213
assertEqualMem(e + 48, "00000000003", 11); /* mtime */
214
assertEqualMem(e + 59, "000010", 6); /* Name size */
215
assertEqualMem(e + 65, "00000000004", 11); /* File size */
216
assertEqualMem(e + 76, "symlink\0", 8); /* Name contents */
217
assertEqualMem(e + 84, "file", 4); /* File contents == link target */
220
/* TODO: Verify other types of entries. */
222
/* Last entry is end-of-archive marker. */
223
assert(is_octal(e, 76));
224
assertEqualMem(e + 0, "070707", 6); /* Magic */
225
assertEqualMem(e + 6, "000000", 6); /* dev */
226
assertEqualMem(e + 12, "000000", 6); /* ino */
227
assertEqualMem(e + 18, "000000", 6); /* Mode */
228
assertEqualMem(e + 24, "000000", 6); /* uid */
229
assertEqualMem(e + 30, "000000", 6); /* gid */
230
assertEqualMem(e + 36, "000001", 6); /* Nlink */
231
assertEqualMem(e + 42, "000000", 6); /* rdev */
232
assertEqualMem(e + 48, "00000000000", 11); /* mtime */
233
assertEqualMem(e + 59, "000013", 6); /* Name size */
234
assertEqualMem(e + 65, "00000000000", 11); /* File size */
235
assertEqualMem(e + 76, "TRAILER!!!\0", 11); /* Name */
238
assertEqualInt((int)used, e - buff);