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