From 37aad300b0706d74f71ba4b8f285bf93c07ff204 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/test_fwupdate.bin | Bin 0 -> 92160 bytes 3 files changed, 116 insertions(+) create mode 100644 protocol/dfu_check_test.cc create mode 100644 protocol/test/test_fwupdate.bin diff --git a/protocol/BUILD b/protocol/BUILD index fa19fff..ee80cff 100644 --- a/protocol/BUILD +++ b/protocol/BUILD @@ -515,6 +515,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..747414e --- /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/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); + + 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 = 1; + mock_response.rom_ext.slots[0].minor = 0; + mock_response.rom_ext.slots[1].major = 1; + mock_response.rom_ext.slots[1].minor = 0; + + mock_response.app.slots[0].major = 2; + mock_response.app.slots[0].minor = 5; + mock_response.app.slots[1].major = 2; + mock_response.app.slots[1].minor = 5; + + 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 = 11; + mock_response.rom_ext.slots[1].major = 0; + mock_response.rom_ext.slots[1].minor = 11; + + mock_response.app.slots[0].major = 0; + mock_response.app.slots[0].minor = 20; + mock_response.app.slots[1].major = 0; + mock_response.app.slots[1].minor = 20; + + EXPECT_EQ( + libhoth_dfu_check(&hoth_dev_, image, statbuf.st_size, &mock_response), + -1); +} diff --git a/protocol/test/test_fwupdate.bin b/protocol/test/test_fwupdate.bin new file mode 100644 index 0000000000000000000000000000000000000000..ea7c7d7d61a1c2dd7b0cd7928426ddc502c7d9e3 GIT binary patch literal 92160 zcmeI&u?fOJ6b9gn=q?rtP9Ra#LePjrY=kV~C{AJF3ZhszfD>3bgH4Rh)!HTS{c!N! zaR-mDxax{5pKUj*$ta%|@5|z}FU!a6c|E^|_vK+X@ApELZne@RltO?20RjXF5FkK+ z009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF)I#9