xref: /freebsd/sys/contrib/openzfs/tests/unit/unit.c (revision d9497217456002b0ddad3cd319570d0b098daa29)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * This file and its contents are supplied under the terms of the
4  * Common Development and Distribution License ("CDDL"), version 1.0.
5  * You may only use this file in accordance with the terms of version
6  * 1.0 of the CDDL.
7  *
8  * A full copy of the text of the CDDL should have accompanied this
9  * source.  A copy of the CDDL is also available via the Internet at
10  * http://www.illumos.org/license/CDDL.
11  */
12 
13 /*
14  * Copyright (c) 2026, TrueNAS.
15  */
16 
17 /* Core stubs, applicable to all test suites. */
18 
19 #include <stdio.h>
20 #include <stdarg.h>
21 
22 #include <sys/types.h>
23 #include <sys/cmn_err.h>
24 #include <sys/zfs_debug.h>
25 
26 #include "munit.h"
27 #include "unit.h"
28 
29 /*
30  * SET_ERROR() expands to __set_error() in debug builds. It's an
31  * under-the-hood tracing aid in production; a no-op is fine.
32  */
33 void
__set_error(const char * file,const char * func,int line,int err)34 __set_error(const char *file, const char *func, int line, int err)
35 {
36 	(void) file; (void) func; (void) line; (void) err;
37 }
38 
39 /* Plumb logging and debug into munit for convenience. */
40 
41 /* dprintf() checks zfs_flags and calls __dprintf() in debug builds. */
42 int zfs_dbgmsg_enable = 1;
43 int zfs_flags = ZFS_DEBUG_DPRINTF;
44 
45 /* Log dprintf() to MUNIT_LOG_DEBUG. */
46 void
__dprintf(boolean_t dprint,const char * file,const char * func,int line,const char * fmt,...)47 __dprintf(boolean_t dprint, const char *file, const char *func,
48     int line, const char *fmt, ...)
49 {
50 	char buf[1024];
51 
52 	va_list ap;
53 	va_start(ap, fmt);
54 	vsnprintf(buf, sizeof (buf), fmt, ap);
55 	va_end(ap);
56 
57 	munit_logf_ex(MUNIT_LOG_DEBUG, NULL, 0, "%s%s:%d [%s]: %s",
58 	    dprint ? "dprintf: " : "", file, line, func, buf);
59 }
60 
61 /* Log cmn_err() to MUNIT_LOG_INFO or WARNING, abort test on CE_PANIC. */
62 void
cmn_err(int ce,const char * fmt,...)63 cmn_err(int ce, const char *fmt, ...)
64 {
65 	if (ce == CE_IGNORE)
66 		return;
67 
68 	char buf[1024];
69 
70 	va_list ap;
71 	va_start(ap, fmt);
72 	vsnprintf(buf, sizeof (buf), fmt, ap);
73 	va_end(ap);
74 
75 	switch (ce) {
76 	case CE_WARN:
77 		munit_logf_ex(MUNIT_LOG_WARNING, NULL, 0, "%s", buf);
78 		break;
79 	case CE_PANIC:
80 		munit_errorf_ex(NULL, 0, "PANIC: %s", buf);
81 		break;
82 	default:
83 		munit_logf_ex(MUNIT_LOG_INFO, NULL, 0, "%s", buf);
84 		break;
85 	}
86 }
87 
88 /* helpers to generate useful random data */
89 uint64_t
unit_rand_uint64(void)90 unit_rand_uint64(void)
91 {
92 	uint64_t v =
93 	    (((uint64_t)munit_rand_uint32()) << 32) |
94 	    ((uint64_t)munit_rand_uint32());
95 	return (v);
96 }
97 
98 char *
unit_rand_str(char * buf,size_t bufsz)99 unit_rand_str(char *buf, size_t bufsz)
100 {
101 	for (int i = 0; i < bufsz-1; i++)
102 		buf[i] = munit_rand_int_range('a', 'z');
103 	buf[bufsz-1] = '\0';
104 	return (buf);
105 }
106