xref: /freebsd/lib/libc/tests/string/strerror_test.c (revision ebacd8013fe5f7fdf9f6a5b286f6680dd2891036)
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  * $FreeBSD$
27  */
28 
29 #include <assert.h>
30 #include <errno.h>
31 #include <limits.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 #include <atf-c.h>
37 
38 static char buf[64];
39 static char *sret;
40 static int iret;
41 
42 ATF_TC_WITHOUT_HEAD(strerror_unknown_error);
43 ATF_TC_BODY(strerror_unknown_error, tc)
44 {
45 
46 	errno = 0;
47 	sret = strerror(INT_MAX);
48 	snprintf(buf, sizeof(buf), "Unknown error: %d", INT_MAX);
49 	ATF_CHECK(strcmp(sret, buf) == 0);
50 	ATF_CHECK(errno == EINVAL);
51 }
52 
53 ATF_TC_WITHOUT_HEAD(strerror_no_error);
54 ATF_TC_BODY(strerror_no_error, tc)
55 {
56 
57 	errno = 0;
58 	sret = strerror(0);
59 	ATF_CHECK(strcmp(sret, "No error: 0") == 0);
60 	ATF_CHECK(errno == 0);
61 }
62 
63 ATF_TC_WITHOUT_HEAD(strerror_EPERM_test);
64 ATF_TC_BODY(strerror_EPERM_test, tc)
65 {
66 
67 	errno = 0;
68 	sret = strerror(EPERM);
69 	ATF_CHECK(strcmp(sret, "Operation not permitted") == 0);
70 	ATF_CHECK(errno == 0);
71 }
72 
73 ATF_TC_WITHOUT_HEAD(strerror_EPFNOSUPPORT_test);
74 ATF_TC_BODY(strerror_EPFNOSUPPORT_test, tc)
75 {
76 
77 	errno = 0;
78 	sret = strerror(EPFNOSUPPORT);
79 	ATF_CHECK(strcmp(sret, "Protocol family not supported") == 0);
80 	ATF_CHECK(errno == 0);
81 }
82 
83 ATF_TC_WITHOUT_HEAD(strerror_ELAST_test);
84 ATF_TC_BODY(strerror_ELAST_test, tc)
85 {
86 
87 	errno = 0;
88 	sret = strerror(ELAST);
89 	ATF_CHECK(errno == 0);
90 }
91 
92 ATF_TC_WITHOUT_HEAD(strerror_r__unknown_error);
93 ATF_TC_BODY(strerror_r__unknown_error, tc)
94 {
95 
96 	memset(buf, '*', sizeof(buf));
97 	iret = strerror_r(-1, buf, sizeof(buf));
98 	ATF_CHECK(strcmp(buf, "Unknown error: -1") == 0);
99 	ATF_CHECK(iret == EINVAL);
100 }
101 
102 ATF_TC_WITHOUT_HEAD(strerror_r__EPERM_one_byte_short);
103 ATF_TC_BODY(strerror_r__EPERM_one_byte_short, tc)
104 {
105 
106 	memset(buf, '*', sizeof(buf));
107 	/* One byte too short. */
108 	iret = strerror_r(EPERM, buf, strlen("Operation not permitted"));
109 	ATF_CHECK(strcmp(buf, "Operation not permitte") == 0);
110 	ATF_CHECK(iret == ERANGE);
111 }
112 
113 ATF_TC_WITHOUT_HEAD(strerror_r__EPERM_unknown_error_one_byte_short);
114 ATF_TC_BODY(strerror_r__EPERM_unknown_error_one_byte_short, tc)
115 {
116 
117 	memset(buf, '*', sizeof(buf));
118 	/* One byte too short. */
119 	iret = strerror_r(-1, buf, strlen("Unknown error: -1"));
120 	ATF_CHECK(strcmp(buf, "Unknown error: -") == 0);
121 	ATF_CHECK(iret == EINVAL);
122 }
123 
124 ATF_TC_WITHOUT_HEAD(strerror_r__EPERM_unknown_error_two_bytes_short);
125 ATF_TC_BODY(strerror_r__EPERM_unknown_error_two_bytes_short, tc)
126 {
127 
128 	memset(buf, '*', sizeof(buf));
129 	/* Two bytes too short. */
130 	iret = strerror_r(-2, buf, strlen("Unknown error: -2") - 1);
131 	ATF_CHECK(strcmp(buf, "Unknown error: ") == 0);
132 	ATF_CHECK(iret == EINVAL);
133 }
134 
135 ATF_TC_WITHOUT_HEAD(strerror_r__EPERM_unknown_error_three_bytes_short);
136 ATF_TC_BODY(strerror_r__EPERM_unknown_error_three_bytes_short, tc)
137 {
138 
139 	memset(buf, '*', sizeof(buf));
140 	/* Three bytes too short. */
141 	iret = strerror_r(-2, buf, strlen("Unknown error: -2") - 2);
142 	ATF_CHECK(strcmp(buf, "Unknown error:") == 0);
143 	ATF_CHECK(iret == EINVAL);
144 }
145 
146 ATF_TC_WITHOUT_HEAD(strerror_r__EPERM_unknown_error_12345_one_byte_short);
147 ATF_TC_BODY(strerror_r__EPERM_unknown_error_12345_one_byte_short, tc)
148 {
149 
150 	memset(buf, '*', sizeof(buf));
151 	/* One byte too short. */
152 	iret = strerror_r(12345, buf, strlen("Unknown error: 12345"));
153 	ATF_CHECK(strcmp(buf, "Unknown error: 1234") == 0);
154 	ATF_CHECK(iret == EINVAL);
155 }
156 
157 ATF_TC_WITHOUT_HEAD(strerror_r__no_error);
158 ATF_TC_BODY(strerror_r__no_error, tc)
159 {
160 
161 	memset(buf, '*', sizeof(buf));
162 	iret = strerror_r(0, buf, sizeof(buf));
163 	ATF_CHECK(strcmp(buf, "No error: 0") == 0);
164 	ATF_CHECK(iret == 0);
165 }
166 
167 ATF_TC_WITHOUT_HEAD(strerror_r__EDEADLK);
168 ATF_TC_BODY(strerror_r__EDEADLK, tc)
169 {
170 
171 	memset(buf, '*', sizeof(buf));
172 	iret = strerror_r(EDEADLK, buf, sizeof(buf));
173 	ATF_CHECK(strcmp(buf, "Resource deadlock avoided") == 0);
174 	ATF_CHECK(iret == 0);
175 }
176 
177 ATF_TC_WITHOUT_HEAD(strerror_r__EPROCLIM);
178 ATF_TC_BODY(strerror_r__EPROCLIM, tc)
179 {
180 
181 	memset(buf, '*', sizeof(buf));
182 	iret = strerror_r(EPROCLIM, buf, sizeof(buf));
183 	ATF_CHECK(strcmp(buf, "Too many processes") == 0);
184 	ATF_CHECK(iret == 0);
185 }
186 
187 ATF_TP_ADD_TCS(tp)
188 {
189 
190 	ATF_TP_ADD_TC(tp, strerror_unknown_error);
191 	ATF_TP_ADD_TC(tp, strerror_no_error);
192 	ATF_TP_ADD_TC(tp, strerror_EPERM_test);
193 	ATF_TP_ADD_TC(tp, strerror_EPFNOSUPPORT_test);
194 	ATF_TP_ADD_TC(tp, strerror_ELAST_test);
195 	ATF_TP_ADD_TC(tp, strerror_r__unknown_error);
196 	ATF_TP_ADD_TC(tp, strerror_r__EPERM_one_byte_short);
197 	ATF_TP_ADD_TC(tp, strerror_r__EPERM_unknown_error_one_byte_short);
198 	ATF_TP_ADD_TC(tp, strerror_r__EPERM_unknown_error_two_bytes_short);
199 	ATF_TP_ADD_TC(tp, strerror_r__EPERM_unknown_error_three_bytes_short);
200 	ATF_TP_ADD_TC(tp, strerror_r__EPERM_unknown_error_12345_one_byte_short);
201 	ATF_TP_ADD_TC(tp, strerror_r__no_error);
202 	ATF_TP_ADD_TC(tp, strerror_r__EDEADLK);
203 	ATF_TP_ADD_TC(tp, strerror_r__EPROCLIM);
204 
205 	return (atf_no_error());
206 }
207