forked from rurban/safeclib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_strnset_s.c
153 lines (126 loc) · 3.9 KB
/
test_strnset_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
/*------------------------------------------------------------------
* test_strnset_s
* File 'strnset_s.c'
* Lines executed:95.00% of 20
*
*------------------------------------------------------------------
*/
#include "test_private.h"
#include "safe_str_lib.h"
#define LEN (128)
int main(void) {
errno_t rc;
rsize_t max_len = 5;
char str1[LEN];
int value = 0;
uint32_t j;
int errs = 0;
/*--------------------------------------------------*/
#ifndef HAVE_CT_BOS_OVR
EXPECT_BOS("empty dest") EXPECT_BOS("dest overflow or empty")
rc = strnset_s(NULL, 5, value, 5);
ERR(ESNULLP)
EXPECT_BOS("empty dest or dmax") EXPECT_BOS("n overflow >dmax")
rc = strnset_s(str1, 0, 0, 5);
ERR(ESZEROL)
EXPECT_BOS("dest overflow")
rc = strnset_s(str1, RSIZE_MAX_STR + 1, 0, 5);
ERR(ESLEMAX);
#ifdef HAVE___BUILTIN_OBJECT_SIZE
EXPECT_BOS("dest overflow")
rc = strnset_s(str1, LEN + 1, 0, 5);
ERR(EOVERFLOW);
#endif
EXPECT_BOS("value overflow >255")
rc = strnset_s(str1, LEN, 256, 5);
ERR(ESLEMAX);
strcpy(str1, "abc");
EXPECT_BOS("n overflow >dmax")
rc = strnset_s(str1, 5, 0, 6);
ERR(ESNOSPC);
#endif
/*--------------------------------------------------*/
strcpy(str1, "abc");
value = 20;
max_len = 1;
rc = strnset_s(str1, LEN, value, max_len);
ERR(EOK)
for (j = 0; j < max_len; j++) {
if (str1[j] != (char)value) {
debug_printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc);
errs++;
}
}
/*--------------------------------------------------*/
strcpy(str1, "abc");
value = 0;
max_len = 2;
rc = strnset_s(str1, LEN, value, max_len);
ERR(EOK)
for (j = 0; j < max_len; j++) {
if (str1[j] != (char)value) {
debug_printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc);
errs++;
}
}
/*--------------------------------------------------*/
strcpy(str1, "abc");
max_len = 3;
rc = strnset_s(str1, LEN, value, max_len); /* !! */
ERR(EOK)
for (j = 0; j < max_len; j++) {
if (str1[j] != (char)value) {
debug_printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc);
errs++;
}
}
CHECK_SLACK(&str1[3], LEN - 3);
/*--------------------------------------------------*/
strcpy(str1, "abc");
max_len = 4;
rc = strnset_s(str1, LEN, value, max_len);
ERR(EOK)
for (j = 0; j < max_len; j++) {
if (str1[j] != (char)value) {
debug_printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc);
errs++;
}
}
CHECK_SLACK(&str1[3], LEN - 3);
/*--------------------------------------------------*/
strcpy(str1, "abc");
max_len = 5;
rc = strnset_s(str1, LEN, value, max_len);
ERR(EOK)
for (j = 0; j < 3; j++) {
if (str1[j] != (char)value) {
debug_printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc);
errs++;
}
}
CHECK_SLACK(&str1[3], LEN - 3);
/*--------------------------------------------------*/
strcpy(str1, "abc");
max_len = LEN;
rc = strnset_s(str1, LEN, value, max_len);
ERR(EOK)
for (j = 0; j < 3; j++) {
if (str1[j] != (char)value) {
debug_printf("%s %u Error rc=%u \n", __FUNCTION__, __LINE__, rc);
errs++;
}
}
CHECK_SLACK(&str1[3], LEN - 3);
/*--------------------------------------------------*/
strcpy(str1, "Now is the time for all data to be zeroed");
max_len = strlen("Now is the ");
rc = strnset_s(str1, LEN, 0, max_len);
ERR(EOK)
if (strcmp(&str1[max_len], "time for all data to be zeroed")) {
debug_printf("%s %u ERROR --%s-- \n", __FUNCTION__, __LINE__,
&str1[max_len]);
errs++;
}
/*--------------------------------------------------*/
return (errs);
}