1273929Sjmmv/* Copyright (c) 2008 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 24273929Sjmmv * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 25240116Smarcel 26273929Sjmmv#include "atf-c/error.h" 27273929Sjmmv 28240116Smarcel#include <errno.h> 29240116Smarcel#include <stdint.h> 30240116Smarcel#include <stdio.h> 31240116Smarcel#include <string.h> 32240116Smarcel 33240116Smarcel#include <atf-c.h> 34240116Smarcel 35240116Smarcel#include "atf-c/defs.h" 36240116Smarcel 37240116Smarcel/* --------------------------------------------------------------------- 38240116Smarcel * Auxiliary functions. 39240116Smarcel * --------------------------------------------------------------------- */ 40240116Smarcel 41240116Smarcelstatic 42240116Smarcelvoid 43240116Smarceltest_format(const atf_error_t err ATF_DEFS_ATTRIBUTE_UNUSED, 44240116Smarcel char *buf, size_t buflen) 45240116Smarcel{ 46240116Smarcel snprintf(buf, buflen, "Test formatting function"); 47240116Smarcel} 48240116Smarcel 49240116Smarcel/* --------------------------------------------------------------------- 50240116Smarcel * Tests for the "atf_error" type. 51240116Smarcel * --------------------------------------------------------------------- */ 52240116Smarcel 53240116SmarcelATF_TC(error_new); 54240116SmarcelATF_TC_HEAD(error_new, tc) 55240116Smarcel{ 56240116Smarcel atf_tc_set_md_var(tc, "descr", "Checks the construction of an error " 57240116Smarcel "object"); 58240116Smarcel} 59240116SmarcelATF_TC_BODY(error_new, tc) 60240116Smarcel{ 61240116Smarcel atf_error_t err; 62240116Smarcel int data; 63240116Smarcel 64240116Smarcel err = atf_error_new("test_error", NULL, 0, NULL); 65240116Smarcel ATF_REQUIRE(atf_error_is(err, "test_error")); 66240116Smarcel ATF_REQUIRE(!atf_error_is(err, "unknown_error")); 67240116Smarcel ATF_REQUIRE(atf_error_data(err) == NULL); 68240116Smarcel atf_error_free(err); 69240116Smarcel 70240116Smarcel data = 5; 71240116Smarcel err = atf_error_new("test_data_error", &data, sizeof(data), NULL); 72240116Smarcel ATF_REQUIRE(atf_error_is(err, "test_data_error")); 73240116Smarcel ATF_REQUIRE(!atf_error_is(err, "unknown_error")); 74240116Smarcel ATF_REQUIRE(atf_error_data(err) != NULL); 75240116Smarcel ATF_REQUIRE_EQ(*((const int *)atf_error_data(err)), 5); 76240116Smarcel atf_error_free(err); 77240116Smarcel} 78240116Smarcel 79240116SmarcelATF_TC(error_new_wo_memory); 80240116SmarcelATF_TC_HEAD(error_new_wo_memory, tc) 81240116Smarcel{ 82240116Smarcel atf_tc_set_md_var(tc, "descr", "Checks that an unavailable memory error " 83240116Smarcel "raised when constructing an error object " 84240116Smarcel "is properly converted to the no_memory " 85240116Smarcel "static error type"); 86240116Smarcel} 87240116SmarcelATF_TC_BODY(error_new_wo_memory, tc) 88240116Smarcel{ 89240116Smarcel atf_error_t err; 90240116Smarcel void *invalid; 91240116Smarcel 92240116Smarcel invalid = (void *)1; 93240116Smarcel 94240116Smarcel err = atf_error_new("test_error", invalid, SIZE_MAX, NULL); 95240116Smarcel ATF_REQUIRE(atf_error_is(err, "no_memory")); 96240116Smarcel ATF_REQUIRE(atf_error_data(err) == NULL); 97240116Smarcel atf_error_free(err); 98240116Smarcel} 99240116Smarcel 100240116SmarcelATF_TC(no_error); 101240116SmarcelATF_TC_HEAD(no_error, tc) 102240116Smarcel{ 103240116Smarcel atf_tc_set_md_var(tc, "descr", "Checks that constructing a non-error " 104240116Smarcel "object works"); 105240116Smarcel} 106240116SmarcelATF_TC_BODY(no_error, tc) 107240116Smarcel{ 108240116Smarcel atf_error_t err; 109240116Smarcel 110240116Smarcel err = atf_no_error(); 111240116Smarcel ATF_REQUIRE(!atf_is_error(err)); 112240116Smarcel} 113240116Smarcel 114240116SmarcelATF_TC(is_error); 115240116SmarcelATF_TC_HEAD(is_error, tc) 116240116Smarcel{ 117240116Smarcel atf_tc_set_md_var(tc, "descr", "Checks the is_error method to determine " 118240116Smarcel "if an error object holds success or an error"); 119240116Smarcel} 120240116SmarcelATF_TC_BODY(is_error, tc) 121240116Smarcel{ 122240116Smarcel atf_error_t err; 123240116Smarcel 124240116Smarcel err = atf_no_error(); 125240116Smarcel ATF_REQUIRE(!atf_is_error(err)); 126240116Smarcel 127240116Smarcel err = atf_error_new("test_error", NULL, 0, NULL); 128240116Smarcel ATF_REQUIRE(atf_is_error(err)); 129240116Smarcel atf_error_free(err); 130240116Smarcel} 131240116Smarcel 132240116SmarcelATF_TC(format); 133240116SmarcelATF_TC_HEAD(format, tc) 134240116Smarcel{ 135240116Smarcel atf_tc_set_md_var(tc, "descr", "Checks the default formatting function " 136240116Smarcel "and the ability to change it"); 137240116Smarcel} 138240116SmarcelATF_TC_BODY(format, tc) 139240116Smarcel{ 140240116Smarcel atf_error_t err; 141240116Smarcel char buf[1024]; 142240116Smarcel 143240116Smarcel printf("Testing default formatting function\n"); 144240116Smarcel err = atf_error_new("test_error", NULL, 0, NULL); 145240116Smarcel atf_error_format(err, buf, sizeof(buf)); 146240116Smarcel printf("Error string is: %s\n", buf); 147240116Smarcel ATF_REQUIRE(strcmp(buf, "Error 'test_error'") == 0); 148240116Smarcel atf_error_free(err); 149240116Smarcel 150240116Smarcel printf("Testing custom formatting function\n"); 151240116Smarcel err = atf_error_new("test_error", NULL, 0, test_format); 152240116Smarcel atf_error_format(err, buf, sizeof(buf)); 153240116Smarcel printf("Error string is: %s\n", buf); 154240116Smarcel ATF_REQUIRE(strcmp(buf, "Test formatting function") == 0); 155240116Smarcel atf_error_free(err); 156240116Smarcel} 157240116Smarcel 158240116Smarcel/* --------------------------------------------------------------------- 159240116Smarcel * Tests for the "libc" error. 160240116Smarcel * --------------------------------------------------------------------- */ 161240116Smarcel 162240116SmarcelATF_TC(libc_new); 163240116SmarcelATF_TC_HEAD(libc_new, tc) 164240116Smarcel{ 165240116Smarcel atf_tc_set_md_var(tc, "descr", "Checks the construction of libc errors"); 166240116Smarcel} 167240116SmarcelATF_TC_BODY(libc_new, tc) 168240116Smarcel{ 169240116Smarcel atf_error_t err; 170240116Smarcel 171240116Smarcel err = atf_libc_error(ENOMEM, "Test message 1"); 172240116Smarcel ATF_REQUIRE(atf_error_is(err, "libc")); 173240116Smarcel ATF_REQUIRE_EQ(atf_libc_error_code(err), ENOMEM); 174240116Smarcel ATF_REQUIRE(strcmp(atf_libc_error_msg(err), "Test message 1") == 0); 175240116Smarcel atf_error_free(err); 176240116Smarcel 177240116Smarcel err = atf_libc_error(EPERM, "%s message %d", "Test", 2); 178240116Smarcel ATF_REQUIRE(atf_error_is(err, "libc")); 179240116Smarcel ATF_REQUIRE_EQ(atf_libc_error_code(err), EPERM); 180240116Smarcel ATF_REQUIRE(strcmp(atf_libc_error_msg(err), "Test message 2") == 0); 181240116Smarcel atf_error_free(err); 182240116Smarcel} 183240116Smarcel 184240116SmarcelATF_TC(libc_format); 185240116SmarcelATF_TC_HEAD(libc_format, tc) 186240116Smarcel{ 187240116Smarcel atf_tc_set_md_var(tc, "descr", "Checks the formatting of libc errors"); 188240116Smarcel} 189240116SmarcelATF_TC_BODY(libc_format, tc) 190240116Smarcel{ 191240116Smarcel atf_error_t err; 192240116Smarcel char buf[1024]; 193240116Smarcel 194240116Smarcel err = atf_libc_error(ENOMEM, "Test message 1"); 195240116Smarcel atf_error_format(err, buf, sizeof(buf)); 196240116Smarcel ATF_REQUIRE(strstr(buf, strerror(ENOMEM)) != NULL); 197240116Smarcel ATF_REQUIRE(strstr(buf, "Test message 1") != NULL); 198240116Smarcel atf_error_free(err); 199240116Smarcel 200240116Smarcel err = atf_libc_error(EPERM, "Test message 2"); 201240116Smarcel atf_error_format(err, buf, sizeof(buf)); 202240116Smarcel ATF_REQUIRE(strstr(buf, strerror(EPERM)) != NULL); 203240116Smarcel ATF_REQUIRE(strstr(buf, "Test message 2") != NULL); 204240116Smarcel atf_error_free(err); 205240116Smarcel 206240116Smarcel err = atf_libc_error(EPERM, "%s message %d", "Test", 3); 207240116Smarcel atf_error_format(err, buf, sizeof(buf)); 208240116Smarcel ATF_REQUIRE(strstr(buf, strerror(EPERM)) != NULL); 209240116Smarcel ATF_REQUIRE(strstr(buf, "Test message 3") != NULL); 210240116Smarcel atf_error_free(err); 211240116Smarcel} 212240116Smarcel 213240116Smarcel/* --------------------------------------------------------------------- 214240116Smarcel * Tests for the "no_memory" error. 215240116Smarcel * --------------------------------------------------------------------- */ 216240116Smarcel 217240116SmarcelATF_TC(no_memory_new); 218240116SmarcelATF_TC_HEAD(no_memory_new, tc) 219240116Smarcel{ 220240116Smarcel atf_tc_set_md_var(tc, "descr", "Checks the construction of no_memory " 221240116Smarcel "errors"); 222240116Smarcel} 223240116SmarcelATF_TC_BODY(no_memory_new, tc) 224240116Smarcel{ 225240116Smarcel atf_error_t err; 226240116Smarcel 227240116Smarcel err = atf_no_memory_error(); 228240116Smarcel ATF_REQUIRE(atf_error_is(err, "no_memory")); 229240116Smarcel ATF_REQUIRE(atf_error_data(err) == NULL); 230240116Smarcel atf_error_free(err); 231240116Smarcel} 232240116Smarcel 233240116SmarcelATF_TC(no_memory_format); 234240116SmarcelATF_TC_HEAD(no_memory_format, tc) 235240116Smarcel{ 236240116Smarcel atf_tc_set_md_var(tc, "descr", "Checks the formatting of no_memory " 237240116Smarcel "errors"); 238240116Smarcel} 239240116SmarcelATF_TC_BODY(no_memory_format, tc) 240240116Smarcel{ 241240116Smarcel atf_error_t err; 242240116Smarcel char buf[1024]; 243240116Smarcel 244240116Smarcel err = atf_no_memory_error(); 245240116Smarcel atf_error_format(err, buf, sizeof(buf)); 246240116Smarcel ATF_REQUIRE(strcmp(buf, "Not enough memory") == 0); 247240116Smarcel atf_error_free(err); 248240116Smarcel} 249240116Smarcel 250240116SmarcelATF_TC(no_memory_twice); 251240116SmarcelATF_TC_HEAD(no_memory_twice, tc) 252240116Smarcel{ 253240116Smarcel atf_tc_set_md_var(tc, "descr", "Checks the construction of no_memory " 254240116Smarcel "errors multiple times, as this error is initialized " 255240116Smarcel "statically"); 256240116Smarcel} 257240116SmarcelATF_TC_BODY(no_memory_twice, tc) 258240116Smarcel{ 259240116Smarcel { 260240116Smarcel atf_error_t err = atf_no_memory_error(); 261240116Smarcel ATF_REQUIRE(atf_error_is(err, "no_memory")); 262240116Smarcel ATF_REQUIRE(atf_error_data(err) == NULL); 263240116Smarcel atf_error_free(err); 264240116Smarcel } 265240116Smarcel 266240116Smarcel { 267240116Smarcel atf_error_t err = atf_no_memory_error(); 268240116Smarcel ATF_REQUIRE(atf_error_is(err, "no_memory")); 269240116Smarcel ATF_REQUIRE(atf_error_data(err) == NULL); 270240116Smarcel atf_error_free(err); 271240116Smarcel } 272240116Smarcel} 273240116Smarcel 274240116Smarcel/* --------------------------------------------------------------------- 275240116Smarcel * Main. 276240116Smarcel * --------------------------------------------------------------------- */ 277240116Smarcel 278240116SmarcelATF_TP_ADD_TCS(tp) 279240116Smarcel{ 280240116Smarcel /* Add the tests for the "atf_error" type. */ 281240116Smarcel ATF_TP_ADD_TC(tp, error_new); 282240116Smarcel ATF_TP_ADD_TC(tp, error_new_wo_memory); 283240116Smarcel ATF_TP_ADD_TC(tp, no_error); 284240116Smarcel ATF_TP_ADD_TC(tp, is_error); 285240116Smarcel ATF_TP_ADD_TC(tp, format); 286240116Smarcel 287240116Smarcel /* Add the tests for the "libc" error. */ 288240116Smarcel ATF_TP_ADD_TC(tp, libc_new); 289240116Smarcel ATF_TP_ADD_TC(tp, libc_format); 290240116Smarcel 291240116Smarcel /* Add the tests for the "no_memory" error. */ 292240116Smarcel ATF_TP_ADD_TC(tp, no_memory_new); 293240116Smarcel ATF_TP_ADD_TC(tp, no_memory_format); 294240116Smarcel ATF_TP_ADD_TC(tp, no_memory_twice); 295240116Smarcel 296240116Smarcel return atf_no_error(); 297240116Smarcel} 298