tests: add signature checks
[gnulib.git] / tests / test-strstr.c
1 /*
2  * Copyright (C) 2004, 2007, 2008, 2009 Free Software Foundation
3  * Written by Bruno Haible and Eric Blake
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #include <config.h>
19
20 #include <string.h>
21
22 #include "signature.h"
23 SIGNATURE_CHECK (strstr, char *, (char const *, char const *));
24
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29
30 #include "zerosize-ptr.h"
31
32 #define ASSERT(expr) \
33   do                                                                         \
34     {                                                                        \
35       if (!(expr))                                                           \
36         {                                                                    \
37           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
38           fflush (stderr);                                                   \
39           abort ();                                                          \
40         }                                                                    \
41     }                                                                        \
42   while (0)
43
44 int
45 main (int argc, char *argv[])
46 {
47 #if HAVE_DECL_ALARM
48   /* Declare failure if test takes too long, by using default abort
49      caused by SIGALRM.  All known platforms that lack alarm also have
50      a quadratic strstr, and the replacement strstr is known to not
51      take too long.  */
52   signal (SIGALRM, SIG_DFL);
53   alarm (50);
54 #endif
55
56   {
57     const char input[] = "foo";
58     const char *result = strstr (input, "");
59     ASSERT (result == input);
60   }
61
62   {
63     const char input[] = "foo";
64     const char *result = strstr (input, "o");
65     ASSERT (result == input + 1);
66   }
67
68   {
69     /* On some platforms, the memchr() functions reads past the first
70        occurrence of the byte to be searched, leading to an out-of-bounds
71        read access for strstr().
72        See <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=521737>.
73        This is a bug in memchr(), see the Austin Group's clarification
74        <http://www.opengroup.org/austin/docs/austin_454.txt>.  */
75     const char *fix = "aBaaaaaaaaaaax";
76     char *page_boundary = (char *) zerosize_ptr ();
77     size_t len = strlen (fix) + 1;
78     char *input = page_boundary ? page_boundary - len : malloc (len);
79     const char *result;
80
81     strcpy (input, fix);
82     result = strstr (input, "B1x");
83     ASSERT (result == NULL);
84     if (!page_boundary)
85       free (input);
86   }
87
88   {
89     const char input[] = "ABC ABCDAB ABCDABCDABDE";
90     const char *result = strstr (input, "ABCDABD");
91     ASSERT (result == input + 15);
92   }
93
94   {
95     const char input[] = "ABC ABCDAB ABCDABCDABDE";
96     const char *result = strstr (input, "ABCDABE");
97     ASSERT (result == NULL);
98   }
99
100   {
101     const char input[] = "ABC ABCDAB ABCDABCDABDE";
102     const char *result = strstr (input, "ABCDABCD");
103     ASSERT (result == input + 11);
104   }
105
106   /* Check that a very long haystack is handled quickly if the needle is
107      short and occurs near the beginning.  */
108   {
109     size_t repeat = 10000;
110     size_t m = 1000000;
111     char *needle =
112       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
113       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
114     char *haystack = (char *) malloc (m + 1);
115     if (haystack != NULL)
116       {
117         memset (haystack, 'A', m);
118         haystack[0] = 'B';
119         haystack[m] = '\0';
120
121         for (; repeat > 0; repeat--)
122           {
123             ASSERT (strstr (haystack, needle) == haystack + 1);
124           }
125
126         free (haystack);
127       }
128   }
129
130   /* Check that a very long needle is discarded quickly if the haystack is
131      short.  */
132   {
133     size_t repeat = 10000;
134     size_t m = 1000000;
135     char *haystack =
136       "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
137       "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB";
138     char *needle = (char *) malloc (m + 1);
139     if (needle != NULL)
140       {
141         memset (needle, 'A', m);
142         needle[m] = '\0';
143
144         for (; repeat > 0; repeat--)
145           {
146             ASSERT (strstr (haystack, needle) == NULL);
147           }
148
149         free (needle);
150       }
151   }
152
153   /* Check that the asymptotic worst-case complexity is not quadratic.  */
154   {
155     size_t m = 1000000;
156     char *haystack = (char *) malloc (2 * m + 2);
157     char *needle = (char *) malloc (m + 2);
158     if (haystack != NULL && needle != NULL)
159       {
160         const char *result;
161
162         memset (haystack, 'A', 2 * m);
163         haystack[2 * m] = 'B';
164         haystack[2 * m + 1] = '\0';
165
166         memset (needle, 'A', m);
167         needle[m] = 'B';
168         needle[m + 1] = '\0';
169
170         result = strstr (haystack, needle);
171         ASSERT (result == haystack + m);
172       }
173     free (needle);
174     free (haystack);
175   }
176
177   /* Sublinear speed is only possible in memmem; strstr must examine
178      every character of haystack to find its length.  */
179
180   return 0;
181 }