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 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 @@ -10,6 +10,7 @@ docs/html/*
*.swp
.vs/*
.vscode/*
.idea/*
CMakeSettings.json
**/.DS_Store

Expand Down
19 changes: 16 additions & 3 deletions src/facility.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
#include <string.h>
#include <stumpless/facility.h>
#include "private/facility.h"
#include <private/validate.h>
#include "private/strhelper.h"
#include "private/error.h"
#include <stumpless/error.h>


static char *facility_enum_to_string[] = {
STUMPLESS_FOREACH_FACILITY( GENERATE_STRING )
Expand All @@ -31,16 +35,25 @@ 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));
VALIDATE_ARG_NOT_NULL_INT_RETURN(facility_string);

enum stumpless_facility facility = stumpless_get_facility_enum_from_buffer(facility_string, strlen(facility_string));
if (facility == -1) {
raise_invalid_facility(STUMPLESS_INVALID_ENCODING);
}
return facility;
}

enum stumpless_facility
stumpless_get_facility_enum_from_buffer(const char *facility_buffer, size_t facility_buffer_length) {
VALIDATE_ARG_NOT_NULL_INT_RETURN(facility_buffer);

size_t facility_bound;
size_t i;
const int str_offset = 19; // to ommit "STUMPLESS_FACILITY_"
Expand All @@ -63,8 +76,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(STUMPLESS_INVALID_FACILITY);
return -1;
}

/* private functions */
Expand Down
103 changes: 102 additions & 1 deletion 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,7 +61,8 @@ namespace {
STUMPLESS_FOREACH_FACILITY( CHECK_FACILITY_ENUM )
}

TEST( GetFacilityEnum, LowercaseFacility ) {

TEST( GetFacilityEnum, LowercaseFacility ) {
int result;

result = stumpless_get_facility_enum( "user" );
Expand Down Expand Up @@ -109,11 +112,109 @@ namespace {
EXPECT_NO_ERROR;
}


TEST( GetFacilityEnum, NoSuchFacility ) {
int result;

result = stumpless_get_facility_enum( "an_invalid_facility" );
EXPECT_EQ( result, -1 );
EXPECT_ERROR_ID_EQ(STUMPLESS_INVALID_FACILITY);
}


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");
EXPECT_ERROR_ID_EQ(STUMPLESS_INVALID_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, -STUMPLESS_ARGUMENT_EMPTY);
EXPECT_ERROR_ID_EQ(STUMPLESS_ARGUMENT_EMPTY);
}

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);
EXPECT_ERROR_ID_EQ(STUMPLESS_ARGUMENT_EMPTY);
}

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);
EXPECT_ERROR_ID_EQ(STUMPLESS_INVALID_FACILITY);
}

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);
EXPECT_ERROR_ID_EQ(STUMPLESS_INVALID_FACILITY);
}

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);
EXPECT_TRUE(stumpless_has_error());
}
}

TEST(GetFacilityEnumFromBuffer, InvalidFacility) {
const char *invalid_facility = "INVALID";

// Call the function with an invalid facility
int result = stumpless_get_facility_enum_from_buffer(
invalid_facility, strlen(invalid_facility));

// Verify the function returns -1 for invalid input
EXPECT_EQ(result, -1);

// Verify the correct error is set
EXPECT_TRUE(stumpless_has_error());
EXPECT_ERROR_ID_EQ(STUMPLESS_INVALID_FACILITY);
}


}
Loading