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