1 /* $NetBSD: error_test.c,v 1.3 2014/12/10 04:38:03 christos Exp $ */
4 * Automated Testing Framework (atf)
6 * Copyright (c) 2008 The NetBSD Foundation, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
19 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
29 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include "atf-c/defs.h"
40 #include "atf-c/error.h"
42 #include "detail/test_helpers.h"
44 /* ---------------------------------------------------------------------
45 * Auxiliary functions.
46 * --------------------------------------------------------------------- */
50 test_format(const atf_error_t err ATF_DEFS_ATTRIBUTE_UNUSED
,
51 char *buf
, size_t buflen
)
53 snprintf(buf
, buflen
, "Test formatting function");
56 /* ---------------------------------------------------------------------
57 * Tests for the "atf_error" type.
58 * --------------------------------------------------------------------- */
61 ATF_TC_HEAD(error_new
, tc
)
63 atf_tc_set_md_var(tc
, "descr", "Checks the construction of an error "
66 ATF_TC_BODY(error_new
, tc
)
71 err
= atf_error_new("test_error", NULL
, 0, NULL
);
72 ATF_REQUIRE(atf_error_is(err
, "test_error"));
73 ATF_REQUIRE(!atf_error_is(err
, "unknown_error"));
74 ATF_REQUIRE(atf_error_data(err
) == NULL
);
78 err
= atf_error_new("test_data_error", &data
, sizeof(data
), NULL
);
79 ATF_REQUIRE(atf_error_is(err
, "test_data_error"));
80 ATF_REQUIRE(!atf_error_is(err
, "unknown_error"));
81 ATF_REQUIRE(atf_error_data(err
) != NULL
);
82 ATF_REQUIRE_EQ(*((const int *)atf_error_data(err
)), 5);
86 ATF_TC(error_new_wo_memory
);
87 ATF_TC_HEAD(error_new_wo_memory
, tc
)
89 atf_tc_set_md_var(tc
, "descr", "Checks that an unavailable memory error "
90 "raised when constructing an error object "
91 "is properly converted to the no_memory "
94 ATF_TC_BODY(error_new_wo_memory
, tc
)
101 err
= atf_error_new("test_error", invalid
, SIZE_MAX
, NULL
);
102 ATF_REQUIRE(atf_error_is(err
, "no_memory"));
103 ATF_REQUIRE(atf_error_data(err
) == NULL
);
108 ATF_TC_HEAD(no_error
, tc
)
110 atf_tc_set_md_var(tc
, "descr", "Checks that constructing a non-error "
113 ATF_TC_BODY(no_error
, tc
)
117 err
= atf_no_error();
118 ATF_REQUIRE(!atf_is_error(err
));
122 ATF_TC_HEAD(is_error
, tc
)
124 atf_tc_set_md_var(tc
, "descr", "Checks the is_error method to determine "
125 "if an error object holds success or an error");
127 ATF_TC_BODY(is_error
, tc
)
131 err
= atf_no_error();
132 ATF_REQUIRE(!atf_is_error(err
));
134 err
= atf_error_new("test_error", NULL
, 0, NULL
);
135 ATF_REQUIRE(atf_is_error(err
));
140 ATF_TC_HEAD(format
, tc
)
142 atf_tc_set_md_var(tc
, "descr", "Checks the default formatting function "
143 "and the ability to change it");
145 ATF_TC_BODY(format
, tc
)
150 printf("Testing default formatting function\n");
151 err
= atf_error_new("test_error", NULL
, 0, NULL
);
152 atf_error_format(err
, buf
, sizeof(buf
));
153 printf("Error string is: %s\n", buf
);
154 ATF_REQUIRE(strcmp(buf
, "Error 'test_error'") == 0);
157 printf("Testing custom formatting function\n");
158 err
= atf_error_new("test_error", NULL
, 0, test_format
);
159 atf_error_format(err
, buf
, sizeof(buf
));
160 printf("Error string is: %s\n", buf
);
161 ATF_REQUIRE(strcmp(buf
, "Test formatting function") == 0);
165 /* ---------------------------------------------------------------------
166 * Tests for the "libc" error.
167 * --------------------------------------------------------------------- */
170 ATF_TC_HEAD(libc_new
, tc
)
172 atf_tc_set_md_var(tc
, "descr", "Checks the construction of libc errors");
174 ATF_TC_BODY(libc_new
, tc
)
178 err
= atf_libc_error(ENOMEM
, "Test message 1");
179 ATF_REQUIRE(atf_error_is(err
, "libc"));
180 ATF_REQUIRE_EQ(atf_libc_error_code(err
), ENOMEM
);
181 ATF_REQUIRE(strcmp(atf_libc_error_msg(err
), "Test message 1") == 0);
184 err
= atf_libc_error(EPERM
, "%s message %d", "Test", 2);
185 ATF_REQUIRE(atf_error_is(err
, "libc"));
186 ATF_REQUIRE_EQ(atf_libc_error_code(err
), EPERM
);
187 ATF_REQUIRE(strcmp(atf_libc_error_msg(err
), "Test message 2") == 0);
192 ATF_TC_HEAD(libc_format
, tc
)
194 atf_tc_set_md_var(tc
, "descr", "Checks the formatting of libc errors");
196 ATF_TC_BODY(libc_format
, tc
)
201 err
= atf_libc_error(ENOMEM
, "Test message 1");
202 atf_error_format(err
, buf
, sizeof(buf
));
203 ATF_REQUIRE(strstr(buf
, strerror(ENOMEM
)) != NULL
);
204 ATF_REQUIRE(strstr(buf
, "Test message 1") != NULL
);
207 err
= atf_libc_error(EPERM
, "Test message 2");
208 atf_error_format(err
, buf
, sizeof(buf
));
209 ATF_REQUIRE(strstr(buf
, strerror(EPERM
)) != NULL
);
210 ATF_REQUIRE(strstr(buf
, "Test message 2") != NULL
);
213 err
= atf_libc_error(EPERM
, "%s message %d", "Test", 3);
214 atf_error_format(err
, buf
, sizeof(buf
));
215 ATF_REQUIRE(strstr(buf
, strerror(EPERM
)) != NULL
);
216 ATF_REQUIRE(strstr(buf
, "Test message 3") != NULL
);
220 /* ---------------------------------------------------------------------
221 * Tests for the "no_memory" error.
222 * --------------------------------------------------------------------- */
224 ATF_TC(no_memory_new
);
225 ATF_TC_HEAD(no_memory_new
, tc
)
227 atf_tc_set_md_var(tc
, "descr", "Checks the construction of no_memory "
230 ATF_TC_BODY(no_memory_new
, tc
)
234 err
= atf_no_memory_error();
235 ATF_REQUIRE(atf_error_is(err
, "no_memory"));
236 ATF_REQUIRE(atf_error_data(err
) == NULL
);
240 ATF_TC(no_memory_format
);
241 ATF_TC_HEAD(no_memory_format
, tc
)
243 atf_tc_set_md_var(tc
, "descr", "Checks the formatting of no_memory "
246 ATF_TC_BODY(no_memory_format
, tc
)
251 err
= atf_no_memory_error();
252 atf_error_format(err
, buf
, sizeof(buf
));
253 ATF_REQUIRE(strcmp(buf
, "Not enough memory") == 0);
257 ATF_TC(no_memory_twice
);
258 ATF_TC_HEAD(no_memory_twice
, tc
)
260 atf_tc_set_md_var(tc
, "descr", "Checks the construction of no_memory "
261 "errors multiple times, as this error is initialized "
264 ATF_TC_BODY(no_memory_twice
, tc
)
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 atf_error_t err
= atf_no_memory_error();
275 ATF_REQUIRE(atf_error_is(err
, "no_memory"));
276 ATF_REQUIRE(atf_error_data(err
) == NULL
);
281 /* ---------------------------------------------------------------------
282 * Tests cases for the header file.
283 * --------------------------------------------------------------------- */
285 HEADER_TC(include
, "atf-c/error.h");
286 HEADER_TC(include_fwd
, "atf-c/error_fwd.h");
288 /* ---------------------------------------------------------------------
290 * --------------------------------------------------------------------- */
294 /* Add the tests for the "atf_error" type. */
295 ATF_TP_ADD_TC(tp
, error_new
);
296 ATF_TP_ADD_TC(tp
, error_new_wo_memory
);
297 ATF_TP_ADD_TC(tp
, no_error
);
298 ATF_TP_ADD_TC(tp
, is_error
);
299 ATF_TP_ADD_TC(tp
, format
);
301 /* Add the tests for the "libc" error. */
302 ATF_TP_ADD_TC(tp
, libc_new
);
303 ATF_TP_ADD_TC(tp
, libc_format
);
305 /* Add the tests for the "no_memory" error. */
306 ATF_TP_ADD_TC(tp
, no_memory_new
);
307 ATF_TP_ADD_TC(tp
, no_memory_format
);
308 ATF_TP_ADD_TC(tp
, no_memory_twice
);
310 /* Add the test cases for the header file. */
311 ATF_TP_ADD_TC(tp
, include
);
312 ATF_TP_ADD_TC(tp
, include_fwd
);
314 return atf_no_error();