xref: /freebsd/contrib/libarchive/tar/test/test_stdio.c (revision 2e113ef82465598b8c26e0ca415fbe90677fbd47)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2003-2007 Tim Kientzle
5  * All rights reserved.
6  */
7 #include "test.h"
8 
9 /*
10  * This data would fit onto one line, but it's easier to understand when it's
11  * on mulitple lines (and thus matches the output files).
12  */
13 static const char *cvf_err =
14 "a f\n"
15 "a l\n";
16 
17 static const char *tf_out =
18 "f\n"
19 "l\n";
20 
21 static const char *xvf_err =
22 "x f\n"
23 "x l\n";
24 
25 /*
26  * This string should appear in the verbose listing regardless of platform,
27  * locale, username, or groupname.
28  */
29 static const char * tvf_contains = "l link to f";
30 
DEFINE_TEST(test_stdio)31 DEFINE_TEST(test_stdio)
32 {
33 	FILE *filelist;
34 	char *p;
35 	size_t s;
36 	int r;
37 
38 	assertUmask(0);
39 
40 	/*
41 	 * Create a couple of files on disk.
42 	 */
43 	/* File */
44 	assertMakeFile("f", 0755, "abc");
45 	/* Link to above file. */
46 	assertMakeHardlink("l", "f");
47 
48 	/* Create file list (text mode here) */
49 	filelist = fopen("filelist", "w");
50 	assert(filelist != NULL);
51 	fprintf(filelist, "f\n");
52 	fprintf(filelist, "l\n");
53 	fclose(filelist);
54 
55 	/*
56 	 * Archive/dearchive with a variety of options, verifying
57 	 * stdio paths.
58 	 */
59 
60 	/* 'cf' should generate no output unless there's an error. */
61 	r = systemf("%s cf archive f l >cf.out 2>cf.err", testprog);
62 	assertEqualInt(r, 0);
63 	assertEmptyFile("cf.out");
64 	assertEmptyFile("cf.err");
65 
66 	/* 'cvf' should generate file list on stderr, empty stdout. */
67 	r = systemf("%s cvf archive f l >cvf.out 2>cvf.err", testprog);
68 	assertEqualInt(r, 0);
69 	failure("'cv' writes filenames to stderr, nothing to stdout (SUSv2)\n"
70 	    "Note that GNU tar writes the file list to stdout by default.");
71 	assertEmptyFile("cvf.out");
72 	assertTextFileContents(cvf_err, "cvf.err");
73 
74 	/* 'cvf -' should generate file list on stderr, archive on stdout. */
75 	r = systemf("%s cvf - f l >cvf-.out 2>cvf-.err", testprog);
76 	assertEqualInt(r, 0);
77 	failure("cvf - should write archive to stdout");
78 	failure("cvf - should write file list to stderr (SUSv2)");
79 	assertEqualFile("cvf.err", "cvf-.err");
80 	/* Check that stdout from 'cvf -' was a valid archive. */
81 	r = systemf("%s tf cvf-.out >cvf-tf.out 2>cvf-tf.err", testprog);
82 	assertEqualInt(r, 0);
83 	assertEmptyFile("cvf-tf.err");
84 	assertTextFileContents(tf_out, "cvf-tf.out");
85 
86 	/* 'tf' should generate file list on stdout, empty stderr. */
87 	r = systemf("%s tf archive >tf.out 2>tf.err", testprog);
88 	assertEqualInt(r, 0);
89 	assertEmptyFile("tf.err");
90 	failure("'t' mode should write results to stdout");
91 	assertTextFileContents(tf_out, "tf.out");
92 
93 	/* 'tvf' should generate file list on stdout, empty stderr. */
94 	r = systemf("%s tvf archive >tvf.out 2>tvf.err", testprog);
95 	assertEqualInt(r, 0);
96 	assertEmptyFile("tvf.err");
97 	failure("'tv' mode should write results to stdout");
98 	/* Check that it contains a string only found in the verbose listing. */
99 	p = slurpfile(&s, "%s", "tvf.out");
100 	assert(strstr(p, tvf_contains) != NULL);
101 	free(p);
102 
103 	/* 'tvf -' uses stdin, file list on stdout, empty stderr. */
104 	r = systemf("%s tvf - < archive >tvf-.out 2>tvf-.err", testprog);
105 	assertEqualInt(r, 0);
106 	assertEmptyFile("tvf-.err");
107 	failure("'tvf-' mode should write the same results as 'tvf'");
108 	assertEqualFile("tvf.out", "tvf-.out");
109 
110 	/* Basic 'xf' should generate no output on stdout or stderr. */
111 	r = systemf("%s xf archive >xf.out 2>xf.err", testprog);
112 	assertEqualInt(r, 0);
113 	assertEmptyFile("xf.err");
114 	assertEmptyFile("xf.out");
115 
116 	/* 'xvf' should generate list on stderr, empty stdout. */
117 	r = systemf("%s xvf archive >xvf.out 2>xvf.err", testprog);
118 	assertEqualInt(r, 0);
119 	assertEmptyFile("xvf.out");
120 	assertTextFileContents(xvf_err, "xvf.err");
121 
122 	/* 'xvOf' should generate list on stderr, file contents on stdout. */
123 	r = systemf("%s xvOf archive >xvOf.out 2>xvOf.err", testprog);
124 	assertEqualInt(r, 0);
125 	/* Verify xvOf.out is the file contents */
126 	p = slurpfile(&s, "xvOf.out");
127 	assertEqualInt((int)s, 3);
128 	assertEqualMem(p, "abc", 3);
129 	assertEqualFile("xvf.err", "xvOf.err");
130 	free(p);
131 
132 	/* 'xvf -' should generate list on stderr, empty stdout. */
133 	r = systemf("%s xvf - < archive >xvf-.out 2>xvf-.err", testprog);
134 	assertEqualInt(r, 0);
135 	assertEmptyFile("xvf-.out");
136 	assertEqualFile("xvf.err", "xvf-.err");
137 }
138