Path: blob/main/sys/contrib/openzfs/tests/unit/unit.c
289319 views
// SPDX-License-Identifier: CDDL-1.01/*2* This file and its contents are supplied under the terms of the3* Common Development and Distribution License ("CDDL"), version 1.0.4* You may only use this file in accordance with the terms of version5* 1.0 of the CDDL.6*7* A full copy of the text of the CDDL should have accompanied this8* source. A copy of the CDDL is also available via the Internet at9* http://www.illumos.org/license/CDDL.10*/1112/*13* Copyright (c) 2026, TrueNAS.14*/1516/* Core stubs, applicable to all test suites. */1718#include <stdio.h>19#include <stdarg.h>2021#include <sys/types.h>22#include <sys/cmn_err.h>23#include <sys/zfs_debug.h>2425#include "munit.h"26#include "unit.h"2728/*29* SET_ERROR() expands to __set_error() in debug builds. It's an30* under-the-hood tracing aid in production; a no-op is fine.31*/32void33__set_error(const char *file, const char *func, int line, int err)34{35(void) file; (void) func; (void) line; (void) err;36}3738/* Plumb logging and debug into munit for convenience. */3940/* dprintf() checks zfs_flags and calls __dprintf() in debug builds. */41int zfs_dbgmsg_enable = 1;42int zfs_flags = ZFS_DEBUG_DPRINTF;4344/* Log dprintf() to MUNIT_LOG_DEBUG. */45void46__dprintf(boolean_t dprint, const char *file, const char *func,47int line, const char *fmt, ...)48{49char buf[1024];5051va_list ap;52va_start(ap, fmt);53vsnprintf(buf, sizeof (buf), fmt, ap);54va_end(ap);5556munit_logf_ex(MUNIT_LOG_DEBUG, NULL, 0, "%s%s:%d [%s]: %s",57dprint ? "dprintf: " : "", file, line, func, buf);58}5960/* Log cmn_err() to MUNIT_LOG_INFO or WARNING, abort test on CE_PANIC. */61void62cmn_err(int ce, const char *fmt, ...)63{64if (ce == CE_IGNORE)65return;6667char buf[1024];6869va_list ap;70va_start(ap, fmt);71vsnprintf(buf, sizeof (buf), fmt, ap);72va_end(ap);7374switch (ce) {75case CE_WARN:76munit_logf_ex(MUNIT_LOG_WARNING, NULL, 0, "%s", buf);77break;78case CE_PANIC:79munit_errorf_ex(NULL, 0, "PANIC: %s", buf);80break;81default:82munit_logf_ex(MUNIT_LOG_INFO, NULL, 0, "%s", buf);83break;84}85}8687/* helpers to generate useful random data */88uint64_t89unit_rand_uint64(void)90{91uint64_t v =92(((uint64_t)munit_rand_uint32()) << 32) |93((uint64_t)munit_rand_uint32());94return (v);95}9697char *98unit_rand_str(char *buf, size_t bufsz)99{100for (int i = 0; i < bufsz-1; i++)101buf[i] = munit_rand_int_range('a', 'z');102buf[bufsz-1] = '\0';103return (buf);104}105106107