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 27 static int 28 is_octal(const char *p, size_t l) 29 { 30 while (l > 0) { 31 if (*p < '0' || *p > '7') 32 return (0); 33 --l; 34 ++p; 35 } 36 return (1); 37 } 38 39 /* 40 * Detailed verification that cpio 'odc' archives are written with 41 * the correct format. 42 */ 43 DEFINE_TEST(test_write_format_cpio_odc) 44 { 45 struct archive *a; 46 struct archive_entry *entry; 47 char *buff, *e, *file; 48 size_t buffsize = 100000; 49 size_t used; 50 51 buff = malloc(buffsize); 52 53 /* Create a new archive in memory. */ 54 assert((a = archive_write_new()) != NULL); 55 assertEqualIntA(a, 0, archive_write_set_format_cpio_odc(a)); 56 assertEqualIntA(a, 0, archive_write_add_filter_none(a)); 57 assertEqualIntA(a, 0, archive_write_open_memory(a, buff, buffsize, &used)); 58 59 /* 60 * Add various files to it. 61 * TODO: Extend this to cover more filetypes. 62 */ 63 64 /* "file" with 10 bytes of content */ 65 assert((entry = archive_entry_new()) != NULL); 66 archive_entry_set_mtime(entry, 1, 10); 67 archive_entry_set_pathname(entry, "file"); 68 archive_entry_set_mode(entry, S_IFREG | 0664); 69 archive_entry_set_size(entry, 10); 70 archive_entry_set_uid(entry, 80); 71 archive_entry_set_gid(entry, 90); 72 archive_entry_set_dev(entry, 12); 73 archive_entry_set_ino(entry, 89); 74 archive_entry_set_nlink(entry, 2); 75 assertEqualIntA(a, 0, archive_write_header(a, entry)); 76 archive_entry_free(entry); 77 assertEqualIntA(a, 10, archive_write_data(a, "1234567890", 10)); 78 79 /* Hardlink to "file" with 10 bytes of content */ 80 assert((entry = archive_entry_new()) != NULL); 81 archive_entry_set_mtime(entry, 1, 10); 82 archive_entry_set_pathname(entry, "linkfile"); 83 archive_entry_set_mode(entry, S_IFREG | 0664); 84 archive_entry_set_size(entry, 10); 85 archive_entry_set_uid(entry, 80); 86 archive_entry_set_gid(entry, 90); 87 archive_entry_set_dev(entry, 12); 88 archive_entry_set_ino(entry, 89); 89 archive_entry_set_nlink(entry, 2); 90 assertEqualIntA(a, 0, archive_write_header(a, entry)); 91 archive_entry_free(entry); 92 assertEqualIntA(a, 10, archive_write_data(a, "1234567890", 10)); 93 94 /* "dir" */ 95 assert((entry = archive_entry_new()) != NULL); 96 archive_entry_set_mtime(entry, 2, 20); 97 archive_entry_set_pathname(entry, "dir"); 98 archive_entry_set_mode(entry, S_IFDIR | 0775); 99 archive_entry_set_size(entry, 10); 100 archive_entry_set_nlink(entry, 2); 101 assertEqualIntA(a, 0, archive_write_header(a, entry)); 102 archive_entry_free(entry); 103 /* Write of data to dir should fail == zero bytes get written. */ 104 assertEqualIntA(a, 0, archive_write_data(a, "1234567890", 10)); 105 106 /* "symlink" pointing to "file" */ 107 assert((entry = archive_entry_new()) != NULL); 108 archive_entry_set_mtime(entry, 3, 30); 109 archive_entry_set_pathname(entry, "symlink"); 110 archive_entry_set_mode(entry, 0664); 111 archive_entry_set_filetype(entry, AE_IFLNK); 112 archive_entry_set_symlink(entry,"file"); 113 archive_entry_set_size(entry, 0); 114 archive_entry_set_uid(entry, 88); 115 archive_entry_set_gid(entry, 98); 116 archive_entry_set_dev(entry, 12); 117 archive_entry_set_ino(entry, 90); 118 archive_entry_set_nlink(entry, 1); 119 assertEqualIntA(a, 0, archive_write_header(a, entry)); 120 archive_entry_free(entry); 121 /* Write of data to symlink should fail == zero bytes get written. */ 122 assertEqualIntA(a, 0, archive_write_data(a, "1234567890", 10)); 123 124 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 125 126 /* 127 * Verify the archive format. 128 * 129 * Notes on the ino validation: cpio does not actually require 130 * that the ino values written to the archive match those read 131 * from disk. It really requires that: 132 * * matching non-zero ino values be written as matching 133 * non-zero values 134 * * non-matching non-zero ino values be written as non-matching 135 * non-zero values 136 * Libarchive further ensures that zero ino values get written 137 * as zeroes. This allows the cpio writer to generate 138 * synthetic ino values for the archive that may be different 139 * than those on disk in order to avoid problems due to truncation. 140 * This is especially needed for odc (POSIX format) that 141 * only supports 18-bit ino values. 142 */ 143 e = buff; 144 145 /* "file" */ 146 file = e; /* Remember where this starts... */ 147 assert(is_octal(e, 76)); /* Entire header is octal digits. */ 148 assertEqualMem(e + 0, "070707", 6); /* Magic */ 149 assertEqualMem(e + 6, "000014", 6); /* dev */ 150 assert(memcmp(e + 12, "000000", 6) != 0); /* ino must be != 0 */ 151 assertEqualMem(e + 18, "100664", 6); /* Mode */ 152 assertEqualMem(e + 24, "000120", 6); /* uid */ 153 assertEqualMem(e + 30, "000132", 6); /* gid */ 154 assertEqualMem(e + 36, "000002", 6); /* nlink */ 155 assertEqualMem(e + 42, "000000", 6); /* rdev */ 156 assertEqualMem(e + 48, "00000000001", 11); /* mtime */ 157 assertEqualMem(e + 59, "000005", 6); /* Name size */ 158 assertEqualMem(e + 65, "00000000012", 11); /* File size */ 159 assertEqualMem(e + 76, "file\0", 5); /* Name contents */ 160 assertEqualMem(e + 81, "1234567890", 10); /* File contents */ 161 e += 91; 162 163 /* hardlink to "file" */ 164 assert(is_octal(e, 76)); /* Entire header is octal digits. */ 165 assertEqualMem(e + 0, "070707", 6); /* Magic */ 166 assertEqualMem(e + 6, "000014", 6); /* dev */ 167 assertEqualMem(e + 12, file + 12, 6); /* ino must match above */ 168 assertEqualMem(e + 18, "100664", 6); /* Mode */ 169 assertEqualMem(e + 24, "000120", 6); /* uid */ 170 assertEqualMem(e + 30, "000132", 6); /* gid */ 171 assertEqualMem(e + 36, "000002", 6); /* nlink */ 172 assertEqualMem(e + 42, "000000", 6); /* rdev */ 173 assertEqualMem(e + 48, "00000000001", 11); /* mtime */ 174 assertEqualMem(e + 59, "000011", 6); /* Name size */ 175 assertEqualMem(e + 65, "00000000012", 11); /* File size */ 176 assertEqualMem(e + 76, "linkfile\0", 9); /* Name contents */ 177 assertEqualMem(e + 85, "1234567890", 10); /* File contents */ 178 e += 95; 179 180 /* "dir" */ 181 assert(is_octal(e, 76)); 182 assertEqualMem(e + 0, "070707", 6); /* Magic */ 183 assertEqualMem(e + 6, "000000", 6); /* dev */ 184 assertEqualMem(e + 12, "000000", 6); /* ino */ 185 assertEqualMem(e + 18, "040775", 6); /* Mode */ 186 assertEqualMem(e + 24, "000000", 6); /* uid */ 187 assertEqualMem(e + 30, "000000", 6); /* gid */ 188 assertEqualMem(e + 36, "000002", 6); /* Nlink */ 189 assertEqualMem(e + 42, "000000", 6); /* rdev */ 190 assertEqualMem(e + 48, "00000000002", 11); /* mtime */ 191 assertEqualMem(e + 59, "000004", 6); /* Name size */ 192 assertEqualMem(e + 65, "00000000000", 11); /* File size */ 193 assertEqualMem(e + 76, "dir\0", 4); /* name */ 194 e += 80; 195 196 /* "symlink" pointing to "file" */ 197 assert(is_octal(e, 76)); /* Entire header is octal digits. */ 198 assertEqualMem(e + 0, "070707", 6); /* Magic */ 199 assertEqualMem(e + 6, "000014", 6); /* dev */ 200 assert(memcmp(e + 12, file + 12, 6) != 0); /* ino must != file ino */ 201 assert(memcmp(e + 12, "000000", 6) != 0); /* ino must != 0 */ 202 assertEqualMem(e + 18, "120664", 6); /* Mode */ 203 assertEqualMem(e + 24, "000130", 6); /* uid */ 204 assertEqualMem(e + 30, "000142", 6); /* gid */ 205 assertEqualMem(e + 36, "000001", 6); /* nlink */ 206 assertEqualMem(e + 42, "000000", 6); /* rdev */ 207 assertEqualMem(e + 48, "00000000003", 11); /* mtime */ 208 assertEqualMem(e + 59, "000010", 6); /* Name size */ 209 assertEqualMem(e + 65, "00000000004", 11); /* File size */ 210 assertEqualMem(e + 76, "symlink\0", 8); /* Name contents */ 211 assertEqualMem(e + 84, "file", 4); /* File contents == link target */ 212 e += 88; 213 214 /* TODO: Verify other types of entries. */ 215 216 /* Last entry is end-of-archive marker. */ 217 assert(is_octal(e, 76)); 218 assertEqualMem(e + 0, "070707", 6); /* Magic */ 219 assertEqualMem(e + 6, "000000", 6); /* dev */ 220 assertEqualMem(e + 12, "000000", 6); /* ino */ 221 assertEqualMem(e + 18, "000000", 6); /* Mode */ 222 assertEqualMem(e + 24, "000000", 6); /* uid */ 223 assertEqualMem(e + 30, "000000", 6); /* gid */ 224 assertEqualMem(e + 36, "000001", 6); /* Nlink */ 225 assertEqualMem(e + 42, "000000", 6); /* rdev */ 226 assertEqualMem(e + 48, "00000000000", 11); /* mtime */ 227 assertEqualMem(e + 59, "000013", 6); /* Name size */ 228 assertEqualMem(e + 65, "00000000000", 11); /* File size */ 229 assertEqualMem(e + 76, "TRAILER!!!\0", 11); /* Name */ 230 e += 87; 231 232 assertEqualInt((int)used, e - buff); 233 234 free(buff); 235 } 236