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