1 /* Copyright (c) 2008 The NetBSD Foundation, Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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. */
26 #include "atf-c/error.h"
35 #include "atf-c/defs.h"
37 /* ---------------------------------------------------------------------
38 * Auxiliary functions.
39 * --------------------------------------------------------------------- */
43 test_format(const atf_error_t err ATF_DEFS_ATTRIBUTE_UNUSED,
44 char *buf, size_t buflen)
46 snprintf(buf, buflen, "Test formatting function");
49 /* ---------------------------------------------------------------------
50 * Tests for the "atf_error" type.
51 * --------------------------------------------------------------------- */
54 ATF_TC_HEAD(error_new, tc)
56 atf_tc_set_md_var(tc, "descr", "Checks the construction of an error "
59 ATF_TC_BODY(error_new, tc)
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);
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);
79 ATF_TC(error_new_wo_memory);
80 ATF_TC_HEAD(error_new_wo_memory, tc)
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 "
87 ATF_TC_BODY(error_new_wo_memory, tc)
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);
101 ATF_TC_HEAD(no_error, tc)
103 atf_tc_set_md_var(tc, "descr", "Checks that constructing a non-error "
106 ATF_TC_BODY(no_error, tc)
110 err = atf_no_error();
111 ATF_REQUIRE(!atf_is_error(err));
115 ATF_TC_HEAD(is_error, tc)
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");
120 ATF_TC_BODY(is_error, tc)
124 err = atf_no_error();
125 ATF_REQUIRE(!atf_is_error(err));
127 err = atf_error_new("test_error", NULL, 0, NULL);
128 ATF_REQUIRE(atf_is_error(err));
133 ATF_TC_HEAD(format, tc)
135 atf_tc_set_md_var(tc, "descr", "Checks the default formatting function "
136 "and the ability to change it");
138 ATF_TC_BODY(format, tc)
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);
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);
158 /* ---------------------------------------------------------------------
159 * Tests for the "libc" error.
160 * --------------------------------------------------------------------- */
163 ATF_TC_HEAD(libc_new, tc)
165 atf_tc_set_md_var(tc, "descr", "Checks the construction of libc errors");
167 ATF_TC_BODY(libc_new, tc)
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);
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);
185 ATF_TC_HEAD(libc_format, tc)
187 atf_tc_set_md_var(tc, "descr", "Checks the formatting of libc errors");
189 ATF_TC_BODY(libc_format, tc)
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);
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);
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);
213 /* ---------------------------------------------------------------------
214 * Tests for the "no_memory" error.
215 * --------------------------------------------------------------------- */
217 ATF_TC(no_memory_new);
218 ATF_TC_HEAD(no_memory_new, tc)
220 atf_tc_set_md_var(tc, "descr", "Checks the construction of no_memory "
223 ATF_TC_BODY(no_memory_new, tc)
227 err = atf_no_memory_error();
228 ATF_REQUIRE(atf_error_is(err, "no_memory"));
229 ATF_REQUIRE(atf_error_data(err) == NULL);
233 ATF_TC(no_memory_format);
234 ATF_TC_HEAD(no_memory_format, tc)
236 atf_tc_set_md_var(tc, "descr", "Checks the formatting of no_memory "
239 ATF_TC_BODY(no_memory_format, tc)
244 err = atf_no_memory_error();
245 atf_error_format(err, buf, sizeof(buf));
246 ATF_REQUIRE(strcmp(buf, "Not enough memory") == 0);
250 ATF_TC(no_memory_twice);
251 ATF_TC_HEAD(no_memory_twice, tc)
253 atf_tc_set_md_var(tc, "descr", "Checks the construction of no_memory "
254 "errors multiple times, as this error is initialized "
257 ATF_TC_BODY(no_memory_twice, tc)
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);
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);
274 /* ---------------------------------------------------------------------
276 * --------------------------------------------------------------------- */
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);
287 /* Add the tests for the "libc" error. */
288 ATF_TP_ADD_TC(tp, libc_new);
289 ATF_TP_ADD_TC(tp, libc_format);
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);
296 return atf_no_error();