From fe4b307e3d7f2300709911405331784eba81beb5 Mon Sep 17 00:00:00 2001 From: Enrico Joerns Date: Fri, 30 Aug 2024 11:17:53 +0200 Subject: [PATCH] test/hash_index: add test for invalid size for testing error handling Signed-off-by: Enrico Joerns --- test/hash_index.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/hash_index.c b/test/hash_index.c index 021e21f9d..22eaa4e26 100644 --- a/test/hash_index.c +++ b/test/hash_index.c @@ -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"); @@ -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(); }