Skip to content

Commit

Permalink
Add ftell check and error injection test for properties
Browse files Browse the repository at this point in the history
  • Loading branch information
pnoltes committed Nov 20, 2023
1 parent 78d7879 commit 95460e9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
10 changes: 10 additions & 0 deletions libs/utils/gtest/src/PropertiesErrorInjectionTestSuite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class PropertiesErrorInjectionTestSuite : public ::testing::Test {
celix_ei_expect_fopen(nullptr, 0, nullptr);
celix_ei_expect_fputc(nullptr, 0, 0);
celix_ei_expect_fseek(nullptr, 0, 0);
celix_ei_expect_ftell(nullptr, 0, 0);
celix_ei_expect_celix_utils_strdup(nullptr, 0, nullptr);
celix_ei_expect_celix_stringHashMap_put(nullptr, 0, 0);
}
Expand Down Expand Up @@ -225,6 +226,15 @@ TEST_F(PropertiesErrorInjectionTestSuite, LoadFailureTest) {
ASSERT_EQ(1, celix_err_getErrorCount());
celix_err_resetErrors();

// When a ftell error injection is set for celix_properties_loadWithStream
celix_ei_expect_ftell((void*)celix_properties_loadWithStream, 0, -1);
// Then the celix_properties_loadWithStream call fails
props = celix_properties_loadWithStream(memStream);
ASSERT_EQ(nullptr, props);
// And a celix err msg is set
ASSERT_EQ(1, celix_err_getErrorCount());
celix_err_resetErrors();

// When a malloc error injection is set for celix_properties_loadWithStream
celix_ei_expect_malloc((void*)celix_properties_loadWithStream, 0, nullptr);
// Then the celix_properties_loadWithStream call fails
Expand Down
8 changes: 7 additions & 1 deletion libs/utils/src/properties.c
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,13 @@ celix_properties_t* celix_properties_loadWithStream(FILE* file) {
celix_err_pushf("Cannot seek to end of file. Got error %i", errno);
return NULL;
}
size_t fileSize = ftell(file);

long fileSize = ftell(file);
if (fileSize < 0) {
celix_err_pushf("Cannot get file size. Got error %i", errno);
return NULL;
}

rc = fseek(file, 0, SEEK_SET);
if (rc != 0) {
celix_err_pushf("Cannot seek to start of file. Got error %i", errno);
Expand Down

0 comments on commit 95460e9

Please sign in to comment.