]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/tests/gen/glob2_test.c
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / lib / libc / tests / gen / glob2_test.c
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 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <glob.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_TP_ADD_TCS(tp)
109 {
110
111         ATF_TP_ADD_TC(tp, glob_pathological_test);
112
113         return (atf_no_error());
114 }