ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// Copyright (c) 2009 The NetBSD Foundation, Inc.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// All rights reserved.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt//
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// Redistribution and use in source and binary forms, with or without
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// modification, are permitted provided that the following conditions
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// are met:
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// 1. Redistributions of source code must retain the above copyright
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// notice, this list of conditions and the following disclaimer.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// 2. Redistributions in binary form must reproduce the above copyright
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// notice, this list of conditions and the following disclaimer in the
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// documentation and/or other materials provided with the distribution.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt//
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include "atf-c++/detail/exceptions.hpp"
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntextern "C" {
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include "atf-c/error.h"
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <cstdio>
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt#include <new>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include <atf-c++.hpp>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include "atf-c++/detail/sanity.hpp"
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// ------------------------------------------------------------------------
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// The "test" error.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// ------------------------------------------------------------------------
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntextern "C" {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstruct test_error_data {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const char* m_msg;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt};
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunttypedef struct test_error_data test_error_data_t;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstatic
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntvoid
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunttest_format(const atf_error_t err, char *buf, size_t buflen)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const test_error_data_t* data;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt PRE(atf_error_is(err, "test"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt data = static_cast< const test_error_data_t * >(atf_error_data(err));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt snprintf(buf, buflen, "Message: %s", data->m_msg);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstatic
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntatf_error_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunttest_error(const char* msg)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_error_t err;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt test_error_data_t data;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt data.m_msg = msg;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt err = atf_error_new("test", &data, sizeof(data), test_format);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return err;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt} // extern
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// ------------------------------------------------------------------------
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// Tests cases for the free functions.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// ------------------------------------------------------------------------
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE(throw_atf_error_libc);
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE_HEAD(throw_atf_error_libc)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt set_md_var("descr", "Tests the throw_atf_error function when raising "
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "a libc error");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE_BODY(throw_atf_error_libc)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt try {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf::throw_atf_error(atf_libc_error(1, "System error 1"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt } catch (const atf::system_error& e) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(e.code() == 1);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(std::string(e.what()).find("System error 1") !=
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt std::string::npos);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt } catch (const std::exception& e) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_FAIL(std::string("Got unexpected exception: ") + e.what());
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE(throw_atf_error_no_memory);
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE_HEAD(throw_atf_error_no_memory)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt set_md_var("descr", "Tests the throw_atf_error function when raising "
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "a no_memory error");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE_BODY(throw_atf_error_no_memory)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt try {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf::throw_atf_error(atf_no_memory_error());
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt } catch (const std::bad_alloc&) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt } catch (const std::exception& e) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_FAIL(std::string("Got unexpected exception: ") + e.what());
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE(throw_atf_error_unknown);
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE_HEAD(throw_atf_error_unknown)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt set_md_var("descr", "Tests the throw_atf_error function when raising "
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt "an unknown error");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE_BODY(throw_atf_error_unknown)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt try {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf::throw_atf_error(test_error("The message"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt } catch (const std::runtime_error& e) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const std::string msg = e.what();
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(msg.find("The message") != std::string::npos);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt } catch (const std::exception& e) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_FAIL(std::string("Got unexpected exception: ") + e.what());
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// ------------------------------------------------------------------------
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// Main.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// ------------------------------------------------------------------------
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_INIT_TEST_CASES(tcs)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt // Add the test cases for the free functions.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_ADD_TEST_CASE(tcs, throw_atf_error_libc);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_ADD_TEST_CASE(tcs, throw_atf_error_no_memory);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_ADD_TEST_CASE(tcs, throw_atf_error_unknown);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}