diff options
author | mukesh agrawal <quiche@google.com> | 2016-11-15 00:18:06 +0000 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2016-11-15 00:18:06 +0000 |
commit | 7c5cfa03261116ed7a36101d44906306707df6a8 (patch) | |
tree | 8c48eca4d8842d1d043fcec4bdf8370bb496657d | |
parent | c14a744a8bb84d1c739bcbe6df517a99fbbbc1a0 (diff) | |
parent | 14f24797c346d72a2fba0e10a411c31c09030355 (diff) | |
download | wifilogd-7c5cfa03261116ed7a36101d44906306707df6a8.tar.gz |
protocol: add setters for Command am: 82b7fa0c88 am: 2b4486e5c9 am: efa3e45855
am: 14f24797c3
Change-Id: Ide962a16e0027db910c8b5b997eae39425a7ce17
-rw-r--r-- | protocol.h | 10 | ||||
-rw-r--r-- | tests/command_processor_unittest.cpp | 36 |
2 files changed, 28 insertions, 18 deletions
@@ -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<decltype(protocol::Command::payload_len)>())); + 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<opcode_enum_t>::type; constexpr auto invalid_opcode = GetMaxVal<opcode_integral_t>(); - protocol::Command command{}; - command.opcode = local_utils::CopyFromBufferOrDie<opcode_enum_t>( - &invalid_opcode, sizeof(invalid_opcode)); - command.payload_len = 0; - + const auto& command = + protocol::Command() + .set_opcode(local_utils::CopyFromBufferOrDie<opcode_enum_t>( + &invalid_opcode, sizeof(invalid_opcode))) + .set_payload_len(0); const auto& buf = CommandBuffer().AppendOrDie(&command, sizeof(command)); constexpr int kFakeFd = 100; EXPECT_FALSE( |