bc_tester_utils.c 19.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
tester - liblinphone test suite
Copyright (C) 2013  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/>.
*/


/* this must be provided at compile time*/
#include BC_CONFIG_FILE

#include "bc_tester_utils.h"

#include <stdlib.h>
26
#include <time.h>
27
#include <stdio.h>
28

29 30 31 32 33
#if __clang__ || ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4)
#pragma GCC diagnostic push
#endif
#pragma GCC diagnostic ignored "-Wstrict-prototypes"

34
#include "CUnit/Basic.h"
35
#include "CUnit/Automated.h"
36
#include "CUnit/MyMem.h"
37

38 39 40 41
#if __clang__ || ((__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ > 4)
#pragma GCC diagnostic pop
#endif

42 43 44 45 46 47 48 49 50 51 52 53
#ifdef _WIN32
#if defined(__MINGW32__) || !defined(WINAPI_FAMILY_PARTITION) || !defined(WINAPI_PARTITION_DESKTOP)
#define BC_TESTER_WINDOWS_DESKTOP 1
#elif defined(WINAPI_FAMILY_PARTITION)
#if defined(WINAPI_PARTITION_DESKTOP) && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
#define BC_TESTER_WINDOWS_DESKTOP 1
#endif
#if defined(WINAPI_PARTITION_PHONE_APP) && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_PHONE_APP)
#define BC_TESTER_WINDOWS_PHONE 1
#endif
#if defined(WINAPI_PARTITION_APP) && WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)
#define BC_TESTER_WINDOWS_UNIVERSAL 1
54 55
#endif
#endif
56 57
#endif

58 59 60 61 62
#ifdef __linux
/*for monitoring total space allocated via malloc*/
#include <malloc.h>
#endif

63
static char *bc_tester_resource_dir_prefix = NULL;
64
// by default writable will always write near the executable
65
static char *bc_tester_writable_dir_prefix = NULL;
66

67 68 69
static char *bc_current_suite_name = NULL;
static char *bc_current_test_name = NULL;

70 71 72
int bc_printf_verbosity_info;
int bc_printf_verbosity_error;

73 74 75
static test_suite_t **test_suite = NULL;
static int nb_test_suites = 0;

76 77
#ifdef HAVE_CU_CURSES
#include "CUnit/CUCurses.h"
78 79 80
static unsigned char curses = 0;
#endif

81
char* xml_file = "CUnitAutomated-Results.xml";
82 83 84
int   xml_enabled = 0;
char * suite_name;
char * test_name;
85 86
static long max_vm_kb = 0;

87
void (*tester_printf_va)(int level, const char *format, va_list args);
88

89
void bc_tester_printf(int level, const char *format, ...) {
90
	va_list args;
91 92
	va_start (args, format);
	tester_printf_va(level, format, args);
93 94 95
	va_end (args);
}

96
int bc_tester_run_suite(test_suite_t *suite) {
97 98
	int i;

99
	CU_pSuite pSuite = CU_add_suite(suite->name, suite->before_all, suite->after_all);
100 101 102 103 104 105 106 107 108 109

	for (i = 0; i < suite->nb_tests; i++) {
		if (NULL == CU_add_test(pSuite, suite->tests[i].name, suite->tests[i].func)) {
			return CU_get_error();
		}
	}

	return 0;
}

110
const char * bc_tester_suite_name(int suite_index) {
111 112 113 114
	if (suite_index >= nb_test_suites) return NULL;
	return test_suite[suite_index]->name;
}

115
int bc_tester_suite_index(const char *suite_name) {
116 117 118
	int i;

	for (i = 0; i < nb_test_suites; i++) {
119
		if (strcmp(suite_name, test_suite[i]->name) == 0) {
120 121 122 123 124 125
			return i;
		}
	}

	return -1;
}
126

127
int bc_tester_nb_suites(void) {
128 129 130 131 132
	return nb_test_suites;
}

const char * bc_tester_test_name(const char *suite_name, int test_index) {
	int suite_index = bc_tester_suite_index(suite_name);
133 134 135 136 137
	if ((suite_index < 0) || (suite_index >= nb_test_suites)) return NULL;
	if (test_index >= test_suite[suite_index]->nb_tests) return NULL;
	return test_suite[suite_index]->tests[test_index].name;
}

138 139
int bc_tester_nb_tests(const char *suite_name) {
	int i = bc_tester_suite_index(suite_name);
140 141 142 143
	if (i < 0) return 0;
	return test_suite[i]->nb_tests;
}

