Debian Codegraph @

ext/safestringlib/unittests/test_wcsnlen_s.c

raw
1 /*------------------------------------------------------------------
2 * test_wcsnlen_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 *
47 *
48 *
49 *
50 *------------------------------------------------------------------
51 */
52
53 #include "test_private.h"
54 #include "safe_str_lib.h"
55
56 #define MAX ( 128*4 )
57 #define LEN ( 128*4 )
58
59 static wchar_t str1[LEN];
60
61 extern rsize_t wcsnlen_s(const wchar_t *dest, rsize_t dmax);
62 extern errno_t wmemset_s (wchar_t *dest, wchar_t value, rsize_t len);
63
64 int test_wcsnlen_s (void)
65 {
66 errno_t rc;
67 unsigned int testno = 0;
68
69
70 printf("\nTesting wcsnlen_s:\n");
71 /*--------------------------------------------------*/
72 /* 1 Test for NULL destination check */
73 printf("Test #%d:\n", ++testno);
74
75 rc = wcsnlen_s(NULL, LEN);
76 if (rc != 0) {
77 printf("%s %u Error rc=%u \n",
78 __FUNCTION__, __LINE__, rc );
79 }
80
81 /*--------------------------------------------------*/
82 /* 2 Test for too large maximum size */
83 printf("Test #%d:\n", ++testno);
84
85 rc = wcsnlen_s(str1, (RSIZE_MAX_STR+1));
86 if (rc != 0) {
87 printf("%s %u Error rc=%u \n",
88 __FUNCTION__, __LINE__, rc );
89 }
90
91 /*--------------------------------------------------*/
92 /* 3 Test for length is equal to maximum */
93 printf("Test #%d:\n", ++testno);
94
95 wmemcpy_s(str1, LEN, L"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 42);
96
97 rc = wcsnlen_s(str1, 41);
98 if (rc != 40) {
99 printf("%s %u Error rc=%u \n",
100 __FUNCTION__, __LINE__, rc );
101 }
102
103 /*--------------------------------------------------*/
104 /* 4 Test for return length is equal to dmax */
105 printf("Test #%d:\n", ++testno);
106
107 wmemcpy_s(str1, LEN, L"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 42);
108
109 rc = wcsnlen_s(str1, 20);
110 if (rc != 20) {
111 printf("%s %u Error rc=%u \n",
112 __FUNCTION__, __LINE__, rc );
113 }
114
115 /*--------------------------------------------------*/
116 /* 5 Test for zero length string */
117 printf("Test #%d:\n", ++testno);
118
119 wmemset_s(str1, L'\0', 42);
120
121 rc = wcsnlen_s(str1, LEN);
122 if (rc != 0) {
123 printf("%s %u Error rc=%u \n",
124 __FUNCTION__, __LINE__, rc );
125 }
126
127
128 /*--------------------------------------------------*/
129 /* 6 Test for single character string */
130 printf("Test #%d:\n", ++testno);
131
132 memset_s(str1, '\0', 50);
133 str1[0] = L'A';
134
135 rc = wcsnlen_s(str1, LEN);
136 if (rc != 1) {
137 printf("%s %u Error rc=%u \n",
138 __FUNCTION__, __LINE__, rc );
139 }
140
141 /*--------------------------------------------------*/
142 /* 7 Test for two char string */
143 printf("Test #%d:\n", ++testno);
144
145 wmemcpy_s(str1, LEN, L"12", 3);
146
147 rc = wcsnlen_s(str1, LEN);
148 if (rc != 2) {
149 printf("%s %u Error rc=%u \n",
150 __FUNCTION__, __LINE__, rc );
151 }
152
153
154 return (0);
155 }
156