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 for (i = 0; i < (int)buff_size; ++i) 64 buff[i] = "abcdefghijklmnopqrstuvwxyz"[rand() % 26]; 65 buff[buff_size - 1] = '\0'; 66 assertMakeFile("f1", 0644, buff); 67 r = systemf("%s rf archive.tar --format=ustar f1 >step2.out 2>step2.err", testprog); 68 failure("Error invoking %s rf archive.tar f1", testprog); 69 assertEqualInt(r, 0); 70 assertEmptyFile("step2.out"); 71 assertEmptyFile("step2.err"); 72 73 /* The constructed archive should just have the new entry appended. */ 74 p1 = slurpfile(&s, "archive.tar"); 75 if (!assert(p1 != NULL)) { 76 free(p0); 77 return; 78 } 79 buff_size_rounded = ((buff_size + 511) / 512) * 512; 80 assert(s >= 2560 + buff_size_rounded); 81 /* Verify first entry is unchanged. */ 82 assertEqualMem(p0, p1, 1024); 83 /* Verify that second entry is correct. */ 84 assertEqualMem(p1 + 1024, "f1", 3); 85 assertEqualMem(p1 + 1536, buff, buff_size); 86 /* Verify end-of-archive marker. */ 87 assertEqualMem(p1 + 1536 + buff_size_rounded, "\0\0\0\0\0\0\0\0", 8); 88 assertEqualMem(p1 + 2048 + buff_size_rounded, "\0\0\0\0\0\0\0\0", 8); 89 90 free(p0); 91 p0 = p1; 92 93 /* Update the archive by adding a different file. */ 94 assertMakeFile("f2", 0644, "f2"); 95 r = systemf("%s rf archive.tar --format=ustar f2 >step3.out 2>step3.err", testprog); 96 failure("Error invoking %s rf archive.tar f2", testprog); 97 assertEqualInt(r, 0); 98 assertEmptyFile("step3.out"); 99 assertEmptyFile("step3.err"); 100 101 /* Validate the constructed archive. */ 102 p1 = slurpfile(&s, "archive.tar"); 103 if (!assert(p1 != NULL)) { 104 free(p0); 105 return; 106 } 107 assert(s >= 3584 + buff_size_rounded); 108 /* Verify first two entries are unchanged. */ 109 assertEqualMem(p0, p1, 1536 + buff_size_rounded); 110 /* Verify that new entry is correct. */ 111 assertEqualMem(p1 + 1536 + buff_size_rounded, "f2", 3); 112 assertEqualMem(p1 + 2048 + buff_size_rounded, "f2", 3); 113 /* Verify end-of-archive marker. */ 114 assertEqualMem(p1 + 2560 + buff_size_rounded, "\0\0\0\0\0\0\0\0", 8); 115 assertEqualMem(p1 + 3072 + buff_size_rounded, "\0\0\0\0\0\0\0\0", 8); 116 free(p0); 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, strlen(buff), "f1"); 130 } 131