xref: /freebsd/contrib/atf/atf-c/tp.c (revision 8f0ea33f2bbf3a6aa80235f0a02fa5f2780c2b17)
1*0677dfd1SJulio Merino /* Copyright (c) 2008 The NetBSD Foundation, Inc.
2c243e490SMarcel Moolenaar  * All rights reserved.
3c243e490SMarcel Moolenaar  *
4c243e490SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
5c243e490SMarcel Moolenaar  * modification, are permitted provided that the following conditions
6c243e490SMarcel Moolenaar  * are met:
7c243e490SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
8c243e490SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
9c243e490SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
10c243e490SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
11c243e490SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
12c243e490SMarcel Moolenaar  *
13c243e490SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14c243e490SMarcel Moolenaar  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15c243e490SMarcel Moolenaar  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16c243e490SMarcel Moolenaar  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17c243e490SMarcel Moolenaar  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18c243e490SMarcel Moolenaar  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19c243e490SMarcel Moolenaar  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20c243e490SMarcel Moolenaar  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21c243e490SMarcel Moolenaar  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22c243e490SMarcel Moolenaar  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23c243e490SMarcel Moolenaar  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24*0677dfd1SJulio Merino  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  */
25*0677dfd1SJulio Merino 
26*0677dfd1SJulio Merino #include "atf-c/tp.h"
27c243e490SMarcel Moolenaar 
28c243e490SMarcel Moolenaar #include <stdio.h>
29c243e490SMarcel Moolenaar #include <stdlib.h>
30c243e490SMarcel Moolenaar #include <string.h>
31c243e490SMarcel Moolenaar #include <unistd.h>
32c243e490SMarcel Moolenaar 
33*0677dfd1SJulio Merino #include "atf-c/detail/fs.h"
34*0677dfd1SJulio Merino #include "atf-c/detail/map.h"
35*0677dfd1SJulio Merino #include "atf-c/detail/sanity.h"
36c243e490SMarcel Moolenaar #include "atf-c/error.h"
37c243e490SMarcel Moolenaar #include "atf-c/tc.h"
38c243e490SMarcel Moolenaar 
39c243e490SMarcel Moolenaar struct atf_tp_impl {
40c243e490SMarcel Moolenaar     atf_list_t m_tcs;
41c243e490SMarcel Moolenaar     atf_map_t m_config;
42c243e490SMarcel Moolenaar };
43c243e490SMarcel Moolenaar 
44c243e490SMarcel Moolenaar /* ---------------------------------------------------------------------
45c243e490SMarcel Moolenaar  * Auxiliary functions.
46c243e490SMarcel Moolenaar  * --------------------------------------------------------------------- */
47c243e490SMarcel Moolenaar 
48c243e490SMarcel Moolenaar static
49c243e490SMarcel Moolenaar const atf_tc_t *
find_tc(const atf_tp_t * tp,const char * ident)50c243e490SMarcel Moolenaar find_tc(const atf_tp_t *tp, const char *ident)
51c243e490SMarcel Moolenaar {
52c243e490SMarcel Moolenaar     const atf_tc_t *tc;
53c243e490SMarcel Moolenaar     atf_list_citer_t iter;
54c243e490SMarcel Moolenaar 
55c243e490SMarcel Moolenaar     tc = NULL;
56c243e490SMarcel Moolenaar     atf_list_for_each_c(iter, &tp->pimpl->m_tcs) {
57c243e490SMarcel Moolenaar         const atf_tc_t *tc2;
58c243e490SMarcel Moolenaar         tc2 = atf_list_citer_data(iter);
59c243e490SMarcel Moolenaar         if (strcmp(atf_tc_get_ident(tc2), ident) == 0) {
60c243e490SMarcel Moolenaar             tc = tc2;
61c243e490SMarcel Moolenaar             break;
62c243e490SMarcel Moolenaar         }
63c243e490SMarcel Moolenaar     }
64c243e490SMarcel Moolenaar     return tc;
65c243e490SMarcel Moolenaar }
66c243e490SMarcel Moolenaar 
67c243e490SMarcel Moolenaar /* ---------------------------------------------------------------------
68c243e490SMarcel Moolenaar  * The "atf_tp" type.
69c243e490SMarcel Moolenaar  * --------------------------------------------------------------------- */
70c243e490SMarcel Moolenaar 
71c243e490SMarcel Moolenaar /*
72c243e490SMarcel Moolenaar  * Constructors/destructors.
73c243e490SMarcel Moolenaar  */
74c243e490SMarcel Moolenaar 
75c243e490SMarcel Moolenaar atf_error_t
atf_tp_init(atf_tp_t * tp,const char * const * config)76c243e490SMarcel Moolenaar atf_tp_init(atf_tp_t *tp, const char *const *config)
77c243e490SMarcel Moolenaar {
78c243e490SMarcel Moolenaar     atf_error_t err;
79c243e490SMarcel Moolenaar 
80c243e490SMarcel Moolenaar     PRE(config != NULL);
81c243e490SMarcel Moolenaar 
82c243e490SMarcel Moolenaar     tp->pimpl = malloc(sizeof(struct atf_tp_impl));
83c243e490SMarcel Moolenaar     if (tp->pimpl == NULL)
84c243e490SMarcel Moolenaar         return atf_no_memory_error();
85c243e490SMarcel Moolenaar 
86c243e490SMarcel Moolenaar     err = atf_list_init(&tp->pimpl->m_tcs);
87c243e490SMarcel Moolenaar     if (atf_is_error(err))
88c243e490SMarcel Moolenaar         goto out;
89c243e490SMarcel Moolenaar 
90c243e490SMarcel Moolenaar     err = atf_map_init_charpp(&tp->pimpl->m_config, config);
91c243e490SMarcel Moolenaar     if (atf_is_error(err)) {
92c243e490SMarcel Moolenaar         atf_list_fini(&tp->pimpl->m_tcs);
93c243e490SMarcel Moolenaar         goto out;
94c243e490SMarcel Moolenaar     }
95c243e490SMarcel Moolenaar 
96c243e490SMarcel Moolenaar     INV(!atf_is_error(err));
97c243e490SMarcel Moolenaar out:
98c243e490SMarcel Moolenaar     return err;
99c243e490SMarcel Moolenaar }
100c243e490SMarcel Moolenaar 
101c243e490SMarcel Moolenaar void
atf_tp_fini(atf_tp_t * tp)102c243e490SMarcel Moolenaar atf_tp_fini(atf_tp_t *tp)
103c243e490SMarcel Moolenaar {
104c243e490SMarcel Moolenaar     atf_list_iter_t iter;
105c243e490SMarcel Moolenaar 
106c243e490SMarcel Moolenaar     atf_map_fini(&tp->pimpl->m_config);
107c243e490SMarcel Moolenaar 
108c243e490SMarcel Moolenaar     atf_list_for_each(iter, &tp->pimpl->m_tcs) {
109c243e490SMarcel Moolenaar         atf_tc_t *tc = atf_list_iter_data(iter);
110c243e490SMarcel Moolenaar         atf_tc_fini(tc);
111c243e490SMarcel Moolenaar     }
112c243e490SMarcel Moolenaar     atf_list_fini(&tp->pimpl->m_tcs);
113c243e490SMarcel Moolenaar 
114c243e490SMarcel Moolenaar     free(tp->pimpl);
115c243e490SMarcel Moolenaar }
116c243e490SMarcel Moolenaar 
117c243e490SMarcel Moolenaar /*
118c243e490SMarcel Moolenaar  * Getters.
119c243e490SMarcel Moolenaar  */
120c243e490SMarcel Moolenaar 
121c243e490SMarcel Moolenaar char **
atf_tp_get_config(const atf_tp_t * tp)122c243e490SMarcel Moolenaar atf_tp_get_config(const atf_tp_t *tp)
123c243e490SMarcel Moolenaar {
124c243e490SMarcel Moolenaar     return atf_map_to_charpp(&tp->pimpl->m_config);
125c243e490SMarcel Moolenaar }
126c243e490SMarcel Moolenaar 
127c243e490SMarcel Moolenaar bool
atf_tp_has_tc(const atf_tp_t * tp,const char * id)128c243e490SMarcel Moolenaar atf_tp_has_tc(const atf_tp_t *tp, const char *id)
129c243e490SMarcel Moolenaar {
130c243e490SMarcel Moolenaar     const atf_tc_t *tc = find_tc(tp, id);
131c243e490SMarcel Moolenaar     return tc != NULL;
132c243e490SMarcel Moolenaar }
133c243e490SMarcel Moolenaar 
134c243e490SMarcel Moolenaar const atf_tc_t *
atf_tp_get_tc(const atf_tp_t * tp,const char * id)135c243e490SMarcel Moolenaar atf_tp_get_tc(const atf_tp_t *tp, const char *id)
136c243e490SMarcel Moolenaar {
137c243e490SMarcel Moolenaar     const atf_tc_t *tc = find_tc(tp, id);
138c243e490SMarcel Moolenaar     PRE(tc != NULL);
139c243e490SMarcel Moolenaar     return tc;
140c243e490SMarcel Moolenaar }
141c243e490SMarcel Moolenaar 
142c243e490SMarcel Moolenaar const atf_tc_t *const *
atf_tp_get_tcs(const atf_tp_t * tp)143c243e490SMarcel Moolenaar atf_tp_get_tcs(const atf_tp_t *tp)
144c243e490SMarcel Moolenaar {
145c243e490SMarcel Moolenaar     const atf_tc_t **array;
146c243e490SMarcel Moolenaar     atf_list_citer_t iter;
147c243e490SMarcel Moolenaar     size_t i;
148c243e490SMarcel Moolenaar 
149c243e490SMarcel Moolenaar     array = malloc(sizeof(atf_tc_t *) *
150c243e490SMarcel Moolenaar                    (atf_list_size(&tp->pimpl->m_tcs) + 1));
151c243e490SMarcel Moolenaar     if (array == NULL)
152c243e490SMarcel Moolenaar         goto out;
153c243e490SMarcel Moolenaar 
154c243e490SMarcel Moolenaar     i = 0;
155c243e490SMarcel Moolenaar     atf_list_for_each_c(iter, &tp->pimpl->m_tcs) {
156c243e490SMarcel Moolenaar         array[i] = atf_list_citer_data(iter);
157c243e490SMarcel Moolenaar         if (array[i] == NULL) {
158c243e490SMarcel Moolenaar             free(array);
159c243e490SMarcel Moolenaar             array = NULL;
160c243e490SMarcel Moolenaar             goto out;
161c243e490SMarcel Moolenaar         }
162c243e490SMarcel Moolenaar 
163c243e490SMarcel Moolenaar         i++;
164c243e490SMarcel Moolenaar     }
165c243e490SMarcel Moolenaar     array[i] = NULL;
166c243e490SMarcel Moolenaar 
167c243e490SMarcel Moolenaar out:
168c243e490SMarcel Moolenaar     return array;
169c243e490SMarcel Moolenaar }
170c243e490SMarcel Moolenaar 
171c243e490SMarcel Moolenaar /*
172c243e490SMarcel Moolenaar  * Modifiers.
173c243e490SMarcel Moolenaar  */
174c243e490SMarcel Moolenaar 
175c243e490SMarcel Moolenaar atf_error_t
atf_tp_add_tc(atf_tp_t * tp,atf_tc_t * tc)176c243e490SMarcel Moolenaar atf_tp_add_tc(atf_tp_t *tp, atf_tc_t *tc)
177c243e490SMarcel Moolenaar {
178c243e490SMarcel Moolenaar     atf_error_t err;
179c243e490SMarcel Moolenaar 
180c243e490SMarcel Moolenaar     PRE(find_tc(tp, atf_tc_get_ident(tc)) == NULL);
181c243e490SMarcel Moolenaar 
182c243e490SMarcel Moolenaar     err = atf_list_append(&tp->pimpl->m_tcs, tc, false);
183c243e490SMarcel Moolenaar 
184c243e490SMarcel Moolenaar     POST(find_tc(tp, atf_tc_get_ident(tc)) != NULL);
185c243e490SMarcel Moolenaar 
186c243e490SMarcel Moolenaar     return err;
187c243e490SMarcel Moolenaar }
188c243e490SMarcel Moolenaar 
189c243e490SMarcel Moolenaar /* ---------------------------------------------------------------------
190c243e490SMarcel Moolenaar  * Free functions.
191c243e490SMarcel Moolenaar  * --------------------------------------------------------------------- */
192c243e490SMarcel Moolenaar 
193c243e490SMarcel Moolenaar atf_error_t
atf_tp_run(const atf_tp_t * tp,const char * tcname,const char * resfile)194c243e490SMarcel Moolenaar atf_tp_run(const atf_tp_t *tp, const char *tcname, const char *resfile)
195c243e490SMarcel Moolenaar {
196c243e490SMarcel Moolenaar     const atf_tc_t *tc;
197c243e490SMarcel Moolenaar 
198c243e490SMarcel Moolenaar     tc = find_tc(tp, tcname);
199c243e490SMarcel Moolenaar     PRE(tc != NULL);
200c243e490SMarcel Moolenaar 
201c243e490SMarcel Moolenaar     return atf_tc_run(tc, resfile);
202c243e490SMarcel Moolenaar }
203c243e490SMarcel Moolenaar 
204c243e490SMarcel Moolenaar atf_error_t
atf_tp_cleanup(const atf_tp_t * tp,const char * tcname)205c243e490SMarcel Moolenaar atf_tp_cleanup(const atf_tp_t *tp, const char *tcname)
206c243e490SMarcel Moolenaar {
207c243e490SMarcel Moolenaar     const atf_tc_t *tc;
208c243e490SMarcel Moolenaar 
209c243e490SMarcel Moolenaar     tc = find_tc(tp, tcname);
210c243e490SMarcel Moolenaar     PRE(tc != NULL);
211c243e490SMarcel Moolenaar 
212c243e490SMarcel Moolenaar     return atf_tc_cleanup(tc);
213c243e490SMarcel Moolenaar }
214