1 /*
2 * Automated Testing Framework (atf)
3 *
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 *
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.
28 */
29
30 #include <errno.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <string.h>
34
35 #include <atf-c.h>
36
37 #include "atf-c/defs.h"
38 #include "atf-c/error.h"
39
40 #include "detail/test_helpers.h"
41
42 /* ---------------------------------------------------------------------
43 * Auxiliary functions.
44 * --------------------------------------------------------------------- */
45
46 static
47 void
test_format(const atf_error_t err ATF_DEFS_ATTRIBUTE_UNUSED,char * buf,size_t buflen)48 test_format(const atf_error_t err ATF_DEFS_ATTRIBUTE_UNUSED,
49 char *buf, size_t buflen)
50 {
51 snprintf(buf, buflen, "Test formatting function");
52 }
53
54 /* ---------------------------------------------------------------------
55 * Tests for the "atf_error" type.
56 * --------------------------------------------------------------------- */
57
58 ATF_TC(error_new);
ATF_TC_HEAD(error_new,tc)59 ATF_TC_HEAD(error_new, tc)
60 {
61 atf_tc_set_md_var(tc, "descr", "Checks the construction of an error "
62 "object");
63 }
ATF_TC_BODY(error_new,tc)64 ATF_TC_BODY(error_new, tc)
65 {
66 atf_error_t err;
67 int data;
68
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);
73 atf_error_free(err);
74
75 data = 5;
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);
81 atf_error_free(err);
82 }
83
84 ATF_TC(error_new_wo_memory);
ATF_TC_HEAD(error_new_wo_memory,tc)85 ATF_TC_HEAD(error_new_wo_memory, tc)
86 {
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 "
90 "static error type");
91 }
ATF_TC_BODY(error_new_wo_memory,tc)92 ATF_TC_BODY(error_new_wo_memory, tc)
93 {
94 atf_error_t err;
95 void *invalid;
96
97 invalid = (void *)1;
98
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);
102 atf_error_free(err);
103 }
104
105 ATF_TC(no_error);
ATF_TC_HEAD(no_error,tc)106 ATF_TC_HEAD(no_error, tc)
107 {
108 atf_tc_set_md_var(tc, "descr", "Checks that constructing a non-error "
109 "object works");
110 }
ATF_TC_BODY(no_error,tc)111 ATF_TC_BODY(no_error, tc)
112 {
113 atf_error_t err;
114
115 err = atf_no_error();
116 ATF_REQUIRE(!atf_is_error(err));
117 }
118
119 ATF_TC(is_error);
ATF_TC_HEAD(is_error,tc)120 ATF_TC_HEAD(is_error, tc)
121 {
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");
124 }
ATF_TC_BODY(is_error,tc)125 ATF_TC_BODY(is_error, tc)
126 {
127 atf_error_t err;
128
129 err = atf_no_error();
130 ATF_REQUIRE(!atf_is_error(err));
131
132 err = atf_error_new("test_error", NULL, 0, NULL);
133 ATF_REQUIRE(atf_is_error(err));
134 atf_error_free(err);
135 }
136
137 ATF_TC(format);
ATF_TC_HEAD(format,tc)138 ATF_TC_HEAD(format, tc)
139 {
140 atf_tc_set_md_var(tc, "descr", "Checks the default formatting function "
141 "and the ability to change it");
142 }
ATF_TC_BODY(format,tc)143 ATF_TC_BODY(format, tc)
144 {
145 atf_error_t err;
146 char buf[1024];
147
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);
153 atf_error_free(err);
154
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);
160 atf_error_free(err);
161 }
162
163 /* ---------------------------------------------------------------------
164 * Tests for the "libc" error.
165 * --------------------------------------------------------------------- */
166
167 ATF_TC(libc_new);
ATF_TC_HEAD(libc_new,tc)168 ATF_TC_HEAD(libc_new, tc)
169 {
170 atf_tc_set_md_var(tc, "descr", "Checks the construction of libc errors");
171 }
ATF_TC_BODY(libc_new,tc)172 ATF_TC_BODY(libc_new, tc)
173 {
174 atf_error_t err;
175
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);
180 atf_error_free(err);
181
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);
186 atf_error_free(err);
187 }
188
189 ATF_TC(libc_format);
ATF_TC_HEAD(libc_format,tc)190 ATF_TC_HEAD(libc_format, tc)
191 {
192 atf_tc_set_md_var(tc, "descr", "Checks the formatting of libc errors");
193 }
ATF_TC_BODY(libc_format,tc)194 ATF_TC_BODY(libc_format, tc)
195 {
196 atf_error_t err;
197 char buf[1024];
198
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);
203 atf_error_free(err);
204
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);
209 atf_error_free(err);
210
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);
215 atf_error_free(err);
216 }
217
218 /* ---------------------------------------------------------------------
219 * Tests for the "no_memory" error.
220 * --------------------------------------------------------------------- */
221
222 ATF_TC(no_memory_new);
ATF_TC_HEAD(no_memory_new,tc)223 ATF_TC_HEAD(no_memory_new, tc)
224 {
225 atf_tc_set_md_var(tc, "descr", "Checks the construction of no_memory "
226 "errors");
227 }
ATF_TC_BODY(no_memory_new,tc)228 ATF_TC_BODY(no_memory_new, tc)
229 {
230 atf_error_t err;
231
232 err = atf_no_memory_error();
233 ATF_REQUIRE(atf_error_is(err, "no_memory"));
234 ATF_REQUIRE(atf_error_data(err) == NULL);
235 atf_error_free(err);
236 }
237
238 ATF_TC(no_memory_format);
ATF_TC_HEAD(no_memory_format,tc)239 ATF_TC_HEAD(no_memory_format, tc)
240 {
241 atf_tc_set_md_var(tc, "descr", "Checks the formatting of no_memory "
242 "errors");
243 }
ATF_TC_BODY(no_memory_format,tc)244 ATF_TC_BODY(no_memory_format, tc)
245 {
246 atf_error_t err;
247 char buf[1024];
248
249 err = atf_no_memory_error();
250 atf_error_format(err, buf, sizeof(buf));
251 ATF_REQUIRE(strcmp(buf, "Not enough memory") == 0);
252 atf_error_free(err);
253 }
254
255 ATF_TC(no_memory_twice);
ATF_TC_HEAD(no_memory_twice,tc)256 ATF_TC_HEAD(no_memory_twice, tc)
257 {
258 atf_tc_set_md_var(tc, "descr", "Checks the construction of no_memory "
259 "errors multiple times, as this error is initialized "
260 "statically");
261 }
ATF_TC_BODY(no_memory_twice,tc)262 ATF_TC_BODY(no_memory_twice, tc)
263 {
264 {
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);
268 atf_error_free(err);
269 }
270
271 {
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);
275 atf_error_free(err);
276 }
277 }
278
279 /* ---------------------------------------------------------------------
280 * Tests cases for the header file.
281 * --------------------------------------------------------------------- */
282
283 HEADER_TC(include, "atf-c/error.h");
284 HEADER_TC(include_fwd, "atf-c/error_fwd.h");
285
286 /* ---------------------------------------------------------------------
287 * Main.
288 * --------------------------------------------------------------------- */
289
ATF_TP_ADD_TCS(tp)290 ATF_TP_ADD_TCS(tp)
291 {
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);
298
299 /* Add the tests for the "libc" error. */
300 ATF_TP_ADD_TC(tp, libc_new);
301 ATF_TP_ADD_TC(tp, libc_format);
302
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);
307
308 /* Add the test cases for the header file. */
309 ATF_TP_ADD_TC(tp, include);
310 ATF_TP_ADD_TC(tp, include_fwd);
311
312 return atf_no_error();
313 }
314