Home | History | Annotate | Line # | Download | only in test
test_basic.c revision 1.2
      1  1.1     joerg /*-
      2  1.1     joerg  * Copyright (c) 2003-2007 Tim Kientzle
      3  1.1     joerg  * All rights reserved.
      4  1.1     joerg  *
      5  1.1     joerg  * Redistribution and use in source and binary forms, with or without
      6  1.1     joerg  * modification, are permitted provided that the following conditions
      7  1.1     joerg  * are met:
      8  1.1     joerg  * 1. Redistributions of source code must retain the above copyright
      9  1.1     joerg  *    notice, this list of conditions and the following disclaimer.
     10  1.1     joerg  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1     joerg  *    notice, this list of conditions and the following disclaimer in the
     12  1.1     joerg  *    documentation and/or other materials provided with the distribution.
     13  1.1     joerg  *
     14  1.1     joerg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
     15  1.1     joerg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     16  1.1     joerg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     17  1.1     joerg  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
     18  1.1     joerg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     19  1.1     joerg  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     20  1.1     joerg  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     21  1.1     joerg  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  1.1     joerg  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     23  1.1     joerg  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  1.1     joerg  */
     25  1.1     joerg #include "test.h"
     26  1.1     joerg __FBSDID("$FreeBSD: src/usr.bin/tar/test/test_basic.c,v 1.2 2008/05/26 17:10:10 kientzle Exp $");
     27  1.1     joerg 
     28  1.2  christos static const char *
     29  1.2  christos make_files(void)
     30  1.2  christos {
     31  1.2  christos 	FILE *f;
     32  1.2  christos 
     33  1.2  christos 	/* File with 10 bytes content. */
     34  1.2  christos 	f = fopen("file", "wb");
     35  1.2  christos 	assert(f != NULL);
     36  1.2  christos 	assertEqualInt(10, fwrite("123456789", 1, 10, f));
     37  1.2  christos 	fclose(f);
     38  1.2  christos 
     39  1.2  christos 	/* hardlink to above file. */
     40  1.2  christos 	assertMakeHardlink("linkfile", "file");
     41  1.2  christos 	assertIsHardlink("file", "linkfile");
     42  1.2  christos 
     43  1.2  christos 	/* Symlink to above file. */
     44  1.2  christos 	if (canSymlink())
     45  1.2  christos 		assertMakeSymlink("symlink", "file", 0);
     46  1.2  christos 
     47  1.2  christos 	/* Directory. */
     48  1.2  christos 	assertMakeDir("dir", 0775);
     49  1.2  christos 
     50  1.2  christos 	return canSymlink()
     51  1.2  christos 	    ? "file linkfile symlink dir"
     52  1.2  christos 	    : "file linkfile dir";
     53  1.2  christos }
     54  1.2  christos 
     55  1.2  christos static void
     56  1.2  christos verify_files(const char *target)
     57  1.2  christos {
     58  1.2  christos 	assertChdir(target);
     59  1.2  christos 
     60  1.2  christos 	/* Regular file with 2 links. */
     61  1.2  christos 	failure("%s", target);
     62  1.2  christos 	assertIsReg("file", -1);
     63  1.2  christos 	failure("%s", target);
     64  1.2  christos 	assertFileSize("file", 10);
     65  1.2  christos 	failure("%s", target);
     66  1.2  christos 	assertFileContents("123456789", 10, "file");
     67  1.2  christos 	failure("%s", target);
     68  1.2  christos 	assertFileNLinks("file", 2);
     69  1.2  christos 
     70  1.2  christos 	/* Another name for the same file. */
     71  1.2  christos 	failure("%s", target);
     72  1.2  christos 	assertIsReg("linkfile", -1);
     73  1.2  christos 	failure("%s", target);
     74  1.2  christos 	assertFileSize("linkfile", 10);
     75  1.2  christos 	assertFileContents("123456789", 10, "linkfile");
     76  1.2  christos 	assertFileNLinks("linkfile", 2);
     77  1.2  christos 	assertIsHardlink("file", "linkfile");
     78  1.2  christos 
     79  1.2  christos 	/* Symlink */
     80  1.2  christos 	if (canSymlink())
     81  1.2  christos 		assertIsSymlink("symlink", "file", 0);
     82  1.2  christos 
     83  1.2  christos 	/* dir */
     84  1.2  christos 	failure("%s", target);
     85  1.2  christos 	assertIsDir("dir", 0775);
     86  1.2  christos 	assertChdir("..");
     87  1.2  christos }
     88  1.1     joerg 
     89  1.1     joerg static void
     90  1.2  christos run_tar(const char *target, const char *pack_options,
     91  1.2  christos     const char *unpack_options, const char *flist)
     92  1.1     joerg {
     93  1.1     joerg 	int r;
     94  1.1     joerg 
     95  1.2  christos 	assertMakeDir(target, 0775);
     96  1.1     joerg 
     97  1.1     joerg 	/* Use the tar program to create an archive. */
     98  1.2  christos 	r = systemf("%s cf - %s %s >%s/archive 2>%s/pack.err", testprog, pack_options, flist, target, target);
     99  1.2  christos 	failure("Error invoking %s cf -%s", testprog, pack_options);
    100  1.1     joerg 	assertEqualInt(r, 0);
    101  1.1     joerg 
    102  1.2  christos 	assertChdir(target);
    103  1.1     joerg 
    104  1.1     joerg 	/* Verify that nothing went to stderr. */
    105  1.1     joerg 	assertEmptyFile("pack.err");
    106  1.1     joerg 
    107  1.1     joerg 	/*
    108  1.1     joerg 	 * Use tar to unpack the archive into another directory.
    109  1.1     joerg 	 */
    110  1.2  christos 	r = systemf("%s xf archive %s >unpack.out 2>unpack.err",
    111  1.2  christos 	    testprog, unpack_options);
    112  1.1     joerg 	failure("Error invoking %s xf archive %s", testprog, unpack_options);
    113  1.1     joerg 	assertEqualInt(r, 0);
    114  1.1     joerg 
    115  1.1     joerg 	/* Verify that nothing went to stderr. */
    116  1.1     joerg 	assertEmptyFile("unpack.err");
    117  1.2  christos 	assertChdir("..");
    118  1.1     joerg }
    119  1.1     joerg 
    120  1.1     joerg DEFINE_TEST(test_basic)
    121  1.1     joerg {
    122  1.2  christos 	const char *flist;
    123  1.1     joerg 
    124  1.2  christos 	assertUmask(0);
    125  1.2  christos 	flist = make_files();
    126  1.2  christos 	/* Archive/dearchive with a variety of options. */
    127  1.2  christos 	run_tar("copy", "", "", flist);
    128  1.2  christos 	verify_files("copy");
    129  1.1     joerg 
    130  1.2  christos 	run_tar("copy_ustar", "--format=ustar", "", flist);
    131  1.2  christos 	verify_files("copy_ustar");
    132  1.1     joerg 
    133  1.1     joerg 	/* tar doesn't handle cpio symlinks correctly */
    134  1.2  christos 	/* run_tar("copy_odc", "--format=odc", ""); */
    135  1.1     joerg }
    136