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$");
27228753Smm
28228753Smm/*
29228753Smm * Also see test_option_q for additional validation of -r support.
30228753Smm */
31228753SmmDEFINE_TEST(test_option_r)
32228753Smm{
33228753Smm	char buff[15];
34228753Smm	char *p0, *p1;
35228753Smm	size_t s;
36228753Smm	FILE *f;
37228753Smm	int r;
38228753Smm
39228753Smm	/* Create a file */
40228753Smm	f = fopen("f1", "w");
41228753Smm	if (!assert(f != NULL))
42228753Smm		return;
43228753Smm	assertEqualInt(3, fwrite("abc", 1, 3, f));
44228753Smm	fclose(f);
45228753Smm
46228753Smm	/* Archive that one file. */
47228753Smm	r = systemf("%s cf archive.tar --format=ustar f1 >step1.out 2>step1.err", testprog);
48228753Smm	failure("Error invoking %s cf archive.tar f1", testprog);
49228753Smm	assertEqualInt(r, 0);
50228753Smm
51228753Smm	/* Verify that nothing went to stdout or stderr. */
52228753Smm	assertEmptyFile("step1.out");
53228753Smm	assertEmptyFile("step1.err");
54228753Smm
55228753Smm
56228753Smm	/* Do some basic validation of the constructed archive. */
57228753Smm	p0 = slurpfile(&s, "archive.tar");
58228753Smm	if (!assert(p0 != NULL))
59228753Smm		return;
60228753Smm	if (!assert(s >= 2048)) {
61228753Smm		free(p0);
62228753Smm		return;
63228753Smm	}
64228753Smm	assertEqualMem(p0 + 0, "f1", 3);
65228753Smm	assertEqualMem(p0 + 512, "abc", 3);
66228753Smm	assertEqualMem(p0 + 1024, "\0\0\0\0\0\0\0\0", 8);
67228753Smm	assertEqualMem(p0 + 1536, "\0\0\0\0\0\0\0\0", 8);
68228753Smm
69228753Smm	/* Edit that file */
70228753Smm	f = fopen("f1", "w");
71228753Smm	if (!assert(f != NULL))
72228753Smm		return;
73228753Smm	assertEqualInt(3, fwrite("123", 1, 3, f));
74228753Smm	fclose(f);
75228753Smm
76228753Smm	/* Update the archive. */
77228753Smm	r = systemf("%s rf archive.tar --format=ustar f1 >step2.out 2>step2.err", testprog);
78228753Smm	failure("Error invoking %s rf archive.tar f1", testprog);
79228753Smm	assertEqualInt(r, 0);
80228753Smm
81228753Smm	/* Verify that nothing went to stdout or stderr. */
82228753Smm	assertEmptyFile("step2.out");
83228753Smm	assertEmptyFile("step2.err");
84228753Smm
85228753Smm	/* Do some basic validation of the constructed archive. */
86228753Smm	p1 = slurpfile(&s, "archive.tar");
87228753Smm	if (!assert(p1 != NULL)) {
88228753Smm		free(p0);
89228753Smm		return;
90228753Smm	}
91228753Smm	assert(s >= 3072);
92228753Smm	/* Verify first entry is unchanged. */
93228753Smm	assertEqualMem(p0, p1, 1024);
94228753Smm	/* Verify that second entry is correct. */
95228753Smm	assertEqualMem(p1 + 1024, "f1", 3);
96228753Smm	assertEqualMem(p1 + 1536, "123", 3);
97228753Smm	/* Verify end-of-archive marker. */
98228753Smm	assertEqualMem(p1 + 2048, "\0\0\0\0\0\0\0\0", 8);
99228753Smm	assertEqualMem(p1 + 2560, "\0\0\0\0\0\0\0\0", 8);
100228753Smm	free(p0);
101228753Smm	free(p1);
102228753Smm
103228753Smm	/* Unpack both items */
104228753Smm	assertMakeDir("step3", 0775);
105228753Smm	assertChdir("step3");
106228753Smm	r = systemf("%s xf ../archive.tar", testprog);
107228753Smm	failure("Error invoking %s xf archive.tar", testprog);
108228753Smm	assertEqualInt(r, 0);
109228753Smm
110228753Smm	/* Verify that the second one overwrote the first. */
111228753Smm	f = fopen("f1", "r");
112228753Smm	if (assert(f != NULL)) {
113228753Smm		assertEqualInt(3, fread(buff, 1, 3, f));
114228753Smm		assertEqualMem(buff, "123", 3);
115228753Smm		fclose(f);
116228753Smm	}
117228753Smm}
118