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 28DEFINE_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_set_compression_gzip(a); 44 if (r == ARCHIVE_FATAL) { 45 skipping("Empty write to gzip-compressed archive"); 46 } else { 47 assertEqualIntA(a, ARCHIVE_OK, r); 48 assertEqualIntA(a, ARCHIVE_OK, 49 archive_write_open_memory(a, buff, sizeof(buff), &used)); 50 /* Write a file to it. */ 51 assert((ae = archive_entry_new()) != NULL); 52 archive_entry_copy_pathname(ae, "file"); 53 archive_entry_set_mode(ae, S_IFREG | 0755); 54 archive_entry_set_size(ae, 0); 55 assertA(0 == archive_write_header(a, ae)); 56 archive_entry_free(ae); 57 58 /* THE TEST: write zero bytes to this entry. */ 59 /* This used to crash. */ 60 assertEqualIntA(a, 0, archive_write_data(a, "", 0)); 61 62 /* Close out the archive. */ 63 assertA(0 == archive_write_close(a)); 64 assertA(0 == archive_write_finish(a)); 65 } 66 67 /* 68 * Again, with bzip2 compression. 69 */ 70 71 /* Create a new archive in memory. */ 72 assert((a = archive_write_new()) != NULL); 73 assertA(0 == archive_write_set_format_ustar(a)); 74 r = archive_write_set_compression_bzip2(a); 75 if (r == ARCHIVE_FATAL) { 76 skipping("Empty write to bzip2-compressed archive"); 77 } else { 78 assertEqualIntA(a, ARCHIVE_OK, r); 79 assertEqualIntA(a, ARCHIVE_OK, 80 archive_write_open_memory(a, buff, sizeof(buff), &used)); 81 /* Write a file to it. */ 82 assert((ae = archive_entry_new()) != NULL); 83 archive_entry_copy_pathname(ae, "file"); 84 archive_entry_set_mode(ae, S_IFREG | 0755); 85 archive_entry_set_size(ae, 0); 86 assertA(0 == archive_write_header(a, ae)); 87 archive_entry_free(ae); 88 89 /* THE TEST: write zero bytes to this entry. */ 90 assertEqualIntA(a, 0, archive_write_data(a, "", 0)); 91 92 /* Close out the archive. */ 93 assertA(0 == archive_write_close(a)); 94 assertA(0 == archive_write_finish(a)); 95 } 96 97 /* 98 * For good measure, one more time with no compression. 99 */ 100 101 /* Create a new archive in memory. */ 102 assert((a = archive_write_new()) != NULL); 103 assertA(0 == archive_write_set_format_ustar(a)); 104 assertA(0 == archive_write_set_compression_none(a)); 105 assertA(0 == archive_write_open_memory(a, buff, sizeof(buff), &used)); 106 /* Write a file to it. */ 107 assert((ae = archive_entry_new()) != NULL); 108 archive_entry_copy_pathname(ae, "file"); 109 archive_entry_set_mode(ae, S_IFREG | 0755); 110 archive_entry_set_size(ae, 0); 111 assertA(0 == archive_write_header(a, ae)); 112 archive_entry_free(ae); 113 114 /* THE TEST: write zero bytes to this entry. */ 115 assertEqualIntA(a, 0, archive_write_data(a, "", 0)); 116 117 /* Close out the archive. */ 118 assertA(0 == archive_write_close(a)); 119 assertA(0 == archive_write_finish(a)); 120} 121