xref: /freebsd/lib/libc/tests/string/memset_s_test.c (revision a03411e84728e9b267056fd31c7d1d9d1dc1b01e)
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 <assert.h>
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <string.h>
30 
31 #include <atf-c.h>
32 
33 static errno_t e;
34 static const char * restrict m;
35 
36 void
37 h(const char * restrict msg, void * restrict ptr __unused, errno_t error)
38 {
39 	e = error;
40 	m = msg;
41 }
42 
43 /* null ptr */
44 ATF_TC_WITHOUT_HEAD(null_ptr);
45 ATF_TC_BODY(null_ptr, tc)
46 {
47 	assert(memset_s(0, 1, 1, 1) != 0);
48 }
49 
50 /* smax > rmax */
51 ATF_TC_WITHOUT_HEAD(smax_gt_rmax);
52 ATF_TC_BODY(smax_gt_rmax, tc)
53 {
54 	char b;
55 
56 	assert(memset_s(&b, RSIZE_MAX + 1, 1, 1) != 0);
57 }
58 
59 /* smax < 0 */
60 ATF_TC_WITHOUT_HEAD(smax_lt_zero);
61 ATF_TC_BODY(smax_lt_zero, tc)
62 {
63 	char b;
64 
65 	assert(memset_s(&b, -1, 1, 1) != 0);
66 }
67 
68 /* normal */
69 ATF_TC_WITHOUT_HEAD(normal);
70 ATF_TC_BODY(normal, tc)
71 {
72 	char b;
73 
74 	b = 3;
75 	assert(memset_s(&b, 1, 5, 1) == 0);
76 	assert(b == 5);
77 }
78 
79 /* n > rmax */
80 ATF_TC_WITHOUT_HEAD(n_gt_rmax);
81 ATF_TC_BODY(n_gt_rmax, tc)
82 {
83 	char b;
84 
85 	assert(memset_s(&b, 1, 1, RSIZE_MAX + 1) != 0);
86 }
87 
88 /* n < 0 */
89 ATF_TC_WITHOUT_HEAD(n_lt_zero);
90 ATF_TC_BODY(n_lt_zero, tc)
91 {
92 	char b;
93 
94 	assert(memset_s(&b, 1, 1, -1) != 0);
95 }
96 
97 /* n < smax */
98 ATF_TC_WITHOUT_HEAD(n_lt_smax);
99 ATF_TC_BODY(n_lt_smax, tc)
100 {
101 	char b[3] = {1, 2, 3};
102 
103 	assert(memset_s(&b[0], 3, 9, 1) == 0);
104 	assert(b[0] == 9);
105 	assert(b[1] == 2);
106 	assert(b[2] == 3);
107 }
108 
109 /* n > smax, handler */
110 ATF_TC_WITHOUT_HEAD(n_gt_smax);
111 ATF_TC_BODY(n_gt_smax, tc)
112 {
113 	char b[3] = {1, 2, 3};
114 
115 	e = 0;
116 	m = NULL;
117 	set_constraint_handler_s(h);
118 	assert(memset_s(&b[0], 1, 9, 3) != 0);
119 	assert(e > 0);
120 	assert(strcmp(m, "memset_s : n > smax") == 0);
121 	assert(b[0] == 9);
122 	assert(b[1] == 2);
123 	assert(b[2] == 3);
124 }
125 
126 /* smax > rmax, handler */
127 ATF_TC_WITHOUT_HEAD(smax_gt_rmax_handler);
128 ATF_TC_BODY(smax_gt_rmax_handler, tc)
129 {
130 	char b;
131 
132 	e = 0;
133 	m = NULL;
134 	set_constraint_handler_s(h);
135 	assert(memset_s(&b, RSIZE_MAX + 1, 1, 1) != 0);
136 	assert(e > 0);
137 	assert(strcmp(m, "memset_s : smax > RSIZE_MAX") == 0);
138 }
139 
140 /* smax < 0, handler */
141 ATF_TC_WITHOUT_HEAD(smax_lt_zero_handler);
142 ATF_TC_BODY(smax_lt_zero_handler, tc)
143 {
144 	char b;
145 
146 	e = 0;
147 	m = NULL;
148 	set_constraint_handler_s(h);
149 	assert(memset_s(&b, -1, 1, 1) != 0);
150 	assert(e > 0);
151 	assert(strcmp(m, "memset_s : smax > RSIZE_MAX") == 0);
152 }
153 
154 /* n > rmax, handler */
155 ATF_TC_WITHOUT_HEAD(n_gt_rmax_handler);
156 ATF_TC_BODY(n_gt_rmax_handler, tc)
157 {
158 	char b;
159 
160 	e = 0;
161 	m = NULL;
162 	set_constraint_handler_s(h);
163 	assert(memset_s(&b, 1, 1, RSIZE_MAX + 1) != 0);
164 	assert(e > 0);
165 	assert(strcmp(m, "memset_s : n > RSIZE_MAX") == 0);
166 }
167 
168 /* n < 0, handler */
169 ATF_TC_WITHOUT_HEAD(n_lt_zero_handler);
170 ATF_TC_BODY(n_lt_zero_handler, tc)
171 {
172 	char b;
173 
174 	e = 0;
175 	m = NULL;
176 	set_constraint_handler_s(h);
177 	assert(memset_s(&b, 1, 1, -1) != 0);
178 	assert(e > 0);
179 	assert(strcmp(m, "memset_s : n > RSIZE_MAX") == 0);
180 }
181 
182 ATF_TP_ADD_TCS(tp)
183 {
184 	ATF_TP_ADD_TC(tp, null_ptr);
185 	ATF_TP_ADD_TC(tp, smax_gt_rmax);
186 	ATF_TP_ADD_TC(tp, smax_lt_zero);
187 	ATF_TP_ADD_TC(tp, normal);
188 	ATF_TP_ADD_TC(tp, n_gt_rmax);
189 	ATF_TP_ADD_TC(tp, n_lt_zero);
190 	ATF_TP_ADD_TC(tp, n_gt_smax);
191 	ATF_TP_ADD_TC(tp, n_lt_smax);
192 	ATF_TP_ADD_TC(tp, smax_gt_rmax_handler);
193 	ATF_TP_ADD_TC(tp, smax_lt_zero_handler);
194 	ATF_TP_ADD_TC(tp, n_gt_rmax_handler);
195 	ATF_TP_ADD_TC(tp, n_lt_zero_handler);
196 	return (atf_no_error());
197 }
198