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