xref: /freebsd/contrib/libarchive/tar/test/test_option_b.c (revision b64c5a0ace59af62eff52bfe110a521dc73c937b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2010 Tim Kientzle
5  * All rights reserved.
6  */
7 #include "test.h"
8 
9 #define USTAR_OPT " --format=ustar"
10 
11 DEFINE_TEST(test_option_b)
12 {
13 	char *testprog_ustar;
14 	size_t testprog_ustar_len;
15 
16 	assertMakeFile("file1", 0644, "file1");
17 	if (systemf("cat file1 > test_cat.out 2> test_cat.err") != 0) {
18 		skipping("This test requires a `cat` program");
19 		return;
20 	}
21 	testprog_ustar_len = strlen(testprog) + sizeof(USTAR_OPT) + 1;
22 	testprog_ustar = malloc(testprog_ustar_len);
23 	strncpy(testprog_ustar, testprog, testprog_ustar_len);
24 	strncat(testprog_ustar, USTAR_OPT, testprog_ustar_len);
25 
26 	/*
27 	 * Bsdtar does not pad if the output is going directly to a disk file.
28 	 */
29 	assertEqualInt(0, systemf("%s -cf archive1.tar file1 >test1.out 2>test1.err", testprog_ustar));
30 	failure("bsdtar does not pad archives written directly to regular files");
31 	assertFileSize("archive1.tar", 2048);
32 	assertEmptyFile("test1.out");
33 	assertEmptyFile("test1.err");
34 
35 	/*
36 	 * Bsdtar does pad to the block size if the output is going to a socket.
37 	 */
38 	/* Default is -b 20 */
39 	assertEqualInt(0, systemf("%s -cf - file1 2>test2.err | cat >archive2.tar ", testprog_ustar));
40 	failure("bsdtar does pad archives written to pipes");
41 	assertFileSize("archive2.tar", 10240);
42 	assertEmptyFile("test2.err");
43 
44 	assertEqualInt(0, systemf("%s -cf - -b 20 file1 2>test3.err | cat >archive3.tar ", testprog_ustar));
45 	assertFileSize("archive3.tar", 10240);
46 	assertEmptyFile("test3.err");
47 
48 	assertEqualInt(0, systemf("%s -cf - -b 10 file1 2>test4.err | cat >archive4.tar ", testprog_ustar));
49 	assertFileSize("archive4.tar", 5120);
50 	assertEmptyFile("test4.err");
51 
52 	assertEqualInt(0, systemf("%s -cf - -b 1 file1 2>test5.err | cat >archive5.tar ", testprog_ustar));
53 	assertFileSize("archive5.tar", 2048);
54 	assertEmptyFile("test5.err");
55 
56 	assertEqualInt(0, systemf("%s -cf - -b 8192 file1 2>test6.err | cat >archive6.tar ", testprog_ustar));
57 	assertFileSize("archive6.tar", 4194304);
58 	assertEmptyFile("test6.err");
59 
60 	/*
61 	 * Note: It's not possible to verify at this level that blocks
62 	 * are getting written with the
63 	 */
64 
65 	free(testprog_ustar);
66 }
67