Prashanth Swaminathan | 8015cec | 2021-06-04 10:00:40 -0700 | [diff] [blame] | 1 | // Copyright 2021 The Pigweed Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
| 14 | |
| 15 | #include "pw_log/proto_utils.h" |
| 16 | |
| 17 | #include "gtest/gtest.h" |
| 18 | #include "pw_protobuf/decoder.h" |
| 19 | |
| 20 | namespace pw::log { |
| 21 | |
| 22 | void VerifyLogEntry(pw::protobuf::Decoder& entry_decoder, |
| 23 | pw::log_tokenized::Metadata expected_metadata, |
| 24 | ConstByteSpan expected_tokenized_data, |
| 25 | const int64_t expected_timestamp) { |
| 26 | ConstByteSpan tokenized_data; |
| 27 | EXPECT_TRUE(entry_decoder.Next().ok()); // message [tokenized] |
| 28 | EXPECT_EQ(1U, entry_decoder.FieldNumber()); |
| 29 | EXPECT_TRUE(entry_decoder.ReadBytes(&tokenized_data).ok()); |
| 30 | EXPECT_TRUE(std::memcmp(tokenized_data.begin(), |
| 31 | expected_tokenized_data.begin(), |
| 32 | expected_tokenized_data.size()) == 0); |
| 33 | |
| 34 | uint32_t line_level; |
| 35 | EXPECT_TRUE(entry_decoder.Next().ok()); // line_level |
| 36 | EXPECT_EQ(2U, entry_decoder.FieldNumber()); |
| 37 | EXPECT_TRUE(entry_decoder.ReadUint32(&line_level).ok()); |
| 38 | EXPECT_EQ(expected_metadata.level(), line_level & PW_LOG_LEVEL_BITMASK); |
| 39 | EXPECT_EQ(expected_metadata.line_number(), |
| 40 | (line_level & ~PW_LOG_LEVEL_BITMASK) >> PW_LOG_LEVEL_BITS); |
| 41 | |
| 42 | if (expected_metadata.flags() != 0) { |
| 43 | uint32_t flags; |
| 44 | EXPECT_TRUE(entry_decoder.Next().ok()); // flags |
| 45 | EXPECT_EQ(3U, entry_decoder.FieldNumber()); |
| 46 | EXPECT_TRUE(entry_decoder.ReadUint32(&flags).ok()); |
| 47 | EXPECT_EQ(expected_metadata.flags(), flags); |
| 48 | } |
| 49 | |
| 50 | int64_t timestamp; |
| 51 | EXPECT_TRUE(entry_decoder.Next().ok()); // timestamp |
| 52 | EXPECT_EQ(4U, entry_decoder.FieldNumber()); |
| 53 | EXPECT_TRUE(entry_decoder.ReadInt64(×tamp).ok()); |
| 54 | EXPECT_EQ(expected_timestamp, timestamp); |
| 55 | } |
| 56 | |
| 57 | TEST(UtilsTest, EncodeTokenizedLog) { |
| 58 | constexpr std::byte kTokenizedData[1] = {(std::byte)0x01}; |
| 59 | constexpr int64_t kExpectedTimestamp = 1; |
| 60 | std::byte encode_buffer[32]; |
| 61 | |
| 62 | pw::log_tokenized::Metadata metadata = |
| 63 | pw::log_tokenized::Metadata::Set<1, 2, 3, 4>(); |
| 64 | |
| 65 | Result<ConstByteSpan> result = EncodeTokenizedLog( |
| 66 | metadata, kTokenizedData, kExpectedTimestamp, encode_buffer); |
| 67 | EXPECT_TRUE(result.ok()); |
| 68 | |
| 69 | pw::protobuf::Decoder log_decoder(result.value()); |
| 70 | VerifyLogEntry(log_decoder, metadata, kTokenizedData, kExpectedTimestamp); |
| 71 | |
| 72 | result = EncodeTokenizedLog(metadata, |
| 73 | reinterpret_cast<const uint8_t*>(kTokenizedData), |
| 74 | sizeof(kTokenizedData), |
| 75 | kExpectedTimestamp, |
| 76 | encode_buffer); |
| 77 | EXPECT_TRUE(result.ok()); |
| 78 | |
| 79 | log_decoder.Reset(result.value()); |
| 80 | VerifyLogEntry(log_decoder, metadata, kTokenizedData, kExpectedTimestamp); |
| 81 | } |
| 82 | |
| 83 | TEST(UtilsTest, EncodeTokenizedLog_EmptyFlags) { |
| 84 | constexpr std::byte kTokenizedData[1] = {(std::byte)0x01}; |
| 85 | constexpr int64_t kExpectedTimestamp = 1; |
| 86 | std::byte encode_buffer[32]; |
| 87 | |
| 88 | // Create an empty flags set. |
| 89 | pw::log_tokenized::Metadata metadata = |
| 90 | pw::log_tokenized::Metadata::Set<1, 2, 0, 4>(); |
| 91 | |
| 92 | Result<ConstByteSpan> result = EncodeTokenizedLog( |
| 93 | metadata, kTokenizedData, kExpectedTimestamp, encode_buffer); |
| 94 | EXPECT_TRUE(result.ok()); |
| 95 | |
| 96 | pw::protobuf::Decoder log_decoder(result.value()); |
| 97 | VerifyLogEntry(log_decoder, metadata, kTokenizedData, kExpectedTimestamp); |
| 98 | } |
| 99 | |
| 100 | TEST(UtilsTest, EncodeTokenizedLog_InsufficientSpace) { |
| 101 | constexpr std::byte kTokenizedData[1] = {(std::byte)0x01}; |
| 102 | constexpr int64_t kExpectedTimestamp = 1; |
| 103 | std::byte encode_buffer[1]; |
| 104 | |
| 105 | pw::log_tokenized::Metadata metadata = |
| 106 | pw::log_tokenized::Metadata::Set<1, 2, 3, 4>(); |
| 107 | |
| 108 | Result<ConstByteSpan> result = EncodeTokenizedLog( |
| 109 | metadata, kTokenizedData, kExpectedTimestamp, encode_buffer); |
| 110 | EXPECT_TRUE(result.status().IsResourceExhausted()); |
| 111 | } |
| 112 | |
| 113 | } // namespace pw::log |