belle_sip_tester_windows.cpp 4.41 KB
Newer Older
1 2 3 4
#include <string>

#include "belle_sip_tester_windows.h"

5
using namespace BelledonneCommunications::BelleSip::Tester;
6
using namespace Platform;
7
using namespace Windows::Foundation;
8
using namespace Windows::Storage;
9
using namespace Windows::System::Threading;
10

Ghislain MARY's avatar
Ghislain MARY committed
11
#define MAX_TRACE_SIZE		2048
12
#define MAX_SUITE_NAME_SIZE	128
13
#define MAX_WRITABLE_DIR_SIZE 1024
14 15 16 17

static OutputTraceListener^ sTraceListener;
static belle_sip_object_pool_t *pool;

18
NativeTester^ NativeTester::_instance = ref new NativeTester();
19

20 21 22 23 24 25 26
static void nativeOutputTraceHandler(int lev, const char *fmt, va_list args)
{
	if (sTraceListener) {
		wchar_t wstr[MAX_TRACE_SIZE];
		std::string str;
		str.resize(MAX_TRACE_SIZE);
		vsnprintf((char *)str.c_str(), MAX_TRACE_SIZE, fmt, args);
Ghislain MARY's avatar
Ghislain MARY committed
27
		mbstowcs(wstr, str.c_str(), MAX_TRACE_SIZE - 1);
28
		String^ msg = ref new String(wstr);
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
		String^ l;
		switch (lev) {
		case BELLE_SIP_LOG_FATAL:
		case BELLE_SIP_LOG_ERROR:
			l = ref new String(L"Error");
			break;
		case BELLE_SIP_LOG_WARNING:
			l = ref new String(L"Warning");
			break;
		case BELLE_SIP_LOG_MESSAGE:
			l = ref new String(L"Message");
			break;
		default:
			l = ref new String(L"Debug");
			break;
		}
		sTraceListener->outputTrace(l, msg);
46 47 48
	}
}

49
static void belleSipNativeOutputTraceHandler(void *info, const char *domain, BctbxLogLevel lev, const char *fmt, va_list args)
50 51 52 53
{
	nativeOutputTraceHandler((int)lev, fmt, args);
}

54

55
NativeTester::NativeTester()
56 57 58
{
}

59
NativeTester::~NativeTester()
60 61 62 63
{
	belle_sip_tester_uninit();
}

64
void NativeTester::setOutputTraceListener(OutputTraceListener^ traceListener)
65 66 67 68
{
	sTraceListener = traceListener;
}

69
void NativeTester::initialize(StorageFolder^ writableDirectory, Platform::Boolean ui)
70
{
71 72
	if (ui) {
		belle_sip_tester_init(nativeOutputTraceHandler);
73 74
	}
	else {
75 76
		belle_sip_tester_init(NULL);
		belle_sip_set_log_level(BELLE_SIP_LOG_DEBUG);
77 78 79 80
	}

	belle_sip_tester_set_root_ca_path("Assets/rootca.pem");
	pool = belle_sip_object_pool_push();
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

	char writable_dir[MAX_WRITABLE_DIR_SIZE] = { 0 };
	const wchar_t *wwritable_dir = writableDirectory->Path->Data();
	wcstombs(writable_dir, wwritable_dir, sizeof(writable_dir));
	bc_tester_set_writable_dir_prefix(writable_dir);
	bc_tester_set_resource_dir_prefix("Assets");

	if (!ui) {
		char *xmlFile = bc_tester_file("BelleSipWindows10.xml");
		char *args[] = { "--xml-file", xmlFile };
		bc_tester_parse_args(2, args, 0);

		char *logFile = bc_tester_file("BelleSipWindows10.log");
		belle_sip_tester_set_log_file(logFile);
		free(logFile);
	}
97 98
}

99
bool NativeTester::run(Platform::String^ suiteName, Platform::String^ caseName, Platform::Boolean verbose)
100 101 102 103 104 105
{
	std::wstring all(L"ALL");
	std::wstring wssuitename = suiteName->Data();
	std::wstring wscasename = caseName->Data();
	char csuitename[MAX_SUITE_NAME_SIZE] = { 0 };
	char ccasename[MAX_SUITE_NAME_SIZE] = { 0 };
106
	bctbx_log_handler_t *log_handler = bctbx_create_log_handler(belleSipNativeOutputTraceHandler, NULL, NULL);
107 108 109
	wcstombs(csuitename, wssuitename.c_str(), sizeof(csuitename));
	wcstombs(ccasename, wscasename.c_str(), sizeof(ccasename));

110 111 112 113 114 115
	if (verbose) {
		belle_sip_set_log_level(BELLE_SIP_LOG_DEBUG);
	}
	else {
		belle_sip_set_log_level(BELLE_SIP_LOG_ERROR);
	}
116
	bctbx_add_log_handler(log_handler);
117
	return bc_tester_run_tests(wssuitename == all ? 0 : csuitename, wscasename == all ? 0 : ccasename, NULL) != 0;
118 119
}

120
void NativeTester::runAllToXml()
121 122
{
	auto workItem = ref new WorkItemHandler([this](IAsyncAction ^workItem) {
123
		bc_tester_start(NULL);
124 125 126 127 128
		bc_tester_uninit();
	});
	_asyncAction = ThreadPool::RunAsync(workItem);
}

129
unsigned int NativeTester::nbTestSuites()
130 131 132 133
{
	return bc_tester_nb_suites();
}

134
unsigned int NativeTester::nbTests(Platform::String^ suiteName)
135 136 137 138 139 140 141
{
	std::wstring suitename = suiteName->Data();
	char cname[MAX_SUITE_NAME_SIZE] = { 0 };
	wcstombs(cname, suitename.c_str(), sizeof(cname));
	return bc_tester_nb_tests(cname);
}

142
Platform::String^ NativeTester::testSuiteName(int index)
143 144 145 146 147 148 149
{
	const char *cname = bc_tester_suite_name(index);
	wchar_t wcname[MAX_SUITE_NAME_SIZE];
	mbstowcs(wcname, cname, sizeof(wcname));
	return ref new String(wcname);
}

150
Platform::String^ NativeTester::testName(Platform::String^ suiteName, int testIndex)
151 152 153 154 155 156 157 158 159
{
	std::wstring suitename = suiteName->Data();
	char csuitename[MAX_SUITE_NAME_SIZE] = { 0 };
	wcstombs(csuitename, suitename.c_str(), sizeof(csuitename));
	const char *cname = bc_tester_test_name(csuitename, testIndex);
	wchar_t wcname[MAX_SUITE_NAME_SIZE];
	mbstowcs(wcname, cname, sizeof(wcname));
	return ref new String(wcname);
}