Skip to content

Commit 8687a69

Browse files
committed
Add unit test coverage for stream read failures
1 parent 060e29f commit 8687a69

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

test/test_config.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,49 @@ flog_config_set_message_from_stream_with_null_stream_arg_fails(void **state) {
13211321
flog_config_free(config);
13221322
}
13231323

1324+
static void
1325+
flog_config_set_message_from_stream_with_broken_stream_fails(void **state) {
1326+
UNUSED(state);
1327+
1328+
FlogError error = TEST_ERROR;
1329+
MOCK_ARGS(
1330+
TEST_PROGRAM_NAME,
1331+
TEST_MESSAGE
1332+
)
1333+
1334+
int temp_fd;
1335+
char template[] = "/tmp/flog.XXXXXXXX";
1336+
1337+
// Create a temporary file
1338+
if ((temp_fd = mkstemp(template)) == -1) {
1339+
perror("mkstemp");
1340+
fail();
1341+
}
1342+
1343+
// Associate a stream with the temporary file
1344+
FILE *temp_stream = fdopen(temp_fd, "r+");
1345+
if (temp_stream == NULL) {
1346+
perror("fdopen");
1347+
close(temp_fd);
1348+
unlink(template);
1349+
fail();
1350+
}
1351+
1352+
// Simulate a broken stream
1353+
fclose(temp_stream);
1354+
1355+
// Initialise a FlogConfig object with message string from arguments
1356+
FlogConfig *config = flog_config_new(mock_argc, mock_argv, &error);
1357+
1358+
// Test message parsing when read from a broken stream
1359+
flog_config_set_message_from_stream(config, temp_stream);
1360+
1361+
assert_non_null(config);
1362+
assert_string_equal(flog_config_get_message(config), "");
1363+
1364+
flog_config_free(config);
1365+
}
1366+
13241367
static void
13251368
flog_config_set_and_get_message_from_stream_succeeds(void **state) {
13261369
UNUSED(state);
@@ -1615,6 +1658,9 @@ int main(void) {
16151658
cmocka_unit_test(flog_config_set_message_from_stream_with_null_config_arg_fails),
16161659
cmocka_unit_test(flog_config_set_message_from_stream_with_null_stream_arg_fails),
16171660

1661+
// flog_config_set_message_from_stream() failure tests
1662+
cmocka_unit_test(flog_config_set_message_from_stream_with_broken_stream_fails),
1663+
16181664
// flog_config_set_message_from_stream() success tests
16191665
cmocka_unit_test(flog_config_set_and_get_message_from_stream_succeeds),
16201666

0 commit comments

Comments
 (0)