]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/libarchive/cpio/test/test_basic.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / libarchive / cpio / test / test_basic.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 verify_files(const char *msg)
30 {
31         /*
32          * Verify unpacked files.
33          */
34
35         /* Regular file with 2 links. */
36         assertIsReg("file", 0644);
37         failure(msg);
38         assertFileSize("file", 10);
39         assertFileNLinks("file", 2);
40
41         /* Another name for the same file. */
42         assertIsHardlink("linkfile", "file");
43
44         /* Symlink */
45         if (canSymlink())
46                 assertIsSymlink("symlink", "file");
47
48         /* Another file with 1 link and different permissions. */
49         assertIsReg("file2", 0777);
50         assertFileSize("file2", 10);
51         assertFileNLinks("file2", 1);
52
53         /* dir */
54         assertIsDir("dir", 0775);
55 }
56
57 static void
58 basic_cpio(const char *target,
59     const char *pack_options,
60     const char *unpack_options,
61     const char *se)
62 {
63         int r;
64
65         if (!assertMakeDir(target, 0775))
66             return;
67
68         /* Use the cpio program to create an archive. */
69         r = systemf("%s -o %s < filelist >%s/archive 2>%s/pack.err",
70             testprog, pack_options, target, target);
71         failure("Error invoking %s -o %s", testprog, pack_options);
72         assertEqualInt(r, 0);
73
74         assertChdir(target);
75
76         /* Verify stderr. */
77         failure("Expected: %s, options=%s", se, pack_options);
78         assertTextFileContents(se, "pack.err");
79
80         /*
81          * Use cpio to unpack the archive into another directory.
82          */
83         r = systemf("%s -i %s< archive >unpack.out 2>unpack.err",
84             testprog, unpack_options);
85         failure("Error invoking %s -i %s", testprog, unpack_options);
86         assertEqualInt(r, 0);
87
88         /* Verify stderr. */
89         failure("Error invoking %s -i %s in dir %s", testprog, unpack_options, target);
90         assertTextFileContents(se, "unpack.err");
91
92         verify_files(pack_options);
93
94         assertChdir("..");
95 }
96
97 static void
98 passthrough(const char *target)
99 {
100         int r;
101
102         if (!assertMakeDir(target, 0775))
103                 return;
104
105         /*
106          * Use cpio passthrough mode to copy files to another directory.
107          */
108         r = systemf("%s -p %s <filelist >%s/stdout 2>%s/stderr",
109             testprog, target, target, target);
110         failure("Error invoking %s -p", testprog);
111         assertEqualInt(r, 0);
112
113         assertChdir(target);
114
115         /* Verify stderr. */
116         failure("Error invoking %s -p in dir %s",
117             testprog, target);
118         assertTextFileContents("1 block\n", "stderr");
119
120         verify_files("passthrough");
121         assertChdir("..");
122 }
123
124 DEFINE_TEST(test_basic)
125 {
126         FILE *filelist;
127         const char *msg;
128
129         assertUmask(0);
130
131         /*
132          * Create an assortment of files on disk.
133          */
134         filelist = fopen("filelist", "w");
135
136         /* File with 10 bytes content. */
137         assertMakeFile("file", 0644, "1234567890");
138         fprintf(filelist, "file\n");
139
140         /* hardlink to above file. */
141         assertMakeHardlink("linkfile", "file");
142         fprintf(filelist, "linkfile\n");
143
144         /* Symlink to above file. */
145         if (canSymlink()) {
146                 assertMakeSymlink("symlink", "file");
147                 fprintf(filelist, "symlink\n");
148         }
149
150         /* Another file with different permissions. */
151         assertMakeFile("file2", 0777, "1234567890");
152         fprintf(filelist, "file2\n");
153
154         /* Directory. */
155         assertMakeDir("dir", 0775);
156         fprintf(filelist, "dir\n");
157         /* All done. */
158         fclose(filelist);
159
160         assertUmask(022);
161
162         /* Archive/dearchive with a variety of options. */
163         msg = canSymlink() ? "2 blocks\n" : "1 block\n";
164         basic_cpio("copy", "", "", msg);
165         basic_cpio("copy_odc", "--format=odc", "", msg);
166         basic_cpio("copy_newc", "-H newc", "", "2 blocks\n");
167         basic_cpio("copy_cpio", "-H odc", "", msg);
168         msg = canSymlink() ? "9 blocks\n" : "8 blocks\n";
169         basic_cpio("copy_ustar", "-H ustar", "", msg);
170
171         /* Copy in one step using -p */
172         passthrough("passthrough");
173 }