Lalit Maganti | 45172a8 | 2018-06-01 03:04:43 +0100 | [diff] [blame] | 1 | /* |
Lalit Maganti | c6bccda | 2018-06-25 11:05:24 +0100 | [diff] [blame] | 2 | * Copyright (C) 2018 The Android Open Source Project |
Lalit Maganti | 45172a8 | 2018-06-01 03:04:43 +0100 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "perfetto/protozero/proto_decoder.h" |
| 18 | |
| 19 | #include "gmock/gmock.h" |
| 20 | #include "gtest/gtest.h" |
| 21 | #include "perfetto/base/utils.h" |
| 22 | #include "perfetto/protozero/message.h" |
| 23 | #include "perfetto/protozero/proto_utils.h" |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 24 | #include "perfetto/protozero/scattered_stream_memory_delegate.h" |
Lalit Maganti | 45172a8 | 2018-06-01 03:04:43 +0100 | [diff] [blame] | 25 | |
| 26 | namespace protozero { |
| 27 | namespace { |
| 28 | |
| 29 | using ::testing::_; |
| 30 | using ::testing::InSequence; |
| 31 | using ::testing::Invoke; |
Isabelle Taylor | aa45fe0 | 2018-12-16 21:02:20 +0000 | [diff] [blame^] | 32 | using namespace proto_utils; |
Lalit Maganti | 45172a8 | 2018-06-01 03:04:43 +0100 | [diff] [blame] | 33 | |
| 34 | TEST(ProtoDecoder, ReadString) { |
| 35 | Message message; |
Hector Dearman | b7fa544 | 2018-11-08 18:39:32 +0000 | [diff] [blame] | 36 | perfetto::ScatteredStreamMemoryDelegate delegate(512); |
Lalit Maganti | 45172a8 | 2018-06-01 03:04:43 +0100 | [diff] [blame] | 37 | ScatteredStreamWriter writer(&delegate); |
| 38 | delegate.set_writer(&writer); |
| 39 | message.Reset(&writer); |
| 40 | |
| 41 | static constexpr char kTestString[] = "test"; |
| 42 | message.AppendString(1, kTestString); |
| 43 | |
| 44 | uint8_t* data = delegate.chunks()[0].get(); |
| 45 | uint64_t bytes_used = 512 - writer.bytes_available(); |
| 46 | |
| 47 | ProtoDecoder decoder(data, bytes_used); |
| 48 | ProtoDecoder::Field field = decoder.ReadField(); |
| 49 | |
Oystein Eftevaag | 706b288 | 2018-06-04 09:48:30 -0700 | [diff] [blame] | 50 | ASSERT_EQ(field.id, 1u); |
Isabelle Taylor | aa45fe0 | 2018-12-16 21:02:20 +0000 | [diff] [blame^] | 51 | ASSERT_EQ(field.type, ProtoWireType::kLengthDelimited); |
Lalit Maganti | df3e926 | 2018-06-04 17:45:00 +0100 | [diff] [blame] | 52 | ASSERT_EQ(field.length_limited.length, sizeof(kTestString) - 1); |
Lalit Maganti | 45172a8 | 2018-06-01 03:04:43 +0100 | [diff] [blame] | 53 | for (size_t i = 0; i < sizeof(kTestString) - 1; i++) { |
Lalit Maganti | df3e926 | 2018-06-04 17:45:00 +0100 | [diff] [blame] | 54 | ASSERT_EQ(field.length_limited.data[i], kTestString[i]); |
Lalit Maganti | 45172a8 | 2018-06-01 03:04:43 +0100 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
| 58 | TEST(ProtoDecoder, FixedData) { |
| 59 | struct FieldExpectation { |
| 60 | const char* encoded; |
| 61 | size_t encoded_size; |
| 62 | uint32_t id; |
Isabelle Taylor | aa45fe0 | 2018-12-16 21:02:20 +0000 | [diff] [blame^] | 63 | ProtoWireType type; |
Lalit Maganti | 45172a8 | 2018-06-01 03:04:43 +0100 | [diff] [blame] | 64 | uint64_t int_value; |
| 65 | }; |
| 66 | |
| 67 | const FieldExpectation kFieldExpectations[] = { |
Isabelle Taylor | aa45fe0 | 2018-12-16 21:02:20 +0000 | [diff] [blame^] | 68 | {"\x08\x00", 2, 1, ProtoWireType::kVarInt, 0}, |
| 69 | {"\x08\x42", 2, 1, ProtoWireType::kVarInt, 0x42}, |
| 70 | {"\xF8\x07\x42", 3, 127, ProtoWireType::kVarInt, 0x42}, |
| 71 | {"\x90\x4D\xFF\xFF\xFF\xFF\x0F", 7, 1234, ProtoWireType::kVarInt, |
| 72 | 0xFFFFFFFF}, |
| 73 | {"\x7D\x42\x00\x00\x00", 5, 15, ProtoWireType::kFixed32, 0x42}, |
| 74 | {"\x95\x4D\x78\x56\x34\x12", 6, 1234, ProtoWireType::kFixed32, |
| 75 | 0x12345678}, |
| 76 | {"\x79\x42\x00\x00\x00\x00\x00\x00\x00", 9, 15, ProtoWireType::kFixed64, |
| 77 | 0x42}, |
| 78 | {"\x91\x4D\x08\x07\x06\x05\x04\x03\x02\x01", 10, 1234, |
| 79 | ProtoWireType::kFixed64, 0x0102030405060708}, |
| 80 | {"\x0A\x00", 2, 1, ProtoWireType::kLengthDelimited, 0}, |
| 81 | {"\x0A\x04|abc", 6, 1, ProtoWireType::kLengthDelimited, 4}, |
| 82 | {"\x92\x4D\x04|abc", 7, 1234, ProtoWireType::kLengthDelimited, 4}, |
Lalit Maganti | 45172a8 | 2018-06-01 03:04:43 +0100 | [diff] [blame] | 83 | {"\x92\x4D\x83\x01|abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzab" |
| 84 | "cdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstu" |
| 85 | "vwx", |
Isabelle Taylor | aa45fe0 | 2018-12-16 21:02:20 +0000 | [diff] [blame^] | 86 | 135, 1234, ProtoWireType::kLengthDelimited, 131}, |
Lalit Maganti | 45172a8 | 2018-06-01 03:04:43 +0100 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | for (size_t i = 0; i < perfetto::base::ArraySize(kFieldExpectations); ++i) { |
| 90 | const FieldExpectation& exp = kFieldExpectations[i]; |
| 91 | ProtoDecoder decoder(reinterpret_cast<const uint8_t*>(exp.encoded), |
| 92 | exp.encoded_size); |
| 93 | |
| 94 | ProtoDecoder::Field field = decoder.ReadField(); |
| 95 | ASSERT_EQ(exp.id, field.id); |
| 96 | ASSERT_EQ(exp.type, field.type); |
| 97 | |
Isabelle Taylor | aa45fe0 | 2018-12-16 21:02:20 +0000 | [diff] [blame^] | 98 | if (field.type == ProtoWireType::kLengthDelimited) { |
Lalit Maganti | df3e926 | 2018-06-04 17:45:00 +0100 | [diff] [blame] | 99 | ASSERT_EQ(exp.int_value, field.length_limited.length); |
Lalit Maganti | 45172a8 | 2018-06-01 03:04:43 +0100 | [diff] [blame] | 100 | } else { |
| 101 | ASSERT_EQ(exp.int_value, field.int_value); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | } // namespace |
| 107 | } // namespace protozero |