Debian Codegraph @

ext/safestringlib/unittests/test_wmemset_s.c

raw
1 /*------------------------------------------------------------------
2 * test_wmemset_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 third parameter, dest maximum length (dmax), being sent in as zero
44 * TC 3: Test for third parameter, dest maximum length (dmax), being larger than the maximum allowed size
45 *
46 * TC 4: Test setting a single character
47 * TC 5: Test setting two characters
48 * TC 6: Test setting multiple characters (12)
49 * TC 7: Test setting multiple characters (31)
50 * TC 8: Test setting multiple characters (133)
51 *
52 *
53 *------------------------------------------------------------------
54 */
55
56
57 #include "test_private.h"
58 #include "safe_mem_lib.h"
59
60 #define LEN ( 256 )
61
62 static wchar_t mem1[LEN];
63
64 extern errno_t wmemset_s (wchar_t *dest, wchar_t value, rsize_t len);
65
66 int test_wmemset_s (void)
67 {
68 errno_t rc;
69 uint32_t len;
70 uint32_t i;
71 unsigned int testno = 0;
72 wchar_t value;
73
74 printf("\nTesting wmemset_s:\n");
75 /*--------------------------------------------------*/
76 /* 1: Test for first parameter, destination char string, being sent in as NULL */
77 printf("Test #%d:\n", ++testno);
78
79 value = 34;
80
81 rc = wmemset_s(NULL, value, LEN);
82 if (rc != ESNULLP) {
83 printf("%s %u Error rc=%u \n",
84 __FUNCTION__, __LINE__, rc);
85 }
86
87 /*--------------------------------------------------*/
88 /* 2: Test for third parameter, dest maximum length (dmax), being sent in as zero */
89 printf("Test #%d:\n", ++testno);
90 value = 34;
91
92 rc = wmemset_s(mem1, value, 0);
93 if (rc != ESZEROL) {
94 printf("%s %u Error rc=%u \n",
95 __FUNCTION__, __LINE__, rc);
96 }
97
98 /*--------------------------------------------------*/
99 /* 3: Test for second parameter, dest maximum length (dmax), being larger than the maximum allowed size */
100 printf("Test #%d:\n", ++testno);
101
102 value = 34;
103
104 rc = wmemset_s(mem1, value, (RSIZE_MAX_MEM+1));
105 if (rc != ESLEMAX) {
106 printf("%s %u Error rc=%u \n",
107 __FUNCTION__, __LINE__, rc);
108 }
109
110 /*--------------------------------------------------*/
111 /* 4: Test setting a single character */
112 printf("Test #%d:\n", ++testno);
113
114 for (i=0; i<LEN; i++) { mem1[i] = 99; }
115
116 len = 1;
117 value = 34;
118
119 rc = wmemset_s(mem1, value, len);
120 if (rc != EOK) {
121 printf("%s %u Error rc=%u \n",
122 __FUNCTION__, __LINE__, rc);
123 }
124
125 if ( mem1[0] != value ) {
126 printf("%d - m1[0]=%d \n",
127 __LINE__, mem1[0]);
128 }
129 for (i=1; i<LEN; i++) {
130 if (mem1[i] != 99) {
131 printf("%d - %d m1=%d \n",
132 __LINE__, i, mem1[i]);
133 }
134 }
135
136 /*--------------------------------------------------*/
137 /* 5: Test setting two characters */
138 printf("Test #%d:\n", ++testno);
139
140 for (i=0; i<LEN; i++) { mem1[i] = 99; }
141
142 len = 2;
143 value = 34;
144
145 rc = wmemset_s(mem1, value, len);
146 if (rc != EOK) {
147 printf("%s %u Error rc=%u \n",
148 __FUNCTION__, __LINE__, rc);
149 }
150
151 if ( mem1[0] != value || mem1[1] != value ) {
152 printf("%d - m1[0]=%d m1[1]=%d \n",
153 __LINE__, mem1[0], mem1[1]);
154 }
155 for (i=2; i<LEN; i++) {
156 if (mem1[i] != 99) {
157 printf("%d - %d m1=%d \n",
158 __LINE__, i, mem1[i]);
159 }
160 }
161
162
163 /*--------------------------------------------------*/
164 /* 6: Test setting multiple characters */
165 printf("Test #%d:\n", ++testno);
166
167 for (i=0; i<LEN; i++) { mem1[i] = 99; }
168
169 len = 12;
170 value = 34;
171
172 rc = wmemset_s(mem1, value, len);
173 if (rc != EOK) {
174 printf("%s %u Error rc=%u \n",
175 __FUNCTION__, __LINE__, rc);
176 }
177
178 for (i=0; i<len; i++) {
179 if (mem1[i] != value) {
180 printf("%d - %d m1=%d \n",
181 __LINE__, i, mem1[i]);
182 }
183 }
184 for (i=len; i<LEN; i++) {
185 if (mem1[i] != 99) {
186 printf("%d - %d m1=%d \n",
187 __LINE__, i, mem1[i]);
188 }
189 }
190
191 /*--------------------------------------------------*/
192 /* 7: Test setting multiple characters */
193 printf("Test #%d:\n", ++testno);
194
195 for (i=0; i<LEN; i++) { mem1[i] = 99; }
196
197 len = 31;
198 value = 3;
199
200 rc = wmemset_s(mem1, value, len);
201 if (rc != EOK) {
202 printf("%s %u Error rc=%u \n",
203 __FUNCTION__, __LINE__, rc);
204 }
205
206 for (i=0; i<len; i++) {
207 if (mem1[i] != value) {
208 printf("%d - %d m1=%d \n",
209 __LINE__, i, mem1[i]);
210 }
211 }
212
213 /*--------------------------------------------------*/
214 /* 8: Test setting multiple characters */
215 printf("Test #%d:\n", ++testno);
216
217 for (i=0; i<LEN; i++) { mem1[i] = 99; }
218
219 len = 133;
220 value = L'A';
221
222 rc = wmemset_s(mem1, value, len);
223 if (rc != EOK) {
224 printf("%s %u Error rc=%u \n",
225 __FUNCTION__, __LINE__, rc);
226 }
227
228 for (i=0; i<len; i++) {
229 if (mem1[i] != value) {
230 printf("%d - %d m1=%d \n",
231 __LINE__, i, mem1[i]);
232 }
233 }
234
235 /*--------------------------------------------------*/
236 /*--------------------------------------------------*/
237
238 return (0);
239 }
240