test_write_format_cpio_odc.c revision 228763
1228753Smm/*-
2228753Smm * Copyright (c) 2003-2007 Tim Kientzle
3228753Smm * All rights reserved.
4228753Smm *
5228753Smm * Redistribution and use in source and binary forms, with or without
6228753Smm * modification, are permitted provided that the following conditions
7228753Smm * are met:
8228753Smm * 1. Redistributions of source code must retain the above copyright
9228753Smm *    notice, this list of conditions and the following disclaimer.
10228753Smm * 2. Redistributions in binary form must reproduce the above copyright
11228753Smm *    notice, this list of conditions and the following disclaimer in the
12228753Smm *    documentation and/or other materials provided with the distribution.
13228753Smm *
14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24228753Smm */
25228753Smm#include "test.h"
26228763Smm__FBSDID("$FreeBSD: head/contrib/libarchive/libarchive/test/test_write_format_cpio_odc.c 228763 2011-12-21 11:13:29Z mm $");
27228753Smm
28228753Smm
29228753Smmstatic int
30228753Smmis_octal(const char *p, size_t l)
31228753Smm{
32228753Smm	while (l > 0) {
33228753Smm		if (*p < '0' || *p > '7')
34228753Smm			return (0);
35228753Smm		--l;
36228753Smm		++p;
37228753Smm	}
38228753Smm	return (1);
39228753Smm}
40228753Smm
41228753Smm/*
42228753Smm * Detailed verification that cpio 'odc' archives are written with
43228753Smm * the correct format.
44228753Smm */
45228753SmmDEFINE_TEST(test_write_format_cpio_odc)
46228753Smm{
47228753Smm	struct archive *a;
48228753Smm	struct archive_entry *entry;
49228753Smm	char *buff, *e, *file;
50228753Smm	size_t buffsize = 100000;
51228753Smm	size_t used;
52228753Smm
53228753Smm	buff = malloc(buffsize);
54228753Smm
55228753Smm	/* Create a new archive in memory. */
56228753Smm	assert((a = archive_write_new()) != NULL);
57228753Smm	assertEqualIntA(a, 0, archive_write_set_format_cpio(a));
58228753Smm	assertEqualIntA(a, 0, archive_write_set_compression_none(a));
59228753Smm	assertEqualIntA(a, 0, archive_write_open_memory(a, buff, buffsize, &used));
60228753Smm
61228753Smm	/*
62228753Smm	 * Add various files to it.
63228753Smm	 * TODO: Extend this to cover more filetypes.
64228753Smm	 */
65228753Smm
66228753Smm	/* "file" with 10 bytes of content */
67228753Smm	assert((entry = archive_entry_new()) != NULL);
68228753Smm	archive_entry_set_mtime(entry, 1, 10);
69228753Smm	archive_entry_set_pathname(entry, "file");
70228753Smm	archive_entry_set_mode(entry, S_IFREG | 0664);
71228753Smm	archive_entry_set_size(entry, 10);
72228753Smm	archive_entry_set_uid(entry, 80);
73228753Smm	archive_entry_set_gid(entry, 90);
74228753Smm	archive_entry_set_dev(entry, 12);
75228753Smm	archive_entry_set_ino(entry, 89);
76228753Smm	archive_entry_set_nlink(entry, 2);
77228753Smm	assertEqualIntA(a, 0, archive_write_header(a, entry));
78228753Smm	archive_entry_free(entry);
79228753Smm	assertEqualIntA(a, 10, archive_write_data(a, "1234567890", 10));
80228753Smm
81228753Smm	/* Hardlink to "file" with 10 bytes of content */
82228753Smm	assert((entry = archive_entry_new()) != NULL);
83228753Smm	archive_entry_set_mtime(entry, 1, 10);
84228753Smm	archive_entry_set_pathname(entry, "linkfile");
85228753Smm	archive_entry_set_mode(entry, S_IFREG | 0664);
86228753Smm	archive_entry_set_size(entry, 10);
87228753Smm	archive_entry_set_uid(entry, 80);
88228753Smm	archive_entry_set_gid(entry, 90);
89228753Smm	archive_entry_set_dev(entry, 12);
90228753Smm	archive_entry_set_ino(entry, 89);
91228753Smm	archive_entry_set_nlink(entry, 2);
92228753Smm	assertEqualIntA(a, 0, archive_write_header(a, entry));
93228753Smm	archive_entry_free(entry);
94228753Smm	assertEqualIntA(a, 10, archive_write_data(a, "1234567890", 10));
95228753Smm
96228753Smm	/* "dir" */
97228753Smm	assert((entry = archive_entry_new()) != NULL);
98228753Smm	archive_entry_set_mtime(entry, 2, 20);
99228753Smm	archive_entry_set_pathname(entry, "dir");
100228753Smm	archive_entry_set_mode(entry, S_IFDIR | 0775);
101228753Smm	archive_entry_set_size(entry, 10);
102228753Smm	archive_entry_set_nlink(entry, 2);
103228753Smm	assertEqualIntA(a, 0, archive_write_header(a, entry));
104228753Smm	archive_entry_free(entry);
105228753Smm	/* Write of data to dir should fail == zero bytes get written. */
106228753Smm	assertEqualIntA(a, 0, archive_write_data(a, "1234567890", 10));
107228753Smm
108228753Smm	/* "symlink" pointing to "file" */
109228753Smm	assert((entry = archive_entry_new()) != NULL);
110228753Smm	archive_entry_set_mtime(entry, 3, 30);
111228753Smm	archive_entry_set_pathname(entry, "symlink");
112228753Smm	archive_entry_set_mode(entry, 0664);
113228753Smm	archive_entry_set_filetype(entry, AE_IFLNK);
114228753Smm	archive_entry_set_symlink(entry,"file");
115228753Smm	archive_entry_set_size(entry, 0);
116228753Smm	archive_entry_set_uid(entry, 88);
117228753Smm	archive_entry_set_gid(entry, 98);
118228753Smm	archive_entry_set_dev(entry, 12);
119228753Smm	archive_entry_set_ino(entry, 90);
120228753Smm	archive_entry_set_nlink(entry, 1);
121228753Smm	assertEqualIntA(a, 0, archive_write_header(a, entry));
122228753Smm	archive_entry_free(entry);
123228753Smm	/* Write of data to symlink should fail == zero bytes get written. */
124228753Smm	assertEqualIntA(a, 0, archive_write_data(a, "1234567890", 10));
125228753Smm
126228753Smm#if ARCHIVE_VERSION_NUMBER < 2000000
127228753Smm	archive_write_finish(a);
128228753Smm#else
129228753Smm	assert(0 == archive_write_finish(a));
130228753Smm#endif
131228753Smm
132228753Smm	/*
133228753Smm	 * Verify the archive format.
134228753Smm	 *
135228753Smm	 * Notes on the ino validation: cpio does not actually require
136228753Smm	 * that the ino values written to the archive match those read
137228753Smm	 * from disk.  It really requires that:
138228753Smm	 *   * matching non-zero ino values be written as matching
139228753Smm	 *     non-zero values
140228753Smm	 *   * non-matching non-zero ino values be written as non-matching
141228753Smm	 *     non-zero values
142228753Smm	 * Libarchive further ensures that zero ino values get written
143228753Smm	 * as zeroes.  This allows the cpio writer to generate
144228753Smm	 * synthetic ino values for the archive that may be different
145228753Smm	 * than those on disk in order to avoid problems due to truncation.
146228753Smm	 * This is especially needed for odc (POSIX format) that
147228753Smm	 * only supports 18-bit ino values.
148228753Smm	 */
149228753Smm	e = buff;
150228753Smm
151228753Smm	/* "file" */
152228753Smm	file = e; /* Remember where this starts... */
153228753Smm	assert(is_octal(e, 76)); /* Entire header is octal digits. */
154228753Smm	assertEqualMem(e + 0, "070707", 6); /* Magic */
155228753Smm	assertEqualMem(e + 6, "000014", 6); /* dev */
156228753Smm	assert(memcmp(e + 12, "000000", 6) != 0); /* ino must be != 0 */
157228753Smm	assertEqualMem(e + 18, "100664", 6); /* Mode */
158228753Smm	assertEqualMem(e + 24, "000120", 6); /* uid */
159228753Smm	assertEqualMem(e + 30, "000132", 6); /* gid */
160228753Smm	assertEqualMem(e + 36, "000002", 6); /* nlink */
161228753Smm	assertEqualMem(e + 42, "000000", 6); /* rdev */
162228753Smm	assertEqualMem(e + 48, "00000000001", 11); /* mtime */
163228753Smm	assertEqualMem(e + 59, "000005", 6); /* Name size */
164228753Smm	assertEqualMem(e + 65, "00000000012", 11); /* File size */
165228753Smm	assertEqualMem(e + 76, "file\0", 5); /* Name contents */
166228753Smm	assertEqualMem(e + 81, "1234567890", 10); /* File contents */
167228753Smm	e += 91;
168228753Smm
169228753Smm	/* hardlink to "file" */
170228753Smm	assert(is_octal(e, 76)); /* Entire header is octal digits. */
171228753Smm	assertEqualMem(e + 0, "070707", 6); /* Magic */
172228753Smm	assertEqualMem(e + 6, "000014", 6); /* dev */
173228753Smm	assertEqualMem(e + 12, file + 12, 6); /* ino must match above */
174228753Smm	assertEqualMem(e + 18, "100664", 6); /* Mode */
175228753Smm	assertEqualMem(e + 24, "000120", 6); /* uid */
176228753Smm	assertEqualMem(e + 30, "000132", 6); /* gid */
177228753Smm	assertEqualMem(e + 36, "000002", 6); /* nlink */
178228753Smm	assertEqualMem(e + 42, "000000", 6); /* rdev */
179228753Smm	assertEqualMem(e + 48, "00000000001", 11); /* mtime */
180228753Smm	assertEqualMem(e + 59, "000011", 6); /* Name size */
181228753Smm	assertEqualMem(e + 65, "00000000012", 11); /* File size */
182228753Smm	assertEqualMem(e + 76, "linkfile\0", 9); /* Name contents */
183228753Smm	assertEqualMem(e + 85, "1234567890", 10); /* File contents */
184228753Smm	e += 95;
185228753Smm
186228753Smm	/* "dir" */
187228753Smm	assert(is_octal(e, 76));
188228753Smm	assertEqualMem(e + 0, "070707", 6); /* Magic */
189228753Smm	assertEqualMem(e + 6, "000000", 6); /* dev */
190228753Smm	assertEqualMem(e + 12, "000000", 6); /* ino */
191228753Smm	assertEqualMem(e + 18, "040775", 6); /* Mode */
192228753Smm	assertEqualMem(e + 24, "000000", 6); /* uid */
193228753Smm	assertEqualMem(e + 30, "000000", 6); /* gid */
194228753Smm	assertEqualMem(e + 36, "000002", 6); /* Nlink */
195228753Smm	assertEqualMem(e + 42, "000000", 6); /* rdev */
196228753Smm	assertEqualMem(e + 48, "00000000002", 11); /* mtime */
197228753Smm	assertEqualMem(e + 59, "000004", 6); /* Name size */
198228753Smm	assertEqualMem(e + 65, "00000000000", 11); /* File size */
199228753Smm	assertEqualMem(e + 76, "dir\0", 4); /* name */
200228753Smm	e += 80;
201228753Smm
202228753Smm	/* "symlink" pointing to "file" */
203228753Smm	assert(is_octal(e, 76)); /* Entire header is octal digits. */
204228753Smm	assertEqualMem(e + 0, "070707", 6); /* Magic */
205228753Smm	assertEqualMem(e + 6, "000014", 6); /* dev */
206228753Smm	assert(memcmp(e + 12, file + 12, 6) != 0); /* ino must != file ino */
207228753Smm	assert(memcmp(e + 12, "000000", 6) != 0); /* ino must != 0 */
208228753Smm	assertEqualMem(e + 18, "120664", 6); /* Mode */
209228753Smm	assertEqualMem(e + 24, "000130", 6); /* uid */
210228753Smm	assertEqualMem(e + 30, "000142", 6); /* gid */
211228753Smm	assertEqualMem(e + 36, "000001", 6); /* nlink */
212228753Smm	assertEqualMem(e + 42, "000000", 6); /* rdev */
213228753Smm	assertEqualMem(e + 48, "00000000003", 11); /* mtime */
214228753Smm	assertEqualMem(e + 59, "000010", 6); /* Name size */
215228753Smm	assertEqualMem(e + 65, "00000000004", 11); /* File size */
216228753Smm	assertEqualMem(e + 76, "symlink\0", 8); /* Name contents */
217228753Smm	assertEqualMem(e + 84, "file", 4); /* File contents == link target */
218228753Smm	e += 88;
219228753Smm
220228753Smm	/* TODO: Verify other types of entries. */
221228753Smm
222228753Smm	/* Last entry is end-of-archive marker. */
223228753Smm	assert(is_octal(e, 76));
224228753Smm	assertEqualMem(e + 0, "070707", 6); /* Magic */
225228753Smm	assertEqualMem(e + 6, "000000", 6); /* dev */
226228753Smm	assertEqualMem(e + 12, "000000", 6); /* ino */
227228753Smm	assertEqualMem(e + 18, "000000", 6); /* Mode */
228228753Smm	assertEqualMem(e + 24, "000000", 6); /* uid */
229228753Smm	assertEqualMem(e + 30, "000000", 6); /* gid */
230228753Smm	assertEqualMem(e + 36, "000001", 6); /* Nlink */
231228753Smm	assertEqualMem(e + 42, "000000", 6); /* rdev */
232228753Smm	assertEqualMem(e + 48, "00000000000", 11); /* mtime */
233228753Smm	assertEqualMem(e + 59, "000013", 6); /* Name size */
234228753Smm	assertEqualMem(e + 65, "00000000000", 11); /* File size */
235228753Smm	assertEqualMem(e + 76, "TRAILER!!!\0", 11); /* Name */
236228753Smm	e += 87;
237228753Smm
238228753Smm	assertEqualInt((int)used, e - buff);
239228753Smm
240228753Smm	free(buff);
241228753Smm}
242