1228753Smm/*-
2228753Smm * Copyright (c) 2003-2007 Tim Kientzle
3228753Smm * All rights reserved.
4228753Smm *
5228753Smm * Redistribution and use in source and binary forms, with or without
6228753Smm * modification, are permitted provided that the following conditions
7228753Smm * are met:
8228753Smm * 1. Redistributions of source code must retain the above copyright
9228753Smm *    notice, this list of conditions and the following disclaimer.
10228753Smm * 2. Redistributions in binary form must reproduce the above copyright
11228753Smm *    notice, this list of conditions and the following disclaimer in the
12228753Smm *    documentation and/or other materials provided with the distribution.
13228753Smm *
14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24228753Smm */
25228753Smm#include "test.h"
26229592Smm__FBSDID("$FreeBSD$");
27228753Smm
28228753SmmDEFINE_TEST(test_stdio)
29228753Smm{
30228753Smm	FILE *filelist;
31228753Smm	char *p;
32228753Smm	size_t s;
33228753Smm	int r;
34228753Smm
35228753Smm	assertUmask(0);
36228753Smm
37228753Smm	/*
38228753Smm	 * Create a couple of files on disk.
39228753Smm	 */
40228753Smm	/* File */
41228753Smm	assertMakeFile("f", 0755, "abc");
42228753Smm	/* Link to above file. */
43228753Smm	assertMakeHardlink("l", "f");
44228753Smm
45228753Smm	/* Create file list (text mode here) */
46228753Smm	filelist = fopen("filelist", "w");
47228753Smm	assert(filelist != NULL);
48228753Smm	fprintf(filelist, "f\n");
49228753Smm	fprintf(filelist, "l\n");
50228753Smm	fclose(filelist);
51228753Smm
52228753Smm	/*
53228753Smm	 * Archive/dearchive with a variety of options, verifying
54228753Smm	 * stdio paths.
55228753Smm	 */
56228753Smm
57228753Smm	/* 'cf' should generate no output unless there's an error. */
58228753Smm	r = systemf("%s cf archive f l >cf.out 2>cf.err", testprog);
59228753Smm	assertEqualInt(r, 0);
60228753Smm	assertEmptyFile("cf.out");
61228753Smm	assertEmptyFile("cf.err");
62228753Smm
63228753Smm	/* 'cvf' should generate file list on stderr, empty stdout. */
64228753Smm	r = systemf("%s cvf archive f l >cvf.out 2>cvf.err", testprog);
65228753Smm	assertEqualInt(r, 0);
66228753Smm	failure("'cv' writes filenames to stderr, nothing to stdout (SUSv2)\n"
67228753Smm	    "Note that GNU tar writes the file list to stdout by default.");
68228753Smm	assertEmptyFile("cvf.out");
69228753Smm	/* TODO: Verify cvf.err has file list in SUSv2-prescribed format. */
70228753Smm
71228753Smm	/* 'cvf -' should generate file list on stderr, archive on stdout. */
72228753Smm	r = systemf("%s cvf - f l >cvf-.out 2>cvf-.err", testprog);
73228753Smm	assertEqualInt(r, 0);
74228753Smm	failure("cvf - should write archive to stdout");
75228753Smm	/* TODO: Verify cvf-.out has archive. */
76228753Smm	failure("cvf - should write file list to stderr (SUSv2)");
77228753Smm	/* TODO: Verify cvf-.err has verbose file list. */
78228753Smm
79228753Smm	/* 'tf' should generate file list on stdout, empty stderr. */
80228753Smm	r = systemf("%s tf archive >tf.out 2>tf.err", testprog);
81228753Smm	assertEqualInt(r, 0);
82228753Smm	assertEmptyFile("tf.err");
83228753Smm	failure("'t' mode should write results to stdout");
84228753Smm	/* TODO: Verify tf.out has file list. */
85228753Smm
86228753Smm	/* 'tvf' should generate file list on stdout, empty stderr. */
87228753Smm	r = systemf("%s tvf archive >tvf.out 2>tvf.err", testprog);
88228753Smm	assertEqualInt(r, 0);
89228753Smm	assertEmptyFile("tvf.err");
90228753Smm	failure("'tv' mode should write results to stdout");
91228753Smm	/* TODO: Verify tvf.out has file list. */
92228753Smm
93228753Smm	/* 'tvf -' uses stdin, file list on stdout, empty stderr. */
94228753Smm	r = systemf("%s tvf - < archive >tvf-.out 2>tvf-.err", testprog);
95228753Smm	assertEqualInt(r, 0);
96228753Smm	assertEmptyFile("tvf-.err");
97228753Smm	/* TODO: Verify tvf-.out has file list. */
98228753Smm
99228753Smm	/* Basic 'xf' should generate no output on stdout or stderr. */
100228753Smm	r = systemf("%s xf archive >xf.out 2>xf.err", testprog);
101228753Smm	assertEqualInt(r, 0);
102228753Smm	assertEmptyFile("xf.err");
103228753Smm	assertEmptyFile("xf.out");
104228753Smm
105228753Smm	/* 'xvf' should generate list on stderr, empty stdout. */
106228753Smm	r = systemf("%s xvf archive >xvf.out 2>xvf.err", testprog);
107228753Smm	assertEqualInt(r, 0);
108228753Smm	assertEmptyFile("xvf.out");
109228753Smm	/* TODO: Verify xvf.err */
110228753Smm
111228753Smm	/* 'xvOf' should generate list on stderr, file contents on stdout. */
112228753Smm	r = systemf("%s xvOf archive >xvOf.out 2>xvOf.err", testprog);
113228753Smm	assertEqualInt(r, 0);
114228753Smm	/* Verify xvOf.out is the file contents */
115228753Smm	p = slurpfile(&s, "xvOf.out");
116228753Smm	assert(s = 3);
117228753Smm	assertEqualMem(p, "abc", 3);
118228753Smm	/* TODO: Verify xvf.err */
119228753Smm
120228753Smm	/* 'xvf -' should generate list on stderr, empty stdout. */
121228753Smm	r = systemf("%s xvf - < archive >xvf-.out 2>xvf-.err", testprog);
122228753Smm	assertEqualInt(r, 0);
123228753Smm	assertEmptyFile("xvf-.out");
124228753Smm	/* TODO: Verify xvf-.err */
125228753Smm}
126