144
void bc_tester_list_suites(void) {
145 146
	int j;
	for(j=0;j<nb_test_suites;j++) {
147
		bc_tester_printf(bc_printf_verbosity_info, "%s", bc_tester_suite_name(j));
148 149 150
	}
}

151
void bc_tester_list_tests(const char *suite_name) {
152
	int j;
153 154
	for( j = 0; j < bc_tester_nb_tests(suite_name); j++) {
		const char *test_name = bc_tester_test_name(suite_name, j);
155
		bc_tester_printf(bc_printf_verbosity_info, "%s", test_name);
156 157 158 159 160 161
	}
}

static void all_complete_message_handler(const CU_pFailureRecord pFailure) {
#ifdef HAVE_CU_GET_SUITE
	char * results = CU_get_run_results_string();
162
	bc_tester_printf(bc_printf_verbosity_info,"\n%s",results);
163
	CU_FREE(results);
164 165 166 167
#endif
}

static void suite_init_failure_message_handler(const CU_pSuite pSuite) {
168
	bc_tester_printf(bc_printf_verbosity_error,"Suite initialization failed for [%s]", pSuite->pName);
169 170 171
}

static void suite_cleanup_failure_message_handler(const CU_pSuite pSuite) {
172
	bc_tester_printf(bc_printf_verbosity_error,"Suite cleanup failed for [%s]", pSuite->pName);
173 174 175
}

#ifdef HAVE_CU_GET_SUITE
176
static time_t suite_start_time = 0;
177
static void suite_start_message_handler(const CU_pSuite pSuite) {
178
	bc_tester_printf(bc_printf_verbosity_info,"Suite [%s] started\n", pSuite->pName);
179
	suite_start_time = time(NULL);
180
	bc_current_suite_name = pSuite->pName;
181 182
}
static void suite_complete_message_handler(const CU_pSuite pSuite, const CU_pFailureRecord pFailure) {
183 184
	bc_tester_printf(bc_printf_verbosity_info, "Suite [%s] ended in %lu sec\n", pSuite->pName,
					 time(NULL) - suite_start_time);
185 186
}

187
static time_t test_start_time = 0;
188
static void test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite) {
189 190 191 192
	int suite_index = bc_tester_suite_index(pSuite->pName);
	if (test_suite[suite_index]->before_each) {
		test_suite[suite_index]->before_each();
	}
193 194
	bc_tester_printf(bc_printf_verbosity_info,"Suite [%s] Test [%s] started", pSuite->pName,pTest->pName);
	test_start_time = time(NULL);
195
	bc_current_test_name = pTest->pName;
196 197 198
}

/*derivated from cunit*/
199 200
static void test_complete_message_handler(const CU_pTest pTest, const CU_pSuite pSuite,
										  const CU_pFailureRecord pFailureList) {
201
	int i;
202
	int suite_index = bc_tester_suite_index(pSuite->pName);
203
	CU_pFailureRecord pFailure = pFailureList;
204 205
	char *buffer = NULL;
	char* result = bc_sprintf("Suite [%s] Test [%s] %s in %lu secs", pSuite->pName, pTest->pName,
206
			 pFailure ? "failed" : "passed", (unsigned long)(time(NULL) - test_start_time));
207

208
	if (pFailure) {
209 210 211 212 213 214 215 216 217
		for (i = 1; (NULL != pFailure); pFailure = pFailure->pNext, i++) {
			buffer = bc_sprintf("%s\n    %d. %s:%u  - %s",
									result,
									i,
									(NULL != pFailure->strFileName) ? pFailure->strFileName : "",
									pFailure->uiLineNumber,
									(NULL != pFailure->strCondition) ? pFailure->strCondition : "");
			free(result);
			result = buffer;
218 219
		}
	}
220

221
	bc_tester_printf(bc_printf_verbosity_info,"%s", result);
222
	free(result);
223

224 225 226 227 228 229
	if (test_suite[suite_index]->after_each) {
		test_suite[suite_index]->after_each();
	}
	//insert empty line
	bc_tester_printf(bc_printf_verbosity_info,"");

230 231 232
#ifdef __linux
	/* use mallinfo() to monitor allocated space. It is linux specific but other methods don't work:
	 * setrlimit() RLIMIT_DATA doesn't count memory allocated via mmap() (which is used internally by malloc)
233 234
	 * setrlimit() RLIMIT_AS works but also counts virtual memory allocated by thread stacks, which is very big and
	 * hardly controllable.
235
	 * setrlimit() RLIMIT_RSS does nothing interesting on linux.
236 237
	 * getrusage() of RSS is unreliable: memory blocks can be leaked without being read or written, which would not
	 * appear in RSS.
238 239
	 * mallinfo() itself is the less worse solution. Allocated bytes are returned as 'int' so limited to 2GB
	 */
240
	if (max_vm_kb) {
241
		struct mallinfo minfo = mallinfo();
242 243 244 245 246
		if (minfo.uordblks > max_vm_kb * 1024) {
			bc_tester_printf(
				bc_printf_verbosity_error,
				"The program exceeded the maximum amount of memory allocatable (%i bytes), aborting now.\n",
				minfo.uordblks);
247 248 249 250
			abort();
		}
	}
#endif
251 252 253
}
#endif

