h_macros.h revision 313535
1284893Sbrd/* $NetBSD: h_macros.h,v 1.13 2016/08/20 15:49:08 christos Exp $ */ 2284893Sbrd 3284893Sbrd/*- 4284893Sbrd * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc. 5284893Sbrd * All rights reserved. 6284893Sbrd * 7284893Sbrd * Redistribution and use in source and binary forms, with or without 8285049Sgjb * modification, are permitted provided that the following conditions 9285814Sgjb * are met: 10284893Sbrd * 1. Redistributions of source code must retain the above copyright 11284893Sbrd * notice, this list of conditions and the following disclaimer. 12284893Sbrd * 2. Redistributions in binary form must reproduce the above copyright 13285049Sgjb * notice, this list of conditions and the following disclaimer in the 14285049Sgjb * documentation and/or other materials provided with the distribution. 15285049Sgjb * 16284893Sbrd * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17284893Sbrd * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18284893Sbrd * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19284893Sbrd * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20287449Sgjb * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21284893Sbrd * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22284893Sbrd * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23285814Sgjb * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24287449Sgjb * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25285814Sgjb * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26285814Sgjb * POSSIBILITY OF SUCH DAMAGE. 27289077Sbdrewery */ 28289077Sbdrewery 29285814Sgjb#ifndef SRC_TESTS_H_MACROS_H_ 30285814Sgjb#define SRC_TESTS_H_MACROS_H_ 31285814Sgjb 32285814Sgjb#include <sys/types.h> 33285814Sgjb#include <errno.h> 34285049Sgjb#include <stdarg.h> 35284893Sbrd#include <stdio.h> 36284893Sbrd#include <stdlib.h> 37285049Sgjb#include <string.h> 38284893Sbrd 39284893Sbrd#include <atf-c.h> 40284893Sbrd 41284893Sbrd#define REQUIRE_LIBC(x, v) \ 42284893Sbrd ATF_REQUIRE_MSG((x) != (v), "%s: %s", #x, strerror(errno)) 43284893Sbrd 44284893Sbrd#define CHECK_LIBC(x, v) \ 45284893Sbrd ATF_CHECK_MSG((x) != (v), "%s: %s", #x, strerror(errno)) 46284893Sbrd 47284893Sbrd#define RL(x) REQUIRE_LIBC(x, -1) 48285049Sgjb#define RLF(x, fmt, arg) \ 49284893Sbrd ATF_CHECK_MSG((x) != -1, "%s [" fmt "]: %s", #x, arg, strerror(errno)) 50284893Sbrd#define RZ(x) \ 51284893Sbrddo { \ 52284893Sbrd int RZ_rv = x; \ 53284893Sbrd ATF_REQUIRE_MSG(RZ_rv == 0, "%s: %s", #x, strerror(RZ_rv)); \ 54285814Sgjb} while (/*CONSTCOND*/0) 55289077Sbdrewery 56285814Sgjb__dead static __inline __printflike(1, 2) void 57285814Sgjbatf_tc_fail_errno(const char *fmt, ...) 58285814Sgjb{ 59284893Sbrd va_list ap; 60285814Sgjb char buf[1024]; 61285814Sgjb int sverrno = errno; 62285814Sgjb 63285814Sgjb va_start(ap, fmt); 64285814Sgjb vsnprintf(buf, sizeof(buf), fmt, ap); 65285814Sgjb va_end(ap); 66289077Sbdrewery 67285814Sgjb strlcat(buf, ": ", sizeof(buf)); 68285814Sgjb strlcat(buf, strerror(sverrno), sizeof(buf)); 69289077Sbdrewery 70285814Sgjb atf_tc_fail("%s", buf); 71285814Sgjb} 72285814Sgjb 73285814Sgjbstatic __inline void 74285814Sgjbtests_makegarbage(void *space, size_t len) 75285814Sgjb{ 76289077Sbdrewery uint16_t *sb = space; 77285814Sgjb uint16_t randval; 78289077Sbdrewery 79285814Sgjb while (len >= sizeof(randval)) { 80285814Sgjb *sb++ = (uint16_t)random(); 81284893Sbrd len -= sizeof(*sb); 82284893Sbrd } 83284893Sbrd randval = (uint16_t)random(); 84285814Sgjb memcpy(sb, &randval, len); 85285814Sgjb} 86285814Sgjb 87285814Sgjb#endif 88285814Sgjb