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