diff --git a/include/stumpless/facility.h b/include/stumpless/facility.h index 256269e50..77da68862 100644 --- a/include/stumpless/facility.h +++ b/include/stumpless/facility.h @@ -389,6 +389,10 @@ stumpless_get_facility_string( enum stumpless_facility facility ); * @since release v2.1.0. * * @param facility_string The facility name to get the enum from. + * + * Clears previous error on success. + * + * Raises STUMPLESS_INVALID_FACILITY on invalid input. * * @return The enum integer corresponding to the given facility or -1 if * the string is not a valid facility name. @@ -415,6 +419,10 @@ stumpless_get_facility_enum( const char *facility_string ); * @param facility_string The facility name to get the enum from. * * @param facility_buffer_length The length of the buffer + * + * Clears previous error on success. + * + * Raises STUMPLESS_INVALID_FACILITY on invalid input. * * @return The enum integer corresponding to the given facility or -1 if * the string is not a valid facility name. diff --git a/src/facility.c b/src/facility.c index dff55c6d6..ecb9091af 100644 --- a/src/facility.c +++ b/src/facility.c @@ -21,6 +21,9 @@ #include #include "private/facility.h" #include "private/strhelper.h" +#include "private/error.h" +#include +#include static char *facility_enum_to_string[] = { STUMPLESS_FOREACH_FACILITY( GENERATE_STRING ) @@ -29,42 +32,54 @@ static char *facility_enum_to_string[] = { const char * stumpless_get_facility_string( enum stumpless_facility facility ) { if ( !facility_is_invalid( facility ) ) { + clear_error(); return facility_enum_to_string[facility >> 3]; } - return "NO_SUCH_FACILITY"; -} -enum stumpless_facility -stumpless_get_facility_enum( const char *facility_string ) { - return stumpless_get_facility_enum_from_buffer(facility_string, strlen(facility_string)); + raise_error( STUMPLESS_INVALID_FACILITY, "invalid facility enum" , 0, NULL); + return "NO_SUCH_FACILITY"; } enum stumpless_facility -stumpless_get_facility_enum_from_buffer(const char *facility_buffer, size_t facility_buffer_length) { - size_t facility_bound; - size_t i; - const int str_offset = 19; // to ommit "STUMPLESS_FACILITY_" +stumpless_get_facility_enum_from_buffer( const char *facility_buffer, size_t facility_buffer_length ) { + size_t facility_bound; + size_t i; + const int str_offset = 19; // to ommit "STUMPLESS_FACILITY_" - facility_bound = sizeof( facility_enum_to_string ) / - sizeof( facility_enum_to_string[0] ); + facility_bound = sizeof( facility_enum_to_string ) / + sizeof( facility_enum_to_string[0] ); - for( i = 0; i < facility_bound; i++ ) { - if( strncasecmp_custom( facility_buffer, facility_enum_to_string[i] + str_offset, facility_buffer_length ) == 0 ) { - return i << 3; + for( i = 0; i < facility_bound; i++ ) { + if( strncasecmp_custom( facility_buffer, facility_enum_to_string[i] + str_offset, facility_buffer_length ) == 0 ) { + clear_error(); + return i << 3; + } } - } - // exeption, for 'security' return 'auth' enum value if( strncasecmp_custom( facility_buffer, "SECURITY", facility_buffer_length ) == 0 ) { - return STUMPLESS_FACILITY_AUTH_VALUE; - } + clear_error(); + return STUMPLESS_FACILITY_AUTH_VALUE; + } - // exeption, for 'authpriv' not presented in enum list if( strncasecmp_custom( facility_buffer, "AUTHPRIV", facility_buffer_length ) == 0 ) { - return STUMPLESS_FACILITY_AUTH2_VALUE; - } + clear_error(); + return STUMPLESS_FACILITY_AUTH2_VALUE; + } + + raise_error( STUMPLESS_INVALID_FACILITY, "invalid facility string", 0, NULL ); + return -1; +} + +enum stumpless_facility +stumpless_get_facility_enum( const char *facility_string ) { + enum stumpless_facility result = stumpless_get_facility_enum_from_buffer( + facility_string, strlen( facility_string ) ); + + if( result != -1 ) { + clear_error(); + } - return -1; + return result; } /* private functions */ diff --git a/test/function/facility.cpp b/test/function/facility.cpp index b3292f1c2..9ef8ebc7f 100644 --- a/test/function/facility.cpp +++ b/test/function/facility.cpp @@ -19,6 +19,7 @@ #include #include #include "test/helper/assert.hpp" +#include namespace { @@ -116,4 +117,26 @@ namespace { EXPECT_EQ( result, -1 ); } + TEST( FacilityErrorHandling, ClearsPreviousErrorOnSuccess ) { + stumpless_version_to_string(NULL); + EXPECT_ERROR_ID_EQ(STUMPLESS_ARGUMENT_EMPTY); + + const char* result = stumpless_get_facility_string(STUMPLESS_FACILITY_USER); + EXPECT_NO_ERROR; + EXPECT_STREQ(result, "STUMPLESS_FACILITY_USER"); + } + + TEST( FacilityErrorHandling, InvalidFacilityEnumRaisesError ) { + const char* result = stumpless_get_facility_string(static_cast(200)); + EXPECT_STREQ(result, "NO_SUCH_FACILITY"); + EXPECT_ERROR_ID_EQ(STUMPLESS_INVALID_FACILITY); + } + + TEST( FacilityErrorHandling, InvalidFacilityStringRaisesError ) { + int result = stumpless_get_facility_enum("not_a_real_facility"); + EXPECT_EQ(result, -1); + EXPECT_ERROR_ID_EQ(STUMPLESS_INVALID_FACILITY); + } + + }