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 19 commits into
base: latest
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 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/
Ankaa19 marked this conversation as resolved.
Show resolved Hide resolved

# Ignore files generated by VS
out/*
Expand Down
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) {
Ankaa19 marked this conversation as resolved.
Show resolved Hide resolved
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) {
Ankaa19 marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -19,6 +19,8 @@
#include <gtest/gtest.h>
#include <stumpless.h>
#include "test/helper/assert.hpp"
#include <stddef.h>
#include <string.h>

namespace {

Expand Down Expand Up @@ -59,6 +61,8 @@ namespace {
STUMPLESS_FOREACH_FACILITY( CHECK_FACILITY_ENUM )
}

//Old test
/*
Ankaa19 marked this conversation as resolved.
Show resolved Hide resolved
TEST( GetFacilityEnum, LowercaseFacility ) {
int result;

Expand Down Expand Up @@ -108,6 +112,7 @@ namespace {
EXPECT_EQ( result, STUMPLESS_FACILITY_ALERT );
EXPECT_NO_ERROR;
}
*/

TEST( GetFacilityEnum, NoSuchFacility ) {
int result;
Expand All @@ -116,4 +121,78 @@ namespace {
EXPECT_EQ( result, -1 );
}


TEST(GetFacilityString, InvalidFacility) {
Ankaa19 marked this conversation as resolved.
Show resolved Hide resolved
const char *result;
stumpless_facility invalid_facility = static_cast<stumpless_facility>(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);
}
}

}
Loading