test_write_format_cpio_odc.c revision 370535
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: stable/11/contrib/libarchive/libarchive/test/test_write_format_cpio_odc.c 370535 2021-09-10 08:34:36Z git2svn $");
27
28
29static int
30is_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 */
45DEFINE_TEST(test_write_format_cpio_odc)
46{
47	struct archive *a;
48	struct archive_entry *entry;
49	char *buff, *e, *file;
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_odc(a));
58	assertEqualIntA(a, 0, archive_write_add_filter_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, 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));
125
126	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
127
128	/*
129	 * Verify the archive format.
130	 *
131	 * Notes on the ino validation: cpio does not actually require
132	 * that the ino values written to the archive match those read
133	 * from disk.  It really requires that:
134	 *   * matching non-zero ino values be written as matching
135	 *     non-zero values
136	 *   * non-matching non-zero ino values be written as non-matching
137	 *     non-zero values
138	 * Libarchive further ensures that zero ino values get written
139	 * as zeroes.  This allows the cpio writer to generate
140	 * synthetic ino values for the archive that may be different
141	 * than those on disk in order to avoid problems due to truncation.
142	 * This is especially needed for odc (POSIX format) that
143	 * only supports 18-bit ino values.
144	 */
145	e = buff;
146
147	/* "file" */
148	file = e; /* Remember where this starts... */
149	assert(is_octal(e, 76)); /* Entire header is octal digits. */
150	assertEqualMem(e + 0, "070707", 6); /* Magic */
151	assertEqualMem(e + 6, "000014", 6); /* dev */
152	assert(memcmp(e + 12, "000000", 6) != 0); /* ino must be != 0 */
153	assertEqualMem(e + 18, "100664", 6); /* Mode */
154	assertEqualMem(e + 24, "000120", 6); /* uid */
155	assertEqualMem(e + 30, "000132", 6); /* gid */
156	assertEqualMem(e + 36, "000002", 6); /* nlink */
157	assertEqualMem(e + 42, "000000", 6); /* rdev */
158	assertEqualMem(e + 48, "00000000001", 11); /* mtime */
159	assertEqualMem(e + 59, "000005", 6); /* Name size */
160	assertEqualMem(e + 65, "00000000012", 11); /* File size */
161	assertEqualMem(e + 76, "file\0", 5); /* Name contents */
162	assertEqualMem(e + 81, "1234567890", 10); /* File contents */
163	e += 91;
164
165	/* hardlink to "file" */
166	assert(is_octal(e, 76)); /* Entire header is octal digits. */
167	assertEqualMem(e + 0, "070707", 6); /* Magic */
168	assertEqualMem(e + 6, "000014", 6); /* dev */
169	assertEqualMem(e + 12, file + 12, 6); /* ino must match above */
170	assertEqualMem(e + 18, "100664", 6); /* Mode */
171	assertEqualMem(e + 24, "000120", 6); /* uid */
172	assertEqualMem(e + 30, "000132", 6); /* gid */
173	assertEqualMem(e + 36, "000002", 6); /* nlink */
174	assertEqualMem(e + 42, "000000", 6); /* rdev */
175	assertEqualMem(e + 48, "00000000001", 11); /* mtime */
176	assertEqualMem(e + 59, "000011", 6); /* Name size */
177	assertEqualMem(e + 65, "00000000012", 11); /* File size */
178	assertEqualMem(e + 76, "linkfile\0", 9); /* Name contents */
179	assertEqualMem(e + 85, "1234567890", 10); /* File contents */
180	e += 95;
181
182	/* "dir" */
183	assert(is_octal(e, 76));
184	assertEqualMem(e + 0, "070707", 6); /* Magic */
185	assertEqualMem(e + 6, "000000", 6); /* dev */
186	assertEqualMem(e + 12, "000000", 6); /* ino */
187	assertEqualMem(e + 18, "040775", 6); /* Mode */
188	assertEqualMem(e + 24, "000000", 6); /* uid */
189	assertEqualMem(e + 30, "000000", 6); /* gid */
190	assertEqualMem(e + 36, "000002", 6); /* Nlink */
191	assertEqualMem(e + 42, "000000", 6); /* rdev */
192	assertEqualMem(e + 48, "00000000002", 11); /* mtime */
193	assertEqualMem(e + 59, "000004", 6); /* Name size */
194	assertEqualMem(e + 65, "00000000000", 11); /* File size */
195	assertEqualMem(e + 76, "dir\0", 4); /* name */
196	e += 80;
197
198	/* "symlink" pointing to "file" */
199	assert(is_octal(e, 76)); /* Entire header is octal digits. */
200	assertEqualMem(e + 0, "070707", 6); /* Magic */
201	assertEqualMem(e + 6, "000014", 6); /* dev */
202	assert(memcmp(e + 12, file + 12, 6) != 0); /* ino must != file ino */
203	assert(memcmp(e + 12, "000000", 6) != 0); /* ino must != 0 */
204	assertEqualMem(e + 18, "120664", 6); /* Mode */
205	assertEqualMem(e + 24, "000130", 6); /* uid */
206	assertEqualMem(e + 30, "000142", 6); /* gid */
207	assertEqualMem(e + 36, "000001", 6); /* nlink */
208	assertEqualMem(e + 42, "000000", 6); /* rdev */
209	assertEqualMem(e + 48, "00000000003", 11); /* mtime */
210	assertEqualMem(e + 59, "000010", 6); /* Name size */
211	assertEqualMem(e + 65, "00000000004", 11); /* File size */
212	assertEqualMem(e + 76, "symlink\0", 8); /* Name contents */
213	assertEqualMem(e + 84, "file", 4); /* File contents == link target */
214	e += 88;
215
216	/* TODO: Verify other types of entries. */
217
218	/* Last entry is end-of-archive marker. */
219	assert(is_octal(e, 76));
220	assertEqualMem(e + 0, "070707", 6); /* Magic */
221	assertEqualMem(e + 6, "000000", 6); /* dev */
222	assertEqualMem(e + 12, "000000", 6); /* ino */
223	assertEqualMem(e + 18, "000000", 6); /* Mode */
224	assertEqualMem(e + 24, "000000", 6); /* uid */
225	assertEqualMem(e + 30, "000000", 6); /* gid */
226	assertEqualMem(e + 36, "000001", 6); /* Nlink */
227	assertEqualMem(e + 42, "000000", 6); /* rdev */
228	assertEqualMem(e + 48, "00000000000", 11); /* mtime */
229	assertEqualMem(e + 59, "000013", 6); /* Name size */
230	assertEqualMem(e + 65, "00000000000", 11); /* File size */
231	assertEqualMem(e + 76, "TRAILER!!!\0", 11); /* Name */
232	e += 87;
233
234	assertEqualInt((int)used, e - buff);
235
236	free(buff);
237}
238