xref: /freebsd/contrib/libarchive/cpio/test/test_option_0.c (revision f5f40dd63bc7acbb5312b26ac1ea1103c12352a6)
1 /*-
2  * Copyright (c) 2003-2010 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 
27 DEFINE_TEST(test_option_0)
28 {
29 	FILE *filelist;
30 	int r;
31 
32 	assertUmask(0);
33 
34 	/* Create a few files. */
35 	assertMakeFile("file1", 0644, "1234567890");
36 	assertMakeFile("file2", 0644, "1234567890");
37 	assertMakeFile("file3", 0644, "1234567890");
38 	assertMakeFile("file4", 0644, "1234567890");
39 
40 	/* Create a file list of filenames with varying end-of-line. */
41 	filelist = fopen("filelist", "wb");
42 	assertEqualInt(fwrite("file1\x0a", 1, 6, filelist), 6);
43 	assertEqualInt(fwrite("file2\x0d", 1, 6, filelist), 6);
44 	assertEqualInt(fwrite("file3\x0a\x0d", 1, 7, filelist), 7);
45 	assertEqualInt(fwrite("file4", 1, 5, filelist), 5);
46 	fclose(filelist);
47 
48 	/* Create a file list of null-delimited names. */
49 	filelist = fopen("filelistNull", "wb");
50 	assertEqualInt(fwrite("file1\0", 1, 6, filelist), 6);
51 	assertEqualInt(fwrite("file2\0", 1, 6, filelist), 6);
52 	assertEqualInt(fwrite("file3\0", 1, 6, filelist), 6);
53 	assertEqualInt(fwrite("file4", 1, 5, filelist), 5);
54 	fclose(filelist);
55 
56 	assertUmask(022);
57 
58 	/* Pack up using the file list with text line endings. */
59 	r = systemf("%s -o < filelist > archive 2> stderr1.txt", testprog);
60 	assertEqualInt(r, 0);
61 
62 	/* Extract into a new dir. */
63 	assertMakeDir("copy", 0775);
64 	assertChdir("copy");
65 	r = systemf("%s -i < ../archive > stdout3.txt 2> stderr3.txt", testprog);
66 	assertEqualInt(r, 0);
67 
68 	/* Verify the files. */
69 	assertIsReg("file1", 0644);
70 	assertIsReg("file2", 0644);
71 	assertIsReg("file3", 0644);
72 	assertIsReg("file4", 0644);
73 
74 	assertChdir("..");
75 
76 	/* Pack up using the file list with nulls. */
77 	r = systemf("%s -o0 < filelistNull > archiveNull 2> stderr2.txt", testprog);
78 	assertEqualInt(r, 0);
79 
80 	/* Extract into a new dir. */
81 	assertMakeDir("copyNull", 0775);
82 	assertChdir("copyNull");
83 	r = systemf("%s -i < ../archiveNull > stdout4.txt 2> stderr4.txt", testprog);
84 	assertEqualInt(r, 0);
85 
86 	/* Verify the files. */
87 	assertIsReg("file1", 0644);
88 	assertIsReg("file2", 0644);
89 	assertIsReg("file3", 0644);
90 	assertIsReg("file4", 0644);
91 }
92