254
int bc_tester_run_tests(const char *suite_name, const char *test_name) {
255 256 257 258 259 260 261
	int i;

	/* initialize the CUnit test registry */
	if (CUE_SUCCESS != CU_initialize_registry())
		return CU_get_error();

	for (i = 0; i < nb_test_suites; i++) {
262
		bc_tester_run_suite(test_suite[i]);
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
	}
#ifdef HAVE_CU_GET_SUITE
	CU_set_suite_start_handler(suite_start_message_handler);
	CU_set_suite_complete_handler(suite_complete_message_handler);
	CU_set_test_start_handler(test_start_message_handler);
	CU_set_test_complete_handler(test_complete_message_handler);
#endif
	CU_set_all_test_complete_handler(all_complete_message_handler);
	CU_set_suite_init_failure_handler(suite_init_failure_message_handler);
	CU_set_suite_cleanup_failure_handler(suite_cleanup_failure_message_handler);

	if( xml_enabled != 0 ){
		CU_automated_run_tests();
	} else {

278
#ifndef HAVE_CU_GET_SUITE
279
		if( suite_name ){
280
			bc_tester_printf(bc_printf_verbosity_info, "Tester compiled without CU_get_suite() function, running all tests instead of suite '%s'", suite_name);
281 282 283 284 285 286
		}
#else
		if (suite_name){
			CU_pSuite suite;
			suite=CU_get_suite(suite_name);
			if (!suite) {
287
				bc_tester_printf(bc_printf_verbosity_error, "Could not find suite '%s'. Available suites are:", suite_name);
288
				bc_tester_list_suites();
289 290 291 292
				return -1;
			} else if (test_name) {
				CU_pTest test=CU_get_test_by_name(test_name, suite);
				if (!test) {
293
					bc_tester_printf(bc_printf_verbosity_error, "Could not find test '%s' in suite '%s'. Available tests are:", test_name, suite_name);
294
					// do not use suite_name here, since this method is case sensitive
295
					bc_tester_list_tests(suite->pName);
296 297 298
					return -2;
				} else {
					CU_ErrorCode err= CU_run_test(suite, test);
299
					if (err != CUE_SUCCESS) bc_tester_printf(bc_printf_verbosity_error, "CU_basic_run_test error %d", err);
300 301 302 303 304 305 306 307
				}
			} else {
				CU_run_suite(suite);
			}
		}
		else
#endif
		{
308
#ifdef HAVE_CU_CURSES
309 310 311 312 313 314 315 316 317 318 319 320
			if (curses) {
			/* Run tests using the CUnit curses interface */
				CU_curses_run_tests();
			}
			else
#endif
			{
				/* Run all tests using the CUnit Basic interface */
				CU_run_all_tests();
			}
		}
	}
321
#ifdef __linux
322 323
	bc_tester_printf(bc_printf_verbosity_info, "Still %i kilobytes allocated when all tests are finished.",
					 mallinfo().uordblks / 1024);
324
#endif
325

326 327 328 329 330 331
	return CU_get_number_of_tests_failed()!=0;

}


void bc_tester_helper(const char *name, const char* additionnal_helper) {
332 333
	bc_tester_printf(bc_printf_verbosity_info,
					 "%s --help\n"
334 335 336
#ifdef HAVE_CU_CURSES
					 "\t\t\t--curses\n"
#endif
337 338 339 340
					 "\t\t\t--list-suites\n"
					 "\t\t\t--list-tests <suite>\n"
					 "\t\t\t--suite <suite name>\n"
					 "\t\t\t--test <test name>\n"
341 342
					 "\t\t\t--resource-dir <folder path> (directory where tester resource are located)\n"
					 "\t\t\t--writable-dir <folder path> (directory where temporary files should be created)\n"
343 344 345 346 347
					 "\t\t\t--xml\n"
					 "\t\t\t--xml-file <xml file name>\n"
					 "\t\t\t--max-alloc <size in ko> (maximum ammount of memory obtained via malloc allocator)\n"
					 "And additionally:\n"
					 "%s",
348 349
					 name,
					 additionnal_helper);
350 351
}

