1228753Smm/*- 2228753Smm * Copyright (c) 2003-2007 Tim Kientzle 3228753Smm * All rights reserved. 4228753Smm * 5228753Smm * Redistribution and use in source and binary forms, with or without 6228753Smm * modification, are permitted provided that the following conditions 7228753Smm * are met: 8228753Smm * 1. Redistributions of source code must retain the above copyright 9228753Smm * notice, this list of conditions and the following disclaimer. 10228753Smm * 2. Redistributions in binary form must reproduce the above copyright 11228753Smm * notice, this list of conditions and the following disclaimer in the 12228753Smm * documentation and/or other materials provided with the distribution. 13228753Smm * 14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24228753Smm */ 25228753Smm#include "test.h" 26228753Smm__FBSDID("$FreeBSD: stable/11/contrib/libarchive/tar/test/test_option_r.c 310569 2016-12-26 06:16:27Z ngie $"); 27228753Smm 28228753Smm/* 29228753Smm * Also see test_option_q for additional validation of -r support. 30228753Smm */ 31228753SmmDEFINE_TEST(test_option_r) 32228753Smm{ 33232153Smm char *buff; 34228753Smm char *p0, *p1; 35232153Smm size_t buff_size = 35000; 36232153Smm size_t s, buff_size_rounded; 37232153Smm int r, i; 38228753Smm 39310569Sngie buff = NULL; 40310569Sngie p0 = NULL; 41310569Sngie p1 = NULL; 42310569Sngie 43232153Smm /* Create an archive with one file. */ 44232153Smm assertMakeFile("f1", 0644, "abc"); 45228753Smm r = systemf("%s cf archive.tar --format=ustar f1 >step1.out 2>step1.err", testprog); 46228753Smm failure("Error invoking %s cf archive.tar f1", testprog); 47228753Smm assertEqualInt(r, 0); 48228753Smm assertEmptyFile("step1.out"); 49228753Smm assertEmptyFile("step1.err"); 50228753Smm 51228753Smm /* Do some basic validation of the constructed archive. */ 52228753Smm p0 = slurpfile(&s, "archive.tar"); 53228753Smm if (!assert(p0 != NULL)) 54310569Sngie goto done; 55310569Sngie if (!assert(s >= 2048)) 56310569Sngie goto done; 57228753Smm assertEqualMem(p0 + 0, "f1", 3); 58228753Smm assertEqualMem(p0 + 512, "abc", 3); 59228753Smm assertEqualMem(p0 + 1024, "\0\0\0\0\0\0\0\0", 8); 60228753Smm assertEqualMem(p0 + 1536, "\0\0\0\0\0\0\0\0", 8); 61228753Smm 62232153Smm /* Edit that file with a lot more data and update the archive with a new copy. */ 63232153Smm buff = malloc(buff_size); 64232153Smm assert(buff != NULL); 65310569Sngie if (buff == NULL) 66310569Sngie goto done; 67248616Smm 68232153Smm for (i = 0; i < (int)buff_size; ++i) 69232153Smm buff[i] = "abcdefghijklmnopqrstuvwxyz"[rand() % 26]; 70232153Smm buff[buff_size - 1] = '\0'; 71232153Smm assertMakeFile("f1", 0644, buff); 72228753Smm r = systemf("%s rf archive.tar --format=ustar f1 >step2.out 2>step2.err", testprog); 73228753Smm failure("Error invoking %s rf archive.tar f1", testprog); 74228753Smm assertEqualInt(r, 0); 75228753Smm assertEmptyFile("step2.out"); 76228753Smm assertEmptyFile("step2.err"); 77228753Smm 78232153Smm /* The constructed archive should just have the new entry appended. */ 79228753Smm p1 = slurpfile(&s, "archive.tar"); 80310569Sngie if (!assert(p1 != NULL)) 81310569Sngie goto done; 82232153Smm buff_size_rounded = ((buff_size + 511) / 512) * 512; 83232153Smm assert(s >= 2560 + buff_size_rounded); 84228753Smm /* Verify first entry is unchanged. */ 85228753Smm assertEqualMem(p0, p1, 1024); 86228753Smm /* Verify that second entry is correct. */ 87228753Smm assertEqualMem(p1 + 1024, "f1", 3); 88232153Smm assertEqualMem(p1 + 1536, buff, buff_size); 89228753Smm /* Verify end-of-archive marker. */ 90232153Smm assertEqualMem(p1 + 1536 + buff_size_rounded, "\0\0\0\0\0\0\0\0", 8); 91232153Smm assertEqualMem(p1 + 2048 + buff_size_rounded, "\0\0\0\0\0\0\0\0", 8); 92232153Smm 93228753Smm free(p0); 94232153Smm p0 = p1; 95232153Smm 96232153Smm /* Update the archive by adding a different file. */ 97232153Smm assertMakeFile("f2", 0644, "f2"); 98232153Smm r = systemf("%s rf archive.tar --format=ustar f2 >step3.out 2>step3.err", testprog); 99232153Smm failure("Error invoking %s rf archive.tar f2", testprog); 100232153Smm assertEqualInt(r, 0); 101232153Smm assertEmptyFile("step3.out"); 102232153Smm assertEmptyFile("step3.err"); 103232153Smm 104232153Smm /* Validate the constructed archive. */ 105232153Smm p1 = slurpfile(&s, "archive.tar"); 106310569Sngie if (!assert(p1 != NULL)) 107310569Sngie goto done; 108232153Smm assert(s >= 3584 + buff_size_rounded); 109232153Smm /* Verify first two entries are unchanged. */ 110232153Smm assertEqualMem(p0, p1, 1536 + buff_size_rounded); 111232153Smm /* Verify that new entry is correct. */ 112232153Smm assertEqualMem(p1 + 1536 + buff_size_rounded, "f2", 3); 113232153Smm assertEqualMem(p1 + 2048 + buff_size_rounded, "f2", 3); 114232153Smm /* Verify end-of-archive marker. */ 115232153Smm assertEqualMem(p1 + 2560 + buff_size_rounded, "\0\0\0\0\0\0\0\0", 8); 116232153Smm assertEqualMem(p1 + 3072 + buff_size_rounded, "\0\0\0\0\0\0\0\0", 8); 117228753Smm free(p1); 118228753Smm 119232153Smm /* Unpack everything */ 120232153Smm assertMakeDir("extract", 0775); 121232153Smm assertChdir("extract"); 122232153Smm r = systemf("%s xf ../archive.tar >extract.out 2>extract.err", testprog); 123228753Smm failure("Error invoking %s xf archive.tar", testprog); 124228753Smm assertEqualInt(r, 0); 125232153Smm assertEmptyFile("extract.out"); 126232153Smm assertEmptyFile("extract.err"); 127228753Smm 128232153Smm /* Verify that the second copy of f1 overwrote the first. */ 129248616Smm assertFileContents(buff, (int)strlen(buff), "f1"); 130310569Sngiedone: 131310569Sngie free(buff); 132310569Sngie free(p0); 133228753Smm} 134