Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion mw/i2c_service/service/i2c_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ core::ErrorCode I2CService::Init(std::shared_ptr<core::i2c::II2CDriver> i2c,

std::optional<std::vector<uint8_t>> I2CService::ReadWrite(
const std::vector<uint8_t> &payload, std::shared_ptr<i2c::Header> headerPtr) {
if (payload.size()%2 != 0) {
// payload: register, number of bytes to read - exactly two bytes (an empty payload used to pass %2 and was read
// out of bounds)
if (payload.size() != 2) {
i2c_logger_.LogWarn() << "Read request rejected: invalid payload size " << payload.size();
return std::nullopt;
}
Expand Down
14 changes: 14 additions & 0 deletions mw/i2c_service/tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,17 @@ cc_test(
"//mw/i2c_service/controller:i2c_controller",
],
)

cc_test(
name = "i2c_service_readwrite_test",
copts = ["-D_GLIBCXX_ASSERTIONS"], # payload[0] on an empty vector aborts instead of silent UB
srcs = [
"test_service_readwrite.cpp",
],
visibility = ["//visibility:public"],
deps=[
"@com_google_googletest//:gtest_main",
"//mw/i2c_service/service:i2c_lib",
"//communication-core/sockets/mock:mock_socket",
],
)
83 changes: 83 additions & 0 deletions mw/i2c_service/tests/test_service_readwrite.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* @file test_service_readwrite.cpp
* @brief Unit test: I2CService::ReadWrite validates the payload size (F-31)
*
* @copyright Copyright (c) 2026
*/
#include <memory>
#include <optional>
#include <vector>

#include "gtest/gtest.h"

// white-box access: I2CService is final and its ReadWrite/Init are protected
#define private public // NOLINT
#define protected public // NOLINT
#include "mw/i2c_service/service/i2c_service.h"
#undef protected
#undef private
#include "communication-core/sockets/mock/mockSocket.h"

namespace {
class FakeDriver : public srp::core::i2c::II2CDriver {
public:
int read_write_calls = 0;
uint8_t last_reg = 0;
uint8_t last_size = 0;
srp::core::ErrorCode Init() override { return srp::core::ErrorCode::kOk; }
srp::core::ErrorCode Ioctl(const uint8_t, const uint16_t) override { return srp::core::ErrorCode::kOk; }
srp::core::ErrorCode Write(const std::vector<uint8_t>&) override { return srp::core::ErrorCode::kOk; }
srp::core::ErrorCode Write(const uint8_t&) override { return srp::core::ErrorCode::kOk; }
std::optional<std::vector<uint8_t>> Read(const uint8_t) override { return std::nullopt; }
srp::core::ErrorCode PageWrite(std::vector<uint8_t>) override { return srp::core::ErrorCode::kOk; }
std::optional<std::vector<uint8_t>> ReadWrite(const uint8_t& reg, const uint8_t size) override {
++read_write_calls;
last_reg = reg;
last_size = size;
return std::vector<uint8_t>(size, 0xAB);
}
std::optional<std::vector<uint8_t>> ReadWrite(const std::vector<uint8_t>&, const uint8_t) override {
return std::nullopt;
}
};

class Wrapper {
public:
srp::mw::I2CService service;
void TestInit(std::shared_ptr<srp::core::i2c::II2CDriver> d) {
service.Init(std::move(d), std::make_unique<MockStreamSocket>());
}
std::optional<std::vector<uint8_t>> Rw(const std::vector<uint8_t>& payload) {
return service.ReadWrite(payload, nullptr);
}
};
} // namespace

TEST(I2cServiceReadWrite, EmptyPayloadIsRejectedWithoutTouchingTheDriver) {
auto drv = std::make_shared<FakeDriver>();
Wrapper w;
w.TestInit(drv);
EXPECT_FALSE(w.Rw({}).has_value()) << "F-31: an empty payload was accepted";
EXPECT_EQ(drv->read_write_calls, 0);
}

TEST(I2cServiceReadWrite, WrongSizesAreRejected) {
auto drv = std::make_shared<FakeDriver>();
Wrapper w;
w.TestInit(drv);
EXPECT_FALSE(w.Rw({0x10}).has_value());
EXPECT_FALSE(w.Rw({0x10, 4, 0x20}).has_value());
EXPECT_FALSE(w.Rw({0x10, 4, 0x20, 4}).has_value());
EXPECT_EQ(drv->read_write_calls, 0);
}

TEST(I2cServiceReadWrite, RegisterAndSizeAreReadFromTheTwoBytes) {
auto drv = std::make_shared<FakeDriver>();
Wrapper w;
w.TestInit(drv);
auto res = w.Rw({0x10, 4});
ASSERT_TRUE(res.has_value());
EXPECT_EQ(res->size(), 4U);
EXPECT_EQ(drv->last_reg, 0x10);
EXPECT_EQ(drv->last_size, 4);
}