forked from rurban/safeclib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_strcpyfldout_s.c
206 lines (160 loc) · 5.48 KB
/
test_strcpyfldout_s.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*------------------------------------------------------------------
* test_strcpyfldout_s
* File 'extstr/strcpyfldout_s.c'
* Lines executed:94.44% of 36
*
*------------------------------------------------------------------
*/
#include "test_private.h"
#include "safe_str_lib.h"
#define MAX (128)
#define LEN (128)
int main(void) {
errno_t rc;
uint32_t j;
rsize_t len;
rsize_t slen;
char str1[LEN];
char str2[LEN];
int errs = 0;
/*--------------------------------------------------*/
#ifndef HAVE_CT_BOS_OVR
EXPECT_BOS("dest overflow or empty")
rc = strcpyfldout_s(NULL, LEN, str2, LEN);
ERR(ESNULLP);
strcpy(str1, "aaaaa");
EXPECT_BOS("dest overflow or empty") EXPECT_BOS("slen overflow >dmax")
rc = strcpyfldout_s(str1, 0, str2, LEN);
ERR(ESZEROL); /* and untouched */
EXPSTR(str1, "aaaaa");
EXPECT_BOS("dest overflow or empty")
rc = strcpyfldout_s(str1, (RSIZE_MAX_STR + 1), str2, LEN);
ERR(ESLEMAX); /* and untouched */
EXPSTR(str1, "aaaaa");
EXPECT_BOS("src overflow or empty")
rc = strcpyfldout_s(str1, LEN, NULL, LEN);
ERR(ESNULLP); /* and cleared! */
CHECK_SLACK(str1, LEN);
EXPECT_BOS("slen overflow >dmax")
rc = strcpyfldout_s(str1, LEN - 1, str2, LEN);
ERR(ESNOSPC); /* and cleared */
CHECK_SLACK(str1, LEN - 1);
#endif
/*--------------------------------------------------*/
strcpy(str1, "aaaaa");
len = 5;
slen = 6;
rc = strcpyfldout_s(str1, len, str2, slen);
ERR(ESNOSPC); /* and cleared */
CHECK_SLACK(str1, len);
/*--------------------------------------------------*/
strcpy(str1, "aaaaa");
strcpy(str2, "01234567890123456789");
len = 5;
rc = strcpyfldout_s(str1, len, str2, 0);
ERR(EOK); /* and untouched */
EXPSTR(str1, "aaaaa");
rc = strcpyfldout_s(NULL, len, str2, 0);
ERR(EOK); /* and untouched */
EXPSTR(str1, "aaaaa");
rc = strcpyfldout_s(str1, 0, str2, 0);
ERR(EOK); /* and untouched */
EXPSTR(str1, "aaaaa");
rc = strcpyfldout_s(str1, len, NULL, 0);
ERR(EOK); /* and untouched */
EXPSTR(str1, "aaaaa");
/*--------------------------------------------------*/
strcpy(str1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
strcpy(str2, "01234567890123456789");
len = 1;
slen = 1;
rc = strcpyfldout_s(str1, len, str2, slen);
ERR(EOK)
/* str1 becomes null */
for (j = 0; j < len; j++) {
if (str1[j] != '\0') {
debug_printf("%s %u diff s1[%d]=%d s2[%d]=%d rc=%u \n",
__FUNCTION__, __LINE__, j, str1[j], j, str2[j], rc);
errs++;
}
}
/*--------------------------------------------------*/
strcpy(str1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
strcpy(str2, "01234567890123456789");
len = 2;
slen = 2;
rc = strcpyfldout_s(str1, len, str2, slen);
ERR(EOK)
for (j = 0; j < len - 1; j++) {
if (str1[j] != str2[j]) {
debug_printf("%s %u diff s1[%d]=%d s2[%d]=%d rc=%u \n",
__FUNCTION__, __LINE__, j, str1[j], j, str2[j], rc);
errs++;
}
}
/*--------------------------------------------------*/
strcpy(str1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
strcpy(str2, "01234567890123456789");
len = 3;
slen = 3;
rc = strcpyfldout_s(str1, len, str2, slen);
ERR(EOK)
for (j = 0; j < len - 1; j++) {
if (str1[j] != str2[j]) {
debug_printf("%s %u diff s1[%d]=%d s2[%d]=%d rc=%u \n",
__FUNCTION__, __LINE__, j, str1[j], j, str2[j], rc);
errs++;
}
}
/*--------------------------------------------------*/
strcpy(str1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
/* same string in dest and src */
len = LEN;
rc = strcpyfldout_s(str1, len, str1, len);
ERR(ESOVRLP); /* and cleared */
CHECK_SLACK(str1, len);
/*--------------------------------------------------*/
strcpy(str1, "keep it simple for best results");
len = strlen(str1);
/* overlap */
rc = strcpyfldout_s(&str1[0], len, &str1[5], len);
ERR(ESOVRLP); /* and cleared */
CHECK_SLACK(str1, len);
/*--------------------------------------------------*/
strcpy(str1, "keep it simple for best results ");
len = 20;
/* overlap */
rc = strcpyfldout_s(&str1[10], len, &str1[0], len);
ERR(ESOVRLP)
CHECK_SLACK(&str1[10], len);
/*--------------------------------------------------*/
strcpy(str1, "keep it simple");
len = strlen(str1);
strcpy(str2, "aaaaaaaaaaaaaa");
slen = strlen(str2);
rc = strcpyfldout_s(str1, len, str2, slen);
ERR(EOK)
for (j = 0; j < len - 1; j++) {
if (str1[j] != str2[j]) {
debug_printf("%s %u diff s1[%d]=%d s2[%d]=%d rc=%u \n",
__FUNCTION__, __LINE__, j, str1[j], j, str2[j], rc);
errs++;
}
}
/*--------------------------------------------------*/
strcpy(str1, "always keep it simple");
len = strlen(str1);
strcpy(str2, "keep it simple");
slen = strlen(str2);
rc = strcpyfldout_s(str1, len, str2, slen);
ERR(EOK)
for (j = 0; j < slen; j++) {
if (str1[j] != str2[j]) {
debug_printf("%s %u diff s1[%d]=%d s2[%d]=%d rc=%u \n",
__FUNCTION__, __LINE__, j, str1[j], j, str2[j], rc);
errs++;
}
}
/*--------------------------------------------------*/
return (errs);
}