1 /* 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 #ifndef SRC_TRACING_TEST_FAKE_PACKET_H_ 18 #define SRC_TRACING_TEST_FAKE_PACKET_H_ 19 20 #include <stdint.h> 21 22 #include <array> 23 #include <initializer_list> 24 #include <string> 25 #include <vector> 26 27 #include "perfetto/tracing/core/basic_types.h" 28 29 namespace perfetto { 30 31 class TraceBuffer; 32 33 class FakePacketFragment { 34 public: 35 // Generates a packet of given |size| with pseudo random payload. The |size| 36 // argument includes the size of the varint header. 37 FakePacketFragment(size_t size, char prefix); 38 FakePacketFragment(const void* payload, size_t payload_size); 39 void CopyInto(std::vector<uint8_t>* data) const; 40 size_t GetSizeHeader() const; 41 bool operator==(const FakePacketFragment& other) const; payload()42 const std::string& payload() const { return payload_; } 43 44 private: 45 std::array<uint8_t, 2> header_{}; 46 size_t header_size_ = 0; 47 std::string payload_; 48 }; 49 50 std::ostream& operator<<(std::ostream&, const FakePacketFragment&); 51 52 class FakeChunk { 53 public: 54 FakeChunk(TraceBuffer* t, ProducerID p, WriterID w, ChunkID c); 55 56 // Appends a packet of exactly |size| bytes (including the varint header 57 // that states the size of the packet itself. The payload of the packet is 58 // NOT a valid proto and is just filled with pseudo-random bytes generated 59 // from |seed|. 60 FakeChunk& AddPacket(size_t size, char seed, uint8_t packet_flag = 0); 61 62 // Appends a packet with the given raw content (including varint header). 63 FakeChunk& AddPacket(std::initializer_list<uint8_t>); 64 65 // Increments the number of packets in the chunk without adding new data. 66 FakeChunk& IncrementNumPackets(); 67 68 FakeChunk& ClearBytes(size_t offset, size_t len); 69 70 FakeChunk& SetUID(uid_t); 71 72 // Extends the size of the FakeChunk to |chunk_size| by 0-filling. 73 FakeChunk& PadTo(size_t chunk_size); 74 75 // Returns the full size of the chunk including the ChunkRecord header. 76 size_t CopyIntoTraceBuffer(bool chunk_complete = true); 77 78 private: 79 TraceBuffer* trace_buffer_; 80 ProducerID producer_id; 81 WriterID writer_id; 82 ChunkID chunk_id; 83 uint8_t flags = 0; 84 uint16_t num_packets = 0; 85 uid_t uid = kInvalidUid; 86 std::vector<uint8_t> data; 87 }; 88 89 } // namespace perfetto 90 91 #endif // SRC_TRACING_TEST_FAKE_PACKET_H_ 92