1 1.1 joerg /*- 2 1.4 christos * SPDX-License-Identifier: BSD-2-Clause 3 1.4 christos * 4 1.1 joerg * Copyright (c) 2003-2007 Tim Kientzle 5 1.1 joerg * All rights reserved. 6 1.1 joerg */ 7 1.1 joerg #include "test.h" 8 1.1 joerg 9 1.2 christos static const char * 10 1.2 christos make_files(void) 11 1.2 christos { 12 1.2 christos FILE *f; 13 1.2 christos 14 1.2 christos /* File with 10 bytes content. */ 15 1.2 christos f = fopen("file", "wb"); 16 1.2 christos assert(f != NULL); 17 1.2 christos assertEqualInt(10, fwrite("123456789", 1, 10, f)); 18 1.2 christos fclose(f); 19 1.2 christos 20 1.2 christos /* hardlink to above file. */ 21 1.2 christos assertMakeHardlink("linkfile", "file"); 22 1.2 christos assertIsHardlink("file", "linkfile"); 23 1.2 christos 24 1.2 christos /* Symlink to above file. */ 25 1.2 christos if (canSymlink()) 26 1.2 christos assertMakeSymlink("symlink", "file", 0); 27 1.2 christos 28 1.2 christos /* Directory. */ 29 1.2 christos assertMakeDir("dir", 0775); 30 1.2 christos 31 1.2 christos return canSymlink() 32 1.2 christos ? "file linkfile symlink dir" 33 1.2 christos : "file linkfile dir"; 34 1.2 christos } 35 1.2 christos 36 1.2 christos static void 37 1.2 christos verify_files(const char *target) 38 1.2 christos { 39 1.2 christos assertChdir(target); 40 1.2 christos 41 1.2 christos /* Regular file with 2 links. */ 42 1.2 christos failure("%s", target); 43 1.2 christos assertIsReg("file", -1); 44 1.2 christos failure("%s", target); 45 1.2 christos assertFileSize("file", 10); 46 1.2 christos failure("%s", target); 47 1.2 christos assertFileContents("123456789", 10, "file"); 48 1.2 christos failure("%s", target); 49 1.2 christos assertFileNLinks("file", 2); 50 1.2 christos 51 1.2 christos /* Another name for the same file. */ 52 1.2 christos failure("%s", target); 53 1.2 christos assertIsReg("linkfile", -1); 54 1.2 christos failure("%s", target); 55 1.2 christos assertFileSize("linkfile", 10); 56 1.2 christos assertFileContents("123456789", 10, "linkfile"); 57 1.2 christos assertFileNLinks("linkfile", 2); 58 1.2 christos assertIsHardlink("file", "linkfile"); 59 1.2 christos 60 1.2 christos /* Symlink */ 61 1.2 christos if (canSymlink()) 62 1.2 christos assertIsSymlink("symlink", "file", 0); 63 1.2 christos 64 1.2 christos /* dir */ 65 1.2 christos failure("%s", target); 66 1.2 christos assertIsDir("dir", 0775); 67 1.2 christos assertChdir(".."); 68 1.2 christos } 69 1.1 joerg 70 1.1 joerg static void 71 1.2 christos run_tar(const char *target, const char *pack_options, 72 1.2 christos const char *unpack_options, const char *flist) 73 1.1 joerg { 74 1.1 joerg int r; 75 1.1 joerg 76 1.2 christos assertMakeDir(target, 0775); 77 1.1 joerg 78 1.1 joerg /* Use the tar program to create an archive. */ 79 1.2 christos r = systemf("%s cf - %s %s >%s/archive 2>%s/pack.err", testprog, pack_options, flist, target, target); 80 1.2 christos failure("Error invoking %s cf -%s", testprog, pack_options); 81 1.1 joerg assertEqualInt(r, 0); 82 1.1 joerg 83 1.2 christos assertChdir(target); 84 1.1 joerg 85 1.1 joerg /* Verify that nothing went to stderr. */ 86 1.1 joerg assertEmptyFile("pack.err"); 87 1.1 joerg 88 1.1 joerg /* 89 1.1 joerg * Use tar to unpack the archive into another directory. 90 1.1 joerg */ 91 1.2 christos r = systemf("%s xf archive %s >unpack.out 2>unpack.err", 92 1.2 christos testprog, unpack_options); 93 1.1 joerg failure("Error invoking %s xf archive %s", testprog, unpack_options); 94 1.1 joerg assertEqualInt(r, 0); 95 1.1 joerg 96 1.1 joerg /* Verify that nothing went to stderr. */ 97 1.1 joerg assertEmptyFile("unpack.err"); 98 1.2 christos assertChdir(".."); 99 1.1 joerg } 100 1.1 joerg 101 1.1 joerg DEFINE_TEST(test_basic) 102 1.1 joerg { 103 1.2 christos const char *flist; 104 1.1 joerg 105 1.2 christos assertUmask(0); 106 1.2 christos flist = make_files(); 107 1.2 christos /* Archive/dearchive with a variety of options. */ 108 1.2 christos run_tar("copy", "", "", flist); 109 1.2 christos verify_files("copy"); 110 1.1 joerg 111 1.2 christos run_tar("copy_ustar", "--format=ustar", "", flist); 112 1.2 christos verify_files("copy_ustar"); 113 1.1 joerg 114 1.1 joerg /* tar doesn't handle cpio symlinks correctly */ 115 1.2 christos /* run_tar("copy_odc", "--format=odc", ""); */ 116 1.1 joerg } 117