Debian Codegraph @

ext/safestringlib/unittests/test_stpcpy_s.c

raw
1 /*------------------------------------------------------------------
2 * test_stpcpy_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 longer string copy over shorter string
54 * TC13: Test for not enough space in destination (dest < src)
55 * TC15: Test for just enough space in destination
56 *
57 * TEST NON-STANDARD FAILURE CONDITIONS FOR NULL DEST STRINGS
58 * TC 9: Test copy null string over destination string (dest < src)
59 * TC10: Test copy NULL string over existing string (dest > src)
60 * TC11: Test copy string over NULL string (dest < src)
61 *
62 * TEST WHEN DEST BUFFER >= SOURCE BUFFER
63 * TC 8: Test src buffer runs into beginning of dest buffer (src buffer is unterminated or been destroyed by copy)
64 * TC14: Test for not enough space in destination (dest > src)
65 * TC16: Test copy short string over long string
66 *
67 *
68 *
69 *------------------------------------------------------------------
70 */
71
72 #include "test_private.h"
73 #include "safe_str_lib.h"
74
75 #define MAX ( 128 )
76 #define LEN ( 128 )
77
78 static char str1[LEN];
79 static char str2[LEN];
80
81 int test_stpcpy_s (void)
82 {
83 char *ret;
84 errno_t rc;
85 #ifdef SAFE_LIB_STR_NULL_SLACK
86 uint32_t i;
87 #endif /*SAFE_LIB_STR_NULL_SLACK*/
88 int32_t ind;
89 unsigned int testno = 0;
90
91 printf("\nTesting stpcpy_s:\n");
92 /*--------------------------------------------------*/
93 /* 1 Test for NULL destination check */
94 printf("Test #%d:\n", ++testno);
95
96 ret = stpcpy_s(NULL, LEN, str2, &rc);
97 if (rc != ESNULLP) {
98 printf("%s %u Error rc=%u \n",
99 __FUNCTION__, __LINE__, rc );
100 }
101
102 if (ret != NULL) {
103 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
104 __FUNCTION__, __LINE__, rc );
105 }
106
107 /*--------------------------------------------------*/
108 /* 2 Test for zero length destination */
109 printf("Test #%d:\n", ++testno);
110
111 ret = stpcpy_s(str1, 0, str2, &rc);
112 if (rc != ESZEROL) {
113 printf("%s %u Error rc=%u \n",
114 __FUNCTION__, __LINE__, rc );
115 }
116 if (ret != NULL) {
117 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
118 __FUNCTION__, __LINE__, rc );
119 }
120
121
122 /*--------------------------------------------------*/
123 /* 3 Test for too large destination size */
124 printf("Test #%d:\n", ++testno);
125
126 ret = stpcpy_s(str1, (RSIZE_MAX_STR+1), str2, &rc);
127 if (rc != ESLEMAX) {
128 printf("%s %u Error rc=%u \n",
129 __FUNCTION__, __LINE__, rc );
130 }
131 if (ret != NULL) {
132 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
133 __FUNCTION__, __LINE__, rc );
134 }
135
136 /*--------------------------------------------------*/
137 /* 4 Test for NULL source check */
138 printf("Test #%d:\n", ++testno);
139
140 strcpy(str1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
141
142 ret = stpcpy_s(str1, 5, NULL, &rc);
143 if (rc != ESNULLP) {
144 printf("%s %u Error rc=%u \n",
145 __FUNCTION__, __LINE__, rc );
146 }
147
148 if (ret != NULL) {
149 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
150 __FUNCTION__, __LINE__, rc );
151 }
152
153 #ifdef SAFE_LIB_STR_NULL_SLACK
154 for (i=0; i<5; i++) {
155 if (str1[i] != '\0') {
156 printf("%s %u Error rc=%u \n",
157 __FUNCTION__, __LINE__, rc );
158 }
159 }
160 #else
161 if (str1[0] != '\0') {
162 printf("%s %u Error rc=%u \n",
163 __FUNCTION__, __LINE__, rc );
164 }
165 #endif
166
167
168 /*--------------------------------------------------*/
169 /* 5 Test for Src is same as dest, but source too long */
170 printf("Test #%d:\n", ++testno);
171
172 strcpy(str1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
173
174 ret = stpcpy_s(str1, 5, str1, &rc);
175 if (rc != ESNOSPC) {
176 printf("%s %u Error rc=%u \n",
177 __FUNCTION__, __LINE__, rc );
178 }
179
180
181 if (ret != NULL) {
182 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
183 __FUNCTION__, __LINE__, rc );
184 }
185
186
187 /*--------------------------------------------------*/
188 /* 6 Test copy the same string onto itself */
189 printf("Test #%d:\n", ++testno);
190
191 strcpy(str1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
192
193 ret = stpcpy_s(str1, LEN, str1, &rc);
194 if (rc != EOK) {
195 printf("%s %u Error rc=%u \n",
196 __FUNCTION__, __LINE__, rc );
197 }
198
199 if (ret == NULL || ret[0] != '\0' || ret != str1+strnlen_s(str1, LEN) ) {
200 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
201 __FUNCTION__, __LINE__, rc );
202 }
203
204 /*--------------------------------------------------*/
205 /* 7 Test for string overlap, destination < src, and
206 * dest overlaps onto the src string, so a copy would
207 * change the src string */
208 printf("Test #%d:\n", ++testno);
209
210 strcpy(&str1[0], "keep it simple");
211
212 ret = stpcpy_s(&str1[0], LEN, &str1[5], &rc);
213 if (rc != ESOVRLP) {
214 printf("%s %u Error rc=%u \n",
215 __FUNCTION__, __LINE__, rc );
216 }
217 if (ret != NULL) {
218 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
219 __FUNCTION__, __LINE__, rc );
220 }
221
222 #ifdef SAFE_LIB_STR_NULL_SLACK
223 for (i=0; i<LEN; i++) {
224 if (str1[i] != '\0') {
225 printf("%s %u Error rc=%u \n",
226 __FUNCTION__, __LINE__, rc );
227 }
228 }
229 #else
230 if (str1[0] != '\0') {
231 printf("%s %u Error rc=%u \n",
232 __FUNCTION__, __LINE__, rc );
233 }
234 #endif
235
236
237 /*--------------------------------------------------*/
238 /* 8 Test for string overlap, destination > src, and
239 * src overlaps onto the dest string, so a copy would
240 * result in an infinite copy operation */
241 printf("Test #%d:\n", ++testno);
242
243 strcpy(&str1[0], "keep it simple");
244
245 ret = stpcpy_s(&str1[5], LEN, &str1[0], &rc);
246 if (rc != ESOVRLP) {
247 printf("%s %u Error rc=%u \n",
248 __FUNCTION__, __LINE__, rc );
249 }
250 if (ret != NULL) {
251 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
252 __FUNCTION__, __LINE__, rc );
253 }
254
255 #ifdef SAFE_LIB_STR_NULL_SLACK
256 for (i=0; i<LEN; i++) {
257 if (str1[i] != '\0') {
258 printf("%s %u Error rc=%u \n",
259 __FUNCTION__, __LINE__, rc );
260 }
261 }
262 #else
263 if (str1[5] != '\0') {
264 printf("%s %u Error rc=%u \n",
265 __FUNCTION__, __LINE__, rc );
266 }
267 #endif
268
269
270
271
272
273
274 /*--------------------------------------------------*/
275 /*--------------------------------------------------*/
276 /* 9 Test copying the null string (zero length string)
277 * into another existing string, and overwriting
278 * it's contents */
279 printf("Test #%d:\n", ++testno);
280
281 strcpy(str1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
282 str2[0] = '\0';
283
284 ret = stpcpy_s(str1, LEN/2, str2, &rc);
285 if (rc != EOK) {
286 printf("%s %u Error rc=%u \n",
287 __FUNCTION__, __LINE__, rc );
288 }
289 if (ret == NULL || ret[0] != '\0' || ret != str1) {
290 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
291 __FUNCTION__, __LINE__, rc );
292 }
293
294 #ifdef SAFE_LIB_STR_NULL_SLACK
295 for (i=0; i<LEN/2; i++) {
296 if (str1[i] != '\0') {
297 printf("%s %u Error rc=%u \n",
298 __FUNCTION__, __LINE__, rc );
299 }
300 }
301 #else
302 if (str1[0] != '\0') {
303 printf("%s %u Error rc=%u \n",
304 __FUNCTION__, __LINE__, rc );
305 }
306 #endif
307
308
309 /*--------------------------------------------------*/
310 /* 10 Test for copying NULL string over existing string */
311 printf("Test #%d:\n", ++testno);
312
313 str1[0] = '\0';
314 strcpy(&str2[0], "keep it simple");
315
316 ret = stpcpy_s(str2, LEN, str1, &rc);
317 if (rc != EOK) {
318 printf("%s %u Error rc=%u \n",
319 __FUNCTION__, __LINE__, rc );
320 }
321 if (ret == NULL || ret[0] != '\0' || ret != str2) {
322 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
323 __FUNCTION__, __LINE__, rc );
324 }
325
326 if (*str2 != '\0') {
327 printf("%s %u -%s- Error rc=%u \n",
328 __FUNCTION__, __LINE__, str1, rc );
329 }
330
331 /*--------------------------------------------------*/
332 /* 11 Test copying string to end of NULL string */
333 printf("Test #%d:\n", ++testno);
334
335 str1[0] = '\0';
336 strcpy(&str2[0], "keep it simple");
337
338 ret = stpcpy_s(str1, LEN, str2, &rc);
339 if (rc != EOK) {
340 printf("%s %u Error rc=%u \n",
341 __FUNCTION__, __LINE__, rc );
342 }
343
344 /* be sure the results are the same as strcmp */
345 ind = strcmp(str1, str2);
346 if (ind != 0) {
347 printf("%s %u -%s- Error rc=%u \n",
348 __FUNCTION__, __LINE__, str1, rc );
349 }
350
351 if (ret == NULL || ret[0] != '\0' || ret != str1+strnlen_s(str1, LEN) ) {
352 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
353 __FUNCTION__, __LINE__, rc );
354 }
355
356 /*--------------------------------------------------*/
357 /* 12 Test for accurate String copy over existing string */
358 printf("Test #%d:\n", ++testno);
359
360 strcpy(str1, "qqweqeqeqeq");
361 strcpy(str2, "keep it simple");
362
363 ret = stpcpy_s(str1, LEN, str2, &rc);
364 if (rc != EOK) {
365 printf("%s %u Error rc=%u \n",
366 __FUNCTION__, __LINE__, rc );
367 }
368
369 if (ret == NULL || ret[0] != '\0' || ret != str1+strnlen_s(str1, LEN) ) {
370 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
371 __FUNCTION__, __LINE__, rc );
372 }
373
374 /* be sure the results are the same as strcmp */
375 ind = strcmp(str1, str2);
376 if (ind != 0) {
377 printf("%s %u -%s- Error rc=%u \n",
378 __FUNCTION__, __LINE__, str1, rc );
379 }
380
381 /*--------------------------------------------------*/
382 /* 13: Test for not enough space in destination */
383 printf("Test #%d:\n", ++testno);
384
385 strcpy(str1, "qqweqeqeqeq");
386 strcpy(str2, "keep it simple");
387
388 ret = stpcpy_s(str1, 1, str2, &rc);
389 if (rc != ESNOSPC) {
390 printf("%s %u Error rc=%u \n",
391 __FUNCTION__, __LINE__, rc );
392 }
393 if (ret != NULL) {
394 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
395 __FUNCTION__, __LINE__, rc );
396 }
397
398 if (*str1 != '\0') {
399 printf("%s %u -%s- Error rc=%u \n",
400 __FUNCTION__, __LINE__, str1, rc );
401 }
402
403 /*--------------------------------------------------*/
404 /* 14: Test for not enough space in destination (dest > src) */
405 printf("Test #%d:\n", ++testno);
406
407 strcpy(str1, "qqweqeqeqeq");
408 strcpy(str2, "keep it simple");
409
410 ret = stpcpy_s(str2, 2, str1, &rc);
411 if (rc != ESNOSPC) {
412 printf("%s %u Error rc=%u \n",
413 __FUNCTION__, __LINE__, rc );
414 }
415 if (ret != NULL) {
416 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
417 __FUNCTION__, __LINE__, rc );
418 }
419
420 if (*str2 != '\0') {
421 printf("%s %u -%s- Error rc=%u \n",
422 __FUNCTION__, __LINE__, str1, rc );
423 }
424
425 /*--------------------------------------------------*/
426 /* 15: Test for just enough space in destination */
427 printf("Test #%d:\n", ++testno);
428
429 strcpy(str1, "qqweqeqeqeq");
430 strcpy(str2, "it");
431
432 ret = stpcpy_s(str1, 3, str2, &rc);
433 if (rc != EOK) {
434 printf("%s %u Error rc=%u \n",
435 __FUNCTION__, __LINE__, rc );
436 }
437
438 /* be sure the results are the same as strcmp */
439 ind = strcmp(str1, str2);
440 if (ind != 0) {
441 printf("%s %u -%s- Error rc=%u \n",
442 __FUNCTION__, __LINE__, str1, rc );
443 }
444 if (ret == NULL || ret[0] != '\0' || ret != str1+strnlen_s(str1, LEN) ) {
445 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
446 __FUNCTION__, __LINE__, rc );
447 }
448
449 /*--------------------------------------------------*/
450 /* 16: Test copy short string over long string */
451 printf("Test #%d:\n", ++testno);
452
453 strcpy(str2, "qq12345weqeqeqeq");
454 strcpy(str1, "it");
455
456 ret = stpcpy_s(str2, 10, str1, &rc);
457 if (rc != EOK) {
458 printf("%s %u Error rc=%u \n",
459 __FUNCTION__, __LINE__, rc );
460 }
461 if (ret == NULL || ret[0] != '\0' || ret != str2+strnlen_s(str2, LEN) ) {
462 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
463 __FUNCTION__, __LINE__, rc );
464 }
465
466 /* be sure the results are the same as strcpy */
467 ind = strcmp(str1, str2);
468 if (ind != 0) {
469 printf("%s %u -%s- Error rc=%u \n",
470 __FUNCTION__, __LINE__, str1, rc );
471 }
472
473
474 /*--------------------------------------------------*/
475
476 return (0);
477 }
478