1 /* $NetBSD: error_test.c,v 1.3 2014/12/10 04:38:03 christos Exp $ */
2
3 /*
4 * Automated Testing Framework (atf)
5 *
6 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * All rights reserved.
8 *
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.
17 *
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.
30 */
31
32 #include <errno.h>
33 #include <stdint.h>
34 #include <stdio.h>
35 #include <string.h>
36
37 #include <atf-c.h>
38
39 #include "atf-c/defs.h"
40 #include "atf-c/error.h"
41
42 #include "detail/test_helpers.h"
43
44 /* ---------------------------------------------------------------------
45 * Auxiliary functions.
46 * --------------------------------------------------------------------- */
47
48 static
49 void
test_format(const atf_error_t err ATF_DEFS_ATTRIBUTE_UNUSED,char * buf,size_t buflen)50 test_format(const atf_error_t err ATF_DEFS_ATTRIBUTE_UNUSED,
51 char *buf, size_t buflen)
52 {
53 snprintf(buf, buflen, "Test formatting function");
54 }
55
56 /* ---------------------------------------------------------------------
57 * Tests for the "atf_error" type.
58 * --------------------------------------------------------------------- */
59
60 ATF_TC(error_new);
ATF_TC_HEAD(error_new,tc)61 ATF_TC_HEAD(error_new, tc)
62 {
63 atf_tc_set_md_var(tc, "descr", "Checks the construction of an error "
64 "object");
65 }
ATF_TC_BODY(error_new,tc)66 ATF_TC_BODY(error_new, tc)
67 {
68 atf_error_t err;
69 int data;
70
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);
76
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);
84 }
85
86 ATF_TC(error_new_wo_memory);
ATF_TC_HEAD(error_new_wo_memory,tc)87 ATF_TC_HEAD(error_new_wo_memory, tc)
88 {
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");
93 }
ATF_TC_BODY(error_new_wo_memory,tc)94 ATF_TC_BODY(error_new_wo_memory, tc)
95 {
96 atf_error_t err;
97 void *invalid;
98
99 invalid = (void *)1;
100
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);
105 }
106
107 ATF_TC(no_error);
ATF_TC_HEAD(no_error,tc)108 ATF_TC_HEAD(no_error, tc)
109 {
110 atf_tc_set_md_var(tc, "descr", "Checks that constructing a non-error "
111 "object works");
112 }
ATF_TC_BODY(no_error,tc)113 ATF_TC_BODY(no_error, tc)
114 {
115 atf_error_t err;
116
117 err = atf_no_error();
118 ATF_REQUIRE(!atf_is_error(err));
119 }
120
121 ATF_TC(is_error);
ATF_TC_HEAD(is_error,tc)122 ATF_TC_HEAD(is_error, tc)
123 {
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");
126 }
ATF_TC_BODY(is_error,tc)127 ATF_TC_BODY(is_error, tc)
128 {
129 atf_error_t err;
130
131 err = atf_no_error();
132 ATF_REQUIRE(!atf_is_error(err));
133
134 err = atf_error_new("test_error", NULL, 0, NULL);
135 ATF_REQUIRE(atf_is_error(err));
136 atf_error_free(err);
137 }
138
139 ATF_TC(format);
ATF_TC_HEAD(format,tc)140 ATF_TC_HEAD(format, tc)
141 {
142 atf_tc_set_md_var(tc, "descr", "Checks the default formatting function "
143 "and the ability to change it");
144 }
ATF_TC_BODY(format,tc)145 ATF_TC_BODY(format, tc)
146 {
147 atf_error_t err;
148 char buf[1024];
149
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);
156
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);
163 }
164
165 /* ---------------------------------------------------------------------
166 * Tests for the "libc" error.
167 * --------------------------------------------------------------------- */
168
169 ATF_TC(libc_new);
ATF_TC_HEAD(libc_new,tc)170 ATF_TC_HEAD(libc_new, tc)
171 {
172 atf_tc_set_md_var(tc, "descr", "Checks the construction of libc errors");
173 }
ATF_TC_BODY(libc_new,tc)174 ATF_TC_BODY(libc_new, tc)
175 {
176 atf_error_t err;
177
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);
183
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);
189 }
190
191 ATF_TC(libc_format);
ATF_TC_HEAD(libc_format,tc)192 ATF_TC_HEAD(libc_format, tc)
193 {
194 atf_tc_set_md_var(tc, "descr", "Checks the formatting of libc errors");
195 }
ATF_TC_BODY(libc_format,tc)196 ATF_TC_BODY(libc_format, tc)
197 {
198 atf_error_t err;
199 char buf[1024];
200
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);
206
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);
212
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);
218 }
219
220 /* ---------------------------------------------------------------------
221 * Tests for the "no_memory" error.
222 * --------------------------------------------------------------------- */
223
224 ATF_TC(no_memory_new);
ATF_TC_HEAD(no_memory_new,tc)225 ATF_TC_HEAD(no_memory_new, tc)
226 {
227 atf_tc_set_md_var(tc, "descr", "Checks the construction of no_memory "
228 "errors");
229 }
ATF_TC_BODY(no_memory_new,tc)230 ATF_TC_BODY(no_memory_new, tc)
231 {
232 atf_error_t err;
233
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);
238 }
239
240 ATF_TC(no_memory_format);
ATF_TC_HEAD(no_memory_format,tc)241 ATF_TC_HEAD(no_memory_format, tc)
242 {
243 atf_tc_set_md_var(tc, "descr", "Checks the formatting of no_memory "
244 "errors");
245 }
ATF_TC_BODY(no_memory_format,tc)246 ATF_TC_BODY(no_memory_format, tc)
247 {
248 atf_error_t err;
249 char buf[1024];
250
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);
255 }
256
257 ATF_TC(no_memory_twice);
ATF_TC_HEAD(no_memory_twice,tc)258 ATF_TC_HEAD(no_memory_twice, tc)
259 {
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");
263 }
ATF_TC_BODY(no_memory_twice,tc)264 ATF_TC_BODY(no_memory_twice, tc)
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 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);
278 }
279 }
280
281 /* ---------------------------------------------------------------------
282 * Tests cases for the header file.
283 * --------------------------------------------------------------------- */
284
285 HEADER_TC(include, "atf-c/error.h");
286 HEADER_TC(include_fwd, "atf-c/error_fwd.h");
287
288 /* ---------------------------------------------------------------------
289 * Main.
290 * --------------------------------------------------------------------- */
291
ATF_TP_ADD_TCS(tp)292 ATF_TP_ADD_TCS(tp)
293 {
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);
300
301 /* Add the tests for the "libc" error. */
302 ATF_TP_ADD_TC(tp, libc_new);
303 ATF_TP_ADD_TC(tp, libc_format);
304
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);
309
310 /* Add the test cases for the header file. */
311 ATF_TP_ADD_TC(tp, include);
312 ATF_TP_ADD_TC(tp, include_fwd);
313
314 return atf_no_error();
315 }
316