1240116Smarcel// Copyright (c) 2009 The NetBSD Foundation, Inc.
2240116Smarcel// All rights reserved.
3240116Smarcel//
4240116Smarcel// Redistribution and use in source and binary forms, with or without
5240116Smarcel// modification, are permitted provided that the following conditions
6240116Smarcel// are met:
7240116Smarcel// 1. Redistributions of source code must retain the above copyright
8240116Smarcel//    notice, this list of conditions and the following disclaimer.
9240116Smarcel// 2. Redistributions in binary form must reproduce the above copyright
10240116Smarcel//    notice, this list of conditions and the following disclaimer in the
11240116Smarcel//    documentation and/or other materials provided with the distribution.
12240116Smarcel//
13240116Smarcel// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14240116Smarcel// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15240116Smarcel// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16240116Smarcel// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17240116Smarcel// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18240116Smarcel// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19240116Smarcel// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20240116Smarcel// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21240116Smarcel// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22240116Smarcel// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23240116Smarcel// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24240116Smarcel// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25240116Smarcel
26273929Sjmmv#include "atf-c++/detail/exceptions.hpp"
27273929Sjmmv
28240116Smarcelextern "C" {
29273929Sjmmv#include "atf-c/error.h"
30240116Smarcel}
31240116Smarcel
32240116Smarcel#include <cstdio>
33240116Smarcel#include <new>
34240116Smarcel
35273929Sjmmv#include <atf-c++.hpp>
36240116Smarcel
37273929Sjmmv#include "atf-c++/detail/sanity.hpp"
38240116Smarcel
39240116Smarcel// ------------------------------------------------------------------------
40240116Smarcel// The "test" error.
41240116Smarcel// ------------------------------------------------------------------------
42240116Smarcel
43240116Smarcelextern "C" {
44240116Smarcel
45240116Smarcelstruct test_error_data {
46240116Smarcel    const char* m_msg;
47240116Smarcel};
48240116Smarceltypedef struct test_error_data test_error_data_t;
49240116Smarcel
50240116Smarcelstatic
51240116Smarcelvoid
52240116Smarceltest_format(const atf_error_t err, char *buf, size_t buflen)
53240116Smarcel{
54240116Smarcel    const test_error_data_t* data;
55240116Smarcel
56240116Smarcel    PRE(atf_error_is(err, "test"));
57240116Smarcel
58240116Smarcel    data = static_cast< const test_error_data_t * >(atf_error_data(err));
59240116Smarcel    snprintf(buf, buflen, "Message: %s", data->m_msg);
60240116Smarcel}
61240116Smarcel
62240116Smarcelstatic
63240116Smarcelatf_error_t
64240116Smarceltest_error(const char* msg)
65240116Smarcel{
66240116Smarcel    atf_error_t err;
67240116Smarcel    test_error_data_t data;
68240116Smarcel
69240116Smarcel    data.m_msg = msg;
70240116Smarcel
71240116Smarcel    err = atf_error_new("test", &data, sizeof(data), test_format);
72240116Smarcel
73240116Smarcel    return err;
74240116Smarcel}
75240116Smarcel
76240116Smarcel} // extern
77240116Smarcel
78240116Smarcel// ------------------------------------------------------------------------
79240116Smarcel// Tests cases for the free functions.
80240116Smarcel// ------------------------------------------------------------------------
81240116Smarcel
82240116SmarcelATF_TEST_CASE(throw_atf_error_libc);
83240116SmarcelATF_TEST_CASE_HEAD(throw_atf_error_libc)
84240116Smarcel{
85240116Smarcel    set_md_var("descr", "Tests the throw_atf_error function when raising "
86240116Smarcel               "a libc error");
87240116Smarcel}
88240116SmarcelATF_TEST_CASE_BODY(throw_atf_error_libc)
89240116Smarcel{
90240116Smarcel    try {
91240116Smarcel        atf::throw_atf_error(atf_libc_error(1, "System error 1"));
92240116Smarcel    } catch (const atf::system_error& e) {
93240116Smarcel        ATF_REQUIRE(e.code() == 1);
94240116Smarcel        ATF_REQUIRE(std::string(e.what()).find("System error 1") !=
95240116Smarcel                  std::string::npos);
96240116Smarcel    } catch (const std::exception& e) {
97240116Smarcel        ATF_FAIL(std::string("Got unexpected exception: ") + e.what());
98240116Smarcel    }
99240116Smarcel}
100240116Smarcel
101240116SmarcelATF_TEST_CASE(throw_atf_error_no_memory);
102240116SmarcelATF_TEST_CASE_HEAD(throw_atf_error_no_memory)
103240116Smarcel{
104240116Smarcel    set_md_var("descr", "Tests the throw_atf_error function when raising "
105240116Smarcel               "a no_memory error");
106240116Smarcel}
107240116SmarcelATF_TEST_CASE_BODY(throw_atf_error_no_memory)
108240116Smarcel{
109240116Smarcel    try {
110240116Smarcel        atf::throw_atf_error(atf_no_memory_error());
111240116Smarcel    } catch (const std::bad_alloc&) {
112240116Smarcel    } catch (const std::exception& e) {
113240116Smarcel        ATF_FAIL(std::string("Got unexpected exception: ") + e.what());
114240116Smarcel    }
115240116Smarcel}
116240116Smarcel
117240116SmarcelATF_TEST_CASE(throw_atf_error_unknown);
118240116SmarcelATF_TEST_CASE_HEAD(throw_atf_error_unknown)
119240116Smarcel{
120240116Smarcel    set_md_var("descr", "Tests the throw_atf_error function when raising "
121240116Smarcel               "an unknown error");
122240116Smarcel}
123240116SmarcelATF_TEST_CASE_BODY(throw_atf_error_unknown)
124240116Smarcel{
125240116Smarcel    try {
126240116Smarcel        atf::throw_atf_error(test_error("The message"));
127240116Smarcel    } catch (const std::runtime_error& e) {
128240116Smarcel        const std::string msg = e.what();
129240116Smarcel        ATF_REQUIRE(msg.find("The message") != std::string::npos);
130240116Smarcel    } catch (const std::exception& e) {
131240116Smarcel        ATF_FAIL(std::string("Got unexpected exception: ") + e.what());
132240116Smarcel    }
133240116Smarcel}
134240116Smarcel
135240116Smarcel// ------------------------------------------------------------------------
136240116Smarcel// Main.
137240116Smarcel// ------------------------------------------------------------------------
138240116Smarcel
139240116SmarcelATF_INIT_TEST_CASES(tcs)
140240116Smarcel{
141240116Smarcel    // Add the test cases for the free functions.
142240116Smarcel    ATF_ADD_TEST_CASE(tcs, throw_atf_error_libc);
143240116Smarcel    ATF_ADD_TEST_CASE(tcs, throw_atf_error_no_memory);
144240116Smarcel    ATF_ADD_TEST_CASE(tcs, throw_atf_error_unknown);
145240116Smarcel}
146