xref: /netbsd-src/external/bsd/atf/dist/atf-c/tp.c (revision f2af7cc18768e2ca30fbc8508a6ee20d421e72de)
1 /*
2  * Automated Testing Framework (atf)
3  *
4  * Copyright (c) 2008, 2009, 2010 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 <stdio.h>
31 #include <string.h>
32 #include <unistd.h>
33 
34 #include "atf-c/error.h"
35 #include "atf-c/fs.h"
36 #include "atf-c/sanity.h"
37 #include "atf-c/tc.h"
38 #include "atf-c/tp.h"
39 
40 /* ---------------------------------------------------------------------
41  * Auxiliary functions.
42  * --------------------------------------------------------------------- */
43 
44 static
45 const atf_tc_t *
46 find_tc(const atf_tp_t *tp, const char *ident)
47 {
48     const atf_tc_t *tc;
49     atf_list_citer_t iter;
50 
51     tc = NULL;
52     atf_list_for_each_c(iter, &tp->m_tcs) {
53         const atf_tc_t *tc2;
54         tc2 = atf_list_citer_data(iter);
55         if (strcmp(tc2->m_ident, ident) == 0) {
56             tc = tc2;
57             break;
58         }
59     }
60     return tc;
61 }
62 
63 /* ---------------------------------------------------------------------
64  * The "atf_tp" type.
65  * --------------------------------------------------------------------- */
66 
67 /*
68  * Constructors/destructors.
69  */
70 
71 atf_error_t
72 atf_tp_init(atf_tp_t *tp, struct atf_map *config)
73 {
74     atf_error_t err;
75 
76     PRE(config != NULL);
77 
78     err = atf_list_init(&tp->m_tcs);
79     if (atf_is_error(err))
80         goto out;
81 
82     tp->m_config = config;
83 
84     INV(!atf_is_error(err));
85 out:
86     return err;
87 }
88 
89 void
90 atf_tp_fini(atf_tp_t *tp)
91 {
92     atf_list_iter_t iter;
93 
94     atf_list_for_each(iter, &tp->m_tcs) {
95         atf_tc_t *tc = atf_list_iter_data(iter);
96         atf_tc_fini(tc);
97     }
98     atf_list_fini(&tp->m_tcs);
99 }
100 
101 /*
102  * Getters.
103  */
104 
105 const struct atf_map *
106 atf_tp_get_config(const atf_tp_t *tp)
107 {
108     return tp->m_config;
109 }
110 
111 bool
112 atf_tp_has_tc(const atf_tp_t *tp, const char *id)
113 {
114     const atf_tc_t *tc = find_tc(tp, id);
115     return tc != NULL;
116 }
117 
118 const atf_tc_t *
119 atf_tp_get_tc(const atf_tp_t *tp, const char *id)
120 {
121     const atf_tc_t *tc = find_tc(tp, id);
122     PRE(tc != NULL);
123     return tc;
124 }
125 
126 const atf_list_t *
127 atf_tp_get_tcs(const atf_tp_t *tp)
128 {
129     return &tp->m_tcs;
130 }
131 
132 /*
133  * Modifiers.
134  */
135 
136 atf_error_t
137 atf_tp_add_tc(atf_tp_t *tp, atf_tc_t *tc)
138 {
139     atf_error_t err;
140 
141     PRE(find_tc(tp, tc->m_ident) == NULL);
142 
143     err = atf_list_append(&tp->m_tcs, tc, false);
144 
145     POST(find_tc(tp, tc->m_ident) != NULL);
146 
147     return err;
148 }
149 
150 /* ---------------------------------------------------------------------
151  * Free functions.
152  * --------------------------------------------------------------------- */
153 
154 atf_error_t
155 atf_tp_run(const atf_tp_t *tp, const char *tcname,
156            const atf_fs_path_t *resfile)
157 {
158     const atf_tc_t *tc;
159 
160     tc = find_tc(tp, tcname);
161     PRE(tc != NULL);
162 
163     return atf_tc_run(tc, resfile);
164 }
165 
166 atf_error_t
167 atf_tp_cleanup(const atf_tp_t *tp, const char *tcname)
168 {
169     const atf_tc_t *tc;
170 
171     tc = find_tc(tp, tcname);
172     PRE(tc != NULL);
173 
174     return atf_tc_cleanup(tc);
175 }
176