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

Increase config test coverage #171

Merged
merged 18 commits into from
Apr 27, 2024
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ jobs:
run: brew install popt just cmocka
- name: Run unit tests
shell: 'script -q /dev/null bash -e {0}' # Ensure stdin is attached to tty for unit tests
run: just test-release
run: just test
2 changes: 1 addition & 1 deletion cmake/add_cmocka_test.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function(add_cmocka_test unit)

target_include_directories(${test_target} PRIVATE ${CMAKE_SOURCE_DIR}/src)

target_compile_options(${test_target} PRIVATE ${CMOCKA_CFLAGS} PRIVATE ${POPT_CFLAGS})
target_compile_options(${test_target} PRIVATE ${CMOCKA_CFLAGS} PRIVATE ${POPT_CFLAGS} PRIVATE -O0)
target_compile_definitions(${test_target} PRIVATE UNIT_TESTING)

add_test(NAME ${test_target} COMMAND ${test_target})
Expand Down
1 change: 1 addition & 0 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ flog_error_map[] = {
[FLOG_ERROR_MSG] = "message string required",
[FLOG_ERROR_SUBSYS] = "category option requires subsystem option to be set",
[FLOG_ERROR_OPTS] = "invalid options",
[FLOG_ERROR_FILE] = "file path too long",
};

const char *
Expand Down
1 change: 1 addition & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ typedef enum FlogErrorData {
FLOG_ERROR_MSG,
FLOG_ERROR_OPTS,
FLOG_ERROR_SUBSYS,
FLOG_ERROR_FILE
} FlogError;

