blob: 047f2b327f104eab5898e8e526f3c0ad0794193b [file] [log] [blame]
Lalit Maganti45172a82018-06-01 03:04:43 +01001/*
Lalit Magantic6bccda2018-06-25 11:05:24 +01002 * Copyright (C) 2018 The Android Open Source Project
Lalit Maganti45172a82018-06-01 03:04:43 +01003 *
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 Dearmanb7fa5442018-11-08 18:39:32 +000024#include "perfetto/protozero/scattered_stream_memory_delegate.h"
Lalit Maganti45172a82018-06-01 03:04:43 +010025
26namespace protozero {
27namespace {
28
29using ::testing::_;
30using ::testing::InSequence;
31using ::testing::Invoke;
Isabelle Tayloraa45fe02018-12-16 21:02:20 +000032using namespace proto_utils;
Lalit Maganti45172a82018-06-01 03:04:43 +010033
34TEST(ProtoDecoder, ReadString) {
35 Message message;
Hector Dearmanb7fa5442018-11-08 18:39:32 +000036 perfetto::ScatteredStreamMemoryDelegate delegate(512);
Lalit Maganti45172a82018-06-01 03:04:43 +010037 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 Eftevaag706b2882018-06-04 09:48:30 -070050 ASSERT_EQ(field.id, 1u);
Isabelle Tayloraa45fe02018-12-16 21:02:20 +000051 ASSERT_EQ(field.type, ProtoWireType::kLengthDelimited);
Lalit Magantidf3e9262018-06-04 17:45:00 +010052 ASSERT_EQ(field.length_limited.length, sizeof(kTestString) - 1);
Lalit Maganti45172a82018-06-01 03:04:43 +010053 for (size_t i = 0; i < sizeof(kTestString) - 1; i++) {
Lalit Magantidf3e9262018-06-04 17:45:00 +010054 ASSERT_EQ(field.length_limited.data[i], kTestString[i]);
Lalit Maganti45172a82018-06-01 03:04:43 +010055 }
56}
57
58TEST(ProtoDecoder, FixedData) {
59 struct FieldExpectation {
60 const char* encoded;
61 size_t encoded_size;
62 uint32_t id;
Isabelle Tayloraa45fe02018-12-16 21:02:20 +000063 ProtoWireType type;
Lalit Maganti45172a82018-06-01 03:04:43 +010064 uint64_t int_value;
65 };
66
67 const FieldExpectation kFieldExpectations[] = {
Isabelle Tayloraa45fe02018-12-16 21:02:20 +000068 {"\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 Maganti45172a82018-06-01 03:04:43 +010083 {"\x92\x4D\x83\x01|abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzab"
84 "cdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstu"
85 "vwx",
Isabelle Tayloraa45fe02018-12-16 21:02:20 +000086 135, 1234, ProtoWireType::kLengthDelimited, 131},
Lalit Maganti45172a82018-06-01 03:04:43 +010087 };
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 Tayloraa45fe02018-12-16 21:02:20 +000098 if (field.type == ProtoWireType::kLengthDelimited) {
Lalit Magantidf3e9262018-06-04 17:45:00 +010099 ASSERT_EQ(exp.int_value, field.length_limited.length);
Lalit Maganti45172a82018-06-01 03:04:43 +0100100 } else {
101 ASSERT_EQ(exp.int_value, field.int_value);
102 }
103 }
104}
105
106} // namespace
107} // namespace protozero