Debian Codegraph @

ext/safestringlib/unittests/test_wcsncat_s.c

raw
1 /*------------------------------------------------------------------
2 * test_wcsncat_s.c
3 *
4 * August 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 third parameter, source char string, being sent in as NULL
44 * TC 3: Test for last parameter, source length (slen), being larger than the maximum allowed size
45 * TC 4: Test for second parameter, dest maximum length (dmax), being sent in as zero
46 * TC 5: Test for second parameter, dest maximum length (dmax), being larger than the maximum allowed size
47 *
48 * TEST WHEN DEST BUFFER < SOURCE BUFFER
49 * TC 6: Test for searching end of dest buffer runs into the src buffer (buffers overlap)
50 * TC 7: Test for dest buffer unterminated in dmax characters
51 * TC 8: Test for overlap of dest buffer into src buffer after copying from source begins
52 * TC 9: Test for proper concatenation of only slen characters from source string
53 * TC10: Test for proper concatenation of two strings
54 *
55 * TEST NON-STANDARD FAILURE CONDITIONS FOR NULL DEST STRINGS
56 * TC 11: Test null string destination and size too short
57 * TC 12: Test null string destination and dmax size is one character too short
58 * TC 13: Test for proper concatenation into a NULL string of appropriate buffer size
59 * TC 14: Test concatenation of two NULL strings
60 * TC 15: Test concatenation of NULL string onto end of existing string
61 *
62 * TEST WHEN DEST BUFFER >= SOURCE BUFFER
63 * TC16: Test concatenation of two strings - proper result
64 * TC17: Test for dest buffer unterminated in dmax characters
65 * TC18: Test for not enough space in dest buffer to fit src buffer characters
66 * TC19: Test src buffer runs into beginning of dest buffer (src buffer is unterminated or been destroyed by copy)
67 * TC20: Test concatenation with just enough space in the destination buffer
68 * TC21: Test for proper concatenation of only slen characters from source string
69 * TC22: Test concatenation with not enough room for final NULL in destination
70 *
71 *
72 *------------------------------------------------------------------
73 */
74
75 #include "test_private.h"
76 #include "safe_str_lib.h"
77
78 #define LEN ( 128 )
79
80 static wchar_t str1[LEN];
81 static wchar_t str2[LEN];
82
83 extern rsize_t wcsnlen_s (const wchar_t *dest, rsize_t dmax);
84 extern errno_t wcscpy_s(wchar_t* dest, rsize_t dmax, const wchar_t* src);
85 extern errno_t wcsncat_s (wchar_t *dest, rsize_t dmax, const wchar_t *src, rsize_t slen);
86
87 int test_wcsncat_s (void)
88 {
89 errno_t rc;
90 int32_t ind;
91 int32_t len1;
92 int32_t len2;
93 int32_t len3;
94 unsigned int testno = 0;
95
96
97 printf("\nTesting wcsncat_s:\n");
98 /*--------------------------------------------------*/
99 /* 1 Test for NULL destination check */
100 printf("Test #%d:\n", ++testno);
101
102 rc = wcsncat_s(NULL, LEN, str2, LEN);
103 if (rc != ESNULLP) {
104 printf("%s %u Error rc=%u \n",
105 __FUNCTION__, __LINE__, rc);
106 }
107
108 /*--------------------------------------------------*/
109 /* 2 Test for NULL source check */
110 printf("Test #%d:\n", ++testno);
111
112 rc = wcsncat_s(str1, LEN, NULL, LEN);
113 if (rc != ESNULLP) {
114 printf("%s %u Error rc=%u \n",
115 __FUNCTION__, __LINE__, rc);
116 }
117
118 /*--------------------------------------------------*/
119 /* 3 Test Exceed Maximum possible size of source */
120 printf("Test #%d:\n", ++testno);
121
122 rc = wcsncat_s(str1, LEN, str2, (RSIZE_MAX_STR+1));
123 if (rc != ESLEMAX) {
124 printf("%s %u Error rc=%u \n",
125 __FUNCTION__, __LINE__, rc);
126 }
127 /*--------------------------------------------------*/
128 /* 4 Test zero maximum length of destination */
129 printf("Test #%d:\n", ++testno);
130
131 rc = wcsncat_s(str1, 0, str2, LEN);
132 if (rc != ESZEROL) {
133 printf("%s %u Error rc=%u \n",
134 __FUNCTION__, __LINE__, rc);
135 }
136
137 /*--------------------------------------------------*/
138 /* 5 Test Exceed Maximum size of destination */
139 printf("Test #%d:\n", ++testno);
140
141 rc = wcsncat_s(str1, (RSIZE_MAX_STR+1), str2, LEN);
142 if (rc != ESLEMAX) {
143 printf("%s %u Error rc=%u \n",
144 __FUNCTION__, __LINE__, rc);
145 }
146
147
148 /*--------------------------------------------------*/
149 /*--------------------------------------------------*/
150 /* 6 Test destination buffer overlaps into the src buffer */
151 printf("Test #%d:\n", ++testno);
152
153 wcscpy_s(str1, LEN, L"aaaaaaaaaa keep it simple");
154
155 rc = wcsncat_s(str1, LEN, &str1[11], LEN);
156 if (rc != ESOVRLP) {
157 printf("%s %u Error rc=%u \n",
158 __FUNCTION__, __LINE__, rc);
159 }
160
161 if (str1[0] != '\0') {
162 printf("%s %u Expected null \n",
163 __FUNCTION__, __LINE__);
164 }
165
166 /*--------------------------------------------------*/
167 /* 7 Test length of destination appears unterminated */
168 printf("Test #%d:\n", ++testno);
169
170 wcscpy_s(str1, LEN, L"keep it simple");
171
172 rc = wcsncat_s(str1, 2, &str1[20], LEN);
173 if (rc != ESUNTERM) {
174 printf("%s %u Error rc=%u \n",
175 __FUNCTION__, __LINE__, rc);
176 }
177
178 if (str1[0] != '\0') {
179 printf("%s %u Expected null \n",
180 __FUNCTION__, __LINE__);
181 }
182
183 /*--------------------------------------------------*/
184 /* 8 Test overlap of destination buffer into source after copying begins */
185 printf("Test #%d:\n", ++testno);
186
187 wcscpy_s(str1, LEN, L"keep it simple");
188 wcscpy_s(&str1[20], LEN, L"aaaaaaaaaaaaaaaaaaaa");
189
190 rc = wcsncat_s(str1, 30, &str1[20], LEN);
191 if (rc != ESOVRLP) {
192 printf("%s %u Error rc=%u \n",
193 __FUNCTION__, __LINE__, rc);
194 }
195
196 if (str1[0] != '\0') {
197 printf("%s %u Expected null \n",
198 __FUNCTION__, __LINE__);
199 }
200
201 /*--------------------------------------------------*/
202 /* 9 Test proper concatenated of at most slen characters */
203 printf("Test #%d:\n", ++testno);
204
205 wcscpy_s(&str1[0], LEN, L"aaaaaaaaaa");
206 wcscpy_s(&str2[0], LEN, L"keep it simple");
207
208 len1 = wcsnlen_s(str1, LEN);
209 len2 = 10;
210
211 rc = wcsncat_s(str1, 50, str2, len2);
212 if (rc != EOK) {
213 printf("%s %u Error rc=%u \n",
214 __FUNCTION__, __LINE__, rc);
215 }
216
217 len3 = wcsnlen_s(str1, LEN);
218 if (len3 != (len1+len2)) {
219 printf("%s %u lengths wrong: %u %u %u \n",
220 __FUNCTION__, __LINE__, len1, len2, len3);
221 }
222
223 rc = memcmp_s(str1, LEN, L"aaaaaaaaaakeep it si", (len3+1)*sizeof(wchar_t), &ind );
224 if (ind != 0) {
225 printf("%s %u Error -%ls- \n",
226 __FUNCTION__, __LINE__, str1);
227 }
228
229 /*--------------------------------------------------*/
230 /* 10 Test Verify proper size of concatenated strings & value of concatenated string */
231 printf("Test #%d:\n", ++testno);
232
233 wcscpy_s(&str1[0], LEN, L"aaaaaaaaaa");
234 wcscpy_s(&str2[0], LEN, L"keep it simple");
235
236 len1 = wcsnlen_s(str1, LEN);
237 len2 = wcsnlen_s(str2, LEN);
238
239 rc = wcsncat_s(str1, 50, str2, LEN);
240 if (rc != EOK) {
241 printf("%s %u Error rc=%u \n",
242 __FUNCTION__, __LINE__, rc);
243 }
244
245 len3 = wcsnlen_s(str1, LEN);
246 if (len3 != (len1+len2)) {
247 printf("%s %u lengths wrong: %u %u %u \n",
248 __FUNCTION__, __LINE__, len1, len2, len3);
249 }
250
251 rc = memcmp_s(str1, LEN, L"aaaaaaaaaakeep it simple", (len3+1)*sizeof(wchar_t), &ind );
252 if (ind != 0) {
253 printf("%s %u Error -%ls- \n",
254 __FUNCTION__, __LINE__, str1);
255 }
256
257 /*--------------------------------------------------*/
258 /*--------------------------------------------------*/
259 /* 11 Test NULL string is destination - too short */
260 printf("Test #%d:\n", ++testno);
261
262 str1[0] = '\0';
263 wcscpy_s(str2, LEN, L"keep it simple");
264
265 rc = wcsncat_s(str1, 1, str2, LEN);
266 if (rc != ESNOSPC) {
267 printf("%s %u Error rc=%u \n",
268 __FUNCTION__, __LINE__, rc);
269 }
270
271 if (str1[0] != '\0') {
272 printf("%s %u Expected null \n",
273 __FUNCTION__, __LINE__);
274 }
275
276 /*--------------------------------------------------*/
277 /* 12 Test NULL string is destination - too short */
278 printf("Test #%d:\n", ++testno);
279
280 str1[0] = '\0';
281 wcscpy_s(str2, LEN, L"keep it simple");
282
283 rc = wcsncat_s(str1, 14, str2, LEN);
284 if (rc != ESNOSPC) {
285 printf("%s %u Error rc=%u \n",
286 __FUNCTION__, __LINE__, rc);
287 }
288
289 if (str1[0] != '\0') {
290 printf("%s %u Expected null \n",
291 __FUNCTION__, __LINE__);
292 }
293
294 /*--------------------------------------------------*/
295 /* 13 Test NULL string is destination - proper result */
296 printf("Test #%d:\n", ++testno);
297
298 str1[0] = '\0';
299 wcscpy_s(str2, LEN, L"keep it simple");
300
301 rc = wcsncat_s(str1, 20, str2, LEN);
302 if (rc != EOK) {
303 printf("%s %u Error rc=%u \n",
304 __FUNCTION__, __LINE__, rc);
305 }
306
307 rc = memcmp_s(str1, LEN, str2, wcsnlen_s(str2, LEN)*sizeof(wchar_t), &ind );
308 if (ind != 0) {
309 printf("%s %u Error -%ls- \n",
310 __FUNCTION__, __LINE__, str1);
311 }
312
313 /*--------------------------------------------------*/
314 /*--------------------------------------------------*/
315 /* 14 Test concat two NULL strings */
316 printf("Test #%d:\n", ++testno);
317
318 str1[0] = '\0';
319 str2[0] = '\0';
320
321 rc = wcsncat_s(str1, LEN, str2, LEN);
322 if (rc != EOK) {
323 printf("%s %u Error rc=%u \n",
324 __FUNCTION__, __LINE__, rc);
325 }
326
327 if (str1[0] != '\0') {
328 printf("%s %u Expected null \n",
329 __FUNCTION__, __LINE__);
330 }
331
332 /*--------------------------------------------------*/
333 /* 15 Test concat NULL string to existing string - proper result */
334 printf("Test #%d:\n", ++testno);
335
336 str1[0] = '\0';
337 wcscpy_s(str2, LEN, L"keep it simple");
338
339 rc = wcsncat_s(str2, LEN, str1, LEN);
340 if (rc != EOK) {
341 printf("%s %u Error rc=%u \n",
342 __FUNCTION__, __LINE__, rc);
343 }
344
345 rc = memcmp_s(str2, LEN, L"keep it simple", (15)*sizeof(wchar_t), &ind );
346 if (ind != 0) {
347 printf("%s %u Error -%ls- \n",
348 __FUNCTION__, __LINE__, str1);
349 }
350
351 /*--------------------------------------------------*/
352 /* 16 Test concat two strings - proper result */
353 printf("Test #%d:\n", ++testno);
354
355 wcscpy_s(str2, LEN, L"qqweqq");
356 wcscpy_s(str1, LEN, L"keep it simple");
357
358 rc = wcsncat_s(str2, LEN, str1, LEN);
359 if (rc != EOK) {
360 printf("%s %u Error rc=%u \n",
361 __FUNCTION__, __LINE__, rc);
362 }
363
364 rc = memcmp_s(str2, LEN, L"qqweqqkeep it simple", (20)*sizeof(wchar_t), &ind );
365 if (ind != 0) {
366 printf("%s %u Error -%ls- \n",
367 __FUNCTION__, __LINE__, str1);
368 }
369
370 /*--------------------------------------------------*/
371 /* 17 Test concat two strings - dest appears unterminated */
372 printf("Test #%d:\n", ++testno);
373
374 wcscpy_s(str1, LEN, L"1234");
375 wcscpy_s(str2, LEN, L"keep it simple");
376
377 rc = wcsncat_s(str2, 12, str1, LEN);
378 if (rc != ESUNTERM) {
379 printf("%s %u Error rc=%u \n",
380 __FUNCTION__, __LINE__, rc);
381 }
382
383 if (str2[0] != '\0') {
384 printf("%s %u Expected null \n",
385 __FUNCTION__, __LINE__);
386 }
387
388 /*--------------------------------------------------*/
389 /* 18 Test concat two strings - not enough space in dest */
390 printf("Test #%d:\n", ++testno);
391
392 wcscpy_s(str1, LEN, L"1234");
393 wcscpy_s(str2, LEN, L"keep it simple");
394
395 rc = wcsncat_s(str2, 16, str1, LEN);
396 if (rc != ESNOSPC) {
397 printf("%s %u Error rc=%u \n",
398 __FUNCTION__, __LINE__, rc);
399 }
400
401 if (str2[0] != '\0') {
402 printf("%s %u Expected null \n",
403 __FUNCTION__, __LINE__);
404 }
405
406 /*--------------------------------------------------*/
407 /* 19 Test overlapping destination string into the beginning of the src */
408 printf("Test #%d:\n", ++testno);
409
410 wcscpy_s(str1, LEN, L"12345678901234567890");
411
412 rc = wcsncat_s(&str1[7], LEN, str1, LEN);
413 if (rc != ESOVRLP) {
414 printf("%s %u Error rc=%u \n",
415 __FUNCTION__, __LINE__, rc);
416 }
417
418 if (str1[7] != '\0') {
419 printf("%s %u Expected null \n",
420 __FUNCTION__, __LINE__);
421 }
422
423 /*--------------------------------------------------*/
424 /* 20 Test concat two strings - just enough space in the destination */
425 printf("Test #%d:\n", ++testno);
426
427 wcscpy_s(str1, LEN, L"1234");
428 wcscpy_s(str2, LEN, L"keep it simple");
429
430 rc = wcsncat_s(str2, 19, str1, LEN);
431 if (rc != EOK) {
432 printf("%s %u Error rc=%u \n",
433 __FUNCTION__, __LINE__, rc);
434 }
435
436 rc = memcmp_s(str2, LEN, L"keep it simple1234", (19)*sizeof(wchar_t), &ind );
437 if (ind != 0) {
438 printf("%s %u Error -%ls- \n",
439 __FUNCTION__, __LINE__, str1);
440 }
441
442 /*--------------------------------------------------*/
443 /* 21 Test copy at most slen characters from src string */
444 printf("Test #%d:\n", ++testno);
445
446 wcscpy_s(str2, LEN, L"123456789");
447 wcscpy_s(str1, LEN, L"keep it simple");
448
449 rc = wcsncat_s(str2, LEN, str1, 7);
450 if (rc != EOK) {
451 printf("%s %u Error rc=%u \n",
452 __FUNCTION__, __LINE__, rc);
453 }
454
455 rc = memcmp_s(str2, LEN, L"123456789keep it", (17)*sizeof(wchar_t), &ind );
456 if (ind != 0) {
457 printf("%s %u Error -%ls- \n",
458 __FUNCTION__, __LINE__, str1);
459 }
460
461 /*--------------------------------------------------*/
462 /* 22 Test normal string concat - no enough room for NULL */
463 printf("Test #%d:\n", ++testno);
464
465 wcscpy_s(str2, LEN, L"1234");
466 wcscpy_s(str1, LEN, L"56789");
467
468 rc = wcsncat_s(str2, 9, str1, LEN);
469 if (rc != ESNOSPC) {
470 printf("%s %u Error rc=%u \n",
471 __FUNCTION__, __LINE__, rc);
472 }
473
474 if (str2[0] != L'\0') {
475 printf("%s %u Expected null \n",
476 __FUNCTION__, __LINE__);
477 }
478
479
480 /*--------------------------------------------------*/
481
482 return (0);
483 }
484
485