Debian Codegraph @

ext/safestringlib/unittests/test_wcscat_s.c

raw
1 /*------------------------------------------------------------------
2 * test_wcscat_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
32 #include "test_private.h"
33 #include "safe_str_lib.h"
34
35 #define LEN ( 128 )
36
37 static wchar_t str1[LEN];
38 static wchar_t str2[LEN];
39
40 extern rsize_t wcsnlen_s (const wchar_t *dest, rsize_t dmax);
41 extern errno_t wcscpy_s(wchar_t* dest, rsize_t dmax, const wchar_t* src);
42 extern errno_t wcscat_s(wchar_t* dest, rsize_t dmax, const wchar_t* src);
43
44 int test_wcscat_s (void)
45 {
46 errno_t rc;
47 int32_t ind;
48 int32_t len1;
49 int32_t len2;
50 int32_t len3;
51 unsigned int testno = 0;
52
53
54 printf("\nTesting wcscat_s:\n");
55 /*--------------------------------------------------*/
56 /* 1 Test for NULL destination check */
57 printf("Test #%d:\n", ++testno);
58
59 rc = wcscat_s(NULL, LEN, str2);
60 if (rc != ESNULLP) {
61 printf("%s %u Error rc=%u \n",
62 __FUNCTION__, __LINE__, rc);
63 }
64
65 /*--------------------------------------------------*/
66 /* 2 Test for NULL source check */
67 printf("Test #%d:\n", ++testno);
68
69 rc = wcscat_s(str1, LEN, NULL);
70 if (rc != ESNULLP) {
71 printf("%s %u Error rc=%u \n",
72 __FUNCTION__, __LINE__, rc);
73 }
74
75 /*--------------------------------------------------*/
76 /* 3 Test zero maximum length of destination */
77 printf("Test #%d:\n", ++testno);
78
79 rc = wcscat_s(str1, 0, str2);
80 if (rc != ESZEROL) {
81 printf("%s %u Error rc=%u \n",
82 __FUNCTION__, __LINE__, rc);
83 }
84
85 /*--------------------------------------------------*/
86 /* 4 Test Exceed Maximum size of destination */
87 printf("Test #%d:\n", ++testno);
88
89 rc = wcscat_s(str1, (RSIZE_MAX_STR+1), str2);
90 if (rc != ESLEMAX) {
91 printf("%s %u Error rc=%u \n",
92 __FUNCTION__, __LINE__, rc);
93 }
94
95 /*--------------------------------------------------*/
96 /*--------------------------------------------------*/
97 /* 5 Test overlapping destination string into the beginning of the src */
98 printf("Test #%d:\n", ++testno);
99
100 wcscpy_s(str1, LEN, L"12345678901234567890");
101
102 rc = wcscat_s(str1, 8, &str1[7]);
103 if (rc != ESOVRLP) {
104 printf("%s %u Error rc=%u \n",
105 __FUNCTION__, __LINE__, rc);
106 }
107
108 if (str1[0] != L'\0') {
109 printf("%s %u Expected null \n",
110 __FUNCTION__, __LINE__);
111 }
112
113 /*--------------------------------------------------*/
114 /* 6 Test too small size - destination appears unterminated */
115 printf("Test #%d:\n", ++testno);
116
117 wcscpy_s(str1, LEN, L"aaaaaaaaaa");
118 wcscpy_s(str2, LEN, L"keep it simple");
119
120 rc = wcscat_s(str1, 1, str2);
121 if (rc != ESUNTERM) {
122 printf("%s %u Error rc=%u \n",
123 __FUNCTION__, __LINE__, rc);
124 }
125
126 if (str1[0] != L'\0') {
127 printf("%s %u Expected null \n",
128 __FUNCTION__, __LINE__);
129 }
130
131 /*--------------------------------------------------*/
132 /* 7 Test length of destination appears unterminated */
133 printf("Test #%d:\n", ++testno);
134
135 wcscpy_s(str1, LEN, L"aaaaaaaaaa");
136 wcscpy_s(str2, LEN, L"keep it simple");
137
138 rc = wcscat_s(str1, 10, str2);
139 if (rc != ESUNTERM) {
140 printf("%s %u Error rc=%u \n",
141 __FUNCTION__, __LINE__, rc);
142 }
143
144 if (str1[0] != L'\0') {
145 printf("%s %u Expected null \n",
146 __FUNCTION__, __LINE__);
147 }
148
149 /*--------------------------------------------------*/
150 /* 8 Test overlap of destination buffer into source after copying begins */
151 printf("Test #%d:\n", ++testno);
152
153 wcscpy_s(str1, LEN, L"keep it simple");
154 wcscpy_s(&str1[20], LEN, L"aaaaaaaaaaaaaaaaaaaa");
155
156 rc = wcscat_s(str1, 30, &str1[20]);
157 if (rc != ESOVRLP) {
158 printf("%s %u Error rc=%u \n",
159 __FUNCTION__, __LINE__, rc);
160 }
161
162 if (str1[0] != L'\0') {
163 printf("%s %u Expected null \n",
164 __FUNCTION__, __LINE__);
165 }
166
167 /*--------------------------------------------------*/
168 /* 9 Test Verify proper size of concatenated strings */
169 printf("Test #%d:\n", ++testno);
170
171 wcscpy_s(&str1[0], LEN, L"aaaaaaaaaa");
172 wcscpy_s(&str2[0], LEN, L"keep it simple");
173
174 len1 = wcsnlen_s(str1, LEN);
175 len2 = wcsnlen_s(str2, LEN);
176
177 rc = wcscat_s(str1, 50, str2);
178 if (rc != EOK) {
179 printf("%s %u Error rc=%u \n",
180 __FUNCTION__, __LINE__, rc);
181 }
182
183 len3 = wcsnlen_s(str1, LEN);
184 if (len3 != (len1+len2)) {
185 printf("%s %u lengths wrong: %u %u %u \n",
186 __FUNCTION__, __LINE__, len1, len2, len3);
187 }
188
189 /*--------------------------------------------------*/
190 /*--------------------------------------------------*/
191 /* 10 Test NULL string is destination - too short */
192 printf("Test #%d:\n", ++testno);
193
194 str1[0] = '\0';
195 wcscpy_s(str2, LEN, L"keep it simple");
196
197 rc = wcscat_s(str1, 1, str2);
198 if (rc != ESNOSPC) {
199 printf("%s %u Error rc=%u \n",
200 __FUNCTION__, __LINE__, rc);
201 }
202
203 if (str1[0] != L'\0') {
204 printf("%s %u Expected null \n",
205 __FUNCTION__, __LINE__);
206 }
207
208 /*--------------------------------------------------*/
209 /* 11 Test NULL string is destination - too short */
210 printf("Test #%d:\n", ++testno);
211
212 str1[0] = '\0';
213 wcscpy_s(str2, LEN, L"keep it simple");
214
215 rc = wcscat_s(str1, 11, str2);
216 if (rc != ESNOSPC) {
217 printf("%s %u Error rc=%u \n",
218 __FUNCTION__, __LINE__, rc);
219 }
220
221 if (str1[0] != L'\0') {
222 printf("%s %u Expected null \n",
223 __FUNCTION__, __LINE__);
224 }
225
226 /*--------------------------------------------------*/
227 /* 12 Test NULL string is destination - proper result */
228 printf("Test #%d:\n", ++testno);
229
230 str1[0] = '\0';
231 wcscpy_s(str2, LEN, L"keep it simple");
232
233 rc = wcscat_s(str1, 20, str2);
234 if (rc != EOK) {
235 printf("%s %u Error rc=%u \n",
236 __FUNCTION__, __LINE__, rc);
237 }
238
239 rc = memcmp_s(str1, LEN, str2, wcsnlen_s(str2, LEN)*sizeof(wchar_t), &ind );
240 if (ind != 0) {
241 printf("%s %u Error -%ls- \n",
242 __FUNCTION__, __LINE__, str1);
243 }
244
245 /*--------------------------------------------------*/
246 /*--------------------------------------------------*/
247 /* 13 Test concat two NULL strings */
248 printf("Test #%d:\n", ++testno);
249
250 str1[0] = '\0';
251 str2[0] = '\0';
252
253 rc = wcscat_s(str1, LEN, str2);
254 if (rc != EOK) {
255 printf("%s %u Error rc=%u \n",
256 __FUNCTION__, __LINE__, rc);
257 }
258
259 if (str1[0] != L'\0') {
260 printf("%s %u Expected null \n",
261 __FUNCTION__, __LINE__);
262 }
263
264 /*--------------------------------------------------*/
265 /* 14 Test concat NULL string to existing string - proper result */
266 printf("Test #%d:\n", ++testno);
267
268 str1[0] = '\0';
269 wcscpy_s(str2, LEN, L"keep it simple");
270
271 rc = wcscat_s(str2, LEN, str1);
272 if (rc != EOK) {
273 printf("%s %u Error rc=%u \n",
274 __FUNCTION__, __LINE__, rc);
275 }
276
277 rc = memcmp_s(str2, LEN, L"keep it simple", (15)*sizeof(wchar_t), &ind );
278 if (ind != 0) {
279 printf("%s %u Error -%ls- \n",
280 __FUNCTION__, __LINE__, str1);
281 }
282
283 /*--------------------------------------------------*/
284 /* 15 Test concat two strings - proper result */
285 printf("Test #%d:\n", ++testno);
286
287 wcscpy_s(str1, LEN, L"qqweqq");
288 wcscpy_s(str2, LEN, L"keep it simple");
289
290 rc = wcscat_s(str1, LEN, str2);
291 if (rc != EOK) {
292 printf("%s %u Error rc=%u \n",
293 __FUNCTION__, __LINE__, rc);
294 }
295
296 rc = memcmp_s(str1, LEN, L"qqweqqkeep it simple", (20)*sizeof(wchar_t), &ind );
297 if (ind != 0) {
298 printf("%s %u Error -%ls- \n",
299 __FUNCTION__, __LINE__, str1);
300 }
301
302 /*--------------------------------------------------*/
303 /* 16 Test concat two strings - not enough space in dest */
304 printf("Test #%d:\n", ++testno);
305
306 wcscpy_s(str1, LEN, L"1234");
307 wcscpy_s(str2, LEN, L"keep it simple");
308
309 rc = wcscat_s(str2, 16, str1);
310 if (rc != ESNOSPC) {
311 printf("%s %u Error rc=%u \n",
312 __FUNCTION__, __LINE__, rc);
313 }
314
315 /*--------------------------------------------------*/
316 /* 17 Test concat two strings - just enough space in the destination */
317 printf("Test #%d:\n", ++testno);
318
319 wcscpy_s(str1, LEN, L"1234");
320 wcscpy_s(str2, LEN, L"keep it simple");
321
322 rc = wcscat_s(str2, 19, str1);
323 if (rc != EOK) {
324 printf("%s %u Error rc=%u \n",
325 __FUNCTION__, __LINE__, rc);
326 }
327
328 rc = memcmp_s(str2, LEN, L"keep it simple1234", (19)*sizeof(wchar_t), &ind );
329 if (ind != 0) {
330 printf("%s %u Error -%ls- \n",
331 __FUNCTION__, __LINE__, str1);
332 }
333
334
335 /*--------------------------------------------------*/
336 /* 18 Test source overlaps into the dest string after copying begins */
337 printf("Test #%d:\n", ++testno);
338
339 wcscpy_s(str1, LEN, L"123456789123456789");
340 wcscpy_s(&str1[13], LEN, L"ABCDEFGHIJKLMNOP123456789");
341
342 rc = wcscat_s(&str1[13], LEN, str1);
343 if (rc != ESOVRLP) {
344 printf("%s %u Error rc=%u \n",
345 __FUNCTION__, __LINE__, rc);
346 }
347
348 if (str1[13] != L'\0') {
349 printf("%s %u Expected null \n",
350 __FUNCTION__, __LINE__);
351 }
352
353 /*--------------------------------------------------*/
354 /* 19 Test normal string concat */
355 printf("Test #%d:\n", ++testno);
356
357 wcscpy_s(str2, LEN, L"123");
358 wcscpy_s(str1, LEN, L"keep it simple");
359
360 rc = wcscat_s(str2, 31, &str1[0]);
361 if (rc != EOK) {
362 printf("%s %u Error rc=%u \n",
363 __FUNCTION__, __LINE__, rc);
364 }
365
366 rc = memcmp_s(str2, LEN, L"123keep it simple", (17)*sizeof(wchar_t), &ind );
367 if (ind != 0) {
368 printf("%s %u Error -%ls- \n",
369 __FUNCTION__, __LINE__, str1);
370 }
371
372 /*--------------------------------------------------*/
373 /* 20 Test normal string concat - just enough room */
374 printf("Test #%d:\n", ++testno);
375
376 wcscpy_s(str2, LEN, L"1234");
377 wcscpy_s(str1, LEN, L"56789");
378
379 rc = wcscat_s(str2, 10, str1);
380 if (rc != EOK) {
381 printf("%s %u Error rc=%u \n",
382 __FUNCTION__, __LINE__, rc);
383 }
384
385 rc = memcmp_s(str2, LEN, L"123456789", (9)*sizeof(wchar_t), &ind );
386
387 if (ind != 0) {
388 printf("%s %u Error -%ls- \n",
389 __FUNCTION__, __LINE__, str1);
390 }
391
392 /*--------------------------------------------------*/
393 /* 21 Test normal string concat - not enough room for NULL */
394 printf("Test #%d:\n", ++testno);
395
396 wcscpy_s(str2, LEN, L"1234");
397 wcscpy_s(str1, LEN, L"56789");
398
399 rc = wcscat_s(str2, 9, str1);
400 if (rc != ESNOSPC) {
401 printf("%s %u Error rc=%u \n",
402 __FUNCTION__, __LINE__, rc);
403 }
404
405 if (str2[0] != L'\0') {
406 printf("%s %u Expected null \n",
407 __FUNCTION__, __LINE__);
408 }
409
410
411 /*--------------------------------------------------*/
412
413 return (0);
414 }
415
416