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