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$");
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));
58248616Smm	assertEqualIntA(a, 0, archive_write_add_filter_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
126232153Smm	assertEqualInt(ARCHIVE_OK, archive_write_free(a));
127228753Smm
128228753Smm	/*
129228753Smm	 * Verify the archive format.
130228753Smm	 *
131228753Smm	 * Notes on the ino validation: cpio does not actually require
132228753Smm	 * that the ino values written to the archive match those read
133228753Smm	 * from disk.  It really requires that:
134228753Smm	 *   * matching non-zero ino values be written as matching
135228753Smm	 *     non-zero values
136228753Smm	 *   * non-matching non-zero ino values be written as non-matching
137228753Smm	 *     non-zero values
138228753Smm	 * Libarchive further ensures that zero ino values get written
139228753Smm	 * as zeroes.  This allows the cpio writer to generate
140228753Smm	 * synthetic ino values for the archive that may be different
141228753Smm	 * than those on disk in order to avoid problems due to truncation.
142228753Smm	 * This is especially needed for odc (POSIX format) that
143228753Smm	 * only supports 18-bit ino values.
144228753Smm	 */
145228753Smm	e = buff;
146228753Smm
147228753Smm	/* "file" */
148228753Smm	file = e; /* Remember where this starts... */
149228753Smm	assert(is_octal(e, 76)); /* Entire header is octal digits. */
150228753Smm	assertEqualMem(e + 0, "070707", 6); /* Magic */
151228753Smm	assertEqualMem(e + 6, "000014", 6); /* dev */
152228753Smm	assert(memcmp(e + 12, "000000", 6) != 0); /* ino must be != 0 */
153228753Smm	assertEqualMem(e + 18, "100664", 6); /* Mode */
154228753Smm	assertEqualMem(e + 24, "000120", 6); /* uid */
155228753Smm	assertEqualMem(e + 30, "000132", 6); /* gid */
156228753Smm	assertEqualMem(e + 36, "000002", 6); /* nlink */
157228753Smm	assertEqualMem(e + 42, "000000", 6); /* rdev */
158228753Smm	assertEqualMem(e + 48, "00000000001", 11); /* mtime */
159228753Smm	assertEqualMem(e + 59, "000005", 6); /* Name size */
160228753Smm	assertEqualMem(e + 65, "00000000012", 11); /* File size */
161228753Smm	assertEqualMem(e + 76, "file\0", 5); /* Name contents */
162228753Smm	assertEqualMem(e + 81, "1234567890", 10); /* File contents */
163228753Smm	e += 91;
164228753Smm
165228753Smm	/* hardlink to "file" */
166228753Smm	assert(is_octal(e, 76)); /* Entire header is octal digits. */
167228753Smm	assertEqualMem(e + 0, "070707", 6); /* Magic */
168228753Smm	assertEqualMem(e + 6, "000014", 6); /* dev */
169228753Smm	assertEqualMem(e + 12, file + 12, 6); /* ino must match above */
170228753Smm	assertEqualMem(e + 18, "100664", 6); /* Mode */
171228753Smm	assertEqualMem(e + 24, "000120", 6); /* uid */
172228753Smm	assertEqualMem(e + 30, "000132", 6); /* gid */
173228753Smm	assertEqualMem(e + 36, "000002", 6); /* nlink */
174228753Smm	assertEqualMem(e + 42, "000000", 6); /* rdev */
175228753Smm	assertEqualMem(e + 48, "00000000001", 11); /* mtime */
176228753Smm	assertEqualMem(e + 59, "000011", 6); /* Name size */
177228753Smm	assertEqualMem(e + 65, "00000000012", 11); /* File size */
178228753Smm	assertEqualMem(e + 76, "linkfile\0", 9); /* Name contents */
179228753Smm	assertEqualMem(e + 85, "1234567890", 10); /* File contents */
180228753Smm	e += 95;
181228753Smm
182228753Smm	/* "dir" */
183228753Smm	assert(is_octal(e, 76));
184228753Smm	assertEqualMem(e + 0, "070707", 6); /* Magic */
185228753Smm	assertEqualMem(e + 6, "000000", 6); /* dev */
186228753Smm	assertEqualMem(e + 12, "000000", 6); /* ino */
187228753Smm	assertEqualMem(e + 18, "040775", 6); /* Mode */
188228753Smm	assertEqualMem(e + 24, "000000", 6); /* uid */
189228753Smm	assertEqualMem(e + 30, "000000", 6); /* gid */
190228753Smm	assertEqualMem(e + 36, "000002", 6); /* Nlink */
191228753Smm	assertEqualMem(e + 42, "000000", 6); /* rdev */
192228753Smm	assertEqualMem(e + 48, "00000000002", 11); /* mtime */
193228753Smm	assertEqualMem(e + 59, "000004", 6); /* Name size */
194228753Smm	assertEqualMem(e + 65, "00000000000", 11); /* File size */
195228753Smm	assertEqualMem(e + 76, "dir\0", 4); /* name */
196228753Smm	e += 80;
197228753Smm
198228753Smm	/* "symlink" pointing to "file" */
199228753Smm	assert(is_octal(e, 76)); /* Entire header is octal digits. */
200228753Smm	assertEqualMem(e + 0, "070707", 6); /* Magic */
201228753Smm	assertEqualMem(e + 6, "000014", 6); /* dev */
202228753Smm	assert(memcmp(e + 12, file + 12, 6) != 0); /* ino must != file ino */
203228753Smm	assert(memcmp(e + 12, "000000", 6) != 0); /* ino must != 0 */
204228753Smm	assertEqualMem(e + 18, "120664", 6); /* Mode */
205228753Smm	assertEqualMem(e + 24, "000130", 6); /* uid */
206228753Smm	assertEqualMem(e + 30, "000142", 6); /* gid */
207228753Smm	assertEqualMem(e + 36, "000001", 6); /* nlink */
208228753Smm	assertEqualMem(e + 42, "000000", 6); /* rdev */
209228753Smm	assertEqualMem(e + 48, "00000000003", 11); /* mtime */
210228753Smm	assertEqualMem(e + 59, "000010", 6); /* Name size */
211228753Smm	assertEqualMem(e + 65, "00000000004", 11); /* File size */
212228753Smm	assertEqualMem(e + 76, "symlink\0", 8); /* Name contents */
213228753Smm	assertEqualMem(e + 84, "file", 4); /* File contents == link target */
214228753Smm	e += 88;
215228753Smm
216228753Smm	/* TODO: Verify other types of entries. */
217228753Smm
218228753Smm	/* Last entry is end-of-archive marker. */
219228753Smm	assert(is_octal(e, 76));
220228753Smm	assertEqualMem(e + 0, "070707", 6); /* Magic */
221228753Smm	assertEqualMem(e + 6, "000000", 6); /* dev */
222228753Smm	assertEqualMem(e + 12, "000000", 6); /* ino */
223228753Smm	assertEqualMem(e + 18, "000000", 6); /* Mode */
224228753Smm	assertEqualMem(e + 24, "000000", 6); /* uid */
225228753Smm	assertEqualMem(e + 30, "000000", 6); /* gid */
226228753Smm	assertEqualMem(e + 36, "000001", 6); /* Nlink */
227228753Smm	assertEqualMem(e + 42, "000000", 6); /* rdev */
228228753Smm	assertEqualMem(e + 48, "00000000000", 11); /* mtime */
229228753Smm	assertEqualMem(e + 59, "000013", 6); /* Name size */
230228753Smm	assertEqualMem(e + 65, "00000000000", 11); /* File size */
231228753Smm	assertEqualMem(e + 76, "TRAILER!!!\0", 11); /* Name */
232228753Smm	e += 87;
233228753Smm
234228753Smm	assertEqualInt((int)used, e - buff);
235228753Smm
236228753Smm	free(buff);
237228753Smm}
238