]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/libarchive/tar/test/test_option_r.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / libarchive / tar / test / test_option_r.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 /*
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         /* Create an archive with one file. */
40         assertMakeFile("f1", 0644, "abc");
41         r = systemf("%s cf archive.tar --format=ustar f1 >step1.out 2>step1.err", testprog);
42         failure("Error invoking %s cf archive.tar f1", testprog);
43         assertEqualInt(r, 0);
44         assertEmptyFile("step1.out");
45         assertEmptyFile("step1.err");
46
47         /* Do some basic validation of the constructed archive. */
48         p0 = slurpfile(&s, "archive.tar");
49         if (!assert(p0 != NULL))
50                 return;
51         if (!assert(s >= 2048)) {
52                 free(p0);
53                 return;
54         }
55         assertEqualMem(p0 + 0, "f1", 3);
56         assertEqualMem(p0 + 512, "abc", 3);
57         assertEqualMem(p0 + 1024, "\0\0\0\0\0\0\0\0", 8);
58         assertEqualMem(p0 + 1536, "\0\0\0\0\0\0\0\0", 8);
59
60         /* Edit that file with a lot more data and update the archive with a new copy. */
61         buff = malloc(buff_size);
62         assert(buff != NULL);
63         if (buff == NULL) {
64                 free(p0);
65                 return;
66         }
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                 free(p0);
82                 return;
83         }
84         buff_size_rounded = ((buff_size + 511) / 512) * 512;
85         assert(s >= 2560 + buff_size_rounded);
86         /* Verify first entry is unchanged. */
87         assertEqualMem(p0, p1, 1024);
88         /* Verify that second entry is correct. */
89         assertEqualMem(p1 + 1024, "f1", 3);
90         assertEqualMem(p1 + 1536, buff, buff_size);
91         /* Verify end-of-archive marker. */
92         assertEqualMem(p1 + 1536 + buff_size_rounded, "\0\0\0\0\0\0\0\0", 8);
93         assertEqualMem(p1 + 2048 + buff_size_rounded, "\0\0\0\0\0\0\0\0", 8);
94
95         free(p0);
96         p0 = p1;
97
98         /* Update the archive by adding a different file. */
99         assertMakeFile("f2", 0644, "f2");
100         r = systemf("%s rf archive.tar --format=ustar f2 >step3.out 2>step3.err", testprog);
101         failure("Error invoking %s rf archive.tar f2", testprog);
102         assertEqualInt(r, 0);
103         assertEmptyFile("step3.out");
104         assertEmptyFile("step3.err");
105
106         /* Validate the constructed archive. */
107         p1 = slurpfile(&s, "archive.tar");
108         if (!assert(p1 != NULL)) {
109                 free(p0);
110                 return;
111         }
112         assert(s >= 3584 + buff_size_rounded);
113         /* Verify first two entries are unchanged. */
114         assertEqualMem(p0, p1, 1536 + buff_size_rounded);
115         /* Verify that new entry is correct. */
116         assertEqualMem(p1 + 1536 + buff_size_rounded, "f2", 3);
117         assertEqualMem(p1 + 2048 + buff_size_rounded, "f2", 3);
118         /* Verify end-of-archive marker. */
119         assertEqualMem(p1 + 2560 + buff_size_rounded, "\0\0\0\0\0\0\0\0", 8);
120         assertEqualMem(p1 + 3072 + buff_size_rounded, "\0\0\0\0\0\0\0\0", 8);
121         free(p0);
122         free(p1);
123
124         /* Unpack everything */
125         assertMakeDir("extract", 0775);
126         assertChdir("extract");
127         r = systemf("%s xf ../archive.tar >extract.out 2>extract.err", testprog);
128         failure("Error invoking %s xf archive.tar", testprog);
129         assertEqualInt(r, 0);
130         assertEmptyFile("extract.out");
131         assertEmptyFile("extract.err");
132
133         /* Verify that the second copy of f1 overwrote the first. */
134         assertFileContents(buff, (int)strlen(buff), "f1");
135 }