xref: /minix3/external/bsd/bind/dist/unit/atf-src/atf-c/error.c (revision 00b67f09dd46474d133c95011a48590a8e8f94c7)
1 /*	$NetBSD: error.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 <stdarg.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 #include "atf-c/error.h"
38 
39 #include "detail/sanity.h"
40 
41 /* Theoretically, there can only be a single error intance at any given
42  * point in time, because errors are raised at one point and must be
43  * handled immediately.  If another error has to be raised during the
44  * handling process, something else has to be done with the previous
45  * error.
46  *
47  * This is per-thread information and will break threaded tests, but we
48  * currently do not have any threading support; therefore, this is fine. */
49 static bool error_on_flight = false;
50 
51 /* ---------------------------------------------------------------------
52  * Auxiliary functions.
53  * --------------------------------------------------------------------- */
54 
55 static
56 void
error_format(const atf_error_t err,char * buf,size_t buflen)57 error_format(const atf_error_t err, char *buf, size_t buflen)
58 {
59     PRE(err != NULL);
60     snprintf(buf, buflen, "Error '%s'", err->m_type);
61 }
62 
63 static
64 bool
error_init(atf_error_t err,const char * type,void * data,size_t datalen,void (* format)(const atf_error_t,char *,size_t))65 error_init(atf_error_t err, const char *type, void *data, size_t datalen,
66            void (*format)(const atf_error_t, char *, size_t))
67 {
68     bool ok;
69 
70     PRE(data != NULL || datalen == 0);
71     PRE(datalen != 0 || data == NULL);
72 
73     err->m_free = false;
74     err->m_type = type;
75     err->m_format = (format == NULL) ? error_format : format;
76 
77     ok = true;
78     if (data == NULL) {
79         err->m_data = NULL;
80     } else {
81         err->m_data = malloc(datalen);
82         if (err->m_data == NULL) {
83             ok = false;
84         } else
85             memcpy(err->m_data, data, datalen);
86     }
87 
88     return ok;
89 }
90 
91 /* ---------------------------------------------------------------------
92  * The "atf_error" type.
93  * --------------------------------------------------------------------- */
94 
95 atf_error_t
atf_error_new(const char * type,void * data,size_t datalen,void (* format)(const atf_error_t,char *,size_t))96 atf_error_new(const char *type, void *data, size_t datalen,
97               void (*format)(const atf_error_t, char *, size_t))
98 {
99     atf_error_t err;
100 
101     PRE(!error_on_flight);
102     PRE(data != NULL || datalen == 0);
103     PRE(datalen != 0 || data == NULL);
104 
105     err = malloc(sizeof(*err));
106     if (err == NULL)
107         err = atf_no_memory_error();
108     else {
109         if (!error_init(err, type, data, datalen, format)) {
110             free(err);
111             err = atf_no_memory_error();
112         } else {
113             err->m_free = true;
114             error_on_flight = true;
115         }
116     }
117 
118     INV(err != NULL);
119     POST(error_on_flight);
120     return err;
121 }
122 
123 void
atf_error_free(atf_error_t err)124 atf_error_free(atf_error_t err)
125 {
126     bool freeit;
127 
128     PRE(error_on_flight);
129     PRE(err != NULL);
130 
131     freeit = err->m_free;
132 
133     if (err->m_data != NULL)
134         free(err->m_data);
135 
136     if (freeit)
137         free(err);
138 
139     error_on_flight = false;
140 }
141 
142 atf_error_t
atf_no_error(void)143 atf_no_error(void)
144 {
145     return NULL;
146 }
147 
148 bool
atf_is_error(const atf_error_t err)149 atf_is_error(const atf_error_t err)
150 {
151     return err != NULL;
152 }
153 
154 bool
atf_error_is(const atf_error_t err,const char * type)155 atf_error_is(const atf_error_t err, const char *type)
156 {
157     PRE(err != NULL);
158 
159     return strcmp(err->m_type, type) == 0;
160 }
161 
162 const void *
atf_error_data(const atf_error_t err)163 atf_error_data(const atf_error_t err)
164 {
165     PRE(err != NULL);
166 
167     return err->m_data;
168 }
169 
170 void
atf_error_format(const atf_error_t err,char * buf,size_t buflen)171 atf_error_format(const atf_error_t err, char *buf, size_t buflen)
172 {
173     PRE(err != NULL);
174     err->m_format(err, buf, buflen);
175 }
176 
177 /* ---------------------------------------------------------------------
178  * Common error types.
179  * --------------------------------------------------------------------- */
180 
181 /*
182  * The "libc" error.
183  */
184 
185 struct atf_libc_error_data {
186     int m_errno;
187     char m_what[4096];
188 };
189 typedef struct atf_libc_error_data atf_libc_error_data_t;
190 
191 static
192 void
libc_format(const atf_error_t err,char * buf,size_t buflen)193 libc_format(const atf_error_t err, char *buf, size_t buflen)
194 {
195     const atf_libc_error_data_t *data;
196 
197     PRE(atf_error_is(err, "libc"));
198 
199     data = atf_error_data(err);
200     snprintf(buf, buflen, "%s: %s", data->m_what, strerror(data->m_errno));
201 }
202 
203 atf_error_t
atf_libc_error(int syserrno,const char * fmt,...)204 atf_libc_error(int syserrno, const char *fmt, ...)
205 {
206     atf_error_t err;
207     atf_libc_error_data_t data;
208     va_list ap;
209 
210     data.m_errno = syserrno;
211     va_start(ap, fmt);
212     vsnprintf(data.m_what, sizeof(data.m_what), fmt, ap);
213     va_end(ap);
214 
215     err = atf_error_new("libc", &data, sizeof(data), libc_format);
216 
217     return err;
218 }
219 
220 int
atf_libc_error_code(const atf_error_t err)221 atf_libc_error_code(const atf_error_t err)
222 {
223     const struct atf_libc_error_data *data;
224 
225     PRE(atf_error_is(err, "libc"));
226 
227     data = atf_error_data(err);
228 
229     return data->m_errno;
230 }
231 
232 const char *
atf_libc_error_msg(const atf_error_t err)233 atf_libc_error_msg(const atf_error_t err)
234 {
235     const struct atf_libc_error_data *data;
236 
237     PRE(atf_error_is(err, "libc"));
238 
239     data = atf_error_data(err);
240 
241     return data->m_what;
242 }
243 
244 /*
245  * The "no_memory" error.
246  */
247 
248 static struct atf_error no_memory_error;
249 
250 static
251 void
no_memory_format(const atf_error_t err,char * buf,size_t buflen)252 no_memory_format(const atf_error_t err, char *buf, size_t buflen)
253 {
254     PRE(atf_error_is(err, "no_memory"));
255 
256     snprintf(buf, buflen, "Not enough memory");
257 }
258 
259 atf_error_t
atf_no_memory_error(void)260 atf_no_memory_error(void)
261 {
262     PRE(!error_on_flight);
263 
264     error_init(&no_memory_error, "no_memory", NULL, 0,
265                no_memory_format);
266 
267     error_on_flight = true;
268     return &no_memory_error;
269 }
270