From 82b7fa0c880bd1c3d57a788625013a3cbbdf4172 Mon Sep 17 00:00:00 2001 From: mukesh agrawal Date: Wed, 26 Oct 2016 13:13:10 -0700 Subject: protocol: add setters for Command - Add setters for the currently used fields of protocol::Command, so that we can use chaining to initialize a Command. - Update the command processor unittests, to make use of this ability. Bug: 32327379 Test: ./runtests.sh (on angler) Change-Id: I2065ecb2a0bb379e944b370f470d1c568f5b9475 --- protocol.h | 10 ++++++++++ tests/command_processor_unittest.cpp | 36 ++++++++++++++++++------------------ 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/protocol.h b/protocol.h index 369a9e5..527e5ff 100644 --- a/protocol.h +++ b/protocol.h @@ -51,6 +51,16 @@ enum class MessageSeverity : uint8_t { }; struct Command { + Command& set_opcode(Opcode new_opcode) { + opcode = new_opcode; + return *this; + } + + Command& set_payload_len(uint16_t new_payload_len) { + payload_len = new_payload_len; + return *this; + } + uint64_t src_boottime_nsec; // For latency measurement. // For drop detection. Sequence numbers are meaningful only within // the context of a single tag. (This is to minimize synchronization diff --git a/tests/command_processor_unittest.cpp b/tests/command_processor_unittest.cpp index fcf9fbc..a4ec552 100644 --- a/tests/command_processor_unittest.cpp +++ b/tests/command_processor_unittest.cpp @@ -97,15 +97,16 @@ class CommandProcessorTest : public ::testing::Test { uint16_t, 0, kMaxDataLength); ascii_message_header.severity = protocol::MessageSeverity::kError; - protocol::Command command{}; - constexpr auto kMaxPayloadLength = GetMaxVal(command.payload_len); - size_t payload_length = sizeof(ascii_message_header) + tag.length() + - message.length() + command_payload_len_adjustment; - EXPECT_TRUE(payload_length <= kMaxPayloadLength); - command.opcode = protocol::Opcode::kWriteAsciiMessage; - command.payload_len = - SAFELY_CLAMP(payload_length, uint16_t, 0, kMaxPayloadLength); - + const size_t payload_len = sizeof(ascii_message_header) + tag.length() + + message.length() + + command_payload_len_adjustment; + const auto& command = + protocol::Command() + .set_opcode(protocol::Opcode::kWriteAsciiMessage) + .set_payload_len(SAFELY_CLAMP( + payload_len, uint16_t, 0, + GetMaxVal())); + EXPECT_EQ(payload_len, command.payload_len); return CommandBuffer() .AppendOrDie(&command, sizeof(command)) .AppendOrDie(&ascii_message_header, sizeof(ascii_message_header)) @@ -139,10 +140,9 @@ class CommandProcessorTest : public ::testing::Test { } bool SendDumpBuffers() { - protocol::Command command{}; - command.opcode = protocol::Opcode::kDumpBuffers; - command.payload_len = 0; - + const auto& command = protocol::Command() + .set_opcode(protocol::Opcode::kDumpBuffers) + .set_payload_len(0); const auto& buf = CommandBuffer().AppendOrDie(&command, sizeof(command)); constexpr int kFakeFd = 100; return command_processor_->ProcessCommand(buf.data(), buf.size(), kFakeFd); @@ -217,11 +217,11 @@ TEST_F(CommandProcessorTest, ProcessCommandInvalidOpcodeReturnsFailure) { using opcode_integral_t = std::underlying_type::type; constexpr auto invalid_opcode = GetMaxVal(); - protocol::Command command{}; - command.opcode = local_utils::CopyFromBufferOrDie( - &invalid_opcode, sizeof(invalid_opcode)); - command.payload_len = 0; - + const auto& command = + protocol::Command() + .set_opcode(local_utils::CopyFromBufferOrDie( + &invalid_opcode, sizeof(invalid_opcode))) + .set_payload_len(0); const auto& buf = CommandBuffer().AppendOrDie(&command, sizeof(command)); constexpr int kFakeFd = 100; EXPECT_FALSE( -- cgit v1.2.3