Skip to content

Commit a207f1c

Browse files
authored
Merge pull request #171 from marcransome/config-test-coverage
Increase config test coverage
2 parents 5b6c5f2 + 44eee4f commit a207f1c

File tree

8 files changed

+1029
-96
lines changed

8 files changed

+1029
-96
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ jobs:
3030
run: brew install popt just cmocka
3131
- name: Run unit tests
3232
shell: 'script -q /dev/null bash -e {0}' # Ensure stdin is attached to tty for unit tests
33-
run: just test-release
33+
run: just test

cmake/add_cmocka_test.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function(add_cmocka_test unit)
88

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

11-
target_compile_options(${test_target} PRIVATE ${CMOCKA_CFLAGS} PRIVATE ${POPT_CFLAGS})
11+
target_compile_options(${test_target} PRIVATE ${CMOCKA_CFLAGS} PRIVATE ${POPT_CFLAGS} PRIVATE -O0)
1212
target_compile_definitions(${test_target} PRIVATE UNIT_TESTING)
1313

1414
add_test(NAME ${test_target} COMMAND ${test_target})

src/common.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ flog_error_map[] = {
3232
[FLOG_ERROR_MSG] = "message string required",
3333
[FLOG_ERROR_SUBSYS] = "category option requires subsystem option to be set",
3434
[FLOG_ERROR_OPTS] = "invalid options",
35+
[FLOG_ERROR_FILE] = "file path too long",
3536
};
3637

3738
const char *

src/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ typedef enum FlogErrorData {
4040
FLOG_ERROR_MSG,
4141
FLOG_ERROR_OPTS,
4242
FLOG_ERROR_SUBSYS,
43+
FLOG_ERROR_FILE
4344
} FlogError;
4445

4546
/*! \brief Print usage information to stdout stream. */

