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