Debian Codegraph @

ext/safestringlib/unittests/test_wcsncpy_s.c

raw
1 /*------------------------------------------------------------------
2 * test_wcsncpy_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 * TC 5: Test for last parameter, source length (slen), being sent in as zero
47 * TC 6: Test for last parameter, source length (slen), being larger than the maximum allowed size
48 *
49 * TEST WHEN SRC & DEST ARE THE SAME
50 * TC 7: Test for src=dest, overlapping buffers not the same
51 *
52 * TEST WHEN DEST BUFFER < SOURCE BUFFER
53 * TC 8: Test for overlap of dest buffer into src buffer
54 * TC 9: Test copy only slen characters from src into dest (src string longer than provided length)
55 * TC10: Test for copy dest < src, and copy ends after copy NULL char (slen > char string)
56 * TC11: Test for copy dest < src, but dmax too small to hold src string
57 * TC16: Test for accurate String copy over existing string (src > dest)
58 *
59 * TEST WHEN DEST BUFFER >= SOURCE BUFFER
60 * TC12: Test overlapping buffers fails ( dest > src )
61 * TC13: Test copy only slen characters from src into dest (dest > src)
62 * TC14: Test for copy src < dest, and copy ends after copy NULL char (slen > char string)
63 * TC15: Test for copy src < dest, but dmax too small to hold src string
64 * TC17: Test for just enough space in destination (src < dest)
65 * TC18: Test for not enough space in destination for final NULL (dest > src)
66 *
67 *------------------------------------------------------------------
68 */
69
70 #include "test_private.h"
71 #include "safe_str_lib.h"
72
73 #define MAX ( 128*4 )
74 #define LEN ( 128*4 )
75
76 static wchar_t str1[LEN];
77 static wchar_t str2[LEN];
78
79 extern errno_t wcsncpy_s(wchar_t* dest, rsize_t dmax, const wchar_t* src, rsize_t slen);
80
81 extern rsize_t wcsnlen_s(const wchar_t *dest, rsize_t dmax);
82 extern errno_t wmemset_s (wchar_t *dest, wchar_t value, rsize_t len);
83
84 int test_wcsncpy_s (void)
85 {
86 errno_t rc;
87 #ifdef SAFE_LIB_STR_NULL_SLACK
88 uint32_t i;
89 #endif /*SAFE_LIB_STR_NULL_SLACK*/
90 int32_t ind;
91 rsize_t sz;
92 rsize_t sz_orig;
93 unsigned int testno = 0;
94
95
96 printf("\nTesting wcsncpy_s:\n");
97 /*--------------------------------------------------*/
98 /* 1 Test for NULL destination check */
99 printf("Test #%d:\n", ++testno);
100
101 rc = wcsncpy_s(NULL, LEN, str2, LEN);
102 if (rc != ESNULLP) {
103 printf("%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 rc = wcsncpy_s(str1, 0, str2, LEN);
112 if (rc != ESZEROL) {
113 printf("%s %u Error rc=%u \n",
114 __FUNCTION__, __LINE__, rc );
115 }
116
117 /*--------------------------------------------------*/
118 /* 3 Test for too large destination size */
119 printf("Test #%d:\n", ++testno);
120
121 rc = wcsncpy_s(str1, (RSIZE_MAX_STR+1), str2, LEN);
122 if (rc != ESLEMAX) {
123 printf("%s %u Error rc=%u \n",
124 __FUNCTION__, __LINE__, rc );
125 }
126
127 /*--------------------------------------------------*/
128 /* 4 Test for NULL source check */
129 printf("Test #%d:\n", ++testno);
130
131 wmemcpy_s(str1, LEN, L"aaaaa", 5);
132
133 rc = wcsncpy_s(str1, 5, NULL, LEN);
134 if (rc != ESNULLP) {
135 printf("%s %u Error rc=%u \n",
136 __FUNCTION__, __LINE__, rc );
137 }
138
139 #ifdef SAFE_LIB_STR_NULL_SLACK
140 for (i=0; i<5; i++) {
141 if (str1[i] != L'\0') {
142 printf("%s %u Error rc=%u \n",
143 __FUNCTION__, __LINE__, rc );
144 }
145 }
146 #else
147 if (str1[0] != L'\0') {
148 printf("%s %u Error rc=%u \n",
149 __FUNCTION__, __LINE__, rc );
150 }
151 #endif
152
153 /*--------------------------------------------------*/
154 /* 5 Test for zero length source */
155 printf("Test #%d:\n", ++testno);
156
157 rc = wcsncpy_s(str1, LEN, str2, 0);
158 if (rc != ESZEROL) {
159 printf("%s %u Error rc=%u \n",
160 __FUNCTION__, __LINE__, rc );
161 }
162 #ifdef SAFE_LIB_STR_NULL_SLACK
163 for (i=0; i<LEN; i++) {
164 if (str1[i] != L'\0') {
165 printf("%s %u Error rc=%u \n",
166 __FUNCTION__, __LINE__, rc );
167 }
168 }
169 #else
170 if (str1[0] != L'\0') {
171 printf("%s %u Error rc=%u \n",
172 __FUNCTION__, __LINE__, rc );
173 }
174 #endif
175
176 /*--------------------------------------------------*/
177 /* 6 Test for too large source size */
178 printf("Test #%d:\n", ++testno);
179
180 rc = wcsncpy_s(str1, LEN, str2, (RSIZE_MAX_STR+1));
181 if (rc != ESLEMAX) {
182 printf("%s %u Error rc=%u \n",
183 __FUNCTION__, __LINE__, rc );
184 }
185 #ifdef SAFE_LIB_STR_NULL_SLACK
186 for (i=0; i<LEN; i++) {
187 if (str1[i] != L'\0') {
188 printf("%s %u Error rc=%u \n",
189 __FUNCTION__, __LINE__, rc );
190 }
191 }
192 #else
193 if (str1[0] != L'\0') {
194 printf("%s %u Error rc=%u \n",
195 __FUNCTION__, __LINE__, rc );
196 }
197 #endif
198
199 /*--------------------------------------------------*/
200 /* 7 Test for Src is same as dest, overlapping buffers not allowed */
201 printf("Test #%d:\n", ++testno);
202
203 wmemcpy_s(str1, LEN, L"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 42);
204
205 rc = wcsncpy_s(str1, 5, str1, LEN);
206 if (rc != ESOVRLP) {
207 printf("%s %u Error rc=%u \n",
208 __FUNCTION__, __LINE__, rc );
209 }
210 #ifdef SAFE_LIB_STR_NULL_SLACK
211 for (i=0; i<5; i++) {
212 if (str1[i] != L'\0') {
213 printf("%s %u Error rc=%u \n",
214 __FUNCTION__, __LINE__, rc );
215 }
216 }
217 #else
218 if (str1[0] != L'\0') {
219 printf("%s %u Error rc=%u \n",
220 __FUNCTION__, __LINE__, rc );
221 }
222 #endif
223
224 /*--------------------------------------------------*/
225 /* 8 Test overlapping buffers fails */
226 printf("Test #%d:\n", ++testno);
227
228 wmemcpy_s(str1, LEN, L"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 42);
229
230 rc = wcsncpy_s(str1, LEN, &str1[5], 30);
231 if (rc != ESOVRLP) {
232 printf("%s %u Error rc=%u \n",
233 __FUNCTION__, __LINE__, rc );
234 }
235 #ifdef SAFE_LIB_STR_NULL_SLACK
236 for (i=0; i<5; i++) {
237 if (str1[i] != L'\0') {
238 printf("%s %u Error rc=%u \n",
239 __FUNCTION__, __LINE__, rc );
240 }
241 }
242 #else
243 if (str1[0] != L'\0') {
244 printf("%s %u Error rc=%u \n",
245 __FUNCTION__, __LINE__, rc );
246 }
247 #endif
248
249 /*--------------------------------------------------*/
250 /*--------------------------------------------------*/
251 /* 9 Test copy only slen characters from src into dest */
252 printf("Test #%d:\n", ++testno);
253
254 wmemcpy_s(str2, LEN, L"keep it simple--keep it simple--keep it simple", 47);
255 wmemset_s(str1, L'\0', 20);
256
257 rc = wcsncpy_s(str1, LEN, str2, 10);
258 if (rc != EOK) {
259 printf("%s %u Error rc=%u \n",
260 __FUNCTION__, __LINE__, rc );
261 }
262
263 if (str1[9] == L'\0' && str2[10] != L'\0') {
264 printf("%s %u Error rc=%u \n",
265 __FUNCTION__, __LINE__, rc );
266 }
267
268 rc = memcmp_s(str1, LEN, str2, (10)*sizeof(wchar_t), &ind );
269 if (ind != 0) {
270 printf("%s %u Error -%ls- <> -%ls-\n",
271 __FUNCTION__, __LINE__, str1, str2);
272 }
273
274
275 /*--------------------------------------------------*/
276 /* 10 Test for copy src > dest, and copy ends after copy NULL char (slen > char string) */
277 printf("Test #%d:\n", ++testno);
278
279 wmemset_s(str1, L'\0', 20);
280 wmemcpy_s(str2, LEN, L"keep it simple--keep it simple--keep it simple", 47);
281 sz = wcsnlen_s(str2, LEN);
282
283 rc = wcsncpy_s(str1, LEN, str2, LEN);
284 if (rc != EOK) {
285 printf("%s %u Error rc=%u \n",
286 __FUNCTION__, __LINE__, rc );
287 }
288
289 if (str1[sz] != L'\0') {
290 printf("%s %u Error rc=%u \n",
291 __FUNCTION__, __LINE__, rc );
292 }
293
294 rc = memcmp_s(str1, LEN, str2, (sz+1)*sizeof(wchar_t), &ind );
295 if (ind != 0) {
296 printf("%s %u Error -%ls- <> -%ls-\n",
297 __FUNCTION__, __LINE__, str1, str2);
298 }
299
300 /*--------------------------------------------------*/
301 /*--------------------------------------------------*/
302 /* 11 Test for copy src > dest, but dmax too small to hold src string */
303 printf("Test #%d:\n", ++testno);
304
305 wmemset_s(str1, L'\0', 20);
306 wmemcpy_s(str2, LEN, L"keep it simple--keep it simple--keep it simple", 47);
307 sz = wcsnlen_s(str2, LEN);
308
309 rc = wcsncpy_s(str1, (sz-5), str2, LEN);
310 if (rc != ESNOSPC) {
311 printf("%s %u Error rc=%u \n",
312 __FUNCTION__, __LINE__, rc );
313 }
314 #ifdef SAFE_LIB_STR_NULL_SLACK
315 for (i=0; i<5; i++) {
316 if (str1[i] != L'\0') {
317 printf("%s %u Error rc=%u \n",
318 __FUNCTION__, __LINE__, rc );
319 }
320 }
321 #else
322 if (str1[0] != L'\0') {
323 printf("%s %u Error rc=%u \n",
324 __FUNCTION__, __LINE__, rc );
325 }
326 #endif
327
328 /*--------------------------------------------------*/
329 /* 12 Test overlapping buffers fails (dest > src ) */
330 printf("Test #%d:\n", ++testno);
331
332 wmemcpy_s(str1, LEN, L"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 42);
333
334 rc = wcsncpy_s(&str1[5], LEN, str1, 30);
335 if (rc != ESOVRLP) {
336 printf("%s %u Error rc=%u \n",
337 __FUNCTION__, __LINE__, rc );
338 }
339 #ifdef SAFE_LIB_STR_NULL_SLACK
340 for (i=0; i<5; i++) {
341 if (str1[i] != L'\0') {
342 printf("%s %u Error rc=%u \n",
343 __FUNCTION__, __LINE__, rc );
344 }
345 }
346 #else
347 if (str1[5] != L'\0') {
348 printf("%s %u Error rc=%u \n",
349 __FUNCTION__, __LINE__, rc );
350 }
351 #endif
352
353 /*--------------------------------------------------*/
354 /*--------------------------------------------------*/
355 /* 13 Test copy only slen characters from src into dest */
356 printf("Test #%d:\n", ++testno);
357
358 wmemcpy_s(str1, LEN, L"keep it simple--keep it simple--keep it simple", 47);
359 wmemset_s(str2, L'\0', 20);
360
361 rc = wcsncpy_s(str2, LEN, str1, 17);
362 if (rc != EOK) {
363 printf("%s %u Error rc=%u \n",
364 __FUNCTION__, __LINE__, rc );
365 }
366
367 if (str2[16] == L'\0' && str2[17] != L'\0') {
368 printf("%s %u Error rc=%u \n",
369 __FUNCTION__, __LINE__, rc );
370 }
371
372 rc = memcmp_s(str1, LEN, str2, (17)*sizeof(wchar_t), &ind );
373 if (ind != 0) {
374 printf("%s %u Error -%ls- <> -%ls-\n",
375 __FUNCTION__, __LINE__, str1, str2);
376 }
377
378
379 /*--------------------------------------------------*/
380 /* 14 Test for copy src < dest, and copy ends after copy NULL char (slen > char string) */
381 printf("Test #%d:\n", ++testno);
382
383 wmemset_s(str2, L'\0', 20);
384 wmemcpy_s(str1, LEN, L"keep it simple--keep it simple--keep it simple", 47);
385 sz = wcsnlen_s(str1, LEN);
386
387 rc = wcsncpy_s(str2, LEN, str1, LEN);
388 if (rc != EOK) {
389 printf("%s %u Error rc=%u \n",
390 __FUNCTION__, __LINE__, rc );
391 }
392
393 if (str1[sz] != L'\0') {
394 printf("%s %u Error rc=%u \n",
395 __FUNCTION__, __LINE__, rc );
396 }
397
398 rc = memcmp_s(str2, LEN, str1, (sz+1)*sizeof(wchar_t), &ind );
399 if (ind != 0) {
400 printf("%s %u Error -%ls- <> -%ls-\n",
401 __FUNCTION__, __LINE__, str2, str1);
402 }
403
404 /*--------------------------------------------------*/
405 /*--------------------------------------------------*/
406 /* 15: Test for copy src < dest, but dmax too small to hold src string */
407 printf("Test #%d:\n", ++testno);
408
409 wmemset_s(str2, L'\0', 20);
410 wmemcpy_s(str1, LEN, L"keep it simple--keep it simple--keep it simple", 47);
411 sz = wcsnlen_s(str1, LEN);
412
413 rc = wcsncpy_s(str2, (sz-5), str1, LEN);
414 if (rc != ESNOSPC) {
415 printf("%s %u Error rc=%u \n",
416 __FUNCTION__, __LINE__, rc );
417 }
418 #ifdef SAFE_LIB_STR_NULL_SLACK
419 for (i=0; i<5; i++) {
420 if (str1[i] != L'\0') {
421 printf("%s %u Error rc=%u \n",
422 __FUNCTION__, __LINE__, rc );
423 }
424 }
425 #else
426 if (str2[0] != L'\0') {
427 printf("%s %u Error rc=%u \n",
428 __FUNCTION__, __LINE__, rc );
429 }
430 #endif
431
432
433 /*--------------------------------------------------*/
434 /* 16: Test for accurate String copy over existing string */
435 printf("Test #%d:\n", ++testno);
436
437 wmemset_s(str1, L'x', 20);
438 wmemcpy_s(str2, LEN, L"keep it simple", 15);
439 sz_orig = wcsnlen_s(str2, LEN);
440
441 rc = wcsncpy_s(str1, LEN, str2, LEN);
442 if (rc != EOK) {
443 printf("%s %u Error rc=%u \n",
444 __FUNCTION__, __LINE__, rc );
445 }
446
447 sz = wcsnlen_s(str1, LEN);
448 if (sz != sz_orig && sz == 14) {
449 printf("%s %u (sz=%lu <> 5) Error rc=%u \n",
450 __FUNCTION__, __LINE__, sz, rc );
451 }
452 rc = memcmp_s(str1, LEN, str2, (sz)*sizeof(wchar_t), &ind );
453 if (ind != 0) {
454 printf("%s %u -%ls- <> -%ls- (size=%lu) Error rc=%u \n",
455 __FUNCTION__, __LINE__, str1, str2, sz, rc );
456 }
457
458
459
460 /*--------------------------------------------------*/
461 /* 17: Test for just enough space in destination */
462 printf("Test #%d:\n", ++testno);
463
464
465 wmemcpy_s(str1, LEN, L"it", 3);
466 wmemcpy_s(str2, LEN, L"qqweqeqeqeq", 12);
467
468 rc = wcsncpy_s(str2, 3, str1, LEN);
469 if (rc != EOK) {
470 printf("%s %u Error rc=%u \n",
471 __FUNCTION__, __LINE__, rc );
472 }
473
474 rc = memcmp_s(str1, LEN, str2, (3)*sizeof(wchar_t), &ind );
475 if (ind != 0) {
476 printf("%s %u -%ls- Error rc=%u \n",
477 __FUNCTION__, __LINE__, str1, rc );
478 }
479
480
481
482 /*--------------------------------------------------*/
483 /* 18: Test for not enough space in destination for final NULL */
484 printf("Test #%d:\n", ++testno);
485
486 wmemcpy_s(str1, LEN, L"it", 3);
487 wmemcpy_s(str2, LEN, L"qqweqeqeqeq", 12);
488
489 sz = wcsnlen_s(str2, LEN);
490
491 rc = wcsncpy_s(str2, 2, str1, LEN);
492 if (rc != ESNOSPC) {
493 printf("%s %u Error rc=%u \n",
494 __FUNCTION__, __LINE__, rc );
495 }
496
497 if (*str2 != L'\0') {
498 printf("%s %u -%ls- Error rc=%u \n",
499 __FUNCTION__, __LINE__, str1, rc );
500 }
501 /*--------------------------------------------------*/
502
503 return (0);
504 }
505