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
DEFINE_TEST(test_empty_write)27 DEFINE_TEST(test_empty_write)
28 {
29 char buff[32768];
30 struct archive_entry *ae;
31 struct archive *a;
32 size_t used;
33 int r;
34
35 /*
36 * Exercise a zero-byte write to a gzip-compressed archive.
37 */
38
39 /* Create a new archive in memory. */
40 assert((a = archive_write_new()) != NULL);
41 assertA(0 == archive_write_set_format_ustar(a));
42 r = archive_write_add_filter_gzip(a);
43 if (r != ARCHIVE_OK && !canGzip()) {
44 skipping("Empty write to gzip-compressed archive");
45 } else {
46 if (r != ARCHIVE_OK && canGzip())
47 assertEqualIntA(a, ARCHIVE_WARN, r);
48 else
49 assertEqualIntA(a, ARCHIVE_OK, r);
50 assertEqualIntA(a, ARCHIVE_OK,
51 archive_write_open_memory(a, buff, sizeof(buff), &used));
52 /* Write a file to it. */
53 assert((ae = archive_entry_new()) != NULL);
54 archive_entry_copy_pathname(ae, "file");
55 archive_entry_set_mode(ae, S_IFREG | 0755);
56 archive_entry_set_size(ae, 0);
57 assertA(0 == archive_write_header(a, ae));
58 archive_entry_free(ae);
59
60 /* THE TEST: write zero bytes to this entry. */
61 /* This used to crash. */
62 assertEqualIntA(a, 0, archive_write_data(a, "", 0));
63
64 /* Close out the archive. */
65 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
66 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
67 }
68
69 /*
70 * Again, with bzip2 compression.
71 */
72
73 /* Create a new archive in memory. */
74 assert((a = archive_write_new()) != NULL);
75 assertA(0 == archive_write_set_format_ustar(a));
76 r = archive_write_add_filter_bzip2(a);
77 if (r != ARCHIVE_OK && !canBzip2()) {
78 skipping("Empty write to bzip2-compressed archive");
79 } else {
80 if (r != ARCHIVE_OK && canBzip2())
81 assertEqualIntA(a, ARCHIVE_WARN, r);
82 else
83 assertEqualIntA(a, ARCHIVE_OK, r);
84 assertEqualIntA(a, ARCHIVE_OK,
85 archive_write_open_memory(a, buff, sizeof(buff), &used));
86 /* Write a file to it. */
87 assert((ae = archive_entry_new()) != NULL);
88 archive_entry_copy_pathname(ae, "file");
89 archive_entry_set_mode(ae, S_IFREG | 0755);
90 archive_entry_set_size(ae, 0);
91 assertA(0 == archive_write_header(a, ae));
92 archive_entry_free(ae);
93
94 /* THE TEST: write zero bytes to this entry. */
95 assertEqualIntA(a, 0, archive_write_data(a, "", 0));
96
97 /* Close out the archive. */
98 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
99 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
100 }
101
102 /*
103 * For good measure, one more time with no compression.
104 */
105
106 /* Create a new archive in memory. */
107 assert((a = archive_write_new()) != NULL);
108 assertA(0 == archive_write_set_format_ustar(a));
109 assertA(0 == archive_write_add_filter_none(a));
110 assertA(0 == archive_write_open_memory(a, buff, sizeof(buff), &used));
111 /* Write a file to it. */
112 assert((ae = archive_entry_new()) != NULL);
113 archive_entry_copy_pathname(ae, "file");
114 archive_entry_set_mode(ae, S_IFREG | 0755);
115 archive_entry_set_size(ae, 0);
116 assertA(0 == archive_write_header(a, ae));
117 archive_entry_free(ae);
118
119 /* THE TEST: write zero bytes to this entry. */
120 assertEqualIntA(a, 0, archive_write_data(a, "", 0));
121
122 /* Close out the archive. */
123 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
124 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
125 }
126