352
#if !defined(BC_TESTER_WINDOWS_PHONE) && !defined(BC_TESTER_WINDOWS_UNIVERSAL) && !defined(__QNX__) && !defined(ANDROID) && !defined(IOS)
353
static int file_exists(const char* root_path) {
354 355 356
	char * res_path = bc_sprintf("%s/common/bc_completion", root_path);
	FILE* file = fopen(res_path, "r");
	int found = (file != NULL);
357
	free(res_path);
358 359 360
	if (file) {
		fclose(file);
	}
361 362
	return found;
}
363
#endif
364 365

static void detect_res_prefix(const char* prog) {
366
	char* progpath = NULL;
367
	FILE* writable_file = NULL;
368

369
	if (prog != NULL) {
370
		progpath = strdup(prog);
371 372 373 374 375
		if (strchr(prog, '/') != NULL) {
			progpath[strrchr(prog, '/') - prog + 1] = '\0';
		} else if (strchr(prog, '\\') != NULL) {
			progpath[strrchr(prog, '\\') - prog + 1] = '\0';
		}
376
	}
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
#if !defined(BC_TESTER_WINDOWS_PHONE) && !defined(BC_TESTER_WINDOWS_UNIVERSAL) && !defined(__QNX__) && !defined(ANDROID) && !defined(IOS)
	{
		char* prefix = NULL;

		if (file_exists(".")) {
			prefix = strdup(".");
		} else if (file_exists("..")) {
			prefix = strdup("..");
		} else if (progpath) {
			//for autotools, binary is in .libs/ subdirectory
			char * progpath2 = bc_sprintf("%s/../", progpath);
			if (file_exists(progpath)) {
				prefix = strdup(progpath);
			} else if (file_exists(progpath2)) {
				prefix = strdup(progpath2);
			}
			free(progpath2);
394
		}
395

396 397 398 399
		if (bc_tester_resource_dir_prefix != NULL && !file_exists(bc_tester_resource_dir_prefix)) {
			bc_tester_printf(bc_printf_verbosity_error, "Invalid provided resource directory: could not find expected resources in %s.\n", bc_tester_resource_dir_prefix);
			free(bc_tester_resource_dir_prefix);
			bc_tester_resource_dir_prefix = NULL;
400
		}
401

402 403 404 405 406 407 408 409 410 411 412
		if (prefix != NULL) {
			if (bc_tester_resource_dir_prefix == NULL) {
				bc_tester_printf(bc_printf_verbosity_error, "Resource directory set to %s\n", prefix);
				bc_tester_set_resource_dir_prefix(prefix);
			}

			if (bc_tester_writable_dir_prefix == NULL) {
				bc_tester_printf(bc_printf_verbosity_error, "Writable directory set to %s\n", prefix);
				bc_tester_set_writable_dir_prefix(prefix);
			}
			free(prefix);
413
		}
414
	}
415
#endif
416 417 418

	// check that we can write in writable directory
	if (bc_tester_writable_dir_prefix != NULL) {
419 420
		char * writable_file_path = bc_sprintf("%s/%s", bc_tester_writable_dir_prefix, ".bc_tester_utils.tmp");
		writable_file = fopen(writable_file_path, "w");
421 422 423
		if (writable_file) {
			fclose(writable_file);
		}
424
		free(writable_file_path);
425 426 427
	}
	if (bc_tester_resource_dir_prefix == NULL || writable_file == NULL) {
		if (bc_tester_resource_dir_prefix == NULL) {
428 429
			bc_tester_printf(bc_printf_verbosity_error, "Failed to detect resources for %s.\n", prog);
			bc_tester_printf(bc_printf_verbosity_error, "Could not find resource directory in %s! Please try again using option --resource-dir.\n", progpath);
430 431
		}
		if (writable_file == NULL) {
432
			bc_tester_printf(bc_printf_verbosity_error, "Failed to write file in %s. Please try again using option --writable-dir.\n", bc_tester_writable_dir_prefix);
433
		}
434
		abort();
435
	}
436 437 438 439

	if (progpath != NULL) {
		free(progpath);
	}
440 441
}

