Debian Codegraph @

ext/safestringlib/unittests/test_stpncpy_s.c

raw
1 /*------------------------------------------------------------------
2 * test_stpncpy_s
3 *
4 * September 2014, D Wheeler
5 *
6 * Copyright (c) 2014 by Intel Corp
7 * All rights reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person
10 * obtaining a copy of this software and associated documentation
11 * files (the "Software"), to deal in the Software without
12 * restriction, including without limitation the rights to use,
13 * copy, modify, merge, publish, distribute, sublicense, and/or
14 * sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following
16 * conditions:
17 *
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 *------------------------------------------------------------------
30 *------------------------------------------------------------------
31 * TEST COVERAGE NOTES
32 *
33 * The following notes describe the purpose of the test cases in
34 * order to ensure full coverage of all return paths in the code to
35 * be tested, as well as test for security concerns in the target.
36 * The test methodology is to perfrom branch-coverage testing to ensure
37 * all exit paths from the code have been tested. Additional tests are added to
38 * perform some corner case validation of certain more complex branches
39 * to verify no side-effects or missing corners are introduced in the code.
40 *
41 * BASIC PARAMETER VALIDATION
42 * TC 1: Test for first parameter, destination char string, being sent in as NULL
43 * TC 2: Test for second parameter, dest maximum length (dmax), being sent in as zero
44 * TC 3: Test for second parameter, dest maximum length (dmax), being larger than the maximum allowed size
45 * TC 4: Test for third parameter, source char string, being sent in as NULL
46 *
47 * TEST WHEN SRC & DEST ARE THE SAME
48 * TC 5: Test for src=dest, but string too long for dmax
49 * TC 6: Test for src=dest, proper return
50 *
51 * TEST WHEN DEST BUFFER < SOURCE BUFFER
52 * TC 7: Test for overlap of dest buffer into src buffer (src string too long)
53 * TC12: Test for proper substring copy of string over long string (limited by smax)
54 * TC13: Test for accurate String copy over existing string, filling dest with dmax with NULLs
55 * TC14: Test for accurate subString copy over existing string (limited by smax), filling dest wtih dmax nulls
56 * TC15: Test for accurate subString copy (limited by smax) and adding null terminator, replacing existing string
57 * TC16: Test copy but not enough space in destination
58 * TC17: Test for not enough space in destination for null terminator
59 * TC18: Test for just enough space in destination
60 * TC19: Test copy short string over long string
61 *
62 *
63 * TEST NON-STANDARD FAILURE CONDITIONS FOR NULL DEST STRINGS
64 * TC 9: Test copy null string over destination string (dest < src)
65 * TC10: Test copy NULL string over existing string (dest > src)
66 * TC11: Test copy string over NULL string (dest < src)
67 *
68 * TEST WHEN DEST BUFFER >= SOURCE BUFFER
69 * TC 8: Test src buffer runs into beginning of dest buffer (src buffer is unterminated or been destroyed by copy)
70 *
71 *
72 *
73 *------------------------------------------------------------------
74 */
75
76
77 #include "test_private.h"
78 #include "safe_str_lib.h"
79
80 #define MAX ( 128 )
81 #define LEN ( 128 )
82
83 static char str1[LEN];
84 static char str2[LEN];
85
86 extern char *stpncpy_s(char *dest, rsize_t dmax, const char *src, rsize_t smax, errno_t *err);
87
88
89 int test_stpncpy_s (void)
90 {
91 char *ret;
92 errno_t rc;
93 uint32_t i;
94 int32_t ind;
95 rsize_t sz;
96 unsigned int testno = 0;
97
98
99 printf("\nTesting stpncpy_s:\n");
100 /*--------------------------------------------------*/
101 /* 1 Test for NULL destination check */
102 printf("Test #%d:\n", ++testno);
103
104 ret = stpncpy_s(NULL, LEN, str2, LEN, &rc);
105 if (rc != ESNULLP) {
106 printf("%s %u Error rc=%u \n",
107 __FUNCTION__, __LINE__, rc );
108 }
109
110 if (ret != NULL) {
111 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
112 __FUNCTION__, __LINE__, rc );
113 }
114
115 /*--------------------------------------------------*/
116 /* 2 Test for zero length destination */
117 printf("Test #%d:\n", ++testno);
118
119 ret = stpncpy_s(str1, 0, str2, LEN, &rc);
120 if (rc != ESZEROL) {
121 printf("%s %u Error rc=%u \n",
122 __FUNCTION__, __LINE__, rc );
123 }
124 if (ret != NULL) {
125 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
126 __FUNCTION__, __LINE__, rc );
127 }
128
129
130 /*--------------------------------------------------*/
131 /* 3 Test for too large destination size */
132 printf("Test #%d:\n", ++testno);
133
134 ret = stpncpy_s(str1, (RSIZE_MAX_STR+1), str2, LEN, &rc);
135 if (rc != ESLEMAX) {
136 printf("%s %u Error rc=%u \n",
137 __FUNCTION__, __LINE__, rc );
138 }
139 if (ret != NULL) {
140 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
141 __FUNCTION__, __LINE__, rc );
142 }
143
144 /*--------------------------------------------------*/
145 /* 4 Test for NULL source check */
146 printf("Test #%d:\n", ++testno);
147
148 strcpy(str1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
149
150 ret = stpncpy_s(str1, 5, NULL, LEN, &rc);
151 if (rc != ESNULLP) {
152 printf("%s %u Error rc=%u \n",
153 __FUNCTION__, __LINE__, rc );
154 }
155
156 if (ret != NULL) {
157 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
158 __FUNCTION__, __LINE__, rc );
159 }
160
161 #ifdef SAFE_LIB_STR_NULL_SLACK
162 for (i=0; i<5; i++) {
163 if (str1[i] != '\0') {
164 printf("%s %u Error rc=%u \n",
165 __FUNCTION__, __LINE__, rc );
166 }
167 }
168 #else
169 if (str1[0] != '\0') {
170 printf("%s %u Error rc=%u \n",
171 __FUNCTION__, __LINE__, rc );
172 }
173 #endif
174
175
176 /*--------------------------------------------------*/
177 /* 5 Test for Src is same as dest, but source too long */
178 printf("Test #%d:\n", ++testno);
179
180 strcpy(str1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
181
182 sz = strnlen_s(str1, LEN);
183 ret = stpncpy_s(str1, 5, str1, sz, &rc);
184 if (rc != ESNOSPC) {
185 printf("%s %u Error rc=%u \n",
186 __FUNCTION__, __LINE__, rc );
187 }
188
189
190 if (ret != NULL) {
191 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
192 __FUNCTION__, __LINE__, rc );
193 }
194
195
196 /*--------------------------------------------------*/
197 /* 6 Test copy the same string onto itself */
198 printf("Test #%d:\n", ++testno);
199
200 strcpy(str1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
201
202 sz = strnlen_s(str1, LEN);
203 ret = stpncpy_s(str1, LEN, str1, sz, &rc);
204 if (rc != EOK) {
205 printf("%s %u Error rc=%u \n",
206 __FUNCTION__, __LINE__, rc );
207 }
208
209 if (ret == NULL || ret[0] != '\0' || ret != str1+strnlen_s(str1, LEN) ) {
210 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
211 __FUNCTION__, __LINE__, rc );
212 }
213
214 /*--------------------------------------------------*/
215 /* 7 Test for string overlap, destination < src, and
216 * dest overlaps onto the src string, so a copy would
217 * change the src string */
218 printf("Test #%d:\n", ++testno);
219
220 strcpy(&str1[0], "keep it simple");
221 sz = strnlen_s(str1, LEN);
222 ret = stpncpy_s(&str1[0], LEN, &str1[5], sz, &rc);
223 if (rc != ESOVRLP) {
224 printf("%s %u Error rc=%u \n",
225 __FUNCTION__, __LINE__, rc );
226 }
227 if (ret != NULL) {
228 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
229 __FUNCTION__, __LINE__, rc );
230 }
231
232 #ifdef SAFE_LIB_STR_NULL_SLACK
233 for (i=0; i<LEN; i++) {
234 if (str1[i] != '\0') {
235 printf("%s %u Error rc=%u \n",
236 __FUNCTION__, __LINE__, rc );
237 }
238 }
239 #else
240 if (str1[0] != '\0') {
241 printf("%s %u Error rc=%u \n",
242 __FUNCTION__, __LINE__, rc );
243 }
244 #endif
245
246
247 /*--------------------------------------------------*/
248 /* 8 Test for string overlap, destination > src, and
249 * src overlaps onto the dest string, so a copy would
250 * result in an infinite copy operation */
251 printf("Test #%d:\n", ++testno);
252
253 strcpy(&str1[0], "keep it simple");
254 sz = strnlen_s(str1, LEN);
255 ret = stpncpy_s(&str1[5], LEN, &str1[0], sz, &rc);
256 if (rc != ESOVRLP) {
257 printf("%s %u Error rc=%u \n",
258 __FUNCTION__, __LINE__, rc );
259 }
260 if (ret != NULL) {
261 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
262 __FUNCTION__, __LINE__, rc );
263 }
264
265 #ifdef SAFE_LIB_STR_NULL_SLACK
266 for (i=0; i<LEN; i++) {
267 if (str1[i] != '\0') {
268 printf("%s %u Error rc=%u \n",
269 __FUNCTION__, __LINE__, rc );
270 }
271 }
272 #else
273 if (str1[5] != '\0') {
274 printf("%s %u Error rc=%u \n",
275 __FUNCTION__, __LINE__, rc );
276 }
277 #endif
278
279
280
281
282
283
284 /*--------------------------------------------------*/
285 /*--------------------------------------------------*/
286 /* 9 Test copying the null string (zero length string)
287 * into another existing string, and overwriting
288 * it's contents */
289 printf("Test #%d:\n", ++testno);
290
291 strcpy(str1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
292 str2[0] = '\0';
293
294 ret = stpncpy_s(str1, LEN/2, str2, 1, &rc);
295 if (rc != EOK) {
296 printf("%s %u Error rc=%u \n",
297 __FUNCTION__, __LINE__, rc );
298 }
299 if (ret == NULL || ret[0] != '\0' || ret != str1) {
300 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
301 __FUNCTION__, __LINE__, rc );
302 }
303
304 #ifdef SAFE_LIB_STR_NULL_SLACK
305 for (i=0; i<LEN/2; i++) {
306 if (str1[i] != '\0') {
307 printf("%s %u Error rc=%u \n",
308 __FUNCTION__, __LINE__, rc );
309 }
310 }
311 #else
312 if (str1[0] != '\0') {
313 printf("%s %u Error rc=%u \n",
314 __FUNCTION__, __LINE__, rc );
315 }
316 #endif
317
318
319 /*--------------------------------------------------*/
320 /* 10 Test for copying NULL string over existing string */
321 printf("Test #%d:\n", ++testno);
322
323 memset_s(str1, '\0', 5);
324 strcpy(&str2[0], "keep it simple");
325
326 ret = stpncpy_s(str2, LEN, str1, 5, &rc);
327 if (rc != EOK) {
328 printf("%s %u Error rc=%u \n",
329 __FUNCTION__, __LINE__, rc );
330 }
331 if (ret == NULL || ret[0] != '\0' || ret != str2) {
332 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
333 __FUNCTION__, __LINE__, rc );
334 }
335
336 if (*str2 != '\0') {
337 printf("%s %u -%s- Error rc=%u \n",
338 __FUNCTION__, __LINE__, str1, rc );
339 }
340
341 /*--------------------------------------------------*/
342 /* 11 Test copying string to end of NULL string */
343 printf("Test #%d:\n", ++testno);
344
345 memset_s(str1, '\0', 50);
346 strcpy(&str2[0], "keep it simple");
347
348 sz = strnlen_s(str2, LEN);
349
350 ret = stpncpy_s(str1, LEN, str2, sz, &rc);
351 if (rc != EOK) {
352 printf("%s %u Error rc=%u \n",
353 __FUNCTION__, __LINE__, rc );
354 }
355
356 /* be sure the results are the same as strcmp */
357 ind = strcmp(str1, str2);
358 if (ind != 0) {
359 printf("%s %u -%s- <> -%s- (smax=%zu) Error rc=%u \n",
360 __FUNCTION__, __LINE__, str1, str2, sz, rc );
361 }
362
363 if (ret == NULL || ret[0] != '\0' || ret != str1+strnlen_s(str1, LEN) ) {
364 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
365 __FUNCTION__, __LINE__, rc );
366 }
367
368 /*--------------------------------------------------*/
369 /* 12 Test for accurate subString copy over existing string */
370 printf("Test #%d:\n", ++testno);
371
372 strcpy(str1, "xxxxxxxxxx");
373 strcpy(str2, "abcde");
374
375 ret = stpncpy_s(str1, LEN, str2, 3, &rc);
376 if (rc != EOK) {
377 printf("%s %u Error rc=%u \n",
378 __FUNCTION__, __LINE__, rc );
379 }
380
381 if (ret == NULL || ret[0] != '\0' || ret != str1+strnlen_s(str1, LEN) ) {
382 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
383 __FUNCTION__, __LINE__, rc );
384 } else {
385
386 /* be sure the results are the same as strcmp */
387 sz = strnlen_s(str1, LEN);
388 ind = strncmp(str1, str2, sz);
389 if (ind != 0 || sz != 3) {
390 printf("%s %u -%s- <> -%s- (smax=%zu) Error rc=%u \n",
391 __FUNCTION__, __LINE__, str1, str2, sz, rc );
392 }
393 /* be sure that the slack is correct */
394 for (i=1; i<6; i++) {
395 if (ret[i] != 'x') {
396 printf("%s %u Incorrect Slack at returned ptr index %d Error rc=%u \n",
397 __FUNCTION__, __LINE__, i, rc );
398 }
399 }
400 }
401
402 /*--------------------------------------------------*/
403 /* 13 Test for accurate String copy with extra nulls over existing string */
404 printf("Test #%d:\n", ++testno);
405
406 strcpy(str1, "xxxxxxxxxxxxxxxxxxxx");
407 strcpy(str2, "abcde");
408
409 ret = stpncpy_s(str1, LEN, str2, 10, &rc);
410 if (rc != EOK) {
411 printf("%s %u Error rc=%u \n",
412 __FUNCTION__, __LINE__, rc );
413 }
414
415 if (ret == NULL || ret[0] != '\0' || ret != str1+strnlen_s(str1, LEN) ) {
416 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
417 __FUNCTION__, __LINE__, rc );
418 } else {
419
420 /* be sure the results are the same as strcmp */
421 ind = strcmp(str1, str2);
422 if (ind != 0) {
423 printf("%s %u -%s- Error rc=%u \n",
424 __FUNCTION__, __LINE__, str1, rc );
425 } else {
426 /* be sure that the nulls are correct */
427 for (i=0; i<5; i++) {
428 if (ret[i] != '\0') {
429 printf("%s %u Incorrect NULL fill at returned ptr index %d Error rc=%u \n",
430 __FUNCTION__, __LINE__, i, rc );
431 }
432 }
433 /* be sure that the slack is correct */
434 for (; i<15; i++) {
435 if (ret[i] != 'x') {
436 printf("%s %u Incorrect Slack at returned ptr index %d Error rc=%u \n",
437 __FUNCTION__, __LINE__, i, rc );
438 }
439 }
440 }
441 }
442
443 /*--------------------------------------------------*/
444 /* 14 Test for accurate String copy and added null over existing string */
445 printf("Test #%d:\n", ++testno);
446
447 strcpy(str1, "xxxxxxxxxx");
448 strcpy(str2, "abcde");
449
450 ret = stpncpy_s(str1, LEN, str2, 5, &rc);
451 if (rc != EOK) {
452 printf("%s %u Error rc=%u \n",
453 __FUNCTION__, __LINE__, rc );
454 }
455
456 if (ret == NULL || ret[0] != '\0' || ret != str1+strnlen_s(str1, LEN) ) {
457 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
458 __FUNCTION__, __LINE__, rc );
459 } else {
460
461 /* be sure the results are the same as strcmp */
462 ind = strcmp(str1, str2);
463 if (ind != 0) {
464 printf("%s %u -%s- Error rc=%u \n",
465 __FUNCTION__, __LINE__, str1, rc );
466 } else {
467 /* be sure that the slack is correct */
468 for (i=1; i<5; i++) {
469 if (ret[i] != 'x') {
470 printf("%s %u Incorrect Slack at returned ptr index %d Error rc=%u \n",
471 __FUNCTION__, __LINE__, i, rc );
472 }
473 }
474 }
475 }
476
477 /*--------------------------------------------------*/
478 /* 15 Test for accurate String copy and added null over existing string */
479 printf("Test #%d:\n", ++testno);
480
481 strcpy(str1, "xxxxxxxxxx");
482 strcpy(str2, "abcde");
483
484 ret = stpncpy_s(str1, LEN, str2, 6, &rc);
485 if (rc != EOK) {
486 printf("%s %u Error rc=%u \n",
487 __FUNCTION__, __LINE__, rc );
488 }
489
490 if (ret == NULL || ret[0] != '\0' || ret != str1+strnlen_s(str1, LEN) ) {
491 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
492 __FUNCTION__, __LINE__, rc );
493 } else {
494
495 /* be sure the results are the same as strcmp */
496 ind = strcmp(str1, str2);
497 if (ind != 0) {
498 printf("%s %u -%s- Error rc=%u \n",
499 __FUNCTION__, __LINE__, str1, rc );
500 } else {
501 /* be sure that the slack is correct */
502 for (i=1; i<5; i++) {
503 if (ret[i] != 'x') {
504 printf("%s %u Incorrect Slack at returned ptr index %d Error rc=%u \n",
505 __FUNCTION__, __LINE__, i, rc );
506 }
507 }
508 }
509 }
510
511 /*--------------------------------------------------*/
512 /* 16 Test for not enough space in destination */
513 printf("Test #%d:\n", ++testno);
514
515 strcpy(str1, "qqweqeqeqeq");
516 strcpy(str2, "keep it simple");
517 sz = strnlen_s(str2, LEN);
518
519 ret = stpncpy_s(str1, 1, str2, sz, &rc);
520 if (rc != ESNOSPC) {
521 printf("%s %u Error rc=%u \n",
522 __FUNCTION__, __LINE__, rc );
523 }
524 if (ret != NULL) {
525 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
526 __FUNCTION__, __LINE__, rc );
527 }
528
529 if (*str1 != '\0') {
530 printf("%s %u -%s- Error rc=%u \n",
531 __FUNCTION__, __LINE__, str1, rc );
532 }
533
534 /*--------------------------------------------------*/
535 /* 17 Test for not enough space in destination for null terminator */
536 printf("Test #%d:\n", ++testno);
537
538 strcpy(str1, "qqweqeqeqeq");
539 strcpy(str2, "keep it simple");
540 sz = strnlen_s(str2, LEN);
541
542 ret = stpncpy_s(str1, 14, str2, sz, &rc);
543 if (rc != ESNOSPC) {
544 printf("%s %u Error rc=%u \n",
545 __FUNCTION__, __LINE__, rc );
546 }
547 if (ret != NULL) {
548 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
549 __FUNCTION__, __LINE__, rc );
550 }
551
552 if (*str1 != '\0') {
553 printf("%s %u -%s- Error rc=%u \n",
554 __FUNCTION__, __LINE__, str1, rc );
555 }
556
557 /*--------------------------------------------------*/
558 /* 18: Test for just enough space in destination */
559 printf("Test #%d:\n", ++testno);
560
561 strcpy(str1, "qqweqeqeqeq");
562 strcpy(str2, "it");
563 sz = strnlen_s(str2, LEN);
564
565 ret = stpncpy_s(str1, 3, str2, sz, &rc);
566 if (rc != EOK) {
567 printf("%s %u Error rc=%u \n",
568 __FUNCTION__, __LINE__, rc );
569 }
570
571 /* be sure the results are the same as strcmp */
572 ind = strcmp(str1, str2);
573 if (ind != 0) {
574 printf("%s %u -%s- Error rc=%u \n",
575 __FUNCTION__, __LINE__, str1, rc );
576 }
577 if (ret == NULL || ret[0] != '\0' || ret != str1+strnlen_s(str1, LEN) ) {
578 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
579 __FUNCTION__, __LINE__, rc );
580 }
581
582 /*--------------------------------------------------*/
583 /* 19: Test copy short string over long string */
584 printf("Test #%d:\n", ++testno);
585
586 strcpy(str1, "qq12345weqeqeqeq");
587 strcpy(str2, "it");
588
589 ret = stpncpy_s(str1, 10, str2, 2, &rc);
590 if (rc != EOK) {
591 printf("%s %u Error rc=%u \n",
592 __FUNCTION__, __LINE__, rc );
593 }
594 if (ret == NULL || ret[0] != '\0' || ret != str1+strnlen_s(str1, LEN) ) {
595 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
596 __FUNCTION__, __LINE__, rc );
597 }
598
599 /* be sure the results are the same as strcpy */
600 ind = strcmp(str1, str2);
601 if (ind != 0) {
602 printf("%s %u -%s- Error rc=%u \n",
603 __FUNCTION__, __LINE__, str1, rc );
604 }
605
606 /*--------------------------------------------------*/
607
608 return (0);
609 }
610