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 28228753Smm/* 29228753Smm * Exercise various lengths of filenames in tar archives, 30228753Smm * especially around the magic sizes where ustar breaks 31228753Smm * filenames into prefix/suffix. 32228753Smm */ 33228753Smm 34228753Smmstatic void 35228753Smmtest_filename(const char *prefix, int dlen, int flen) 36228753Smm{ 37228753Smm char buff[8192]; 38228753Smm char filename[400]; 39228753Smm char dirname[400]; 40228753Smm struct archive_entry *ae; 41228753Smm struct archive *a; 42228753Smm size_t used; 43228753Smm char *p; 44228753Smm int i; 45228753Smm 46228753Smm p = filename; 47228753Smm if (prefix) { 48228753Smm strcpy(filename, prefix); 49228753Smm p += strlen(p); 50228753Smm } 51228753Smm if (dlen > 0) { 52228753Smm for (i = 0; i < dlen; i++) 53228753Smm *p++ = 'a'; 54228753Smm *p++ = '/'; 55228753Smm } 56228753Smm for (i = 0; i < flen; i++) 57228753Smm *p++ = 'b'; 58228753Smm *p = '\0'; 59228753Smm 60228753Smm strcpy(dirname, filename); 61228753Smm 62228753Smm /* Create a new archive in memory. */ 63228753Smm assert((a = archive_write_new()) != NULL); 64228753Smm assertA(0 == archive_write_set_format_pax_restricted(a)); 65248616Smm assertA(0 == archive_write_add_filter_none(a)); 66228753Smm assertA(0 == archive_write_set_bytes_per_block(a,0)); 67228753Smm assertA(0 == archive_write_open_memory(a, buff, sizeof(buff), &used)); 68228753Smm 69228753Smm /* 70228753Smm * Write a file to it. 71228753Smm */ 72228753Smm assert((ae = archive_entry_new()) != NULL); 73228753Smm archive_entry_copy_pathname(ae, filename); 74228753Smm archive_entry_set_mode(ae, S_IFREG | 0755); 75228753Smm failure("Pathname %d/%d", dlen, flen); 76228753Smm assertA(0 == archive_write_header(a, ae)); 77228753Smm archive_entry_free(ae); 78228753Smm 79228753Smm /* 80228753Smm * Write a dir to it (without trailing '/'). 81228753Smm */ 82228753Smm assert((ae = archive_entry_new()) != NULL); 83228753Smm archive_entry_copy_pathname(ae, dirname); 84228753Smm archive_entry_set_mode(ae, S_IFDIR | 0755); 85228753Smm failure("Dirname %d/%d", dlen, flen); 86228753Smm assertA(0 == archive_write_header(a, ae)); 87228753Smm archive_entry_free(ae); 88228753Smm 89228753Smm /* Tar adds a '/' to directory names. */ 90228753Smm strcat(dirname, "/"); 91228753Smm 92228753Smm /* 93228753Smm * Write a dir to it (with trailing '/'). 94228753Smm */ 95228753Smm assert((ae = archive_entry_new()) != NULL); 96228753Smm archive_entry_copy_pathname(ae, dirname); 97228753Smm archive_entry_set_mode(ae, S_IFDIR | 0755); 98228753Smm failure("Dirname %d/%d", dlen, flen); 99228753Smm assertA(0 == archive_write_header(a, ae)); 100228753Smm archive_entry_free(ae); 101228753Smm 102228753Smm /* Close out the archive. */ 103232153Smm assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); 104232153Smm assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 105228753Smm 106228753Smm /* 107228753Smm * Now, read the data back. 108228753Smm */ 109228753Smm assert((a = archive_read_new()) != NULL); 110228753Smm assertA(0 == archive_read_support_format_all(a)); 111232153Smm assertA(0 == archive_read_support_filter_all(a)); 112228753Smm assertA(0 == archive_read_open_memory(a, buff, used)); 113228753Smm 114228753Smm /* Read the file and check the filename. */ 115228753Smm assertA(0 == archive_read_next_header(a, &ae)); 116228753Smm assertEqualString(filename, archive_entry_pathname(ae)); 117228753Smm assertEqualInt((S_IFREG | 0755), archive_entry_mode(ae)); 118228753Smm 119228753Smm /* 120228753Smm * Read the two dirs and check the names. 121228753Smm * 122228753Smm * Both dirs should read back with the same name, since 123228753Smm * tar should add a trailing '/' to any dir that doesn't 124228753Smm * already have one. We only report the first such failure 125228753Smm * here. 126228753Smm */ 127228753Smm assertA(0 == archive_read_next_header(a, &ae)); 128228753Smm assertEqualString(dirname, archive_entry_pathname(ae)); 129228753Smm assert((S_IFDIR | 0755) == archive_entry_mode(ae)); 130228753Smm 131228753Smm assertA(0 == archive_read_next_header(a, &ae)); 132228753Smm assertEqualString(dirname, archive_entry_pathname(ae)); 133228753Smm assert((S_IFDIR | 0755) == archive_entry_mode(ae)); 134228753Smm 135228753Smm /* Verify the end of the archive. */ 136228753Smm assert(1 == archive_read_next_header(a, &ae)); 137232153Smm assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 138232153Smm assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 139228753Smm} 140228753Smm 141228753SmmDEFINE_TEST(test_tar_filenames) 142228753Smm{ 143228753Smm int dlen, flen; 144228753Smm 145228753Smm /* Repeat the following for a variety of dir/file lengths. */ 146228753Smm for (dlen = 45; dlen < 55; dlen++) { 147228753Smm for (flen = 45; flen < 55; flen++) { 148228753Smm test_filename(NULL, dlen, flen); 149228753Smm test_filename("/", dlen, flen); 150228753Smm } 151228753Smm } 152228753Smm 153228753Smm for (dlen = 0; dlen < 140; dlen += 10) { 154228753Smm for (flen = 98; flen < 102; flen++) { 155228753Smm test_filename(NULL, dlen, flen); 156228753Smm test_filename("/", dlen, flen); 157228753Smm } 158228753Smm } 159228753Smm 160228753Smm for (dlen = 140; dlen < 160; dlen++) { 161228753Smm for (flen = 95; flen < 105; flen++) { 162228753Smm test_filename(NULL, dlen, flen); 163228753Smm test_filename("/", dlen, flen); 164228753Smm } 165228753Smm } 166228753Smm} 167