442
void bc_tester_init(void (*ftester_printf)(int level, const char *format, va_list args), int iverbosity_info, int iverbosity_error) {
443
	tester_printf_va = ftester_printf;
444 445
	bc_printf_verbosity_error = iverbosity_error;
	bc_printf_verbosity_info = iverbosity_info;
446
	bc_tester_writable_dir_prefix = strdup(".");
447 448
}

449
void bc_tester_set_max_vm(long max_vm_kb) {
450 451 452 453
#ifdef __linux
	max_vm_kb = max_vm_kb;
	bc_tester_printf(bc_printf_verbosity_info, "Maximum virtual memory space set to %li kilo bytes", max_vm_kb);
#else
454
	bc_tester_printf(bc_printf_verbosity_error, "Maximum virtual memory space setting is only implemented on Linux.");
455 456 457
#endif
}

458 459 460
int bc_tester_parse_args(int argc, char **argv, int argid)
{
	int i = argid;
461

462 463 464 465 466 467 468 469 470
	if (strcmp(argv[i],"--help")==0){
		return -1;
	} else if (strcmp(argv[i],"--test")==0){
		CHECK_ARG("--test", ++i, argc);
		test_name=argv[i];
	}else if (strcmp(argv[i],"--suite")==0){
		CHECK_ARG("--suite", ++i, argc);
		suite_name=argv[i];
	} else if (strcmp(argv[i],"--list-suites")==0){
471
		bc_tester_list_suites();
472 473 474 475
		return 0;
	} else if (strcmp(argv[i],"--list-tests")==0){
		CHECK_ARG("--list-tests", ++i, argc);
		suite_name = argv[i];
476
		bc_tester_list_tests(suite_name);
477 478 479 480 481 482 483
		return 0;
	} else if (strcmp(argv[i], "--xml-file") == 0){
		CHECK_ARG("--xml-file", ++i, argc);
		xml_file = argv[i];
		xml_enabled = 1;
	} else if (strcmp(argv[i], "--xml") == 0){
		xml_enabled = 1;
484
	} else if (strcmp(argv[i], "--max-alloc") == 0) {
485 486
		CHECK_ARG("--max-alloc", ++i, argc);
		max_vm_kb = atol(argv[i]);
487 488 489 490 491 492
	} else if (strcmp(argv[i], "--resource-dir") == 0) {
		CHECK_ARG("--resource-dir", ++i, argc);
		bc_tester_resource_dir_prefix = strdup(argv[i]);
	} else if (strcmp(argv[i], "--writable-dir") == 0) {
		CHECK_ARG("--writable-dir", ++i, argc);
		bc_tester_writable_dir_prefix = strdup(argv[i]);
493
	} else {
494
		bc_tester_printf(bc_printf_verbosity_error, "Unknown option \"%s\"\n", argv[i]);
495 496 497 498
		return -1;
	}

	if( xml_enabled && (suite_name || test_name) ){
499
		bc_tester_printf(bc_printf_verbosity_error, "Cannot use both XML and specific test suite\n");
500 501 502 503 504 505 506
		return -1;
	}

	/* returns number of arguments read + 1 */
	return i - argid + 1;
}

507
int bc_tester_start(const char* prog_name) {
508
	int ret;
509

510 511
	detect_res_prefix(prog_name);

512 513
	if (max_vm_kb)
		bc_tester_set_max_vm(max_vm_kb);
514

515
	if( xml_enabled ){
516
		char * xml_tmp_file = bc_sprintf("%s.tmp", xml_file);
517 518 519 520
		CU_set_output_filename(xml_tmp_file);
		free(xml_tmp_file);
	}

521
	ret = bc_tester_run_tests(suite_name, test_name);
522 523 524 525 526 527 528 529 530 531 532 533 534 535

	return ret;
}
void bc_tester_add_suite(test_suite_t *suite) {
	if (test_suite == NULL) {
		test_suite = (test_suite_t **)malloc(10 * sizeof(test_suite_t *));
	}
	test_suite[nb_test_suites] = suite;
	nb_test_suites++;
	if ((nb_test_suites % 10) == 0) {
		test_suite = (test_suite_t **)realloc(test_suite, (nb_test_suites + 10) * sizeof(test_suite_t *));
	}
}

