tests.hpp revision 275988
1// Copyright (c) 2007 The NetBSD Foundation, Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions
6// are met:
7// 1. Redistributions of source code must retain the above copyright
8//    notice, this list of conditions and the following disclaimer.
9// 2. Redistributions in binary form must reproduce the above copyright
10//    notice, this list of conditions and the following disclaimer in the
11//    documentation and/or other materials provided with the distribution.
12//
13// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26#if !defined(ATF_CXX_TESTS_HPP)
27#define ATF_CXX_TESTS_HPP
28
29#include <map>
30#include <memory>
31#include <string>
32
33extern "C" {
34#include <atf-c/defs.h>
35}
36
37namespace atf {
38namespace tests {
39
40namespace detail {
41
42class atf_tp_writer {
43    std::ostream& m_os;
44
45    bool m_is_first;
46
47public:
48    atf_tp_writer(std::ostream&);
49
50    void start_tc(const std::string&);
51    void end_tc(void);
52    void tc_meta_data(const std::string&, const std::string&);
53};
54
55bool match(const std::string&, const std::string&);
56
57} // namespace
58
59// ------------------------------------------------------------------------
60// The "vars_map" class.
61// ------------------------------------------------------------------------
62
63typedef std::map< std::string, std::string > vars_map;
64
65// ------------------------------------------------------------------------
66// The "tc" class.
67// ------------------------------------------------------------------------
68
69struct tc_impl;
70
71class tc {
72    // Non-copyable.
73    tc(const tc&);
74    tc& operator=(const tc&);
75
76    std::auto_ptr< tc_impl > pimpl;
77
78protected:
79    virtual void head(void);
80    virtual void body(void) const = 0;
81    virtual void cleanup(void) const;
82
83    void require_prog(const std::string&) const;
84
85    friend struct tc_impl;
86
87public:
88    tc(const std::string&, const bool);
89    virtual ~tc(void);
90
91    void init(const vars_map&);
92
93    const std::string get_config_var(const std::string&) const;
94    const std::string get_config_var(const std::string&, const std::string&)
95        const;
96    const std::string get_md_var(const std::string&) const;
97    const vars_map get_md_vars(void) const;
98    bool has_config_var(const std::string&) const;
99    bool has_md_var(const std::string&) const;
100    void set_md_var(const std::string&, const std::string&);
101
102    void run(const std::string&) const;
103    void run_cleanup(void) const;
104
105    // To be called from the child process only.
106    static void pass(void) ATF_DEFS_ATTRIBUTE_NORETURN;
107    static void fail(const std::string&) ATF_DEFS_ATTRIBUTE_NORETURN;
108    static void fail_nonfatal(const std::string&);
109    static void skip(const std::string&) ATF_DEFS_ATTRIBUTE_NORETURN;
110    static void check_errno(const char*, const int, const int, const char*,
111                            const bool);
112    static void require_errno(const char*, const int, const int, const char*,
113                              const bool);
114    static void expect_pass(void);
115    static void expect_fail(const std::string&);
116    static void expect_exit(const int, const std::string&);
117    static void expect_signal(const int, const std::string&);
118    static void expect_death(const std::string&);
119    static void expect_timeout(const std::string&);
120};
121
122} // namespace tests
123} // namespace atf
124
125#endif // !defined(ATF_CXX_TESTS_HPP)
126