test_read_truncated.c revision 302001
156043Syokota/*- 256043Syokota * Copyright (c) 2003-2007 Tim Kientzle 356043Syokota * All rights reserved. 456043Syokota * 556043Syokota * Redistribution and use in source and binary forms, with or without 656043Syokota * modification, are permitted provided that the following conditions 756043Syokota * are met: 856043Syokota * 1. Redistributions of source code must retain the above copyright 956043Syokota * notice, this list of conditions and the following disclaimer. 1056043Syokota * 2. Redistributions in binary form must reproduce the above copyright 1156043Syokota * notice, this list of conditions and the following disclaimer in the 1256043Syokota * documentation and/or other materials provided with the distribution. 1356043Syokota * 1456043Syokota * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 1556043Syokota * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1656043Syokota * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1756043Syokota * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 1856043Syokota * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 1956043Syokota * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2056043Syokota * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2156043Syokota * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2256043Syokota * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2356043Syokota * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2456043Syokota */ 2556043Syokota#include "test.h" 2656043Syokota__FBSDID("$FreeBSD: stable/10/contrib/libarchive/libarchive/test/test_read_truncated.c 302001 2016-06-17 22:40:10Z mm $"); 2756043Syokota 28119420Sobrienstatic char buff[1000000]; 29119420Sobrienstatic char buff2[100000]; 30119420Sobrien 3156043SyokotaDEFINE_TEST(test_read_truncated) 3256043Syokota{ 3356043Syokota struct archive_entry *ae; 3456043Syokota struct archive *a; 35164033Srwatson unsigned int i; 36181905Sed size_t used; 3756043Syokota 38199881Sed /* Create a new archive in memory. */ 3956043Syokota assert((a = archive_write_new()) != NULL); 40268366Sray assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_ustar(a)); 4166834Sphk assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a)); 4266860Sphk assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff), &used)); 4356043Syokota 4456043Syokota /* 4556043Syokota * Write a file to it. 4656043Syokota */ 4756043Syokota assert((ae = archive_entry_new()) != NULL); 4856043Syokota archive_entry_copy_pathname(ae, "file"); 4956043Syokota archive_entry_set_mode(ae, S_IFREG | 0755); 5056043Syokota for (i = 0; i < sizeof(buff2); i++) 5156043Syokota buff2[i] = (unsigned char)rand(); 5256043Syokota archive_entry_set_size(ae, sizeof(buff2)); 53181905Sed assertEqualIntA(a, ARCHIVE_OK, archive_write_header(a, ae)); 54181905Sed archive_entry_free(ae); 5556043Syokota assertEqualIntA(a, sizeof(buff2), archive_write_data(a, buff2, sizeof(buff2))); 5656043Syokota 5756043Syokota /* Close out the archive. */ 5856043Syokota assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); 5956043Syokota assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 60181905Sed 6156043Syokota /* Now, read back a truncated version of the archive and 6256043Syokota * verify that we get an appropriate error. */ 6356043Syokota for (i = 1; i < used + 100; i += 100) { 6456043Syokota assert((a = archive_read_new()) != NULL); 6556043Syokota assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 6656043Syokota assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 6756043Syokota if (i < 512) { 6856043Syokota assertEqualIntA(a, ARCHIVE_FATAL, archive_read_open_memory(a, buff, i)); 6956043Syokota goto wrap_up; 7056043Syokota } else { 7156043Syokota assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, i)); 7256043Syokota } 7356043Syokota assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 7456043Syokota 7556043Syokota if (i < 512 + sizeof(buff2)) { 7656043Syokota assertEqualIntA(a, ARCHIVE_FATAL, archive_read_data(a, buff2, sizeof(buff2))); 7756043Syokota goto wrap_up; 7856043Syokota } else { 7956043Syokota assertEqualIntA(a, sizeof(buff2), archive_read_data(a, buff2, sizeof(buff2))); 8056043Syokota } 8156043Syokota 8256043Syokota /* Verify the end of the archive. */ 8356043Syokota /* Archive must be long enough to capture a 512-byte 8456043Syokota * block of zeroes after the entry. (POSIX requires a 8556043Syokota * second block of zeros to be written but libarchive 8656043Syokota * does not return an error if it can't consume 8756043Syokota * it.) */ 8856043Syokota if (i < 512 + 512*((sizeof(buff2) + 511)/512) + 512) { 8956043Syokota assertEqualIntA(a, ARCHIVE_FATAL, archive_read_next_header(a, &ae)); 9056043Syokota } else { 9156043Syokota assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); 9256043Syokota } 9356043Syokota wrap_up: 9456043Syokota assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 9556043Syokota assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 9656043Syokota } 9756043Syokota 9856043Syokota 9956043Syokota 10056043Syokota /* Same as above, except skip the body instead of reading it. */ 10156043Syokota for (i = 1; i < used + 100; i += 100) { 10256043Syokota assert((a = archive_read_new()) != NULL); 10356043Syokota assertEqualIntA(a, ARCHIVE_OK, archive_read_support_format_all(a)); 10465129Syokota assertEqualIntA(a, ARCHIVE_OK, archive_read_support_filter_all(a)); 10565129Syokota if (i < 512) { 10665129Syokota assertEqualIntA(a, ARCHIVE_FATAL, archive_read_open_memory(a, buff, i)); 10756043Syokota goto wrap_up2; 10865129Syokota } else { 10965129Syokota assertEqualIntA(a, ARCHIVE_OK, archive_read_open_memory(a, buff, i)); 11056043Syokota } 11156043Syokota assertEqualIntA(a, ARCHIVE_OK, archive_read_next_header(a, &ae)); 11256043Syokota 11356043Syokota if (i < 512 + 512*((sizeof(buff2)+511)/512)) { 11456043Syokota assertEqualIntA(a, ARCHIVE_FATAL, archive_read_data_skip(a)); 11556043Syokota goto wrap_up2; 11656043Syokota } else { 11756043Syokota assertEqualIntA(a, ARCHIVE_OK, archive_read_data_skip(a)); 11856043Syokota } 11956043Syokota 12056043Syokota /* Verify the end of the archive. */ 12156043Syokota /* Archive must be long enough to capture a 512-byte 12256043Syokota * block of zeroes after the entry. (POSIX requires a 12356043Syokota * second block of zeros to be written but libarchive 12456043Syokota * does not return an error if it can't consume 12556043Syokota * it.) */ 12656043Syokota if (i < 512 + 512*((sizeof(buff2) + 511)/512) + 512) { 12756043Syokota assertEqualIntA(a, ARCHIVE_FATAL, archive_read_next_header(a, &ae)); 12856043Syokota } else { 12956043Syokota assertEqualIntA(a, ARCHIVE_EOF, archive_read_next_header(a, &ae)); 13056043Syokota } 131153072Sru wrap_up2: 13256043Syokota assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 13356043Syokota assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 13456043Syokota } 13556043Syokota} 13656043Syokota