From 0df78aa33caf6e5e760db53510d9af6aa5e8e242 Mon Sep 17 00:00:00 2001 From: mukesh agrawal Date: Wed, 26 Oct 2016 13:21:23 -0700 Subject: protocol: add setters for AsciiMessage - Add setters for the fields of protocol::AsciiMessage, so that we can use chaining to initialize an AsciiMessage. - Update the command processor unittests, to make use of this ability. Note that this CL introduces a behavioral change. Previously, the EXPECTations in BuildAsciiMessageCommandWithAdjustments were based on the unadjusted size of the ASCII message. After this CL, the relevant EXPECTations are based on the adjusted size. This is intentional, as our goal is to verify that the AsciiMessage we construct has the final (adjusted) size that the test case would intend. Bug: 32327379 Test: ./runtests.sh (on angler) Change-Id: Ic568ae663de713f31d745635d278089e25359693 --- protocol.h | 15 +++++++++++++++ tests/command_processor_unittest.cpp | 27 +++++++++++++++------------ 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/protocol.h b/protocol.h index 527e5ff..d082850 100644 --- a/protocol.h +++ b/protocol.h @@ -73,6 +73,21 @@ struct Command { }; struct AsciiMessage { // Old-style log messages. + AsciiMessage& set_data_len(uint16_t new_data_len) { + data_len = new_data_len; + return *this; + } + + AsciiMessage& set_tag_len(uint8_t new_tag_len) { + tag_len = new_tag_len; + return *this; + } + + AsciiMessage& set_severity(MessageSeverity new_severity) { + severity = new_severity; + return *this; + } + uint16_t data_len; uint8_t tag_len; MessageSeverity severity; diff --git a/tests/command_processor_unittest.cpp b/tests/command_processor_unittest.cpp index a4ec552..ede5cb0 100644 --- a/tests/command_processor_unittest.cpp +++ b/tests/command_processor_unittest.cpp @@ -84,18 +84,21 @@ class CommandProcessorTest : public ::testing::Test { ssize_t command_payload_len_adjustment, ssize_t ascii_message_tag_len_adjustment, ssize_t ascii_message_data_len_adjustment) { - protocol::AsciiMessage ascii_message_header; - constexpr auto kMaxTagLength = GetMaxVal(ascii_message_header.tag_len); - constexpr auto kMaxDataLength = GetMaxVal(ascii_message_header.data_len); - EXPECT_TRUE(tag.length() <= kMaxTagLength); - EXPECT_TRUE(message.length() <= kMaxDataLength); - ascii_message_header.tag_len = - SAFELY_CLAMP(tag.length() + ascii_message_tag_len_adjustment, uint8_t, - 0, kMaxTagLength); - ascii_message_header.data_len = - SAFELY_CLAMP(message.length() + ascii_message_data_len_adjustment, - uint16_t, 0, kMaxDataLength); - ascii_message_header.severity = protocol::MessageSeverity::kError; + const size_t adjusted_tag_len = + tag.length() + ascii_message_tag_len_adjustment; + const size_t adjusted_data_len = + message.length() + ascii_message_data_len_adjustment; + const auto& ascii_message_header = + protocol::AsciiMessage() + .set_tag_len(SAFELY_CLAMP( + adjusted_tag_len, uint8_t, 0, + GetMaxVal())) + .set_data_len(SAFELY_CLAMP( + adjusted_data_len, uint16_t, 0, + GetMaxVal())) + .set_severity(protocol::MessageSeverity::kError); + EXPECT_EQ(adjusted_tag_len, ascii_message_header.tag_len); + EXPECT_EQ(adjusted_data_len, ascii_message_header.data_len); const size_t payload_len = sizeof(ascii_message_header) + tag.length() + message.length() + -- cgit v1.2.3