bzrtpTest.c 3.96 KB
Newer Older
johan's avatar
johan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/**
 @file bzrtpTests.c

 @author Johan Pascal

 @copyright Copyright (C) 2014 Belledonne Communications, Grenoble, France
 
 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, write to the Free Software
20
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
johan's avatar
johan committed
21 22 23
 */

#include <stdio.h>
Johan Pascal's avatar
Johan Pascal committed
24
#include "bzrtpTest.h"
25
#include "typedef.h"
johan's avatar
johan committed
26
#include "testUtils.h"
Johan Pascal's avatar
Johan Pascal committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

static FILE * log_file = NULL;
static const char *log_domain = "bzrtp-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.) */
	vfprintf(lev == BCTBX_LOG_ERROR ? stderr : stdout, fmt, args);
	fprintf(lev == BCTBX_LOG_ERROR ? stderr : stdout, "\n");
#else
	va_list cap;
	va_copy(cap,args);
	vfprintf(lev == BCTBX_LOG_ERROR ? stderr : stdout, fmt, cap);
	fprintf(lev == BCTBX_LOG_ERROR ? stderr : stdout, "\n");
	va_end(cap);
#endif
	if (log_file){
		bctbx_logv(log_domain, (BctbxLogLevel)lev, fmt, args);
	}
}

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
int silent_arg_func(const char *arg) {
	bctbx_set_log_level(log_domain, BCTBX_LOG_FATAL);
	bctbx_set_log_level(BCTBX_LOG_DOMAIN, BCTBX_LOG_FATAL);
	verbose = 0;
	return 0;
}

int verbose_arg_func(const char *arg) {
	bctbx_set_log_level(log_domain, BCTBX_LOG_DEBUG);
	bctbx_set_log_level(BCTBX_LOG_DOMAIN,BCTBX_LOG_DEBUG);
	verbose = 1;
	return 0;
}

int logfile_arg_func(const char *arg) {
	if (bzrtp_tester_set_log_file(argv[i]) < 0) return -2;
	return 0;
}


Johan Pascal's avatar
Johan Pascal committed
69
void bzrtp_tester_init(void(*ftester_printf)(int level, const char *fmt, va_list args)) {
70 71 72
	bc_tester_set_silent_func(silent_arg_func);
	bc_tester_set_verbose_func(verbose_arg_func);
	bc_tester_set_logfile_func(logfile_arg_func);
Johan Pascal's avatar
Johan Pascal committed
73 74
	if (ftester_printf == NULL) ftester_printf = log_handler;
	bc_tester_init(ftester_printf, BCTBX_LOG_MESSAGE, BCTBX_LOG_ERROR, NULL);
johan's avatar
johan committed
75

76 77
	bc_tester_add_suite(&crypto_utils_test_suite);
	bc_tester_add_suite(&packet_parser_test_suite);
johan's avatar
johan committed
78
	bc_tester_add_suite(&key_exchange_test_suite);
johan's avatar
johan committed
79
	bc_tester_add_suite(&zidcache_test_suite);
80
}
johan's avatar
johan committed
81

82 83 84 85
void bzrtp_tester_uninit(void) {
	bc_tester_uninit();
}

Johan Pascal's avatar
Johan Pascal committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
int bzrtp_tester_set_log_file(const char *filename) {
	bctbx_log_handler_t* filehandler;
	char* dir;
	char* base;
	if (log_file) {
		fclose(log_file);
	}
	log_file = fopen(filename, "w");
	if (!log_file) {
		bctbx_error("Cannot open file [%s] for writing logs because [%s]", filename, strerror(errno));
		return -1;
	}
	dir = bctbx_dirname(filename);
	base = bctbx_basename(filename);
	bctbx_message("Redirecting traces to file [%s]", filename);
	filehandler = bctbx_create_file_log_handler(0, dir, base, log_file);
	bctbx_add_log_handler(filehandler);
	if (dir) bctbx_free(dir);
	if (base) bctbx_free(base);
	return 0;
}

#if !defined(__ANDROID__) && !(defined(BCTBX_WINDOWS_PHONE) || defined(BCTBX_WINDOWS_UNIVERSAL))

110
static const char* lime_helper = "";
Johan Pascal's avatar
Johan Pascal committed
111

112 113 114 115
int main(int argc, char *argv[]) {
	int i;
	int ret;

Johan Pascal's avatar
Johan Pascal committed
116
	bzrtp_tester_init(NULL);
117

Johan Pascal's avatar
Johan Pascal committed
118 119 120 121 122 123
	if (strstr(argv[0], ".libs")) {
		int prefix_length = (int)(strstr(argv[0], ".libs") - argv[0]) + 1;
		char prefix[200];
		sprintf(prefix, "%s%.*s", argv[0][0] == '/' ? "" : "./", prefix_length, argv[0]);
		bc_tester_set_resource_dir_prefix(prefix);
		bc_tester_set_writable_dir_prefix(prefix);
124
	}
125

Johan Pascal's avatar
Johan Pascal committed
126
	for(i = 1; i < argc; ++i) {
127 128 129 130 131 132
		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], lime_helper);
Johan Pascal's avatar
Johan Pascal committed
133
		}
134
		return ret;
Johan Pascal's avatar
Johan Pascal committed
135
	}
136 137
	ret = bc_tester_start(argv[0]);
	bzrtp_tester_uninit();
Johan Pascal's avatar
Johan Pascal committed
138
	bctbx_uninit_logger();
139
	return ret;
johan's avatar
johan committed
140 141
}

Johan Pascal's avatar
Johan Pascal committed
142
#endif