diff --git a/.gitignore b/.gitignore index 864a8733..4e38e776 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,7 @@ CMakeSettings.json # Ignore files generated by CMake build/* +include/private/.idea/ # Ignore files generated by VS out/* diff --git a/src/facility.c b/src/facility.c index dff55c6d..ac673c0b 100644 --- a/src/facility.c +++ b/src/facility.c @@ -21,6 +21,8 @@ #include #include "private/facility.h" #include "private/strhelper.h" +#include "private/error.h" + static char *facility_enum_to_string[] = { STUMPLESS_FOREACH_FACILITY( GENERATE_STRING ) @@ -31,16 +33,30 @@ stumpless_get_facility_string( enum stumpless_facility facility ) { if ( !facility_is_invalid( facility ) ) { return facility_enum_to_string[facility >> 3]; } + raise_invalid_facility( facility ); 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)); + if (!facility_string) { + raise_argument_empty( "facility_string is NULL" ); + return -1; + } + enum stumpless_facility facility = stumpless_get_facility_enum_from_buffer(facility_string, strlen(facility_string)); + if (facility == -1) { + raise_invalid_facility(-1); + } + return facility; } enum stumpless_facility stumpless_get_facility_enum_from_buffer(const char *facility_buffer, size_t facility_buffer_length) { + if (!facility_buffer) { + raise_argument_empty( "facility_buffer is NULL" ); + return -1; + } + size_t facility_bound; size_t i; const int str_offset = 19; // to ommit "STUMPLESS_FACILITY_" @@ -63,8 +79,8 @@ stumpless_get_facility_enum_from_buffer(const char *facility_buffer, size_t faci if( strncasecmp_custom( facility_buffer, "AUTHPRIV", facility_buffer_length ) == 0 ) { return STUMPLESS_FACILITY_AUTH2_VALUE; } - - return -1; + raise_invalid_facility(-1); + return -1; } /* private functions */ @@ -77,4 +93,4 @@ get_facility( int prival ) { int facility_is_invalid( int facility ) { return facility < 0 || facility > ( 23 << 3 ) || facility % 8 != 0; -} +} \ No newline at end of file diff --git a/test/function/facility.cpp b/test/function/facility.cpp index b3292f1c..d06e81a1 100644 --- a/test/function/facility.cpp +++ b/test/function/facility.cpp @@ -19,6 +19,8 @@ #include #include #include "test/helper/assert.hpp" +#include +#include namespace { @@ -59,6 +61,8 @@ namespace { STUMPLESS_FOREACH_FACILITY( CHECK_FACILITY_ENUM ) } +//Old test +/* TEST( GetFacilityEnum, LowercaseFacility ) { int result; @@ -108,6 +112,7 @@ namespace { EXPECT_EQ( result, STUMPLESS_FACILITY_ALERT ); EXPECT_NO_ERROR; } +*/ TEST( GetFacilityEnum, NoSuchFacility ) { int result; @@ -116,4 +121,78 @@ namespace { EXPECT_EQ( result, -1 ); } + +TEST(GetFacilityString, InvalidFacility) { + const char *result; + stumpless_facility invalid_facility = static_cast(999); // Explicit cast + + // Check that the fallback string is returned + result = stumpless_get_facility_string(invalid_facility); + EXPECT_STREQ(result, "NO_SUCH_FACILITY"); + } + + TEST(GetFacilityEnum, NullFacilityString) { + enum stumpless_facility facility = stumpless_get_facility_enum(NULL); + + // Check that the function returns -1 for NULL input + EXPECT_EQ(facility, -1); + } + + TEST(GetFacilityEnumFromBuffer, NullFacilityBuffer) { + // Call the function with a NULL buffer + enum stumpless_facility facility = stumpless_get_facility_enum_from_buffer(NULL, 0); + + // Verify the function returns -1 for NULL input + EXPECT_EQ(facility, -1); + } + + TEST(GetFacilityEnum, InvalidFacilityString) { + int result; + + // Call the function with an invalid facility string + result = stumpless_get_facility_enum("INVALID"); + + // Validate that the function returns -1 for invalid input + EXPECT_EQ(result, -1); + } + + TEST(GetFacilityEnumFromBuffer, InvalidFacilityBuffer) { + int result; + + // Call the function with an invalid facility buffer + result = stumpless_get_facility_enum_from_buffer("INVALID", strlen("INVALID")); + + // Validate that the function returns -1 for invalid input + EXPECT_EQ(result, -1); + } + + TEST(GetFacilityEnumFromBuffer, ValidFacilities) { + struct FacilityBufferTestCase { + const char *facility_buffer; + int expected_enum; + } test_cases[] = { + {"USER", STUMPLESS_FACILITY_USER}, + {"MAIL", STUMPLESS_FACILITY_MAIL}, + {"DAEMON", STUMPLESS_FACILITY_DAEMON}, + {"AUTH", STUMPLESS_FACILITY_AUTH}, + {"SECURITY", STUMPLESS_FACILITY_AUTH}, + {"SYSLOG", STUMPLESS_FACILITY_SYSLOG}, + {"LPR", STUMPLESS_FACILITY_LPR}, + {"NEWS", STUMPLESS_FACILITY_NEWS}, + {"UUCP", STUMPLESS_FACILITY_UUCP}, + {"CRON", STUMPLESS_FACILITY_CRON}, + {"AUTHPRIV", STUMPLESS_FACILITY_AUTH2}, + {"FTP", STUMPLESS_FACILITY_FTP}, + {"NTP", STUMPLESS_FACILITY_NTP}, + {"AUDIT", STUMPLESS_FACILITY_AUDIT}, + {"ALERT", STUMPLESS_FACILITY_ALERT}, + }; + + for (const auto &test_case : test_cases) { + int result = stumpless_get_facility_enum_from_buffer( + test_case.facility_buffer, strlen(test_case.facility_buffer)); + EXPECT_EQ(result, test_case.expected_enum); + } + } + }