1//
2// Automated Testing Framework (atf)
3//
4// Copyright (c) 2009 The NetBSD Foundation, Inc.
5// All rights reserved.
6//
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
10// 1. Redistributions of source code must retain the above copyright
11//    notice, this list of conditions and the following disclaimer.
12// 2. Redistributions in binary form must reproduce the above copyright
13//    notice, this list of conditions and the following disclaimer in the
14//    documentation and/or other materials provided with the distribution.
15//
16// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28//
29
30extern "C" {
31#include "../../atf-c/error.h"
32}
33
34#include <cstdio>
35
36#include "../macros.hpp"
37
38#include "exceptions.hpp"
39#include "sanity.hpp"
40
41// ------------------------------------------------------------------------
42// The "test" error.
43// ------------------------------------------------------------------------
44
45extern "C" {
46
47struct test_error_data {
48    const char* m_msg;
49};
50typedef struct test_error_data test_error_data_t;
51
52static
53void
54test_format(const atf_error_t err, char *buf, size_t buflen)
55{
56    const test_error_data_t* data;
57
58    PRE(atf_error_is(err, "test"));
59
60    data = static_cast< const test_error_data_t * >(atf_error_data(err));
61    snprintf(buf, buflen, "Message: %s", data->m_msg);
62}
63
64static
65atf_error_t
66test_error(const char* msg)
67{
68    atf_error_t err;
69    test_error_data_t data;
70
71    data.m_msg = msg;
72
73    err = atf_error_new("test", &data, sizeof(data), test_format);
74
75    return err;
76}
77
78} // extern
79
80// ------------------------------------------------------------------------
81// Tests cases for the free functions.
82// ------------------------------------------------------------------------
83
84ATF_TEST_CASE(throw_atf_error_libc);
85ATF_TEST_CASE_HEAD(throw_atf_error_libc)
86{
87    set_md_var("descr", "Tests the throw_atf_error function when raising "
88               "a libc error");
89}
90ATF_TEST_CASE_BODY(throw_atf_error_libc)
91{
92    try {
93        atf::throw_atf_error(atf_libc_error(1, "System error 1"));
94    } catch (const atf::system_error& e) {
95        ATF_REQUIRE(e.code() == 1);
96        ATF_REQUIRE(std::string(e.what()).find("System error 1") !=
97                  std::string::npos);
98    } catch (const std::exception& e) {
99        ATF_FAIL(std::string("Got unexpected exception: ") + e.what());
100    }
101}
102
103ATF_TEST_CASE(throw_atf_error_no_memory);
104ATF_TEST_CASE_HEAD(throw_atf_error_no_memory)
105{
106    set_md_var("descr", "Tests the throw_atf_error function when raising "
107               "a no_memory error");
108}
109ATF_TEST_CASE_BODY(throw_atf_error_no_memory)
110{
111    try {
112        atf::throw_atf_error(atf_no_memory_error());
113    } catch (const std::bad_alloc&) {
114    } catch (const std::exception& e) {
115        ATF_FAIL(std::string("Got unexpected exception: ") + e.what());
116    }
117}
118
119ATF_TEST_CASE(throw_atf_error_unknown);
120ATF_TEST_CASE_HEAD(throw_atf_error_unknown)
121{
122    set_md_var("descr", "Tests the throw_atf_error function when raising "
123               "an unknown error");
124}
125ATF_TEST_CASE_BODY(throw_atf_error_unknown)
126{
127    try {
128        atf::throw_atf_error(test_error("The message"));
129    } catch (const std::runtime_error& e) {
130        const std::string msg = e.what();
131        ATF_REQUIRE(msg.find("The message") != std::string::npos);
132    } catch (const std::exception& e) {
133        ATF_FAIL(std::string("Got unexpected exception: ") + e.what());
134    }
135}
136
137// ------------------------------------------------------------------------
138// Main.
139// ------------------------------------------------------------------------
140
141ATF_INIT_TEST_CASES(tcs)
142{
143    // Add the test cases for the free functions.
144    ATF_ADD_TEST_CASE(tcs, throw_atf_error_libc);
145    ATF_ADD_TEST_CASE(tcs, throw_atf_error_no_memory);
146    ATF_ADD_TEST_CASE(tcs, throw_atf_error_unknown);
147}
148