Blame tar/test/test_option_r.c

Packit Service 1d0348
/*-
Packit Service 1d0348
 * Copyright (c) 2003-2007 Tim Kientzle
Packit Service 1d0348
 * All rights reserved.
Packit Service 1d0348
 *
Packit Service 1d0348
 * Redistribution and use in source and binary forms, with or without
Packit Service 1d0348
 * modification, are permitted provided that the following conditions
Packit Service 1d0348
 * are met:
Packit Service 1d0348
 * 1. Redistributions of source code must retain the above copyright
Packit Service 1d0348
 *    notice, this list of conditions and the following disclaimer.
Packit Service 1d0348
 * 2. Redistributions in binary form must reproduce the above copyright
Packit Service 1d0348
 *    notice, this list of conditions and the following disclaimer in the
Packit Service 1d0348
 *    documentation and/or other materials provided with the distribution.
Packit Service 1d0348
 *
Packit Service 1d0348
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
Packit Service 1d0348
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit Service 1d0348
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Packit Service 1d0348
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit Service 1d0348
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit Service 1d0348
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit Service 1d0348
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit Service 1d0348
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit Service 1d0348
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Packit Service 1d0348
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 1d0348
 */
Packit Service 1d0348
#include "test.h"
Packit Service 1d0348
__FBSDID("$FreeBSD$");
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Also see test_option_q for additional validation of -r support.
Packit Service 1d0348
 */