536
void bc_tester_uninit(void) {
537
	/* Redisplay list of failed tests on end */
Simon Morlat's avatar
Simon Morlat committed
538 539
	/*BUG: do not display list of failures on mingw, it crashes mysteriously*/
#if !defined(WIN32) && !defined(_MSC_VER)
540 541 542 543
	/* Redisplay list of failed tests on end */
	if (CU_get_number_of_failure_records()){
		CU_basic_show_failures(CU_get_failure_list());
	}
Simon Morlat's avatar
Simon Morlat committed
544
#endif
545
	CU_cleanup_registry();
546
	/*add missing final newline*/
547
	bc_tester_printf(bc_printf_verbosity_info,"");
548 549 550

	if( xml_enabled ){
		/*create real xml file only if tester did not crash*/
551
		char * xml_tmp_file = bc_sprintf("%s.tmp-Results.xml", xml_file);
552 553 554 555 556 557 558 559 560
		rename(xml_tmp_file, xml_file);
		free(xml_tmp_file);
	}

	if (test_suite != NULL) {
		free(test_suite);
		test_suite = NULL;
		nb_test_suites = 0;
	}
561 562 563 564 565 566 567 568 569

	if (bc_tester_resource_dir_prefix != NULL) {
		free(bc_tester_resource_dir_prefix);
		bc_tester_resource_dir_prefix = NULL;
	}
	if (bc_tester_writable_dir_prefix != NULL) {
		free(bc_tester_writable_dir_prefix);
		bc_tester_writable_dir_prefix = NULL;
	}
570
}
571

572 573
static void bc_tester_set_dir_prefix(char **prefix, const char *name) {
	if (*prefix != NULL) free(*prefix);
Simon Morlat's avatar
Simon Morlat committed
574
	*prefix = strdup(name);
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
}

const char * bc_tester_get_resource_dir_prefix(void) {
	return bc_tester_resource_dir_prefix;
}

void bc_tester_set_resource_dir_prefix(const char *name) {
	bc_tester_set_dir_prefix(&bc_tester_resource_dir_prefix, name);
}

const char * bc_tester_get_writable_dir_prefix(void) {
	return bc_tester_writable_dir_prefix;
}

void bc_tester_set_writable_dir_prefix(const char *name) {
	bc_tester_set_dir_prefix(&bc_tester_writable_dir_prefix, name);
}

static char * bc_tester_path(const char *prefix, const char *name) {
594
	if (name) {
595 596 597
		return bc_sprintf("%s/%s", prefix, name);
	} else {
		return NULL;
598 599
	}
}
600 601 602 603 604 605 606 607

char * bc_tester_res(const char *name) {
	return bc_tester_path(bc_tester_resource_dir_prefix, name);
}

char * bc_tester_file(const char *name) {
	return bc_tester_path(bc_tester_writable_dir_prefix, name);
}
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631

char* bc_sprintfva(const char* format, va_list args) {
	/* Guess we need no more than 100 bytes. */
	int n, size = 200;
	char *p,*np;
#ifndef WIN32
	va_list cap;/*copy of our argument list: a va_list cannot be re-used (SIGSEGV on linux 64 bits)*/
#endif
	if ((p = malloc(size)) == NULL)
		return NULL;
	while (1)
	{
		/* Try to print in the allocated space. */
#ifndef WIN32
		va_copy(cap,args);
		n = vsnprintf (p, size, format, cap);
		va_end(cap);
#else
		/*this works on 32 bits, luckily*/
		n = vsnprintf (p, size, format, args);
#endif
		/* If that worked, return the string. */
		if (n > -1 && n < size)
			return p;
632
		//bc_tester_printf(bc_printf_verbosity_error, "Reallocing space.\n");
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
		/* Else try again with more space. */
		if (n > -1)	/* glibc 2.1 */
			size = n + 1;	/* precisely what is needed */
		else		/* glibc 2.0 */
			size *= 2;	/* twice the old size */
		if ((np = realloc (p, size)) == NULL)
		{
			free(p);
			return NULL;
		}
		else
		{
			p = np;
		}
	}
}

char* bc_sprintf(const char* format, ...) {
	va_list args;
	char* res;
	va_start(args, format);
	res = bc_sprintfva(format, args);
	va_end (args);
	return res;
}
658

659
const char * bc_tester_current_suite_name(void) {
660 661 662
	return bc_current_suite_name;
}

663
const char * bc_tester_current_test_name(void) {
664 665
	return bc_current_test_name;
}