1 /* 2 * Copyright (c) 2017 Dell EMC Isilon 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 AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 #include <sys/param.h> 29 #include <errno.h> 30 #include <fcntl.h> 31 #include <glob.h> 32 #include <stdio.h> 33 #include <stdlib.h> 34 #include <string.h> 35 #include <time.h> 36 #include <unistd.h> 37 38 #include <atf-c.h> 39 40 /* 41 * Derived from Russ Cox' pathological case test program used for the 42 * https://research.swtch.com/glob article. 43 */ 44 ATF_TC_WITHOUT_HEAD(glob_pathological_test); 45 ATF_TC_BODY(glob_pathological_test, tc) 46 { 47 struct timespec t, t2; 48 glob_t g; 49 const char *longname = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 50 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; 51 char pattern[1000], *p; 52 double dt; 53 unsigned i, j, k, mul; 54 int fd, rc; 55 56 fd = open(longname, O_CREAT | O_RDWR, 0666); 57 ATF_REQUIRE(fd >= 0); 58 59 /* 60 * Test up to 100 a* groups. Exponential implementations typically go 61 * bang at i=7 or 8. 62 */ 63 for (i = 0; i < 100; i++) { 64 /* 65 * Create a*...b pattern with i 'a*' groups. 66 */ 67 p = pattern; 68 for (k = 0; k < i; k++) { 69 *p++ = 'a'; 70 *p++ = '*'; 71 } 72 *p++ = 'b'; 73 *p = '\0'; 74 75 clock_gettime(CLOCK_REALTIME, &t); 76 for (j = 0; j < mul; j++) { 77 memset(&g, 0, sizeof g); 78 rc = glob(pattern, 0, 0, &g); 79 if (rc == GLOB_NOSPACE || rc == GLOB_ABORTED) { 80 ATF_REQUIRE_MSG(rc == GLOB_NOMATCH, 81 "an unexpected error occurred: " 82 "rc=%d errno=%d", rc, errno); 83 /* NORETURN */ 84 } 85 86 ATF_CHECK_MSG(rc == GLOB_NOMATCH, 87 "A bogus match occurred: '%s' ~ '%s'", pattern, 88 g.gl_pathv[0]); 89 globfree(&g); 90 } 91 clock_gettime(CLOCK_REALTIME, &t2); 92 93 t2.tv_sec -= t.tv_sec; 94 t2.tv_nsec -= t.tv_nsec; 95 dt = t2.tv_sec + (double)t2.tv_nsec/1e9; 96 dt /= mul; 97 98 ATF_CHECK_MSG(dt < 1, "glob(3) took far too long: %d %.9f", i, 99 dt); 100 101 if (dt >= 0.0001) 102 mul = 1; 103 } 104 } 105 106 ATF_TP_ADD_TCS(tp) 107 { 108 109 ATF_TP_ADD_TC(tp, glob_pathological_test); 110 111 return (atf_no_error()); 112 } 113