Debian Codegraph @

src/microhttpd/test_str_token_remove.c

raw
1 /*
2 This file is part of libmicrohttpd
3 Copyright (C) 2017-2021 Karlson2k (Evgeny Grin)
4
5 This test tool is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2, or
8 (at your option) any later version.
9
10 This test tool 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 GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /**
21 * @file microhttpd/test_str_token.c
22 * @brief Unit tests for MHD_str_remove_token_caseless_() function
23 * @author Karlson2k (Evgeny Grin)
24 */
25
26 #include "mhd_options.h"
27 #include <string.h>
28 #include <stdio.h>
29 #include "mhd_str.h"
30 #include "mhd_assert.h"
31
32
33 static int
34 expect_result_n (const char *str, size_t str_len,
35 const char *token, size_t token_len,
36 const char *expected, size_t expected_len,
37 const bool expected_removed)
38 {
39 char buf_in[1024];
40 char buf_token[256];
41 char buf_out[1024];
42 size_t buf_len;
43
44 mhd_assert (sizeof(buf_in) > str_len + 2);
45 mhd_assert (sizeof(buf_token) > token_len + 2);
46 mhd_assert (sizeof(buf_out) > expected_len + 2);
47
48 memset (buf_in, '#', sizeof(buf_in));
49 memset (buf_token, '#', sizeof(buf_token));
50 memcpy (buf_in, str, str_len); /* Copy without zero-termination */
51 memcpy (buf_token, token, token_len); /* Copy without zero-termination */
52
53 for (buf_len = 0; buf_len <= expected_len + 3; ++buf_len)
54 {
55 bool res;
56 ssize_t result_len;
57 memset (buf_out, '$', sizeof(buf_out));
58
59 result_len = (ssize_t) buf_len;
60 mhd_assert (0 <= result_len);
61
62 res = MHD_str_remove_token_caseless_ (buf_in, str_len, buf_token, token_len,
63 buf_out, &result_len);
64 if (buf_len < expected_len)
65 { /* The result should not fit into the buffer */
66 if (res || (0 <= result_len))
67 {
68 fprintf (stderr,
69 "MHD_str_remove_token_caseless_() FAILED:\n"
70 "\tMHD_str_remove_token_caseless_(\"%.*s\", %lu,"
71 " \"%.*s\", %lu, buf, &(%ld->%ld)) returned %s\n",
72 (int) str_len + 2, buf_in, (unsigned long) str_len,
73 (int) token_len + 2, buf_token, (unsigned long) token_len,
74 (long) buf_len, (long) result_len, res ? "true" : "false");
75 return 1;
76 }
77 }
78 else
79 { /* The result should fit into the buffer */
80 if ( (expected_removed != res) ||
81 (result_len < 0) ||
82 (expected_len != (size_t) result_len) ||
83 ((0 != result_len) && (0 != memcmp (expected, buf_out,
84 (size_t) result_len))) ||
85 ('$' != buf_out[result_len]))
86 {
87 fprintf (stderr,
88 "MHD_str_remove_token_caseless_() FAILED:\n"
89 "\tMHD_str_remove_token_caseless_(\"%.*s\", %lu,"
90 " \"%.*s\", %lu, \"%.*s\", &(%ld->%ld)) returned %s\n",
91 (int) str_len + 2, buf_in, (unsigned long) str_len,
92 (int) token_len + 2, buf_token, (unsigned long) token_len,
93 (int) expected_len + 2, buf_out,
94 (long) buf_len, (long) result_len,
95 res ? "true" : "false");
96 return 1;
97 }
98 }
99 }
100 return 0;
101 }
102
103
104 #define expect_result(s,t,e,found) \
105 expect_result_n ((s),MHD_STATICSTR_LEN_ (s), \
106 (t),MHD_STATICSTR_LEN_ (t), \
107 (e),MHD_STATICSTR_LEN_ (e), found)
108
109 static int
110 check_result (void)
111 {
112 int errcount = 0;
113 errcount += expect_result ("string", "string", "", true);
114 errcount += expect_result ("String", "string", "", true);
115 errcount += expect_result ("string", "String", "", true);
116 errcount += expect_result ("strinG", "String", "", true);
117 errcount += expect_result ("\t strinG", "String", "", true);
118 errcount += expect_result ("strinG\t ", "String", "", true);
119 errcount += expect_result (" \t tOkEn ", "toKEN", "", true);
120 errcount += expect_result ("not token\t, tOkEn ", "toKEN", "not token",
121 true);
122 errcount += expect_result ("not token,\t tOkEn, more token", "toKEN",
123 "not token, more token", true);
124 errcount += expect_result ("not token,\t tOkEn\t, more token", "toKEN",
125 "not token, more token", true);
126 errcount += expect_result (",,,,,,test,,,,", "TESt", "", true);
127 errcount += expect_result (",,,,,\t,test,,,,", "TESt", "", true);
128 errcount += expect_result (",,,,,,test, ,,,", "TESt", "", true);
129 errcount += expect_result (",,,,,, test,,,,", "TESt", "", true);
130 errcount += expect_result (",,,,,, test not,test,,", "TESt", "test not",
131 true);
132 errcount += expect_result (",,,,,, test not,,test,,", "TESt", "test not",
133 true);
134 errcount += expect_result (",,,,,, test not ,test,,", "TESt", "test not",
135 true);
136 errcount += expect_result (",,,,,, test", "TESt", "", true);
137 errcount += expect_result (",,,,,, test ", "TESt", "", true);
138 errcount += expect_result ("no test,,,,,, test ", "TESt", "no test",
139 true);
140 errcount += expect_result ("the-token,, the-token , the-token" \
141 ",the-token ,the-token", "the-token", "", true);
142 errcount += expect_result (" the-token,, the-token , the-token," \
143 "the-token ,the-token ", "the-token", "", true);
144 errcount += expect_result (" the-token ,, the-token , the-token," \
145 "the-token , the-token ", "the-token", "", true);
146 errcount += expect_result ("the-token,a, the-token , the-token,b," \
147 "the-token , c,the-token", "the-token", "a, b, c",
148 true);
149 errcount += expect_result (" the-token, a, the-token , the-token, b," \
150 "the-token ,c ,the-token ", "the-token",
151 "a, b, c", true);
152 errcount += expect_result (" the-token , a , the-token , the-token, b ," \
153 "the-token , c , the-token ", "the-token",
154 "a, b, c",true);
155 errcount += expect_result ("the-token,aa, the-token , the-token,bb," \
156 "the-token , cc,the-token", "the-token",
157 "aa, bb, cc", true);
158 errcount += expect_result (" the-token, aa, the-token , the-token, bb," \
159 "the-token ,cc ,the-token ", "the-token",
160 "aa, bb, cc", true);
161 errcount += expect_result (" the-token , aa , the-token , the-token, bb ," \
162 "the-token , cc , the-token ", "the-token",
163 "aa, bb, cc", true);
164
165 errcount += expect_result ("strin", "string", "strin", false);
166 errcount += expect_result ("Stringer", "string", "Stringer", false);
167 errcount += expect_result ("sstring", "String", "sstring", false);
168 errcount += expect_result ("string", "Strin", "string", false);
169 errcount += expect_result ("\t( strinG", "String", "( strinG", false);
170 errcount += expect_result (")strinG\t ", "String", ")strinG", false);
171 errcount += expect_result (" \t tOkEn t ", "toKEN", "tOkEn t", false);
172 errcount += expect_result ("not token\t, tOkEner ", "toKEN",
173 "not token, tOkEner", false);
174 errcount += expect_result ("not token,\t tOkEns, more token", "toKEN",
175 "not token, tOkEns, more token", false);
176 errcount += expect_result ("not token,\t tOkEns\t, more token", "toKEN",
177 "not token, tOkEns, more token", false);
178 errcount += expect_result (",,,,,,testing,,,,", "TESt", "testing", false);
179 errcount += expect_result (",,,,,\t,test,,,,", "TESting", "test", false);
180 errcount += expect_result ("tests,,,,,,quest, ,,,", "TESt", "tests, quest",
181 false);
182 errcount += expect_result (",,,,,, testы,,,,", "TESt", "testы", false);
183 errcount += expect_result (",,,,,, test not,хtest,,", "TESt",
184 "test not, хtest", false);
185 errcount += expect_result ("testing,,,,,, test not,,test2,,", "TESt",
186 "testing, test not, test2", false);
187 errcount += expect_result (",testi,,,,, test not ,test,,", "TESting",
188 "testi, test not, test", false);
189 errcount += expect_result (",,,,,,2 test", "TESt", "2 test", false);
190 errcount += expect_result (",,,,,,test test ", "test", "test test",
191 false);
192 errcount += expect_result ("no test,,,,,,test test", "test",
193 "no test, test test", false);
194 errcount += expect_result (",,,,,,,,,,,,,,,,,,,", "the-token", "", false);
195 errcount += expect_result (",a,b,c,d,e,f,g,,,,,,,,,,,,", "the-token",
196 "a, b, c, d, e, f, g", false);
197 errcount += expect_result (",,,,,,,,,,,,,,,,,,,", "", "", false);
198 errcount += expect_result (",a,b,c,d,e,f,g,,,,,,,,,,,,", "",
199 "a, b, c, d, e, f, g", false);
200 errcount += expect_result ("a,b,c,d,e,f,g", "", "a, b, c, d, e, f, g",
201 false);
202 errcount += expect_result ("a1,b1,c1,d1,e1,f1,g1", "",
203 "a1, b1, c1, d1, e1, f1, g1", false);
204
205 errcount += expect_result (",a,b,c,d,e,f,g,,,,,,,,,,,,the-token",
206 "the-token", "a, b, c, d, e, f, g", true);
207 errcount += expect_result (",a,b,c,d,e,f,g,,,,,,,,,,,,the-token,",
208 "the-token", "a, b, c, d, e, f, g", true);
209 errcount += expect_result (",a,b,c,d,e,f,g,,,,,,,,,,,,the-token,x",
210 "the-token", "a, b, c, d, e, f, g, x", true);
211 errcount += expect_result (",a,b,c,d,e,f,g,,,,,,,,,,,,the-token x",
212 "the-token", "a, b, c, d, e, f, g, the-token x",
213 false);
214 errcount += expect_result (",a,b,c,d,e,f,g,,,,,,,,,,,,the-token x,",
215 "the-token", "a, b, c, d, e, f, g, the-token x",
216 false);
217 errcount += expect_result (",a,b,c,d,e,f,g,,,,,,,,,,,,the-token x,x",
218 "the-token", "a, b, c, d, e, f, g," \
219 " the-token x, x", false);
220 errcount += expect_result ("the-token,a,b,c,d,e,f,g,,,,,,,,,,,,the-token",
221 "the-token", "a, b, c, d, e, f, g", true);
222 errcount += expect_result ("the-token ,a,b,c,d,e,f,g,,,,,,,,,,,,the-token,",
223 "the-token", "a, b, c, d, e, f, g", true);
224 errcount += expect_result ("the-token,a,b,c,d,e,f,g,,,,,,,,,,,,the-token,x",
225 "the-token", "a, b, c, d, e, f, g, x", true);
226 errcount += expect_result ("the-token x,a,b,c,d,e,f,g,,,,,,,,,,,," \
227 "the-token x", "the-token",
228 "the-token x, a, b, c, d, e, f, g, the-token x",
229 false);
230 errcount += expect_result ("the-token x,a,b,c,d,e,f,g,,,,,,,,,,,," \
231 "the-token x,", "the-token",
232 "the-token x, a, b, c, d, e, f, g, the-token x",
233 false);
234 errcount += expect_result ("the-token x,a,b,c,d,e,f,g,,,,,,,,,,,," \
235 "the-token x,x", "the-token",
236 "the-token x, a, b, c, d, e, f, g, " \
237 "the-token x, x", false);
238
239 return errcount;
240 }
241
242
243 int
244 main (int argc, char *argv[])
245 {
246 int errcount = 0;
247 (void) argc; (void) argv; /* Unused. Silent compiler warning. */
248 errcount += check_result ();
249 return errcount == 0 ? 0 : 1;
250 }