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$"); 27 28 /* Number of bytes needed to pad 'n' to multiple of 'block', assuming 29 * that 'block' is a power of two. This trick can be more easily 30 * remembered as -n & (block - 1), but many compilers quite reasonably 31 * warn about "-n" when n is an unsigned value. (~(n) + 1) is the 32 * same thing, but written in a way that won't offend anyone. */ 33 #define PAD(n, block) ((~(n) + 1) & ((block) - 1)) 34 35 static int 36 is_hex(const char *p, size_t l) 37 { 38 while (l > 0) { 39 if ((*p >= '0' && *p <= '9') 40 || (*p >= 'a' && *p <= 'f') 41 || (*p >= 'A' && *p <= 'F')) 42 { 43 --l; 44 ++p; 45 } else 46 return (0); 47 48 } 49 return (1); 50 } 51 52 /* Convert up to 8 hex characters to unsigned 32-bit decimal integer */ 53 static uint32_t 54 from_hex(const char *p, size_t l) 55 { 56 uint32_t r = 0; 57 58 while (l > 0) { 59 r *= 16; 60 if (*p >= 'a' && *p <= 'f') 61 r += *p + 10 - 'a'; 62 else if (*p >= 'A' && *p <= 'F') 63 r += *p + 10 - 'A'; 64 else 65 r += *p - '0'; 66 --l; 67 ++p; 68 } 69 return (r); 70 } 71 72 #if !defined(_WIN32) || defined(__CYGWIN__) 73 static int 74 nlinks(const char *p) 75 { 76 struct stat st; 77 assertEqualInt(0, stat(p, &st)); 78 return st.st_nlink; 79 } 80 #endif 81 82 DEFINE_TEST(test_format_newc) 83 { 84 FILE *list; 85 int r; 86 uint32_t devmajor, devminor, ino, gid, uid; 87 time_t t, t2, now; 88 char *p, *e; 89 size_t s; 90 uint64_t fs, ns; 91 char result[1024]; 92 93 assertUmask(0); 94 95 #if !defined(_WIN32) 96 uid = getuid(); 97 #endif 98 99 /* 100 * Create an assortment of files. 101 * TODO: Extend this to cover more filetypes. 102 */ 103 list = fopen("list", "w"); 104 105 /* "file1" */ 106 assertMakeFile("file1", 0644, "1234567890"); 107 fprintf(list, "file1\n"); 108 109 /* "hardlink" */ 110 assertMakeHardlink("hardlink", "file1"); 111 fprintf(list, "hardlink\n"); 112 113 /* Another hardlink, but this one won't be archived. */ 114 assertMakeHardlink("hardlink2", "file1"); 115 116 /* "symlink" */ 117 if (canSymlink()) { 118 assertMakeSymlink("symlink", "file1", 0); 119 fprintf(list, "symlink\n"); 120 } 121 122 /* "dir" */ 123 assertMakeDir("dir", 0775); 124 fprintf(list, "dir\n"); 125 126 /* Setup result message. */ 127 memset(result, 0, sizeof(result)); 128 if (is_LargeInode("file1")) { 129 strncat(result, 130 "bsdcpio: file1: large inode number truncated: ", 131 sizeof(result) - strlen(result) -1); 132 strncat(result, strerror(ERANGE), 133 sizeof(result) - strlen(result) -1); 134 strncat(result, "\n", 135 sizeof(result) - strlen(result) -1); 136 } 137 if (canSymlink() && is_LargeInode("symlink")) { 138 strncat(result, 139 "bsdcpio: symlink: large inode number truncated: ", 140 sizeof(result) - strlen(result) -1); 141 strncat(result, strerror(ERANGE), 142 sizeof(result) - strlen(result) -1); 143 strncat(result, "\n", 144 sizeof(result) - strlen(result) -1); 145 } 146 if (is_LargeInode("dir")) { 147 strncat(result, 148 "bsdcpio: dir: large inode number truncated: ", 149 sizeof(result) - strlen(result) -1); 150 strncat(result, strerror(ERANGE), 151 sizeof(result) - strlen(result) -1); 152 strncat(result, "\n", 153 sizeof(result) - strlen(result) -1); 154 } 155 if (is_LargeInode("hardlink")) { 156 strncat(result, 157 "bsdcpio: hardlink: large inode number truncated: ", 158 sizeof(result) - strlen(result) -1); 159 strncat(result, strerror(ERANGE), 160 sizeof(result) - strlen(result) -1); 161 strncat(result, "\n", 162 sizeof(result) - strlen(result) -1); 163 } 164 165 /* Record some facts about what we just created: */ 166 now = time(NULL); /* They were all created w/in last two seconds. */ 167 168 /* Use the cpio program to create an archive. */ 169 fclose(list); 170 r = systemf("%s -o --format=newc <list >newc.out 2>newc.err", 171 testprog); 172 if (!assertEqualInt(r, 0)) 173 return; 174 175 /* Verify that nothing went to stderr. */ 176 if (canSymlink()) { 177 strncat(result, "2 blocks\n", sizeof(result) - strlen(result) -1); 178 } else { 179 strncat(result, "1 block\n", sizeof(result) - strlen(result) -1); 180 } 181 assertTextFileContents(result, "newc.err"); 182 183 /* Verify that stdout is a well-formed cpio file in "newc" format. */ 184 p = slurpfile(&s, "newc.out"); 185 assertEqualInt(s, canSymlink() ? 1024 : 512); 186 e = p; 187 188 /* 189 * Some of these assertions could be stronger, but it's 190 * a little tricky because they depend on the local environment. 191 */ 192 193 /* First entry is "file1" */ 194 assert(is_hex(e, 110)); /* Entire header is octal digits. */ 195 assertEqualMem(e + 0, "070701", 6); /* Magic */ 196 ino = from_hex(e + 6, 8); /* ino */ 197 #if defined(_WIN32) && !defined(__CYGWIN__) 198 /* Group members bits and others bits do not work. */ 199 assertEqualInt(0x8180, from_hex(e + 14, 8) & 0xffc0); /* Mode */ 200 #else 201 assertEqualInt(0x81a4, from_hex(e + 14, 8)); /* Mode */ 202 #endif 203 #if defined(_WIN32) 204 uid = from_hex(e + 22, 8); 205 #else 206 assertEqualInt(from_hex(e + 22, 8), uid); /* uid */ 207 #endif 208 gid = from_hex(e + 30, 8); /* gid */ 209 assertEqualMem(e + 38, "00000003", 8); /* nlink */ 210 t = from_hex(e + 46, 8); /* mtime */ 211 failure("t=%#08jx now=%#08jx=%jd", (intmax_t)t, (intmax_t)now, 212 (intmax_t)now); 213 assert(t <= now); /* File wasn't created in future. */ 214 failure("t=%#08jx now - 2=%#08jx=%jd", (intmax_t)t, (intmax_t)now - 2, 215 (intmax_t)now - 2); 216 assert(t >= now - 2); /* File was created w/in last 2 secs. */ 217 failure("newc format stores body only with last appearance of a link\n" 218 " first appearance should be empty, so this file size\n" 219 " field should be zero"); 220 assertEqualInt(0, from_hex(e + 54, 8)); /* File size */ 221 fs = (uint64_t)from_hex(e + 54, 8); 222 fs += PAD(fs, 4); 223 devmajor = from_hex(e + 62, 8); /* devmajor */ 224 devminor = from_hex(e + 70, 8); /* devminor */ 225 assert(is_hex(e + 78, 8)); /* rdevmajor */ 226 assert(is_hex(e + 86, 8)); /* rdevminor */ 227 assertEqualMem(e + 94, "00000006", 8); /* Name size */ 228 ns = (uint64_t)from_hex(e + 94, 8); 229 ns += PAD(ns + 2, 4); 230 assertEqualInt(0, from_hex(e + 102, 8)); /* check field */ 231 assertEqualMem(e + 110, "file1\0", 6); /* Name contents */ 232 /* Since there's another link, no file contents here. */ 233 /* But add in file size so that an error here doesn't cascade. */ 234 e += 110 + fs + ns; 235 236 if (canSymlink()) { 237 /* "symlink" pointing to "file1" */ 238 assert(is_hex(e, 110)); 239 assertEqualMem(e + 0, "070701", 6); /* Magic */ 240 assert(is_hex(e + 6, 8)); /* ino */ 241 #if defined(_WIN32) && !defined(CYGWIN) 242 /* Mode: Group members bits and others bits do not work. */ 243 assertEqualInt(0xa180, from_hex(e + 14, 8) & 0xffc0); 244 #else 245 assertEqualInt(0xa1ff, from_hex(e + 14, 8)); /* Mode */ 246 #endif 247 assertEqualInt(from_hex(e + 22, 8), uid); /* uid */ 248 assertEqualInt(gid, from_hex(e + 30, 8)); /* gid */ 249 assertEqualMem(e + 38, "00000001", 8); /* nlink */ 250 t2 = from_hex(e + 46, 8); /* mtime */ 251 failure("First entry created at t=%#08jx this entry created" 252 " at t2=%#08jx", (intmax_t)t, (intmax_t)t2); 253 assert(t2 == t || t2 == t + 1); /* Almost same as first entry. */ 254 assertEqualMem(e + 54, "00000005", 8); /* File size */ 255 fs = (uint64_t)from_hex(e + 54, 8); 256 fs += PAD(fs, 4); 257 assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */ 258 assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */ 259 assert(is_hex(e + 78, 8)); /* rdevmajor */ 260 assert(is_hex(e + 86, 8)); /* rdevminor */ 261 assertEqualMem(e + 94, "00000008", 8); /* Name size */ 262 ns = (uint64_t)from_hex(e + 94, 8); 263 ns += PAD(ns + 2, 4); 264 assertEqualInt(0, from_hex(e + 102, 8)); /* check field */ 265 assertEqualMem(e + 110, "symlink\0\0\0", 10); /* Name contents */ 266 assertEqualMem(e + 110 + ns, "file1\0\0\0", 8); /* symlink target */ 267 e += 110 + fs + ns; 268 } 269 270 /* "dir" */ 271 assert(is_hex(e, 110)); 272 assertEqualMem(e + 0, "070701", 6); /* Magic */ 273 assert(is_hex(e + 6, 8)); /* ino */ 274 #if defined(_WIN32) && !defined(__CYGWIN__) 275 /* Group members bits and others bits do not work. */ 276 assertEqualInt(0x41c0, from_hex(e + 14, 8) & 0xffc0); /* Mode */ 277 #else 278 /* Mode: sgid bit sometimes propagates from parent dirs, ignore it. */ 279 assertEqualInt(040775, from_hex(e + 14, 8) & ~02000); 280 #endif 281 assertEqualInt(uid, from_hex(e + 22, 8)); /* uid */ 282 assertEqualInt(gid, from_hex(e + 30, 8)); /* gid */ 283 #if !defined(_WIN32) || defined(__CYGWIN__) 284 assertEqualInt(nlinks("dir"), from_hex(e + 38, 8)); /* nlinks */ 285 #endif 286 t2 = from_hex(e + 46, 8); /* mtime */ 287 failure("First entry created at t=%#08jx this entry created at" 288 "t2=%#08jx", (intmax_t)t, (intmax_t)t2); 289 assert(t2 == t || t2 == t + 1); /* Almost same as first entry. */ 290 assertEqualMem(e + 54, "00000000", 8); /* File size */ 291 fs = (uint64_t)from_hex(e + 54, 8); 292 fs += PAD(fs, 4); 293 assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */ 294 assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */ 295 assert(is_hex(e + 78, 8)); /* rdevmajor */ 296 assert(is_hex(e + 86, 8)); /* rdevminor */ 297 assertEqualMem(e + 94, "00000004", 8); /* Name size */ 298 ns = (uint64_t)from_hex(e + 94, 8); 299 ns += PAD(ns + 2, 4); 300 assertEqualInt(0, from_hex(e + 102, 8)); /* check field */ 301 assertEqualMem(e + 110, "dir\0\0\0", 6); /* Name contents */ 302 e += 110 + fs + ns; 303 304 /* Hardlink identical to "file1" */ 305 /* Since we only wrote two of the three links to this 306 * file, this link should get deferred by the hardlink logic. */ 307 assert(is_hex(e, 110)); 308 assertEqualMem(e + 0, "070701", 6); /* Magic */ 309 failure("If these aren't the same, then the hardlink detection failed to match them."); 310 assertEqualInt(ino, from_hex(e + 6, 8)); /* ino */ 311 #if defined(_WIN32) && !defined(__CYGWIN__) 312 /* Group members bits and others bits do not work. */ 313 assertEqualInt(0x8180, from_hex(e + 14, 8) & 0xffc0); /* Mode */ 314 #else 315 assertEqualInt(0x81a4, from_hex(e + 14, 8)); /* Mode */ 316 #endif 317 assertEqualInt(from_hex(e + 22, 8), uid); /* uid */ 318 assertEqualInt(gid, from_hex(e + 30, 8)); /* gid */ 319 assertEqualMem(e + 38, "00000003", 8); /* nlink */ 320 t2 = from_hex(e + 46, 8); /* mtime */ 321 failure("First entry created at t=%#08jx this entry created at" 322 "t2=%#08jx", (intmax_t)t, (intmax_t)t2); 323 assert(t2 == t || t2 == t + 1); /* Almost same as first entry. */ 324 assertEqualInt(10, from_hex(e + 54, 8)); /* File size */ 325 fs = (uint64_t)from_hex(e + 54, 8); 326 fs += PAD(fs, 4); 327 assertEqualInt(devmajor, from_hex(e + 62, 8)); /* devmajor */ 328 assertEqualInt(devminor, from_hex(e + 70, 8)); /* devminor */ 329 assert(is_hex(e + 78, 8)); /* rdevmajor */ 330 assert(is_hex(e + 86, 8)); /* rdevminor */ 331 assertEqualMem(e + 94, "00000009", 8); /* Name size */ 332 ns = (uint64_t)from_hex(e + 94, 8); 333 ns += PAD(ns + 2, 4); 334 assertEqualInt(0, from_hex(e + 102, 8)); /* check field */ 335 assertEqualMem(e + 110, "hardlink\0\0", 10); /* Name contents */ 336 assertEqualMem(e + 110 + ns, "1234567890\0\0", 12); /* File contents */ 337 e += 110 + ns + fs; 338 339 /* Last entry is end-of-archive marker. */ 340 assert(is_hex(e, 110)); 341 assertEqualMem(e + 0, "070701", 6); /* Magic */ 342 assertEqualMem(e + 8, "00000000", 8); /* ino */ 343 assertEqualMem(e + 14, "00000000", 8); /* mode */ 344 assertEqualMem(e + 22, "00000000", 8); /* uid */ 345 assertEqualMem(e + 30, "00000000", 8); /* gid */ 346 assertEqualMem(e + 38, "00000001", 8); /* nlink */ 347 assertEqualMem(e + 46, "00000000", 8); /* mtime */ 348 assertEqualMem(e + 54, "00000000", 8); /* size */ 349 assertEqualMem(e + 62, "00000000", 8); /* devmajor */ 350 assertEqualMem(e + 70, "00000000", 8); /* devminor */ 351 assertEqualMem(e + 78, "00000000", 8); /* rdevmajor */ 352 assertEqualMem(e + 86, "00000000", 8); /* rdevminor */ 353 assertEqualInt(11, from_hex(e + 94, 8)); /* name size */ 354 assertEqualMem(e + 102, "00000000", 8); /* check field */ 355 assertEqualMem(e + 110, "TRAILER!!!\0\0", 12); /* Name */ 356 357 free(p); 358 } 359