From 3b0275ea0a31013c21ed646230b567f6303c10a3 Mon Sep 17 00:00:00 2001 From: Ellis Sarza-Nguyen Date: Tue, 13 Jan 2026 09:02:08 -0800 Subject: [PATCH] [protocol] Add unit test for dfu check Adding dfu checks to cover the average case a passing test case given a .fwupdate file and a fail negative case Signed-off-by: Ellis Sarza-Nguyen --- protocol/BUILD | 18 +++++++ protocol/dfu_check_test.cc | 98 +++++++++++++++++++++++++++++++++++++ protocol/test/BUILD | 1 + protocol/test/dummy.bin | Bin 0 -> 92160 bytes 4 files changed, 117 insertions(+) create mode 100644 protocol/dfu_check_test.cc create mode 100644 protocol/test/dummy.bin diff --git a/protocol/BUILD b/protocol/BUILD index 00c706a..ca91498 100644 --- a/protocol/BUILD +++ b/protocol/BUILD @@ -504,6 +504,24 @@ cc_library( ], ) +cc_test( + name = "dfu_check_test", + srcs = ["dfu_check_test.cc"], + data = [ + "//protocol/test:test_data", + ], + deps = [ + ":host_cmd", + ":dfu_check", + ":opentitan_version", + "//protocol/test:libhoth_device_mock", + "//transports:libhoth_device", + "@googletest//:gtest", + "@googletest//:gtest_main", + ], +) + + cc_library( name = "util", srcs = ["util.c"], diff --git a/protocol/dfu_check_test.cc b/protocol/dfu_check_test.cc new file mode 100644 index 0000000..084281b --- /dev/null +++ b/protocol/dfu_check_test.cc @@ -0,0 +1,98 @@ +// Copyright 2026 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "dfu_check.h" + +#include +#include +#include +#include + +#include +#include +#include + +#include "opentitan_version.h" +#include "test/libhoth_device_mock.h" + +using ::testing::_; +using ::testing::DoAll; +using ::testing::ElementsAreArray; +using ::testing::Return; + +constexpr char kTestData[] = "protocol/test/dummy.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); + + 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); + + struct opentitan_get_version_resp mock_response = {}; + + mock_response.rom_ext.slots[0].major = 0; + mock_response.rom_ext.slots[0].minor = 0; + mock_response.rom_ext.slots[1].major = 0; + mock_response.rom_ext.slots[1].minor = 0; + + mock_response.app.slots[0].major = 0; + mock_response.app.slots[0].minor = 0; + mock_response.app.slots[1].major = 0; + mock_response.app.slots[1].minor = 0; + + EXPECT_EQ( + libhoth_dfu_check(&hoth_dev_, image, statbuf.st_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); + + struct opentitan_get_version_resp mock_response = {}; + + mock_response.rom_ext.slots[0].major = 0; + mock_response.rom_ext.slots[0].minor = 115; + mock_response.rom_ext.slots[1].major = 0; + mock_response.rom_ext.slots[1].minor = 116; + + mock_response.app.slots[0].major = 0; + mock_response.app.slots[0].minor = 2025102930; + mock_response.app.slots[1].major = 0; + mock_response.app.slots[1].minor = 2025102930; + + EXPECT_EQ( + libhoth_dfu_check(&hoth_dev_, image, statbuf.st_size, &mock_response), + -1); +} diff --git a/protocol/test/BUILD b/protocol/test/BUILD index 5275b20..30e113f 100644 --- a/protocol/test/BUILD +++ b/protocol/test/BUILD @@ -17,3 +17,4 @@ filegroup( name = "test_data", srcs = glob(["*.bin"]), ) + diff --git a/protocol/test/dummy.bin b/protocol/test/dummy.bin new file mode 100644 index 0000000000000000000000000000000000000000..dc6d822d9922cd6d22624605a605e9a79296930b GIT binary patch literal 92160 zcmeI&u?fOJ6b9f34r8I<1QNwq2pW+S8zD