Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

456-fix-error-codes #475

Open
wants to merge 8 commits into
base: latest
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ CMakeSettings.json

# Ignore files generated by CMake
build/*
include/private/.idea/

# Ignore files generated by VS
out/*
Expand Down
8 changes: 8 additions & 0 deletions include/private/.idea/.gitignore
Ankaa19 marked this conversation as resolved.
Show resolved Hide resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 20 additions & 4 deletions src/facility.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <stumpless/facility.h>
#include "private/facility.h"
#include "private/strhelper.h"
#include "private/error.h"


static char *facility_enum_to_string[] = {
STUMPLESS_FOREACH_FACILITY( GENERATE_STRING )
Expand All @@ -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_"
Expand All @@ -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 */
Expand All @@ -77,4 +93,4 @@ get_facility( int prival ) {
int
facility_is_invalid( int facility ) {
return facility < 0 || facility > ( 23 << 3 ) || facility % 8 != 0;
}
}
79 changes: 79 additions & 0 deletions test/function/facility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,83 @@ namespace {
EXPECT_EQ( result, -1 );
}


TEST(GetFacilityString, InvalidFacility) {
const char *result;
stumpless_facility invalid_facility = 999; // Value out of valid range

EXPECT_THROW({
result = stumpless_get_facility_string(invalid_facility);
}, std::runtime_error);

// Ensure fallback string is still returned
result = stumpless_get_facility_string(invalid_facility);
EXPECT_STREQ(result, "NO_SUCH_FACILITY");
}

TEST(GetFacilityEnum, NullFacilityString) {
EXPECT_THROW({
stumpless_get_facility_enum(NULL);
}, std::invalid_argument);
}

TEST(GetFacilityEnumFromBuffer, NullFacilityBuffer) {
EXPECT_THROW({
stumpless_get_facility_enum_from_buffer(NULL, 0);
}, std::invalid_argument);
}

TEST(GetFacilityEnum, InvalidFacilityString) {
int result;

EXPECT_THROW({
result = stumpless_get_facility_enum("INVALID");
}, std::runtime_error);

// Ensure fallback value is still returned (-1 for invalid facility)
result = stumpless_get_facility_enum("INVALID");
EXPECT_EQ(result, -1);
}

TEST(GetFacilityEnumFromBuffer, InvalidFacilityBuffer) {
int result;

EXPECT_THROW({
result = stumpless_get_facility_enum_from_buffer("INVALID", strlen("INVALID"));
}, std::runtime_error);

// Ensure fallback value is still returned
result = stumpless_get_facility_enum_from_buffer("INVALID", strlen("INVALID"));
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);
}
}

}
Loading