1 /*
2 * Copyright (C) 2024 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/protozero/scattered_heap_buffer.h"
18
19 #include "src/trace_redaction/proto_util.h"
20 #include "test/gtest_and_gmock.h"
21
22 #include "protos/perfetto/config/test_config.gen.h"
23 #include "protos/perfetto/config/test_config.pbzero.h"
24 #include "protos/perfetto/trace/trace_packet.gen.h"
25
26 namespace perfetto::trace_redaction {
27
28 namespace proto_util {
29
30 class ProtoUtilTest : public testing::Test {
31 protected:
Reserialize(const protos::gen::TestConfig_DummyFields & fields)32 void Reserialize(const protos::gen::TestConfig_DummyFields& fields) {
33 // Serialize the object and then deserialize it with proto decoder. This is
34 // needed to get the fields.
35 auto serialized = fields.SerializeAsString();
36 protozero::ProtoDecoder decoder(serialized);
37
38 protozero::HeapBuffered<protos::pbzero::TestConfig_DummyFields> message;
39
40 for (auto field = decoder.ReadField(); field.valid();
41 field = decoder.ReadField()) {
42 AppendField(field, message.get());
43 }
44
45 auto reserialized = message.SerializeAsString();
46
47 ASSERT_EQ(serialized, reserialized);
48 }
49 };
50
51 class ProtoUtilUint32Test : public ProtoUtilTest,
52 public testing::WithParamInterface<uint32_t> {};
53 class ProtoUtilUint64Test : public ProtoUtilTest,
54 public testing::WithParamInterface<uint64_t> {};
55 class ProtoUtilInt32Test : public ProtoUtilTest,
56 public testing::WithParamInterface<int32_t> {};
57 class ProtoUtilInt64Test : public ProtoUtilTest,
58 public testing::WithParamInterface<int64_t> {};
59 class ProtoUtilFixed32Test : public ProtoUtilTest,
60 public testing::WithParamInterface<uint32_t> {};
61 class ProtoUtilFixed64Test : public ProtoUtilTest,
62 public testing::WithParamInterface<uint64_t> {};
63 class ProtoUtilSfixed32Test : public ProtoUtilTest,
64 public testing::WithParamInterface<int32_t> {};
65 class ProtoUtilSfixed64Test : public ProtoUtilTest,
66 public testing::WithParamInterface<int64_t> {};
67 class ProtoUtilDoubleTest : public ProtoUtilTest,
68 public testing::WithParamInterface<double> {};
69 class ProtoUtilFloatTest : public ProtoUtilTest,
70 public testing::WithParamInterface<float> {};
71 class ProtoUtilSint32Test : public ProtoUtilTest,
72 public testing::WithParamInterface<int32_t> {};
73 class ProtoUtilSint64Test : public ProtoUtilTest,
74 public testing::WithParamInterface<int64_t> {};
75 class ProtoUtilStringTest : public ProtoUtilTest,
76 public testing::WithParamInterface<std::string> {};
77 class ProtoUtilBytesTest : public ProtoUtilTest,
78 public testing::WithParamInterface<std::string> {};
79
TEST_P(ProtoUtilUint32Test,FullDomain)80 TEST_P(ProtoUtilUint32Test, FullDomain) {
81 auto value = GetParam();
82
83 protos::gen::TestConfig_DummyFields fields;
84 fields.set_field_uint32(value);
85 Reserialize(fields);
86 }
87
88 INSTANTIATE_TEST_SUITE_P(Reserialize,
89 ProtoUtilUint32Test,
90 testing::Values(std::numeric_limits<uint32_t>::min(),
91 0,
92 0xFAAAAAAA,
93 std::numeric_limits<uint32_t>::max()));
94
TEST_P(ProtoUtilUint64Test,FullDomain)95 TEST_P(ProtoUtilUint64Test, FullDomain) {
96 auto value = GetParam();
97
98 protos::gen::TestConfig_DummyFields fields;
99 fields.set_field_uint64(value);
100 Reserialize(fields);
101 }
102
103 INSTANTIATE_TEST_SUITE_P(Reserialize,
104 ProtoUtilUint64Test,
105 testing::Values(std::numeric_limits<uint64_t>::min(),
106 0,
107 0xFAAAAAAAAAAAAAAA,
108 std::numeric_limits<uint64_t>::max()));
109
TEST_P(ProtoUtilInt32Test,FullDomain)110 TEST_P(ProtoUtilInt32Test, FullDomain) {
111 auto value = GetParam();
112
113 protos::gen::TestConfig_DummyFields fields;
114 fields.set_field_int32(value);
115 Reserialize(fields);
116 }
117
118 INSTANTIATE_TEST_SUITE_P(Reserialize,
119 ProtoUtilInt32Test,
120 testing::Values(std::numeric_limits<int32_t>::min(),
121 0xFAAAAAAA,
122 0,
123 0x0AAAAAAA,
124 std::numeric_limits<int32_t>::max()));
125
TEST_P(ProtoUtilInt64Test,FullDomain)126 TEST_P(ProtoUtilInt64Test, FullDomain) {
127 auto value = GetParam();
128
129 protos::gen::TestConfig_DummyFields fields;
130 fields.set_field_int64(value);
131 Reserialize(fields);
132 }
133
134 INSTANTIATE_TEST_SUITE_P(Reserialize,
135 ProtoUtilInt64Test,
136 testing::Values(std::numeric_limits<int64_t>::min(),
137 0xFAAAAAAAAAAAAAAA,
138 0,
139 0x0AAAAAAAAAAAAAAA,
140 std::numeric_limits<int64_t>::max()));
141
TEST_P(ProtoUtilFixed32Test,FullDomain)142 TEST_P(ProtoUtilFixed32Test, FullDomain) {
143 auto value = GetParam();
144
145 protos::gen::TestConfig_DummyFields fields;
146 fields.set_field_fixed32(value);
147 Reserialize(fields);
148 }
149
150 INSTANTIATE_TEST_SUITE_P(Reserialize,
151 ProtoUtilFixed32Test,
152 testing::Values(std::numeric_limits<uint32_t>::min(),
153 0,
154 0xFAAAAAAAAAAAAAAA,
155 std::numeric_limits<uint32_t>::max()));
156
TEST_P(ProtoUtilSfixed32Test,FullDomain)157 TEST_P(ProtoUtilSfixed32Test, FullDomain) {
158 auto value = GetParam();
159
160 protos::gen::TestConfig_DummyFields fields;
161 fields.set_field_sfixed32(value);
162 Reserialize(fields);
163 }
164
165 INSTANTIATE_TEST_SUITE_P(Reserialize,
166 ProtoUtilSfixed32Test,
167 testing::Values(std::numeric_limits<int32_t>::min(),
168 0xFAAAAAAA,
169 0,
170 0x0AAAAAAA,
171 std::numeric_limits<int32_t>::max()));
172
TEST_P(ProtoUtilDoubleTest,FullDomain)173 TEST_P(ProtoUtilDoubleTest, FullDomain) {
174 auto value = GetParam();
175
176 protos::gen::TestConfig_DummyFields fields;
177 fields.set_field_double(value);
178 Reserialize(fields);
179 }
180
181 INSTANTIATE_TEST_SUITE_P(
182 Reserialize,
183 ProtoUtilDoubleTest,
184 testing::Values(std::numeric_limits<double>::min(),
185 0.0,
186 1.0,
187 std::numeric_limits<double>::infinity(),
188 std::numeric_limits<double>::max()));
189
TEST_P(ProtoUtilFloatTest,FullDomain)190 TEST_P(ProtoUtilFloatTest, FullDomain) {
191 auto value = GetParam();
192
193 protos::gen::TestConfig_DummyFields fields;
194 fields.set_field_float(value);
195 Reserialize(fields);
196 }
197
198 INSTANTIATE_TEST_SUITE_P(Reserialize,
199 ProtoUtilFloatTest,
200 testing::Values(std::numeric_limits<float>::min(),
201 0.0f,
202 1.0f,
203 std::numeric_limits<float>::infinity(),
204 std::numeric_limits<float>::max()));
205
TEST_P(ProtoUtilSint64Test,FullDomain)206 TEST_P(ProtoUtilSint64Test, FullDomain) {
207 auto value = GetParam();
208
209 protos::gen::TestConfig_DummyFields fields;
210 fields.set_field_sint64(value);
211 Reserialize(fields);
212 }
213
214 INSTANTIATE_TEST_SUITE_P(Reserialize,
215 ProtoUtilSint64Test,
216 testing::Values(std::numeric_limits<int64_t>::min(),
217 0xFAAAAAAAAAAAAAAA,
218 0,
219 0x0AAAAAAAAAAAAAAA,
220 std::numeric_limits<int64_t>::max()));
221
TEST_P(ProtoUtilSint32Test,FullDomain)222 TEST_P(ProtoUtilSint32Test, FullDomain) {
223 auto value = GetParam();
224
225 protos::gen::TestConfig_DummyFields fields;
226 fields.set_field_sint32(value);
227 Reserialize(fields);
228 }
229
230 INSTANTIATE_TEST_SUITE_P(Reserialize,
231 ProtoUtilSint32Test,
232 testing::Values(std::numeric_limits<int32_t>::min(),
233 0xFAAAAAAA,
234 0,
235 0x0AAAAAAA,
236 std::numeric_limits<int32_t>::max()));
237
TEST_P(ProtoUtilStringTest,Various)238 TEST_P(ProtoUtilStringTest, Various) {
239 auto value = GetParam();
240
241 protos::gen::TestConfig_DummyFields fields;
242 fields.set_field_string(value);
243 Reserialize(fields);
244 }
245
246 INSTANTIATE_TEST_SUITE_P(Reserialize,
247 ProtoUtilStringTest,
248 testing::Values("",
249 "a",
250 "abcdefghijklmonpqrstuvwxyz",
251 std::string(1024, 'a')));
252
TEST_P(ProtoUtilBytesTest,Various)253 TEST_P(ProtoUtilBytesTest, Various) {
254 auto value = GetParam();
255
256 protos::gen::TestConfig_DummyFields fields;
257 fields.set_field_bytes(value);
258 Reserialize(fields);
259 }
260
261 INSTANTIATE_TEST_SUITE_P(Reserialize,
262 ProtoUtilBytesTest,
263 testing::Values("",
264 "a",
265 "abcdefghijklmonpqrstuvwxyz",
266 std::string(1024, 'a')));
267
268 } // namespace proto_util
269
270 } // namespace perfetto::trace_redaction
271