From e55d9932cacacbac5778b6616b1e4af04f49b5ff Mon Sep 17 00:00:00 2001 From: rory-cd Date: Mon, 17 Nov 2025 14:28:12 +1100 Subject: [PATCH 1/2] Add bitmap_filename unit test --- .../src/test/unit_tests/unit_test_bitmap.cpp | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/coresdk/src/test/unit_tests/unit_test_bitmap.cpp b/coresdk/src/test/unit_tests/unit_test_bitmap.cpp index e8e64bed..b61d5ca2 100644 --- a/coresdk/src/test/unit_tests/unit_test_bitmap.cpp +++ b/coresdk/src/test/unit_tests/unit_test_bitmap.cpp @@ -4,6 +4,8 @@ #include "catch.hpp" +#include + #include "types.h" #include "graphics.h" #include "resources.h" @@ -128,3 +130,39 @@ TEST_CASE("bitmap bounding details can be retrieved", "[bitmap]") } free_bitmap(bmp); } + +TEST_CASE("bitmap filename can be retrieved", "[bitmap_filename]") +{ + SECTION("returns correct filename for bitmap") + { + std::string expected_filename = "rocket_sprt.png"; + + // Load a bitmap + bitmap bmp = load_bitmap("rocket", expected_filename); + REQUIRE(bmp != nullptr); + REQUIRE(bitmap_valid(bmp)); + + // Extract filename + std::string filepath = bitmap_filename(bmp); + size_t idx = filepath.size() - expected_filename.size(); + std::string filename = filepath.substr(idx, expected_filename.size()); + + REQUIRE(filename == expected_filename); + free_bitmap(bmp); + } + + SECTION("returns empty string for null bitmap") + { + bitmap null_bmp = nullptr; + std::string filepath = bitmap_filename(null_bmp); + REQUIRE(filepath == ""); + } + + SECTION("returns empty string for newly created bitmap") + { + bitmap created_bmp = create_bitmap("new bitmap", 100, 100); + std::string filepath = bitmap_filename(created_bmp); + REQUIRE(filepath == ""); + free_bitmap(created_bmp); + } +} \ No newline at end of file From d1b150aeb87f48b7fc9e179f76e94d7d2a26ed78 Mon Sep 17 00:00:00 2001 From: rory-cd Date: Mon, 1 Dec 2025 10:51:18 +1100 Subject: [PATCH 2/2] Fix potential integer underflow as per review Adds requirement ensuring the filepath being read exceeds or equals the length of the expected filename. This removes the potential for integer underflow. --- coresdk/src/test/unit_tests/unit_test_bitmap.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/coresdk/src/test/unit_tests/unit_test_bitmap.cpp b/coresdk/src/test/unit_tests/unit_test_bitmap.cpp index d09ca12a..7193f887 100644 --- a/coresdk/src/test/unit_tests/unit_test_bitmap.cpp +++ b/coresdk/src/test/unit_tests/unit_test_bitmap.cpp @@ -150,8 +150,9 @@ TEST_CASE("bitmap filename can be retrieved", "[bitmap_filename]") // Extract filename std::string filepath = bitmap_filename(bmp); + REQUIRE(filepath.size() >= expected_filename.size()); size_t idx = filepath.size() - expected_filename.size(); - std::string filename = filepath.substr(idx, expected_filename.size()); + std::string filename = filepath.substr(idx); REQUIRE(filename == expected_filename); free_bitmap(bmp);