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