tc_test.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 <stdbool.h>
31240116Smarcel#include <string.h>
32240116Smarcel
33240116Smarcel#include <atf-c.h>
34240116Smarcel
35240116Smarcel#include "detail/test_helpers.h"
36240116Smarcel
37240116Smarcel/* ---------------------------------------------------------------------
38240116Smarcel * Auxiliary test cases.
39240116Smarcel * --------------------------------------------------------------------- */
40240116Smarcel
41240116SmarcelATF_TC_HEAD(empty, tc)
42240116Smarcel{
43240116Smarcel    if (tc != NULL) {}
44240116Smarcel}
45240116SmarcelATF_TC_BODY(empty, tc)
46240116Smarcel{
47240116Smarcel}
48240116Smarcel
49240116SmarcelATF_TC_HEAD(test_var, tc)
50240116Smarcel{
51240116Smarcel    atf_tc_set_md_var(tc, "test-var", "Test text");
52240116Smarcel}
53240116Smarcel
54240116Smarcel/* ---------------------------------------------------------------------
55240116Smarcel * Test cases for the "atf_tc_t" type.
56240116Smarcel * --------------------------------------------------------------------- */
57240116Smarcel
58240116SmarcelATF_TC(init);
59240116SmarcelATF_TC_HEAD(init, tc)
60240116Smarcel{
61240116Smarcel    atf_tc_set_md_var(tc, "descr", "Tests the atf_tc_init function");
62240116Smarcel}
63240116SmarcelATF_TC_BODY(init, tcin)
64240116Smarcel{
65240116Smarcel    atf_tc_t tc;
66240116Smarcel
67240116Smarcel    RE(atf_tc_init(&tc, "test1", ATF_TC_HEAD_NAME(empty),
68240116Smarcel                   ATF_TC_BODY_NAME(empty), NULL, NULL));
69240116Smarcel    ATF_REQUIRE(strcmp(atf_tc_get_ident(&tc), "test1") == 0);
70240116Smarcel    ATF_REQUIRE(!atf_tc_has_md_var(&tc, "test-var"));
71240116Smarcel    atf_tc_fini(&tc);
72240116Smarcel
73240116Smarcel    RE(atf_tc_init(&tc, "test2", ATF_TC_HEAD_NAME(test_var),
74240116Smarcel                   ATF_TC_BODY_NAME(empty), NULL, NULL));
75240116Smarcel    ATF_REQUIRE(strcmp(atf_tc_get_ident(&tc), "test2") == 0);
76240116Smarcel    ATF_REQUIRE(atf_tc_has_md_var(&tc, "test-var"));
77240116Smarcel    atf_tc_fini(&tc);
78240116Smarcel}
79240116Smarcel
80240116SmarcelATF_TC(init_pack);
81240116SmarcelATF_TC_HEAD(init_pack, tc)
82240116Smarcel{
83240116Smarcel    atf_tc_set_md_var(tc, "descr", "Tests the atf_tc_init_pack function");
84240116Smarcel}
85240116SmarcelATF_TC_BODY(init_pack, tcin)
86240116Smarcel{
87240116Smarcel    atf_tc_t tc;
88240116Smarcel    atf_tc_pack_t tcp1 = {
89240116Smarcel        .m_ident = "test1",
90240116Smarcel        .m_head = ATF_TC_HEAD_NAME(empty),
91240116Smarcel        .m_body = ATF_TC_BODY_NAME(empty),
92240116Smarcel        .m_cleanup = NULL,
93240116Smarcel    };
94240116Smarcel    atf_tc_pack_t tcp2 = {
95240116Smarcel        .m_ident = "test2",
96240116Smarcel        .m_head = ATF_TC_HEAD_NAME(test_var),
97240116Smarcel        .m_body = ATF_TC_BODY_NAME(empty),
98240116Smarcel        .m_cleanup = NULL,
99240116Smarcel    };
100240116Smarcel
101240116Smarcel    RE(atf_tc_init_pack(&tc, &tcp1, NULL));
102240116Smarcel    ATF_REQUIRE(strcmp(atf_tc_get_ident(&tc), "test1") == 0);
103240116Smarcel    ATF_REQUIRE(!atf_tc_has_md_var(&tc, "test-var"));
104240116Smarcel    atf_tc_fini(&tc);
105240116Smarcel
106240116Smarcel    RE(atf_tc_init_pack(&tc, &tcp2, NULL));
107240116Smarcel    ATF_REQUIRE(strcmp(atf_tc_get_ident(&tc), "test2") == 0);
108240116Smarcel    ATF_REQUIRE(atf_tc_has_md_var(&tc, "test-var"));
109240116Smarcel    atf_tc_fini(&tc);
110240116Smarcel}
111240116Smarcel
112240116SmarcelATF_TC(vars);
113240116SmarcelATF_TC_HEAD(vars, tc)
114240116Smarcel{
115240116Smarcel    atf_tc_set_md_var(tc, "descr", "Tests the atf_tc_get_md_var, "
116240116Smarcel                      "atf_tc_has_md_var and atf_tc_set_md_var functions");
117240116Smarcel}
118240116SmarcelATF_TC_BODY(vars, tcin)
119240116Smarcel{
120240116Smarcel    atf_tc_t tc;
121240116Smarcel
122240116Smarcel    RE(atf_tc_init(&tc, "test1", ATF_TC_HEAD_NAME(empty),
123240116Smarcel                   ATF_TC_BODY_NAME(empty), NULL, NULL));
124240116Smarcel    ATF_REQUIRE(!atf_tc_has_md_var(&tc, "test-var"));
125240116Smarcel    RE(atf_tc_set_md_var(&tc, "test-var", "Test value"));
126240116Smarcel    ATF_REQUIRE(atf_tc_has_md_var(&tc, "test-var"));
127240116Smarcel    ATF_REQUIRE(strcmp(atf_tc_get_md_var(&tc, "test-var"), "Test value") == 0);
128240116Smarcel    atf_tc_fini(&tc);
129240116Smarcel}
130240116Smarcel
131240116SmarcelATF_TC(config);
132240116SmarcelATF_TC_HEAD(config, tc)
133240116Smarcel{
134240116Smarcel    atf_tc_set_md_var(tc, "descr", "Tests the atf_tc_get_config_var, "
135240116Smarcel                      "atf_tc_get_config_var_wd and atf_tc_has_config_var "
136240116Smarcel                      "functions");
137240116Smarcel}
138240116SmarcelATF_TC_BODY(config, tcin)
139240116Smarcel{
140240116Smarcel    atf_tc_t tc;
141240116Smarcel    const char *const config[] = { "test-var", "test-value", NULL };
142240116Smarcel
143240116Smarcel    RE(atf_tc_init(&tc, "test1", ATF_TC_HEAD_NAME(empty),
144240116Smarcel                   ATF_TC_BODY_NAME(empty), NULL, NULL));
145240116Smarcel    ATF_REQUIRE(!atf_tc_has_config_var(&tc, "test-var"));
146240116Smarcel    ATF_REQUIRE(!atf_tc_has_md_var(&tc, "test-var"));
147240116Smarcel    atf_tc_fini(&tc);
148240116Smarcel
149240116Smarcel    RE(atf_tc_init(&tc, "test1", ATF_TC_HEAD_NAME(empty),
150240116Smarcel                   ATF_TC_BODY_NAME(empty), NULL, config));
151240116Smarcel    ATF_REQUIRE(atf_tc_has_config_var(&tc, "test-var"));
152240116Smarcel    ATF_REQUIRE(strcmp(atf_tc_get_config_var(&tc, "test-var"),
153240116Smarcel                     "test-value") == 0);
154240116Smarcel    ATF_REQUIRE(!atf_tc_has_md_var(&tc, "test-var"));
155240116Smarcel    ATF_REQUIRE(!atf_tc_has_config_var(&tc, "test-var2"));
156240116Smarcel    ATF_REQUIRE(strcmp(atf_tc_get_config_var_wd(&tc, "test-var2", "def-value"),
157240116Smarcel                     "def-value") == 0);
158240116Smarcel    atf_tc_fini(&tc);
159240116Smarcel}
160240116Smarcel
161240116Smarcel/* ---------------------------------------------------------------------
162240116Smarcel * Test cases for the free functions.
163240116Smarcel * --------------------------------------------------------------------- */
164240116Smarcel
165240116Smarcel/* TODO: Add test cases for atf_tc_run.  This is going to be very tough,
166240116Smarcel * but good tests here could allow us to avoid much of the indirect
167240116Smarcel * testing done later on. */
168240116Smarcel
169240116Smarcel/* ---------------------------------------------------------------------
170240116Smarcel * Tests cases for the header file.
171240116Smarcel * --------------------------------------------------------------------- */
172240116Smarcel
173240116SmarcelHEADER_TC(include, "atf-c/tc.h");
174240116Smarcel
175240116Smarcel/* ---------------------------------------------------------------------
176240116Smarcel * Main.
177240116Smarcel * --------------------------------------------------------------------- */
178240116Smarcel
179240116SmarcelATF_TP_ADD_TCS(tp)
180240116Smarcel{
181240116Smarcel    /* Add the test cases for the "atf_tcr_t" type. */
182240116Smarcel    ATF_TP_ADD_TC(tp, init);
183240116Smarcel    ATF_TP_ADD_TC(tp, init_pack);
184240116Smarcel    ATF_TP_ADD_TC(tp, vars);
185240116Smarcel    ATF_TP_ADD_TC(tp, config);
186240116Smarcel
187240116Smarcel    /* Add the test cases for the free functions. */
188240116Smarcel    /* TODO */
189240116Smarcel
190240116Smarcel    /* Add the test cases for the header file. */
191240116Smarcel    ATF_TP_ADD_TC(tp, include);
192240116Smarcel
193240116Smarcel    return atf_no_error();
194240116Smarcel}
195