Skip to content

Commit 7978751

Browse files
committed
[protocol] Add unittest for libhoth_payload_update_read_chunk
This function is being used within libhoth host command and needs a check. Signed-off-by: Ellis Nguyen <sarzanguyen@google.com>
1 parent 3286843 commit 7978751

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

protocol/payload_update_test.cc

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616

1717
#include <gmock/gmock.h>
1818
#include <gtest/gtest.h>
19+
#include <unistd.h>
1920

2021
#include <cstdint>
22+
#include <cstdlib>
23+
#include <cstring>
2124
#include <memory>
2225

2326
#include "command_version.h"
@@ -47,6 +50,16 @@ MATCHER_P2(IsEraseRequest, offset, len, "") {
4750
p->len == static_cast<uint32_t>(len);
4851
}
4952

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+
5063
TEST_F(LibHothTest, payload_update_bad_image_test) {
5164
EXPECT_CALL(mock_, send(_, UsesCommand(kCmd), _))
5265
.WillRepeatedly(Return(LIBHOTH_OK));
@@ -496,3 +509,29 @@ TEST_F(LibHothTest, payload_update_erase_cmd_range_overflow_test) {
496509
EXPECT_EQ(libhoth_payload_update_erase(&hoth_dev_, kOffset, kSize),
497510
PAYLOAD_UPDATE_INVALID_ARGS);
498511
}
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

Comments
 (0)