1238825Smm/*- 2238825Smm * Copyright (c) 2012 Michihiro NAKAJIMA 3238825Smm * All rights reserved. 4238825Smm * 5238825Smm * Redistribution and use in source and binary forms, with or without 6238825Smm * modification, are permitted provided that the following conditions 7238825Smm * are met: 8238825Smm * 1. Redistributions of source code must retain the above copyright 9238825Smm * notice, this list of conditions and the following disclaimer. 10238825Smm * 2. Redistributions in binary form must reproduce the above copyright 11238825Smm * notice, this list of conditions and the following disclaimer in the 12238825Smm * documentation and/or other materials provided with the distribution. 13238825Smm * 14238825Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15238825Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16238825Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17238825Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18238825Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19238825Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20238825Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21238825Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22238825Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23238825Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24238825Smm */ 25238825Smm 26238825Smm#include "test.h" 27238825Smm__FBSDID("$FreeBSD$"); 28238825Smm 29238825Smmstatic void 30238825Smmtest_exclusion_mbs(void) 31238825Smm{ 32238825Smm struct archive_entry *ae; 33238825Smm struct archive *m; 34238825Smm 35238825Smm if (!assert((m = archive_match_new()) != NULL)) 36238825Smm return; 37238825Smm if (!assert((ae = archive_entry_new()) != NULL)) { 38238825Smm archive_match_free(m); 39238825Smm return; 40238825Smm } 41238825Smm 42238825Smm /* Test for pattern "^aa*" */ 43238825Smm assertEqualIntA(m, 0, archive_match_exclude_pattern(m, "^aa*")); 44238825Smm 45238825Smm /* Test with 'aa1234', which should be excluded. */ 46238825Smm archive_entry_copy_pathname(ae, "aa1234"); 47238825Smm failure("'aa1234' should be excluded"); 48238825Smm assertEqualInt(1, archive_match_path_excluded(m, ae)); 49238825Smm assertEqualInt(1, archive_match_excluded(m, ae)); 50238825Smm archive_entry_clear(ae); 51238825Smm archive_entry_copy_pathname_w(ae, L"aa1234"); 52238825Smm failure("'aa1234' should be excluded"); 53238825Smm assertEqualInt(1, archive_match_path_excluded(m, ae)); 54238825Smm assertEqualInt(1, archive_match_excluded(m, ae)); 55238825Smm 56238825Smm /* Test with 'a1234', which should not be excluded. */ 57238825Smm archive_entry_copy_pathname(ae, "a1234"); 58238825Smm failure("'a1234' should not be excluded"); 59238825Smm assertEqualInt(0, archive_match_path_excluded(m, ae)); 60238825Smm assertEqualInt(0, archive_match_excluded(m, ae)); 61238825Smm archive_entry_clear(ae); 62238825Smm archive_entry_copy_pathname_w(ae, L"a1234"); 63238825Smm failure("'a1234' should not be excluded"); 64238825Smm assertEqualInt(0, archive_match_path_excluded(m, ae)); 65238825Smm assertEqualInt(0, archive_match_excluded(m, ae)); 66238825Smm 67238825Smm /* Clean up. */ 68238825Smm archive_entry_free(ae); 69238825Smm archive_match_free(m); 70238825Smm} 71238825Smm 72238825Smmstatic void 73238825Smmtest_exclusion_wcs(void) 74238825Smm{ 75238825Smm struct archive_entry *ae; 76238825Smm struct archive *m; 77238825Smm 78238825Smm if (!assert((m = archive_match_new()) != NULL)) 79238825Smm return; 80238825Smm if (!assert((ae = archive_entry_new()) != NULL)) { 81238825Smm archive_match_free(m); 82238825Smm return; 83238825Smm } 84238825Smm 85238825Smm /* Test for pattern "^aa*" */ 86238825Smm assertEqualIntA(m, 0, archive_match_exclude_pattern_w(m, L"^aa*")); 87238825Smm 88238825Smm /* Test with 'aa1234', which should be excluded. */ 89238825Smm archive_entry_copy_pathname(ae, "aa1234"); 90238825Smm failure("'aa1234' should be excluded"); 91238825Smm assertEqualInt(1, archive_match_path_excluded(m, ae)); 92238825Smm assertEqualInt(1, archive_match_excluded(m, ae)); 93238825Smm archive_entry_clear(ae); 94238825Smm archive_entry_copy_pathname_w(ae, L"aa1234"); 95238825Smm failure("'aa1234' should be excluded"); 96238825Smm assertEqualInt(1, archive_match_path_excluded(m, ae)); 97238825Smm assertEqualInt(1, archive_match_excluded(m, ae)); 98238825Smm 99238825Smm /* Test with 'a1234', which should not be excluded. */ 100238825Smm archive_entry_copy_pathname(ae, "a1234"); 101238825Smm failure("'a1234' should not be excluded"); 102238825Smm assertEqualInt(0, archive_match_path_excluded(m, ae)); 103238825Smm assertEqualInt(0, archive_match_excluded(m, ae)); 104238825Smm archive_entry_clear(ae); 105238825Smm archive_entry_copy_pathname_w(ae, L"a1234"); 106238825Smm failure("'a1234' should not be excluded"); 107238825Smm assertEqualInt(0, archive_match_path_excluded(m, ae)); 108238825Smm assertEqualInt(0, archive_match_excluded(m, ae)); 109238825Smm 110238825Smm /* Clean up. */ 111238825Smm archive_entry_free(ae); 112238825Smm archive_match_free(m); 113238825Smm} 114238825Smm 115238825Smmstatic void 116238825Smmexclusion_from_file(struct archive *m) 117238825Smm{ 118238825Smm struct archive_entry *ae; 119238825Smm 120238825Smm if (!assert((ae = archive_entry_new()) != NULL)) { 121238825Smm archive_match_free(m); 122238825Smm return; 123238825Smm } 124238825Smm 125238825Smm /* Test with 'first', which should not be excluded. */ 126238825Smm archive_entry_copy_pathname(ae, "first"); 127238825Smm failure("'first' should not be excluded"); 128238825Smm assertEqualInt(0, archive_match_path_excluded(m, ae)); 129238825Smm assertEqualInt(0, archive_match_excluded(m, ae)); 130238825Smm archive_entry_clear(ae); 131238825Smm archive_entry_copy_pathname_w(ae, L"first"); 132238825Smm failure("'first' should not be excluded"); 133238825Smm assertEqualInt(0, archive_match_path_excluded(m, ae)); 134238825Smm assertEqualInt(0, archive_match_excluded(m, ae)); 135238825Smm 136238825Smm /* Test with 'second', which should be excluded. */ 137238825Smm archive_entry_copy_pathname(ae, "second"); 138238825Smm failure("'second' should be excluded"); 139238825Smm assertEqualInt(1, archive_match_path_excluded(m, ae)); 140238825Smm assertEqualInt(1, archive_match_excluded(m, ae)); 141238825Smm archive_entry_clear(ae); 142238825Smm archive_entry_copy_pathname_w(ae, L"second"); 143238825Smm failure("'second' should be excluded"); 144238825Smm assertEqualInt(1, archive_match_path_excluded(m, ae)); 145238825Smm assertEqualInt(1, archive_match_excluded(m, ae)); 146238825Smm 147238825Smm /* Test with 'third', which should not be excluded. */ 148238825Smm archive_entry_copy_pathname(ae, "third"); 149238825Smm failure("'third' should not be excluded"); 150238825Smm assertEqualInt(0, archive_match_path_excluded(m, ae)); 151238825Smm assertEqualInt(0, archive_match_excluded(m, ae)); 152238825Smm archive_entry_clear(ae); 153238825Smm archive_entry_copy_pathname_w(ae, L"third"); 154238825Smm failure("'third' should not be excluded"); 155238825Smm assertEqualInt(0, archive_match_path_excluded(m, ae)); 156238825Smm assertEqualInt(0, archive_match_excluded(m, ae)); 157238825Smm 158238825Smm /* Test with 'four', which should be excluded. */ 159238825Smm archive_entry_copy_pathname(ae, "four"); 160238825Smm failure("'four' should be excluded"); 161238825Smm assertEqualInt(1, archive_match_path_excluded(m, ae)); 162238825Smm assertEqualInt(1, archive_match_excluded(m, ae)); 163238825Smm archive_entry_clear(ae); 164238825Smm archive_entry_copy_pathname_w(ae, L"four"); 165238825Smm failure("'four' should be excluded"); 166238825Smm assertEqualInt(1, archive_match_path_excluded(m, ae)); 167238825Smm assertEqualInt(1, archive_match_excluded(m, ae)); 168238825Smm 169238825Smm /* Clean up. */ 170238825Smm archive_entry_free(ae); 171238825Smm} 172238825Smm 173238825Smmstatic void 174238825Smmtest_exclusion_from_file_mbs(void) 175238825Smm{ 176238825Smm struct archive *m; 177238825Smm 178238825Smm /* Test1: read exclusion patterns from file */ 179238825Smm if (!assert((m = archive_match_new()) != NULL)) 180238825Smm return; 181238825Smm assertEqualIntA(m, 0, 182238825Smm archive_match_exclude_pattern_from_file(m, "exclusion", 0)); 183238825Smm exclusion_from_file(m); 184238825Smm /* Clean up. */ 185238825Smm archive_match_free(m); 186238825Smm 187238825Smm /* Test2: read exclusion patterns in a null separator from file */ 188238825Smm if (!assert((m = archive_match_new()) != NULL)) 189238825Smm return; 190238825Smm /* Test for pattern reading from file */ 191238825Smm assertEqualIntA(m, 0, 192238825Smm archive_match_exclude_pattern_from_file(m, "exclusion_null", 1)); 193238825Smm exclusion_from_file(m); 194238825Smm /* Clean up. */ 195238825Smm archive_match_free(m); 196238825Smm} 197238825Smm 198238825Smmstatic void 199238825Smmtest_exclusion_from_file_wcs(void) 200238825Smm{ 201238825Smm struct archive *m; 202238825Smm 203238825Smm /* Test1: read exclusion patterns from file */ 204238825Smm if (!assert((m = archive_match_new()) != NULL)) 205238825Smm return; 206238825Smm assertEqualIntA(m, 0, 207238825Smm archive_match_exclude_pattern_from_file_w(m, L"exclusion", 0)); 208238825Smm exclusion_from_file(m); 209238825Smm /* Clean up. */ 210238825Smm archive_match_free(m); 211238825Smm 212238825Smm /* Test2: read exclusion patterns in a null separator from file */ 213238825Smm if (!assert((m = archive_match_new()) != NULL)) 214238825Smm return; 215238825Smm /* Test for pattern reading from file */ 216238825Smm assertEqualIntA(m, 0, 217238825Smm archive_match_exclude_pattern_from_file_w(m, L"exclusion_null", 1)); 218238825Smm exclusion_from_file(m); 219238825Smm /* Clean up. */ 220238825Smm archive_match_free(m); 221238825Smm} 222238825Smm 223238825Smmstatic void 224238825Smmtest_inclusion_mbs(void) 225238825Smm{ 226238825Smm struct archive_entry *ae; 227238825Smm struct archive *m; 228238825Smm const char *mp; 229238825Smm 230238825Smm if (!assert((m = archive_match_new()) != NULL)) 231238825Smm return; 232238825Smm if (!assert((ae = archive_entry_new()) != NULL)) { 233238825Smm archive_match_free(m); 234238825Smm return; 235238825Smm } 236238825Smm 237238825Smm /* Test for pattern "^aa*" */ 238238825Smm assertEqualIntA(m, 0, archive_match_include_pattern(m, "^aa*")); 239238825Smm 240238825Smm /* Test with 'aa1234', which should not be excluded. */ 241238825Smm archive_entry_copy_pathname(ae, "aa1234"); 242238825Smm failure("'aa1234' should not be excluded"); 243238825Smm assertEqualInt(0, archive_match_path_excluded(m, ae)); 244238825Smm assertEqualInt(0, archive_match_excluded(m, ae)); 245238825Smm archive_entry_clear(ae); 246238825Smm archive_entry_copy_pathname_w(ae, L"aa1234"); 247238825Smm failure("'aa1234' should not be excluded"); 248238825Smm assertEqualInt(0, archive_match_path_excluded(m, ae)); 249238825Smm assertEqualInt(0, archive_match_excluded(m, ae)); 250238825Smm 251238825Smm /* Test with 'a1234', which should be excluded. */ 252238825Smm archive_entry_copy_pathname(ae, "a1234"); 253238825Smm failure("'a1234' should be excluded"); 254238825Smm assertEqualInt(1, archive_match_path_excluded(m, ae)); 255238825Smm assertEqualInt(1, archive_match_excluded(m, ae)); 256238825Smm archive_entry_clear(ae); 257238825Smm archive_entry_copy_pathname_w(ae, L"a1234"); 258238825Smm failure("'a1234' should be excluded"); 259238825Smm assertEqualInt(1, archive_match_path_excluded(m, ae)); 260238825Smm assertEqualInt(1, archive_match_excluded(m, ae)); 261238825Smm 262238825Smm /* Verify unmatched_inclusions. */ 263238825Smm assertEqualInt(0, archive_match_path_unmatched_inclusions(m)); 264238825Smm assertEqualIntA(m, ARCHIVE_EOF, 265238825Smm archive_match_path_unmatched_inclusions_next(m, &mp)); 266238825Smm 267238825Smm /* Clean up. */ 268238825Smm archive_entry_free(ae); 269238825Smm archive_match_free(m); 270238825Smm} 271238825Smm 272238825Smmstatic void 273238825Smmtest_inclusion_wcs(void) 274238825Smm{ 275238825Smm struct archive_entry *ae; 276238825Smm struct archive *m; 277238825Smm const char *mp; 278238825Smm 279238825Smm if (!assert((m = archive_match_new()) != NULL)) 280238825Smm return; 281238825Smm if (!assert((ae = archive_entry_new()) != NULL)) { 282238825Smm archive_match_free(m); 283238825Smm return; 284238825Smm } 285238825Smm 286238825Smm /* Test for pattern "^aa*" */ 287238825Smm assertEqualIntA(m, 0, archive_match_include_pattern_w(m, L"^aa*")); 288238825Smm 289238825Smm /* Test with 'aa1234', which should not be excluded. */ 290238825Smm archive_entry_copy_pathname(ae, "aa1234"); 291238825Smm failure("'aa1234' should not be excluded"); 292238825Smm assertEqualInt(0, archive_match_path_excluded(m, ae)); 293238825Smm assertEqualInt(0, archive_match_excluded(m, ae)); 294238825Smm archive_entry_clear(ae); 295238825Smm archive_entry_copy_pathname_w(ae, L"aa1234"); 296238825Smm failure("'aa1234' should not be excluded"); 297238825Smm assertEqualInt(0, archive_match_path_excluded(m, ae)); 298238825Smm assertEqualInt(0, archive_match_excluded(m, ae)); 299238825Smm 300238825Smm /* Test with 'a1234', which should be excluded. */ 301238825Smm archive_entry_copy_pathname(ae, "a1234"); 302238825Smm failure("'a1234' should be excluded"); 303238825Smm assertEqualInt(1, archive_match_path_excluded(m, ae)); 304238825Smm assertEqualInt(1, archive_match_excluded(m, ae)); 305238825Smm archive_entry_clear(ae); 306238825Smm archive_entry_copy_pathname_w(ae, L"a1234"); 307238825Smm failure("'a1234' should be excluded"); 308238825Smm assertEqualInt(1, archive_match_path_excluded(m, ae)); 309238825Smm assertEqualInt(1, archive_match_excluded(m, ae)); 310238825Smm 311238825Smm /* Verify unmatched_inclusions. */ 312238825Smm assertEqualInt(0, archive_match_path_unmatched_inclusions(m)); 313238825Smm assertEqualIntA(m, ARCHIVE_EOF, 314238825Smm archive_match_path_unmatched_inclusions_next(m, &mp)); 315238825Smm 316238825Smm /* Clean up. */ 317238825Smm archive_entry_free(ae); 318238825Smm archive_match_free(m); 319238825Smm} 320238825Smm 321238825Smmstatic void 322238825Smmtest_inclusion_from_file_mbs(void) 323238825Smm{ 324238825Smm struct archive *m; 325238825Smm 326238825Smm /* Test1: read inclusion patterns from file */ 327238825Smm if (!assert((m = archive_match_new()) != NULL)) 328238825Smm return; 329238825Smm assertEqualIntA(m, 0, 330238825Smm archive_match_include_pattern_from_file(m, "inclusion", 0)); 331238825Smm exclusion_from_file(m); 332238825Smm /* Clean up. */ 333238825Smm archive_match_free(m); 334238825Smm 335238825Smm /* Test2: read inclusion patterns in a null separator from file */ 336238825Smm if (!assert((m = archive_match_new()) != NULL)) 337238825Smm return; 338238825Smm assertEqualIntA(m, 0, 339238825Smm archive_match_include_pattern_from_file(m, "inclusion_null", 1)); 340238825Smm exclusion_from_file(m); 341238825Smm /* Clean up. */ 342238825Smm archive_match_free(m); 343238825Smm} 344238825Smm 345238825Smmstatic void 346238825Smmtest_inclusion_from_file_wcs(void) 347238825Smm{ 348238825Smm struct archive *m; 349238825Smm 350238825Smm /* Test1: read inclusion patterns from file */ 351238825Smm if (!assert((m = archive_match_new()) != NULL)) 352238825Smm return; 353238825Smm /* Test for pattern reading from file */ 354238825Smm assertEqualIntA(m, 0, 355238825Smm archive_match_include_pattern_from_file_w(m, L"inclusion", 0)); 356238825Smm exclusion_from_file(m); 357238825Smm /* Clean up. */ 358238825Smm archive_match_free(m); 359238825Smm 360238825Smm /* Test2: read inclusion patterns in a null separator from file */ 361238825Smm if (!assert((m = archive_match_new()) != NULL)) 362238825Smm return; 363238825Smm /* Test for pattern reading from file */ 364238825Smm assertEqualIntA(m, 0, 365238825Smm archive_match_include_pattern_from_file_w(m, L"inclusion_null", 1)); 366238825Smm exclusion_from_file(m); 367238825Smm /* Clean up. */ 368238825Smm archive_match_free(m); 369238825Smm} 370238825Smm 371238825Smmstatic void 372238825Smmtest_exclusion_and_inclusion(void) 373238825Smm{ 374238825Smm struct archive_entry *ae; 375238825Smm struct archive *m; 376238825Smm const char *mp; 377238825Smm const wchar_t *wp; 378238825Smm 379238825Smm if (!assert((m = archive_match_new()) != NULL)) 380238825Smm return; 381238825Smm if (!assert((ae = archive_entry_new()) != NULL)) { 382238825Smm archive_match_free(m); 383238825Smm return; 384238825Smm } 385238825Smm 386238825Smm assertEqualIntA(m, 0, archive_match_exclude_pattern(m, "^aaa*")); 387238825Smm assertEqualIntA(m, 0, archive_match_include_pattern_w(m, L"^aa*")); 388238825Smm assertEqualIntA(m, 0, archive_match_include_pattern(m, "^a1*")); 389238825Smm 390238825Smm /* Test with 'aa1234', which should not be excluded. */ 391238825Smm archive_entry_copy_pathname(ae, "aa1234"); 392238825Smm failure("'aa1234' should not be excluded"); 393238825Smm assertEqualInt(0, archive_match_path_excluded(m, ae)); 394238825Smm assertEqualInt(0, archive_match_excluded(m, ae)); 395238825Smm archive_entry_clear(ae); 396238825Smm archive_entry_copy_pathname_w(ae, L"aa1234"); 397238825Smm failure("'aa1234' should not be excluded"); 398238825Smm assertEqualInt(0, archive_match_path_excluded(m, ae)); 399238825Smm assertEqualInt(0, archive_match_excluded(m, ae)); 400238825Smm 401238825Smm /* Test with 'aaa1234', which should be excluded. */ 402238825Smm archive_entry_copy_pathname(ae, "aaa1234"); 403238825Smm failure("'aaa1234' should be excluded"); 404238825Smm assertEqualInt(1, archive_match_path_excluded(m, ae)); 405238825Smm assertEqualInt(1, archive_match_excluded(m, ae)); 406238825Smm archive_entry_clear(ae); 407238825Smm archive_entry_copy_pathname_w(ae, L"aaa1234"); 408238825Smm failure("'aaa1234' should be excluded"); 409238825Smm assertEqualInt(1, archive_match_path_excluded(m, ae)); 410238825Smm assertEqualInt(1, archive_match_excluded(m, ae)); 411238825Smm 412238825Smm /* Verify unmatched_inclusions. */ 413238825Smm assertEqualInt(1, archive_match_path_unmatched_inclusions(m)); 414238825Smm /* Verify unmatched inclusion patterns. */ 415238825Smm assertEqualIntA(m, ARCHIVE_OK, 416238825Smm archive_match_path_unmatched_inclusions_next(m, &mp)); 417238825Smm assertEqualString("^a1*", mp); 418238825Smm assertEqualIntA(m, ARCHIVE_EOF, 419238825Smm archive_match_path_unmatched_inclusions_next(m, &mp)); 420238825Smm /* Verify unmatched inclusion patterns again in Wide-Char. */ 421238825Smm assertEqualIntA(m, ARCHIVE_OK, 422238825Smm archive_match_path_unmatched_inclusions_next_w(m, &wp)); 423238825Smm assertEqualWString(L"^a1*", wp); 424238825Smm assertEqualIntA(m, ARCHIVE_EOF, 425238825Smm archive_match_path_unmatched_inclusions_next_w(m, &wp)); 426238825Smm 427238825Smm /* Clean up. */ 428238825Smm archive_entry_free(ae); 429238825Smm archive_match_free(m); 430238825Smm} 431238825Smm 432238825SmmDEFINE_TEST(test_archive_match_path) 433238825Smm{ 434238825Smm /* Make exclusion sample files which contain exclusion patterns. */ 435238825Smm assertMakeFile("exclusion", 0666, "second\nfour\n"); 436238825Smm assertMakeBinFile("exclusion_null", 0666, 12, "second\0four\0"); 437238825Smm /* Make inclusion sample files which contain inclusion patterns. */ 438238825Smm assertMakeFile("inclusion", 0666, "first\nthird\n"); 439238825Smm assertMakeBinFile("inclusion_null", 0666, 12, "first\0third\0"); 440238825Smm 441238825Smm test_exclusion_mbs(); 442238825Smm test_exclusion_wcs(); 443238825Smm test_exclusion_from_file_mbs(); 444238825Smm test_exclusion_from_file_wcs(); 445238825Smm test_inclusion_mbs(); 446238825Smm test_inclusion_wcs(); 447238825Smm test_inclusion_from_file_mbs(); 448238825Smm test_inclusion_from_file_wcs(); 449238825Smm test_exclusion_and_inclusion(); 450238825Smm} 451