xref: /freebsd/contrib/libarchive/tar/test/test_option_r.c (revision a7623790fb345e6dc986dfd31df0ace115e6f2e4)
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 /*
29  * Also see test_option_q for additional validation of -r support.
30  */
31 DEFINE_TEST(test_option_r)
32 {
33 	char *buff;
34 	char *p0, *p1;
35 	size_t buff_size = 35000;
36 	size_t s, buff_size_rounded;
37 	int r, i;
38 
39 	buff = NULL;
40 	p0 = NULL;
41 	p1 = NULL;
42 
43 	/* Create an archive with one file. */
44 	assertMakeFile("f1", 0644, "abc");
45 	r = systemf("%s cf archive.tar --format=ustar f1 >step1.out 2>step1.err", testprog);
46 	failure("Error invoking %s cf archive.tar f1", testprog);
47 	assertEqualInt(r, 0);
48 	assertEmptyFile("step1.out");
49 	assertEmptyFile("step1.err");
50 
51 	/* Do some basic validation of the constructed archive. */
52 	p0 = slurpfile(&s, "archive.tar");
53 	if (!assert(p0 != NULL))
54 		goto done;
55 	if (!assert(s >= 2048))
56 		goto done;
57 	assertEqualMem(p0 + 0, "f1", 3);
58 	assertEqualMem(p0 + 512, "abc", 3);
59 	assertEqualMem(p0 + 1024, "\0\0\0\0\0\0\0\0", 8);
60 	assertEqualMem(p0 + 1536, "\0\0\0\0\0\0\0\0", 8);
61 
62 	/* Edit that file with a lot more data and update the archive with a new copy. */
63 	buff = malloc(buff_size);
64 	assert(buff != NULL);
65 	if (buff == NULL)
66 		goto done;
67 
68 	for (i = 0; i < (int)buff_size; ++i)
69 		buff[i] = "abcdefghijklmnopqrstuvwxyz"[rand() % 26];
70 	buff[buff_size - 1] = '\0';
71 	assertMakeFile("f1", 0644, buff);
72 	r = systemf("%s rf archive.tar --format=ustar f1 >step2.out 2>step2.err", testprog);
73 	failure("Error invoking %s rf archive.tar f1", testprog);
74 	assertEqualInt(r, 0);
75 	assertEmptyFile("step2.out");
76 	assertEmptyFile("step2.err");
77 
78 	/* The constructed archive should just have the new entry appended. */
79 	p1 = slurpfile(&s, "archive.tar");
80 	if (!assert(p1 != NULL))
81 		goto done;
82 	buff_size_rounded = ((buff_size + 511) / 512) * 512;
83 	assert(s >= 2560 + buff_size_rounded);
84 	/* Verify first entry is unchanged. */
85 	assertEqualMem(p0, p1, 1024);
86 	/* Verify that second entry is correct. */
87 	assertEqualMem(p1 + 1024, "f1", 3);
88 	assertEqualMem(p1 + 1536, buff, buff_size);
89 	/* Verify end-of-archive marker. */
90 	assertEqualMem(p1 + 1536 + buff_size_rounded, "\0\0\0\0\0\0\0\0", 8);
91 	assertEqualMem(p1 + 2048 + buff_size_rounded, "\0\0\0\0\0\0\0\0", 8);
92 
93 	free(p0);
94 	p0 = p1;
95 
96 	/* Update the archive by adding a different file. */
97 	assertMakeFile("f2", 0644, "f2");
98 	r = systemf("%s rf archive.tar --format=ustar f2 >step3.out 2>step3.err", testprog);
99 	failure("Error invoking %s rf archive.tar f2", testprog);
100 	assertEqualInt(r, 0);
101 	assertEmptyFile("step3.out");
102 	assertEmptyFile("step3.err");
103 
104 	/* Validate the constructed archive. */
105 	p1 = slurpfile(&s, "archive.tar");
106 	if (!assert(p1 != NULL))
107 		goto done;
108 	assert(s >= 3584 + buff_size_rounded);
109 	/* Verify first two entries are unchanged. */
110 	assertEqualMem(p0, p1, 1536 + buff_size_rounded);
111 	/* Verify that new entry is correct. */
112 	assertEqualMem(p1 + 1536 + buff_size_rounded, "f2", 3);
113 	assertEqualMem(p1 + 2048 + buff_size_rounded, "f2", 3);
114 	/* Verify end-of-archive marker. */
115 	assertEqualMem(p1 + 2560 + buff_size_rounded, "\0\0\0\0\0\0\0\0", 8);
116 	assertEqualMem(p1 + 3072 + buff_size_rounded, "\0\0\0\0\0\0\0\0", 8);
117 	free(p1);
118 
119 	/* Unpack everything */
120 	assertMakeDir("extract", 0775);
121 	assertChdir("extract");
122 	r = systemf("%s xf ../archive.tar >extract.out 2>extract.err", testprog);
123 	failure("Error invoking %s xf archive.tar", testprog);
124 	assertEqualInt(r, 0);
125 	assertEmptyFile("extract.out");
126 	assertEmptyFile("extract.err");
127 
128 	/* Verify that the second copy of f1 overwrote the first. */
129 	assertFileContents(buff, (int)strlen(buff), "f1");
130 done:
131 	free(buff);
132 	free(p0);
133 }
134