xref: /freebsd/contrib/libarchive/cpio/test/test_option_0.c (revision b64c5a0ace59af62eff52bfe110a521dc73c937b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2003-2010 Tim Kientzle
5  * All rights reserved.
6  */
7 #include "test.h"
8 
9 DEFINE_TEST(test_option_0)
10 {
11 	FILE *filelist;
12 	int r;
13 
14 	assertUmask(0);
15 
16 	/* Create a few files. */
17 	assertMakeFile("file1", 0644, "1234567890");
18 	assertMakeFile("file2", 0644, "1234567890");
19 	assertMakeFile("file3", 0644, "1234567890");
20 	assertMakeFile("file4", 0644, "1234567890");
21 
22 	/* Create a file list of filenames with varying end-of-line. */
23 	filelist = fopen("filelist", "wb");
24 	assertEqualInt(fwrite("file1\x0a", 1, 6, filelist), 6);
25 	assertEqualInt(fwrite("file2\x0d", 1, 6, filelist), 6);
26 	assertEqualInt(fwrite("file3\x0a\x0d", 1, 7, filelist), 7);
27 	assertEqualInt(fwrite("file4", 1, 5, filelist), 5);
28 	fclose(filelist);
29 
30 	/* Create a file list of null-delimited names. */
31 	filelist = fopen("filelistNull", "wb");
32 	assertEqualInt(fwrite("file1\0", 1, 6, filelist), 6);
33 	assertEqualInt(fwrite("file2\0", 1, 6, filelist), 6);
34 	assertEqualInt(fwrite("file3\0", 1, 6, filelist), 6);
35 	assertEqualInt(fwrite("file4", 1, 5, filelist), 5);
36 	fclose(filelist);
37 
38 	assertUmask(022);
39 
40 	/* Pack up using the file list with text line endings. */
41 	r = systemf("%s -o < filelist > archive 2> stderr1.txt", testprog);
42 	assertEqualInt(r, 0);
43 
44 	/* Extract into a new dir. */
45 	assertMakeDir("copy", 0775);
46 	assertChdir("copy");
47 	r = systemf("%s -i < ../archive > stdout3.txt 2> stderr3.txt", testprog);
48 	assertEqualInt(r, 0);
49 
50 	/* Verify the files. */
51 	assertIsReg("file1", 0644);
52 	assertIsReg("file2", 0644);
53 	assertIsReg("file3", 0644);
54 	assertIsReg("file4", 0644);
55 
56 	assertChdir("..");
57 
58 	/* Pack up using the file list with nulls. */
59 	r = systemf("%s -o0 < filelistNull > archiveNull 2> stderr2.txt", testprog);
60 	assertEqualInt(r, 0);
61 
62 	/* Extract into a new dir. */
63 	assertMakeDir("copyNull", 0775);
64 	assertChdir("copyNull");
65 	r = systemf("%s -i < ../archiveNull > stdout4.txt 2> stderr4.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