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