xref: /freebsd/contrib/libarchive/libarchive/test/test_write_open_memory.c (revision 38f0b757fd84d17d0fc24739a7cda160c4516d81)
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #include "test.h"
26 __FBSDID("$FreeBSD$");
27 
28 /* Try to force archive_write_open_memory.c to write past the end of an array. */
29 static unsigned char buff[16384];
30 
31 DEFINE_TEST(test_write_open_memory)
32 {
33 	unsigned int i;
34 	struct archive *a;
35 	struct archive_entry *ae;
36 	const char *name="/tmp/test";
37 
38 	/* Create a simple archive_entry. */
39 	assert((ae = archive_entry_new()) != NULL);
40 	archive_entry_set_pathname(ae, name);
41 	archive_entry_set_mode(ae, S_IFREG);
42 	assertEqualString(archive_entry_pathname(ae), name);
43 
44 	/* Try writing with different buffer sizes. */
45 	/* Make sure that we get failure on too-small buffers, success on
46 	 * large enough ones. */
47 	for (i = 100; i < 1600; i++) {
48 		size_t used;
49 		size_t blocksize = 94;
50 		assert((a = archive_write_new()) != NULL);
51 		assertEqualIntA(a, ARCHIVE_OK,
52 		    archive_write_set_format_ustar(a));
53 		assertEqualIntA(a, ARCHIVE_OK,
54 		    archive_write_set_bytes_in_last_block(a, 1));
55 		assertEqualIntA(a, ARCHIVE_OK,
56 		    archive_write_set_bytes_per_block(a, (int)blocksize));
57 		buff[i] = 0xAE;
58 		assertEqualIntA(a, ARCHIVE_OK,
59 		    archive_write_open_memory(a, buff, i, &used));
60 		/* If buffer is smaller than a tar header, this should fail. */
61 		if (i < (511/blocksize)*blocksize)
62 			assertEqualIntA(a, ARCHIVE_FATAL,
63 			    archive_write_header(a,ae));
64 		else
65 			assertEqualIntA(a, ARCHIVE_OK,
66 			    archive_write_header(a, ae));
67 		/* If buffer is smaller than a tar header plus 1024 byte
68 		 * end-of-archive marker, then this should fail. */
69 		failure("buffer size=%d\n", (int)i);
70 		if (i < 1536)
71 			assertEqualIntA(a, ARCHIVE_FATAL,
72 			    archive_write_close(a));
73 		else {
74 			assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
75 			assertEqualInt(used, archive_filter_bytes(a, -1));
76 			assertEqualInt(archive_filter_bytes(a, -1),
77 			    archive_filter_bytes(a, 0));
78 		}
79 		assertEqualInt(ARCHIVE_OK, archive_write_free(a));
80 		assertEqualInt(buff[i], 0xAE);
81 		assert(used <= i);
82 	}
83 	archive_entry_free(ae);
84 }
85