From de29b505c006f21c481894411041ddb1e90ea4de Mon Sep 17 00:00:00 2001 From: Jan Kolarik Date: Fri, 24 Nov 2023 14:08:13 +0100 Subject: [PATCH] Test for adding an empty list to memory file Although this scenario is not expected, make sure it is not crashing. --- test/libdnf5/repo/test_temp_files_memory.cpp | 11 +++++++++++ test/libdnf5/repo/test_temp_files_memory.hpp | 2 ++ 2 files changed, 13 insertions(+) diff --git a/test/libdnf5/repo/test_temp_files_memory.cpp b/test/libdnf5/repo/test_temp_files_memory.cpp index c1437c277..428c54221 100644 --- a/test/libdnf5/repo/test_temp_files_memory.cpp +++ b/test/libdnf5/repo/test_temp_files_memory.cpp @@ -79,6 +79,17 @@ void TempFilesMemoryTest::test_add_files_when_empty_storage() { CPPUNIT_ASSERT_EQUAL(new_paths, memory.get_files()); } +void TempFilesMemoryTest::test_add_no_files_when_empty_storage() { + // make sure that adding an empty vector will not cause crashing + + TempFilesMemory memory(base.get_weak_ptr(), parent_dir_path); + CPPUNIT_ASSERT(memory.get_files().empty()); + + std::vector empty_paths = {}; + memory.add_files(empty_paths); + CPPUNIT_ASSERT(memory.get_files().empty()); +} + void TempFilesMemoryTest::test_add_files_when_existing_storage() { std::vector new_paths = {"path3", "path4"}; libdnf5::utils::fs::File(full_path, "w") diff --git a/test/libdnf5/repo/test_temp_files_memory.hpp b/test/libdnf5/repo/test_temp_files_memory.hpp index 42f23f983..f552f257f 100644 --- a/test/libdnf5/repo/test_temp_files_memory.hpp +++ b/test/libdnf5/repo/test_temp_files_memory.hpp @@ -34,6 +34,7 @@ class TempFilesMemoryTest : public BaseTestCase { CPPUNIT_TEST(test_get_files_throws_exception_when_invalid_format); CPPUNIT_TEST(test_get_files_returns_stored_values); CPPUNIT_TEST(test_add_files_when_empty_storage); + CPPUNIT_TEST(test_add_no_files_when_empty_storage); CPPUNIT_TEST(test_add_files_when_existing_storage); CPPUNIT_TEST(test_add_files_deduplicates_and_sorts_data); CPPUNIT_TEST(test_clear_deletes_storage_content); @@ -48,6 +49,7 @@ class TempFilesMemoryTest : public BaseTestCase { void test_get_files_throws_exception_when_invalid_format(); void test_get_files_returns_stored_values(); void test_add_files_when_empty_storage(); + void test_add_no_files_when_empty_storage(); void test_add_files_when_existing_storage(); void test_add_files_deduplicates_and_sorts_data(); void test_clear_deletes_storage_content();