diff --git a/protocol/BUILD b/protocol/BUILD index 23817b8..c7fe2b9 100644 --- a/protocol/BUILD +++ b/protocol/BUILD @@ -558,6 +558,18 @@ cc_library( ], ) +cc_test( + name = "console_test", + srcs = ["console_test.cc"], + deps = [ + ":console", + "//protocol/test:libhoth_device_mock", + "//transports:libhoth_device", + "@googletest//:gtest", + "@googletest//:gtest_main", + ], +) + cc_library( name = "dfu_hostcmd", diff --git a/protocol/console.c b/protocol/console.c index 15634d3..6b9b62e 100644 --- a/protocol/console.c +++ b/protocol/console.c @@ -91,7 +91,7 @@ libhoth_error libhoth_read_console(struct libhoth_device* dev, int fd, struct hoth_channel_read_request req = { .channel_id = channel_id, .offset = *offset, - .size = HOTH_FIFO_MAX_REQUEST_SIZE - + .size = HOTH_FIFO_MAX_REQUEST_SIZE - sizeof(struct hoth_host_response) - sizeof(struct hoth_channel_read_response), .timeout_us = 10000, }; diff --git a/protocol/console_test.cc b/protocol/console_test.cc new file mode 100644 index 0000000..3cc2895 --- /dev/null +++ b/protocol/console_test.cc @@ -0,0 +1,56 @@ +// 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 "console.h" + +#include +#include +#include + +#include + +#include "test/libhoth_device_mock.h" + +using ::testing::_; +using ::testing::DoAll; +using ::testing::Return; + +constexpr uint32_t kCmdChannelRead = + HOTH_CMD_BOARD_SPECIFIC_BASE + HOTH_PRV_CMD_HOTH_CHANNEL_READ; + +bool CheckReadSize(const void* data) { + const auto* req = static_cast(data); + return req->size == 1012; +} + +TEST_F(LibHothTest, read_console_test) { + EXPECT_CALL(mock_, + send(_, UsesCommandWithData(kCmdChannelRead, CheckReadSize), _)) + .WillOnce(Return(LIBHOTH_OK)); + + struct { + struct hoth_channel_read_response hdr; + uint8_t data[1012]; + } fake_response = {}; + fake_response.hdr.offset = 100; + + EXPECT_CALL(mock_, receive) + .WillOnce(DoAll(CopyResp(&fake_response, sizeof(fake_response)), + Return(LIBHOTH_OK))); + + uint32_t offset = 100; + EXPECT_EQ(libhoth_read_console(&hoth_dev_, STDOUT_FILENO, false, 0, &offset), + HOTH_SUCCESS); + EXPECT_EQ(offset, 100 + 1012); +}