Skip to content

Commit d2a8d1b

Browse files
authored
[protocol] Add unittest for libhoth_payload_update_read_chunk (#241)
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 d2a8d1b

1 file changed

Lines changed: 54 additions & 2 deletions

File tree

protocol/payload_update_test.cc

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
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>
25+
#include <string>
2226

2327
#include "command_version.h"
2428
#include "payload_info.h"
@@ -38,15 +42,35 @@ constexpr int64_t kAlign = 1 << 16;
3842
constexpr int64_t kDummy = 0;
3943

4044
MATCHER_P2(IsEraseRequest, offset, len, "") {
41-
const uint8_t* data = static_cast<const uint8_t*>(arg);
45+
const struct hoth_host_request* req =
46+
static_cast<const struct hoth_host_request*>(arg);
47+
if (req->struct_version != HOTH_HOST_REQUEST_VERSION ||
48+
req->command != kCmd) {
49+
return false;
50+
}
4251
const struct payload_update_packet* p =
4352
reinterpret_cast<const struct payload_update_packet*>(
44-
data + sizeof(struct hoth_host_request));
53+
static_cast<const uint8_t*>(arg) + sizeof(struct hoth_host_request));
4554
return p->type == PAYLOAD_UPDATE_ERASE &&
4655
p->offset == static_cast<uint32_t>(offset) &&
4756
p->len == static_cast<uint32_t>(len);
4857
}
4958

59+
MATCHER_P2(IsReadRequest, offset, len, "") {
60+
const struct hoth_host_request* req =
61+
static_cast<const struct hoth_host_request*>(arg);
62+
if (req->struct_version != HOTH_HOST_REQUEST_VERSION ||
63+
req->command != kCmd) {
64+
return false;
65+
}
66+
const struct payload_update_packet* p =
67+
reinterpret_cast<const struct payload_update_packet*>(
68+
static_cast<const uint8_t*>(arg) + sizeof(struct hoth_host_request));
69+
return p->type == PAYLOAD_UPDATE_READ &&
70+
p->offset == static_cast<uint32_t>(offset) &&
71+
p->len == static_cast<uint32_t>(len);
72+
}
73+
5074
TEST_F(LibHothTest, payload_update_bad_image_test) {
5175
EXPECT_CALL(mock_, send(_, UsesCommand(kCmd), _))
5276
.WillRepeatedly(Return(LIBHOTH_OK));
@@ -496,3 +520,31 @@ TEST_F(LibHothTest, payload_update_erase_cmd_range_overflow_test) {
496520
EXPECT_EQ(libhoth_payload_update_erase(&hoth_dev_, kOffset, kSize),
497521
PAYLOAD_UPDATE_INVALID_ARGS);
498522
}
523+
524+
TEST_F(LibHothTest, payload_update_read_chunk_test) {
525+
uint8_t expected_data[] = {0x11, 0x22, 0x33, 0x44, 0x55};
526+
size_t offset = 0x100;
527+
size_t len = sizeof(expected_data);
528+
529+
EXPECT_CALL(mock_, send(_, IsReadRequest(offset, len), _))
530+
.WillOnce(Return(LIBHOTH_OK));
531+
EXPECT_CALL(mock_, receive)
532+
.WillOnce(DoAll(CopyResp(expected_data, len), Return(LIBHOTH_OK)));
533+
534+
std::string temp_path = testing::TempDir() + "libhoth_payload_read_XXXXXX";
535+
int fd = mkstemp(&temp_path[0]);
536+
ASSERT_GE(fd, 0);
537+
unlink(temp_path.c_str());
538+
struct FdGuard {
539+
int fd;
540+
~FdGuard() { close(fd); }
541+
} guard = {fd};
542+
543+
EXPECT_EQ(libhoth_payload_update_read_chunk(&hoth_dev_, fd, len, offset),
544+
PAYLOAD_UPDATE_OK);
545+
546+
ASSERT_EQ(lseek(fd, 0, SEEK_SET), 0);
547+
uint8_t actual_data[sizeof(expected_data)] = {0};
548+
ASSERT_EQ(read(fd, actual_data, len), static_cast<ssize_t>(len));
549+
EXPECT_THAT(actual_data, ::testing::ElementsAreArray(expected_data));
550+
}

0 commit comments

Comments
 (0)