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
27 /* Try to force archive_write_open_memory.c to write past the end of an array. */
28 static unsigned char buff[16384];
29
DEFINE_TEST(test_write_open_memory)30 DEFINE_TEST(test_write_open_memory)
31 {
32 unsigned int i;
33 struct archive *a;
34 struct archive_entry *ae;
35 const char *name="/tmp/test";
36
37 /* Create a simple archive_entry. */
38 assert((ae = archive_entry_new()) != NULL);
39 archive_entry_set_pathname(ae, name);
40 archive_entry_set_mode(ae, S_IFREG);
41 assertEqualString(archive_entry_pathname(ae), name);
42
43 /* Try writing with different buffer sizes. */
44 /* Make sure that we get failure on too-small buffers, success on
45 * large enough ones. */
46 for (i = 100; i < 1600; i++) {
47 size_t used;
48 size_t blocksize = 94;
49 assert((a = archive_write_new()) != NULL);
50 assertEqualIntA(a, ARCHIVE_OK,
51 archive_write_set_format_ustar(a));
52 assertEqualIntA(a, ARCHIVE_OK,
53 archive_write_set_bytes_in_last_block(a, 1));
54 assertEqualIntA(a, ARCHIVE_OK,
55 archive_write_set_bytes_per_block(a, (int)blocksize));
56 buff[i] = 0xAE;
57 assertEqualIntA(a, ARCHIVE_OK,
58 archive_write_open_memory(a, buff, i, &used));
59 /* If buffer is smaller than a tar header, this should fail. */
60 if (i < (511/blocksize)*blocksize)
61 assertEqualIntA(a, ARCHIVE_FATAL,
62 archive_write_header(a,ae));
63 else
64 assertEqualIntA(a, ARCHIVE_OK,
65 archive_write_header(a, ae));
66 /* If buffer is smaller than a tar header plus 1024 byte
67 * end-of-archive marker, then this should fail. */
68 failure("buffer size=%d\n", (int)i);
69 if (i < 1536)
70 assertEqualIntA(a, ARCHIVE_FATAL,
71 archive_write_close(a));
72 else {
73 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
74 assertEqualInt(used, archive_filter_bytes(a, -1));
75 assertEqualInt(archive_filter_bytes(a, -1),
76 archive_filter_bytes(a, 0));
77 }
78 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
79 assertEqualInt(buff[i], 0xAE);
80 assert(used <= i);
81 }
82 archive_entry_free(ae);
83 }
84