belle_sip_tester_windows.cpp 4.29 KB
Newer Older
1 2 3 4 5 6
#include <string>

#include "belle_sip_tester_windows.h"

using namespace belle_sip_tester_runtime_component;
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 19
BelleSipTester^ BelleSipTester::_instance = ref new BelleSipTester();

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 50 51 52 53
static void belleSipNativeOutputTraceHandler(belle_sip_log_level lev, const char *fmt, va_list args)
{
	nativeOutputTraceHandler((int)lev, fmt, args);
}

54

55
BelleSipTester::BelleSipTester()
56 57
{
	belle_sip_tester_init(nativeOutputTraceHandler);
58
	bc_tester_set_resource_dir_prefix("Assets");
59 60 61 62 63 64 65
}

BelleSipTester::~BelleSipTester()
{
	belle_sip_tester_uninit();
}

66 67 68 69 70 71 72 73
void BelleSipTester::setWritableDirectory(StorageFolder^ folder)
{
	char writable_dir[MAX_WRITABLE_DIR_SIZE] = { 0 };
	const wchar_t *wwritable_dir = folder->Path->Data();
	wcstombs(writable_dir, wwritable_dir, sizeof(writable_dir));
	bc_tester_set_writable_dir_prefix(writable_dir);
}

74 75 76 77 78
void BelleSipTester::setOutputTraceListener(OutputTraceListener^ traceListener)
{
	sTraceListener = traceListener;
}

79 80 81 82 83 84 85 86 87 88 89 90 91
void BelleSipTester::init(bool verbose)
{
	if (verbose) {
		belle_sip_set_log_level(BELLE_SIP_LOG_DEBUG);
	}
	else {
		belle_sip_set_log_level(BELLE_SIP_LOG_ERROR);
	}

	belle_sip_tester_set_root_ca_path("Assets/rootca.pem");
	pool = belle_sip_object_pool_push();
}

92
bool BelleSipTester::run(Platform::String^ suiteName, Platform::String^ caseName, Platform::Boolean verbose)
93 94 95 96 97 98 99 100 101
{
	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 };
	wcstombs(csuitename, wssuitename.c_str(), sizeof(csuitename));
	wcstombs(ccasename, wscasename.c_str(), sizeof(ccasename));

102
	init(verbose);
103 104
	belle_sip_set_log_handler(belleSipNativeOutputTraceHandler);
	return bc_tester_run_tests(wssuitename == all ? 0 : csuitename, wscasename == all ? 0 : ccasename) != 0;
105 106
}

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
void BelleSipTester::runAllToXml()
{
	auto workItem = ref new WorkItemHandler([this](IAsyncAction ^workItem) {
		char *xmlFile = bc_tester_file("BelleSipWindows10.xml");
		char *logFile = bc_tester_file("BelleSipWindows10.log");
		char *args[] = { "--xml-file", xmlFile };
		bc_tester_parse_args(2, args, 0);
		init(true);
		FILE *f = fopen(logFile, "w");
		belle_sip_set_log_file(f);
		bc_tester_start();
		bc_tester_uninit();
		fclose(f);
		free(xmlFile);
		free(logFile);
	});
	_asyncAction = ThreadPool::RunAsync(workItem);
}

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
unsigned int BelleSipTester::nbTestSuites()
{
	return bc_tester_nb_suites();
}

unsigned int BelleSipTester::nbTests(Platform::String^ suiteName)
{
	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);
}

Platform::String^ BelleSipTester::testSuiteName(int index)
{
	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);
}

Platform::String^ BelleSipTester::testName(Platform::String^ suiteName, int testIndex)
{
	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);
}