Debian Codegraph @

ext/safestringlib/unittests/test_wmemmove_s.c

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