tp.c revision 240116
1240116Smarcel/*
2240116Smarcel * Automated Testing Framework (atf)
3240116Smarcel *
4240116Smarcel * Copyright (c) 2008 The NetBSD Foundation, Inc.
5240116Smarcel * All rights reserved.
6240116Smarcel *
7240116Smarcel * Redistribution and use in source and binary forms, with or without
8240116Smarcel * modification, are permitted provided that the following conditions
9240116Smarcel * are met:
10240116Smarcel * 1. Redistributions of source code must retain the above copyright
11240116Smarcel *    notice, this list of conditions and the following disclaimer.
12240116Smarcel * 2. Redistributions in binary form must reproduce the above copyright
13240116Smarcel *    notice, this list of conditions and the following disclaimer in the
14240116Smarcel *    documentation and/or other materials provided with the distribution.
15240116Smarcel *
16240116Smarcel * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17240116Smarcel * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18240116Smarcel * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19240116Smarcel * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20240116Smarcel * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21240116Smarcel * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22240116Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23240116Smarcel * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24240116Smarcel * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25240116Smarcel * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26240116Smarcel * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27240116Smarcel * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28240116Smarcel */
29240116Smarcel
30240116Smarcel#include <stdio.h>
31240116Smarcel#include <stdlib.h>
32240116Smarcel#include <string.h>
33240116Smarcel#include <unistd.h>
34240116Smarcel
35240116Smarcel#include "atf-c/error.h"
36240116Smarcel#include "atf-c/tc.h"
37240116Smarcel#include "atf-c/tp.h"
38240116Smarcel
39240116Smarcel#include "detail/fs.h"
40240116Smarcel#include "detail/map.h"
41240116Smarcel#include "detail/sanity.h"
42240116Smarcel
43240116Smarcelstruct atf_tp_impl {
44240116Smarcel    atf_list_t m_tcs;
45240116Smarcel    atf_map_t m_config;
46240116Smarcel};
47240116Smarcel
48240116Smarcel/* ---------------------------------------------------------------------
49240116Smarcel * Auxiliary functions.
50240116Smarcel * --------------------------------------------------------------------- */
51240116Smarcel
52240116Smarcelstatic
53240116Smarcelconst atf_tc_t *
54240116Smarcelfind_tc(const atf_tp_t *tp, const char *ident)
55240116Smarcel{
56240116Smarcel    const atf_tc_t *tc;
57240116Smarcel    atf_list_citer_t iter;
58240116Smarcel
59240116Smarcel    tc = NULL;
60240116Smarcel    atf_list_for_each_c(iter, &tp->pimpl->m_tcs) {
61240116Smarcel        const atf_tc_t *tc2;
62240116Smarcel        tc2 = atf_list_citer_data(iter);
63240116Smarcel        if (strcmp(atf_tc_get_ident(tc2), ident) == 0) {
64240116Smarcel            tc = tc2;
65240116Smarcel            break;
66240116Smarcel        }
67240116Smarcel    }
68240116Smarcel    return tc;
69240116Smarcel}
70240116Smarcel
71240116Smarcel/* ---------------------------------------------------------------------
72240116Smarcel * The "atf_tp" type.
73240116Smarcel * --------------------------------------------------------------------- */
74240116Smarcel
75240116Smarcel/*
76240116Smarcel * Constructors/destructors.
77240116Smarcel */
78240116Smarcel
79240116Smarcelatf_error_t
80240116Smarcelatf_tp_init(atf_tp_t *tp, const char *const *config)
81240116Smarcel{
82240116Smarcel    atf_error_t err;
83240116Smarcel
84240116Smarcel    PRE(config != NULL);
85240116Smarcel
86240116Smarcel    tp->pimpl = malloc(sizeof(struct atf_tp_impl));
87240116Smarcel    if (tp->pimpl == NULL)
88240116Smarcel        return atf_no_memory_error();
89240116Smarcel
90240116Smarcel    err = atf_list_init(&tp->pimpl->m_tcs);
91240116Smarcel    if (atf_is_error(err))
92240116Smarcel        goto out;
93240116Smarcel
94240116Smarcel    err = atf_map_init_charpp(&tp->pimpl->m_config, config);
95240116Smarcel    if (atf_is_error(err)) {
96240116Smarcel        atf_list_fini(&tp->pimpl->m_tcs);
97240116Smarcel        goto out;
98240116Smarcel    }
99240116Smarcel
100240116Smarcel    INV(!atf_is_error(err));
101240116Smarcelout:
102240116Smarcel    return err;
103240116Smarcel}
104240116Smarcel
105240116Smarcelvoid
106240116Smarcelatf_tp_fini(atf_tp_t *tp)
107240116Smarcel{
108240116Smarcel    atf_list_iter_t iter;
109240116Smarcel
110240116Smarcel    atf_map_fini(&tp->pimpl->m_config);
111240116Smarcel
112240116Smarcel    atf_list_for_each(iter, &tp->pimpl->m_tcs) {
113240116Smarcel        atf_tc_t *tc = atf_list_iter_data(iter);
114240116Smarcel        atf_tc_fini(tc);
115240116Smarcel    }
116240116Smarcel    atf_list_fini(&tp->pimpl->m_tcs);
117240116Smarcel
118240116Smarcel    free(tp->pimpl);
119240116Smarcel}
120240116Smarcel
121240116Smarcel/*
122240116Smarcel * Getters.
123240116Smarcel */
124240116Smarcel
125240116Smarcelchar **
126240116Smarcelatf_tp_get_config(const atf_tp_t *tp)
127240116Smarcel{
128240116Smarcel    return atf_map_to_charpp(&tp->pimpl->m_config);
129240116Smarcel}
130240116Smarcel
131240116Smarcelbool
132240116Smarcelatf_tp_has_tc(const atf_tp_t *tp, const char *id)
133240116Smarcel{
134240116Smarcel    const atf_tc_t *tc = find_tc(tp, id);
135240116Smarcel    return tc != NULL;
136240116Smarcel}
137240116Smarcel
138240116Smarcelconst atf_tc_t *
139240116Smarcelatf_tp_get_tc(const atf_tp_t *tp, const char *id)
140240116Smarcel{
141240116Smarcel    const atf_tc_t *tc = find_tc(tp, id);
142240116Smarcel    PRE(tc != NULL);
143240116Smarcel    return tc;
144240116Smarcel}
145240116Smarcel
146240116Smarcelconst atf_tc_t *const *
147240116Smarcelatf_tp_get_tcs(const atf_tp_t *tp)
148240116Smarcel{
149240116Smarcel    const atf_tc_t **array;
150240116Smarcel    atf_list_citer_t iter;
151240116Smarcel    size_t i;
152240116Smarcel
153240116Smarcel    array = malloc(sizeof(atf_tc_t *) *
154240116Smarcel                   (atf_list_size(&tp->pimpl->m_tcs) + 1));
155240116Smarcel    if (array == NULL)
156240116Smarcel        goto out;
157240116Smarcel
158240116Smarcel    i = 0;
159240116Smarcel    atf_list_for_each_c(iter, &tp->pimpl->m_tcs) {
160240116Smarcel        array[i] = atf_list_citer_data(iter);
161240116Smarcel        if (array[i] == NULL) {
162240116Smarcel            free(array);
163240116Smarcel            array = NULL;
164240116Smarcel            goto out;
165240116Smarcel        }
166240116Smarcel
167240116Smarcel        i++;
168240116Smarcel    }
169240116Smarcel    array[i] = NULL;
170240116Smarcel
171240116Smarcelout:
172240116Smarcel    return array;
173240116Smarcel}
174240116Smarcel
175240116Smarcel/*
176240116Smarcel * Modifiers.
177240116Smarcel */
178240116Smarcel
179240116Smarcelatf_error_t
180240116Smarcelatf_tp_add_tc(atf_tp_t *tp, atf_tc_t *tc)
181240116Smarcel{
182240116Smarcel    atf_error_t err;
183240116Smarcel
184240116Smarcel    PRE(find_tc(tp, atf_tc_get_ident(tc)) == NULL);
185240116Smarcel
186240116Smarcel    err = atf_list_append(&tp->pimpl->m_tcs, tc, false);
187240116Smarcel
188240116Smarcel    POST(find_tc(tp, atf_tc_get_ident(tc)) != NULL);
189240116Smarcel
190240116Smarcel    return err;
191240116Smarcel}
192240116Smarcel
193240116Smarcel/* ---------------------------------------------------------------------
194240116Smarcel * Free functions.
195240116Smarcel * --------------------------------------------------------------------- */
196240116Smarcel
197240116Smarcelatf_error_t
198240116Smarcelatf_tp_run(const atf_tp_t *tp, const char *tcname, const char *resfile)
199240116Smarcel{
200240116Smarcel    const atf_tc_t *tc;
201240116Smarcel
202240116Smarcel    tc = find_tc(tp, tcname);
203240116Smarcel    PRE(tc != NULL);
204240116Smarcel
205240116Smarcel    return atf_tc_run(tc, resfile);
206240116Smarcel}
207240116Smarcel
208240116Smarcelatf_error_t
209240116Smarcelatf_tp_cleanup(const atf_tp_t *tp, const char *tcname)
210240116Smarcel{
211240116Smarcel    const atf_tc_t *tc;
212240116Smarcel
213240116Smarcel    tc = find_tc(tp, tcname);
214240116Smarcel    PRE(tc != NULL);
215240116Smarcel
216240116Smarcel    return atf_tc_cleanup(tc);
217240116Smarcel}
218