xref: /freebsd/contrib/libarchive/tar/test/test_stdio.c (revision a0ca4af9455b844c5e094fc1b09b1390ffa979fc)
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 
27 DEFINE_TEST(test_stdio)
28 {
29 	FILE *filelist;
30 	char *p;
31 	size_t s;
32 	int r;
33 
34 	assertUmask(0);
35 
36 	/*
37 	 * Create a couple of files on disk.
38 	 */
39 	/* File */
40 	assertMakeFile("f", 0755, "abc");
41 	/* Link to above file. */
42 	assertMakeHardlink("l", "f");
43 
44 	/* Create file list (text mode here) */
45 	filelist = fopen("filelist", "w");
46 	assert(filelist != NULL);
47 	fprintf(filelist, "f\n");
48 	fprintf(filelist, "l\n");
49 	fclose(filelist);
50 
51 	/*
52 	 * Archive/dearchive with a variety of options, verifying
53 	 * stdio paths.
54 	 */
55 
56 	/* 'cf' should generate no output unless there's an error. */
57 	r = systemf("%s cf archive f l >cf.out 2>cf.err", testprog);
58 	assertEqualInt(r, 0);
59 	assertEmptyFile("cf.out");
60 	assertEmptyFile("cf.err");
61 
62 	/* 'cvf' should generate file list on stderr, empty stdout. */
63 	r = systemf("%s cvf archive f l >cvf.out 2>cvf.err", testprog);
64 	assertEqualInt(r, 0);
65 	failure("'cv' writes filenames to stderr, nothing to stdout (SUSv2)\n"
66 	    "Note that GNU tar writes the file list to stdout by default.");
67 	assertEmptyFile("cvf.out");
68 	/* TODO: Verify cvf.err has file list in SUSv2-prescribed format. */
69 
70 	/* 'cvf -' should generate file list on stderr, archive on stdout. */
71 	r = systemf("%s cvf - f l >cvf-.out 2>cvf-.err", testprog);
72 	assertEqualInt(r, 0);
73 	failure("cvf - should write archive to stdout");
74 	/* TODO: Verify cvf-.out has archive. */
75 	failure("cvf - should write file list to stderr (SUSv2)");
76 	/* TODO: Verify cvf-.err has verbose file list. */
77 
78 	/* 'tf' should generate file list on stdout, empty stderr. */
79 	r = systemf("%s tf archive >tf.out 2>tf.err", testprog);
80 	assertEqualInt(r, 0);
81 	assertEmptyFile("tf.err");
82 	failure("'t' mode should write results to stdout");
83 	/* TODO: Verify tf.out has file list. */
84 
85 	/* 'tvf' should generate file list on stdout, empty stderr. */
86 	r = systemf("%s tvf archive >tvf.out 2>tvf.err", testprog);
87 	assertEqualInt(r, 0);
88 	assertEmptyFile("tvf.err");
89 	failure("'tv' mode should write results to stdout");
90 	/* TODO: Verify tvf.out has file list. */
91 
92 	/* 'tvf -' uses stdin, file list on stdout, empty stderr. */
93 	r = systemf("%s tvf - < archive >tvf-.out 2>tvf-.err", testprog);
94 	assertEqualInt(r, 0);
95 	assertEmptyFile("tvf-.err");
96 	/* TODO: Verify tvf-.out has file list. */
97 
98 	/* Basic 'xf' should generate no output on stdout or stderr. */
99 	r = systemf("%s xf archive >xf.out 2>xf.err", testprog);
100 	assertEqualInt(r, 0);
101 	assertEmptyFile("xf.err");
102 	assertEmptyFile("xf.out");
103 
104 	/* 'xvf' should generate list on stderr, empty stdout. */
105 	r = systemf("%s xvf archive >xvf.out 2>xvf.err", testprog);
106 	assertEqualInt(r, 0);
107 	assertEmptyFile("xvf.out");
108 	/* TODO: Verify xvf.err */
109 
110 	/* 'xvOf' should generate list on stderr, file contents on stdout. */
111 	r = systemf("%s xvOf archive >xvOf.out 2>xvOf.err", testprog);
112 	assertEqualInt(r, 0);
113 	/* Verify xvOf.out is the file contents */
114 	p = slurpfile(&s, "xvOf.out");
115 	assertEqualInt((int)s, 3);
116 	assertEqualMem(p, "abc", 3);
117 	/* TODO: Verify xvf.err */
118 	free(p);
119 
120 	/* 'xvf -' should generate list on stderr, empty stdout. */
121 	r = systemf("%s xvf - < archive >xvf-.out 2>xvf-.err", testprog);
122 	assertEqualInt(r, 0);
123 	assertEmptyFile("xvf-.out");
124 	/* TODO: Verify xvf-.err */
125 }
126