xref: /freebsd/lib/libc/tests/string/strerror_test.c (revision e1e636193db45630c7881246d25902e57c43d24e)
1 /*-
2  * Copyright (c) 2001 Wes Peters <wes@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <assert.h>
28 #include <errno.h>
29 #include <limits.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
34 #include <atf-c.h>
35 
36 static char buf[64];
37 static char *sret;
38 static int iret;
39 
40 ATF_TC_WITHOUT_HEAD(strerror_unknown_error);
41 ATF_TC_BODY(strerror_unknown_error, tc)
42 {
43 
44 	errno = 0;
45 	sret = strerror(INT_MAX);
46 	snprintf(buf, sizeof(buf), "Unknown error: %d", INT_MAX);
47 	ATF_CHECK(strcmp(sret, buf) == 0);
48 	ATF_CHECK(errno == EINVAL);
49 }
50 
51 ATF_TC_WITHOUT_HEAD(strerror_no_error);
52 ATF_TC_BODY(strerror_no_error, tc)
53 {
54 
55 	errno = 0;
56 	sret = strerror(0);
57 	ATF_CHECK(strcmp(sret, "No error: 0") == 0);
58 	ATF_CHECK(errno == 0);
59 }
60 
61 ATF_TC_WITHOUT_HEAD(strerror_EPERM_test);
62 ATF_TC_BODY(strerror_EPERM_test, tc)
63 {
64 
65 	errno = 0;
66 	sret = strerror(EPERM);
67 	ATF_CHECK(strcmp(sret, "Operation not permitted") == 0);
68 	ATF_CHECK(errno == 0);
69 }
70 
71 ATF_TC_WITHOUT_HEAD(strerror_EPFNOSUPPORT_test);
72 ATF_TC_BODY(strerror_EPFNOSUPPORT_test, tc)
73 {
74 
75 	errno = 0;
76 	sret = strerror(EPFNOSUPPORT);
77 	ATF_CHECK(strcmp(sret, "Protocol family not supported") == 0);
78 	ATF_CHECK(errno == 0);
79 }
80 
81 ATF_TC_WITHOUT_HEAD(strerror_ELAST_test);
82 ATF_TC_BODY(strerror_ELAST_test, tc)
83 {
84 
85 	errno = 0;
86 	sret = strerror(ELAST);
87 	ATF_CHECK(errno == 0);
88 }
89 
90 ATF_TC_WITHOUT_HEAD(strerror_r__unknown_error);
91 ATF_TC_BODY(strerror_r__unknown_error, tc)
92 {
93 
94 	memset(buf, '*', sizeof(buf));
95 	iret = strerror_r(-1, buf, sizeof(buf));
96 	ATF_CHECK(strcmp(buf, "Unknown error: -1") == 0);
97 	ATF_CHECK(iret == EINVAL);
98 }
99 
100 ATF_TC_WITHOUT_HEAD(strerror_r__EPERM_one_byte_short);
101 ATF_TC_BODY(strerror_r__EPERM_one_byte_short, tc)
102 {
103 
104 	memset(buf, '*', sizeof(buf));
105 	/* One byte too short. */
106 	iret = strerror_r(EPERM, buf, strlen("Operation not permitted"));
107 	ATF_CHECK(strcmp(buf, "Operation not permitte") == 0);
108 	ATF_CHECK(iret == ERANGE);
109 }
110 
111 ATF_TC_WITHOUT_HEAD(strerror_r__EPERM_unknown_error_one_byte_short);
112 ATF_TC_BODY(strerror_r__EPERM_unknown_error_one_byte_short, tc)
113 {
114 
115 	memset(buf, '*', sizeof(buf));
116 	/* One byte too short. */
117 	iret = strerror_r(-1, buf, strlen("Unknown error: -1"));
118 	ATF_CHECK(strcmp(buf, "Unknown error: -") == 0);
119 	ATF_CHECK(iret == EINVAL);
120 }
121 
122 ATF_TC_WITHOUT_HEAD(strerror_r__EPERM_unknown_error_two_bytes_short);
123 ATF_TC_BODY(strerror_r__EPERM_unknown_error_two_bytes_short, tc)
124 {
125 
126 	memset(buf, '*', sizeof(buf));
127 	/* Two bytes too short. */
128 	iret = strerror_r(-2, buf, strlen("Unknown error: -2") - 1);
129 	ATF_CHECK(strcmp(buf, "Unknown error: ") == 0);
130 	ATF_CHECK(iret == EINVAL);
131 }
132 
133 ATF_TC_WITHOUT_HEAD(strerror_r__EPERM_unknown_error_three_bytes_short);
134 ATF_TC_BODY(strerror_r__EPERM_unknown_error_three_bytes_short, tc)
135 {
136 
137 	memset(buf, '*', sizeof(buf));
138 	/* Three bytes too short. */
139 	iret = strerror_r(-2, buf, strlen("Unknown error: -2") - 2);
140 	ATF_CHECK(strcmp(buf, "Unknown error:") == 0);
141 	ATF_CHECK(iret == EINVAL);
142 }
143 
144 ATF_TC_WITHOUT_HEAD(strerror_r__EPERM_unknown_error_12345_one_byte_short);
145 ATF_TC_BODY(strerror_r__EPERM_unknown_error_12345_one_byte_short, tc)
146 {
147 
148 	memset(buf, '*', sizeof(buf));
149 	/* One byte too short. */
150 	iret = strerror_r(12345, buf, strlen("Unknown error: 12345"));
151 	ATF_CHECK(strcmp(buf, "Unknown error: 1234") == 0);
152 	ATF_CHECK(iret == EINVAL);
153 }
154 
155 ATF_TC_WITHOUT_HEAD(strerror_r__no_error);
156 ATF_TC_BODY(strerror_r__no_error, tc)
157 {
158 
159 	memset(buf, '*', sizeof(buf));
160 	iret = strerror_r(0, buf, sizeof(buf));
161 	ATF_CHECK(strcmp(buf, "No error: 0") == 0);
162 	ATF_CHECK(iret == 0);
163 }
164 
165 ATF_TC_WITHOUT_HEAD(strerror_r__EDEADLK);
166 ATF_TC_BODY(strerror_r__EDEADLK, tc)
167 {
168 
169 	memset(buf, '*', sizeof(buf));
170 	iret = strerror_r(EDEADLK, buf, sizeof(buf));
171 	ATF_CHECK(strcmp(buf, "Resource deadlock avoided") == 0);
172 	ATF_CHECK(iret == 0);
173 }
174 
175 ATF_TC_WITHOUT_HEAD(strerror_r__EPROCLIM);
176 ATF_TC_BODY(strerror_r__EPROCLIM, tc)
177 {
178 
179 	memset(buf, '*', sizeof(buf));
180 	iret = strerror_r(EPROCLIM, buf, sizeof(buf));
181 	ATF_CHECK(strcmp(buf, "Too many processes") == 0);
182 	ATF_CHECK(iret == 0);
183 }
184 
185 ATF_TP_ADD_TCS(tp)
186 {
187 
188 	ATF_TP_ADD_TC(tp, strerror_unknown_error);
189 	ATF_TP_ADD_TC(tp, strerror_no_error);
190 	ATF_TP_ADD_TC(tp, strerror_EPERM_test);
191 	ATF_TP_ADD_TC(tp, strerror_EPFNOSUPPORT_test);
192 	ATF_TP_ADD_TC(tp, strerror_ELAST_test);
193 	ATF_TP_ADD_TC(tp, strerror_r__unknown_error);
194 	ATF_TP_ADD_TC(tp, strerror_r__EPERM_one_byte_short);
195 	ATF_TP_ADD_TC(tp, strerror_r__EPERM_unknown_error_one_byte_short);
196 	ATF_TP_ADD_TC(tp, strerror_r__EPERM_unknown_error_two_bytes_short);
197 	ATF_TP_ADD_TC(tp, strerror_r__EPERM_unknown_error_three_bytes_short);
198 	ATF_TP_ADD_TC(tp, strerror_r__EPERM_unknown_error_12345_one_byte_short);
199 	ATF_TP_ADD_TC(tp, strerror_r__no_error);
200 	ATF_TP_ADD_TC(tp, strerror_r__EDEADLK);
201 	ATF_TP_ADD_TC(tp, strerror_r__EPROCLIM);
202 
203 	return (atf_no_error());
204 }
205