xref: /freebsd/contrib/atf/atf-c++/detail/exceptions_test.cpp (revision 8f0ea33f2bbf3a6aa80235f0a02fa5f2780c2b17)
1c243e490SMarcel Moolenaar // Copyright (c) 2009 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
24c243e490SMarcel Moolenaar // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*0677dfd1SJulio Merino 
26*0677dfd1SJulio Merino #include "atf-c++/detail/exceptions.hpp"
27c243e490SMarcel Moolenaar 
28c243e490SMarcel Moolenaar extern "C" {
29*0677dfd1SJulio Merino #include "atf-c/error.h"
30c243e490SMarcel Moolenaar }
31c243e490SMarcel Moolenaar 
32c243e490SMarcel Moolenaar #include <cstdio>
33c243e490SMarcel Moolenaar #include <new>
34c243e490SMarcel Moolenaar 
35*0677dfd1SJulio Merino #include <atf-c++.hpp>
36c243e490SMarcel Moolenaar 
37*0677dfd1SJulio Merino #include "atf-c++/detail/sanity.hpp"
38c243e490SMarcel Moolenaar 
39c243e490SMarcel Moolenaar // ------------------------------------------------------------------------
40c243e490SMarcel Moolenaar // The "test" error.
41c243e490SMarcel Moolenaar // ------------------------------------------------------------------------
42c243e490SMarcel Moolenaar 
43c243e490SMarcel Moolenaar extern "C" {
44c243e490SMarcel Moolenaar 
45c243e490SMarcel Moolenaar struct test_error_data {
46c243e490SMarcel Moolenaar     const char* m_msg;
47c243e490SMarcel Moolenaar };
48c243e490SMarcel Moolenaar typedef struct test_error_data test_error_data_t;
49c243e490SMarcel Moolenaar 
50c243e490SMarcel Moolenaar static
51c243e490SMarcel Moolenaar void
test_format(const atf_error_t err,char * buf,size_t buflen)52c243e490SMarcel Moolenaar test_format(const atf_error_t err, char *buf, size_t buflen)
53c243e490SMarcel Moolenaar {
54c243e490SMarcel Moolenaar     const test_error_data_t* data;
55c243e490SMarcel Moolenaar 
56c243e490SMarcel Moolenaar     PRE(atf_error_is(err, "test"));
57c243e490SMarcel Moolenaar 
58c243e490SMarcel Moolenaar     data = static_cast< const test_error_data_t * >(atf_error_data(err));
59c243e490SMarcel Moolenaar     snprintf(buf, buflen, "Message: %s", data->m_msg);
60c243e490SMarcel Moolenaar }
61c243e490SMarcel Moolenaar 
62c243e490SMarcel Moolenaar static
63c243e490SMarcel Moolenaar atf_error_t
test_error(const char * msg)64c243e490SMarcel Moolenaar test_error(const char* msg)
65c243e490SMarcel Moolenaar {
66c243e490SMarcel Moolenaar     atf_error_t err;
67c243e490SMarcel Moolenaar     test_error_data_t data;
68c243e490SMarcel Moolenaar 
69c243e490SMarcel Moolenaar     data.m_msg = msg;
70c243e490SMarcel Moolenaar 
71c243e490SMarcel Moolenaar     err = atf_error_new("test", &data, sizeof(data), test_format);
72c243e490SMarcel Moolenaar 
73c243e490SMarcel Moolenaar     return err;
74c243e490SMarcel Moolenaar }
75c243e490SMarcel Moolenaar 
76c243e490SMarcel Moolenaar } // extern
77c243e490SMarcel Moolenaar 
78c243e490SMarcel Moolenaar // ------------------------------------------------------------------------
79c243e490SMarcel Moolenaar // Tests cases for the free functions.
80c243e490SMarcel Moolenaar // ------------------------------------------------------------------------
81c243e490SMarcel Moolenaar 
82c243e490SMarcel Moolenaar ATF_TEST_CASE(throw_atf_error_libc);
ATF_TEST_CASE_HEAD(throw_atf_error_libc)83c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(throw_atf_error_libc)
84c243e490SMarcel Moolenaar {
85c243e490SMarcel Moolenaar     set_md_var("descr", "Tests the throw_atf_error function when raising "
86c243e490SMarcel Moolenaar                "a libc error");
87c243e490SMarcel Moolenaar }
ATF_TEST_CASE_BODY(throw_atf_error_libc)88c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(throw_atf_error_libc)
89c243e490SMarcel Moolenaar {
90c243e490SMarcel Moolenaar     try {
91c243e490SMarcel Moolenaar         atf::throw_atf_error(atf_libc_error(1, "System error 1"));
92c243e490SMarcel Moolenaar     } catch (const atf::system_error& e) {
93c243e490SMarcel Moolenaar         ATF_REQUIRE(e.code() == 1);
94c243e490SMarcel Moolenaar         ATF_REQUIRE(std::string(e.what()).find("System error 1") !=
95c243e490SMarcel Moolenaar                   std::string::npos);
96c243e490SMarcel Moolenaar     } catch (const std::exception& e) {
97c243e490SMarcel Moolenaar         ATF_FAIL(std::string("Got unexpected exception: ") + e.what());
98c243e490SMarcel Moolenaar     }
99c243e490SMarcel Moolenaar }
100c243e490SMarcel Moolenaar 
101c243e490SMarcel Moolenaar ATF_TEST_CASE(throw_atf_error_no_memory);
ATF_TEST_CASE_HEAD(throw_atf_error_no_memory)102c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(throw_atf_error_no_memory)
103c243e490SMarcel Moolenaar {
104c243e490SMarcel Moolenaar     set_md_var("descr", "Tests the throw_atf_error function when raising "
105c243e490SMarcel Moolenaar                "a no_memory error");
106c243e490SMarcel Moolenaar }
ATF_TEST_CASE_BODY(throw_atf_error_no_memory)107c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(throw_atf_error_no_memory)
108c243e490SMarcel Moolenaar {
109c243e490SMarcel Moolenaar     try {
110c243e490SMarcel Moolenaar         atf::throw_atf_error(atf_no_memory_error());
111c243e490SMarcel Moolenaar     } catch (const std::bad_alloc&) {
112c243e490SMarcel Moolenaar     } catch (const std::exception& e) {
113c243e490SMarcel Moolenaar         ATF_FAIL(std::string("Got unexpected exception: ") + e.what());
114c243e490SMarcel Moolenaar     }
115c243e490SMarcel Moolenaar }
116c243e490SMarcel Moolenaar 
117c243e490SMarcel Moolenaar ATF_TEST_CASE(throw_atf_error_unknown);
ATF_TEST_CASE_HEAD(throw_atf_error_unknown)118c243e490SMarcel Moolenaar ATF_TEST_CASE_HEAD(throw_atf_error_unknown)
119c243e490SMarcel Moolenaar {
120c243e490SMarcel Moolenaar     set_md_var("descr", "Tests the throw_atf_error function when raising "
121c243e490SMarcel Moolenaar                "an unknown error");
122c243e490SMarcel Moolenaar }
ATF_TEST_CASE_BODY(throw_atf_error_unknown)123c243e490SMarcel Moolenaar ATF_TEST_CASE_BODY(throw_atf_error_unknown)
124c243e490SMarcel Moolenaar {
125c243e490SMarcel Moolenaar     try {
126c243e490SMarcel Moolenaar         atf::throw_atf_error(test_error("The message"));
127c243e490SMarcel Moolenaar     } catch (const std::runtime_error& e) {
128c243e490SMarcel Moolenaar         const std::string msg = e.what();
129c243e490SMarcel Moolenaar         ATF_REQUIRE(msg.find("The message") != std::string::npos);
130c243e490SMarcel Moolenaar     } catch (const std::exception& e) {
131c243e490SMarcel Moolenaar         ATF_FAIL(std::string("Got unexpected exception: ") + e.what());
132c243e490SMarcel Moolenaar     }
133c243e490SMarcel Moolenaar }
134c243e490SMarcel Moolenaar 
135c243e490SMarcel Moolenaar // ------------------------------------------------------------------------
136c243e490SMarcel Moolenaar // Main.
137c243e490SMarcel Moolenaar // ------------------------------------------------------------------------
138c243e490SMarcel Moolenaar 
ATF_INIT_TEST_CASES(tcs)139c243e490SMarcel Moolenaar ATF_INIT_TEST_CASES(tcs)
140c243e490SMarcel Moolenaar {
141c243e490SMarcel Moolenaar     // Add the test cases for the free functions.
142c243e490SMarcel Moolenaar     ATF_ADD_TEST_CASE(tcs, throw_atf_error_libc);
143c243e490SMarcel Moolenaar     ATF_ADD_TEST_CASE(tcs, throw_atf_error_no_memory);
144c243e490SMarcel Moolenaar     ATF_ADD_TEST_CASE(tcs, throw_atf_error_unknown);
145c243e490SMarcel Moolenaar }
146