|
16 | 16 |
|
17 | 17 | #include <gmock/gmock.h> |
18 | 18 | #include <gtest/gtest.h> |
| 19 | +#include <unistd.h> |
19 | 20 |
|
20 | 21 | #include <cstdint> |
| 22 | +#include <cstdlib> |
| 23 | +#include <cstring> |
21 | 24 | #include <memory> |
22 | 25 |
|
23 | 26 | #include "command_version.h" |
@@ -47,6 +50,16 @@ MATCHER_P2(IsEraseRequest, offset, len, "") { |
47 | 50 | p->len == static_cast<uint32_t>(len); |
48 | 51 | } |
49 | 52 |
|
| 53 | +MATCHER_P2(IsReadRequest, offset, len, "") { |
| 54 | + const uint8_t* data = static_cast<const uint8_t*>(arg); |
| 55 | + const struct payload_update_packet* p = |
| 56 | + reinterpret_cast<const struct payload_update_packet*>( |
| 57 | + data + sizeof(struct hoth_host_request)); |
| 58 | + return p->type == PAYLOAD_UPDATE_READ && |
| 59 | + p->offset == static_cast<uint32_t>(offset) && |
| 60 | + p->len == static_cast<uint32_t>(len); |
| 61 | +} |
| 62 | + |
50 | 63 | TEST_F(LibHothTest, payload_update_bad_image_test) { |
51 | 64 | EXPECT_CALL(mock_, send(_, UsesCommand(kCmd), _)) |
52 | 65 | .WillRepeatedly(Return(LIBHOTH_OK)); |
@@ -496,3 +509,29 @@ TEST_F(LibHothTest, payload_update_erase_cmd_range_overflow_test) { |
496 | 509 | EXPECT_EQ(libhoth_payload_update_erase(&hoth_dev_, kOffset, kSize), |
497 | 510 | PAYLOAD_UPDATE_INVALID_ARGS); |
498 | 511 | } |
| 512 | + |
| 513 | +TEST_F(LibHothTest, payload_update_read_chunk_test) { |
| 514 | + uint8_t expected_data[] = {0x11, 0x22, 0x33, 0x44, 0x55}; |
| 515 | + size_t offset = 0x100; |
| 516 | + size_t len = sizeof(expected_data); |
| 517 | + |
| 518 | + EXPECT_CALL(mock_, send(_, IsReadRequest(offset, len), _)) |
| 519 | + .WillOnce(Return(LIBHOTH_OK)); |
| 520 | + EXPECT_CALL(mock_, receive) |
| 521 | + .WillOnce(DoAll(CopyResp(expected_data, len), Return(LIBHOTH_OK))); |
| 522 | + |
| 523 | + char temp_path[] = "/tmp/libhoth_payload_read_XXXXXX"; |
| 524 | + int fd = mkstemp(temp_path); |
| 525 | + ASSERT_GE(fd, 0); |
| 526 | + |
| 527 | + EXPECT_EQ(libhoth_payload_update_read_chunk(&hoth_dev_, fd, len, offset), |
| 528 | + PAYLOAD_UPDATE_OK); |
| 529 | + |
| 530 | + lseek(fd, 0, SEEK_SET); |
| 531 | + uint8_t actual_data[sizeof(expected_data)] = {0}; |
| 532 | + ASSERT_EQ(read(fd, actual_data, len), static_cast<ssize_t>(len)); |
| 533 | + EXPECT_THAT(actual_data, ::testing::ElementsAreArray(expected_data)); |
| 534 | + |
| 535 | + close(fd); |
| 536 | + unlink(temp_path); |
| 537 | +} |
0 commit comments