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