xref: /minix3/external/bsd/libarchive/dist/tar/test/test_stdio.c (revision 543adbed3a3a783ed36434adafbc258b6bde442d)
1*543adbedSBen Gras /*-
2*543adbedSBen Gras  * Copyright (c) 2003-2007 Tim Kientzle
3*543adbedSBen Gras  * All rights reserved.
4*543adbedSBen Gras  *
5*543adbedSBen Gras  * Redistribution and use in source and binary forms, with or without
6*543adbedSBen Gras  * modification, are permitted provided that the following conditions
7*543adbedSBen Gras  * are met:
8*543adbedSBen Gras  * 1. Redistributions of source code must retain the above copyright
9*543adbedSBen Gras  *    notice, this list of conditions and the following disclaimer.
10*543adbedSBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
11*543adbedSBen Gras  *    notice, this list of conditions and the following disclaimer in the
12*543adbedSBen Gras  *    documentation and/or other materials provided with the distribution.
13*543adbedSBen Gras  *
14*543adbedSBen Gras  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15*543adbedSBen Gras  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16*543adbedSBen Gras  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17*543adbedSBen Gras  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18*543adbedSBen Gras  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19*543adbedSBen Gras  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20*543adbedSBen Gras  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21*543adbedSBen Gras  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22*543adbedSBen Gras  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23*543adbedSBen Gras  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24*543adbedSBen Gras  */
25*543adbedSBen Gras #include "test.h"
26*543adbedSBen Gras __FBSDID("$FreeBSD: src/usr.bin/tar/test/test_stdio.c,v 1.2 2008/05/26 17:10:10 kientzle Exp $");
27*543adbedSBen Gras 
DEFINE_TEST(test_stdio)28*543adbedSBen Gras DEFINE_TEST(test_stdio)
29*543adbedSBen Gras {
30*543adbedSBen Gras 	FILE *filelist;
31*543adbedSBen Gras 	char *p;
32*543adbedSBen Gras 	size_t s;
33*543adbedSBen Gras 	int r;
34*543adbedSBen Gras 
35*543adbedSBen Gras 	assertUmask(0);
36*543adbedSBen Gras 
37*543adbedSBen Gras 	/*
38*543adbedSBen Gras 	 * Create a couple of files on disk.
39*543adbedSBen Gras 	 */
40*543adbedSBen Gras 	/* File */
41*543adbedSBen Gras 	assertMakeFile("f", 0755, "abc");
42*543adbedSBen Gras 	/* Link to above file. */
43*543adbedSBen Gras 	assertMakeHardlink("l", "f");
44*543adbedSBen Gras 
45*543adbedSBen Gras 	/* Create file list (text mode here) */
46*543adbedSBen Gras 	filelist = fopen("filelist", "w");
47*543adbedSBen Gras 	assert(filelist != NULL);
48*543adbedSBen Gras 	fprintf(filelist, "f\n");
49*543adbedSBen Gras 	fprintf(filelist, "l\n");
50*543adbedSBen Gras 	fclose(filelist);
51*543adbedSBen Gras 
52*543adbedSBen Gras 	/*
53*543adbedSBen Gras 	 * Archive/dearchive with a variety of options, verifying
54*543adbedSBen Gras 	 * stdio paths.
55*543adbedSBen Gras 	 */
56*543adbedSBen Gras 
57*543adbedSBen Gras 	/* 'cf' should generate no output unless there's an error. */
58*543adbedSBen Gras 	r = systemf("%s cf archive f l >cf.out 2>cf.err", testprog);
59*543adbedSBen Gras 	assertEqualInt(r, 0);
60*543adbedSBen Gras 	assertEmptyFile("cf.out");
61*543adbedSBen Gras 	assertEmptyFile("cf.err");
62*543adbedSBen Gras 
63*543adbedSBen Gras 	/* 'cvf' should generate file list on stderr, empty stdout. */
64*543adbedSBen Gras 	r = systemf("%s cvf archive f l >cvf.out 2>cvf.err", testprog);
65*543adbedSBen Gras 	assertEqualInt(r, 0);
66*543adbedSBen Gras 	failure("'cv' writes filenames to stderr, nothing to stdout (SUSv2)\n"
67*543adbedSBen Gras 	    "Note that GNU tar writes the file list to stdout by default.");
68*543adbedSBen Gras 	assertEmptyFile("cvf.out");
69*543adbedSBen Gras 	/* TODO: Verify cvf.err has file list in SUSv2-prescribed format. */
70*543adbedSBen Gras 
71*543adbedSBen Gras 	/* 'cvf -' should generate file list on stderr, archive on stdout. */
72*543adbedSBen Gras 	r = systemf("%s cvf - f l >cvf-.out 2>cvf-.err", testprog);
73*543adbedSBen Gras 	assertEqualInt(r, 0);
74*543adbedSBen Gras 	failure("cvf - should write archive to stdout");
75*543adbedSBen Gras 	/* TODO: Verify cvf-.out has archive. */
76*543adbedSBen Gras 	failure("cvf - should write file list to stderr (SUSv2)");
77*543adbedSBen Gras 	/* TODO: Verify cvf-.err has verbose file list. */
78*543adbedSBen Gras 
79*543adbedSBen Gras 	/* 'tf' should generate file list on stdout, empty stderr. */
80*543adbedSBen Gras 	r = systemf("%s tf archive >tf.out 2>tf.err", testprog);
81*543adbedSBen Gras 	assertEqualInt(r, 0);
82*543adbedSBen Gras 	assertEmptyFile("tf.err");
83*543adbedSBen Gras 	failure("'t' mode should write results to stdout");
84*543adbedSBen Gras 	/* TODO: Verify tf.out has file list. */
85*543adbedSBen Gras 
86*543adbedSBen Gras 	/* 'tvf' should generate file list on stdout, empty stderr. */
87*543adbedSBen Gras 	r = systemf("%s tvf archive >tvf.out 2>tvf.err", testprog);
88*543adbedSBen Gras 	assertEqualInt(r, 0);
89*543adbedSBen Gras 	assertEmptyFile("tvf.err");
90*543adbedSBen Gras 	failure("'tv' mode should write results to stdout");
91*543adbedSBen Gras 	/* TODO: Verify tvf.out has file list. */
92*543adbedSBen Gras 
93*543adbedSBen Gras 	/* 'tvf -' uses stdin, file list on stdout, empty stderr. */
94*543adbedSBen Gras 	r = systemf("%s tvf - < archive >tvf-.out 2>tvf-.err", testprog);
95*543adbedSBen Gras 	assertEqualInt(r, 0);
96*543adbedSBen Gras 	assertEmptyFile("tvf-.err");
97*543adbedSBen Gras 	/* TODO: Verify tvf-.out has file list. */
98*543adbedSBen Gras 
99*543adbedSBen Gras 	/* Basic 'xf' should generate no output on stdout or stderr. */
100*543adbedSBen Gras 	r = systemf("%s xf archive >xf.out 2>xf.err", testprog);
101*543adbedSBen Gras 	assertEqualInt(r, 0);
102*543adbedSBen Gras 	assertEmptyFile("xf.err");
103*543adbedSBen Gras 	assertEmptyFile("xf.out");
104*543adbedSBen Gras 
105*543adbedSBen Gras 	/* 'xvf' should generate list on stderr, empty stdout. */
106*543adbedSBen Gras 	r = systemf("%s xvf archive >xvf.out 2>xvf.err", testprog);
107*543adbedSBen Gras 	assertEqualInt(r, 0);
108*543adbedSBen Gras 	assertEmptyFile("xvf.out");
109*543adbedSBen Gras 	/* TODO: Verify xvf.err */
110*543adbedSBen Gras 
111*543adbedSBen Gras 	/* 'xvOf' should generate list on stderr, file contents on stdout. */
112*543adbedSBen Gras 	r = systemf("%s xvOf archive >xvOf.out 2>xvOf.err", testprog);
113*543adbedSBen Gras 	assertEqualInt(r, 0);
114*543adbedSBen Gras 	/* Verify xvOf.out is the file contents */
115*543adbedSBen Gras 	p = slurpfile(&s, "xvOf.out");
116*543adbedSBen Gras 	assert(s = 3);
117*543adbedSBen Gras 	assertEqualMem(p, "abc", 3);
118*543adbedSBen Gras 	/* TODO: Verify xvf.err */
119*543adbedSBen Gras 
120*543adbedSBen Gras 	/* 'xvf -' should generate list on stderr, empty stdout. */
121*543adbedSBen Gras 	r = systemf("%s xvf - < archive >xvf-.out 2>xvf-.err", testprog);
122*543adbedSBen Gras 	assertEqualInt(r, 0);
123*543adbedSBen Gras 	assertEmptyFile("xvf-.out");
124*543adbedSBen Gras 	/* TODO: Verify xvf-.err */
125*543adbedSBen Gras }
126