lime-tester.cpp 6.18 KB
Newer Older
johan's avatar
johan committed
1 2
/*
	lime-tester.cpp
johan's avatar
johan committed
3 4
	@author Johan Pascal
	@copyright 	Copyright (C) 2017  Belledonne Communications SARL
johan's avatar
johan committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18

	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 3 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/>.
*/
19

Johan Pascal's avatar
Johan Pascal committed
20
#include "lime_log.hpp"
johan's avatar
johan committed
21
#include "belle-sip/belle-sip.h"
johan's avatar
johan committed
22 23

#include "lime-tester.hpp"
24
#include "lime-tester-utils.hpp"
johan's avatar
johan committed
25 26

static const char *log_domain = "lime";
johan's avatar
johan committed
27
bool cleanDatabase = true;
28
#ifdef FFI_ENABLED
johan's avatar
johan committed
29
extern "C" {
johan's avatar
johan committed
30
uint8_t ffi_cleanDatabase = 1;
johan's avatar
johan committed
31
}
32
#endif
Johan Pascal's avatar
Johan Pascal committed
33
bool bench = false;
johan's avatar
johan committed
34 35 36 37 38 39 40 41 42 43 44 45 46

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
47 48

	bctbx_logv(log_domain, (BctbxLogLevel)lev, fmt, args);
johan's avatar
johan committed
49 50 51 52 53 54
}

void lime_tester_init(void(*ftester_printf)(int level, const char *fmt, va_list args)) {
	if (ftester_printf == NULL) ftester_printf = log_handler;
	bc_tester_init(ftester_printf, BCTBX_LOG_MESSAGE, BCTBX_LOG_ERROR, "data");

Johan Pascal's avatar
Johan Pascal committed
55
	bc_tester_add_suite(&lime_crypto_test_suite);
johan's avatar
johan committed
56 57
	bc_tester_add_suite(&lime_double_ratchet_test_suite);
	bc_tester_add_suite(&lime_lime_test_suite);
Johan Pascal's avatar
Johan Pascal committed
58
	bc_tester_add_suite(&lime_massive_group_test_suite);
johan's avatar
johan committed
59
	bc_tester_add_suite(&lime_helloworld_test_suite);
60
#ifdef FFI_ENABLED
johan's avatar
johan committed
61
	bc_tester_add_suite(&lime_ffi_test_suite);
62
#endif
johan's avatar
johan committed
63 64 65 66 67 68 69 70 71 72
}

void lime_tester_uninit(void) {
	bc_tester_uninit();
}

void lime_tester_before_each() {
}

int lime_tester_set_log_file(const char *filename) {
73 74 75 76 77 78
	int res = -1;
	char *dir = bctbx_dirname(filename);
	char *base = bctbx_basename(filename);
	LIME_LOGI << "Redirecting traces to file [" << std::string{filename} << "]";
	bctbx_log_handler_t *filehandler = bctbx_create_file_log_handler(0, dir, base);
	if (filehandler == NULL) goto end;
johan's avatar
johan committed
79
	bctbx_add_log_handler(filehandler);
80 81 82
	res = 0;

end:
johan's avatar
johan committed
83 84
	if (dir) bctbx_free(dir);
	if (base) bctbx_free(base);
85
	return res;
johan's avatar
johan committed
86 87 88 89 90 91 92
}

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

static const char* lime_helper =
		"\t\t\t--verbose\n"
		"\t\t\t--silent\n"
johan's avatar
johan committed
93
		"\t\t\t--x3dh-server-name <server name (without protocol prefix nor port)>, default : localhost\n\t\t\t                   a test instance shall be running on sip5.linphone.org\n"
94 95 96 97 98 99
#ifdef EC25519_ENABLED
		"\t\t\t--c255-x3dh-server-port <port to use on x3dh server for instance running on curve25519>, default : 25519\n"
#endif
#ifdef EC448_ENABLED
		"\t\t\t--c448-x3dh-server-port <port to use on x3dh server for instance running on curve448>, default : 25520\n"
#endif
100
		"\t\t\t--operation-timeout <delay in ms to complete basic operations involving server>, default : 4000\n\t\t\t                    you may want to increase this value if you are not using a local X3DH server and experience tests failures\n"