src/config.c

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ extern void mock_assert(const int result, const char* const expression,
4040
mock_assert((int)(expression), #expression, __FILE__, __LINE__);
4141
#endif
4242

43-
#define SUBSYSTEM_LEN 257
44-
#define CATEGORY_LEN 257
45-
#define MESSAGE_LEN 8193
43+
const int subsystem_len = 257;
44+
const int category_len = 257;
45+
const int message_len = 8193;
4646

4747
FlogConfigLevel flog_config_parse_level(const char *str);
4848

@@ -60,10 +60,10 @@ static struct poptOption options[] = {
6060
struct FlogConfigData {
6161
FlogConfigLevel level;
6262
FlogConfigMessageType message_type;
63-
char subsystem[SUBSYSTEM_LEN];
64-
char category[CATEGORY_LEN];
63+
char subsystem[subsystem_len];
64+
char category[category_len];
6565
char output_file[PATH_MAX];
66-
char message[MESSAGE_LEN];
66+
char message[message_len];
6767
bool version;
6868
bool help;
6969
};
@@ -83,7 +83,7 @@ flog_config_new(int argc, char *argv[], FlogError *error) {
8383
}
8484

8585
flog_config_set_level(config, LVL_DEFAULT);
86-
flog_config_set_message_type(config, Public);
86+
flog_config_set_message_type(config, MSG_PUBLIC);
8787
flog_config_set_version_flag(config, false);
8888
flog_config_set_help_flag(config, false);
8989

@@ -104,7 +104,12 @@ flog_config_new(int argc, char *argv[], FlogError *error) {
104104
poptFreeContext(context);
105105
return config;
106106
case 'a':
107-
flog_config_set_output_file(config, option_argument);
107+
*error = flog_config_set_output_file(config, option_argument);
108+
if (*error != FLOG_ERROR_NONE) {
109+
flog_config_free(config);
110+
poptFreeContext(context);
111+
return NULL;
112+
}
108113
break;
109114
case 'l':
110115
flog_config_set_level(config, flog_config_parse_level(option_argument));
@@ -117,13 +122,12 @@ flog_config_new(int argc, char *argv[], FlogError *error) {
117122
break;
118123
case 's':
119124
flog_config_set_subsystem(config, option_argument);
120-
121125
break;
122126
case 'c':
123127
flog_config_set_category(config, option_argument);
124128
break;
125129
case 'p':
126-
flog_config_set_message_type(config, Private);
130+
flog_config_set_message_type(config, MSG_PRIVATE);
127131
break;
128132
}
129133

@@ -187,8 +191,8 @@ flog_config_set_subsystem(FlogConfig *config, const char *subsystem) {
187191
assert(config != NULL);
188192
assert(subsystem != NULL);
189193

190-
if (strlcpy(config->subsystem, subsystem, SUBSYSTEM_LEN) >= SUBSYSTEM_LEN) {
191-
fprintf(stderr, "%s: subsystem name truncated to %d bytes\n", PROGRAM_NAME, SUBSYSTEM_LEN - 1);
194+
if (strlcpy(config->subsystem, subsystem, subsystem_len) >= subsystem_len) {
195+
fprintf(stderr, "%s: subsystem name truncated to %d bytes\n", PROGRAM_NAME, subsystem_len - 1);
192196
}
193197
}
194198

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

207-
if (strlcpy(config->category, category, CATEGORY_LEN) >= CATEGORY_LEN) {
208-
fprintf(stderr, "%s: category name truncated to %d bytes\n", PROGRAM_NAME, CATEGORY_LEN - 1);
211+
if (strlcpy(config->category, category, category_len) >= category_len) {
212+
fprintf(stderr, "%s: category name truncated to %d bytes\n", PROGRAM_NAME, category_len - 1);
209213
}
210214
}
211215

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

219-
void
223+
FlogError
220224
flog_config_set_output_file(FlogConfig *config, const char *output_file) {
221225
assert(config != NULL);
222226
assert(output_file != NULL);
223227

224228
if (strlcpy(config->output_file, output_file, PATH_MAX) >= PATH_MAX) {
225-
fprintf(stderr, "%s: specify an output file path up to a maximum of %d characters\n", PROGRAM_NAME, PATH_MAX - 1);
226-
exit(EXIT_FAILURE);
229+
return FLOG_ERROR_FILE;
227230
}
231+
232+
return FLOG_ERROR_NONE;
228233
}
229234

230235
FlogConfigLevel
@@ -274,8 +279,8 @@ flog_config_set_message(FlogConfig *config, const char *message) {
274279
assert(config != NULL);
275280
assert(message != NULL);
276281

277-
if (strlcpy(config->message, message, MESSAGE_LEN) >= MESSAGE_LEN) {
278-
fprintf(stderr, "%s: message string was truncated to %d bytes\n", PROGRAM_NAME, MESSAGE_LEN - 1);
282+
if (strlcpy(config->message, message, message_len) >= message_len) {
283+
fprintf(stderr, "%s: message string was truncated to %d bytes\n", PROGRAM_NAME, message_len - 1);
279284
}
280285
}
281286

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

292+
config->message[0] = '\0';
293+
287294
bool message_truncated = false;
288295
while (*args != NULL) {
289-
if (strlcat(config->message, *args, MESSAGE_LEN) >= MESSAGE_LEN) {
296+
if (strlcat(config->message, *args, message_len) >= message_len) {
290297
message_truncated = true;
291298
break;
292299
}
293300
if (*(args + 1) != NULL) {
294-
if (strlcat(config->message, " ", MESSAGE_LEN) >= MESSAGE_LEN) {
301+
if (strlcat(config->message, " ", message_len) >= message_len) {
295302
message_truncated = true;
296303
break;
297304
}
@@ -300,7 +307,7 @@ flog_config_set_message_from_args(FlogConfig *config, const char **args) {
300307
}
301308

302309
if (message_truncated) {
303-
fprintf(stderr, "%s: message was truncated to %d bytes\n", PROGRAM_NAME, MESSAGE_LEN - 1);
310+
fprintf(stderr, "%s: message was truncated to %d bytes\n", PROGRAM_NAME, message_len - 1);
304311
}
305312
}
306313

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

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

src/config.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
#include <stdio.h>
3434
#include "common.h"
3535

36+
extern const int subsystem_len;
37+
extern const int category_len;
38+
extern const int message_len;
39+
3640
/*! \brief An enumerated type representing the log level. */
3741
typedef enum FlogConfigLevelData {
3842
LVL_DEFAULT,
@@ -45,8 +49,8 @@ typedef enum FlogConfigLevelData {
4549

4650
/*! \brief An enumerated type representing the log message type. */
4751
typedef enum FlogConfigMessageTypeData {
48-
Public,
49-
Private,
52+
MSG_PUBLIC,
53+
MSG_PRIVATE
5054
} FlogConfigMessageType;
5155

5256
/*! \struct FlogConfig
@@ -139,8 +143,11 @@ const char * flog_config_get_output_file(const FlogConfig *config);
139143
*
140144
* \pre \c config is \e not \c NULL
141145
* \pre \c output_file is \e not \c NULL
146+
*
147+
* \return If successful, the FlogError variant FLOG_ERROR_NONE, or FLOG_ERROR_FILE
148+
* if the output_file path exceeds the maximum path limit
142149
*/
143-
void flog_config_set_output_file(FlogConfig *config, const char *output_file);
150+
FlogError flog_config_set_output_file(FlogConfig *config, const char *output_file);
144151

145152
/*! \brief Get the log level value from a FlogConfig object.
146153
*

src/flog.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ flog_commit_message(FlogCli *flog) {
104104
assert(flog != NULL);
105105

106106
FlogConfig *config = flog_cli_get_config(flog);
107-
if (flog_config_get_message_type(config) == Public) {
107+
if (flog_config_get_message_type(config) == MSG_PUBLIC) {
108108
flog_commit_public_message(flog);
109-
} else if (flog_config_get_message_type(config) == Private) {
109+
} else if (flog_config_get_message_type(config) == MSG_PRIVATE) {
110110
flog_commit_private_message(flog);
111111
}
112112
}

0 commit comments

Comments
 (0)