xref: /freebsd/contrib/atf/atf-c/error_test.c (revision 8d13bc63c0e1d50bc9e47ac1f26329c999bfecf0)
1 /* Copyright (c) 2008 The NetBSD Foundation, Inc.
2  * 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 NETBSD FOUNDATION, INC. AND
14  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  */
25 
26 #include "atf-c/error.h"
27 
28 #include <errno.h>
29 #include <stdint.h>
30 #include <stdio.h>
31 #include <string.h>
32 
33 #include <atf-c.h>
34 
35 #include "atf-c/defs.h"
36 
37 /* ---------------------------------------------------------------------
38  * Auxiliary functions.
39  * --------------------------------------------------------------------- */
40 
41 static
42 void
43 test_format(const atf_error_t err ATF_DEFS_ATTRIBUTE_UNUSED,
44             char *buf, size_t buflen)
45 {
46     snprintf(buf, buflen, "Test formatting function");
47 }
48 
49 /* ---------------------------------------------------------------------
50  * Tests for the "atf_error" type.
51  * --------------------------------------------------------------------- */
52 
53 ATF_TC(error_new);
54 ATF_TC_HEAD(error_new, tc)
55 {
56     atf_tc_set_md_var(tc, "descr", "Checks the construction of an error "
57                       "object");
58 }
59 ATF_TC_BODY(error_new, tc)
60 {
61     atf_error_t err;
62     int data;
63 
64     err = atf_error_new("test_error", NULL, 0, NULL);
65     ATF_REQUIRE(atf_error_is(err, "test_error"));
66     ATF_REQUIRE(!atf_error_is(err, "unknown_error"));
67     ATF_REQUIRE(atf_error_data(err) == NULL);
68     atf_error_free(err);
69 
70     data = 5;
71     err = atf_error_new("test_data_error", &data, sizeof(data), NULL);
72     ATF_REQUIRE(atf_error_is(err, "test_data_error"));
73     ATF_REQUIRE(!atf_error_is(err, "unknown_error"));
74     ATF_REQUIRE(atf_error_data(err) != NULL);
75     ATF_REQUIRE_EQ(*((const int *)atf_error_data(err)), 5);
76     atf_error_free(err);
77 }
78 
79 ATF_TC(error_new_wo_memory);
80 ATF_TC_HEAD(error_new_wo_memory, tc)
81 {
82     atf_tc_set_md_var(tc, "descr", "Checks that an unavailable memory error "
83                       "raised when constructing an error object "
84                             "is properly converted to the no_memory "
85                             "static error type");
86 }
87 ATF_TC_BODY(error_new_wo_memory, tc)
88 {
89     atf_error_t err;
90     void *invalid;
91 
92     invalid = (void *)1;
93 
94     err = atf_error_new("test_error", invalid, SIZE_MAX, NULL);
95     ATF_REQUIRE(atf_error_is(err, "no_memory"));
96     ATF_REQUIRE(atf_error_data(err) == NULL);
97     atf_error_free(err);
98 }
99 
100 ATF_TC(no_error);
101 ATF_TC_HEAD(no_error, tc)
102 {
103     atf_tc_set_md_var(tc, "descr", "Checks that constructing a non-error "
104                       "object works");
105 }
106 ATF_TC_BODY(no_error, tc)
107 {
108     atf_error_t err;
109 
110     err = atf_no_error();
111     ATF_REQUIRE(!atf_is_error(err));
112 }
113 
114 ATF_TC(is_error);
115 ATF_TC_HEAD(is_error, tc)
116 {
117     atf_tc_set_md_var(tc, "descr", "Checks the is_error method to determine "
118                       "if an error object holds success or an error");
119 }
120 ATF_TC_BODY(is_error, tc)
121 {
122     atf_error_t err;
123 
124     err = atf_no_error();
125     ATF_REQUIRE(!atf_is_error(err));
126 
127     err = atf_error_new("test_error", NULL, 0, NULL);
128     ATF_REQUIRE(atf_is_error(err));
129     atf_error_free(err);
130 }
131 
132 ATF_TC(format);
133 ATF_TC_HEAD(format, tc)
134 {
135     atf_tc_set_md_var(tc, "descr", "Checks the default formatting function "
136                       "and the ability to change it");
137 }
138 ATF_TC_BODY(format, tc)
139 {
140     atf_error_t err;
141     char buf[1024];
142 
143     printf("Testing default formatting function\n");
144     err = atf_error_new("test_error", NULL, 0, NULL);
145     atf_error_format(err, buf, sizeof(buf));
146     printf("Error string is: %s\n", buf);
147     ATF_REQUIRE(strcmp(buf, "Error 'test_error'") == 0);
148     atf_error_free(err);
149 
150     printf("Testing custom formatting function\n");
151     err = atf_error_new("test_error", NULL, 0, test_format);
152     atf_error_format(err, buf, sizeof(buf));
153     printf("Error string is: %s\n", buf);
154     ATF_REQUIRE(strcmp(buf, "Test formatting function") == 0);
155     atf_error_free(err);
156 }
157 
158 /* ---------------------------------------------------------------------
159  * Tests for the "libc" error.
160  * --------------------------------------------------------------------- */
161 
162 ATF_TC(libc_new);
163 ATF_TC_HEAD(libc_new, tc)
164 {
165     atf_tc_set_md_var(tc, "descr", "Checks the construction of libc errors");
166 }
167 ATF_TC_BODY(libc_new, tc)
168 {
169     atf_error_t err;
170 
171     err = atf_libc_error(ENOMEM, "Test message 1");
172     ATF_REQUIRE(atf_error_is(err, "libc"));
173     ATF_REQUIRE_EQ(atf_libc_error_code(err), ENOMEM);
174     ATF_REQUIRE(strcmp(atf_libc_error_msg(err), "Test message 1") == 0);
175     atf_error_free(err);
176 
177     err = atf_libc_error(EPERM, "%s message %d", "Test", 2);
178     ATF_REQUIRE(atf_error_is(err, "libc"));
179     ATF_REQUIRE_EQ(atf_libc_error_code(err), EPERM);
180     ATF_REQUIRE(strcmp(atf_libc_error_msg(err), "Test message 2") == 0);
181     atf_error_free(err);
182 }
183 
184 ATF_TC(libc_format);
185 ATF_TC_HEAD(libc_format, tc)
186 {
187     atf_tc_set_md_var(tc, "descr", "Checks the formatting of libc errors");
188 }
189 ATF_TC_BODY(libc_format, tc)
190 {
191     atf_error_t err;
192     char buf[1024];
193 
194     err = atf_libc_error(ENOMEM, "Test message 1");
195     atf_error_format(err, buf, sizeof(buf));
196     ATF_REQUIRE(strstr(buf, strerror(ENOMEM)) != NULL);
197     ATF_REQUIRE(strstr(buf, "Test message 1") != NULL);
198     atf_error_free(err);
199 
200     err = atf_libc_error(EPERM, "Test message 2");
201     atf_error_format(err, buf, sizeof(buf));
202     ATF_REQUIRE(strstr(buf, strerror(EPERM)) != NULL);
203     ATF_REQUIRE(strstr(buf, "Test message 2") != NULL);
204     atf_error_free(err);
205 
206     err = atf_libc_error(EPERM, "%s message %d", "Test", 3);
207     atf_error_format(err, buf, sizeof(buf));
208     ATF_REQUIRE(strstr(buf, strerror(EPERM)) != NULL);
209     ATF_REQUIRE(strstr(buf, "Test message 3") != NULL);
210     atf_error_free(err);
211 }
212 
213 /* ---------------------------------------------------------------------
214  * Tests for the "no_memory" error.
215  * --------------------------------------------------------------------- */
216 
217 ATF_TC(no_memory_new);
218 ATF_TC_HEAD(no_memory_new, tc)
219 {
220     atf_tc_set_md_var(tc, "descr", "Checks the construction of no_memory "
221                       "errors");
222 }
223 ATF_TC_BODY(no_memory_new, tc)
224 {
225     atf_error_t err;
226 
227     err = atf_no_memory_error();
228     ATF_REQUIRE(atf_error_is(err, "no_memory"));
229     ATF_REQUIRE(atf_error_data(err) == NULL);
230     atf_error_free(err);
231 }
232 
233 ATF_TC(no_memory_format);
234 ATF_TC_HEAD(no_memory_format, tc)
235 {
236     atf_tc_set_md_var(tc, "descr", "Checks the formatting of no_memory "
237                       "errors");
238 }
239 ATF_TC_BODY(no_memory_format, tc)
240 {
241     atf_error_t err;
242     char buf[1024];
243 
244     err = atf_no_memory_error();
245     atf_error_format(err, buf, sizeof(buf));
246     ATF_REQUIRE(strcmp(buf, "Not enough memory") == 0);
247     atf_error_free(err);
248 }
249 
250 ATF_TC(no_memory_twice);
251 ATF_TC_HEAD(no_memory_twice, tc)
252 {
253     atf_tc_set_md_var(tc, "descr", "Checks the construction of no_memory "
254                       "errors multiple times, as this error is initialized "
255                       "statically");
256 }
257 ATF_TC_BODY(no_memory_twice, tc)
258 {
259     {
260         atf_error_t err = atf_no_memory_error();
261         ATF_REQUIRE(atf_error_is(err, "no_memory"));
262         ATF_REQUIRE(atf_error_data(err) == NULL);
263         atf_error_free(err);
264     }
265 
266     {
267         atf_error_t err = atf_no_memory_error();
268         ATF_REQUIRE(atf_error_is(err, "no_memory"));
269         ATF_REQUIRE(atf_error_data(err) == NULL);
270         atf_error_free(err);
271     }
272 }
273 
274 /* ---------------------------------------------------------------------
275  * Main.
276  * --------------------------------------------------------------------- */
277 
278 ATF_TP_ADD_TCS(tp)
279 {
280     /* Add the tests for the "atf_error" type. */
281     ATF_TP_ADD_TC(tp, error_new);
282     ATF_TP_ADD_TC(tp, error_new_wo_memory);
283     ATF_TP_ADD_TC(tp, no_error);
284     ATF_TP_ADD_TC(tp, is_error);
285     ATF_TP_ADD_TC(tp, format);
286 
287     /* Add the tests for the "libc" error. */
288     ATF_TP_ADD_TC(tp, libc_new);
289     ATF_TP_ADD_TC(tp, libc_format);
290 
291     /* Add the tests for the "no_memory" error. */
292     ATF_TP_ADD_TC(tp, no_memory_new);
293     ATF_TP_ADD_TC(tp, no_memory_format);
294     ATF_TP_ADD_TC(tp, no_memory_twice);
295 
296     return atf_no_error();
297 }
298