kmod/testsuite/testsuite.h

150 lines
4.1 KiB
C
Raw Normal View History

2012-01-27 02:01:41 +07:00
/*
* Copyright (C) 2012 ProFUSION embedded systems
*
2012-07-10 19:42:24 +07:00
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
2012-01-27 02:01:41 +07:00
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2012-07-10 19:42:24 +07:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
2012-01-27 02:01:41 +07:00
*
2012-07-10 19:42:24 +07:00
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2012-01-27 02:01:41 +07:00
*/
2012-01-25 05:59:54 +07:00
#ifndef _LIBKMOD_TESTSUITE_
#define _LIBKMOD_TESTSUITE_
#include <stdbool.h>
#include <stdarg.h>
struct test;
typedef int (*testfunc)(const struct test *t);
enum test_config {
2012-02-07 01:50:54 +07:00
/*
* Where's the roots dir for this test. It will LD_PRELOAD path.so in
* order to trap calls to functions using paths.
*/
2012-01-25 05:59:54 +07:00
TC_ROOTFS = 0,
2012-02-07 01:50:54 +07:00
/*
* What's the desired string to be returned by `uname -r`. It will
* trap calls to uname(3P) by LD_PRELOAD'ing uname.so and then filling
* in the information in u.release.
*/
2012-01-25 05:59:54 +07:00
TC_UNAME_R,
2012-02-07 01:50:54 +07:00
/*
* Fake calls to init_module(2), returning return-code and setting
* errno to err-code. Set this variable with the following format:
*
* modname:return-code:err-code
*
* When this variable is used, all calls to init_module() are trapped
* and by default the return code is 0. In other words, they fake
* "success" for all modules, except the ones in the list above, for
* which the return codes are used.
*/
TC_INIT_MODULE_RETCODES,
2012-02-07 01:50:54 +07:00
/*
* Fake calls to delete_module(2), returning return-code and setting
* errno to err-code. Set this variable with the following format:
*
* modname:return-code:err-code
*
* When this variable is used, all calls to init_module() are trapped
* and by default the return code is 0. In other words, they fake
* "success" for all modules, except the ones in the list above, for
* which the return codes are used.
*/
TC_DELETE_MODULE_RETCODES,
2012-02-07 01:50:54 +07:00
2012-01-25 05:59:54 +07:00
_TC_LAST,
};
#define S_TC_ROOTFS "TESTSUITE_ROOTFS"
2012-01-25 07:04:46 +07:00
#define S_TC_UNAME_R "TESTSUITE_UNAME_R"
#define S_TC_INIT_MODULE_RETCODES "TESTSUITE_INIT_MODULE_RETCODES"
#define S_TC_DELETE_MODULE_RETCODES "TESTSUITE_DELETE_MODULE_RETCODES"
2012-01-25 07:04:46 +07:00
struct keyval {
const char *key;
const char *val;
};
2012-01-25 07:04:46 +07:00
2012-01-25 05:59:54 +07:00
struct test {
const char *name;
const char *description;
struct {
const char *stdout;
const char *stderr;
} output;
2012-01-25 05:59:54 +07:00
testfunc func;
const char *config[_TC_LAST];
const char *path;
const struct keyval *env_vars;
bool need_spawn;
bool expected_fail;
2012-01-25 05:59:54 +07:00
};
const struct test *test_find(const struct test *tests[], const char *name);
int test_init(int argc, char *const argv[], const struct test *tests[]);
int test_spawn_prog(const char *prog, const char *const args[]);
2012-01-25 05:59:54 +07:00
int test_run(const struct test *t);
#define TS_EXPORT __attribute__ ((visibility("default")))
#define _LOG(prefix, fmt, ...) printf("TESTSUITE: " prefix fmt, ## __VA_ARGS__)
#define LOG(fmt, ...) _LOG("", fmt, ## __VA_ARGS__)
#define WARN(fmt, ...) _LOG("WARN: ", fmt, ## __VA_ARGS__)
#define ERR(fmt, ...) _LOG("ERR: ", fmt, ## __VA_ARGS__)
/* Test definitions */
2012-02-07 19:46:46 +07:00
#define DEFINE_TEST(_name, ...) \
const struct test s##_name = { \
2012-01-25 05:59:54 +07:00
.name = #_name, \
.func = _name, \
2012-02-07 19:46:46 +07:00
## __VA_ARGS__ \
2012-01-25 05:59:54 +07:00
}
2012-02-07 19:09:20 +07:00
#define TESTSUITE_MAIN(_tests) \
int main(int argc, char *argv[]) \
{ \
const struct test *t; \
int arg; \
size_t i; \
\
arg = test_init(argc, argv, tests); \
if (arg == 0) \
return 0; \
\
if (arg < argc) { \
t = test_find(tests, argv[arg]); \
if (t == NULL) { \
fprintf(stderr, "could not find test %s\n", argv[arg]);\
exit(EXIT_FAILURE); \
} \
\
return test_run(t); \
} \
\
for (i = 0; tests[i] != NULL; i++) { \
if (test_run(tests[i]) != 0) \
exit(EXIT_FAILURE); \
} \
\
exit(EXIT_SUCCESS); \
} \
2012-01-25 05:59:54 +07:00
#endif
#define __noreturn __attribute__((noreturn))