Debian Codegraph @

ext/safestringlib/unittests/test_strncpy_s.c

raw
1 /*------------------------------------------------------------------
2 * test_strncpy_s
3 *
4 *
5 *------------------------------------------------------------------
6 */
7
8 #include "test_private.h"
9 #include "safe_str_lib.h"
10
11 #define MAX ( 128 )
12 #define LEN ( 128 )
13
14 static char str1[LEN];
15 static char str2[LEN];
16 static char dest[LEN];
17
18 int test_strncpy_s (void)
19 {
20 errno_t rc;
21 rsize_t nlen;
22 int32_t ind;
23
24 /*--------------------------------------------------*/
25
26 nlen = 5;
27 rc = strncpy_s(NULL, LEN, str2, nlen);
28 if (rc != ESNULLP) {
29 debug_printf("%s %u Error rc=%u \n",
30 __FUNCTION__, __LINE__, rc );
31 }
32
33 /*--------------------------------------------------*/
34
35 strcpy(str1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
36
37 nlen = 5;
38 rc = strncpy_s(str1, 5, NULL, nlen);
39 if (rc != ESNULLP) {
40 debug_printf("%s %u Error rc=%u \n",
41 __FUNCTION__, __LINE__, rc );
42 }
43
44 if (str1[0] != '\0') {
45 debug_printf("%s %u Error rc=%u \n",
46 __FUNCTION__, __LINE__, rc );
47 }
48
49 /*--------------------------------------------------*/
50
51 nlen = 5;
52 rc = strncpy_s(str1, 0, str2, nlen);
53 if (rc != ESZEROL) {
54 debug_printf("%s %u Error rc=%u \n",
55 __FUNCTION__, __LINE__, rc );
56 }
57
58 /*--------------------------------------------------*/
59
60 rc = strncpy_s(str1, (RSIZE_MAX_STR+1), str2, nlen);
61 if (rc != ESLEMAX) {
62 debug_printf("%s %u Error rc=%u \n",
63 __FUNCTION__, __LINE__, rc );
64 }
65
66 /*--------------------------------------------------*/
67 /*--------------------------------------------------*/
68
69 strcpy(str1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
70 str2[0] = '\0';
71
72 rc = strncpy_s(str1, 5, str2, 0);
73 if (rc != ESZEROL) {
74 debug_printf("%s %u Error rc=%u \n",
75 __FUNCTION__, __LINE__, rc );
76 }
77
78 if (str1[0] != '\0') {
79 debug_printf("%s %u Error rc=%u \n",
80 __FUNCTION__, __LINE__, rc );
81 }
82
83 /*--------------------------------------------------*/
84
85 #if 1
86 strcpy(str1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
87 strcpy(str2, "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
88
89 rc = strncpy_s(str1, 5, str2, (RSIZE_MAX_STR+1));
90 if (rc != ESLEMAX) {
91 debug_printf("%s %u Error rc=%u \n",
92 __FUNCTION__, __LINE__, rc );
93 }
94 #endif
95
96 /*--------------------------------------------------*/
97
98 strcpy(str1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
99 str2[0] = '\0';
100 nlen = 5;
101
102 rc = strncpy_s(&str1[0], LEN/2, &str2[0], nlen);
103 if (rc != EOK) {
104 debug_printf("%s %u Error rc=%u \n",
105 __FUNCTION__, __LINE__, rc );
106 }
107
108 if (str1[0] != '\0') {
109 debug_printf("%s %u Error rc=%u \n",
110 __FUNCTION__, __LINE__, rc );
111 }
112
113 /*--------------------------------------------------*/
114
115 strcpy(str1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
116 nlen = 5;
117
118 /* test overlap */
119 rc = strncpy_s(str1, LEN, str1, nlen);
120 if (rc != ESOVRLP) {
121 debug_printf("%s %u Error rc=%u \n",
122 __FUNCTION__, __LINE__, rc );
123 }
124
125 if (str1[0] != '\0') {
126 debug_printf("%s %u Error rc=%u \n",
127 __FUNCTION__, __LINE__, rc );
128 }
129
130 /*--------------------------------------------------*/
131
132 strcpy(str1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
133 nlen = 18;
134
135 rc = strncpy_s(&str1[0], LEN, &str1[5], nlen);
136 if (rc != ESOVRLP) {
137 debug_printf("%s %u Error rc=%u \n",
138 __FUNCTION__, __LINE__, rc );
139 }
140
141 if (str1[0] != '\0') {
142 debug_printf("%s %u Error rc=%u \n",
143 __FUNCTION__, __LINE__, rc );
144 }
145
146 /*--------------------------------------------------*/
147
148 strcpy(str1, "keep it simple");
149 str2[0] = '\0';
150
151 nlen = 10;
152 rc = strncpy_s(str1, LEN, str2, nlen);
153 if (rc != EOK) {
154 debug_printf("%s %u Error rc=%u \n",
155 __FUNCTION__, __LINE__, rc );
156 }
157
158 if (str1[0] != '\0') {
159 debug_printf("%s %u Error rc=%u \n",
160 __FUNCTION__, __LINE__, rc );
161 }
162
163 /*--------------------------------------------------*/
164
165 str1[0] = '\0';
166 strcpy(str2, "keep it simple");
167
168 nlen = 20;
169 rc = strncpy_s(str1, LEN, str2, nlen);
170 if (rc != EOK) {
171 debug_printf("%s %u Error rc=%u \n",
172 __FUNCTION__, __LINE__, rc );
173 }
174
175 /* be sure the results are the same as strcmp */
176 ind = strcmp(str1, str2);
177 if (ind != 0) {
178 debug_printf("%s %u -%s- Error rc=%u \n",
179 __FUNCTION__, __LINE__, str1, rc );
180 }
181
182
183 /*--------------------------------------------------*/
184
185 strcpy(str1, "qqweqeqeqeq");
186 strcpy(str2, "keep it simple");
187
188 nlen = 32;
189 rc = strncpy_s(str1, LEN, str2, nlen);
190 if (rc != EOK) {
191 debug_printf("%s %u Error rc=%u \n",
192 __FUNCTION__, __LINE__, rc );
193 }
194
195 /* be sure the results are the same as strcmp */
196 ind = strcmp(str1, str2);
197 if (ind != 0) {
198 debug_printf("%s %u -%s- Error rc=%u \n",
199 __FUNCTION__, __LINE__, str1, rc );
200 }
201
202 /*--------------------------------------------------*/
203 /*--------------------------------------------------*/
204
205 strcpy(str1, "qqweqeqeqeq");
206 strcpy(str2, "keep it simple");
207
208 rc = strncpy_s(str1, 1, str2, nlen);
209 if (rc != ESNOSPC) {
210 debug_printf("%s %u Error rc=%u \n",
211 __FUNCTION__, __LINE__, rc );
212 }
213
214 if (*str1 != '\0') {
215 debug_printf("%s %u -%s- Error rc=%u \n",
216 __FUNCTION__, __LINE__, str1, rc );
217 }
218
219 /*--------------------------------------------------*/
220
221 strcpy(str1, "qqweqeqeqeq");
222 strcpy(str2, "keep it simple");
223
224 rc = strncpy_s(str1, 2, str2, nlen);
225 if (rc != ESNOSPC) {
226 debug_printf("%s %u Error rc=%u \n",
227 __FUNCTION__, __LINE__, rc );
228 }
229
230 if (*str1 != '\0') {
231 debug_printf("%s %u -%s- Error rc=%u \n",
232 __FUNCTION__, __LINE__, str1, rc );
233 }
234
235 /*--------------------------------------------------*/
236 /* TR example */
237
238 strcpy(dest, " ");
239 strcpy(str1, "hello");
240
241 rc = strncpy_s(dest, 6, str1, 100);
242 if (rc != EOK) {
243 debug_printf("%s %u Error rc=%u \n",
244 __FUNCTION__, __LINE__, rc );
245 }
246
247 /* be sure the results are the same as strcmp */
248 ind = strcmp(dest, str1);
249 if (ind != 0) {
250 debug_printf("%s %u -%s- Error rc=%u \n",
251 __FUNCTION__, __LINE__, str1, rc );
252 }
253
254 /*--------------------------------------------------*/
255 /* TR example */
256
257 strcpy(dest, " ");
258 strcpy(str2, "goodbye");
259
260 rc = strncpy_s(dest, 5, str2, 7);
261 if (rc != ESNOSPC) {
262 debug_printf("%s %u Error rc=%u \n",
263 __FUNCTION__, __LINE__, rc );
264 }
265
266 /*--------------------------------------------------*/
267 /* TR example */
268
269 strcpy(dest, " ");
270 strcpy(str2, "goodbye");
271
272 rc = strncpy_s(dest, 5, str2, 4);
273 if (rc != EOK) {
274 debug_printf("%s %u Error rc=%u \n",
275 __FUNCTION__, __LINE__, rc );
276 }
277
278 /* be sure the results are the same as strcmp */
279 ind = strcmp(dest, "good");
280 if (ind != 0) {
281 debug_printf("%s %u -%s- Error rc=%u \n",
282 __FUNCTION__, __LINE__, str1, rc );
283 }
284
285 /*--------------------------------------------------*/
286
287 strcpy(dest, " ");
288 strcpy(str2, "good");
289
290 /* strnlen("good") < 5 */
291 rc = strncpy_s(dest, 5, str2, 8);
292 if (rc != EOK) {
293 debug_printf("%s %u Error rc=%u \n",
294 __FUNCTION__, __LINE__, rc );
295 }
296
297 /* be sure the results are the same as strcmp */
298 ind = strcmp(dest, "good");
299 if (ind != 0) {
300 debug_printf("%s %u -%s- Error rc=%u \n",
301 __FUNCTION__, __LINE__, str1, rc );
302 }
303
304 /*--------------------------------------------------*/
305
306 strcpy(str1, "qq12345weqeqeqeq");
307 strcpy(str2, "it");
308
309 nlen = 10;
310 rc = strncpy_s(str1, 10, str2, nlen);
311 if (rc != EOK) {
312 debug_printf("%s %u Error rc=%u \n",
313 __FUNCTION__, __LINE__, rc );
314 }
315
316 /* be sure the results are the same as strcmp */
317 ind = strcmp(str1, str2);
318 if (ind != 0) {
319 debug_printf("%s %u -%s- Error rc=%u \n",
320 __FUNCTION__, __LINE__, str1, rc );
321 }
322
323 /*--------------------------------------------------*/
324
325 return (0);
326 }
327
328