Johan Pascal's avatar
Johan Pascal committed
101
		"\t\t\t--keep-tmp-db, when set don't delete temporary db files created by tests, useful for debug\n"
102

Johan Pascal's avatar
Johan Pascal committed
103 104
		"\t\t\t--log-file <output log file path>\n"
		"\t\t\t--bench run benchmarks when set";
johan's avatar
johan committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

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

	lime_tester_init(nullptr);

	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);
	}

	for(i = 1; i < argc; ++i) {
		if (strcmp(argv[i],"--verbose")==0){
			bctbx_set_log_level(log_domain, BCTBX_LOG_DEBUG);
			bctbx_set_log_level(BCTBX_LOG_DOMAIN,BCTBX_LOG_DEBUG);
johan's avatar
johan committed
124
			belle_sip_log_level_enabled(BCTBX_LOG_DEBUG);
125
			// belle_sip_set_log_level(BELLE_SIP_LOG_MESSAGE); //uncomment to get belle-sip logs on verbose
johan's avatar
johan committed
126 127 128 129 130 131
		} else if (strcmp(argv[i],"--silent")==0){
			bctbx_set_log_level(log_domain, BCTBX_LOG_FATAL);
			bctbx_set_log_level(BCTBX_LOG_DOMAIN, BCTBX_LOG_FATAL);
		} else if (strcmp(argv[i],"--log-file")==0){
			CHECK_ARG("--log-file", ++i, argc);
			if (lime_tester_set_log_file(argv[i]) < 0) return -2;
132 133
		} else if (strcmp(argv[i],"--x3dh-server-name")==0){
			CHECK_ARG("--x3dh-server-name", ++i, argc);
Johan Pascal's avatar
Johan Pascal committed
134
			lime_tester::test_x3dh_server_url=std::string(argv[i]);
135
#ifdef FFI_ENABLED
johan's avatar
johan committed
136
			strncpy(ffi_test_x3dh_server_url, argv[i], sizeof(ffi_test_x3dh_server_url)-1);
137
#endif
138 139
		} else if (strcmp(argv[i],"--c255-x3dh-server-port")==0){
			CHECK_ARG("--c255-x3dh-server-port", ++i, argc);
Johan Pascal's avatar
Johan Pascal committed
140
			lime_tester::test_x3dh_c25519_server_port=std::string(argv[i]);
141
#ifdef FFI_ENABLED
johan's avatar
johan committed
142
			strncpy(ffi_test_x3dh_c25519_server_port, argv[i], sizeof(ffi_test_x3dh_c25519_server_port)-1);
143
#endif
144 145
		} else if (strcmp(argv[i],"--c448-x3dh-server-port")==0){
			CHECK_ARG("--c448-x3dh-server-port", ++i, argc);
Johan Pascal's avatar
Johan Pascal committed
146
			lime_tester::test_x3dh_c448_server_port=std::string(argv[i]);
147
#ifdef FFI_ENABLED
johan's avatar
johan committed
148
			strncpy(ffi_test_x3dh_c448_server_port, argv[i], sizeof(ffi_test_x3dh_c448_server_port)-1);
149
#endif
150 151
		} else if (strcmp(argv[i],"--operation-timeout")==0){
			CHECK_ARG("--operation-timeout", ++i, argc);
152
			lime_tester::wait_for_timeout=std::atoi(argv[i]);
153
#ifdef FFI_ENABLED
johan's avatar
johan committed
154
			ffi_wait_for_timeout=lime_tester::wait_for_timeout;
155
#endif
johan's avatar
johan committed
156 157
		} else if (strcmp(argv[i],"--keep-tmp-db")==0){
			cleanDatabase=false;
158
#ifdef FFI_ENABLED
johan's avatar
johan committed
159
			ffi_cleanDatabase=0;
160
#endif
Johan Pascal's avatar
Johan Pascal committed
161 162
		} else if (strcmp(argv[i],"--bench")==0){
			bench=true;
johan's avatar
johan committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
		}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], lime_helper);
			}
			return ret;
		}
	}
	ret = bc_tester_start(argv[0]);
	lime_tester_uninit();
	bctbx_uninit_logger();
	return ret;
}

#endif