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