Debian Codegraph @

ext/safestringlib/unittests/test_wcpcpy_s.c

raw
1 /*------------------------------------------------------------------
2 * test_wcpcpy_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 copy of short string over long string
54 * TC13: Test for not enough space in destination (dest < src)
55 * TC16: Test copy short string over long string
56 *
57 * TEST NON-STANDARD FAILURE CONDITIONS FOR NULL DEST STRINGS
58 * TC 9: Test copy null string over destination string
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 (src < dest)
65 * TC15: Test copy of string - just enough space in dest
66 * TC17: Test copy with not enough room for final NULL in destination
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 wchar_t str1[LEN];
79 static wchar_t str2[LEN];
80
81 extern wchar_t *wcpcpy_s(wchar_t* dest, rsize_t dmax, const wchar_t* src, errno_t *err);
82 extern errno_t wcscpy_s(wchar_t* dest, rsize_t dmax, const wchar_t* src);
83 extern rsize_t wcsnlen_s (const wchar_t *dest, rsize_t dmax);
84
85 int test_wcpcpy_s (void)
86 {
87 wchar_t *ret;
88 errno_t rc;
89 #ifdef SAFE_LIB_STR_NULL_SLACK
90 uint32_t i;
91 #endif /*SAFE_LIB_STR_NULL_SLACK*/
92 int32_t ind;
93 rsize_t sz;
94 unsigned int testno = 0;
95
96
97 printf("\nTesting wcpcpy_s:\n");
98 /*--------------------------------------------------*/
99 /* 1 Test for NULL destination check */
100 printf("Test #%d:\n", ++testno);
101
102 ret = wcpcpy_s(NULL, LEN, str2, &rc);
103 if (rc != ESNULLP) {
104 printf("%s %u Error rc=%u \n",
105 __FUNCTION__, __LINE__, rc );
106 }
107
108 if (ret != NULL) {
109 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
110 __FUNCTION__, __LINE__, rc );
111 }
112
113 /*--------------------------------------------------*/
114 /* 2 Test for zero length destination */
115 printf("Test #%d:\n", ++testno);
116
117 ret = wcpcpy_s(str1, 0, str2, &rc);
118 if (rc != ESZEROL) {
119 printf("%s %u Error rc=%u \n",
120 __FUNCTION__, __LINE__, rc );
121 }
122 if (ret != NULL) {
123 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
124 __FUNCTION__, __LINE__, rc );
125 }
126
127
128 /*--------------------------------------------------*/
129 /* 3 Test for too large destination size */
130 printf("Test #%d:\n", ++testno);
131
132 ret = wcpcpy_s(str1, (RSIZE_MAX_STR+1), str2, &rc);
133 if (rc != ESLEMAX) {
134 printf("%s %u Error rc=%u \n",
135 __FUNCTION__, __LINE__, rc );
136 }
137 if (ret != NULL) {
138 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
139 __FUNCTION__, __LINE__, rc );
140 }
141
142 /*--------------------------------------------------*/
143 /* 4 Test for NULL source check */
144 printf("Test #%d:\n", ++testno);
145
146 wcscpy_s(str1, LEN, L"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
147
148 ret = wcpcpy_s(str1, 5, NULL, &rc);
149 if (rc != ESNULLP) {
150 printf("%s %u Error rc=%u \n",
151 __FUNCTION__, __LINE__, rc );
152 }
153
154 if (ret != NULL) {
155 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
156 __FUNCTION__, __LINE__, rc );
157 }
158
159 #ifdef SAFE_LIB_STR_NULL_SLACK
160 for (i=0; i<5; i++) {
161 if (str1[i] != L'\0') {
162 printf("%s %u Error rc=%u \n",
163 __FUNCTION__, __LINE__, rc );
164 }
165 }
166 #else
167 if (str1[0] != L'\0') {
168 printf("%s %u Error rc=%u \n",
169 __FUNCTION__, __LINE__, rc );
170 }
171 #endif
172
173
174 /*--------------------------------------------------*/
175 /* 5 Test for Src is same as dest, but source too long */
176 printf("Test #%d:\n", ++testno);
177
178 wcscpy_s(str1, LEN, L"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
179
180 ret = wcpcpy_s(str1, 5, str1, &rc);
181 if (rc != ESLEMAX) {
182 printf("%s %u Error rc=%u \n",
183 __FUNCTION__, __LINE__, rc );
184 }
185
186 if (ret != NULL) {
187 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
188 __FUNCTION__, __LINE__, rc );
189 }
190
191
192 /*--------------------------------------------------*/
193 /* 6 Test copy the same string onto itself */
194 printf("Test #%d:\n", ++testno);
195
196 wcscpy_s(str1, LEN, L"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
197
198 ret = wcpcpy_s(str1, LEN, str1, &rc);
199 if (rc != EOK) {
200 printf("%s %u Error rc=%u \n",
201 __FUNCTION__, __LINE__, rc );
202 }
203
204 if (ret == NULL || ret[0] != L'\0' || ret != str1+wcsnlen_s(str1, LEN) ) {
205 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
206 __FUNCTION__, __LINE__, rc );
207 }
208
209 /*--------------------------------------------------*/
210 /*--------------------------------------------------*/
211 /* 7 Test for string overlap, destination < src, and
212 * dest overlaps onto the src string, so a copy would
213 * change the src string */
214 printf("Test #%d:\n", ++testno);
215
216 wcscpy_s(&str1[0], LEN, L"keep it simple");
217 sz = wcsnlen_s(str1, LEN);
218
219 ret = wcpcpy_s(&str1[0], LEN, &str1[5], &rc);
220 if (rc != ESOVRLP) {
221 printf("%s %u Error rc=%u \n",
222 __FUNCTION__, __LINE__, rc );
223 }
224 if (ret != NULL) {
225 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
226 __FUNCTION__, __LINE__, rc );
227 }
228
229 #ifdef SAFE_LIB_STR_NULL_SLACK
230 for (i=0; i<LEN; i++) {
231 if (str1[i] != L'\0') {
232 printf("%s %u Error rc=%u \n",
233 __FUNCTION__, __LINE__, rc );
234 }
235 }
236 #else
237 if (str1[0] != L'\0') {
238 printf("%s %u Error rc=%u \n",
239 __FUNCTION__, __LINE__, rc );
240 }
241 #endif
242
243
244 /*--------------------------------------------------*/
245 /* 8 Test for string overlap, src < dest, and
246 * src overlaps onto the dest string, so a copy would
247 * result in an infinite copy operation */
248 printf("Test #%d:\n", ++testno);
249
250 wcscpy_s(&str1[0], LEN, L"keep it simple");
251 sz = wcsnlen_s(str1, LEN);
252
253 ret = wcpcpy_s(&str1[5], LEN, &str1[0], &rc);
254 if (rc != ESOVRLP) {
255 printf("%s %u Error rc=%u \n",
256 __FUNCTION__, __LINE__, rc );
257 }
258 if (ret != NULL) {
259 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
260 __FUNCTION__, __LINE__, rc );
261 }
262
263 #ifdef SAFE_LIB_STR_NULL_SLACK
264 for (i=0; i<LEN; i++) {
265 if (str1[i] != L'\0') {
266 printf("%s %u Error rc=%u \n",
267 __FUNCTION__, __LINE__, rc );
268 }
269 }
270 #else
271 if (str1[5] != L'\0') {
272 printf("%s %u Error rc=%u \n",
273 __FUNCTION__, __LINE__, rc );
274 }
275 #endif
276
277
278
279
280
281
282 /*--------------------------------------------------*/
283 /*--------------------------------------------------*/
284 /* 9 Test copying the null string (zero length string)
285 * into another existing string, and overwriting
286 * it's contents */
287 printf("Test #%d:\n", ++testno);
288
289 wcscpy_s(str1, LEN, L"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
290 str2[0] = L'\0';
291
292 ret = wcpcpy_s(str1, LEN/2, str2, &rc);
293 if (rc != EOK) {
294 printf("%s %u Error rc=%u \n",
295 __FUNCTION__, __LINE__, rc );
296 }
297 if (ret == NULL || ret[0] != L'\0' || ret != str1) {
298 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
299 __FUNCTION__, __LINE__, rc );
300 }
301
302 #ifdef SAFE_LIB_STR_NULL_SLACK
303 for (i=0; i<LEN/2; i++) {
304 if (str1[i] != L'\0') {
305 printf("%s %u Error rc=%u \n",
306 __FUNCTION__, __LINE__, rc );
307 }
308 }
309 #else
310 if (str1[0] != L'\0') {
311 printf("%s %u Error - string should be NULL rc=%u \n",
312 __FUNCTION__, __LINE__, rc );
313 }
314 #endif
315
316
317 /*--------------------------------------------------*/
318 /* 10 Test for copying string over NULL string */
319 printf("Test #%d:\n", ++testno);
320
321 wcscpy_s(str1, LEN, L"keep it simple");
322
323 memset_s(str2, '\0', 30);
324
325 ret = wcpcpy_s(str2, LEN, str1, &rc);
326 if (rc != EOK) {
327 printf("%s %u Error rc=%u \n",
328 __FUNCTION__, __LINE__, rc );
329 }
330 if (ret == NULL || ret[0] != L'\0' || ret != str2+wcsnlen_s(str2,LEN)) {
331 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
332 __FUNCTION__, __LINE__, rc );
333 }
334
335 rc = memcmp_s(str2, LEN, L"keep it simple", (15)*sizeof(wchar_t), &ind );
336 if (ind != 0) {
337 printf("%s %u Error -%ls- \n",
338 __FUNCTION__, __LINE__, str1);
339 }
340
341
342 /*--------------------------------------------------*/
343 /* 11 Test copying string over the NULL string */
344 printf("Test #%d:\n", ++testno);
345
346 memset_s(str1, '\0', 50);
347 wcscpy_s(&str2[0], LEN, L"keep it simple");
348
349 sz = wcsnlen_s(str2, LEN);
350
351 ret = wcpcpy_s(str1, LEN, str2, &rc);
352 if (rc != EOK) {
353 printf("%s %u Error rc=%u \n",
354 __FUNCTION__, __LINE__, rc );
355 }
356
357 rc = memcmp_s(str1, LEN, str2, (wcsnlen_s(str2, LEN))*sizeof(wchar_t), &ind );
358 if (ind != 0) {
359 printf("%s %u -%ls- <> -%ls- (smax=%lu) Error rc=%u \n",
360 __FUNCTION__, __LINE__, str1, str2, sz, rc );
361 }
362
363 if (ret == NULL || ret[0] != L'\0' || ret != str1+wcsnlen_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 wcscpy_s(str1, LEN, L"xxxxxxxxxx");
373 wcscpy_s(str2, LEN, L"abcde");
374
375 ret = wcpcpy_s(str1, LEN, str2, &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] != L'\0' || ret != str1+wcsnlen_s(str1, LEN) ) {
382 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
383 __FUNCTION__, __LINE__, rc );
384 }
385
386 sz = wcsnlen_s(str1, LEN);
387 if (sz != 5) {
388 printf("%s %u (sz=%lu <> 5) Error rc=%u \n",
389 __FUNCTION__, __LINE__, sz, rc );
390 }
391 rc = memcmp_s(str1, LEN, str2, (6)*sizeof(wchar_t), &ind );
392 if (ind != 0) {
393 printf("%s %u -%ls- <> -%ls- (size=%lu) Error rc=%u \n",
394 __FUNCTION__, __LINE__, str1, str2, sz, rc );
395 }
396
397
398 /*--------------------------------------------------*/
399 /* 13 Test for not enough space in destination (dest < src) */
400 printf("Test #%d:\n", ++testno);
401
402 wcscpy_s(str1, LEN, L"qqweqeqeqeq");
403 wcscpy_s(str2, LEN, L"keep it simple");
404 sz = wcsnlen_s(str2, LEN);
405
406 ret = wcpcpy_s(str1, 14, str2, &rc);
407 if (rc != ESNOSPC) {
408 printf("%s %u Error rc=%u \n",
409 __FUNCTION__, __LINE__, rc );
410 }
411 if (ret != NULL) {
412 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
413 __FUNCTION__, __LINE__, rc );
414 }
415
416 if (*str1 != L'\0') {
417 printf("%s %u -%ls- Error rc=%u \n",
418 __FUNCTION__, __LINE__, str1, rc );
419 }
420
421 /*--------------------------------------------------*/
422 /* 14 Test for not enough space in destination (src < dest) */
423 printf("Test #%d:\n", ++testno);
424
425 wcscpy_s(str1, LEN, L"qqweqeqeqeq");
426 wcscpy_s(str2, LEN, L"keep it simple");
427 sz = wcsnlen_s(str2, LEN);
428
429 ret = wcpcpy_s(str2, 6, str1, &rc);
430 if (rc != ESNOSPC) {
431 printf("%s %u Error rc=%u \n",
432 __FUNCTION__, __LINE__, rc );
433 }
434 if (ret != NULL) {
435 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
436 __FUNCTION__, __LINE__, rc );
437 }
438
439 if (*str2 != L'\0') {
440 printf("%s %u -%ls- Error rc=%u \n",
441 __FUNCTION__, __LINE__, str1, rc );
442 }
443
444 /*--------------------------------------------------*/
445 /* 15 Test for just enough space in destination */
446 printf("Test #%d:\n", ++testno);
447
448 wcscpy_s(str2, LEN, L"qqweqeqeqeq");
449 wcscpy_s(str1, LEN, L"it");
450
451 ret = wcpcpy_s(str2, 3, str1, &rc);
452 if (rc != EOK) {
453 printf("%s %u Error rc=%u \n",
454 __FUNCTION__, __LINE__, rc );
455 }
456
457 rc = memcmp_s(str1, LEN, str2, (3)*sizeof(wchar_t), &ind );
458 if (ind != 0) {
459 printf("%s %u -%ls- Error rc=%u \n",
460 __FUNCTION__, __LINE__, str1, rc );
461 }
462 if (ret == NULL || ret[0] != L'\0' || ret != str2+wcsnlen_s(str2, LEN) ) {
463 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
464 __FUNCTION__, __LINE__, rc );
465 }
466
467 /*--------------------------------------------------*/
468 /* 16 Test copy short string over long string */
469 printf("Test #%d:\n", ++testno);
470
471 wcscpy_s(str1, LEN, L"qq12345weqeqeqeq");
472 wcscpy_s(str2, LEN, L"it");
473
474 ret = wcpcpy_s(str1, 10, str2, &rc);
475 if (rc != EOK) {
476 printf("%s %u Error rc=%u \n",
477 __FUNCTION__, __LINE__, rc );
478 }
479 if (ret == NULL || ret[0] != L'\0' || ret != str1+wcsnlen_s(str1, LEN) ) {
480 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
481 __FUNCTION__, __LINE__, rc );
482 }
483
484 rc = memcmp_s(str1, LEN, str2, (3)*sizeof(wchar_t), &ind );
485 if (ind != 0) {
486 printf("%s %u -%ls- Error rc=%u \n",
487 __FUNCTION__, __LINE__, str1, rc );
488 }
489
490
491 /*--------------------------------------------------*/
492 /* 17 Test for not enough space in destination for final NULL */
493 printf("Test #%d:\n", ++testno);
494
495 wcscpy_s(str2, LEN, L"qqweqeqeqeq");
496 wcscpy_s(str1, LEN, L"it");
497 sz = wcsnlen_s(str2, LEN);
498
499 ret = wcpcpy_s(str2, 2, str1, &rc);
500 if (rc != ESNOSPC) {
501 printf("%s %u Error rc=%u \n",
502 __FUNCTION__, __LINE__, rc );
503 }
504
505 if (ret != NULL) {
506 printf("Returned pointer incorrect: %s %u Error rc=%u \n",
507 __FUNCTION__, __LINE__, rc );
508 }
509
510 if (*str2 != L'\0') {
511 printf("%s %u -%ls- Error rc=%u \n",
512 __FUNCTION__, __LINE__, str1, rc );
513 }
514 /*--------------------------------------------------*/
515
516 return (0);
517 }
518