Esempio n. 1
0
DECLARE_TEST(error, context) {
	error_context_t* context = 0;

	context = error_context();
	if (context)
		EXPECT_EQ(context->depth, 0);

	error_context_push(STRING_CONST("error test"), STRING_CONST("data"));
	context = error_context();

#if BUILD_ENABLE_ERROR_CONTEXT
	EXPECT_NE(context, 0);
	EXPECT_EQ(context->depth, 1);
	EXPECT_CONSTSTRINGEQ(context->frame[0].name, string_const(STRING_CONST("error test")));
	EXPECT_CONSTSTRINGEQ(context->frame[0].data, string_const(STRING_CONST("data")));
#else
	EXPECT_EQ(context, 0);
#endif

	error_context_pop();
	context = error_context();

#if BUILD_ENABLE_ERROR_CONTEXT
	EXPECT_NE(context, 0);
	EXPECT_EQ(context->depth, 0);
#else
	EXPECT_EQ(context, 0);
#endif

	error_context_push(STRING_CONST("error test"), STRING_CONST("data"));
	error_context_push(STRING_CONST("another test"), STRING_CONST("more data"));
	context = error_context();

#if BUILD_ENABLE_ERROR_CONTEXT
	EXPECT_NE(context, 0);
	EXPECT_EQ(context->depth, 2);
	EXPECT_CONSTSTRINGEQ(context->frame[0].name, string_const(STRING_CONST("error test")));
	EXPECT_CONSTSTRINGEQ(context->frame[0].data, string_const(STRING_CONST("data")));
	EXPECT_CONSTSTRINGEQ(context->frame[1].name, string_const(STRING_CONST("another test")));
	EXPECT_CONSTSTRINGEQ(context->frame[1].data, string_const(STRING_CONST("more data")));
#else
	EXPECT_EQ(context, 0);
#endif

	error_context_pop();
	context = error_context();

#if BUILD_ENABLE_ERROR_CONTEXT
	EXPECT_NE(context, 0);
	EXPECT_EQ(context->depth, 1);
	EXPECT_CONSTSTRINGEQ(context->frame[0].name, string_const(STRING_CONST("error test")));
	EXPECT_CONSTSTRINGEQ(context->frame[0].data, string_const(STRING_CONST("data")));
#else
	EXPECT_EQ(context, 0);
#endif

	error_context_pop();

	return 0;
}
Esempio n. 2
0
bin2hex_input_t bin2hex_parse_command_line( char const* const* cmdline )
{
    bin2hex_input_t input = {0};
    int arg, asize;

    error_context_push( "parsing command line", "" );
    for( arg = 1, asize = array_size( cmdline ); arg < asize; ++arg )
    {
        if( string_equal( cmdline[arg], "--columns" ) )
        {
            if( arg < ( asize - 1 ) )
                input.columns = string_to_int( cmdline[++arg] );
        }
        else if( string_equal( cmdline[arg], "--" ) )
            break; //Stop parsing cmdline options
        else if( ( string_length( cmdline[arg] ) > 2 ) && string_equal_substr( cmdline[arg], "--", 2 ) )
            continue; //Cmdline argument not parsed here
        else
        {
            array_push( input.input_files, string_clone( cmdline[arg] ) );
            array_push( input.output_files, string_format( "%s.hex", cmdline[arg] ) );
        }
    }
    error_context_pop();

    return input;
}
Esempio n. 3
0
hashify_input_t hashify_parse_command_line( const char* const* cmdline )
{
	hashify_input_t input = {0};
	int arg, asize;

	input.check_only = false;

	error_context_push( "parsing command line", "" );
	for( arg = 1, asize = array_size( cmdline ); arg < asize; ++arg )
	{
		if( string_equal( cmdline[arg], "--validate" ) )
		{
			input.check_only = true;
			continue;
		}
		else if( string_equal( cmdline[arg], "--generate-string" ) )
		{
			if( arg < asize - 1 )
			{
				++arg;
				array_push( input.strings, string_clone( cmdline[arg] ) );
			}
			continue;
		}
		else if( string_equal( cmdline[arg], "--" ) )
			break; //Stop parsing cmdline options
		else if( ( string_length( cmdline[arg] ) > 2 ) && string_equal_substr( cmdline[arg], "--", 2 ) )
			continue; //Cmdline argument not parsed here			

		array_push( input.files, string_clone( cmdline[arg] ) );
	}
	error_context_pop();

	return input;
}
Esempio n. 4
0
int bin2hex_process_files( char const* const* input, char const* const* output, int columns )
{
    int result = BIN2HEX_RESULT_OK;
    unsigned int ifile, files_size;
    for( ifile = 0, files_size = array_size( input ); ( result == BIN2HEX_RESULT_OK ) && ( ifile < files_size ); ++ifile )
    {
        char* input_filename = 0;
        char* output_filename = 0;

        stream_t* input_file = 0;
        stream_t* output_file = 0;

        input_filename = path_clean( string_clone( input[ifile] ), path_is_absolute( input[ifile] ) );
        error_context_push( "parsing file", input_filename );

        output_filename = path_clean( string_clone( output[ifile] ), path_is_absolute( output[ifile] ) );

        log_infof( 0, "bin2hex %s -> %s", input_filename, output_filename );

        input_file = stream_open( input_filename, STREAM_IN | STREAM_BINARY );

        if( !input_file )
        {
            log_warnf( 0, WARNING_BAD_DATA, "Unable to open input file: %s", input_filename );
            result = BIN2HEX_RESULT_MISSING_INPUT_FILE;
        }
        else
        {
            output_file = stream_open( output_filename, STREAM_OUT );
            if( !output_file )
            {
                log_warnf( 0, WARNING_BAD_DATA, "Unable to open output file: %s", output_filename );
                result = BIN2HEX_RESULT_UNABLE_TO_OPEN_OUTPUT_FILE;
            }
        }

        if( input_file && output_file )
            result = bin2hex_process_file( input_file, output_file, columns );

        stream_deallocate( input_file );
        stream_deallocate( output_file );

        string_deallocate( output_filename );

        error_context_pop();
        string_deallocate( input_filename );
    }

    if( ( result == BIN2HEX_RESULT_OK ) && ( files_size > 0 ) )
        log_info( 0, "All files generated" );

    return result;
}
Esempio n. 5
0
hashify_input_t
hashify_parse_command_line(const string_const_t* cmdline) {
	hashify_input_t input;
	size_t arg, asize;

	memset(&input, 0, sizeof(input));
	input.check_only = false;

	error_context_push(STRING_CONST("parsing command line"), STRING_CONST(""));
	for (arg = 1, asize = array_size(cmdline); arg < asize; ++arg) {
		if (string_equal(STRING_ARGS(cmdline[arg]), STRING_CONST("--help"))) {
			hashify_print_usage();
			continue;
		}
		else if (string_equal(STRING_ARGS(cmdline[arg]), STRING_CONST("--validate"))) {
			input.check_only = true;
			continue;
		}
		else if (string_equal(STRING_ARGS(cmdline[arg]), STRING_CONST("--generate-string"))) {
			if (arg < asize - 1) {
				++arg;
				array_push(input.strings, string_clone(STRING_ARGS(cmdline[arg])));
			}
			continue;
		}
		else if (string_equal(STRING_ARGS(cmdline[arg]), STRING_CONST("--debug"))) {
			log_set_suppress(0, ERRORLEVEL_NONE);
			continue;
		}
		else if (string_equal(STRING_ARGS(cmdline[arg]), STRING_CONST("--")))
			break; //Stop parsing cmdline options
		else if ((cmdline[arg].length > 2) && string_equal(cmdline[arg].str, 2, STRING_CONST("--")))
			continue; //Cmdline argument not parsed here

		array_push(input.files, string_clone(STRING_ARGS(cmdline[arg])));
	}
	error_context_pop();

	if (array_size(cmdline) <= 1)
		hashify_print_usage();

	return input;
}
Esempio n. 6
0
DECLARE_TEST(error, output) {
#if BUILD_ENABLE_LOG
	error_handler_fn handler_error = error_handler();
	log_handler_fn handler_log = log_handler();
	string_const_t shortmsg = string_const(STRING_CONST("Short message with prefix"));
    string_const_t longmsg = string_const(STRING_CONST("Longer message which should be output without a prefix"));

	error_set_handler(ignore_error_handler);
	log_set_handler(log_verify_handler);

    log_enable_stdout(false);
    EXPECT_EQ(log_stdout(), false);
    log_warn(HASH_TEST, WARNING_SUSPICIOUS, STRING_ARGS(shortmsg));
    log_enable_stdout(true);
    EXPECT_EQ(log_stdout(), true);
	EXPECT_EQ(_last_log_context, HASH_TEST);
	EXPECT_EQ(_last_log_severity, ERRORLEVEL_WARNING);
	EXPECT_GE(_last_log_length, shortmsg.length);
	EXPECT_NE(string_find_string(_last_log_msg, _last_log_length, STRING_ARGS(shortmsg), 0), STRING_NPOS);
	EXPECT_GT(string_find_string(_last_log_msg, _last_log_length, STRING_ARGS(shortmsg), 0), 0);
	EXPECT_NE(string_find_string(_last_log_msg, _last_log_length, STRING_CONST("WARNING [suspicious]"), 0), STRING_NPOS);

	_last_log_context = 0;
	_last_log_severity = ERRORLEVEL_NONE;
	_last_log_msg = nullptr;
	_last_log_length = 0;

    log_enable_stdout(false);
	log_warn(HASH_TEST, (warning_t)0x1000, STRING_ARGS(shortmsg));
    log_enable_stdout(true);
	EXPECT_EQ(_last_log_context, HASH_TEST);
	EXPECT_EQ(_last_log_severity, ERRORLEVEL_WARNING);
	EXPECT_GE(_last_log_length, shortmsg.length);
	EXPECT_NE(string_find_string(_last_log_msg, _last_log_length, STRING_ARGS(shortmsg), 0), STRING_NPOS);
	EXPECT_GT(string_find_string(_last_log_msg, _last_log_length, STRING_ARGS(shortmsg), 0), 0);
	EXPECT_NE(string_find_string(_last_log_msg, _last_log_length, STRING_CONST("WARNING [4096]"), 0), STRING_NPOS);

	_last_log_context = 0;
	_last_log_severity = ERRORLEVEL_NONE;
	_last_log_msg = nullptr;
	_last_log_length = 0;

    log_enable_prefix(false);
    log_enable_stdout(false);
	log_warn(HASH_TEST, WARNING_SYSTEM_CALL_FAIL, STRING_ARGS(longmsg));
    log_enable_stdout(true);
    log_enable_prefix(true);
	EXPECT_EQ(_last_log_context, HASH_TEST);
	EXPECT_EQ(_last_log_severity, ERRORLEVEL_WARNING);
	EXPECT_NE(string_find_string(_last_log_msg, _last_log_length, STRING_ARGS(longmsg), 0), STRING_NPOS);
	EXPECT_GT(string_find_string(_last_log_msg, _last_log_length, STRING_ARGS(longmsg), 0), 0);

	_last_log_context = 0;
	_last_log_severity = ERRORLEVEL_NONE;
	_last_log_msg = nullptr;
	_last_log_length = 0;

    log_enable_stdout(false);
	log_error(HASH_TEST, ERROR_DEPRECATED, STRING_ARGS(shortmsg));
    log_enable_stdout(true);
	EXPECT_EQ(_last_log_context, HASH_TEST);
	EXPECT_EQ(_last_log_severity, ERRORLEVEL_ERROR);
	EXPECT_GE(_last_log_length, shortmsg.length);
	EXPECT_NE(string_find_string(_last_log_msg, _last_log_length, STRING_ARGS(shortmsg), 0), STRING_NPOS);
	EXPECT_GT(string_find_string(_last_log_msg, _last_log_length, STRING_ARGS(shortmsg), 0), 0);
	EXPECT_NE(string_find_string(_last_log_msg, _last_log_length, STRING_CONST("ERROR [deprecated]"), 0), STRING_NPOS);

	_last_log_context = 0;
	_last_log_severity = ERRORLEVEL_NONE;
	_last_log_msg = nullptr;
	_last_log_length = 0;

    log_enable_stdout(false);
	log_error(HASH_TEST, (error_t)0x1000, STRING_ARGS(shortmsg));
    log_enable_stdout(true);
	EXPECT_EQ(_last_log_context, HASH_TEST);
	EXPECT_EQ(_last_log_severity, ERRORLEVEL_ERROR);
	EXPECT_GE(_last_log_length, shortmsg.length);
	EXPECT_NE(string_find_string(_last_log_msg, _last_log_length, STRING_ARGS(shortmsg), 0), STRING_NPOS);
	EXPECT_GT(string_find_string(_last_log_msg, _last_log_length, STRING_ARGS(shortmsg), 0), 0);
	EXPECT_NE(string_find_string(_last_log_msg, _last_log_length, STRING_CONST("ERROR [4096]"), 0), STRING_NPOS);

	_last_log_context = 0;
	_last_log_severity = ERRORLEVEL_NONE;
	_last_log_msg = nullptr;
	_last_log_length = 0;

	log_enable_prefix(false);
    log_enable_stdout(false);
	log_error(HASH_TEST, ERROR_INVALID_VALUE, STRING_ARGS(longmsg));
    log_enable_stdout(true);
    log_enable_prefix(true);
	EXPECT_EQ(_last_log_context, HASH_TEST);
	EXPECT_EQ(_last_log_severity, ERRORLEVEL_ERROR);
	EXPECT_NE(string_find_string(_last_log_msg, _last_log_length, STRING_ARGS(longmsg), 0), STRING_NPOS);
	EXPECT_GT(string_find_string(_last_log_msg, _last_log_length, STRING_ARGS(longmsg), 0), 0);

	_last_log_context = 0;
	_last_log_severity = ERRORLEVEL_NONE;
	_last_log_msg = nullptr;
	_last_log_length = 0;

    log_enable_stdout(false);
	log_panic(HASH_TEST, ERROR_DEPRECATED, STRING_ARGS(shortmsg));
    log_enable_stdout(true);
	EXPECT_EQ(_last_log_context, HASH_TEST);
	EXPECT_EQ(_last_log_severity, ERRORLEVEL_PANIC);
	EXPECT_GE(_last_log_length, shortmsg.length);
	EXPECT_NE(string_find_string(_last_log_msg, _last_log_length, STRING_ARGS(shortmsg), 0), STRING_NPOS);
	EXPECT_GT(string_find_string(_last_log_msg, _last_log_length, STRING_ARGS(shortmsg), 0), 0);
	EXPECT_NE(string_find_string(_last_log_msg, _last_log_length, STRING_CONST("PANIC [deprecated]"), 0), STRING_NPOS);

	_last_log_context = 0;
	_last_log_severity = ERRORLEVEL_NONE;
	_last_log_msg = nullptr;
	_last_log_length = 0;

    log_enable_stdout(false);
	log_panic(HASH_TEST, (error_t)0x1000, STRING_ARGS(shortmsg));
    log_enable_stdout(true);
	EXPECT_EQ(_last_log_context, HASH_TEST);
	EXPECT_EQ(_last_log_severity, ERRORLEVEL_PANIC);
	EXPECT_GE(_last_log_length, shortmsg.length);
	EXPECT_NE(string_find_string(_last_log_msg, _last_log_length, STRING_ARGS(shortmsg), 0), STRING_NPOS);
	EXPECT_GT(string_find_string(_last_log_msg, _last_log_length, STRING_ARGS(shortmsg), 0), 0);
	EXPECT_NE(string_find_string(_last_log_msg, _last_log_length, STRING_CONST("PANIC [4096]"), 0), STRING_NPOS);

	_last_log_context = 0;
	_last_log_severity = ERRORLEVEL_NONE;
	_last_log_msg = nullptr;
	_last_log_length = 0;

	log_enable_prefix(false);
    log_enable_stdout(false);
	log_panic(HASH_TEST, ERROR_INVALID_VALUE, STRING_ARGS(longmsg));
    log_enable_stdout(true);
    log_enable_prefix(true);
	EXPECT_EQ(_last_log_context, HASH_TEST);
	EXPECT_EQ(_last_log_severity, ERRORLEVEL_PANIC);
	EXPECT_NE(string_find_string(_last_log_msg, _last_log_length, STRING_ARGS(longmsg), 0), STRING_NPOS);
	EXPECT_GT(string_find_string(_last_log_msg, _last_log_length, STRING_ARGS(longmsg), 0), 0);

#  if BUILD_ENABLE_ERROR_CONTEXT
    
	error_context_push(STRING_CONST("one"), STRING_CONST("dataone"));
	error_context_push(STRING_CONST("two"), STRING_CONST("datatwo"));
	error_context_push(STRING_CONST("three"), STRING_CONST("datathree"));

	_last_log_context = 0;
	_last_log_severity = ERRORLEVEL_NONE;
	_last_log_msg = nullptr;
	_last_log_length = 0;

    log_enable_stdout(false);
	log_error_context(HASH_TEST, ERRORLEVEL_INFO);
    log_enable_stdout(true);

    error_context_pop();
    error_context_pop();
    error_context_pop();

	EXPECT_SIZEEQ(string_find_string(_last_log_msg, _last_log_length, STRING_CONST("When one: dataone"), 0), STRING_NPOS);
	EXPECT_SIZEEQ(string_find_string(_last_log_msg, _last_log_length, STRING_CONST("When two: datatwo"), 0), STRING_NPOS);
	EXPECT_SIZENE(string_find_string(_last_log_msg, _last_log_length, STRING_CONST("When three: datathree"), 0), STRING_NPOS);

#  endif

	log_set_handler(handler_log);
	error_set_handler(handler_error);
#endif
	return 0;
}
Esempio n. 7
0
static void*
error_test_thread(void) {
	error_context_t* context = 0;

	EXPECT_EQ(error(), ERROR_NONE);
	EXPECT_EQ(error(), ERROR_NONE);

	error_report(ERRORLEVEL_WARNING, ERROR_ACCESS_DENIED);
	EXPECT_EQ(error(), ERROR_ACCESS_DENIED);
	EXPECT_EQ(error(), ERROR_NONE);

	error_report(ERRORLEVEL_ERROR, ERROR_INVALID_VALUE);
	EXPECT_EQ(error(), ERROR_INVALID_VALUE);
	EXPECT_EQ(error(), ERROR_NONE);

	context = error_context();
	if (context)
		EXPECT_EQ(context->depth, 0);

	error_context_push(STRING_CONST("error test"), STRING_CONST("data"));
	context = error_context();

#if BUILD_ENABLE_ERROR_CONTEXT
	EXPECT_NE(context, 0);
	EXPECT_EQ(context->depth, 1);
	EXPECT_CONSTSTRINGEQ(context->frame[0].name, string_const(STRING_CONST("error test")));
	EXPECT_CONSTSTRINGEQ(context->frame[0].data, string_const(STRING_CONST("data")));
#else
	EXPECT_EQ(context, 0);
#endif

	error_context_pop();
	context = error_context();

#if BUILD_ENABLE_ERROR_CONTEXT
	EXPECT_NE(context, 0);
	EXPECT_EQ(context->depth, 0);
#else
	EXPECT_EQ(context, 0);
#endif

	error_context_push(STRING_CONST("error test"), STRING_CONST("data"));
	error_context_push(STRING_CONST("another test"), STRING_CONST("more data"));
	context = error_context();

#if BUILD_ENABLE_ERROR_CONTEXT
	EXPECT_NE(context, 0);
	EXPECT_EQ(context->depth, 2);
	EXPECT_CONSTSTRINGEQ(context->frame[0].name, string_const(STRING_CONST("error test")));
	EXPECT_CONSTSTRINGEQ(context->frame[0].data, string_const(STRING_CONST("data")));
	EXPECT_CONSTSTRINGEQ(context->frame[1].name, string_const(STRING_CONST("another test")));
	EXPECT_CONSTSTRINGEQ(context->frame[1].data, string_const(STRING_CONST("more data")));
#else
	EXPECT_EQ(context, 0);
#endif

	error_context_pop();
	context = error_context();

#if BUILD_ENABLE_ERROR_CONTEXT
	EXPECT_NE(context, 0);
	EXPECT_EQ(context->depth, 1);
	EXPECT_CONSTSTRINGEQ(context->frame[0].name, string_const(STRING_CONST("error test")));
	EXPECT_CONSTSTRINGEQ(context->frame[0].data, string_const(STRING_CONST("data")));
#else
	EXPECT_EQ(context, 0);
#endif

	error_context_pop();

	return 0;
}
Esempio n. 8
0
DECLARE_TEST( error, context )
{
	error_context_t* context = 0;

	context = error_context();
	if( context )
		EXPECT_EQ( context->depth, 0 );

	error_context_push( "error test", "data" );
	context = error_context();

#if BUILD_ENABLE_ERROR_CONTEXT
	EXPECT_NE( context, 0 );
	EXPECT_EQ( context->depth, 1 );
	EXPECT_STREQ( context->frame[0].name, "error test" );
	EXPECT_STREQ( context->frame[0].data, "data" );
#else
	EXPECT_EQ( context, 0 );
#endif

	error_context_pop();
	context = error_context();

#if BUILD_ENABLE_ERROR_CONTEXT
	EXPECT_NE( context, 0 );
	EXPECT_EQ( context->depth, 0 );
#else
	EXPECT_EQ( context, 0 );
#endif

	error_context_push( "error test", "data" );
	error_context_push( "another test", "more data" );
	context = error_context();

#if BUILD_ENABLE_ERROR_CONTEXT
	EXPECT_NE( context, 0 );
	EXPECT_EQ( context->depth, 2 );
	EXPECT_STREQ( context->frame[0].name, "error test" );
	EXPECT_STREQ( context->frame[0].data, "data" );
	EXPECT_STREQ( context->frame[1].name, "another test" );
	EXPECT_STREQ( context->frame[1].data, "more data" );
#else
	EXPECT_EQ( context, 0 );
#endif

	error_context_pop();
	context = error_context();

#if BUILD_ENABLE_ERROR_CONTEXT
	EXPECT_NE( context, 0 );
	EXPECT_EQ( context->depth, 1 );
	EXPECT_STREQ( context->frame[0].name, "error test" );
	EXPECT_STREQ( context->frame[0].data, "data" );
#else
	EXPECT_EQ( context, 0 );
#endif

	error_context_pop();

	return 0;
}
Esempio n. 9
0
rendercompile_input_t
rendercompile_parse_command_line(const string_const_t* cmdline) {
	rendercompile_input_t input;
	size_t arg, asize;

	memset(&input, 0, sizeof(input));

	for (arg = 1, asize = array_size(cmdline); arg < asize; ++arg) {
		if (string_equal(STRING_ARGS(cmdline[arg]), STRING_CONST("--help")))
			input.display_help = true;
		else if (string_equal(STRING_ARGS(cmdline[arg]), STRING_CONST("--source"))) {
			if (arg < asize - 1)
				input.source_path = cmdline[++arg];
		}
		/*else if (string_equal(STRING_ARGS(cmdline[arg]), STRING_CONST("--uuid"))) {
			if (arg < asize - 1) {
				++arg;
				input.uuid = string_to_uuid(STRING_ARGS(cmdline[arg]));
				if (uuid_is_null(input.uuid))
					log_warnf(HASH_RESOURCE, WARNING_INVALID_VALUE, STRING_CONST("Invalid UUID: %.*s"),
					          STRING_FORMAT(cmdline[arg]));
			}
		}
		else if (string_equal(STRING_ARGS(cmdline[arg]), STRING_CONST("--set"))) {
			if (arg < asize - 2) {
				input.key = cmdline[++arg];
				input.value = cmdline[++arg];
			}
		}*/
		else if (string_equal(STRING_ARGS(cmdline[arg]), STRING_CONST("--binary"))) {
			input.binary = 1;
		}
		else if (string_equal(STRING_ARGS(cmdline[arg]), STRING_CONST("--ascii"))) {
			input.binary = 0;
		}
		else if (string_equal(STRING_ARGS(cmdline[arg]), STRING_CONST("--debug"))) {
			log_set_suppress(0, ERRORLEVEL_NONE);
			log_set_suppress(HASH_RESOURCE, ERRORLEVEL_NONE);
			log_set_suppress(HASH_RENDER, ERRORLEVEL_NONE);
		}
		else if (string_equal(STRING_ARGS(cmdline[arg]), STRING_CONST("--")))
			break; //Stop parsing cmdline options
		else {
			array_push(input.input_files, cmdline[arg]);
		}
	}
	error_context_pop();

	bool already_help = input.display_help;
	if (!input.source_path.length)
		input.source_path = resource_source_path();
	if (!already_help && !input.source_path.length) {
		log_errorf(HASH_RESOURCE, ERROR_INVALID_VALUE, STRING_CONST("No source path given"));
		input.display_help = true;
	}
	if (!already_help && !array_size(input.input_files)) {
		log_errorf(HASH_RESOURCE, ERROR_INVALID_VALUE, STRING_CONST("No input files given"));
		input.display_help = true;
	}

	return input;
}
Esempio n. 10
0
DECLARE_TEST(exception, error) {
    error_handler_fn handler;
    int ret;

    error();
    EXPECT_EQ(error(), ERROR_NONE);

    error_report(ERRORLEVEL_ERROR, ERROR_NONE);
    EXPECT_EQ(error(), ERROR_NONE);

    error_report(ERRORLEVEL_ERROR, ERROR_EXCEPTION);
    EXPECT_EQ(error(), ERROR_EXCEPTION);

    handler = error_handler();
    error_set_handler(_error_handler_test);

    ret = error_report(ERRORLEVEL_WARNING, ERROR_INVALID_VALUE);
    EXPECT_EQ(error(), ERROR_INVALID_VALUE);
    EXPECT_EQ(ret, 2);
    EXPECT_EQ(_error_level_test, ERRORLEVEL_WARNING);
    EXPECT_EQ(_error_test, ERROR_INVALID_VALUE);
    EXPECT_EQ(error_handler(), _error_handler_test);

    error_set_handler(handler);

    {
#if BUILD_ENABLE_ERROR_CONTEXT
        const char context_data[] = "another message";
#endif
        char context_buffer[512];
        string_t contextstr;
        error_context_clear();
        error_context_push(STRING_CONST("test context"), STRING_CONST("some message"));
        error_context_push(STRING_CONST("foo bar"), 0, 0);
        error_context_pop();
        error_context_pop();
        error_context_pop();
        error_context_push(STRING_CONST("test context"), STRING_CONST(context_data));

#if BUILD_ENABLE_ERROR_CONTEXT
        EXPECT_NE(error_context(), 0);
        EXPECT_EQ(error_context()->depth, 1);
        EXPECT_CONSTSTRINGEQ(error_context()->frame[0].name, string_const(STRING_CONST("test context")));
        EXPECT_EQ(error_context()->frame[0].data.str, context_data);
        EXPECT_EQ(error_context()->frame[0].data.length, sizeof(context_data) - 1);
#endif

        contextstr = error_context_buffer(context_buffer, 512);
#if BUILD_ENABLE_ERROR_CONTEXT
        EXPECT_NE_MSGFORMAT(string_find_string(STRING_ARGS(contextstr), STRING_CONST("test context"), 0),
                            STRING_NPOS, "context name 'test context' not found in buffer: %s", context_buffer);
        EXPECT_NE_MSGFORMAT(string_find_string(STRING_ARGS(contextstr), STRING_CONST(context_data), 0),
                            STRING_NPOS, "context data '%s' not found in buffer: %s", context_data, context_buffer);
#else
        EXPECT_EQ(contextstr.length, 0);
#endif

        error_context_clear();
        contextstr = error_context_buffer(context_buffer, 512);
#if BUILD_ENABLE_ERROR_CONTEXT
        EXPECT_STRINGEQ(contextstr, string_empty());
#endif
    }

    return 0;
}
Esempio n. 11
0
int
hashify_process_files(string_t* files, bool check_only) {
	int result = HASHIFY_RESULT_OK;
	hashify_string_t* history = 0;
	size_t ifile, files_size;
	for (ifile = 0, files_size = array_size(files); (result == HASHIFY_RESULT_OK) &&
	        (ifile < files_size); ++ifile) {
		string_t input_filename;
		string_t output_filename;
		string_const_t base_filename;

		stream_t* input_file;
		stream_t* output_file;

		input_filename = string_allocate(0, BUILD_MAX_PATHLEN);
		input_filename = string_copy(input_filename.str, BUILD_MAX_PATHLEN, STRING_ARGS(files[ifile]));
		input_filename = path_clean(STRING_ARGS(input_filename), BUILD_MAX_PATHLEN);
		error_context_push(STRING_CONST("parsing file"), STRING_ARGS(input_filename));

		base_filename = path_base_file_name_with_directory(STRING_ARGS(input_filename));
		output_filename = string_allocate_format(STRING_CONST("%.*s.h"), STRING_FORMAT(base_filename));

		log_infof(0, STRING_CONST("Hashifying %.*s -> %.*s"), STRING_FORMAT(input_filename),
		          STRING_FORMAT(output_filename));

		input_file  = stream_open(STRING_ARGS(input_filename), STREAM_IN);

		//If only validating, open the final output file. If generating, make a memory buffer as intermediate storage
		if (check_only)
			output_file = stream_open(STRING_ARGS(output_filename), STREAM_IN);
		else
			output_file = buffer_stream_allocate(memory_allocate(0, 65536, 0, MEMORY_PERSISTENT),
			                                     STREAM_IN | STREAM_OUT, 0, 65536, true, true);

		if (!input_file) {
			log_warnf(0, WARNING_INVALID_VALUE, STRING_CONST("Unable to open input file: %.*s"),
			          STRING_FORMAT(input_filename));
			result = HASHIFY_RESULT_MISSING_INPUT_FILE;
		}
		else if (!output_file) {
			log_warnf(0, WARNING_INVALID_VALUE, STRING_CONST("Unable to open output file: %.*s"),
			          STRING_FORMAT(output_filename));
			result = HASHIFY_RESULT_MISSING_OUTPUT_FILE;
		}

		if (input_file && output_file) {
			result = hashify_process_file(input_file, output_file, output_filename, check_only, &history);
			if ((result == HASHIFY_RESULT_OK) && !check_only)
				result = hashify_write_file(output_file, output_filename);
		}

		stream_deallocate(input_file);
		stream_deallocate(output_file);

		error_context_pop();

		string_deallocate(input_filename.str);
		string_deallocate(output_filename.str);
	}

	if ((result == HASHIFY_RESULT_OK) && (files_size > 0)) {
		if (check_only)
			log_info(0, STRING_CONST("All hashes validated"));
		else
			log_info(0, STRING_CONST("All hashes generated"));
	}

	array_deallocate(history);

	return result;
}
Esempio n. 12
0
int hashify_process_files( const char* const* files, bool check_only )
{
	int result = HASHIFY_RESULT_OK;
	hashify_string_t* history = 0;
	unsigned int ifile, files_size;
	for( ifile = 0, files_size = array_size( files ); ( result == HASHIFY_RESULT_OK ) && ( ifile < files_size ); ++ifile )
	{
		char* input_filename;
		char* output_filename;

		stream_t* input_file;
		stream_t* output_file;

		input_filename = path_clean( string_clone( files[ifile] ), path_is_absolute( files[ifile] ) );
		error_context_push( "parsing file", input_filename );

		output_filename = string_append( path_base_file_name_with_path( input_filename ), ".h" );

		log_infof( "Hashifying %s -> %s", input_filename, output_filename );

		input_file  = stream_open( input_filename, STREAM_IN );

		//If only validating, open the final output file. If generating, make a memory buffer as intermediate storage
		if( check_only )
			output_file = stream_open( output_filename, STREAM_IN );
		else
			output_file = buffer_stream_allocate( memory_allocate( 65536, 0, MEMORY_PERSISTENT ), STREAM_IN | STREAM_OUT, 0, 65536, true, true );

		if( !input_file )
		{
			log_warnf( WARNING_BAD_DATA, "Unable to open input file: %s", input_filename );
			result = HASHIFY_RESULT_MISSING_INPUT_FILE;
		}
		else if( !output_file )
		{
			log_warnf( WARNING_BAD_DATA, "Unable to open output file: %s", output_filename );
			result = HASHIFY_RESULT_MISSING_OUTPUT_FILE;
		}
		
		if( input_file && output_file )
		{
			result = hashify_process_file( input_file, output_file, check_only, &history );
			if( ( result == HASHIFY_RESULT_OK ) && !check_only )
				result = hashify_write_file( output_file, output_filename );
		}

		stream_deallocate( input_file );
		stream_deallocate( output_file );

		error_context_pop();
		
		string_deallocate( input_filename );
		string_deallocate( output_filename );
	}

	if( ( result == HASHIFY_RESULT_OK ) && ( files_size > 0 ) )
	{
		if( check_only )
			log_infof( "All hashes validated" );
		else
			log_infof( "All hashes generated" );
	}

	array_deallocate( history );

	return result;
}
Esempio n. 13
0
object_t library_load( const char* name )
{
	library_t* library;
	hash_t namehash;
	unsigned int i, size;
	uint64_t id;
#if FOUNDATION_PLATFORM_WINDOWS
	char* dllname;
	HANDLE dll;
#endif

	//Locate already loaded library
	library = 0;
	namehash = string_hash( name );
	for( i = 0, size = objectmap_size( _library_map ); i < size; ++i )
	{
		library = objectmap_raw_lookup( _library_map, i );
		if( library && ( library->namehash == namehash ) )
		{
			FOUNDATION_ASSERT( string_equal( library->name, name ) );
			atomic_incr32( &library->ref );
			return library->id;
		}
	}
	
	error_context_push( "loading library", name );

	//Try loading library
#if FOUNDATION_PLATFORM_WINDOWS

	dllname = string_format( "%s.dll", name );
	dll = LoadLibraryA( dllname );
	if( !dll )
	{
#if FOUNDATION_PLATFORM_ARCH_X86
		string_deallocate( dllname );
		dllname = string_format( "%s32.dll", name );
		dll = LoadLibraryA( dllname );
#elif FOUNDATION_PLATFORM_ARCH_X86_64
		string_deallocate( dllname );
		dllname = string_format( "%s64.dll", name );
		dll = LoadLibraryA( dllname );
#endif
	}
	string_deallocate( dllname );
	if( !dll )
	{
		log_warnf( 0, WARNING_SUSPICIOUS, "Unable to load DLL '%s': %s", name, system_error_message( 0 ) );
		error_context_pop();
		return 0;
	}

#elif FOUNDATION_PLATFORM_POSIX

#  if FOUNDATION_PLATFORM_APPLE
	char* libname = string_format( "lib%s.dylib", name );
#  else
	char* libname = string_format( "lib%s.so", name );
#  endif
	void* lib = dlopen( libname, RTLD_LAZY );
	string_deallocate( libname );
#if FOUNDATION_PLATFORM_ANDROID
	if( !lib )
	{
		libname = string_format( "%s/lib%s.so", environment_executable_directory(), name );
		lib = dlopen( libname, RTLD_LAZY );
		string_deallocate( libname );
	}
#endif
	if( !lib )
	{
		log_warnf( 0, WARNING_SUSPICIOUS, "Unable to load dynamic library '%s': %s", name, dlerror() );
		error_context_pop();
		return 0;
	}

#else
	
	log_errorf( 0, ERROR_NOT_IMPLEMENTED, "Dynamic library loading not implemented for this platform: %s", name );
	error_context_pop();
	return 0;

#endif

	id = objectmap_reserve( _library_map );
	if( !id )
	{
#if FOUNDATION_PLATFORM_WINDOWS
		FreeLibrary( dll );
#elif FOUNDATION_PLATFORM_POSIX
		dlclose( lib );
#endif
		log_errorf( 0, ERROR_OUT_OF_MEMORY, "Unable to allocate new library '%s', map full", name );	
		error_context_pop();
		return 0;
	}
	library = memory_allocate_zero( sizeof( library_t ), 0, MEMORY_PERSISTENT );
	library->ref = 1;
	library->id = id;
	library->namehash = string_hash( name );
	string_copy( library->name, name, 32 );
#if FOUNDATION_PLATFORM_WINDOWS
	library->dll = dll;
#elif FOUNDATION_PLATFORM_POSIX
	library->lib = lib;
#endif
	objectmap_set( _library_map, id, library );

	error_context_pop();
	
	return library->id;
}