blob: 03f15f40a77cab84a070878e47fada311e754577 [file] [log] [blame]
Sami Kyostila32e0b542018-02-14 08:55:43 +00001/*
2 * Copyright (C) 2018 The Android Open Source Project
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 "src/tracing/core/packet_stream_validator.h"
18
19#include <string>
20
21#include "gtest/gtest.h"
22
23#include "perfetto/trace/trace_packet.pb.h"
24
25namespace perfetto {
26namespace {
27
28TEST(PacketStreamValidatorTest, NullPacket) {
29 std::string ser_buf;
Primiano Tucci16d1d632018-02-22 10:16:22 +000030 Slices seq;
Sami Kyostila32e0b542018-02-14 08:55:43 +000031 EXPECT_TRUE(PacketStreamValidator::Validate(seq));
32}
33
34TEST(PacketStreamValidatorTest, SimplePacket) {
35 protos::TracePacket proto;
36 proto.mutable_for_testing()->set_str("string field");
37 std::string ser_buf = proto.SerializeAsString();
38
Primiano Tucci16d1d632018-02-22 10:16:22 +000039 Slices seq;
Sami Kyostila32e0b542018-02-14 08:55:43 +000040 seq.emplace_back(&ser_buf[0], ser_buf.size());
41 EXPECT_TRUE(PacketStreamValidator::Validate(seq));
42}
43
44TEST(PacketStreamValidatorTest, ComplexPacket) {
45 protos::TracePacket proto;
46 proto.mutable_for_testing()->set_str("string field");
47 proto.mutable_ftrace_events()->set_cpu(0);
48 auto* ft = proto.mutable_ftrace_events()->add_event();
49 ft->set_pid(42);
50 ft->mutable_sched_switch()->set_prev_comm("tom");
51 ft->mutable_sched_switch()->set_prev_pid(123);
52 ft->mutable_sched_switch()->set_next_comm("jerry");
53 ft->mutable_sched_switch()->set_next_pid(456);
54 std::string ser_buf = proto.SerializeAsString();
55
Primiano Tucci16d1d632018-02-22 10:16:22 +000056 Slices seq;
Sami Kyostila32e0b542018-02-14 08:55:43 +000057 seq.emplace_back(&ser_buf[0], ser_buf.size());
58 EXPECT_TRUE(PacketStreamValidator::Validate(seq));
59}
60
61TEST(PacketStreamValidatorTest, SimplePacketWithUid) {
62 protos::TracePacket proto;
63 proto.set_trusted_uid(123);
64 std::string ser_buf = proto.SerializeAsString();
65
Primiano Tucci16d1d632018-02-22 10:16:22 +000066 Slices seq;
Sami Kyostila32e0b542018-02-14 08:55:43 +000067 seq.emplace_back(&ser_buf[0], ser_buf.size());
68 EXPECT_FALSE(PacketStreamValidator::Validate(seq));
69}
70
71TEST(PacketStreamValidatorTest, SimplePacketWithZeroUid) {
72 protos::TracePacket proto;
73 proto.set_trusted_uid(0);
74 std::string ser_buf = proto.SerializeAsString();
75
Primiano Tucci16d1d632018-02-22 10:16:22 +000076 Slices seq;
Sami Kyostila32e0b542018-02-14 08:55:43 +000077 seq.emplace_back(&ser_buf[0], ser_buf.size());
78 EXPECT_FALSE(PacketStreamValidator::Validate(seq));
79}
80
81TEST(PacketStreamValidatorTest, SimplePacketWithNegativeOneUid) {
82 protos::TracePacket proto;
83 proto.set_trusted_uid(-1);
84 std::string ser_buf = proto.SerializeAsString();
85
Primiano Tucci16d1d632018-02-22 10:16:22 +000086 Slices seq;
Sami Kyostila32e0b542018-02-14 08:55:43 +000087 seq.emplace_back(&ser_buf[0], ser_buf.size());
88 EXPECT_FALSE(PacketStreamValidator::Validate(seq));
89}
90
91TEST(PacketStreamValidatorTest, ComplexPacketWithUid) {
92 protos::TracePacket proto;
93 proto.mutable_for_testing()->set_str("string field");
94 proto.mutable_ftrace_events()->set_cpu(0);
95 auto* ft = proto.mutable_ftrace_events()->add_event();
96 ft->set_pid(42);
97 ft->mutable_sched_switch()->set_prev_comm("tom");
98 ft->mutable_sched_switch()->set_prev_pid(123);
99 ft->mutable_sched_switch()->set_next_comm("jerry");
100 ft->mutable_sched_switch()->set_next_pid(456);
101 proto.set_trusted_uid(123);
102 std::string ser_buf = proto.SerializeAsString();
103
Primiano Tucci16d1d632018-02-22 10:16:22 +0000104 Slices seq;
Sami Kyostila32e0b542018-02-14 08:55:43 +0000105 seq.emplace_back(&ser_buf[0], ser_buf.size());
106 EXPECT_FALSE(PacketStreamValidator::Validate(seq));
107}
108
109TEST(PacketStreamValidatorTest, FragmentedPacket) {
110 protos::TracePacket proto;
111 proto.mutable_for_testing()->set_str("string field");
112 proto.mutable_ftrace_events()->set_cpu(0);
113 auto* ft = proto.mutable_ftrace_events()->add_event();
114 ft->set_pid(42);
115 ft->mutable_sched_switch()->set_prev_comm("tom");
116 ft->mutable_sched_switch()->set_prev_pid(123);
117 ft->mutable_sched_switch()->set_next_comm("jerry");
118 ft->mutable_sched_switch()->set_next_pid(456);
119 std::string ser_buf = proto.SerializeAsString();
120
121 for (size_t i = 0; i < ser_buf.size(); i++) {
Primiano Tucci16d1d632018-02-22 10:16:22 +0000122 Slices seq;
Sami Kyostila32e0b542018-02-14 08:55:43 +0000123 seq.emplace_back(&ser_buf[0], i);
124 seq.emplace_back(&ser_buf[i], ser_buf.size() - i);
125 EXPECT_TRUE(PacketStreamValidator::Validate(seq));
126 }
127}
128
129TEST(PacketStreamValidatorTest, FragmentedPacketWithUid) {
130 protos::TracePacket proto;
131 proto.mutable_for_testing()->set_str("string field");
132 proto.set_trusted_uid(123);
133 proto.mutable_ftrace_events()->set_cpu(0);
134 auto* ft = proto.mutable_ftrace_events()->add_event();
135 ft->set_pid(42);
136 ft->mutable_sched_switch()->set_prev_comm("tom");
137 ft->mutable_sched_switch()->set_prev_pid(123);
138 ft->mutable_sched_switch()->set_next_comm("jerry");
139 ft->mutable_sched_switch()->set_next_pid(456);
140 proto.mutable_for_testing()->set_str("foo");
141 std::string ser_buf = proto.SerializeAsString();
142
143 for (size_t i = 0; i < ser_buf.size(); i++) {
Primiano Tucci16d1d632018-02-22 10:16:22 +0000144 Slices seq;
Sami Kyostila32e0b542018-02-14 08:55:43 +0000145 seq.emplace_back(&ser_buf[0], i);
146 seq.emplace_back(&ser_buf[i], ser_buf.size() - i);
147 EXPECT_FALSE(PacketStreamValidator::Validate(seq));
148 }
149}
150
151TEST(PacketStreamValidatorTest, TruncatedPacket) {
152 protos::TracePacket proto;
153 proto.mutable_for_testing()->set_str("string field");
154 std::string ser_buf = proto.SerializeAsString();
155
156 for (size_t i = 1; i < ser_buf.size(); i++) {
Primiano Tucci16d1d632018-02-22 10:16:22 +0000157 Slices seq;
Sami Kyostila32e0b542018-02-14 08:55:43 +0000158 seq.emplace_back(&ser_buf[0], i);
159 EXPECT_FALSE(PacketStreamValidator::Validate(seq));
160 }
161}
162
163TEST(PacketStreamValidatorTest, TrailingGarbage) {
164 protos::TracePacket proto;
165 proto.mutable_for_testing()->set_str("string field");
166 std::string ser_buf = proto.SerializeAsString();
167 ser_buf += "bike is short for bichael";
168
Primiano Tucci16d1d632018-02-22 10:16:22 +0000169 Slices seq;
Sami Kyostila32e0b542018-02-14 08:55:43 +0000170 seq.emplace_back(&ser_buf[0], ser_buf.size());
171 EXPECT_FALSE(PacketStreamValidator::Validate(seq));
172}
173
174} // namespace
175} // namespace perfetto