1228753Smm/*- 2228753Smm * Copyright (c) 2003-2007 Tim Kientzle 3228753Smm * All rights reserved. 4228753Smm * 5228753Smm * Redistribution and use in source and binary forms, with or without 6228753Smm * modification, are permitted provided that the following conditions 7228753Smm * are met: 8228753Smm * 1. Redistributions of source code must retain the above copyright 9228753Smm * notice, this list of conditions and the following disclaimer. 10228753Smm * 2. Redistributions in binary form must reproduce the above copyright 11228753Smm * notice, this list of conditions and the following disclaimer in the 12228753Smm * documentation and/or other materials provided with the distribution. 13228753Smm * 14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24228753Smm */ 25228753Smm#include "test.h" 26228763Smm__FBSDID("$FreeBSD$"); 27228753Smm 28228753Smmchar buff[1000000]; 29228753Smmchar buff2[100000]; 30228753Smm 31228753SmmDEFINE_TEST(test_read_truncated) 32228753Smm{ 33228753Smm struct archive_entry *ae; 34228753Smm struct archive *a; 35228753Smm unsigned int i; 36228753Smm size_t used; 37228753Smm 38228753Smm /* Create a new archive in memory. */ 39228753Smm assert((a = archive_write_new()) != NULL); 40232153Smm assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a)); 41248616Smm assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a)); 42232153Smm assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff), &used)); 43228753Smm 44228753Smm /* 45228753Smm * Write a file to it. 46228753Smm */ 47228753Smm assert((ae = archive_entry_new()) != NULL); 48228753Smm archive_entry_copy_pathname(ae, "file"); 49228753Smm archive_entry_set_mode(ae, S_IFREG | 0755); 50228753Smm for (i = 0; i < sizeof(buff2); i++) 51228753Smm buff2[i] = (unsigned char)rand(); 52228753Smm archive_entry_set_size(ae, sizeof(buff2)); 53232153Smm assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae)); 54228753Smm archive_entry_free(ae); 55232153Smm assertEqualIntA(a, sizeof(buff2), archive_write_data(a, buff2, sizeof(buff2))); 56228753Smm 57228753Smm /* Close out the archive. */ 58232153Smm assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); 59232153Smm assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 60228753Smm 61228753Smm /* Now, read back a truncated version of the archive and 62228753Smm * verify that we get an appropriate error. */ 63228753Smm for (i = 1; i < used + 100; i += 100) { 64228753Smm assert((a = archive_read_new()) != NULL); 65232153Smm assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 66232153Smm assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 67228753Smm if (i < 512) { 68232153Smm assertEqualIntA(a, ARCHIVE_FATAL, archive_read_open_memory(a, buff, i)); 69228753Smm goto wrap_up; 70228753Smm } else { 71232153Smm assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, i)); 72228753Smm } 73232153Smm assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 74228753Smm 75228753Smm if (i < 512 + sizeof(buff2)) { 76232153Smm assertEqualIntA(a, ARCHIVE_FATAL, archive_read_data(a, buff2, sizeof(buff2))); 77228753Smm goto wrap_up; 78228753Smm } else { 79232153Smm assertEqualIntA(a, sizeof(buff2), archive_read_data(a, buff2, sizeof(buff2))); 80228753Smm } 81228753Smm 82228753Smm /* Verify the end of the archive. */ 83228753Smm /* Archive must be long enough to capture a 512-byte 84228753Smm * block of zeroes after the entry. (POSIX requires a 85228753Smm * second block of zeros to be written but libarchive 86228753Smm * does not return an error if it can't consume 87228753Smm * it.) */ 88228753Smm if (i < 512 + 512*((sizeof(buff2) + 511)/512) + 512) { 89232153Smm assertEqualIntA(a, ARCHIVE_FATAL, archive_read_next_header(a, &ae)); 90228753Smm } else { 91232153Smm assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); 92228753Smm } 93228753Smm wrap_up: 94232153Smm assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 95232153Smm assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 96228753Smm } 97228753Smm 98228753Smm 99228753Smm 100228753Smm /* Same as above, except skip the body instead of reading it. */ 101228753Smm for (i = 1; i < used + 100; i += 100) { 102228753Smm assert((a = archive_read_new()) != NULL); 103232153Smm assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 104232153Smm assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 105228753Smm if (i < 512) { 106232153Smm assertEqualIntA(a, ARCHIVE_FATAL, archive_read_open_memory(a, buff, i)); 107228753Smm goto wrap_up2; 108228753Smm } else { 109232153Smm assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, i)); 110228753Smm } 111232153Smm assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 112228753Smm 113228753Smm if (i < 512 + 512*((sizeof(buff2)+511)/512)) { 114232153Smm assertEqualIntA(a, ARCHIVE_FATAL, archive_read_data_skip(a)); 115228753Smm goto wrap_up2; 116228753Smm } else { 117232153Smm assertEqualIntA(a, ARCHIVE_OK, archive_read_data_skip(a)); 118228753Smm } 119228753Smm 120228753Smm /* Verify the end of the archive. */ 121228753Smm /* Archive must be long enough to capture a 512-byte 122228753Smm * block of zeroes after the entry. (POSIX requires a 123228753Smm * second block of zeros to be written but libarchive 124228753Smm * does not return an error if it can't consume 125228753Smm * it.) */ 126228753Smm if (i < 512 + 512*((sizeof(buff2) + 511)/512) + 512) { 127232153Smm assertEqualIntA(a, ARCHIVE_FATAL, archive_read_next_header(a, &ae)); 128228753Smm } else { 129232153Smm assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); 130228753Smm } 131228753Smm wrap_up2: 132232153Smm assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 133232153Smm assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 134228753Smm } 135228753Smm} 136