1caf54c4fSMartin Matuska /*-
2*bd66c1b4SMartin Matuska * SPDX-License-Identifier: BSD-2-Clause
3*bd66c1b4SMartin Matuska *
4caf54c4fSMartin Matuska * Copyright (c) 2003-2007 Tim Kientzle
5caf54c4fSMartin Matuska * All rights reserved.
6caf54c4fSMartin Matuska */
7caf54c4fSMartin Matuska #include "test.h"
8caf54c4fSMartin Matuska
9caf54c4fSMartin Matuska /*
10caf54c4fSMartin Matuska * Also see test_option_q for additional validation of -r support.
11caf54c4fSMartin Matuska */
DEFINE_TEST(test_option_r)12caf54c4fSMartin Matuska DEFINE_TEST(test_option_r)
13caf54c4fSMartin Matuska {
146c95142eSMartin Matuska char *buff;
15caf54c4fSMartin Matuska char *p0, *p1;
166c95142eSMartin Matuska size_t buff_size = 35000;
176c95142eSMartin Matuska size_t s, buff_size_rounded;
186c95142eSMartin Matuska int r, i;
19caf54c4fSMartin Matuska
2063ecfce8SEnji Cooper buff = NULL;
2163ecfce8SEnji Cooper p0 = NULL;
2263ecfce8SEnji Cooper p1 = NULL;
2363ecfce8SEnji Cooper
246c95142eSMartin Matuska /* Create an archive with one file. */
256c95142eSMartin Matuska assertMakeFile("f1", 0644, "abc");
26caf54c4fSMartin Matuska r = systemf("%s cf archive.tar --format=ustar f1 >step1.out 2>step1.err", testprog);
27caf54c4fSMartin Matuska failure("Error invoking %s cf archive.tar f1", testprog);
28caf54c4fSMartin Matuska assertEqualInt(r, 0);
29caf54c4fSMartin Matuska assertEmptyFile("step1.out");
30caf54c4fSMartin Matuska assertEmptyFile("step1.err");
31caf54c4fSMartin Matuska
32caf54c4fSMartin Matuska /* Do some basic validation of the constructed archive. */
33caf54c4fSMartin Matuska p0 = slurpfile(&s, "archive.tar");
34caf54c4fSMartin Matuska if (!assert(p0 != NULL))
3563ecfce8SEnji Cooper goto done;
3663ecfce8SEnji Cooper if (!assert(s >= 2048))
3763ecfce8SEnji Cooper goto done;
38caf54c4fSMartin Matuska assertEqualMem(p0 + 0, "f1", 3);
39caf54c4fSMartin Matuska assertEqualMem(p0 + 512, "abc", 3);
40caf54c4fSMartin Matuska assertEqualMem(p0 + 1024, "\0\0\0\0\0\0\0\0", 8);
41caf54c4fSMartin Matuska assertEqualMem(p0 + 1536, "\0\0\0\0\0\0\0\0", 8);
42caf54c4fSMartin Matuska
436c95142eSMartin Matuska /* Edit that file with a lot more data and update the archive with a new copy. */
446c95142eSMartin Matuska buff = malloc(buff_size);
456c95142eSMartin Matuska assert(buff != NULL);
4663ecfce8SEnji Cooper if (buff == NULL)
4763ecfce8SEnji Cooper goto done;
48acc60b03SMartin Matuska
496c95142eSMartin Matuska for (i = 0; i < (int)buff_size; ++i)
506c95142eSMartin Matuska buff[i] = "abcdefghijklmnopqrstuvwxyz"[rand() % 26];
516c95142eSMartin Matuska buff[buff_size - 1] = '\0';
526c95142eSMartin Matuska assertMakeFile("f1", 0644, buff);
53caf54c4fSMartin Matuska r = systemf("%s rf archive.tar --format=ustar f1 >step2.out 2>step2.err", testprog);
54caf54c4fSMartin Matuska failure("Error invoking %s rf archive.tar f1", testprog);
55caf54c4fSMartin Matuska assertEqualInt(r, 0);
56caf54c4fSMartin Matuska assertEmptyFile("step2.out");
57caf54c4fSMartin Matuska assertEmptyFile("step2.err");
58caf54c4fSMartin Matuska
596c95142eSMartin Matuska /* The constructed archive should just have the new entry appended. */
60caf54c4fSMartin Matuska p1 = slurpfile(&s, "archive.tar");
6163ecfce8SEnji Cooper if (!assert(p1 != NULL))
6263ecfce8SEnji Cooper goto done;
636c95142eSMartin Matuska buff_size_rounded = ((buff_size + 511) / 512) * 512;
646c95142eSMartin Matuska assert(s >= 2560 + buff_size_rounded);
65caf54c4fSMartin Matuska /* Verify first entry is unchanged. */
66caf54c4fSMartin Matuska assertEqualMem(p0, p1, 1024);
67caf54c4fSMartin Matuska /* Verify that second entry is correct. */
68caf54c4fSMartin Matuska assertEqualMem(p1 + 1024, "f1", 3);
696c95142eSMartin Matuska assertEqualMem(p1 + 1536, buff, buff_size);
70caf54c4fSMartin Matuska /* Verify end-of-archive marker. */
716c95142eSMartin Matuska assertEqualMem(p1 + 1536 + buff_size_rounded, "\0\0\0\0\0\0\0\0", 8);
726c95142eSMartin Matuska assertEqualMem(p1 + 2048 + buff_size_rounded, "\0\0\0\0\0\0\0\0", 8);
736c95142eSMartin Matuska
746c95142eSMartin Matuska free(p0);
756c95142eSMartin Matuska p0 = p1;
766c95142eSMartin Matuska
776c95142eSMartin Matuska /* Update the archive by adding a different file. */
786c95142eSMartin Matuska assertMakeFile("f2", 0644, "f2");
796c95142eSMartin Matuska r = systemf("%s rf archive.tar --format=ustar f2 >step3.out 2>step3.err", testprog);
806c95142eSMartin Matuska failure("Error invoking %s rf archive.tar f2", testprog);
816c95142eSMartin Matuska assertEqualInt(r, 0);
826c95142eSMartin Matuska assertEmptyFile("step3.out");
836c95142eSMartin Matuska assertEmptyFile("step3.err");
846c95142eSMartin Matuska
856c95142eSMartin Matuska /* Validate the constructed archive. */
866c95142eSMartin Matuska p1 = slurpfile(&s, "archive.tar");
8763ecfce8SEnji Cooper if (!assert(p1 != NULL))
8863ecfce8SEnji Cooper goto done;
896c95142eSMartin Matuska assert(s >= 3584 + buff_size_rounded);
906c95142eSMartin Matuska /* Verify first two entries are unchanged. */
916c95142eSMartin Matuska assertEqualMem(p0, p1, 1536 + buff_size_rounded);
926c95142eSMartin Matuska /* Verify that new entry is correct. */
936c95142eSMartin Matuska assertEqualMem(p1 + 1536 + buff_size_rounded, "f2", 3);
946c95142eSMartin Matuska assertEqualMem(p1 + 2048 + buff_size_rounded, "f2", 3);
956c95142eSMartin Matuska /* Verify end-of-archive marker. */
966c95142eSMartin Matuska assertEqualMem(p1 + 2560 + buff_size_rounded, "\0\0\0\0\0\0\0\0", 8);
976c95142eSMartin Matuska assertEqualMem(p1 + 3072 + buff_size_rounded, "\0\0\0\0\0\0\0\0", 8);
98caf54c4fSMartin Matuska free(p1);
99caf54c4fSMartin Matuska
1006c95142eSMartin Matuska /* Unpack everything */
1016c95142eSMartin Matuska assertMakeDir("extract", 0775);
1026c95142eSMartin Matuska assertChdir("extract");
1036c95142eSMartin Matuska r = systemf("%s xf ../archive.tar >extract.out 2>extract.err", testprog);
104caf54c4fSMartin Matuska failure("Error invoking %s xf archive.tar", testprog);
105caf54c4fSMartin Matuska assertEqualInt(r, 0);
1066c95142eSMartin Matuska assertEmptyFile("extract.out");
1076c95142eSMartin Matuska assertEmptyFile("extract.err");
108caf54c4fSMartin Matuska
1096c95142eSMartin Matuska /* Verify that the second copy of f1 overwrote the first. */
110acc60b03SMartin Matuska assertFileContents(buff, (int)strlen(buff), "f1");
11163ecfce8SEnji Cooper done:
11263ecfce8SEnji Cooper free(buff);
11363ecfce8SEnji Cooper free(p0);
114caf54c4fSMartin Matuska }
115