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 28 /* 29 * Exercise various lengths of filenames in ustar archives. 30 */ 31 32 static void 33 test_filename(const char *prefix, int dlen, int flen) 34 { 35 char buff[8192]; 36 char filename[400]; 37 char dirname[400]; 38 struct archive_entry *ae; 39 struct archive *a; 40 size_t used; 41 int separator = 0; 42 int i = 0; 43 44 if (prefix != NULL) { 45 strcpy(filename, prefix); 46 i = (int)strlen(prefix); 47 } 48 if (dlen > 0) { 49 for (; i < dlen; i++) 50 filename[i] = 'a'; 51 filename[i++] = '/'; 52 separator = 1; 53 } 54 for (; i < dlen + flen + separator; i++) 55 filename[i] = 'b'; 56 filename[i] = '\0'; 57 58 strcpy(dirname, filename); 59 60 /* Create a new archive in memory. */ 61 assert((a = archive_write_new()) != NULL); 62 assertA(0 == archive_write_set_format_ustar(a)); 63 assertA(0 == archive_write_add_filter_none(a)); 64 assertA(0 == archive_write_set_bytes_per_block(a,0)); 65 assertA(0 == archive_write_open_memory(a, buff, sizeof(buff), &used)); 66 67 /* 68 * Write a file to it. 69 */ 70 assert((ae = archive_entry_new()) != NULL); 71 archive_entry_copy_pathname(ae, filename); 72 archive_entry_set_mode(ae, S_IFREG | 0755); 73 failure("dlen=%d, flen=%d", dlen, flen); 74 if (flen > 100) { 75 assertEqualIntA(a, ARCHIVE_FAILED, archive_write_header(a, ae)); 76 } else { 77 assertEqualIntA(a, 0, archive_write_header(a, ae)); 78 } 79 archive_entry_free(ae); 80 81 /* 82 * Write a dir to it (without trailing '/'). 83 */ 84 assert((ae = archive_entry_new()) != NULL); 85 archive_entry_copy_pathname(ae, dirname); 86 archive_entry_set_mode(ae, S_IFDIR | 0755); 87 failure("dlen=%d, flen=%d", dlen, flen); 88 if (flen >= 100) { 89 assertEqualIntA(a, ARCHIVE_FAILED, archive_write_header(a, ae)); 90 } else { 91 assertEqualIntA(a, 0, archive_write_header(a, ae)); 92 } 93 archive_entry_free(ae); 94 95 /* Tar adds a '/' to directory names. */ 96 strcat(dirname, "/"); 97 98 /* 99 * Write a dir to it (with trailing '/'). 100 */ 101 assert((ae = archive_entry_new()) != NULL); 102 archive_entry_copy_pathname(ae, dirname); 103 archive_entry_set_mode(ae, S_IFDIR | 0755); 104 failure("dlen=%d, flen=%d", dlen, flen); 105 if (flen >= 100) { 106 assertEqualIntA(a, ARCHIVE_FAILED, archive_write_header(a, ae)); 107 } else { 108 assertEqualIntA(a, 0, archive_write_header(a, ae)); 109 } 110 archive_entry_free(ae); 111 112 /* Close out the archive. */ 113 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a)); 114 assertEqualInt(ARCHIVE_OK, archive_write_free(a)); 115 116 /* 117 * Now, read the data back. 118 */ 119 assert((a = archive_read_new()) != NULL); 120 assertA(0 == archive_read_support_format_all(a)); 121 assertA(0 == archive_read_support_filter_all(a)); 122 assertA(0 == archive_read_open_memory(a, buff, used)); 123 124 if (flen <= 100) { 125 /* Read the file and check the filename. */ 126 assertA(0 == archive_read_next_header(a, &ae)); 127 failure("dlen=%d, flen=%d", dlen, flen); 128 assertEqualString(filename, archive_entry_pathname(ae)); 129 assertEqualInt((S_IFREG | 0755), archive_entry_mode(ae)); 130 } 131 132 /* 133 * Read the two dirs and check the names. 134 * 135 * Both dirs should read back with the same name, since 136 * tar should add a trailing '/' to any dir that doesn't 137 * already have one. 138 */ 139 if (flen <= 99) { 140 assertA(0 == archive_read_next_header(a, &ae)); 141 assert((S_IFDIR | 0755) == archive_entry_mode(ae)); 142 failure("dlen=%d, flen=%d", dlen, flen); 143 assertEqualString(dirname, archive_entry_pathname(ae)); 144 } 145 146 if (flen <= 99) { 147 assertA(0 == archive_read_next_header(a, &ae)); 148 assert((S_IFDIR | 0755) == archive_entry_mode(ae)); 149 assertEqualString(dirname, archive_entry_pathname(ae)); 150 } 151 152 /* Verify the end of the archive. */ 153 failure("This fails if entries were written that should not have been written. dlen=%d, flen=%d", dlen, flen); 154 assertEqualInt(1, archive_read_next_header(a, &ae)); 155 assertEqualIntA(a, ARCHIVE_OK, archive_read_close(a)); 156 assertEqualInt(ARCHIVE_OK, archive_read_free(a)); 157 } 158 159 DEFINE_TEST(test_ustar_filenames) 160 { 161 int dlen, flen; 162 163 /* Try a bunch of different file/dir lengths that add up 164 * to just a little less or a little more than 100 bytes. 165 * This exercises the code that splits paths between ustar 166 * filename and prefix fields. 167 */ 168 for (dlen = 5; dlen < 70; dlen += 5) { 169 for (flen = 100 - dlen - 5; flen < 100 - dlen + 5; flen++) { 170 test_filename(NULL, dlen, flen); 171 test_filename("/", dlen, flen); 172 } 173 } 174 175 /* Probe the 100-char limit for paths with no '/'. */ 176 for (flen = 90; flen < 110; flen++) { 177 test_filename(NULL, 0, flen); 178 test_filename("/", dlen, flen); 179 } 180 181 /* XXXX TODO Probe the 100-char limit with a dir prefix. */ 182 /* XXXX TODO Probe the 255-char total limit. */ 183 } 184