Skip to content

Commit a050775

Browse files
committed
feat: Add facility function tests and fix missing test targets (without white space changes and fixed header)
1 parent 8ef62c2 commit a050775

File tree

3 files changed

+65
-22
lines changed

3 files changed

+65
-22
lines changed

include/stumpless/facility.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,9 @@ stumpless_get_facility_string( enum stumpless_facility facility );
389389
* @since release v2.1.0.
390390
*
391391
* @param facility_string The facility name to get the enum from.
392+
*
393+
* @error Clears previous error on success.
394+
* @error Raises STUMPLESS_INVALID_FACILITY on invalid input.
392395
*
393396
* @return The enum integer corresponding to the given facility or -1 if
394397
* the string is not a valid facility name.
@@ -415,6 +418,9 @@ stumpless_get_facility_enum( const char *facility_string );
415418
* @param facility_string The facility name to get the enum from.
416419
*
417420
* @param facility_buffer_length The length of the buffer
421+
*
422+
* @error Clears previous error on success.
423+
* @error Raises STUMPLESS_INVALID_FACILITY on invalid input.
418424
*
419425
* @return The enum integer corresponding to the given facility or -1 if
420426
* the string is not a valid facility name.

src/facility.c

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <stumpless/facility.h>
2222
#include "private/facility.h"
2323
#include "private/strhelper.h"
24+
#include "private/error.h"
2425

2526
static char *facility_enum_to_string[] = {
2627
STUMPLESS_FOREACH_FACILITY( GENERATE_STRING )
@@ -29,44 +30,58 @@ static char *facility_enum_to_string[] = {
2930
const char *
3031
stumpless_get_facility_string( enum stumpless_facility facility ) {
3132
if ( !facility_is_invalid( facility ) ) {
33+
clear_error();
3234
return facility_enum_to_string[facility >> 3];
3335
}
34-
return "NO_SUCH_FACILITY";
35-
}
3636

37-
enum stumpless_facility
38-
stumpless_get_facility_enum( const char *facility_string ) {
39-
return stumpless_get_facility_enum_from_buffer(facility_string, strlen(facility_string));
37+
raise_error( STUMPLESS_INVALID_FACILITY, "invalid facility enum" , 0, NULL);
38+
return "NO_SUCH_FACILITY";
4039
}
4140

4241
enum stumpless_facility
43-
stumpless_get_facility_enum_from_buffer(const char *facility_buffer, size_t facility_buffer_length) {
44-
size_t facility_bound;
45-
size_t i;
46-
const int str_offset = 19; // to ommit "STUMPLESS_FACILITY_"
42+
stumpless_get_facility_enum_from_buffer( const char *facility_buffer, size_t facility_buffer_length ) {
43+
size_t facility_bound;
44+
size_t i;
45+
const int str_offset = 19; // to ommit "STUMPLESS_FACILITY_"
4746

48-
facility_bound = sizeof( facility_enum_to_string ) /
49-
sizeof( facility_enum_to_string[0] );
47+
facility_bound = sizeof( facility_enum_to_string ) /
48+
sizeof( facility_enum_to_string[0] );
5049

51-
for( i = 0; i < facility_bound; i++ ) {
52-
if( strncasecmp_custom( facility_buffer, facility_enum_to_string[i] + str_offset, facility_buffer_length ) == 0 ) {
53-
return i << 3;
50+
for( i = 0; i < facility_bound; i++ ) {
51+
if( strncasecmp_custom( facility_buffer, facility_enum_to_string[i] + str_offset, facility_buffer_length ) == 0 ) {
52+
clear_error();
53+
return i << 3;
54+
}
5455
}
55-
}
5656

57-
// exeption, for 'security' return 'auth' enum value
5857
if( strncasecmp_custom( facility_buffer, "SECURITY", facility_buffer_length ) == 0 ) {
59-
return STUMPLESS_FACILITY_AUTH_VALUE;
60-
}
58+
clear_error();
59+
return STUMPLESS_FACILITY_AUTH_VALUE;
60+
}
6161

62-
// exeption, for 'authpriv' not presented in enum list
6362
if( strncasecmp_custom( facility_buffer, "AUTHPRIV", facility_buffer_length ) == 0 ) {
64-
return STUMPLESS_FACILITY_AUTH2_VALUE;
65-
}
63+
clear_error();
64+
return STUMPLESS_FACILITY_AUTH2_VALUE;
65+
}
66+
67+
raise_error( STUMPLESS_INVALID_FACILITY, "invalid facility string", 0, NULL );
68+
return -1;
69+
}
70+
71+
enum stumpless_facility
72+
stumpless_get_facility_enum( const char *facility_string ) {
73+
enum stumpless_facility result = stumpless_get_facility_enum_from_buffer(
74+
facility_string, strlen( facility_string ) );
6675

67-
return -1;
76+
if( result != -1 ) {
77+
clear_error();
78+
}
79+
80+
return result;
6881
}
6982

83+
84+
7085
/* private functions */
7186

7287
int

test/function/facility.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,26 @@ namespace {
116116
EXPECT_EQ( result, -1 );
117117
}
118118

119+
TEST( FacilityErrorHandling, ClearsPreviousErrorOnSuccess ) {
120+
stumpless_version_to_string(NULL);
121+
EXPECT_ERROR_ID_EQ(STUMPLESS_ARGUMENT_EMPTY);
122+
123+
const char* result = stumpless_get_facility_string(STUMPLESS_FACILITY_USER);
124+
EXPECT_NO_ERROR;
125+
EXPECT_STREQ(result, "STUMPLESS_FACILITY_USER");
126+
}
127+
128+
TEST( FacilityErrorHandling, InvalidFacilityEnumRaisesError ) {
129+
const char* result = stumpless_get_facility_string(static_cast<stumpless_facility>(200));
130+
EXPECT_STREQ(result, "NO_SUCH_FACILITY");
131+
EXPECT_ERROR_ID_EQ(STUMPLESS_INVALID_FACILITY);
132+
}
133+
134+
TEST( FacilityErrorHandling, InvalidFacilityStringRaisesError ) {
135+
int result = stumpless_get_facility_enum("not_a_real_facility");
136+
EXPECT_EQ(result, -1);
137+
EXPECT_ERROR_ID_EQ(STUMPLESS_INVALID_FACILITY);
138+
}
139+
140+
119141
}

0 commit comments

Comments
 (0)