Path: blob/main/contrib/atf/atf-c++/detail/exceptions_test.cpp
39563 views
// Copyright (c) 2009 The NetBSD Foundation, Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions5// are met:6// 1. Redistributions of source code must retain the above copyright7// notice, this list of conditions and the following disclaimer.8// 2. Redistributions in binary form must reproduce the above copyright9// notice, this list of conditions and the following disclaimer in the10// documentation and/or other materials provided with the distribution.11//12// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND13// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,14// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF15// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE19// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS20// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER21// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR22// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN23// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2425#include "atf-c++/detail/exceptions.hpp"2627extern "C" {28#include "atf-c/error.h"29}3031#include <cstdio>32#include <new>3334#include <atf-c++.hpp>3536#include "atf-c++/detail/sanity.hpp"3738// ------------------------------------------------------------------------39// The "test" error.40// ------------------------------------------------------------------------4142extern "C" {4344struct test_error_data {45const char* m_msg;46};47typedef struct test_error_data test_error_data_t;4849static50void51test_format(const atf_error_t err, char *buf, size_t buflen)52{53const test_error_data_t* data;5455PRE(atf_error_is(err, "test"));5657data = static_cast< const test_error_data_t * >(atf_error_data(err));58snprintf(buf, buflen, "Message: %s", data->m_msg);59}6061static62atf_error_t63test_error(const char* msg)64{65atf_error_t err;66test_error_data_t data;6768data.m_msg = msg;6970err = atf_error_new("test", &data, sizeof(data), test_format);7172return err;73}7475} // extern7677// ------------------------------------------------------------------------78// Tests cases for the free functions.79// ------------------------------------------------------------------------8081ATF_TEST_CASE(throw_atf_error_libc);82ATF_TEST_CASE_HEAD(throw_atf_error_libc)83{84set_md_var("descr", "Tests the throw_atf_error function when raising "85"a libc error");86}87ATF_TEST_CASE_BODY(throw_atf_error_libc)88{89try {90atf::throw_atf_error(atf_libc_error(1, "System error 1"));91} catch (const atf::system_error& e) {92ATF_REQUIRE(e.code() == 1);93ATF_REQUIRE(std::string(e.what()).find("System error 1") !=94std::string::npos);95} catch (const std::exception& e) {96ATF_FAIL(std::string("Got unexpected exception: ") + e.what());97}98}99100ATF_TEST_CASE(throw_atf_error_no_memory);101ATF_TEST_CASE_HEAD(throw_atf_error_no_memory)102{103set_md_var("descr", "Tests the throw_atf_error function when raising "104"a no_memory error");105}106ATF_TEST_CASE_BODY(throw_atf_error_no_memory)107{108try {109atf::throw_atf_error(atf_no_memory_error());110} catch (const std::bad_alloc&) {111} catch (const std::exception& e) {112ATF_FAIL(std::string("Got unexpected exception: ") + e.what());113}114}115116ATF_TEST_CASE(throw_atf_error_unknown);117ATF_TEST_CASE_HEAD(throw_atf_error_unknown)118{119set_md_var("descr", "Tests the throw_atf_error function when raising "120"an unknown error");121}122ATF_TEST_CASE_BODY(throw_atf_error_unknown)123{124try {125atf::throw_atf_error(test_error("The message"));126} catch (const std::runtime_error& e) {127const std::string msg = e.what();128ATF_REQUIRE(msg.find("The message") != std::string::npos);129} catch (const std::exception& e) {130ATF_FAIL(std::string("Got unexpected exception: ") + e.what());131}132}133134// ------------------------------------------------------------------------135// Main.136// ------------------------------------------------------------------------137138ATF_INIT_TEST_CASES(tcs)139{140// Add the test cases for the free functions.141ATF_ADD_TEST_CASE(tcs, throw_atf_error_libc);142ATF_ADD_TEST_CASE(tcs, throw_atf_error_no_memory);143ATF_ADD_TEST_CASE(tcs, throw_atf_error_unknown);144}145146147