test_basic.c revision 228753
1207614Simp/*- 2207614Simp * Copyright (c) 2003-2007 Tim Kientzle 3207614Simp * All rights reserved. 4207614Simp * 5207614Simp * Redistribution and use in source and binary forms, with or without 6207614Simp * modification, are permitted provided that the following conditions 7207614Simp * are met: 8207614Simp * 1. Redistributions of source code must retain the above copyright 9207614Simp * notice, this list of conditions and the following disclaimer. 10207614Simp * 2. Redistributions in binary form must reproduce the above copyright 11207614Simp * notice, this list of conditions and the following disclaimer in the 12207614Simp * documentation and/or other materials provided with the distribution. 13207614Simp * 14207614Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15207614Simp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16207614Simp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17207614Simp * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18207614Simp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19207614Simp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20207614Simp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21207614Simp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22207614Simp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23207614Simp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24207614Simp */ 25207614Simp#include "test.h" 26207614Simp__FBSDID("$FreeBSD: src/usr.bin/cpio/test/test_basic.c,v 1.4 2008/08/25 06:39:29 kientzle Exp $"); 27207614Simp 28207614Simpstatic void 29207614Simpverify_files(const char *msg) 30207614Simp{ 31207614Simp /* 32207614Simp * Verify unpacked files. 33207614Simp */ 34207614Simp 35207614Simp /* Regular file with 2 links. */ 36207614Simp assertIsReg("file", 0644); 37207614Simp failure(msg); 38207614Simp assertFileSize("file", 10); 39207614Simp assertFileNLinks("file", 2); 40207614Simp 41207614Simp /* Another name for the same file. */ 42207614Simp assertIsHardlink("linkfile", "file"); 43207614Simp 44207614Simp /* Symlink */ 45207614Simp if (canSymlink()) 46207614Simp assertIsSymlink("symlink", "file"); 47207614Simp 48207614Simp /* Another file with 1 link and different permissions. */ 49207614Simp assertIsReg("file2", 0777); 50207614Simp assertFileSize("file2", 10); 51207614Simp assertFileNLinks("file2", 1); 52207614Simp 53207614Simp /* dir */ 54207614Simp assertIsDir("dir", 0775); 55207614Simp} 56241720Sed 57207614Simpstatic void 58207614Simpbasic_cpio(const char *target, 59207614Simp const char *pack_options, 60207614Simp const char *unpack_options, 61207614Simp const char *se) 62207614Simp{ 63207614Simp int r; 64207614Simp 65207614Simp if (!assertMakeDir(target, 0775)) 66207614Simp return; 67207614Simp 68207614Simp /* Use the cpio program to create an archive. */ 69207614Simp r = systemf("%s -o %s < filelist >%s/archive 2>%s/pack.err", 70207614Simp testprog, pack_options, target, target); 71207614Simp failure("Error invoking %s -o %s", testprog, pack_options); 72207614Simp assertEqualInt(r, 0); 73207614Simp 74207614Simp assertChdir(target); 75229780Suqs 76207614Simp /* Verify stderr. */ 77207614Simp failure("Expected: %s, options=%s", se, pack_options); 78207614Simp assertTextFileContents(se, "pack.err"); 79207614Simp 80207614Simp /* 81229780Suqs * Use cpio to unpack the archive into another directory. 82207614Simp */ 83207614Simp r = systemf("%s -i %s< archive >unpack.out 2>unpack.err", 84207614Simp testprog, unpack_options); 85207614Simp failure("Error invoking %s -i %s", testprog, unpack_options); 86207614Simp assertEqualInt(r, 0); 87207614Simp 88207614Simp /* Verify stderr. */ 89207614Simp failure("Error invoking %s -i %s in dir %s", testprog, unpack_options, target); 90207614Simp assertTextFileContents(se, "unpack.err"); 91207614Simp 92207614Simp verify_files(pack_options); 93207614Simp 94207614Simp assertChdir(".."); 95207614Simp} 96244686Santoine 97207614Simpstatic void 98207614Simppassthrough(const char *target) 99207614Simp{ 100207614Simp int r; 101207614Simp 102207614Simp if (!assertMakeDir(target, 0775)) 103207614Simp return; 104207614Simp 105207614Simp /* 106207614Simp * Use cpio passthrough mode to copy files to another directory. 107207614Simp */ 108207614Simp r = systemf("%s -p %s <filelist >%s/stdout 2>%s/stderr", 109246139Smarius testprog, target, target, target); 110246139Smarius failure("Error invoking %s -p", testprog); 111207614Simp assertEqualInt(r, 0); 112207614Simp 113207614Simp assertChdir(target); 114246139Smarius 115246139Smarius /* Verify stderr. */ 116207614Simp failure("Error invoking %s -p in dir %s", 117207614Simp testprog, target); 118207614Simp assertTextFileContents("1 block\n", "stderr"); 119207614Simp 120207614Simp verify_files("passthrough"); 121207614Simp assertChdir(".."); 122207614Simp} 123207614Simp 124207614SimpDEFINE_TEST(test_basic) 125207614Simp{ 126207614Simp FILE *filelist; 127207614Simp const char *msg; 128207614Simp 129207614Simp assertUmask(0); 130207614Simp 131207614Simp /* 132207614Simp * Create an assortment of files on disk. 133207614Simp */ 134207614Simp filelist = fopen("filelist", "w"); 135207614Simp 136207614Simp /* File with 10 bytes content. */ 137207614Simp assertMakeFile("file", 0644, "1234567890"); 138207614Simp fprintf(filelist, "file\n"); 139207614Simp 140207614Simp /* hardlink to above file. */ 141207614Simp assertMakeHardlink("linkfile", "file"); 142207614Simp fprintf(filelist, "linkfile\n"); 143207614Simp 144207614Simp /* Symlink to above file. */ 145246106Ssbruno if (canSymlink()) { 146207614Simp assertMakeSymlink("symlink", "file"); 147207614Simp fprintf(filelist, "symlink\n"); 148207614Simp } 149207614Simp 150207614Simp /* Another file with different permissions. */ 151207614Simp assertMakeFile("file2", 0777, "1234567890"); 152207614Simp fprintf(filelist, "file2\n"); 153207614Simp 154207614Simp /* Directory. */ 155207614Simp assertMakeDir("dir", 0775); 156207614Simp fprintf(filelist, "dir\n"); 157207614Simp /* All done. */ 158207614Simp fclose(filelist); 159207614Simp 160207614Simp assertUmask(022); 161207614Simp 162207614Simp /* Archive/dearchive with a variety of options. */ 163207614Simp msg = canSymlink() ? "2 blocks\n" : "1 block\n"; 164207614Simp basic_cpio("copy", "", "", msg); 165207614Simp basic_cpio("copy_odc", "--format=odc", "", msg); 166207614Simp basic_cpio("copy_newc", "-H newc", "", "2 blocks\n"); 167207614Simp basic_cpio("copy_cpio", "-H odc", "", msg); 168207614Simp msg = canSymlink() ? "9 blocks\n" : "8 blocks\n"; 169207614Simp basic_cpio("copy_ustar", "-H ustar", "", msg); 170207614Simp 171207614Simp /* Copy in one step using -p */ 172207614Simp passthrough("passthrough"); 173207614Simp} 174207614Simp