Home | History | Annotate | Line # | Download | only in test
test_gcpio_compat.c revision 1.1.1.1.12.2
      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 
     29 static void
     30 unpack_test(const char *from, const char *options, const char *se)
     31 {
     32 	struct stat st, st2;
     33 	char buff[128];
     34 	int r;
     35 
     36 	/* Create a work dir named after the file we're unpacking. */
     37 	assertEqualInt(0, mkdir(from, 0775));
     38 	chdir(from);
     39 
     40 	/*
     41 	 * Use cpio to unpack the sample archive
     42 	 */
     43 	extract_reference_file(from);
     44 	r = systemf("%s -i %s < %s >unpack.out 2>unpack.err",
     45 	    testprog, options, from);
     46 	failure("Error invoking %s -i %s < %s",
     47 	    testprog, options, from);
     48 	assertEqualInt(r, 0);
     49 
     50 	/* Verify that nothing went to stderr. */
     51 	assertFileContents(se, strlen(se), "unpack.err");
     52 
     53 	/*
     54 	 * Verify unpacked files.
     55 	 */
     56 
     57 	/* Regular file with 2 links. */
     58 	r = lstat("file", &st);
     59 	failure("Failed to stat file %s/file, errno=%d", from, errno);
     60 	assertEqualInt(r, 0);
     61 	if (r == 0) {
     62 		assert(S_ISREG(st.st_mode));
     63 		assertEqualInt(0644, st.st_mode & 0777);
     64 		failure("file %s/file", from);
     65 		assertEqualInt(10, st.st_size);
     66 		failure("file %s/file", from);
     67 		assertEqualInt(2, st.st_nlink);
     68 	}
     69 
     70 	/* Another name for the same file. */
     71 	r = lstat("linkfile", &st2);
     72 	failure("Failed to stat file %s/linkfile, errno=%d", from, errno);
     73 	assertEqualInt(r, 0);
     74 	if (r == 0) {
     75 		assert(S_ISREG(st2.st_mode));
     76 		assertEqualInt(0644, st2.st_mode & 0777);
     77 		failure("file %s/file", from);
     78 		assertEqualInt(10, st2.st_size);
     79 		failure("file %s/file", from);
     80 		assertEqualInt(2, st2.st_nlink);
     81 		failure("file and linkfile should be hardlinked");
     82 		assertEqualInt(st.st_dev, st2.st_dev);
     83 		failure("file %s/file", from);
     84 		assertEqualInt(st.st_ino, st2.st_ino);
     85 	}
     86 
     87 	/* Symlink */
     88 	r = lstat("symlink", &st);
     89 	failure("Failed to stat file %s/symlink, errno=%d", from, errno);
     90 	assertEqualInt(r, 0);
     91 	if (r == 0) {
     92 		failure("symlink should be a symlink; actual mode is %o",
     93 		    st.st_mode);
     94 		assert(S_ISLNK(st.st_mode));
     95 		if (S_ISLNK(st.st_mode)) {
     96 			r = readlink("symlink", buff, sizeof(buff));
     97 			assertEqualInt(r, 4);
     98 			buff[r] = '\0';
     99 			assertEqualString(buff, "file");
    100 		}
    101 	}
    102 
    103 	/* dir */
    104 	r = lstat("dir", &st);
    105 	if (r == 0) {
    106 		assertEqualInt(r, 0);
    107 		assert(S_ISDIR(st.st_mode));
    108 		assertEqualInt(0775, st.st_mode & 0777);
    109 	}
    110 
    111 	chdir("..");
    112 }
    113 
    114 DEFINE_TEST(test_gcpio_compat)
    115 {
    116 	int oldumask;
    117 
    118 	oldumask = umask(0);
    119 
    120 	/* Dearchive sample files with a variety of options. */
    121 	unpack_test("test_gcpio_compat_ref.bin", "", "1 block\n");
    122 	unpack_test("test_gcpio_compat_ref.crc", "", "2 blocks\n");
    123 	unpack_test("test_gcpio_compat_ref.newc", "", "2 blocks\n");
    124 	unpack_test("test_gcpio_compat_ref.ustar", "", "7 blocks\n");
    125 
    126 	umask(oldumask);
    127 }
    128