Packit Service 1d0348
DEFINE_TEST(test_option_r)
Packit Service 1d0348
{
Packit Service 1d0348
	char *buff;
Packit Service 1d0348
	char *p0, *p1;
Packit Service 1d0348
	size_t buff_size = 35000;
Packit Service 1d0348
	size_t s, buff_size_rounded;
Packit Service 1d0348
	int r, i;
Packit Service 1d0348
Packit Service 1d0348
	buff = NULL;
Packit Service 1d0348
	p0 = NULL;
Packit Service 1d0348
	p1 = NULL;
Packit Service 1d0348
Packit Service 1d0348
	/* Create an archive with one file. */
Packit Service 1d0348
	assertMakeFile("f1", 0644, "abc");
Packit Service 1d0348
	r = systemf("%s cf archive.tar --format=ustar f1 >step1.out 2>step1.err", testprog);
Packit Service 1d0348
	failure("Error invoking %s cf archive.tar f1", testprog);
Packit Service 1d0348
	assertEqualInt(r, 0);
Packit Service 1d0348
	assertEmptyFile("step1.out");
Packit Service 1d0348
	assertEmptyFile("step1.err");
Packit Service 1d0348
Packit Service 1d0348
	/* Do some basic validation of the constructed archive. */
Packit Service 1d0348
	p0 = slurpfile(&s, "archive.tar");
Packit Service 1d0348
	if (!assert(p0 != NULL))
Packit Service 1d0348
		goto done;
Packit Service 1d0348
	if (!assert(s >= 2048))
Packit Service 1d0348
		goto done;
Packit Service 1d0348
	assertEqualMem(p0 + 0, "f1", 3);
Packit Service 1d0348
	assertEqualMem(p0 + 512, "abc", 3);
Packit Service 1d0348
	assertEqualMem(p0 + 1024, "\0\0\0\0\0\0\0\0", 8);
Packit Service 1d0348
	assertEqualMem(p0 + 1536, "\0\0\0\0\0\0\0\0", 8);
Packit Service 1d0348
Packit Service 1d0348
	/* Edit that file with a lot more data and update the archive with a new copy. */
Packit Service 1d0348
	buff = malloc(buff_size);
Packit Service 1d0348
	assert(buff != NULL);
Packit Service 1d0348
	if (buff == NULL)
Packit Service 1d0348
		goto done;
Packit Service 1d0348
Packit Service 1d0348
	for (i = 0; i < (int)buff_size; ++i)
Packit Service 1d0348
		buff[i] = "abcdefghijklmnopqrstuvwxyz"[rand() % 26];
Packit Service 1d0348
	buff[buff_size - 1] = '\0';
Packit Service 1d0348
	assertMakeFile("f1", 0644, buff);
Packit Service 1d0348
	r = systemf("%s rf archive.tar --format=ustar f1 >step2.out 2>step2.err", testprog);
Packit Service 1d0348
	failure("Error invoking %s rf archive.tar f1", testprog);
Packit Service 1d0348
	assertEqualInt(r, 0);
Packit Service 1d0348
	assertEmptyFile("step2.out");
Packit Service 1d0348
	assertEmptyFile("step2.err");
Packit Service 1d0348
Packit Service 1d0348
	/* The constructed archive should just have the new entry appended. */
Packit Service 1d0348
	p1 = slurpfile(&s, "archive.tar");
Packit Service 1d0348
	if (!assert(p1 != NULL))
Packit Service 1d0348
		goto done;
Packit Service 1d0348
	buff_size_rounded = ((buff_size + 511) / 512) * 512;
Packit Service 1d0348
	assert(s >= 2560 + buff_size_rounded);
Packit Service 1d0348
	/* Verify first entry is unchanged. */
Packit Service 1d0348
	assertEqualMem(p0, p1, 1024);
Packit Service 1d0348
	/* Verify that second entry is correct. */
Packit Service 1d0348
	assertEqualMem(p1 + 1024, "f1", 3);
Packit Service 1d0348
	assertEqualMem(p1 + 1536, buff, buff_size);
Packit Service 1d0348
	/* Verify end-of-archive marker. */
Packit Service 1d0348
	assertEqualMem(p1 + 1536 + buff_size_rounded, "\0\0\0\0\0\0\0\0", 8);
Packit Service 1d0348
	assertEqualMem(p1 + 2048 + buff_size_rounded, "\0\0\0\0\0\0\0\0", 8);
Packit Service 1d0348
Packit Service 1d0348
	free(p0);
Packit Service 1d0348
	p0 = p1;
Packit Service 1d0348
Packit Service 1d0348
	/* Update the archive by adding a different file. */
Packit Service 1d0348
	assertMakeFile("f2", 0644, "f2");
Packit Service 1d0348
	r = systemf("%s rf archive.tar --format=ustar f2 >step3.out 2>step3.err", testprog);
Packit Service 1d0348
	failure("Error invoking %s rf archive.tar f2", testprog);
Packit Service 1d0348
	assertEqualInt(r, 0);
Packit Service 1d0348
	assertEmptyFile("step3.out");
Packit Service 1d0348
	assertEmptyFile("step3.err");
Packit Service 1d0348
Packit Service 1d0348
	/* Validate the constructed archive. */
Packit Service 1d0348
	p1 = slurpfile(&s, "archive.tar");
Packit Service 1d0348
	if (!assert(p1 != NULL))
Packit Service 1d0348
		goto done;
Packit Service 1d0348
	assert(s >= 3584 + buff_size_rounded);
Packit Service 1d0348
	/* Verify first two entries are unchanged. */
Packit Service 1d0348
	assertEqualMem(p0, p1, 1536 + buff_size_rounded);
Packit Service 1d0348
	/* Verify that new entry is correct. */
Packit Service 1d0348
	assertEqualMem(p1 + 1536 + buff_size_rounded, "f2", 3);
Packit Service 1d0348
	assertEqualMem(p1 + 2048 + buff_size_rounded, "f2", 3);
Packit Service 1d0348
	/* Verify end-of-archive marker. */
Packit Service 1d0348
	assertEqualMem(p1 + 2560 + buff_size_rounded, "\0\0\0\0\0\0\0\0", 8);
Packit Service 1d0348
	assertEqualMem(p1 + 3072 + buff_size_rounded, "\0\0\0\0\0\0\0\0", 8);
Packit Service 1d0348
	free(p1);
Packit Service 1d0348
Packit Service 1d0348
	/* Unpack everything */
Packit Service 1d0348
	assertMakeDir("extract", 0775);
Packit Service 1d0348
	assertChdir("extract");
Packit Service 1d0348
	r = systemf("%s xf ../archive.tar >extract.out 2>extract.err", testprog);
Packit Service 1d0348
	failure("Error invoking %s xf archive.tar", testprog);
Packit Service 1d0348
	assertEqualInt(r, 0);
Packit Service 1d0348
	assertEmptyFile("extract.out");
Packit Service 1d0348
	assertEmptyFile("extract.err");
Packit Service 1d0348
Packit Service 1d0348
	/* Verify that the second copy of f1 overwrote the first. */
Packit Service 1d0348
	assertFileContents(buff, (int)strlen(buff), "f1");
Packit Service 1d0348
done:
Packit Service 1d0348
	free(buff);
Packit Service 1d0348
	free(p0);
Packit Service 1d0348
}