From 2c627d1c4bcafe2254d52428f3ca49e45bbf534d Mon Sep 17 00:00:00 2001 From: Ellis Nguyen Date: Mon, 16 Mar 2026 17:00:04 +0000 Subject: [PATCH] [protocol] Clean dfu_check_test Remove unused calls for the libhoth_extract_bundle. In addition, the fopen doesnt have a proper close to free up memory in case of failure. This make the test more cleaner. Signed-off-by: Ellis Nguyen --- protocol/dfu_check_test.cc | 57 +++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/protocol/dfu_check_test.cc b/protocol/dfu_check_test.cc index 747414e..03705dd 100644 --- a/protocol/dfu_check_test.cc +++ b/protocol/dfu_check_test.cc @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -33,20 +34,31 @@ using ::testing::Return; constexpr char kTestData[] = "protocol/test/test_fwupdate.bin"; -TEST_F(LibHothTest, dfu_check_test) { - int fd = open(kTestData, O_RDONLY, 0); - ASSERT_NE(fd, -1); - - struct stat statbuf; - ASSERT_EQ(fstat(fd, &statbuf), 0); +// Helper to manage test image lifetime +struct MappedFile { + int fd = -1; + uint8_t* data = nullptr; + size_t size = 0; + ~MappedFile() { + if (data) munmap(data, size); + if (fd != -1) close(fd); + } + + bool Load(const char* path) { + fd = open(path, O_RDONLY, 0); + if (fd == -1) return false; + struct stat statbuf = {0}; + if (fstat(fd, &statbuf) != 0) return false; + size = statbuf.st_size; + data = reinterpret_cast( + mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0)); + return data != MAP_FAILED; + } +}; - uint8_t* image = reinterpret_cast( - mmap(NULL, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0)); - ASSERT_NE(image, nullptr); - - struct opentitan_image_version rom_ext; - struct opentitan_image_version app; - libhoth_extract_ot_bundle(image, statbuf.st_size, &rom_ext, &app); +TEST_F(LibHothTest, dfu_check_test) { + MappedFile image = {}; + ASSERT_TRUE(image.Load(kTestData)); struct opentitan_get_version_resp mock_response = {}; @@ -61,24 +73,13 @@ TEST_F(LibHothTest, dfu_check_test) { mock_response.app.slots[1].minor = 5; EXPECT_EQ( - libhoth_dfu_check(&hoth_dev_, image, statbuf.st_size, &mock_response), + libhoth_dfu_check(&hoth_dev_, image.data, image.size, &mock_response), LIBHOTH_OK); } TEST_F(LibHothTest, dfu_check_fail) { - int fd = open(kTestData, O_RDONLY, 0); - ASSERT_NE(fd, -1); - - struct stat statbuf; - ASSERT_EQ(fstat(fd, &statbuf), 0); - - uint8_t* image = reinterpret_cast( - mmap(NULL, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0)); - ASSERT_NE(image, nullptr); - - struct opentitan_image_version rom_ext; - struct opentitan_image_version app; - libhoth_extract_ot_bundle(image, statbuf.st_size, &rom_ext, &app); + MappedFile image = {}; + ASSERT_TRUE(image.Load(kTestData)); struct opentitan_get_version_resp mock_response = {}; @@ -93,6 +94,6 @@ TEST_F(LibHothTest, dfu_check_fail) { mock_response.app.slots[1].minor = 20; EXPECT_EQ( - libhoth_dfu_check(&hoth_dev_, image, statbuf.st_size, &mock_response), + libhoth_dfu_check(&hoth_dev_, image.data, image.size, &mock_response), -1); }