2 * Automated Testing Framework (atf)
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include "atf-c/defs.h"
38 #include "atf-c/error.h"
40 #include "detail/test_helpers.h"
42 /* ---------------------------------------------------------------------
43 * Auxiliary functions.
44 * --------------------------------------------------------------------- */
48 test_format(const atf_error_t err ATF_DEFS_ATTRIBUTE_UNUSED
,
49 char *buf
, size_t buflen
)
51 snprintf(buf
, buflen
, "Test formatting function");
54 /* ---------------------------------------------------------------------
55 * Tests for the "atf_error" type.
56 * --------------------------------------------------------------------- */
59 ATF_TC_HEAD(error_new
, tc
)
61 atf_tc_set_md_var(tc
, "descr", "Checks the construction of an error "
64 ATF_TC_BODY(error_new
, tc
)
69 err
= atf_error_new("test_error", NULL
, 0, NULL
);
70 ATF_REQUIRE(atf_error_is(err
, "test_error"));
71 ATF_REQUIRE(!atf_error_is(err
, "unknown_error"));
72 ATF_REQUIRE(atf_error_data(err
) == NULL
);
76 err
= atf_error_new("test_data_error", &data
, sizeof(data
), NULL
);
77 ATF_REQUIRE(atf_error_is(err
, "test_data_error"));
78 ATF_REQUIRE(!atf_error_is(err
, "unknown_error"));
79 ATF_REQUIRE(atf_error_data(err
) != NULL
);
80 ATF_REQUIRE_EQ(*((const int *)atf_error_data(err
)), 5);
84 ATF_TC(error_new_wo_memory
);
85 ATF_TC_HEAD(error_new_wo_memory
, tc
)
87 atf_tc_set_md_var(tc
, "descr", "Checks that an unavailable memory error "
88 "raised when constructing an error object "
89 "is properly converted to the no_memory "
92 ATF_TC_BODY(error_new_wo_memory
, tc
)
99 err
= atf_error_new("test_error", invalid
, SIZE_MAX
, NULL
);
100 ATF_REQUIRE(atf_error_is(err
, "no_memory"));
101 ATF_REQUIRE(atf_error_data(err
) == NULL
);
106 ATF_TC_HEAD(no_error
, tc
)
108 atf_tc_set_md_var(tc
, "descr", "Checks that constructing a non-error "
111 ATF_TC_BODY(no_error
, tc
)
115 err
= atf_no_error();
116 ATF_REQUIRE(!atf_is_error(err
));
120 ATF_TC_HEAD(is_error
, tc
)
122 atf_tc_set_md_var(tc
, "descr", "Checks the is_error method to determine "
123 "if an error object holds success or an error");
125 ATF_TC_BODY(is_error
, tc
)
129 err
= atf_no_error();
130 ATF_REQUIRE(!atf_is_error(err
));
132 err
= atf_error_new("test_error", NULL
, 0, NULL
);
133 ATF_REQUIRE(atf_is_error(err
));
138 ATF_TC_HEAD(format
, tc
)
140 atf_tc_set_md_var(tc
, "descr", "Checks the default formatting function "
141 "and the ability to change it");
143 ATF_TC_BODY(format
, tc
)
148 printf("Testing default formatting function\n");
149 err
= atf_error_new("test_error", NULL
, 0, NULL
);
150 atf_error_format(err
, buf
, sizeof(buf
));
151 printf("Error string is: %s\n", buf
);
152 ATF_REQUIRE(strcmp(buf
, "Error 'test_error'") == 0);
155 printf("Testing custom formatting function\n");
156 err
= atf_error_new("test_error", NULL
, 0, test_format
);
157 atf_error_format(err
, buf
, sizeof(buf
));
158 printf("Error string is: %s\n", buf
);
159 ATF_REQUIRE(strcmp(buf
, "Test formatting function") == 0);
163 /* ---------------------------------------------------------------------
164 * Tests for the "libc" error.
165 * --------------------------------------------------------------------- */
168 ATF_TC_HEAD(libc_new
, tc
)
170 atf_tc_set_md_var(tc
, "descr", "Checks the construction of libc errors");
172 ATF_TC_BODY(libc_new
, tc
)
176 err
= atf_libc_error(ENOMEM
, "Test message 1");
177 ATF_REQUIRE(atf_error_is(err
, "libc"));
178 ATF_REQUIRE_EQ(atf_libc_error_code(err
), ENOMEM
);
179 ATF_REQUIRE(strcmp(atf_libc_error_msg(err
), "Test message 1") == 0);
182 err
= atf_libc_error(EPERM
, "%s message %d", "Test", 2);
183 ATF_REQUIRE(atf_error_is(err
, "libc"));
184 ATF_REQUIRE_EQ(atf_libc_error_code(err
), EPERM
);
185 ATF_REQUIRE(strcmp(atf_libc_error_msg(err
), "Test message 2") == 0);
190 ATF_TC_HEAD(libc_format
, tc
)
192 atf_tc_set_md_var(tc
, "descr", "Checks the formatting of libc errors");
194 ATF_TC_BODY(libc_format
, tc
)
199 err
= atf_libc_error(ENOMEM
, "Test message 1");
200 atf_error_format(err
, buf
, sizeof(buf
));
201 ATF_REQUIRE(strstr(buf
, strerror(ENOMEM
)) != NULL
);
202 ATF_REQUIRE(strstr(buf
, "Test message 1") != NULL
);
205 err
= atf_libc_error(EPERM
, "Test message 2");
206 atf_error_format(err
, buf
, sizeof(buf
));
207 ATF_REQUIRE(strstr(buf
, strerror(EPERM
)) != NULL
);
208 ATF_REQUIRE(strstr(buf
, "Test message 2") != NULL
);
211 err
= atf_libc_error(EPERM
, "%s message %d", "Test", 3);
212 atf_error_format(err
, buf
, sizeof(buf
));
213 ATF_REQUIRE(strstr(buf
, strerror(EPERM
)) != NULL
);
214 ATF_REQUIRE(strstr(buf
, "Test message 3") != NULL
);
218 /* ---------------------------------------------------------------------
219 * Tests for the "no_memory" error.
220 * --------------------------------------------------------------------- */
222 ATF_TC(no_memory_new
);
223 ATF_TC_HEAD(no_memory_new
, tc
)
225 atf_tc_set_md_var(tc
, "descr", "Checks the construction of no_memory "
228 ATF_TC_BODY(no_memory_new
, tc
)
232 err
= atf_no_memory_error();
233 ATF_REQUIRE(atf_error_is(err
, "no_memory"));
234 ATF_REQUIRE(atf_error_data(err
) == NULL
);
238 ATF_TC(no_memory_format
);
239 ATF_TC_HEAD(no_memory_format
, tc
)
241 atf_tc_set_md_var(tc
, "descr", "Checks the formatting of no_memory "
244 ATF_TC_BODY(no_memory_format
, tc
)
249 err
= atf_no_memory_error();
250 atf_error_format(err
, buf
, sizeof(buf
));
251 ATF_REQUIRE(strcmp(buf
, "Not enough memory") == 0);
255 ATF_TC(no_memory_twice
);
256 ATF_TC_HEAD(no_memory_twice
, tc
)
258 atf_tc_set_md_var(tc
, "descr", "Checks the construction of no_memory "
259 "errors multiple times, as this error is initialized "
262 ATF_TC_BODY(no_memory_twice
, tc
)
265 atf_error_t err
= atf_no_memory_error();
266 ATF_REQUIRE(atf_error_is(err
, "no_memory"));
267 ATF_REQUIRE(atf_error_data(err
) == NULL
);
272 atf_error_t err
= atf_no_memory_error();
273 ATF_REQUIRE(atf_error_is(err
, "no_memory"));
274 ATF_REQUIRE(atf_error_data(err
) == NULL
);
279 /* ---------------------------------------------------------------------
280 * Tests cases for the header file.
281 * --------------------------------------------------------------------- */
283 HEADER_TC(include
, "atf-c/error.h");
284 HEADER_TC(include_fwd
, "atf-c/error_fwd.h");
286 /* ---------------------------------------------------------------------
288 * --------------------------------------------------------------------- */
292 /* Add the tests for the "atf_error" type. */
293 ATF_TP_ADD_TC(tp
, error_new
);
294 ATF_TP_ADD_TC(tp
, error_new_wo_memory
);
295 ATF_TP_ADD_TC(tp
, no_error
);
296 ATF_TP_ADD_TC(tp
, is_error
);
297 ATF_TP_ADD_TC(tp
, format
);
299 /* Add the tests for the "libc" error. */
300 ATF_TP_ADD_TC(tp
, libc_new
);
301 ATF_TP_ADD_TC(tp
, libc_format
);
303 /* Add the tests for the "no_memory" error. */
304 ATF_TP_ADD_TC(tp
, no_memory_new
);
305 ATF_TP_ADD_TC(tp
, no_memory_format
);
306 ATF_TP_ADD_TC(tp
, no_memory_twice
);
308 /* Add the test cases for the header file. */
309 ATF_TP_ADD_TC(tp
, include
);
310 ATF_TP_ADD_TC(tp
, include_fwd
);
312 return atf_no_error();