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