1 /*- 2 * Copyright (c) 2003-2011 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 26 #include "test.h" 27 28 static void 29 test_empty_file1(void) 30 { 31 struct archive* a = archive_read_new(); 32 33 /* Try opening an empty file with the raw handler. */ 34 assertEqualInt(ARCHIVE_OK, archive_read_support_format_raw(a)); 35 assertEqualInt(0, archive_errno(a)); 36 assertEqualString(NULL, archive_error_string(a)); 37 38 /* Raw handler doesn't support empty files. */ 39 assertEqualInt(ARCHIVE_FATAL, archive_read_open_filename(a, "emptyfile", 0)); 40 assert(NULL != archive_error_string(a)); 41 42 archive_read_free(a); 43 } 44 45 static void 46 test_empty_file2_check(struct archive* a) 47 { 48 struct archive_entry* e; 49 assertEqualInt(0, archive_errno(a)); 50 assertEqualString(NULL, archive_error_string(a)); 51 52 assertEqualInt(ARCHIVE_OK, archive_read_open_filename(a, "emptyfile", 0)); 53 assertEqualInt(0, archive_errno(a)); 54 assertEqualString(NULL, archive_error_string(a)); 55 56 assertEqualInt(ARCHIVE_EOF, archive_read_next_header(a, &e)); 57 assertEqualInt(0, archive_errno(a)); 58 assertEqualString(NULL, archive_error_string(a)); 59 60 archive_read_free(a); 61 } 62 63 static void 64 test_empty_file2(void) 65 { 66 struct archive* a = archive_read_new(); 67 68 /* Try opening an empty file with raw and empty handlers. */ 69 assertEqualInt(ARCHIVE_OK, archive_read_support_format_raw(a)); 70 assertEqualInt(ARCHIVE_OK, archive_read_support_format_empty(a)); 71 test_empty_file2_check(a); 72 73 a = archive_read_new(); 74 assertEqualInt(ARCHIVE_OK, archive_read_support_format_by_code(a, ARCHIVE_FORMAT_EMPTY)); 75 test_empty_file2_check(a); 76 77 a = archive_read_new(); 78 assertEqualInt(ARCHIVE_OK, archive_read_set_format(a, ARCHIVE_FORMAT_EMPTY)); 79 test_empty_file2_check(a); 80 } 81 82 static void 83 test_empty_tarfile(void) 84 { 85 struct archive* a = archive_read_new(); 86 struct archive_entry* e; 87 88 /* Try opening an empty file with raw and empty handlers. */ 89 assertEqualInt(ARCHIVE_OK, archive_read_support_format_tar(a)); 90 assertEqualInt(0, archive_errno(a)); 91 assertEqualString(NULL, archive_error_string(a)); 92 93 assertEqualInt(ARCHIVE_OK, archive_read_open_filename(a, "empty.tar", 0)); 94 assertEqualInt(0, archive_errno(a)); 95 assertEqualString(NULL, archive_error_string(a)); 96 97 assertEqualInt(ARCHIVE_EOF, archive_read_next_header(a, &e)); 98 assertEqualInt(0, archive_errno(a)); 99 assertEqualString(NULL, archive_error_string(a)); 100 101 archive_read_free(a); 102 } 103 104 /* 512 zero bytes. */ 105 static char nulls[512]; 106 107 DEFINE_TEST(test_archive_read_next_header_empty) 108 { 109 FILE *f; 110 111 /* Create an empty file. */ 112 f = fopen("emptyfile", "wb"); 113 fclose(f); 114 115 /* Create a file with 512 zero bytes. */ 116 f = fopen("empty.tar", "wb"); 117 assertEqualInt(512, fwrite(nulls, 1, 512, f)); 118 fclose(f); 119 120 test_empty_file1(); 121 test_empty_file2(); 122 test_empty_tarfile(); 123 124 } 125