]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/libarchive/libarchive/test/test_write_open_memory.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / libarchive / libarchive / test / test_write_open_memory.c
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 s;
49                 size_t blocksize = 94;
50                 assert((a = archive_write_new()) != NULL);
51                 assertA(0 == archive_write_set_format_ustar(a));
52                 assertA(0 == archive_write_set_bytes_in_last_block(a, 1));
53                 assertA(0 == archive_write_set_bytes_per_block(a, (int)blocksize));
54                 buff[i] = 0xAE;
55                 assertA(0 == archive_write_open_memory(a, buff, i, &s));
56                 /* If buffer is smaller than a tar header, this should fail. */
57                 if (i < (511/blocksize)*blocksize)
58                         assertA(ARCHIVE_FATAL == archive_write_header(a,ae));
59                 else
60                         assertA(0 == archive_write_header(a, ae));
61                 /* If buffer is smaller than a tar header plus 1024 byte
62                  * end-of-archive marker, then this should fail. */
63                 if (i < 1536)
64                         assertA(ARCHIVE_FATAL == archive_write_close(a));
65                 else
66                         assertA(0 == archive_write_close(a));
67 #if ARCHIVE_VERSION_NUMBER < 2000000
68                 archive_write_finish(a);
69 #else
70                 assert(0 == archive_write_finish(a));
71 #endif
72                 assert(buff[i] == 0xAE);
73                 assert(s <= i);
74         }
75         archive_entry_free(ae);
76 }