test_option_r.c revision 248616
1162271Srwatson/*- 2162271Srwatson * Copyright (c) 2003-2007 Tim Kientzle 3172106Srwatson * All rights reserved. 4162271Srwatson * 5162271Srwatson * Redistribution and use in source and binary forms, with or without 6162271Srwatson * modification, are permitted provided that the following conditions 7162271Srwatson * are met: 8162271Srwatson * 1. Redistributions of source code must retain the above copyright 9162271Srwatson * notice, this list of conditions and the following disclaimer. 10162271Srwatson * 2. Redistributions in binary form must reproduce the above copyright 11162271Srwatson * notice, this list of conditions and the following disclaimer in the 12162271Srwatson * documentation and/or other materials provided with the distribution. 13162271Srwatson * 14162271Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15162271Srwatson * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16162271Srwatson * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17162271Srwatson * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18162271Srwatson * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19162271Srwatson * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20162271Srwatson * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21162271Srwatson * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22162271Srwatson * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23162271Srwatson * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24162271Srwatson */ 25162271Srwatson#include "test.h" 26162271Srwatson__FBSDID("$FreeBSD: head/contrib/libarchive/tar/test/test_option_r.c 248616 2013-03-22 13:36:03Z mm $"); 27162271Srwatson 28162271Srwatson/* 29162271Srwatson * Also see test_option_q for additional validation of -r support. 30162271Srwatson */ 31162271SrwatsonDEFINE_TEST(test_option_r) 32162271Srwatson{ 33162271Srwatson char *buff; 34172106Srwatson char *p0, *p1; 35162271Srwatson size_t buff_size = 35000; 36162271Srwatson size_t s, buff_size_rounded; 37162271Srwatson int r, i; 38162271Srwatson 39162271Srwatson /* Create an archive with one file. */ 40162271Srwatson assertMakeFile("f1", 0644, "abc"); 41162271Srwatson r = systemf("%s cf archive.tar --format=ustar f1 >step1.out 2>step1.err", testprog); 42162271Srwatson failure("Error invoking %s cf archive.tar f1", testprog); 43162271Srwatson assertEqualInt(r, 0); 44162271Srwatson assertEmptyFile("step1.out"); 45162271Srwatson assertEmptyFile("step1.err"); 46172106Srwatson 47172106Srwatson /* Do some basic validation of the constructed archive. */ 48172106Srwatson p0 = slurpfile(&s, "archive.tar"); 49172106Srwatson if (!assert(p0 != NULL)) 50172106Srwatson return; 51172106Srwatson if (!assert(s >= 2048)) { 52172106Srwatson free(p0); 53162271Srwatson return; 54172106Srwatson } 55162271Srwatson assertEqualMem(p0 + 0, "f1", 3); 56162271Srwatson assertEqualMem(p0 + 512, "abc", 3); 57162271Srwatson assertEqualMem(p0 + 1024, "\0\0\0\0\0\0\0\0", 8); 58172106Srwatson assertEqualMem(p0 + 1536, "\0\0\0\0\0\0\0\0", 8); 59172106Srwatson 60172106Srwatson /* Edit that file with a lot more data and update the archive with a new copy. */ 61172106Srwatson buff = malloc(buff_size); 62172106Srwatson assert(buff != NULL); 63172106Srwatson if (buff == NULL) { 64172106Srwatson free(p0); 65172106Srwatson return; 66172106Srwatson } 67172106Srwatson 68162271Srwatson for (i = 0; i < (int)buff_size; ++i) 69172106Srwatson buff[i] = "abcdefghijklmnopqrstuvwxyz"[rand() % 26]; 70172106Srwatson buff[buff_size - 1] = '\0'; 71172106Srwatson assertMakeFile("f1", 0644, buff); 72162271Srwatson r = systemf("%s rf archive.tar --format=ustar f1 >step2.out 2>step2.err", testprog); 73162271Srwatson 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} 136