Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/stumpless/facility.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
59 changes: 37 additions & 22 deletions src/facility.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#include <stumpless/facility.h>
#include "private/facility.h"
#include "private/strhelper.h"
#include "private/error.h"
#include <stddef.h>
#include <stumpless/error.h>

static char *facility_enum_to_string[] = {
STUMPLESS_FOREACH_FACILITY( GENERATE_STRING )
Expand All @@ -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 */
Expand Down
23 changes: 23 additions & 0 deletions test/function/facility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <gtest/gtest.h>
#include <stumpless.h>
#include "test/helper/assert.hpp"
#include <cstddef>

namespace {

Expand Down Expand Up @@ -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<stumpless_facility>(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);
}


}
Loading