12b15cb3dSCy Schubert /* tinytest.h -- Copyright 2009-2012 Nick Mathewson 22b15cb3dSCy Schubert * 32b15cb3dSCy Schubert * Redistribution and use in source and binary forms, with or without 42b15cb3dSCy Schubert * modification, are permitted provided that the following conditions 52b15cb3dSCy Schubert * are met: 62b15cb3dSCy Schubert * 1. Redistributions of source code must retain the above copyright 72b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer. 82b15cb3dSCy Schubert * 2. Redistributions in binary form must reproduce the above copyright 92b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer in the 102b15cb3dSCy Schubert * documentation and/or other materials provided with the distribution. 112b15cb3dSCy Schubert * 3. The name of the author may not be used to endorse or promote products 122b15cb3dSCy Schubert * derived from this software without specific prior written permission. 132b15cb3dSCy Schubert * 142b15cb3dSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 152b15cb3dSCy Schubert * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 162b15cb3dSCy Schubert * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 172b15cb3dSCy Schubert * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 182b15cb3dSCy Schubert * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 192b15cb3dSCy Schubert * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 202b15cb3dSCy Schubert * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 212b15cb3dSCy Schubert * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 222b15cb3dSCy Schubert * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 232b15cb3dSCy Schubert * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 242b15cb3dSCy Schubert */ 252b15cb3dSCy Schubert 262b15cb3dSCy Schubert #ifndef TINYTEST_H_INCLUDED_ 272b15cb3dSCy Schubert #define TINYTEST_H_INCLUDED_ 282b15cb3dSCy Schubert 292b15cb3dSCy Schubert /** Flag for a test that needs to run in a subprocess. */ 302b15cb3dSCy Schubert #define TT_FORK (1<<0) 312b15cb3dSCy Schubert /** Runtime flag for a test we've decided to skip. */ 322b15cb3dSCy Schubert #define TT_SKIP (1<<1) 332b15cb3dSCy Schubert /** Internal runtime flag for a test we've decided to run. */ 342b15cb3dSCy Schubert #define TT_ENABLED_ (1<<2) 352b15cb3dSCy Schubert /** Flag for a test that's off by default. */ 362b15cb3dSCy Schubert #define TT_OFF_BY_DEFAULT (1<<3) 37*a466cc55SCy Schubert /** Flag for a test that should be runned again in case of failure (but not 38*a466cc55SCy Schubert * more then 3 times). */ 39*a466cc55SCy Schubert #define TT_RETRIABLE (1<<4) 402b15cb3dSCy Schubert /** If you add your own flags, make them start at this point. */ 41*a466cc55SCy Schubert #define TT_FIRST_USER_FLAG (1<<5) 422b15cb3dSCy Schubert 432b15cb3dSCy Schubert typedef void (*testcase_fn)(void *); 442b15cb3dSCy Schubert 452b15cb3dSCy Schubert struct testcase_t; 462b15cb3dSCy Schubert 472b15cb3dSCy Schubert /** Functions to initialize/teardown a structure for a testcase. */ 482b15cb3dSCy Schubert struct testcase_setup_t { 492b15cb3dSCy Schubert /** Return a new structure for use by a given testcase. */ 502b15cb3dSCy Schubert void *(*setup_fn)(const struct testcase_t *); 512b15cb3dSCy Schubert /** Clean/free a structure from setup_fn. Return 1 if ok, 0 on err. */ 522b15cb3dSCy Schubert int (*cleanup_fn)(const struct testcase_t *, void *); 532b15cb3dSCy Schubert }; 542b15cb3dSCy Schubert 552b15cb3dSCy Schubert /** A single test-case that you can run. */ 562b15cb3dSCy Schubert struct testcase_t { 572b15cb3dSCy Schubert const char *name; /**< An identifier for this case. */ 582b15cb3dSCy Schubert testcase_fn fn; /**< The function to run to implement this case. */ 592b15cb3dSCy Schubert unsigned long flags; /**< Bitfield of TT_* flags. */ 602b15cb3dSCy Schubert const struct testcase_setup_t *setup; /**< Optional setup/cleanup fns*/ 612b15cb3dSCy Schubert void *setup_data; /**< Extra data usable by setup function */ 622b15cb3dSCy Schubert }; 632b15cb3dSCy Schubert #define END_OF_TESTCASES { NULL, NULL, 0, NULL, NULL } 642b15cb3dSCy Schubert 652b15cb3dSCy Schubert /** A group of tests that are selectable together. */ 662b15cb3dSCy Schubert struct testgroup_t { 672b15cb3dSCy Schubert const char *prefix; /**< Prefix to prepend to testnames. */ 682b15cb3dSCy Schubert struct testcase_t *cases; /** Array, ending with END_OF_TESTCASES */ 692b15cb3dSCy Schubert }; 702b15cb3dSCy Schubert #define END_OF_GROUPS { NULL, NULL} 712b15cb3dSCy Schubert 722b15cb3dSCy Schubert struct testlist_alias_t { 732b15cb3dSCy Schubert const char *name; 742b15cb3dSCy Schubert const char **tests; 752b15cb3dSCy Schubert }; 762b15cb3dSCy Schubert #define END_OF_ALIASES { NULL, NULL } 772b15cb3dSCy Schubert 782b15cb3dSCy Schubert /** Implementation: called from a test to indicate failure, before logging. */ 792b15cb3dSCy Schubert void tinytest_set_test_failed_(void); 802b15cb3dSCy Schubert /** Implementation: called from a test to indicate that we're skipping. */ 812b15cb3dSCy Schubert void tinytest_set_test_skipped_(void); 822b15cb3dSCy Schubert /** Implementation: return 0 for quiet, 1 for normal, 2 for loud. */ 832b15cb3dSCy Schubert int tinytest_get_verbosity_(void); 842b15cb3dSCy Schubert /** Implementation: Set a flag on tests matching a name; returns number 852b15cb3dSCy Schubert * of tests that matched. */ 862b15cb3dSCy Schubert int tinytest_set_flag_(struct testgroup_t *, const char *, int set, unsigned long); 87a25439b6SCy Schubert /** Implementation: Put a chunk of memory into hex. */ 88a25439b6SCy Schubert char *tinytest_format_hex_(const void *, unsigned long); 892b15cb3dSCy Schubert 902b15cb3dSCy Schubert /** Set all tests in 'groups' matching the name 'named' to be skipped. */ 912b15cb3dSCy Schubert #define tinytest_skip(groups, named) \ 922b15cb3dSCy Schubert tinytest_set_flag_(groups, named, 1, TT_SKIP) 932b15cb3dSCy Schubert 942b15cb3dSCy Schubert /** Run a single testcase in a single group. */ 952b15cb3dSCy Schubert int testcase_run_one(const struct testgroup_t *,const struct testcase_t *); 962b15cb3dSCy Schubert 972b15cb3dSCy Schubert void tinytest_set_aliases(const struct testlist_alias_t *aliases); 982b15cb3dSCy Schubert 992b15cb3dSCy Schubert /** Run a set of testcases from an END_OF_GROUPS-terminated array of groups, 1002b15cb3dSCy Schubert as selected from the command line. */ 1012b15cb3dSCy Schubert int tinytest_main(int argc, const char **argv, struct testgroup_t *groups); 1022b15cb3dSCy Schubert 1032b15cb3dSCy Schubert #endif 104