Debian Codegraph @

ext/safestringlib/unittests/test_wmemcmp_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, indicator, being sent in as NULL
45 * TC 4: Test for second parameter, dest maximum length (dmax), being sent in as zero
46 * TC 5: Test for last parameter, source maximum (smax), being sent in as zero
47 * TC 6: Test for second parameter, dest maximum length (dmax), being larger than the maximum allowed size
48 * TC 7: Test for last parameter, source length (smax), being larger than the maximum allowed size
49 *
50 *
51 * TC 8: Test for single character equality comparison - proper result
52 * TC 9: Test for two character equality comparison - proper result
53 * TC10: Test for long string character equality comparison - proper result
54 * TC11: Test long string many differences - s1 < s2 at index 10
55 * TC12: Test long string many differences - s1 < s2 at index 10
56 * TC13: Test long string - single char greater than difference at index 1
57 * TC14: Test long string - single char less than difference at index 1
58 * TC15: Test long string - single char greater than difference at end of string
59 * TC16: Test long string - single char less than difference at end of string
60 *
61 *------------------------------------------------------------------
62 */
63
64
65 #include "test_private.h"
66 #include "safe_mem_lib.h"
67
68 #define LEN ( 128 )
69
70 extern errno_t wmemcmp_s (const wchar_t *dest, rsize_t dmax,
71 const wchar_t *src, rsize_t smax, int *diff);
72
73 int test_wmemcmp_s()
74 {
75 errno_t rc;
76 uint32_t len;
77 int32_t ind;
78 uint32_t i;
79
80 wchar_t mem1[LEN];
81 wchar_t mem2[LEN];
82 unsigned int testno = 0;
83
84
85 printf("\nTesting wmemcmp_s:\n");
86 /*--------------------------------------------------*/
87 /* 1: Test for first parameter, destination char string, being sent in as NULL */
88
89 printf("Test #%d:\n", ++testno);
90 rc = wmemcmp_s(NULL, LEN, mem2, LEN, &ind);
91 if (rc != ESNULLP) {
92 printf("%s %u Ind=%d Error rc=%u \n",
93 __FUNCTION__, __LINE__, ind, rc );
94 }
95
96 /*--------------------------------------------------*/
97 /* 2: Test for third parameter, source char string, being sent in as NULL */
98
99 printf("Test #%d:\n", ++testno);
100 rc = wmemcmp_s(mem1, LEN, NULL, LEN, &ind);
101 if (rc != ESNULLP) {
102 printf("%s %u Ind=%d Error rc=%u \n",
103 __FUNCTION__, __LINE__, ind, rc );
104 }
105
106 /*--------------------------------------------------*/
107 /* 3: Test for last parameter, indicator, being sent in as NULL */
108
109 printf("Test #%d:\n", ++testno);
110 rc = wmemcmp_s(mem1, LEN, mem2, LEN, NULL);
111 if (rc != ESNULLP) {
112 printf("%s %u Ind=%d Error rc=%u \n",
113 __FUNCTION__, __LINE__, ind, rc );
114 }
115
116 /*--------------------------------------------------*/
117 /* 4: Test for second parameter, dest maximum length (dmax), being sent in as zero */
118
119 printf("Test #%d:\n", ++testno);
120 rc = wmemcmp_s(mem1, 0, mem2, LEN, &ind);
121 if (rc != ESZEROL) {
122 printf("%s %u Ind=%d Error rc=%u \n",
123 __FUNCTION__, __LINE__, ind, rc );
124 }
125
126 /*--------------------------------------------------*/
127 /* 5: Test for last parameter, source maximum (smax), being sent in as zero */
128
129 printf("Test #%d:\n", ++testno);
130 rc = wmemcmp_s(mem1, LEN, mem2, 0, &ind);
131 if (rc != ESZEROL) {
132 printf("%s %u Ind=%d Error rc=%u \n",
133 __FUNCTION__, __LINE__, ind, rc );
134 }
135
136 /*--------------------------------------------------*/
137 /* 6: Test for second parameter, dest maximum length (dmax), being larger than the maximum allowed size */
138
139 printf("Test #%d:\n", ++testno);
140 rc = wmemcmp_s(mem1, RSIZE_MAX_MEM+1, mem2, LEN, &ind);
141 if (rc != ESLEMAX) {
142 printf("%s %u Ind=%d Error rc=%u \n",
143 __FUNCTION__, __LINE__, ind, rc );
144 }
145
146 /*--------------------------------------------------*/
147 /* 7: Test for last parameter, source length (smax), being larger than the maximum allowed size */
148
149 printf("Test #%d:\n", ++testno);
150 rc = wmemcmp_s(mem1, LEN, mem2, RSIZE_MAX_MEM+1, &ind);
151 if (rc != ESLEMAX) {
152 printf("%s %u Ind=%d Error rc=%u \n",
153 __FUNCTION__, __LINE__, ind, rc );
154 }
155
156 /*--------------------------------------------------*/
157 /* 8: Test for single character equality comparison */
158
159 printf("Test #%d:\n", ++testno);
160 for (i=0; i<LEN; i++) { mem1[i] = i; }
161 for (i=0; i<LEN; i++) { mem2[i] = i; }
162
163 len = 1;
164 rc = wmemcmp_s(mem1, len, mem2, len, &ind);
165 if (rc != EOK) {
166 printf("%s %u Ind=%d Error rc=%u \n",
167 __FUNCTION__, __LINE__, ind, rc );
168 }
169
170 if (ind != 0) {
171 printf("%s %u Ind=%d rc=%u \n",
172 __FUNCTION__, __LINE__, ind, rc );
173 }
174
175 ind = memcmp(mem1, mem2, len*4);
176 if (ind != 0) {
177 printf("%s %u Ind=%d rc=%u \n",
178 __FUNCTION__, __LINE__, ind, rc );
179 }
180
181 /*--------------------------------------------------*/
182 /* 9: Test for two character equality comparison */
183
184 printf("Test #%d:\n", ++testno);
185 for (i=0; i<LEN; i++) { mem1[i] = i; }
186 for (i=0; i<LEN; i++) { mem2[i] = i; }
187
188 len = 2;
189 rc = wmemcmp_s(mem1, len, mem2, len, &ind);
190 if (rc != EOK) {
191 printf("%s %u Ind=%d Error rc=%u \n",
192 __FUNCTION__, __LINE__, ind, rc );
193 }
194
195 if (ind != 0) {
196 printf("%s %u Ind=%d rc=%u \n",
197 __FUNCTION__, __LINE__, ind, rc );
198 }
199
200 ind = memcmp(mem1, mem2, len*4);
201 if (ind != 0) {
202 printf("%s %u Ind=%d rc=%u \n",
203 __FUNCTION__, __LINE__, ind, rc );
204 }
205
206 /*--------------------------------------------------*/
207 /* 10: Test for long string character equality comparison */
208
209 printf("Test #%d:\n", ++testno);
210 for (i=0; i<LEN; i++) { mem1[i] = i; }
211 for (i=0; i<LEN; i++) { mem2[i] = i; }
212
213 rc = wmemcmp_s(mem1, LEN, mem2, LEN, &ind);
214 if (rc != EOK) {
215 printf("%s %u Ind=%d Error rc=%u \n",
216 __FUNCTION__, __LINE__, ind, rc );
217 }
218
219 if (ind != 0) {
220 printf("%s %u Ind=%d rc=%u \n",
221 __FUNCTION__, __LINE__, ind, rc );
222 }
223
224 ind = memcmp(mem1, mem2, LEN*4);
225 if (ind != 0) {
226 printf("%s %u Ind=%d rc=%u \n",
227 __FUNCTION__, __LINE__, ind, rc );
228 }
229
230 /*--------------------------------------------------*/
231 /* 11: Test short string - s1 > s2 at index 10 */
232
233 printf("Test #%d:\n", ++testno);
234 for (i=0; i<LEN; i++) { mem1[i] = i; }
235 for (i=0; i<LEN; i++) { mem2[i] = i; }
236
237 for (i=10; i<LEN-10; i++) { mem2[i] = 5; }
238
239 rc = wmemcmp_s(mem1, LEN, mem2, LEN, &ind);
240 if (rc != EOK) {
241 printf("%s %u Ind=%d Error rc=%u \n",
242 __FUNCTION__, __LINE__, ind, rc );
243 }
244
245 if (ind <= 0) {
246 printf("%s %u Ind=%d rc=%u \n",
247 __FUNCTION__, __LINE__, ind, rc );
248 }
249
250
251 /*--------------------------------------------------*/
252 /* 12: Test short string - s1 < s2 at index 10 */
253
254 printf("Test #%d:\n", ++testno);
255 for (i=0; i<LEN; i++) { mem1[i] = i; }
256 for (i=0; i<LEN; i++) { mem2[i] = i; }
257
258 for (i=10; i<LEN-10; i++) { mem2[i] = 5; }
259
260 rc = wmemcmp_s(mem2, LEN, mem1, LEN, &ind);
261 if (rc != EOK) {
262 printf("%s %u Ind=%d Error rc=%u \n",
263 __FUNCTION__, __LINE__, ind, rc );
264 }
265
266 if (ind >= 0) {
267 printf("%s %u Ind=%d rc=%u \n",
268 __FUNCTION__, __LINE__, ind, rc );
269 }
270
271 /*--------------------------------------------------*/
272 /* 13: Test long string - single char greater than difference at index 1 */
273
274 printf("Test #%d:\n", ++testno);
275 for (i=0; i<LEN; i++) { mem1[i] = i; }
276 for (i=0; i<LEN; i++) { mem2[i] = i; }
277
278 mem2[1] += 1;
279
280 rc = wmemcmp_s(mem1, LEN, mem2, LEN, &ind);
281 if (rc != EOK) {
282 printf("%s %u Ind=%d Error rc=%u \n",
283 __FUNCTION__, __LINE__, ind, rc );
284 }
285
286 if (ind >= 0) {
287 printf("%s %u Ind=%d rc=%u \n",
288 __FUNCTION__, __LINE__, ind, rc );
289 }
290
291 /*--------------------------------------------------*/
292 /* 14: Test long string - single char less than difference at index 1 */
293
294 printf("Test #%d:\n", ++testno);
295 for (i=0; i<LEN; i++) { mem1[i] = i; }
296 for (i=0; i<LEN; i++) { mem2[i] = i; }
297
298 mem2[1] -= 1;
299
300 rc = wmemcmp_s(mem1, LEN, mem2, LEN, &ind);
301 if (rc != EOK) {
302 printf("%s %u Ind=%d Error rc=%u \n",
303 __FUNCTION__, __LINE__, ind, rc );
304 }
305
306 if (ind <= 0) {
307 printf("%s %u Ind=%d rc=%u \n",
308 __FUNCTION__, __LINE__, ind, rc );
309 }
310
311 /*--------------------------------------------------*/
312 /* 15: Test long string - single char greater than difference at end of string */
313
314 printf("Test #%d:\n", ++testno);
315 for (i=0; i<LEN; i++) { mem1[i] = i; }
316 for (i=0; i<LEN; i++) { mem2[i] = i; }
317
318 mem2[LEN-1] += 1;
319
320 rc = wmemcmp_s(mem1, LEN, mem2, LEN, &ind);
321 if (rc != EOK) {
322 printf("%s %u Ind=%d Error rc=%u \n",
323 __FUNCTION__, __LINE__, ind, rc );
324 }
325
326 if (ind >= 0) {
327 printf("%s %u Ind=%d rc=%u \n",
328 __FUNCTION__, __LINE__, ind, rc );
329 }
330
331 /*--------------------------------------------------*/
332 /* 16: Test long string - single char less than difference at end of string */
333
334 printf("Test #%d:\n", ++testno);
335 for (i=0; i<LEN; i++) { mem1[i] = i; }
336 for (i=0; i<LEN; i++) { mem2[i] = i; }
337
338 mem2[LEN-1] -= 1;
339
340 rc = wmemcmp_s(mem1, LEN, mem2, LEN, &ind);
341 if (rc != EOK) {
342 printf("%s %u Ind=%d Error rc=%u \n",
343 __FUNCTION__, __LINE__, ind, rc );
344 }
345
346 if (ind <= 0) {
347 printf("%s %u Ind=%d rc=%u \n",
348 __FUNCTION__, __LINE__, ind, rc );
349 }
350
351 /*--------------------------------------------------*/
352 /*--------------------------------------------------*/
353
354 return (0);
355 }