blob: 8003c4cd5959d9f24652b6252deb6a46ba67e18c [file] [log] [blame]
Primiano Tucci5c599012018-03-01 17:52:07 +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#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
29namespace perfetto {
30
Hector Dearman6214c8f2018-03-27 16:16:22 +010031class TraceBuffer;
Primiano Tucci5c599012018-03-01 17:52:07 +000032
33class 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;
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
50std::ostream& operator<<(std::ostream&, const FakePacketFragment&);
51
52class FakeChunk {
53 public:
Hector Dearman6214c8f2018-03-27 16:16:22 +010054 FakeChunk(TraceBuffer* t, ProducerID p, WriterID w, ChunkID c);
Primiano Tucci5c599012018-03-01 17:52:07 +000055
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
Primiano Tucci7b14f652018-04-06 12:16:34 +020065 // Increments the number of packets in the chunk without adding new data.
66 FakeChunk& IncrementNumPackets();
67
Primiano Tucci5c599012018-03-01 17:52:07 +000068 FakeChunk& ClearBytes(size_t offset, size_t len);
69
Primiano Tucci09db8272018-03-08 17:47:47 +000070 FakeChunk& SetUID(uid_t);
71
Primiano Tucci5c599012018-03-01 17:52:07 +000072 // Returns the full size of the chunk including the ChunkRecord header.
73 size_t CopyIntoTraceBuffer();
74
75 private:
Hector Dearman6214c8f2018-03-27 16:16:22 +010076 TraceBuffer* trace_buffer_;
Primiano Tucci5c599012018-03-01 17:52:07 +000077 ProducerID producer_id;
78 WriterID writer_id;
79 ChunkID chunk_id;
80 uint8_t flags = 0;
81 uint16_t num_packets = 0;
Primiano Tucci3cbb10a2018-04-10 17:52:40 +010082 uid_t uid = kInvalidUid;
Primiano Tucci5c599012018-03-01 17:52:07 +000083 std::vector<uint8_t> data;
84};
85
86} // namespace perfetto
87
88#endif // SRC_TRACING_TEST_FAKE_PACKET_H_