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/param.h> 28 #include <sys/stat.h> 29 30 #include <errno.h> 31 #include <fcntl.h> 32 #include <glob.h> 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <time.h> 37 #include <unistd.h> 38 39 #include <atf-c.h> 40 41 static int glob_callback_invoked; 42 43 /* 44 * Derived from Russ Cox' pathological case test program used for the 45 * https://research.swtch.com/glob article. 46 */ 47 ATF_TC_WITHOUT_HEAD(glob_pathological_test); 48 ATF_TC_BODY(glob_pathological_test, tc) 49 { 50 struct timespec t, t2; 51 glob_t g; 52 const char *longname = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 53 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; 54 char pattern[1000], *p; 55 double dt; 56 unsigned i, j, k, mul; 57 int fd, rc; 58 59 fd = open(longname, O_CREAT | O_RDWR, 0666); 60 ATF_REQUIRE(fd >= 0); 61 62 /* 63 * Test up to 100 a* groups. Exponential implementations typically go 64 * bang at i=7 or 8. 65 */ 66 for (i = 0; i < 100; i++) { 67 /* 68 * Create a*...b pattern with i 'a*' groups. 69 */ 70 p = pattern; 71 for (k = 0; k < i; k++) { 72 *p++ = 'a'; 73 *p++ = '*'; 74 } 75 *p++ = 'b'; 76 *p = '\0'; 77 78 clock_gettime(CLOCK_REALTIME, &t); 79 for (j = 0; j < mul; j++) { 80 memset(&g, 0, sizeof g); 81 rc = glob(pattern, 0, 0, &g); 82 if (rc == GLOB_NOSPACE || rc == GLOB_ABORTED) { 83 ATF_REQUIRE_MSG(rc == GLOB_NOMATCH, 84 "an unexpected error occurred: " 85 "rc=%d errno=%d", rc, errno); 86 /* NORETURN */ 87 } 88 89 ATF_CHECK_MSG(rc == GLOB_NOMATCH, 90 "A bogus match occurred: '%s' ~ '%s'", pattern, 91 g.gl_pathv[0]); 92 globfree(&g); 93 } 94 clock_gettime(CLOCK_REALTIME, &t2); 95 96 t2.tv_sec -= t.tv_sec; 97 t2.tv_nsec -= t.tv_nsec; 98 dt = t2.tv_sec + (double)t2.tv_nsec/1e9; 99 dt /= mul; 100 101 ATF_CHECK_MSG(dt < 1, "glob(3) took far too long: %d %.9f", i, 102 dt); 103 104 if (dt >= 0.0001) 105 mul = 1; 106 } 107 } 108 109 ATF_TC(glob_period); 110 ATF_TC_HEAD(glob_period, tc) 111 { 112 atf_tc_set_md_var(tc, "descr", 113 "Test behaviour when matching files that start with a period" 114 "(documented in the glob(3) CAVEATS section)."); 115 } 116 ATF_TC_BODY(glob_period, tc) 117 { 118 int i; 119 glob_t g; 120 121 atf_utils_create_file(".test", ""); 122 glob(".", 0, NULL, &g); 123 ATF_REQUIRE_MSG(g.gl_matchc == 1, 124 "glob(3) shouldn't match files starting with a period when using '.'"); 125 for (i = 0; i < g.gl_matchc; i++) 126 printf("%s\n", g.gl_pathv[i]); 127 glob(".*", 0, NULL, &g); 128 ATF_REQUIRE_MSG(g.gl_matchc == 3 && strcmp(g.gl_pathv[2], ".test") == 0, 129 "glob(3) should match files starting with a period when using '.*'"); 130 } 131 132 static int 133 errfunc(const char *path, int err) 134 { 135 ATF_REQUIRE_STREQ(path, "test/"); 136 ATF_REQUIRE(err == EACCES); 137 glob_callback_invoked = 1; 138 /* Suppress EACCES errors. */ 139 return (0); 140 } 141 142 ATF_TC_WITHOUT_HEAD(glob_callback_test); 143 ATF_TC_BODY(glob_callback_test, tc) 144 { 145 int rv; 146 glob_t g; 147 148 glob_callback_invoked = 0; 149 ATF_REQUIRE_EQ(0, mkdir("test", 0007)); 150 rv = glob("test/*", 0, errfunc, &g); 151 ATF_REQUIRE_MSG(glob_callback_invoked == 1, 152 "glob(3) failed to invoke callback function"); 153 ATF_REQUIRE_MSG(rv == GLOB_NOMATCH, 154 "error callback function failed to suppress EACCES"); 155 156 /* GLOB_ERR should ignore the suppressed error. */ 157 rv = glob("test/*", GLOB_ERR, errfunc, &g); 158 ATF_REQUIRE_MSG(rv == GLOB_ABORTED, 159 "GLOB_ERR didn't override error callback function"); 160 } 161 162 ATF_TP_ADD_TCS(tp) 163 { 164 165 ATF_TP_ADD_TC(tp, glob_pathological_test); 166 ATF_TP_ADD_TC(tp, glob_period); 167 ATF_TP_ADD_TC(tp, glob_callback_test); 168 return (atf_no_error()); 169 } 170