Skip to content

Commit

Permalink
test/hash_index: add test for invalid size for testing error handling
Browse files Browse the repository at this point in the history
Signed-off-by: Enrico Joerns <ejo@pengutronix.de>
  • Loading branch information
ejoerns committed Aug 30, 2024
1 parent 893187f commit fe4b307
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions test/hash_index.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,27 @@ static void test_ranges(Fixture *fixture, gconstpointer user_data)
g_clear_pointer(&hash, g_free);
}

/* Tests error handling by ... on a file size that is not a multiple of 4096 */
static void test_invalid_size(Fixture *fixture, gconstpointer user_data)
{
g_autoptr(GError) error = NULL;
g_autoptr(RaucHashIndex) index = NULL;
int datafd = -1;

g_autofree gchar *data_filename = write_random_file(fixture->tmpdir, "broken.img", 2048*63, 0xf56ce6bf);
g_assert_nonnull(data_filename);

datafd = g_open(data_filename, O_RDONLY|O_CLOEXEC, 0);
g_assert_cmpint(datafd, >, 0);

// open and calculate hash index
index = r_hash_index_open("test", datafd, NULL, &error);
g_assert_error(error, R_HASH_INDEX_ERROR, R_HASH_INDEX_ERROR_SIZE);
g_assert_nonnull(index);
datafd = -1; /* belongs to index now */
(void)datafd; /* ignore dead store */
}

int main(int argc, char *argv[])
{
setlocale(LC_ALL, "C");
Expand All @@ -290,6 +311,7 @@ int main(int argc, char *argv[])

g_test_add("/hash_index/basic", Fixture, NULL, fixture_set_up, test_basic, fixture_tear_down);
g_test_add("/hash_index/ranges", Fixture, NULL, fixture_set_up, test_ranges, fixture_tear_down);
g_test_add("/hash_index/invalid-size", Fixture, NULL, fixture_set_up, test_invalid_size, fixture_tear_down);

return g_test_run();
}

0 comments on commit fe4b307

Please sign in to comment.