1 /* $NetBSD: t_memset.c,v 1.4 2015/09/11 09:25:52 martin Exp $ */
2
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jukka Ruohonen.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_memset.c,v 1.4 2015/09/11 09:25:52 martin Exp $");
33
34 #include <sys/stat.h>
35
36 #include <atf-c.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40
41 static long page = 0;
42 static void fill(char *, size_t, char);
43 static bool check(char *, size_t, char);
44
45 int zero; /* always zero, but the compiler does not know */
46
47 ATF_TC(memset_array);
ATF_TC_HEAD(memset_array,tc)48 ATF_TC_HEAD(memset_array, tc)
49 {
50 atf_tc_set_md_var(tc, "descr", "Test memset(3) with arrays");
51 }
52
ATF_TC_BODY(memset_array,tc)53 ATF_TC_BODY(memset_array, tc)
54 {
55 char buf[1024];
56
57 (void)memset(buf, 0, sizeof(buf));
58
59 if (check(buf, sizeof(buf), 0) != true)
60 atf_tc_fail("memset(3) did not fill a static buffer");
61
62 (void)memset(buf, 'x', sizeof(buf));
63
64 if (check(buf, sizeof(buf), 'x') != true)
65 atf_tc_fail("memset(3) did not fill a static buffer");
66 }
67
68 ATF_TC(memset_return);
ATF_TC_HEAD(memset_return,tc)69 ATF_TC_HEAD(memset_return, tc)
70 {
71 atf_tc_set_md_var(tc, "descr", "Test memset(3) return value");
72 }
73
ATF_TC_BODY(memset_return,tc)74 ATF_TC_BODY(memset_return, tc)
75 {
76 char *b = (char *)0x1;
77 char c[2];
78 ATF_REQUIRE_EQ(memset(b, 0, 0), b);
79 ATF_REQUIRE_EQ(memset(c, 2, sizeof(c)), c);
80 }
81
82 ATF_TC(memset_basic);
ATF_TC_HEAD(memset_basic,tc)83 ATF_TC_HEAD(memset_basic, tc)
84 {
85 atf_tc_set_md_var(tc, "descr", "A basic test of memset(3)");
86 }
87
ATF_TC_BODY(memset_basic,tc)88 ATF_TC_BODY(memset_basic, tc)
89 {
90 char *buf, *ret;
91
92 buf = malloc(page);
93 ret = malloc(page);
94
95 ATF_REQUIRE(buf != NULL);
96 ATF_REQUIRE(ret != NULL);
97
98 fill(ret, page, 0);
99 memset(buf, 0, page);
100
101 ATF_REQUIRE(memcmp(ret, buf, page) == 0);
102
103 fill(ret, page, 'x');
104 memset(buf, 'x', page);
105
106 ATF_REQUIRE(memcmp(ret, buf, page) == 0);
107
108 free(buf);
109 free(ret);
110 }
111
112 ATF_TC(memset_nonzero);
ATF_TC_HEAD(memset_nonzero,tc)113 ATF_TC_HEAD(memset_nonzero, tc)
114 {
115 atf_tc_set_md_var(tc, "descr", "Test memset(3) with non-zero params");
116 }
117
ATF_TC_BODY(memset_nonzero,tc)118 ATF_TC_BODY(memset_nonzero, tc)
119 {
120 const size_t n = 0x7f;
121 char *buf;
122 size_t i;
123
124 buf = malloc(page);
125 ATF_REQUIRE(buf != NULL);
126
127 for (i = 0x21; i < n; i++) {
128
129 (void)memset(buf, i, page);
130
131 if (check(buf, page, i) != true)
132 atf_tc_fail("memset(3) did not fill properly");
133 }
134
135 free(buf);
136 }
137
138 ATF_TC(memset_zero_size);
139
ATF_TC_HEAD(memset_zero_size,tc)140 ATF_TC_HEAD(memset_zero_size, tc)
141 {
142 atf_tc_set_md_var(tc, "descr", "Test memset(3) with zero size");
143 }
144
ATF_TC_BODY(memset_zero_size,tc)145 ATF_TC_BODY(memset_zero_size, tc)
146 {
147 char buf[1024];
148
149 (void)memset(buf, 'x', sizeof(buf));
150
151 if (check(buf, sizeof(buf), 'x') != true)
152 atf_tc_fail("memset(3) did not fill a static buffer");
153
154 (void)memset(buf+sizeof(buf)/2, 0, zero);
155
156 if (check(buf, sizeof(buf), 'x') != true)
157 atf_tc_fail("memset(3) with 0 size did change the buffer");
158 }
159
160 ATF_TC(bzero_zero_size);
161
ATF_TC_HEAD(bzero_zero_size,tc)162 ATF_TC_HEAD(bzero_zero_size, tc)
163 {
164 atf_tc_set_md_var(tc, "descr", "Test bzero(3) with zero size");
165 }
166
ATF_TC_BODY(bzero_zero_size,tc)167 ATF_TC_BODY(bzero_zero_size, tc)
168 {
169 char buf[1024];
170
171 (void)memset(buf, 'x', sizeof(buf));
172
173 if (check(buf, sizeof(buf), 'x') != true)
174 atf_tc_fail("memset(3) did not fill a static buffer");
175
176 (void)bzero(buf+sizeof(buf)/2, zero);
177
178 if (check(buf, sizeof(buf), 'x') != true)
179 atf_tc_fail("bzero(3) with 0 size did change the buffer");
180 }
181
182 ATF_TC(memset_struct);
ATF_TC_HEAD(memset_struct,tc)183 ATF_TC_HEAD(memset_struct, tc)
184 {
185 atf_tc_set_md_var(tc, "descr", "Test memset(3) with a structure");
186 }
187
ATF_TC_BODY(memset_struct,tc)188 ATF_TC_BODY(memset_struct, tc)
189 {
190 struct stat st;
191
192 st.st_dev = 0;
193 st.st_ino = 1;
194 st.st_mode = 2;
195 st.st_nlink = 3;
196 st.st_uid = 4;
197 st.st_gid = 5;
198 st.st_rdev = 6;
199 st.st_size = 7;
200 st.st_atime = 8;
201 st.st_mtime = 9;
202
203 (void)memset(&st, 0, sizeof(struct stat));
204
205 ATF_CHECK(st.st_dev == 0);
206 ATF_CHECK(st.st_ino == 0);
207 ATF_CHECK(st.st_mode == 0);
208 ATF_CHECK(st.st_nlink == 0);
209 ATF_CHECK(st.st_uid == 0);
210 ATF_CHECK(st.st_gid == 0);
211 ATF_CHECK(st.st_rdev == 0);
212 ATF_CHECK(st.st_size == 0);
213 ATF_CHECK(st.st_atime == 0);
214 ATF_CHECK(st.st_mtime == 0);
215 }
216
217 static void
fill(char * buf,size_t len,char x)218 fill(char *buf, size_t len, char x)
219 {
220 size_t i;
221
222 for (i = 0; i < len; i++)
223 buf[i] = x;
224 }
225
226 static bool
check(char * buf,size_t len,char x)227 check(char *buf, size_t len, char x)
228 {
229 size_t i;
230
231 for (i = 0; i < len; i++) {
232
233 if (buf[i] != x)
234 return false;
235 }
236
237 return true;
238 }
239
ATF_TP_ADD_TCS(tp)240 ATF_TP_ADD_TCS(tp)
241 {
242
243 page = sysconf(_SC_PAGESIZE);
244 ATF_REQUIRE(page >= 0);
245
246 ATF_TP_ADD_TC(tp, memset_array);
247 ATF_TP_ADD_TC(tp, memset_basic);
248 ATF_TP_ADD_TC(tp, memset_nonzero);
249 ATF_TP_ADD_TC(tp, memset_struct);
250 ATF_TP_ADD_TC(tp, memset_return);
251 ATF_TP_ADD_TC(tp, memset_zero_size);
252 ATF_TP_ADD_TC(tp, bzero_zero_size);
253
254 return atf_no_error();
255 }
256