/*! \brief Print usage information to stdout stream. */
Expand Down
56 changes: 32 additions & 24 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ extern void mock_assert(const int result, const char* const expression,
mock_assert((int)(expression), #expression, __FILE__, __LINE__);
#endif

#define SUBSYSTEM_LEN 257
#define CATEGORY_LEN 257
#define MESSAGE_LEN 8193
const int subsystem_len = 257;
const int category_len = 257;
const int message_len = 8193;

FlogConfigLevel flog_config_parse_level(const char *str);

Expand All @@ -60,10 +60,10 @@ static struct poptOption options[] = {
struct FlogConfigData {
FlogConfigLevel level;
FlogConfigMessageType message_type;
char subsystem[SUBSYSTEM_LEN];
char category[CATEGORY_LEN];
char subsystem[subsystem_len];
char category[category_len];
char output_file[PATH_MAX];
char message[MESSAGE_LEN];
char message[message_len];
bool version;
bool help;
};
Expand All @@ -83,7 +83,7 @@ flog_config_new(int argc, char *argv[], FlogError *error) {
}

flog_config_set_level(config, LVL_DEFAULT);
flog_config_set_message_type(config, Public);
flog_config_set_message_type(config, MSG_PUBLIC);
flog_config_set_version_flag(config, false);
flog_config_set_help_flag(config, false);

Expand All @@ -104,7 +104,12 @@ flog_config_new(int argc, char *argv[], FlogError *error) {
poptFreeContext(context);
return config;
case 'a':
flog_config_set_output_file(config, option_argument);
*error = flog_config_set_output_file(config, option_argument);
if (*error != FLOG_ERROR_NONE) {
flog_config_free(config);
poptFreeContext(context);
return NULL;
}
break;
case 'l':
flog_config_set_level(config, flog_config_parse_level(option_argument));
Expand All @@ -117,13 +122,12 @@ flog_config_new(int argc, char *argv[], FlogError *error) {
break;
case 's':
flog_config_set_subsystem(config, option_argument);

break;
case 'c':
flog_config_set_category(config, option_argument);
break;
case 'p':
flog_config_set_message_type(config, Private);
flog_config_set_message_type(config, MSG_PRIVATE);
break;
}

Expand Down Expand Up @@ -187,8 +191,8 @@ flog_config_set_subsystem(FlogConfig *config, const char *subsystem) {
assert(config != NULL);
assert(subsystem != NULL);

if (strlcpy(config->subsystem, subsystem, SUBSYSTEM_LEN) >= SUBSYSTEM_LEN) {
fprintf(stderr, "%s: subsystem name truncated to %d bytes\n", PROGRAM_NAME, SUBSYSTEM_LEN - 1);
if (strlcpy(config->subsystem, subsystem, subsystem_len) >= subsystem_len) {
fprintf(stderr, "%s: subsystem name truncated to %d bytes\n", PROGRAM_NAME, subsystem_len - 1);
}
}

Expand All @@ -204,8 +208,8 @@ flog_config_set_category(FlogConfig *config, const char *category) {
assert(config != NULL);
assert(category != NULL);

if (strlcpy(config->category, category, CATEGORY_LEN) >= CATEGORY_LEN) {
fprintf(stderr, "%s: category name truncated to %d bytes\n", PROGRAM_NAME, CATEGORY_LEN - 1);
if (strlcpy(config->category, category, category_len) >= category_len) {
fprintf(stderr, "%s: category name truncated to %d bytes\n", PROGRAM_NAME, category_len - 1);
}
}

Expand All @@ -216,15 +220,16 @@ flog_config_get_output_file(const FlogConfig *config) {
return config->output_file;
}

void
FlogError
flog_config_set_output_file(FlogConfig *config, const char *output_file) {
assert(config != NULL);
assert(output_file != NULL);

if (strlcpy(config->output_file, output_file, PATH_MAX) >= PATH_MAX) {
fprintf(stderr, "%s: specify an output file path up to a maximum of %d characters\n", PROGRAM_NAME, PATH_MAX - 1);
exit(EXIT_FAILURE);
return FLOG_ERROR_FILE;
}

return FLOG_ERROR_NONE;
}

FlogConfigLevel
Expand Down Expand Up @@ -274,8 +279,8 @@ flog_config_set_message(FlogConfig *config, const char *message) {
assert(config != NULL);
assert(message != NULL);

if (strlcpy(config->message, message, MESSAGE_LEN) >= MESSAGE_LEN) {
fprintf(stderr, "%s: message string was truncated to %d bytes\n", PROGRAM_NAME, MESSAGE_LEN - 1);
if (strlcpy(config->message, message, message_len) >= message_len) {
fprintf(stderr, "%s: message string was truncated to %d bytes\n", PROGRAM_NAME, message_len - 1);
}
}

Expand All @@ -284,14 +289,16 @@ flog_config_set_message_from_args(FlogConfig *config, const char **args) {
assert(config != NULL);
assert(args != NULL);

config->message[0] = '\0';

bool message_truncated = false;
while (*args != NULL) {
if (strlcat(config->message, *args, MESSAGE_LEN) >= MESSAGE_LEN) {
if (strlcat(config->message, *args, message_len) >= message_len) {
message_truncated = true;
break;
}
if (*(args + 1) != NULL) {
if (strlcat(config->message, " ", MESSAGE_LEN) >= MESSAGE_LEN) {
if (strlcat(config->message, " ", message_len) >= message_len) {
message_truncated = true;
break;
}
Expand All @@ -300,7 +307,7 @@ flog_config_set_message_from_args(FlogConfig *config, const char **args) {
}

if (message_truncated) {
fprintf(stderr, "%s: message was truncated to %d bytes\n", PROGRAM_NAME, MESSAGE_LEN - 1);
fprintf(stderr, "%s: message was truncated to %d bytes\n", PROGRAM_NAME, message_len - 1);
}
}

Expand All @@ -309,8 +316,9 @@ flog_config_set_message_from_stream(FlogConfig *config, FILE *restrict stream) {
assert(config != NULL);
assert(stream != NULL);

if (fread(config->message, sizeof(char), MESSAGE_LEN, stream) >= MESSAGE_LEN) {
fprintf(stderr, "%s: message was truncated to %d bytes\n", PROGRAM_NAME, MESSAGE_LEN - 1);
if (fread(config->message, sizeof(char), message_len, stream) >= message_len) {
fprintf(stderr, "%s: message was truncated to %d bytes\n", PROGRAM_NAME, message_len - 1);
config->message[message_len - 1] = '\0';
}
}

Expand Down
13 changes: 10 additions & 3 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
#include <stdio.h>
#include "common.h"

extern const int subsystem_len;
extern const int category_len;
extern const int message_len;

/*! \brief An enumerated type representing the log level. */
typedef enum FlogConfigLevelData {
LVL_DEFAULT,
Expand All @@ -45,8 +49,8 @@ typedef enum FlogConfigLevelData {

/*! \brief An enumerated type representing the log message type. */
typedef enum FlogConfigMessageTypeData {
Public,
Private,
MSG_PUBLIC,
MSG_PRIVATE
} FlogConfigMessageType;

/*! \struct FlogConfig
Expand Down Expand Up @@ -139,8 +143,11 @@ const char * flog_config_get_output_file(const FlogConfig *config);
*
* \pre \c config is \e not \c NULL
* \pre \c output_file is \e not \c NULL
*
* \return If successful, the FlogError variant FLOG_ERROR_NONE, or FLOG_ERROR_FILE
* if the output_file path exceeds the maximum path limit
*/
void flog_config_set_output_file(FlogConfig *config, const char *output_file);
FlogError flog_config_set_output_file(FlogConfig *config, const char *output_file);

/*! \brief Get the log level value from a FlogConfig object.
*
Expand Down
4 changes: 2 additions & 2 deletions src/flog.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ flog_commit_message(FlogCli *flog) {
assert(flog != NULL);

FlogConfig *config = flog_cli_get_config(flog);
if (flog_config_get_message_type(config) == Public) {
if (flog_config_get_message_type(config) == MSG_PUBLIC) {
flog_commit_public_message(flog);
} else if (flog_config_get_message_type(config) == Private) {
} else if (flog_config_get_message_type(config) == MSG_PRIVATE) {
flog_commit_private_message(flog);
}
}
Expand Down
Loading
Loading