]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - usr.bin/tar/test/test_stdio.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / usr.bin / tar / test / test_stdio.c
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         int fd;
31         int filelist;
32         int oldumask;
33         int r;
34
35         oldumask = umask(0);
36
37         /*
38          * Create a couple of files on disk.
39          */
40         filelist = open("filelist", O_CREAT | O_WRONLY, 0644);
41         /* File */
42         fd = open("f", O_CREAT | O_WRONLY, 0644);
43         assert(fd >= 0);
44         write(fd, "f\n", 2);
45         close(fd);
46         write(filelist, "f\n", 2);
47         /* Link to above file. */
48         assertEqualInt(0, link("f", "l"));
49         write(filelist, "l\n", 2);
50         close(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         /* TODO: Verify xvOf.out */
115         /* TODO: Verify xvf.err */
116
117         /* 'xvf -' should generate list on stderr, empty stdout. */
118         r = systemf("%s xvf - < archive >xvf-.out 2>xvf-.err", testprog);
119         assertEqualInt(r, 0);
120         assertEmptyFile("xvf-.out");
121         /* TODO: Verify xvf-.err */
122
123         umask(oldumask);
124 }