Home | History | Annotate | Line # | Download | only in detail
      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 
     30 extern "C" {
     31 #include "../../atf-c/error.h"
     32 }
     33 
     34 #include <cstdio>
     35 #include <new>
     36 
     37 #include "../macros.hpp"
     38 
     39 #include "exceptions.hpp"
     40 #include "sanity.hpp"
     41 
     42 // ------------------------------------------------------------------------
     43 // The "test" error.
     44 // ------------------------------------------------------------------------
     45 
     46 extern "C" {
     47 
     48 struct test_error_data {
     49     const char* m_msg;
     50 };
     51 typedef struct test_error_data test_error_data_t;
     52 
     53 static
     54 void
     55 test_format(const atf_error_t err, char *buf, size_t buflen)
     56 {
     57     const test_error_data_t* data;
     58 
     59     PRE(atf_error_is(err, "test"));
     60 
     61     data = static_cast< const test_error_data_t * >(atf_error_data(err));
     62     snprintf(buf, buflen, "Message: %s", data->m_msg);
     63 }
     64 
     65 static
     66 atf_error_t
     67 test_error(const char* msg)
     68 {
     69     atf_error_t err;
     70     test_error_data_t data;
     71 
     72     data.m_msg = msg;
     73 
     74     err = atf_error_new("test", &data, sizeof(data), test_format);
     75 
     76     return err;
     77 }
     78 
     79 } // extern
     80 
     81 // ------------------------------------------------------------------------
     82 // Tests cases for the free functions.
     83 // ------------------------------------------------------------------------
     84 
     85 ATF_TEST_CASE(throw_atf_error_libc);
     86 ATF_TEST_CASE_HEAD(throw_atf_error_libc)
     87 {
     88     set_md_var("descr", "Tests the throw_atf_error function when raising "
     89                "a libc error");
     90 }
     91 ATF_TEST_CASE_BODY(throw_atf_error_libc)
     92 {
     93     try {
     94         atf::throw_atf_error(atf_libc_error(1, "System error 1"));
     95     } catch (const atf::system_error& e) {
     96         ATF_REQUIRE(e.code() == 1);
     97         ATF_REQUIRE(std::string(e.what()).find("System error 1") !=
     98                   std::string::npos);
     99     } catch (const std::exception& e) {
    100         ATF_FAIL(std::string("Got unexpected exception: ") + e.what());
    101     }
    102 }
    103 
    104 ATF_TEST_CASE(throw_atf_error_no_memory);
    105 ATF_TEST_CASE_HEAD(throw_atf_error_no_memory)
    106 {
    107     set_md_var("descr", "Tests the throw_atf_error function when raising "
    108                "a no_memory error");
    109 }
    110 ATF_TEST_CASE_BODY(throw_atf_error_no_memory)
    111 {
    112     try {
    113         atf::throw_atf_error(atf_no_memory_error());
    114     } catch (const std::bad_alloc&) {
    115     } catch (const std::exception& e) {
    116         ATF_FAIL(std::string("Got unexpected exception: ") + e.what());
    117     }
    118 }
    119 
    120 ATF_TEST_CASE(throw_atf_error_unknown);
    121 ATF_TEST_CASE_HEAD(throw_atf_error_unknown)
    122 {
    123     set_md_var("descr", "Tests the throw_atf_error function when raising "
    124                "an unknown error");
    125 }
    126 ATF_TEST_CASE_BODY(throw_atf_error_unknown)
    127 {
    128     try {
    129         atf::throw_atf_error(test_error("The message"));
    130     } catch (const std::runtime_error& e) {
    131         const std::string msg = e.what();
    132         ATF_REQUIRE(msg.find("The message") != std::string::npos);
    133     } catch (const std::exception& e) {
    134         ATF_FAIL(std::string("Got unexpected exception: ") + e.what());
    135     }
    136 }
    137 
    138 // ------------------------------------------------------------------------
    139 // Main.
    140 // ------------------------------------------------------------------------
    141 
    142 ATF_INIT_TEST_CASES(tcs)
    143 {
    144     // Add the test cases for the free functions.
    145     ATF_ADD_TEST_CASE(tcs, throw_atf_error_libc);
    146     ATF_ADD_TEST_CASE(tcs, throw_atf_error_no_memory);
    147     ATF_ADD_TEST_CASE(tcs, throw_atf_error_unknown);
    148 }
    149