Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 "perfetto/tracing/core/trace_packet.h" |
| 18 | |
| 19 | #include <string> |
| 20 | |
| 21 | #include "gtest/gtest.h" |
| 22 | |
Primiano Tucci | 20b760c | 2018-01-19 12:36:12 +0000 | [diff] [blame] | 23 | #include "perfetto/trace/trace_packet.pb.h" |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 24 | |
| 25 | namespace perfetto { |
| 26 | namespace { |
| 27 | |
| 28 | TEST(TracePacketTest, Simple) { |
| 29 | protos::TracePacket proto; |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 30 | proto.mutable_for_testing()->set_str("string field"); |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 31 | std::string ser_buf = proto.SerializeAsString(); |
| 32 | TracePacket tp; |
Primiano Tucci | 09db827 | 2018-03-08 17:47:47 +0000 | [diff] [blame^] | 33 | tp.AddSlice(ser_buf.data(), ser_buf.size()); |
| 34 | auto slice = tp.slices().begin(); |
| 35 | ASSERT_NE(tp.slices().end(), slice); |
Primiano Tucci | 16d1d63 | 2018-02-22 10:16:22 +0000 | [diff] [blame] | 36 | ASSERT_EQ(ser_buf.data(), slice->start); |
| 37 | ASSERT_EQ(ser_buf.size(), slice->size); |
Primiano Tucci | 09db827 | 2018-03-08 17:47:47 +0000 | [diff] [blame^] | 38 | ASSERT_EQ(tp.slices().end(), ++slice); |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 39 | |
| 40 | ASSERT_TRUE(tp.Decode()); |
| 41 | ASSERT_TRUE(tp.Decode()); // Decode() should be idempotent. |
| 42 | ASSERT_NE(nullptr, tp.operator->()); |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 43 | ASSERT_EQ(proto.for_testing().str(), tp->for_testing().str()); |
| 44 | ASSERT_EQ(proto.for_testing().str(), (*tp).for_testing().str()); |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 45 | |
| 46 | // Check move operators. |
| 47 | TracePacket moved_tp(std::move(tp)); |
| 48 | ASSERT_NE(nullptr, moved_tp.operator->()); |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 49 | ASSERT_EQ(proto.for_testing().str(), moved_tp->for_testing().str()); |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 50 | |
| 51 | TracePacket moved_tp_2; |
| 52 | moved_tp_2 = std::move(moved_tp); |
| 53 | ASSERT_NE(nullptr, moved_tp_2.operator->()); |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 54 | ASSERT_EQ(proto.for_testing().str(), moved_tp_2->for_testing().str()); |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Primiano Tucci | 16d1d63 | 2018-02-22 10:16:22 +0000 | [diff] [blame] | 57 | TEST(TracePacketTest, Sliced) { |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 58 | protos::TracePacket proto; |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 59 | proto.mutable_for_testing()->set_str( |
| 60 | "this is an arbitrarily long string ........................"); |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 61 | std::string ser_buf = proto.SerializeAsString(); |
| 62 | TracePacket tp; |
Primiano Tucci | 16d1d63 | 2018-02-22 10:16:22 +0000 | [diff] [blame] | 63 | tp.AddSlice({ser_buf.data(), 3}); |
| 64 | tp.AddSlice({ser_buf.data() + 3, 5}); |
| 65 | tp.AddSlice({ser_buf.data() + 3 + 5, ser_buf.size() - 3 - 5}); |
Primiano Tucci | af429f9 | 2017-12-19 01:51:50 +0100 | [diff] [blame] | 66 | ASSERT_EQ(ser_buf.size(), tp.size()); |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 67 | |
Primiano Tucci | 09db827 | 2018-03-08 17:47:47 +0000 | [diff] [blame^] | 68 | auto slice = tp.slices().begin(); |
| 69 | ASSERT_NE(tp.slices().end(), slice); |
Primiano Tucci | 16d1d63 | 2018-02-22 10:16:22 +0000 | [diff] [blame] | 70 | ASSERT_EQ(ser_buf.data(), slice->start); |
| 71 | ASSERT_EQ(3u, slice->size); |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 72 | |
Primiano Tucci | 09db827 | 2018-03-08 17:47:47 +0000 | [diff] [blame^] | 73 | ASSERT_NE(tp.slices().end(), ++slice); |
Primiano Tucci | 16d1d63 | 2018-02-22 10:16:22 +0000 | [diff] [blame] | 74 | ASSERT_EQ(ser_buf.data() + 3, slice->start); |
| 75 | ASSERT_EQ(5u, slice->size); |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 76 | |
Primiano Tucci | 09db827 | 2018-03-08 17:47:47 +0000 | [diff] [blame^] | 77 | ASSERT_NE(tp.slices().end(), ++slice); |
Primiano Tucci | 16d1d63 | 2018-02-22 10:16:22 +0000 | [diff] [blame] | 78 | ASSERT_EQ(ser_buf.data() + 3 + 5, slice->start); |
| 79 | ASSERT_EQ(ser_buf.size() - 3 - 5, slice->size); |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 80 | |
Primiano Tucci | 09db827 | 2018-03-08 17:47:47 +0000 | [diff] [blame^] | 81 | ASSERT_EQ(tp.slices().end(), ++slice); |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 82 | |
| 83 | ASSERT_TRUE(tp.Decode()); |
| 84 | ASSERT_NE(nullptr, tp.operator->()); |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 85 | ASSERT_EQ(proto.for_testing().str(), tp->for_testing().str()); |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | TEST(TracePacketTest, Corrupted) { |
| 89 | protos::TracePacket proto; |
Sami Kyostila | 32e0b54 | 2018-02-14 08:55:43 +0000 | [diff] [blame] | 90 | proto.mutable_for_testing()->set_str("string field"); |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 91 | std::string ser_buf = proto.SerializeAsString(); |
| 92 | TracePacket tp; |
Primiano Tucci | 16d1d63 | 2018-02-22 10:16:22 +0000 | [diff] [blame] | 93 | tp.AddSlice({ser_buf.data(), ser_buf.size() - 2}); // corrupted. |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 94 | ASSERT_FALSE(tp.Decode()); |
Primiano Tucci | 42e2de1 | 2017-12-07 16:46:04 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | } // namespace |
| 98 | } // namespace perfetto |