Debian Codegraph @

ext/safestringlib/unittests/test_strcpy_s.c

raw
1 /*------------------------------------------------------------------
2 * test_strcpy_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
17 int test_strcpy_s (void)
18 {
19 errno_t rc;
20 #ifdef SAFE_LIB_STR_NULL_SLACK
21 uint32_t i;
22 #endif /*SAFE_LIB_STR_NULL_SLACK*/
23 int32_t ind;
24
25 /*--------------------------------------------------*/
26
27 rc = strcpy_s(NULL, LEN, str2);
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 rc = strcpy_s(str1, 5, NULL);
38 if (rc != ESNULLP) {
39 debug_printf("%s %u Error rc=%u \n",
40 __FUNCTION__, __LINE__, rc );
41 }
42
43 #ifdef SAFE_LIB_STR_NULL_SLACK
44 for (i=0; i<5; i++) {
45 if (str1[i] != '\0') {
46 debug_printf("%s %u Error rc=%u \n",
47 __FUNCTION__, __LINE__, rc );
48 }
49 }
50 #else
51 if (str1[0] != '\0') {
52 debug_printf("%s %u Error rc=%u \n",
53 __FUNCTION__, __LINE__, rc );
54 }
55 #endif
56
57 /*--------------------------------------------------*/
58
59 rc = strcpy_s(str1, 0, str2);
60 if (rc != ESZEROL) {
61 debug_printf("%s %u Error rc=%u \n",
62 __FUNCTION__, __LINE__, rc );
63 }
64
65 /*--------------------------------------------------*/
66
67 rc = strcpy_s(str1, (RSIZE_MAX_STR+1), str2);
68 if (rc != ESLEMAX) {
69 debug_printf("%s %u Error rc=%u \n",
70 __FUNCTION__, __LINE__, rc );
71 }
72
73 /*--------------------------------------------------*/
74 /*--------------------------------------------------*/
75
76 strcpy(str1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
77 str2[0] = '\0';
78
79 rc = strcpy_s(str1, LEN/2, str2);
80 if (rc != EOK) {
81 debug_printf("%s %u Error rc=%u \n",
82 __FUNCTION__, __LINE__, rc );
83 }
84
85 #ifdef SAFE_LIB_STR_NULL_SLACK
86 for (i=0; i<LEN/2; i++) {
87 if (str1[i] != '\0') {
88 debug_printf("%s %u Error rc=%u \n",
89 __FUNCTION__, __LINE__, rc );
90 }
91 }
92 #else
93 if (str1[0] != '\0') {
94 debug_printf("%s %u Error rc=%u \n",
95 __FUNCTION__, __LINE__, rc );
96 }
97 #endif
98
99 /*--------------------------------------------------*/
100
101 strcpy(str1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
102
103 rc = strcpy_s(str1, LEN, str1);
104 if (rc != EOK) {
105 debug_printf("%s %u Error rc=%u \n",
106 __FUNCTION__, __LINE__, rc );
107 }
108
109 /*--------------------------------------------------*/
110
111 strcpy(&str1[0], "keep it simple");
112
113 rc = strcpy_s(&str1[0], LEN, &str1[5]);
114 if (rc != ESOVRLP) {
115 debug_printf("%s %u Error rc=%u \n",
116 __FUNCTION__, __LINE__, rc );
117 }
118
119 #ifdef SAFE_LIB_STR_NULL_SLACK
120 for (i=0; i<LEN; i++) {
121 if (str1[i] != '\0') {
122 debug_printf("%s %u Error rc=%u \n",
123 __FUNCTION__, __LINE__, rc );
124 }
125 }
126 #else
127 if (str1[0] != '\0') {
128 debug_printf("%s %u Error rc=%u \n",
129 __FUNCTION__, __LINE__, rc );
130 }
131 #endif
132
133 /*--------------------------------------------------*/
134
135 strcpy(&str1[0], "keep it simple");
136 str2[0] = '\0';
137
138 rc = strcpy_s(str1, LEN, str2);
139 if (rc != EOK) {
140 debug_printf("%s %u Error rc=%u \n",
141 __FUNCTION__, __LINE__, rc );
142 }
143
144 if (*str1 != '\0') {
145 debug_printf("%s %u -%s- Error rc=%u \n",
146 __FUNCTION__, __LINE__, str1, rc );
147 }
148
149 /*--------------------------------------------------*/
150
151 str1[0] = '\0';
152 strcpy(&str2[0], "keep it simple");
153
154 rc = strcpy_s(str1, LEN, str2);
155 if (rc != EOK) {
156 debug_printf("%s %u Error rc=%u \n",
157 __FUNCTION__, __LINE__, rc );
158 }
159
160 /* be sure the results are the same as strcmp */
161 ind = strcmp(str1, str2);
162 if (ind != 0) {
163 debug_printf("%s %u -%s- Error rc=%u \n",
164 __FUNCTION__, __LINE__, str1, rc );
165 }
166
167 /*--------------------------------------------------*/
168
169 strcpy(str1, "qqweqeqeqeq");
170 strcpy(str2, "keep it simple");
171
172 rc = strcpy_s(str1, LEN, str2);
173 if (rc != EOK) {
174 debug_printf("%s %u Error rc=%u \n",
175 __FUNCTION__, __LINE__, rc );
176 }
177
178 /* be sure the results are the same as strcmp */
179 ind = strcmp(str1, str2);
180 if (ind != 0) {
181 debug_printf("%s %u -%s- Error rc=%u \n",
182 __FUNCTION__, __LINE__, str1, rc );
183 }
184
185 /*--------------------------------------------------*/
186
187 strcpy(str1, "qqweqeqeqeq");
188 strcpy(str2, "keep it simple");
189
190 rc = strcpy_s(str1, 1, str2);
191 if (rc != ESNOSPC) {
192 debug_printf("%s %u Error rc=%u \n",
193 __FUNCTION__, __LINE__, rc );
194 }
195
196 if (*str1 != '\0') {
197 debug_printf("%s %u -%s- Error rc=%u \n",
198 __FUNCTION__, __LINE__, str1, rc );
199 }
200
201 /*--------------------------------------------------*/
202
203 strcpy(str1, "qqweqeqeqeq");
204 strcpy(str2, "keep it simple");
205
206 rc = strcpy_s(str1, 2, str2);
207 if (rc != ESNOSPC) {
208 debug_printf("%s %u Error rc=%u \n",
209 __FUNCTION__, __LINE__, rc );
210 }
211
212 if (*str1 != '\0') {
213 debug_printf("%s %u -%s- Error rc=%u \n",
214 __FUNCTION__, __LINE__, str1, rc );
215 }
216
217 /*--------------------------------------------------*/
218
219 strcpy(str1, "qqweqeqeqeq");
220 strcpy(str2, "it");
221
222 rc = strcpy_s(str1, 3, str2);
223 if (rc != EOK) {
224 debug_printf("%s %u Error rc=%u \n",
225 __FUNCTION__, __LINE__, rc );
226 }
227
228 /* be sure the results are the same as strcmp */
229 ind = strcmp(str1, str2);
230 if (ind != 0) {
231 debug_printf("%s %u -%s- Error rc=%u \n",
232 __FUNCTION__, __LINE__, str1, rc );
233 }
234
235 /*--------------------------------------------------*/
236
237 strcpy(str1, "qq12345weqeqeqeq");
238 strcpy(str2, "it");
239
240 rc = strcpy_s(str1, 10, str2);
241 if (rc != EOK) {
242 debug_printf("%s %u Error rc=%u \n",
243 __FUNCTION__, __LINE__, rc );
244 }
245
246 /* be sure the results are the same as strcpy */
247 ind = strcmp(str1, str2);
248 if (ind != 0) {
249 debug_printf("%s %u -%s- Error rc=%u \n",
250 __FUNCTION__, __LINE__, str1, rc );
251 }
252
253 /*
254 for (i=0; i<10; i++) {
255 printf("str1[%i] = %d \n", i, str1[i]);
256 } */
257
258 /*--------------------------------------------------*/
259
260 return (0);
261 }
262
263