Debian Codegraph @

ext/safestringlib/unittests/test_strcasestr_s.c

raw
1 /*------------------------------------------------------------------
2 * test_strcasestr_s
3 *
4 *
5 *------------------------------------------------------------------
6 */
7
8 #include "test_private.h"
9 #include "safe_str_lib.h"
10
11 #define LEN ( 128 )
12 #define SHORT_LEN ( 5 )
13
14 extern errno_t strcasestr_s (char *dest, rsize_t dmax, const char *src, rsize_t slen, char **substring);
15
16 int test_strcasestr_s()
17 {
18 errno_t rc;
19 char *sub;
20 char *std_sub;
21
22 rsize_t len1;
23 rsize_t len2;
24
25 char str1[LEN];
26 char str2[LEN];
27
28
29 /*--------------------------------------------------*/
30
31 rc = strcasestr_s(NULL, LEN, str2, LEN, &sub);
32 if (rc != ESNULLP) {
33 printf("%s %u Error rc=%d \n",
34 __FUNCTION__, __LINE__, rc);
35 }
36
37 if (sub) {
38 printf("%s %u Error rc=%d \n",
39 __FUNCTION__, __LINE__, rc);
40 }
41
42 /*--------------------------------------------------*/
43
44 rc = strcasestr_s(str1, LEN, NULL, LEN, &sub);
45 if (rc != ESNULLP) {
46 printf("%s %u Error rc=%d \n",
47 __FUNCTION__, __LINE__, rc);
48 }
49
50 if (sub) {
51 printf("%s %u Error rc=%d \n",
52 __FUNCTION__, __LINE__, rc);
53 }
54
55 /*--------------------------------------------------*/
56
57 rc = strcasestr_s(str1, LEN, str2, LEN, NULL);
58 if (rc != ESNULLP) {
59 printf("%s %u Error rc=%d \n",
60 __FUNCTION__, __LINE__, rc);
61 }
62
63 /*--------------------------------------------------*/
64
65 rc = strcasestr_s(str1, 0, str2, LEN, &sub);
66 if (rc != ESZEROL) {
67 printf("%s %u Error rc=%d \n",
68 __FUNCTION__, __LINE__, rc);
69 }
70
71 if (sub) {
72 printf("%s %u Error rc=%d \n",
73 __FUNCTION__, __LINE__, rc);
74 }
75
76 /*--------------------------------------------------*/
77
78 rc = strcasestr_s(str1, RSIZE_MAX_STR+1, str2, LEN, &sub);
79 if (rc != ESLEMAX) {
80 printf("%s %u Error rc=%d \n",
81 __FUNCTION__, __LINE__, rc);
82 }
83
84 if (sub) {
85 printf("%s %u Error rc=%d \n",
86 __FUNCTION__, __LINE__, rc);
87 }
88
89 /*--------------------------------------------------*/
90
91 rc = strcasestr_s(str1, LEN, str2, 0, &sub);
92 if (rc != ESZEROL) {
93 printf("%s %u Error rc=%d \n",
94 __FUNCTION__, __LINE__, rc);
95 }
96
97 if (sub) {
98 printf("%s %u Error rc=%d \n",
99 __FUNCTION__, __LINE__, rc);
100 }
101
102 /*--------------------------------------------------*/
103
104 rc = strcasestr_s(str1, LEN, str2, RSIZE_MAX_STR+1, &sub);
105 if (rc != ESLEMAX) {
106 printf("%s %u Error rc=%d \n",
107 __FUNCTION__, __LINE__, rc);
108 }
109
110 if (sub) {
111 printf("%s %u Error rc=%d \n",
112 __FUNCTION__, __LINE__, rc);
113 }
114
115 /*--------------------------------------------------*/
116
117 *str1 = '\0';
118 *str2 = '\0';
119
120 rc = strcasestr_s(str1, LEN, str2, LEN, &sub);
121 if (rc != EOK) {
122 printf("%s %u Error rc=%d \n",
123 __FUNCTION__, __LINE__, rc);
124 }
125
126 if (sub != str1) {
127 printf("%s %u Error rc=%d \n",
128 __FUNCTION__, __LINE__, rc);
129 }
130
131 /* compare to legacy */
132 std_sub = strcasestr(str1, str2);
133 if ((int)sub != (int)std_sub) { // comparison to handle 32-bit library return and 64-bit library return
134 printf("%s %u Error strcasestr_s() does not have same return as strcasestr() when str1 & str2 are zero length strings. rc=%u \n",
135 __FUNCTION__, __LINE__, rc);
136 printf("str1:[%s]\n", str1);
137 printf("str2:[%s]\n", str2);
138 printf("strcasestr_s returns:[%p]\n", sub);
139 printf("strcasestr returns:[%p]\n\n", std_sub);
140 }
141
142 /*--------------------------------------------------*/
143
144 *str1 = '\0';
145 strcpy(str1, "key");
146
147 rc = strcasestr_s(str1, LEN, str2, LEN, &sub);
148 if (rc != EOK) {
149 printf("%s %u Error rc=%d \n",
150 __FUNCTION__, __LINE__, rc);
151 }
152
153 if (sub != str1) {
154 printf("%s %u Error rc=%d \n",
155 __FUNCTION__, __LINE__, rc);
156 }
157
158 /* compare to legacy */
159 std_sub = strcasestr(str1, str2);
160 if ((int)sub != (int)std_sub) { // comparison to handle 32-bit library return and 64-bit library return
161 printf("%s %u Error strcasestr_s() does not have same return value as strcasestr() when str2 is zero length string. rc=%u \n",
162 __FUNCTION__, __LINE__, rc);
163 printf("str1:[%s]\n", str1);
164 printf("str2:[%s]\n", str2);
165 printf("strcasestr_s returns:[%p]\n", sub);
166 printf("strcasestr returns:[%p]\n\n", std_sub);
167 }
168
169 /*--------------------------------------------------*/
170
171 strcpy(str1, "keep it all together");
172 *str2 = '\0';
173
174 /* str2 being empty, must return str1 */
175 rc = strcasestr_s(str1, LEN, str2, LEN, &sub);
176 if (rc != EOK) {
177 printf("%s %u Error rc=%d \n",
178 __FUNCTION__, __LINE__, rc);
179 }
180
181 if (sub != str1) {
182 printf("%s %u Error rc=%d \n",
183 __FUNCTION__, __LINE__, rc);
184 }
185
186 /* compare to legacy */
187 std_sub = strcasestr(str1, str2);
188 if ((int)sub != (int)std_sub) { // comparison to handle 32-bit library return and 64-bit library return
189 printf("%s %u Error strcasestr_s() does not have same return value as strcasestr() when str2 is zero length string. rc=%u \n",
190 __FUNCTION__, __LINE__, rc);
191 printf("str1:[%s]\n", str1);
192 printf("str2:[%s]\n", str2);
193 printf("strcasestr_s returns:[%p]\n", sub);
194 printf("strcasestr returns:[%p]\n\n", std_sub);
195 }
196
197 /*--------------------------------------------------*/
198
199 strcpy(str1, "keep it all together");
200 strcpy(str2, "kee");
201
202 /* substring at beginning */
203 rc = strcasestr_s(str1, LEN, str2, LEN, &sub);
204 if (rc != EOK) {
205 printf("%s %u Error rc=%d \n",
206 __FUNCTION__, __LINE__, rc);
207 }
208
209 if (sub != &str1[0]) {
210 printf("%s %u Error rc=%d \n",
211 __FUNCTION__, __LINE__, rc);
212 }
213
214 /*--------------------------------------------------*/
215
216 strcpy(str1, "keep it all together");
217 strcpy(str2, "eep it");
218
219 /* substring in the middle */
220 rc = strcasestr_s(str1, LEN, str2, LEN, &sub);
221 if (rc != EOK) {
222 printf("%s %u Error rc=%d \n",
223 __FUNCTION__, __LINE__, rc);
224 }
225
226 if (sub != &str1[1]) {
227 printf("%s %u Error rc=%d \n",
228 __FUNCTION__, __LINE__, rc);
229 }
230
231 /*--------------------------------------------------*/
232
233 strcpy(str1, "keep it all together");
234 strcpy(str2, "ethe");
235
236 /* substring in the middle */
237 rc = strcasestr_s(str1, LEN, str2, LEN, &sub);
238 if (rc != EOK) {
239 printf("%s %u Error rc=%d \n",
240 __FUNCTION__, __LINE__, rc);
241 }
242
243 if (sub != &str1[15]) {
244 printf("%s %u Error rc=%d \n",
245 __FUNCTION__, __LINE__, rc);
246 }
247
248 /*--------------------------------------------------*/
249
250 strcpy(str1, "keep it all together");
251 strcpy(str2, "er");
252
253 len1 = strlen(str1);
254 len2 = strlen(str2);
255
256 /* substring at the end */
257 rc = strcasestr_s(str1, len1, str2, len2, &sub);
258 if (rc != EOK) {
259 printf("%s %u Error rc=%d \n",
260 __FUNCTION__, __LINE__, rc);
261 }
262
263 if (sub != &str1[18]) {
264 printf("%s %u Error rc=%d \n",
265 __FUNCTION__, __LINE__, rc);
266 }
267
268 /* compare to legacy */
269 std_sub = strcasestr(str1, str2);
270 if ((int)sub != (int)std_sub) { // comparison to handle 32-bit library return and 64-bit library return
271 printf("%s %u Error strcasestr_s() does not have same return value as strcasestr() when str2 is substring of the end of str1. rc=%u \n",
272 __FUNCTION__, __LINE__, rc);
273 printf("str1:[%s]\n", str1);
274 printf("str2:[%s]\n", str2);
275 printf("strcasestr_s returns:[%p]\n", sub);
276 printf("strcasestr returns:[%p]\n\n", std_sub);
277 }
278
279 /*--------------------------------------------------*/
280
281 strcpy(str1, "keep it all together");
282 strcpy(str2, "er");
283
284 len1 = strlen(str1);
285 len2 = strlen(str2);
286
287 /* substring at the end */
288 rc = strcasestr_s(str1, len1, str2, 2, &sub);
289 if (rc != EOK) {
290 printf("%s %u Error rc=%d \n",
291 __FUNCTION__, __LINE__, rc);
292 }
293
294 if (sub != &str1[18]) {
295 printf("%s %u Error rc=%d \n",
296 __FUNCTION__, __LINE__, rc);
297 }
298
299 /* compare to legacy */
300 std_sub = strcasestr(str1, str2);
301 if ((int)sub != (int)std_sub) { // comparison to handle 32-bit library return and 64-bit library return
302 printf("%s %u Error strcasestr_s() does not have same return value as strcasestr() when str2 is substring of middle of str1. rc=%u \n",
303 __FUNCTION__, __LINE__, rc);
304 printf("str1:[%s]\n", str1);
305 printf("str2:[%s]\n", str2);
306 printf("strcasestr_s returns:[%p]\n", sub);
307 printf("strcasestr returns:[%p]\n\n", std_sub);
308 }
309
310 /*--------------------------------------------------*/
311
312 strcpy(str1, "keep it all together");
313 strcpy(str2, "it all");
314
315 rc = strcasestr_s(str1, 3, str2, LEN, &sub);
316 if (rc != ESNOTFND) {
317 printf("%s %u Error rc=%d \n",
318 __FUNCTION__, __LINE__, rc);
319 }
320
321 if (sub != NULL) {
322 printf("%s %u Error rc=%d \n",
323 __FUNCTION__, __LINE__, rc);
324 }
325
326 /*--------------------------------------------------*/
327
328 strcpy(str1, "keep it all together");
329 strcpy(str2, "it all");
330
331 rc = strcasestr_s(str1, LEN, str2, 1, &sub);
332 if (rc != EOK) {
333 printf("%s %u Error rc=%d \n",
334 __FUNCTION__, __LINE__, rc);
335 }
336
337 if (sub != &str1[5]) {
338 printf("%s %u Error rc=%d \n",
339 __FUNCTION__, __LINE__, rc);
340 }
341
342 /*--------------------------------------------------*/
343
344 strcpy(str1, "keep it all together");
345 strcpy(str2, "it all");
346
347 rc = strcasestr_s(str1, LEN, str2, 2, &sub);
348 if (rc != EOK) {
349 printf("%s %u Error rc=%d \n",
350 __FUNCTION__, __LINE__, rc);
351 }
352
353 if (sub != &str1[5]) {
354 printf("%s %u Error rc=%d \n",
355 __FUNCTION__, __LINE__, rc);
356 }
357
358 /*--------------------------------------------------*/
359
360 strcpy(str1, "keep it all together");
361 strcpy(str2, "it all");
362
363 rc = strcasestr_s(str1, LEN, str2, 5, &sub);
364 if (rc != EOK) {
365 printf("%s %u Error rc=%d \n",
366 __FUNCTION__, __LINE__, rc);
367 }
368
369 if (sub != &str1[5]) {
370 printf("%s %u Error rc=%d \n",
371 __FUNCTION__, __LINE__, rc);
372 }
373
374 /*--------------------------------------------------*/
375
376 strcpy(str1, "keep it all together");
377 strcpy(str2, "1234");
378
379 len1 = strlen(str1);
380 rc = strcasestr_s(str1, len1, str2, LEN, &sub);
381 if (rc != ESNOTFND) {
382 printf("%s %u Error rc=%d \n",
383 __FUNCTION__, __LINE__, rc);
384 }
385
386 if (sub != NULL) {
387 printf("%s %u Error rc=%d \n",
388 __FUNCTION__, __LINE__, rc);
389 }
390
391 /*--------------------------------------------------*/
392
393 strcpy(str1, "keep it all together");
394 strcpy(str2, "IT ALL");
395
396 rc = strcasestr_s(str1, LEN, str2, LEN, &sub);
397 if (rc != EOK) {
398 printf("%s %u Error rc=%d \n",
399 __FUNCTION__, __LINE__, rc);
400 }
401
402 if (sub != &str1[5]) {
403 printf("%s %u Error rc=%d \n",
404 __FUNCTION__, __LINE__, rc);
405 }
406
407 /*--------------------------------------------------*/
408
409 strcpy(str1, "keep it all together");
410 strcpy(str2, "EEP");
411
412 /* validate */
413 rc = strcasestr_s(str1, LEN, str2, LEN, &sub);
414 if (rc != EOK) {
415 printf("%s %u Error rc=%u \n",
416 __FUNCTION__, __LINE__, rc);
417 }
418
419 if (sub != &str1[1]) {
420 printf("%s %u Error rc=%u \n",
421 __FUNCTION__, __LINE__, rc);
422 }
423
424 /* compare to legacy */
425 std_sub = strcasestr(str1, str2);
426 if ((int)sub != (int)std_sub) { // comparison to handle 32-bit library return and 64-bit library return
427 printf("%s %u Error rc=%u \n",
428 __FUNCTION__, __LINE__, rc);
429 }
430
431 /*--------------------------------------------------*/
432
433 return (0);
434 }