Debian Codegraph @

ext/safestringlib/unittests/test_wmemcpy_s.c

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