1 /* 2 * Automated Testing Framework (atf) 3 * 4 * Copyright (c) 2008, 2009, 2010 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 #include <errno.h> 31 #include <stdint.h> 32 #include <stdio.h> 33 #include <string.h> 34 35 #include <atf-c.h> 36 37 #include "atf-c/error.h" 38 39 #include "detail/test_helpers.h" 40 41 /* --------------------------------------------------------------------- 42 * Auxiliary functions. 43 * --------------------------------------------------------------------- */ 44 45 static 46 void 47 test_format(const atf_error_t err, char *buf, size_t buflen) 48 { 49 snprintf(buf, buflen, "Test formatting function"); 50 } 51 52 /* --------------------------------------------------------------------- 53 * Tests for the "atf_error" type. 54 * --------------------------------------------------------------------- */ 55 56 ATF_TC(error_new); 57 ATF_TC_HEAD(error_new, tc) 58 { 59 atf_tc_set_md_var(tc, "descr", "Checks the construction of an error " 60 "object"); 61 } 62 ATF_TC_BODY(error_new, tc) 63 { 64 atf_error_t err; 65 int data; 66 67 err = atf_error_new("test_error", NULL, 0, NULL); 68 ATF_REQUIRE(atf_error_is(err, "test_error")); 69 ATF_REQUIRE(!atf_error_is(err, "unknown_error")); 70 ATF_REQUIRE(atf_error_data(err) == NULL); 71 atf_error_free(err); 72 73 data = 5; 74 err = atf_error_new("test_data_error", &data, sizeof(data), NULL); 75 ATF_REQUIRE(atf_error_is(err, "test_data_error")); 76 ATF_REQUIRE(!atf_error_is(err, "unknown_error")); 77 ATF_REQUIRE(atf_error_data(err) != NULL); 78 ATF_REQUIRE_EQ(*((const int *)atf_error_data(err)), 5); 79 atf_error_free(err); 80 } 81 82 ATF_TC(error_new_wo_memory); 83 ATF_TC_HEAD(error_new_wo_memory, tc) 84 { 85 atf_tc_set_md_var(tc, "descr", "Checks that an unavailable memory error " 86 "raised when constructing an error object " 87 "is properly converted to the no_memory " 88 "static error type"); 89 } 90 ATF_TC_BODY(error_new_wo_memory, tc) 91 { 92 atf_error_t err; 93 void *invalid; 94 95 invalid = (void *)1; 96 97 err = atf_error_new("test_error", invalid, SIZE_MAX, NULL); 98 ATF_REQUIRE(atf_error_is(err, "no_memory")); 99 ATF_REQUIRE(atf_error_data(err) == NULL); 100 atf_error_free(err); 101 } 102 103 ATF_TC(no_error); 104 ATF_TC_HEAD(no_error, tc) 105 { 106 atf_tc_set_md_var(tc, "descr", "Checks that constructing a non-error " 107 "object works"); 108 } 109 ATF_TC_BODY(no_error, tc) 110 { 111 atf_error_t err; 112 113 err = atf_no_error(); 114 ATF_REQUIRE(!atf_is_error(err)); 115 } 116 117 ATF_TC(is_error); 118 ATF_TC_HEAD(is_error, tc) 119 { 120 atf_tc_set_md_var(tc, "descr", "Checks the is_error method to determine " 121 "if an error object holds success or an error"); 122 } 123 ATF_TC_BODY(is_error, tc) 124 { 125 atf_error_t err; 126 127 err = atf_no_error(); 128 ATF_REQUIRE(!atf_is_error(err)); 129 130 err = atf_error_new("test_error", NULL, 0, NULL); 131 ATF_REQUIRE(atf_is_error(err)); 132 atf_error_free(err); 133 } 134 135 ATF_TC(format); 136 ATF_TC_HEAD(format, tc) 137 { 138 atf_tc_set_md_var(tc, "descr", "Checks the default formatting function " 139 "and the ability to change it"); 140 } 141 ATF_TC_BODY(format, tc) 142 { 143 atf_error_t err; 144 char buf[1024]; 145 146 printf("Testing default formatting function\n"); 147 err = atf_error_new("test_error", NULL, 0, NULL); 148 atf_error_format(err, buf, sizeof(buf)); 149 printf("Error string is: %s\n", buf); 150 ATF_REQUIRE(strcmp(buf, "Error 'test_error'") == 0); 151 atf_error_free(err); 152 153 printf("Testing custom formatting function\n"); 154 err = atf_error_new("test_error", NULL, 0, test_format); 155 atf_error_format(err, buf, sizeof(buf)); 156 printf("Error string is: %s\n", buf); 157 ATF_REQUIRE(strcmp(buf, "Test formatting function") == 0); 158 atf_error_free(err); 159 } 160 161 /* --------------------------------------------------------------------- 162 * Tests for the "libc" error. 163 * --------------------------------------------------------------------- */ 164 165 ATF_TC(libc_new); 166 ATF_TC_HEAD(libc_new, tc) 167 { 168 atf_tc_set_md_var(tc, "descr", "Checks the construction of libc errors"); 169 } 170 ATF_TC_BODY(libc_new, tc) 171 { 172 atf_error_t err; 173 174 err = atf_libc_error(ENOMEM, "Test message 1"); 175 ATF_REQUIRE(atf_error_is(err, "libc")); 176 ATF_REQUIRE_EQ(atf_libc_error_code(err), ENOMEM); 177 ATF_REQUIRE(strcmp(atf_libc_error_msg(err), "Test message 1") == 0); 178 atf_error_free(err); 179 180 err = atf_libc_error(EPERM, "%s message %d", "Test", 2); 181 ATF_REQUIRE(atf_error_is(err, "libc")); 182 ATF_REQUIRE_EQ(atf_libc_error_code(err), EPERM); 183 ATF_REQUIRE(strcmp(atf_libc_error_msg(err), "Test message 2") == 0); 184 atf_error_free(err); 185 } 186 187 ATF_TC(libc_format); 188 ATF_TC_HEAD(libc_format, tc) 189 { 190 atf_tc_set_md_var(tc, "descr", "Checks the formatting of libc errors"); 191 } 192 ATF_TC_BODY(libc_format, tc) 193 { 194 atf_error_t err; 195 char buf[1024]; 196 197 err = atf_libc_error(ENOMEM, "Test message 1"); 198 atf_error_format(err, buf, sizeof(buf)); 199 ATF_REQUIRE(strstr(buf, strerror(ENOMEM)) != NULL); 200 ATF_REQUIRE(strstr(buf, "Test message 1") != NULL); 201 atf_error_free(err); 202 203 err = atf_libc_error(EPERM, "Test message 2"); 204 atf_error_format(err, buf, sizeof(buf)); 205 ATF_REQUIRE(strstr(buf, strerror(EPERM)) != NULL); 206 ATF_REQUIRE(strstr(buf, "Test message 2") != NULL); 207 atf_error_free(err); 208 209 err = atf_libc_error(EPERM, "%s message %d", "Test", 3); 210 atf_error_format(err, buf, sizeof(buf)); 211 ATF_REQUIRE(strstr(buf, strerror(EPERM)) != NULL); 212 ATF_REQUIRE(strstr(buf, "Test message 3") != NULL); 213 atf_error_free(err); 214 } 215 216 /* --------------------------------------------------------------------- 217 * Tests for the "no_memory" error. 218 * --------------------------------------------------------------------- */ 219 220 ATF_TC(no_memory_new); 221 ATF_TC_HEAD(no_memory_new, tc) 222 { 223 atf_tc_set_md_var(tc, "descr", "Checks the construction of no_memory " 224 "errors"); 225 } 226 ATF_TC_BODY(no_memory_new, tc) 227 { 228 atf_error_t err; 229 230 err = atf_no_memory_error(); 231 ATF_REQUIRE(atf_error_is(err, "no_memory")); 232 ATF_REQUIRE(atf_error_data(err) == NULL); 233 atf_error_free(err); 234 } 235 236 ATF_TC(no_memory_format); 237 ATF_TC_HEAD(no_memory_format, tc) 238 { 239 atf_tc_set_md_var(tc, "descr", "Checks the formatting of no_memory " 240 "errors"); 241 } 242 ATF_TC_BODY(no_memory_format, tc) 243 { 244 atf_error_t err; 245 char buf[1024]; 246 247 err = atf_no_memory_error(); 248 atf_error_format(err, buf, sizeof(buf)); 249 ATF_REQUIRE(strcmp(buf, "Not enough memory") == 0); 250 atf_error_free(err); 251 } 252 253 ATF_TC(no_memory_twice); 254 ATF_TC_HEAD(no_memory_twice, tc) 255 { 256 atf_tc_set_md_var(tc, "descr", "Checks the construction of no_memory " 257 "errors multiple times, as this error is initialized " 258 "statically"); 259 } 260 ATF_TC_BODY(no_memory_twice, tc) 261 { 262 { 263 atf_error_t err = atf_no_memory_error(); 264 ATF_REQUIRE(atf_error_is(err, "no_memory")); 265 ATF_REQUIRE(atf_error_data(err) == NULL); 266 atf_error_free(err); 267 } 268 269 { 270 atf_error_t err = atf_no_memory_error(); 271 ATF_REQUIRE(atf_error_is(err, "no_memory")); 272 ATF_REQUIRE(atf_error_data(err) == NULL); 273 atf_error_free(err); 274 } 275 } 276 277 /* --------------------------------------------------------------------- 278 * Tests cases for the header file. 279 * --------------------------------------------------------------------- */ 280 281 HEADER_TC(include, "atf-c/error.h"); 282 HEADER_TC(include_fwd, "atf-c/error_fwd.h"); 283 284 /* --------------------------------------------------------------------- 285 * Main. 286 * --------------------------------------------------------------------- */ 287 288 ATF_TP_ADD_TCS(tp) 289 { 290 /* Add the tests for the "atf_error" type. */ 291 ATF_TP_ADD_TC(tp, error_new); 292 ATF_TP_ADD_TC(tp, error_new_wo_memory); 293 ATF_TP_ADD_TC(tp, no_error); 294 ATF_TP_ADD_TC(tp, is_error); 295 ATF_TP_ADD_TC(tp, format); 296 297 /* Add the tests for the "libc" error. */ 298 ATF_TP_ADD_TC(tp, libc_new); 299 ATF_TP_ADD_TC(tp, libc_format); 300 301 /* Add the tests for the "no_memory" error. */ 302 ATF_TP_ADD_TC(tp, no_memory_new); 303 ATF_TP_ADD_TC(tp, no_memory_format); 304 ATF_TP_ADD_TC(tp, no_memory_twice); 305 306 /* Add the test cases for the header file. */ 307 ATF_TP_ADD_TC(tp, include); 308 ATF_TP_ADD_TC(tp, include_fwd); 309 310 return atf_no_error(); 311 } 312