unstack, sort: cleanup and improvement
[minix.git] / commands / tar / test / test_option_r.c
blob516a8307936cd628a0d25595bf1af26d1016f776
1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
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.
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.
25 #include "test.h"
26 __FBSDID("$FreeBSD$");
29 * Also see test_option_q for additional validation of -r support.
31 DEFINE_TEST(test_option_r)
33 char buff[15];
34 char *p0, *p1;
35 size_t s;
36 FILE *f;
37 int r;
39 /* Create a file */
40 f = fopen("f1", "w");
41 if (!assert(f != NULL))
42 return;
43 assertEqualInt(3, fwrite("abc", 1, 3, f));
44 fclose(f);
46 /* Archive that one file. */
47 r = systemf("%s cf archive.tar --format=ustar f1 >step1.out 2>step1.err", testprog);
48 failure("Error invoking %s cf archive.tar f1", testprog);
49 assertEqualInt(r, 0);
51 /* Verify that nothing went to stdout or stderr. */
52 assertEmptyFile("step1.out");
53 assertEmptyFile("step1.err");
56 /* Do some basic validation of the constructed archive. */
57 p0 = slurpfile(&s, "archive.tar");
58 if (!assert(p0 != NULL))
59 return;
60 if (!assert(s >= 2048)) {
61 free(p0);
62 return;
64 assertEqualMem(p0 + 0, "f1", 3);
65 assertEqualMem(p0 + 512, "abc", 3);
66 assertEqualMem(p0 + 1024, "\0\0\0\0\0\0\0\0", 8);
67 assertEqualMem(p0 + 1536, "\0\0\0\0\0\0\0\0", 8);
69 /* Edit that file */
70 f = fopen("f1", "w");
71 if (!assert(f != NULL))
72 return;
73 assertEqualInt(3, fwrite("123", 1, 3, f));
74 fclose(f);
76 /* Update the archive. */
77 r = systemf("%s rf archive.tar --format=ustar f1 >step2.out 2>step2.err", testprog);
78 failure("Error invoking %s rf archive.tar f1", testprog);
79 assertEqualInt(r, 0);
81 /* Verify that nothing went to stdout or stderr. */
82 assertEmptyFile("step2.out");
83 assertEmptyFile("step2.err");
85 /* Do some basic validation of the constructed archive. */
86 p1 = slurpfile(&s, "archive.tar");
87 if (!assert(p1 != NULL)) {
88 free(p0);
89 return;
91 assert(s >= 3072);
92 /* Verify first entry is unchanged. */
93 assertEqualMem(p0, p1, 1024);
94 /* Verify that second entry is correct. */
95 assertEqualMem(p1 + 1024, "f1", 3);
96 assertEqualMem(p1 + 1536, "123", 3);
97 /* Verify end-of-archive marker. */
98 assertEqualMem(p1 + 2048, "\0\0\0\0\0\0\0\0", 8);
99 assertEqualMem(p1 + 2560, "\0\0\0\0\0\0\0\0", 8);
100 free(p0);
101 free(p1);
103 /* Unpack both items */
104 assertMakeDir("step3", 0775);
105 assertChdir("step3");
106 r = systemf("%s xf ../archive.tar", testprog);
107 failure("Error invoking %s xf archive.tar", testprog);
108 assertEqualInt(r, 0);
110 /* Verify that the second one overwrote the first. */
111 f = fopen("f1", "r");
112 if (assert(f != NULL)) {
113 assertEqualInt(3, fread(buff, 1, 3, f));
114 assertEqualMem(buff, "123", 3);
115 fclose(f);