bctoolbox_tester.c 3.26 KB
Newer Older
jehan's avatar
jehan committed
1
/*
2
	bctoolbox
jehan's avatar
jehan committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
	Copyright (C) 2016  Belledonne Communications SARL

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "bctoolbox/logging.h"
#include "bctoolbox_tester.h"
Benjamin REIS's avatar
Benjamin REIS committed
23

jehan's avatar
jehan committed
24 25 26 27 28
static const char *log_domain = "bctoolbox-tester";

static void log_handler(int lev, const char *fmt, va_list args) {
#ifdef _WIN32
	/* We must use stdio to avoid log formatting (for autocompletion etc.) */
29 30
	vfprintf(lev == BCTBX_LOG_ERROR ? stderr : stdout, fmt, args);
	fprintf(lev == BCTBX_LOG_ERROR ? stderr : stdout, "\n");
jehan's avatar
jehan committed
31 32 33
#else
	va_list cap;
	va_copy(cap,args);
jehan's avatar
jehan committed
34 35
	vfprintf(lev == BCTBX_LOG_ERROR ? stderr : stdout, fmt, cap);
	fprintf(lev == BCTBX_LOG_ERROR ? stderr : stdout, "\n");
jehan's avatar
jehan committed
36 37
	va_end(cap);
#endif
38 39

	bctbx_logv(log_domain,lev, fmt, args);
jehan's avatar
jehan committed
40 41 42 43
}


void bctoolbox_tester_init(void(*ftester_printf)(int level, const char *fmt, va_list args)) {
jehan's avatar
jehan committed
44
	bc_tester_init(log_handler,BCTBX_LOG_ERROR, 0,NULL);
jehan's avatar
jehan committed
45
	bc_tester_add_suite(&containers_test_suite);
46
	bc_tester_add_suite(&utils_test_suite);
jehan's avatar
jehan committed
47
#if (HAVE_MBEDTLS | HAVE_POLARSSL)
johan's avatar
johan committed
48
	bc_tester_add_suite(&crypto_test_suite);
jehan's avatar
jehan committed
49
#endif
50
	bc_tester_add_suite(&parser_test_suite);
jehan's avatar
jehan committed
51 52 53 54 55 56 57 58 59 60
}

void bctoolbox_tester_uninit(void) {
	bc_tester_uninit();
}

void bctoolbox_tester_before_each() {
}

int bctoolbox_tester_set_log_file(const char *filename) {
61 62 63
	int res = 0;
	char* dir = bctbx_dirname(filename);
	char* base = bctbx_basename(filename);
64
	bctbx_message("Redirecting traces to file [%s]", filename);
65 66 67 68 69
	bctbx_log_handler_t *filehandler = bctbx_create_file_log_handler(0, dir, base);
	if (filehandler == NULL) {
		res = -1;
		goto end;
	}
Benjamin REIS's avatar
Benjamin REIS committed
70
	bctbx_add_log_handler(filehandler);
71 72 73 74 75

end:
	bctbx_free(dir);
	bctbx_free(base);
	return res;
jehan's avatar
jehan committed
76 77 78
}


79
#if !defined(__ANDROID__) && !(defined(BCTBX_WINDOWS_PHONE) || defined(BCTBX_WINDOWS_UNIVERSAL))
jehan's avatar
jehan committed
80 81 82 83 84 85 86 87 88

static const char* bctoolbox_helper =
		"\t\t\t--verbose\n"
		"\t\t\t--silent\n"
		"\t\t\t--log-file <output log file path>\n";

int main (int argc, char *argv[]) {
	int i;
	int ret;
89

Benjamin REIS's avatar
Benjamin REIS committed
90
	bctbx_init_logger(TRUE);
jehan's avatar
jehan committed
91 92 93 94
	bctoolbox_tester_init(NULL);

	for(i=1;i<argc;++i){
		if (strcmp(argv[i],"--verbose")==0){
jehan's avatar
jehan committed
95 96
			bctbx_set_log_level(log_domain, BCTBX_LOG_DEBUG);
			bctbx_set_log_level(BCTBX_LOG_DOMAIN,BCTBX_LOG_DEBUG);
jehan's avatar
jehan committed
97
		} else if (strcmp(argv[i],"--silent")==0){
jehan's avatar
jehan committed
98
			bctbx_set_log_level(log_domain, BCTBX_LOG_FATAL);
jehan's avatar
jehan committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
		} else if (strcmp(argv[i],"--log-file")==0){
			CHECK_ARG("--log-file", ++i, argc);
			if (bctoolbox_tester_set_log_file(argv[i]) < 0) return -2;
		}else {
			int ret = bc_tester_parse_args(argc, argv, i);
			if (ret>0) {
				i += ret - 1;
				continue;
			} else if (ret<0) {
				bc_tester_helper(argv[0], bctoolbox_helper);
			}
			return ret;
		}
	}
	ret = bc_tester_start(argv[0]);
	bctoolbox_tester_uninit();
Benjamin REIS's avatar
Benjamin REIS committed
115
	bctbx_uninit_logger();
jehan's avatar
jehan committed
116 117 118 119
	return ret;
}

#endif