xref: /freebsd/lib/libc/tests/gen/glob2_test.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
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 <errno.h>
29 #include <fcntl.h>
30 #include <glob.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <time.h>
35 #include <unistd.h>
36 
37 #include <atf-c.h>
38 
39 /*
40  * Derived from Russ Cox' pathological case test program used for the
41  * https://research.swtch.com/glob article.
42  */
43 ATF_TC_WITHOUT_HEAD(glob_pathological_test);
44 ATF_TC_BODY(glob_pathological_test, tc)
45 {
46 	struct timespec t, t2;
47 	glob_t g;
48 	const char *longname = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
49 	    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
50 	char pattern[1000], *p;
51 	double dt;
52 	unsigned i, j, k, mul;
53 	int fd, rc;
54 
55 	fd = open(longname, O_CREAT | O_RDWR, 0666);
56 	ATF_REQUIRE(fd >= 0);
57 
58 	/*
59 	 * Test up to 100 a* groups.  Exponential implementations typically go
60 	 * bang at i=7 or 8.
61 	 */
62 	for (i = 0; i < 100; i++) {
63 		/*
64 		 * Create a*...b pattern with i 'a*' groups.
65 		 */
66 		p = pattern;
67 		for (k = 0; k < i; k++) {
68 			*p++ = 'a';
69 			*p++ = '*';
70 		}
71 		*p++ = 'b';
72 		*p = '\0';
73 
74 		clock_gettime(CLOCK_REALTIME, &t);
75 		for (j = 0; j < mul; j++) {
76 			memset(&g, 0, sizeof g);
77 			rc = glob(pattern, 0, 0, &g);
78 			if (rc == GLOB_NOSPACE || rc == GLOB_ABORTED) {
79 				ATF_REQUIRE_MSG(rc == GLOB_NOMATCH,
80 				    "an unexpected error occurred: "
81 				    "rc=%d errno=%d", rc, errno);
82 				/* NORETURN */
83 			}
84 
85 			ATF_CHECK_MSG(rc == GLOB_NOMATCH,
86 			    "A bogus match occurred: '%s' ~ '%s'", pattern,
87 			    g.gl_pathv[0]);
88 			globfree(&g);
89 		}
90 		clock_gettime(CLOCK_REALTIME, &t2);
91 
92 		t2.tv_sec -= t.tv_sec;
93 		t2.tv_nsec -= t.tv_nsec;
94 		dt = t2.tv_sec + (double)t2.tv_nsec/1e9;
95 		dt /= mul;
96 
97 		ATF_CHECK_MSG(dt < 1, "glob(3) took far too long: %d %.9f", i,
98 		    dt);
99 
100 		if (dt >= 0.0001)
101 			mul = 1;
102 	}
103 }
104 
105 ATF_TP_ADD_TCS(tp)
106 {
107 
108 	ATF_TP_ADD_TC(tp, glob_pathological_test);
109 
110 	return (atf_no_error());
111 }
112