Remove building with NOCRYPTO option
[minix.git] / external / bsd / bind / dist / unit / atf-src / atf-c / error_test.c
blob5abfa44e8e9a6c28b5762018f4346de37fbc2ff3
1 /* $NetBSD: error_test.c,v 1.3 2014/12/10 04:38:03 christos Exp $ */
3 /*
4 * Automated Testing Framework (atf)
6 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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.
32 #include <errno.h>
33 #include <stdint.h>
34 #include <stdio.h>
35 #include <string.h>
37 #include <atf-c.h>
39 #include "atf-c/defs.h"
40 #include "atf-c/error.h"
42 #include "detail/test_helpers.h"
44 /* ---------------------------------------------------------------------
45 * Auxiliary functions.
46 * --------------------------------------------------------------------- */
48 static
49 void
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 * --------------------------------------------------------------------- */
60 ATF_TC(error_new);
61 ATF_TC_HEAD(error_new, tc)
63 atf_tc_set_md_var(tc, "descr", "Checks the construction of an error "
64 "object");
66 ATF_TC_BODY(error_new, tc)
68 atf_error_t err;
69 int data;
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);
75 atf_error_free(err);
77 data = 5;
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);
83 atf_error_free(err);
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 "
92 "static error type");
94 ATF_TC_BODY(error_new_wo_memory, tc)
96 atf_error_t err;
97 void *invalid;
99 invalid = (void *)1;
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);
104 atf_error_free(err);
107 ATF_TC(no_error);
108 ATF_TC_HEAD(no_error, tc)
110 atf_tc_set_md_var(tc, "descr", "Checks that constructing a non-error "
111 "object works");
113 ATF_TC_BODY(no_error, tc)
115 atf_error_t err;
117 err = atf_no_error();
118 ATF_REQUIRE(!atf_is_error(err));
121 ATF_TC(is_error);
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)
129 atf_error_t err;
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));
136 atf_error_free(err);
139 ATF_TC(format);
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)
147 atf_error_t err;
148 char buf[1024];
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);
155 atf_error_free(err);
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);
162 atf_error_free(err);
165 /* ---------------------------------------------------------------------
166 * Tests for the "libc" error.
167 * --------------------------------------------------------------------- */
169 ATF_TC(libc_new);
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)
176 atf_error_t err;
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);
182 atf_error_free(err);
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);
188 atf_error_free(err);
191 ATF_TC(libc_format);
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)
198 atf_error_t err;
199 char buf[1024];
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);
205 atf_error_free(err);
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);
211 atf_error_free(err);
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);
217 atf_error_free(err);
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 "
228 "errors");
230 ATF_TC_BODY(no_memory_new, tc)
232 atf_error_t err;
234 err = atf_no_memory_error();
235 ATF_REQUIRE(atf_error_is(err, "no_memory"));
236 ATF_REQUIRE(atf_error_data(err) == NULL);
237 atf_error_free(err);
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 "
244 "errors");
246 ATF_TC_BODY(no_memory_format, tc)
248 atf_error_t err;
249 char buf[1024];
251 err = atf_no_memory_error();
252 atf_error_format(err, buf, sizeof(buf));
253 ATF_REQUIRE(strcmp(buf, "Not enough memory") == 0);
254 atf_error_free(err);
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 "
262 "statically");
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);
270 atf_error_free(err);
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);
277 atf_error_free(err);
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 /* ---------------------------------------------------------------------
289 * Main.
290 * --------------------------------------------------------------------- */
292 ATF_TP_ADD_TCS(tp)
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();