xref: /freebsd/contrib/libarchive/tar/test/test_option_stdout_size_exceeds_declared.c (revision 185becb1e1bd2657c156f78aeb52edac05ba5fb5)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2026 Tim Kientzle
5  * All rights reserved.
6  */
7 #include "test.h"
8 
9 /*
10  * `bsdtar -x` writing to real files is protected by archive_write_disk,
11  * which already truncates output at an entry's declared size.  `bsdtar
12  * -xO` (stdout) bypasses archive_write_disk entirely and writes
13  * archive_read_data_into_fd()'s output straight to fd 1 -- this test
14  * exercises that path's own backstop (added directly to
15  * archive_read_data_into_fd(), so it applies no matter which archive
16  * format is being read) at the bsdtar CLI level.
17  *
18  * The reference archives are crafted ZIP files with two entries: "first"
19  * declares uncompressed_size=10 but its true content is longer
20  * ("AAAAAAAAAABBBBBBBBBB" for _stored, 256KiB+6 bytes of
21  * "CCCCCCCCCCDDDDDDDDDD..." for _deflate), and "second" is a normal
22  * 11-byte "hello world" file, extracted right after "first" fails to
23  * confirm the mis-declared entry doesn't disrupt the rest of the
24  * archive.
25  *
26  * The ZIP reader itself also rejects "first" outright (a separate,
27  * format-level fix -- see test_read_format_zip_size_exceeds_declared),
28  * so archive_read_data_block() never hands this backstop any bytes for
29  * "first" to (partially) pass through: stdout ends up with none of
30  * "first"'s data at all, followed directly by "second".  That's still
31  * "never more than declared" -- just fewer bytes than the boundary
32  * allows, since the format reader was already more conservative here.
33  */
34 static void
verify(const char * refname)35 verify(const char *refname)
36 {
37 	extract_reference_file(refname);
38 	failure("bsdtar -xO must report an error for the mis-declared entry");
39 	assert(systemf("%s -xOf %s >test.out 2>test.err", testprog, refname)
40 	    != 0);
41 
42 	assertFileContents("hello world", 11, "test.out");
43 	assertNonEmptyFile("test.err");
44 }
45 
DEFINE_TEST(test_option_stdout_size_exceeds_declared)46 DEFINE_TEST(test_option_stdout_size_exceeds_declared)
47 {
48 	verify("test_option_stdout_size_exceeds_declared_stored.zip");
49 	verify("test_option_stdout_size_exceeds_declared_deflate.zip");
50 }
51