xref: /freebsd/lib/libc/tests/string/memset_s_test.c (revision 0183e0151669735d62584fbba9125ed90716af5e)
1 /*-
2  * Copyright (c) 2017 Juniper Networks.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28 
29 #include <assert.h>
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
34 #include <atf-c.h>
35 
36 static errno_t e;
37 static const char * restrict m;
38 
39 void
40 h(const char * restrict msg, void * restrict ptr __unused, errno_t error)
41 {
42 	e = error;
43 	m = msg;
44 }
45 
46 /* null ptr */
47 ATF_TC_WITHOUT_HEAD(null_ptr);
48 ATF_TC_BODY(null_ptr, tc)
49 {
50 	assert(memset_s(0, 1, 1, 1) != 0);
51 }
52 
53 /* smax > rmax */
54 ATF_TC_WITHOUT_HEAD(smax_gt_rmax);
55 ATF_TC_BODY(smax_gt_rmax, tc)
56 {
57 	char b;
58 
59 	assert(memset_s(&b, RSIZE_MAX + 1, 1, 1) != 0);
60 }
61 
62 /* smax < 0 */
63 ATF_TC_WITHOUT_HEAD(smax_lt_zero);
64 ATF_TC_BODY(smax_lt_zero, tc)
65 {
66 	char b;
67 
68 	assert(memset_s(&b, -1, 1, 1) != 0);
69 }
70 
71 /* normal */
72 ATF_TC_WITHOUT_HEAD(normal);
73 ATF_TC_BODY(normal, tc)
74 {
75 	char b;
76 
77 	b = 3;
78 	assert(memset_s(&b, 1, 5, 1) == 0);
79 	assert(b == 5);
80 }
81 
82 /* n > rmax */
83 ATF_TC_WITHOUT_HEAD(n_gt_rmax);
84 ATF_TC_BODY(n_gt_rmax, tc)
85 {
86 	char b;
87 
88 	assert(memset_s(&b, 1, 1, RSIZE_MAX + 1) != 0);
89 }
90 
91 /* n < 0 */
92 ATF_TC_WITHOUT_HEAD(n_lt_zero);
93 ATF_TC_BODY(n_lt_zero, tc)
94 {
95 	char b;
96 
97 	assert(memset_s(&b, 1, 1, -1) != 0);
98 }
99 
100 /* n < smax */
101 ATF_TC_WITHOUT_HEAD(n_lt_smax);
102 ATF_TC_BODY(n_lt_smax, tc)
103 {
104 	char b[3] = {1, 2, 3};
105 
106 	assert(memset_s(&b[0], 3, 9, 1) == 0);
107 	assert(b[0] == 9);
108 	assert(b[1] == 2);
109 	assert(b[2] == 3);
110 }
111 
112 /* n > smax */
113 ATF_TC_WITHOUT_HEAD(n_gt_smax);
114 ATF_TC_BODY(n_gt_smax, tc)
115 {
116 	char b[3] = {1, 2, 3};
117 
118 	assert(memset_s(&b[0], 1, 9, 3) == 0);
119 	assert(b[0] == 9);
120 	assert(b[1] == 2);
121 	assert(b[2] == 3);
122 }
123 
124 /* smax > rmax, handler */
125 ATF_TC_WITHOUT_HEAD(smax_gt_rmax_handler);
126 ATF_TC_BODY(smax_gt_rmax_handler, tc)
127 {
128 	char b;
129 
130 	e = 0;
131 	m = NULL;
132 	set_constraint_handler_s(h);
133 	assert(memset_s(&b, RSIZE_MAX + 1, 1, 1) != 0);
134 	assert(e > 0);
135 	assert(strcmp(m, "memset_s : smax > RSIZE_MAX") == 0);
136 }
137 
138 /* smax < 0, handler */
139 ATF_TC_WITHOUT_HEAD(smax_lt_zero_handler);
140 ATF_TC_BODY(smax_lt_zero_handler, tc)
141 {
142 	char b;
143 
144 	e = 0;
145 	m = NULL;
146 	set_constraint_handler_s(h);
147 	assert(memset_s(&b, -1, 1, 1) != 0);
148 	assert(e > 0);
149 	assert(strcmp(m, "memset_s : smax > RSIZE_MAX") == 0);
150 }
151 
152 /* n > rmax, handler */
153 ATF_TC_WITHOUT_HEAD(n_gt_rmax_handler);
154 ATF_TC_BODY(n_gt_rmax_handler, tc)
155 {
156 	char b;
157 
158 	e = 0;
159 	m = NULL;
160 	set_constraint_handler_s(h);
161 	assert(memset_s(&b, 1, 1, RSIZE_MAX + 1) != 0);
162 	assert(e > 0);
163 	assert(strcmp(m, "memset_s : n > RSIZE_MAX") == 0);
164 }
165 
166 /* n < 0, handler */
167 ATF_TC_WITHOUT_HEAD(n_lt_zero_handler);
168 ATF_TC_BODY(n_lt_zero_handler, tc)
169 {
170 	char b;
171 
172 	e = 0;
173 	m = NULL;
174 	set_constraint_handler_s(h);
175 	assert(memset_s(&b, 1, 1, -1) != 0);
176 	assert(e > 0);
177 	assert(strcmp(m, "memset_s : n > RSIZE_MAX") == 0);
178 }
179 
180 ATF_TP_ADD_TCS(tp)
181 {
182 	ATF_TP_ADD_TC(tp, null_ptr);
183 	ATF_TP_ADD_TC(tp, smax_gt_rmax);
184 	ATF_TP_ADD_TC(tp, smax_lt_zero);
185 	ATF_TP_ADD_TC(tp, normal);
186 	ATF_TP_ADD_TC(tp, n_gt_rmax);
187 	ATF_TP_ADD_TC(tp, n_lt_zero);
188 	ATF_TP_ADD_TC(tp, n_gt_smax);
189 	ATF_TP_ADD_TC(tp, n_lt_smax);
190 	ATF_TP_ADD_TC(tp, smax_gt_rmax_handler);
191 	ATF_TP_ADD_TC(tp, smax_lt_zero_handler);
192 	ATF_TP_ADD_TC(tp, n_gt_rmax_handler);
193 	ATF_TP_ADD_TC(tp, n_lt_zero_handler);
194 	return (atf_no_error());
195 }
196