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 0000000..dc6d822 Binary files /dev/null and b/protocol/test/dummy.bin differ