1/*	$NetBSD: tinytest.h,v 1.1.1.3 2021/04/07 02:43:15 christos Exp $	*/
2/* tinytest.h -- Copyright 2009-2012 Nick Mathewson
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 *    derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#ifndef TINYTEST_H_INCLUDED_
28#define TINYTEST_H_INCLUDED_
29
30/** Flag for a test that needs to run in a subprocess. */
31#define TT_FORK  (1<<0)
32/** Runtime flag for a test we've decided to skip. */
33#define TT_SKIP  (1<<1)
34/** Internal runtime flag for a test we've decided to run. */
35#define TT_ENABLED_  (1<<2)
36/** Flag for a test that's off by default. */
37#define TT_OFF_BY_DEFAULT  (1<<3)
38/** Flag for a test that should be runned again in case of failure (but not
39 * more then 3 times). */
40#define TT_RETRIABLE	(1<<4)
41/** If you add your own flags, make them start at this point. */
42#define TT_FIRST_USER_FLAG (1<<5)
43
44typedef void (*testcase_fn)(void *);
45
46struct testcase_t;
47
48/** Functions to initialize/teardown a structure for a testcase. */
49struct testcase_setup_t {
50	/** Return a new structure for use by a given testcase. */
51	void *(*setup_fn)(const struct testcase_t *);
52	/** Clean/free a structure from setup_fn. Return 1 if ok, 0 on err. */
53	int (*cleanup_fn)(const struct testcase_t *, void *);
54};
55
56/** A single test-case that you can run. */
57struct testcase_t {
58	const char *name; /**< An identifier for this case. */
59	testcase_fn fn; /**< The function to run to implement this case. */
60	unsigned long flags; /**< Bitfield of TT_* flags. */
61	const struct testcase_setup_t *setup; /**< Optional setup/cleanup fns*/
62	void *setup_data; /**< Extra data usable by setup function */
63};
64#define END_OF_TESTCASES { NULL, NULL, 0, NULL, NULL }
65
66/** A group of tests that are selectable together. */
67struct testgroup_t {
68	const char *prefix; /**< Prefix to prepend to testnames. */
69	struct testcase_t *cases; /** Array, ending with END_OF_TESTCASES */
70};
71#define END_OF_GROUPS { NULL, NULL}
72
73struct testlist_alias_t {
74	const char *name;
75	const char **tests;
76};
77#define END_OF_ALIASES { NULL, NULL }
78
79/** Implementation: called from a test to indicate failure, before logging. */
80void tinytest_set_test_failed_(void);
81/** Implementation: called from a test to indicate that we're skipping. */
82void tinytest_set_test_skipped_(void);
83/** Implementation: return 0 for quiet, 1 for normal, 2 for loud. */
84int tinytest_get_verbosity_(void);
85/** Implementation: Set a flag on tests matching a name; returns number
86 * of tests that matched. */
87int tinytest_set_flag_(struct testgroup_t *, const char *, int set, unsigned long);
88/** Implementation: Put a chunk of memory into hex. */
89char *tinytest_format_hex_(const void *, unsigned long);
90
91/** Set all tests in 'groups' matching the name 'named' to be skipped. */
92#define tinytest_skip(groups, named) \
93	tinytest_set_flag_(groups, named, 1, TT_SKIP)
94
95/** Run a single testcase in a single group. */
96int testcase_run_one(const struct testgroup_t *,const struct testcase_t *);
97
98void tinytest_set_aliases(const struct testlist_alias_t *aliases);
99
100/** Run a set of testcases from an END_OF_GROUPS-terminated array of groups,
101    as selected from the command line. */
102int tinytest_main(int argc, const char **argv, struct testgroup_t *groups);
103
104#endif
105