1 /* Copyright (c) 2021 The WebRTC Project Authors. All rights reserved.
2 *
3 * Use of this source code is governed by a BSD-style license
4 * that can be found in the LICENSE file in the root of the source
5 * tree. An additional intellectual property rights grant can be found
6 * in the file PATENTS. All contributing project authors may
7 * be found in the AUTHORS file in the root of the source tree.
8 */
9 #include "logging/rtc_event_log/events/rtc_event_field_encoding.h"
10
11 #include <limits>
12 #include <memory>
13 #include <string>
14
15 #include "absl/strings/string_view.h"
16 #include "api/rtc_event_log/rtc_event.h"
17 #include "logging/rtc_event_log/encoder/var_int.h"
18 #include "logging/rtc_event_log/events/rtc_event_field_encoding_parser.h"
19 #include "test/gtest.h"
20
21 namespace webrtc {
22
23 namespace {
24 constexpr int32_t kInt32Max = std::numeric_limits<int32_t>::max();
25 constexpr int32_t kInt32Min = std::numeric_limits<int32_t>::min();
26 constexpr uint32_t kUint32Max = std::numeric_limits<uint32_t>::max();
27 constexpr int64_t kInt64Max = std::numeric_limits<int64_t>::max();
28 constexpr int64_t kInt64Min = std::numeric_limits<int64_t>::min();
29 constexpr uint64_t kUint64Max = std::numeric_limits<uint64_t>::max();
30
31 template <typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true>
ExpectedVarIntSize(T value)32 size_t ExpectedVarIntSize(T value) {
33 size_t bytes = 0;
34 uint64_t x = EncodeAsUnsigned(value);
35 do {
36 ++bytes;
37 x = x >> 7;
38 } while (x > 0);
39 return bytes;
40 }
41
42 template <typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true>
ExpectedBaseValueSize(const FieldParameters & params,T value)43 size_t ExpectedBaseValueSize(const FieldParameters& params, T value) {
44 switch (params.field_type) {
45 case FieldType::kFixed8:
46 return 1;
47 case FieldType::kFixed32:
48 return 4;
49 case FieldType::kFixed64:
50 return 8;
51 case FieldType::kVarInt:
52 return ExpectedVarIntSize(value);
53 default:
54 break;
55 }
56 RTC_DCHECK_NOTREACHED();
57 return 0;
58 }
59
60 template <typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true>
ExpectedEncodingSize(const FieldParameters & params,const std::vector<T> & v,size_t expected_bits_per_delta)61 size_t ExpectedEncodingSize(const FieldParameters& params,
62 const std::vector<T>& v,
63 size_t expected_bits_per_delta) {
64 if (v.size() == 0)
65 return 0;
66
67 uint64_t numeric_field_type = static_cast<uint64_t>(params.field_type);
68 RTC_DCHECK_LT(numeric_field_type, 1u << 3);
69 size_t tag_size =
70 ExpectedVarIntSize((params.field_id << 3) + numeric_field_type);
71 T base = v[0];
72 size_t base_size = ExpectedBaseValueSize(params, base);
73 if (v.size() == 1)
74 return tag_size + base_size;
75
76 size_t delta_header_size = 1;
77 // Check if there is an element *not* equal to base.
78 if (std::all_of(v.begin(), v.end(), [base](T x) { return x == base; })) {
79 return tag_size + base_size + delta_header_size;
80 }
81
82 size_t delta_size = ((v.size() - 1) * expected_bits_per_delta + 7) / 8;
83 return tag_size + base_size + delta_header_size + delta_size;
84 }
85
86 template <typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true>
ExpectedEncodingSize(const FieldParameters & params,const std::vector<absl::optional<T>> & v,size_t expected_bits_per_delta)87 size_t ExpectedEncodingSize(const FieldParameters& params,
88 const std::vector<absl::optional<T>>& v,
89 size_t expected_bits_per_delta) {
90 size_t num_existing_values =
91 v.size() - std::count(v.begin(), v.end(), absl::nullopt);
92 auto first_existing_value = std::find_if(
93 v.begin(), v.end(), [](absl::optional<T> x) { return x.has_value(); });
94 if (num_existing_values == 0)
95 return 0;
96
97 uint64_t numeric_field_type = static_cast<uint64_t>(params.field_type);
98 RTC_DCHECK_LT(numeric_field_type, 1u << 3);
99 size_t tag_size =
100 ExpectedVarIntSize((params.field_id << 3) + numeric_field_type);
101 T base = first_existing_value->value();
102 size_t base_size = ExpectedBaseValueSize(params, base);
103 if (num_existing_values == 1 && v.size() == 1)
104 return tag_size + base_size;
105
106 size_t delta_header_size = (num_existing_values == v.size() ? 1 : 2);
107 size_t positions_size =
108 (num_existing_values == v.size() ? 0 : (v.size() + 7) / 8);
109 // Check if there is an element *not* equal to base.
110 if (std::all_of(v.begin(), v.end(),
111 [base](absl::optional<T> x) { return x == base; })) {
112 return tag_size + base_size + delta_header_size + positions_size;
113 }
114
115 size_t delta_size =
116 ((num_existing_values - 1) * expected_bits_per_delta + 7) / 8;
117 return tag_size + base_size + delta_header_size + positions_size + delta_size;
118 }
119
ExpectedStringEncodingSize(const FieldParameters & params,const std::vector<std::string> & values)120 size_t ExpectedStringEncodingSize(const FieldParameters& params,
121 const std::vector<std::string>& values) {
122 EXPECT_EQ(params.field_type, FieldType::kString);
123 uint64_t numeric_field_type = static_cast<uint64_t>(params.field_type);
124 RTC_DCHECK_LT(numeric_field_type, 1u << 3);
125 size_t tag_size =
126 ExpectedVarIntSize((params.field_id << 3) + numeric_field_type);
127
128 size_t expected_size = tag_size;
129 if (values.size() > 1) {
130 // VarInt encoding header reserved for future use. Currently always 0.
131 expected_size += 1;
132 }
133 for (const auto& s : values) {
134 expected_size += ExpectedVarIntSize(s.size());
135 expected_size += s.size();
136 }
137 return expected_size;
138 }
139
140 } // namespace
141
142 class RtcTestEvent final : public RtcEvent {
143 public:
RtcTestEvent(bool b,int32_t signed32,uint32_t unsigned32,int64_t signed64,uint64_t unsigned64)144 RtcTestEvent(bool b,
145 int32_t signed32,
146 uint32_t unsigned32,
147 int64_t signed64,
148 uint64_t unsigned64)
149 : b_(b),
150 signed32_(signed32),
151 unsigned32_(unsigned32),
152 signed64_(signed64),
153 unsigned64_(unsigned64) {}
RtcTestEvent(bool b,int32_t signed32,uint32_t unsigned32,int64_t signed64,uint64_t unsigned64,absl::optional<int32_t> optional_signed32,absl::optional<int64_t> optional_signed64,uint32_t wrapping21,absl::string_view string)154 RtcTestEvent(bool b,
155 int32_t signed32,
156 uint32_t unsigned32,
157 int64_t signed64,
158 uint64_t unsigned64,
159 absl::optional<int32_t> optional_signed32,
160 absl::optional<int64_t> optional_signed64,
161 uint32_t wrapping21,
162 absl::string_view string)
163 : b_(b),
164 signed32_(signed32),
165 unsigned32_(unsigned32),
166 signed64_(signed64),
167 unsigned64_(unsigned64),
168 optional_signed32_(optional_signed32),
169 optional_signed64_(optional_signed64),
170 wrapping21_(wrapping21),
171 string_(string) {}
172 ~RtcTestEvent() override = default;
173
GetType() const174 Type GetType() const override { return static_cast<Type>(4711); }
IsConfigEvent() const175 bool IsConfigEvent() const override { return false; }
176
177 static constexpr EventParameters event_params{
178 "TestEvent", static_cast<RtcEvent::Type>(4711)};
179 static constexpr FieldParameters timestamp_params{
180 "timestamp_ms", FieldParameters::kTimestampField, FieldType::kVarInt, 64};
181 static constexpr FieldParameters bool_params{"b", 2, FieldType::kFixed8, 1};
182 static constexpr FieldParameters signed32_params{"signed32", 3,
183 FieldType::kVarInt, 32};
184 static constexpr FieldParameters unsigned32_params{"unsigned32", 4,
185 FieldType::kFixed32, 32};
186 static constexpr FieldParameters signed64_params{"signed64", 5,
187 FieldType::kFixed64, 64};
188 static constexpr FieldParameters unsigned64_params{"unsigned64", 6,
189 FieldType::kVarInt, 64};
190 static constexpr FieldParameters optional32_params{"optional_signed32", 7,
191 FieldType::kFixed32, 32};
192 static constexpr FieldParameters optional64_params{"optional_signed64", 8,
193 FieldType::kVarInt, 64};
194 static constexpr FieldParameters wrapping21_params{"wrapping21", 9,
195 FieldType::kFixed32, 21};
196 static constexpr FieldParameters string_params{
197 "string", 10, FieldType::kString, /*value_width = */ 0};
198
199 static constexpr Type kType = static_cast<RtcEvent::Type>(4711);
200
201 const bool b_;
202 const int32_t signed32_;
203 const uint32_t unsigned32_;
204 const int64_t signed64_;
205 const uint64_t unsigned64_;
206 const absl::optional<int32_t> optional_signed32_ = absl::nullopt;
207 const absl::optional<int64_t> optional_signed64_ = absl::nullopt;
208 const uint32_t wrapping21_ = 0;
209 const std::string string_;
210 };
211
212 constexpr EventParameters RtcTestEvent::event_params;
213 constexpr FieldParameters RtcTestEvent::timestamp_params;
214 constexpr FieldParameters RtcTestEvent::bool_params;
215 constexpr FieldParameters RtcTestEvent::signed32_params;
216 constexpr FieldParameters RtcTestEvent::unsigned32_params;
217 constexpr FieldParameters RtcTestEvent::signed64_params;
218 constexpr FieldParameters RtcTestEvent::unsigned64_params;
219
220 constexpr FieldParameters RtcTestEvent::optional32_params;
221 constexpr FieldParameters RtcTestEvent::optional64_params;
222 constexpr FieldParameters RtcTestEvent::wrapping21_params;
223 constexpr FieldParameters RtcTestEvent::string_params;
224
225 constexpr RtcEvent::Type RtcTestEvent::kType;
226
227 class RtcEventFieldTest : public ::testing::Test {
228 protected:
SetUp()229 void SetUp() override {}
230
CreateFullEvents(const std::vector<bool> & bool_values,const std::vector<int32_t> & signed32_values,const std::vector<uint32_t> & unsigned32_values,const std::vector<int64_t> & signed64_values,const std::vector<uint64_t> & unsigned64_values,const std::vector<absl::optional<int32_t>> & optional32_values,const std::vector<absl::optional<int64_t>> & optional64_values,const std::vector<uint32_t> & wrapping21_values,const std::vector<std::string> & string_values)231 void CreateFullEvents(
232 const std::vector<bool>& bool_values,
233 const std::vector<int32_t>& signed32_values,
234 const std::vector<uint32_t>& unsigned32_values,
235 const std::vector<int64_t>& signed64_values,
236 const std::vector<uint64_t>& unsigned64_values,
237 const std::vector<absl::optional<int32_t>>& optional32_values,
238 const std::vector<absl::optional<int64_t>>& optional64_values,
239 const std::vector<uint32_t>& wrapping21_values,
240 const std::vector<std::string>& string_values) {
241 size_t size = bool_values.size();
242 RTC_CHECK_EQ(signed32_values.size(), size);
243 RTC_CHECK_EQ(unsigned32_values.size(), size);
244 RTC_CHECK_EQ(signed64_values.size(), size);
245 RTC_CHECK_EQ(unsigned64_values.size(), size);
246 RTC_CHECK_EQ(optional32_values.size(), size);
247 RTC_CHECK_EQ(optional64_values.size(), size);
248 RTC_CHECK_EQ(wrapping21_values.size(), size);
249 RTC_CHECK_EQ(string_values.size(), size);
250
251 for (size_t i = 0; i < size; i++) {
252 batch_.push_back(new RtcTestEvent(
253 bool_values[i], signed32_values[i], unsigned32_values[i],
254 signed64_values[i], unsigned64_values[i], optional32_values[i],
255 optional64_values[i], wrapping21_values[i], string_values[i]));
256 }
257 }
258
PrintBytes(absl::string_view s)259 void PrintBytes(absl::string_view s) {
260 for (auto c : s) {
261 fprintf(stderr, "%d ", static_cast<uint8_t>(c));
262 }
263 fprintf(stderr, "\n");
264 }
265
ParseEventHeader(absl::string_view encoded_event)266 void ParseEventHeader(absl::string_view encoded_event) {
267 uint64_t event_tag;
268 bool success;
269 std::tie(success, encoded_event) = DecodeVarInt(encoded_event, &event_tag);
270 ASSERT_TRUE(success);
271 uint64_t event_id = event_tag >> 1;
272 ASSERT_EQ(event_id, static_cast<uint64_t>(RtcTestEvent::event_params.id));
273 bool batched = event_tag & 1u;
274 ASSERT_EQ(batched, batch_.size() > 1u);
275
276 uint64_t size;
277 std::tie(success, encoded_event) = DecodeVarInt(encoded_event, &size);
278 ASSERT_EQ(encoded_event.size(), size);
279
280 ASSERT_TRUE(parser_.Initialize(encoded_event, batched).ok());
281 }
282
ParseAndVerifyTimestamps()283 void ParseAndVerifyTimestamps() {
284 auto result = parser_.ParseNumericField(RtcTestEvent::timestamp_params);
285 ASSERT_TRUE(result.ok()) << result.message().c_str();
286 ASSERT_EQ(result.value().size(), batch_.size());
287 for (size_t i = 0; i < batch_.size(); i++) {
288 EXPECT_EQ(result.value()[i],
289 static_cast<uint64_t>(batch_[i]->timestamp_ms()));
290 }
291 }
292
ParseAndVerifyStringField(const FieldParameters & params,const std::vector<std::string> & expected_values,size_t expected_skipped_bytes=0)293 void ParseAndVerifyStringField(
294 const FieldParameters& params,
295 const std::vector<std::string>& expected_values,
296 size_t expected_skipped_bytes = 0) {
297 size_t expected_size = ExpectedStringEncodingSize(params, expected_values) +
298 expected_skipped_bytes;
299 size_t size_before = parser_.RemainingBytes();
300 auto result = parser_.ParseStringField(params);
301 ASSERT_TRUE(result.ok()) << result.message().c_str();
302 ASSERT_EQ(result.value().size(), expected_values.size());
303 for (size_t i = 0; i < expected_values.size(); i++) {
304 EXPECT_EQ(result.value()[i], expected_values[i]);
305 }
306 size_t size_after = parser_.RemainingBytes();
307 EXPECT_EQ(size_before - size_after, expected_size)
308 << " for field " << params.name;
309 }
310
311 template <typename T>
ParseAndVerifyField(const FieldParameters & params,const std::vector<T> & expected_values,size_t expected_bits_per_delta,size_t expected_skipped_bytes=0)312 void ParseAndVerifyField(const FieldParameters& params,
313 const std::vector<T>& expected_values,
314 size_t expected_bits_per_delta,
315 size_t expected_skipped_bytes = 0) {
316 size_t expected_size =
317 ExpectedEncodingSize(params, expected_values, expected_bits_per_delta) +
318 expected_skipped_bytes;
319 size_t size_before = parser_.RemainingBytes();
320 auto result = parser_.ParseNumericField(params);
321 ASSERT_TRUE(result.ok()) << result.message().c_str();
322 ASSERT_EQ(result.value().size(), expected_values.size());
323 for (size_t i = 0; i < expected_values.size(); i++) {
324 EXPECT_EQ(DecodeFromUnsignedToType<T>(result.value()[i]),
325 expected_values[i]);
326 }
327 size_t size_after = parser_.RemainingBytes();
328 EXPECT_EQ(size_before - size_after, expected_size)
329 << " for field " << params.name;
330 }
331
332 template <typename T>
ParseAndVerifyOptionalField(const FieldParameters & params,const std::vector<absl::optional<T>> & expected_values,size_t expected_bits_per_delta,size_t expected_skipped_bytes=0)333 void ParseAndVerifyOptionalField(
334 const FieldParameters& params,
335 const std::vector<absl::optional<T>>& expected_values,
336 size_t expected_bits_per_delta,
337 size_t expected_skipped_bytes = 0) {
338 size_t expected_size =
339 ExpectedEncodingSize(params, expected_values, expected_bits_per_delta) +
340 expected_skipped_bytes;
341 size_t size_before = parser_.RemainingBytes();
342 auto result = parser_.ParseOptionalNumericField(params);
343 ASSERT_TRUE(result.ok()) << result.message().c_str();
344 rtc::ArrayView<uint64_t> values = result.value().values;
345 rtc::ArrayView<uint8_t> positions = result.value().positions;
346 ASSERT_EQ(positions.size(), expected_values.size());
347 auto value_it = values.begin();
348 for (size_t i = 0; i < expected_values.size(); i++) {
349 if (positions[i]) {
350 ASSERT_NE(value_it, values.end());
351 ASSERT_TRUE(expected_values[i].has_value());
352 EXPECT_EQ(DecodeFromUnsignedToType<T>(*value_it),
353 expected_values[i].value());
354 ++value_it;
355 } else {
356 EXPECT_EQ(absl::nullopt, expected_values[i]);
357 }
358 }
359 EXPECT_EQ(value_it, values.end());
360 size_t size_after = parser_.RemainingBytes();
361 EXPECT_EQ(size_before - size_after, expected_size);
362 }
363
ParseAndVerifyMissingField(const FieldParameters & params)364 void ParseAndVerifyMissingField(const FieldParameters& params) {
365 auto result = parser_.ParseNumericField(params, /*required_field=*/false);
366 ASSERT_TRUE(result.ok()) << result.message().c_str();
367 EXPECT_EQ(result.value().size(), 0u);
368 }
369
ParseAndVerifyMissingOptionalField(const FieldParameters & params)370 void ParseAndVerifyMissingOptionalField(const FieldParameters& params) {
371 auto result =
372 parser_.ParseOptionalNumericField(params, /*required_field=*/false);
373 ASSERT_TRUE(result.ok()) << result.message().c_str();
374 rtc::ArrayView<uint64_t> values = result.value().values;
375 rtc::ArrayView<uint8_t> positions = result.value().positions;
376 EXPECT_EQ(positions.size(), 0u);
377 EXPECT_EQ(values.size(), 0u);
378 }
379
TearDown()380 void TearDown() override {
381 for (const RtcEvent* event : batch_) {
382 delete event;
383 }
384 }
385
386 std::vector<const RtcEvent*> batch_;
387 EventParser parser_;
388 };
389
TEST_F(RtcEventFieldTest,EmptyList)390 TEST_F(RtcEventFieldTest, EmptyList) {
391 EventEncoder encoder(RtcTestEvent::event_params, batch_);
392 encoder.EncodeField(RtcTestEvent::bool_params,
393 ExtractRtcEventMember(batch_, &RtcTestEvent::b_));
394 std::string s = encoder.AsString();
395 EXPECT_TRUE(s.empty());
396 }
397
TEST_F(RtcEventFieldTest,Singleton)398 TEST_F(RtcEventFieldTest, Singleton) {
399 std::vector<bool> bool_values = {true};
400 std::vector<int32_t> signed32_values = {-2};
401 std::vector<uint32_t> unsigned32_values = {123456789};
402 std::vector<int64_t> signed64_values = {-9876543210};
403 std::vector<uint64_t> unsigned64_values = {9876543210};
404 std::vector<absl::optional<int32_t>> optional32_values = {kInt32Min};
405 std::vector<absl::optional<int64_t>> optional64_values = {kInt64Max};
406 std::vector<uint32_t> wrapping21_values = {(1 << 21) - 1};
407 std::vector<std::string> string_values = {"foo"};
408
409 CreateFullEvents(bool_values, signed32_values, unsigned32_values,
410 signed64_values, unsigned64_values, optional32_values,
411 optional64_values, wrapping21_values, string_values);
412
413 EventEncoder encoder(RtcTestEvent::event_params, batch_);
414 encoder.EncodeField(RtcTestEvent::bool_params,
415 ExtractRtcEventMember(batch_, &RtcTestEvent::b_));
416 encoder.EncodeField(RtcTestEvent::signed32_params,
417 ExtractRtcEventMember(batch_, &RtcTestEvent::signed32_));
418 encoder.EncodeField(
419 RtcTestEvent::unsigned32_params,
420 ExtractRtcEventMember(batch_, &RtcTestEvent::unsigned32_));
421 encoder.EncodeField(RtcTestEvent::signed64_params,
422 ExtractRtcEventMember(batch_, &RtcTestEvent::signed64_));
423 encoder.EncodeField(
424 RtcTestEvent::unsigned64_params,
425 ExtractRtcEventMember(batch_, &RtcTestEvent::unsigned64_));
426 encoder.EncodeField(
427 RtcTestEvent::optional32_params,
428 ExtractRtcEventMember(batch_, &RtcTestEvent::optional_signed32_));
429 encoder.EncodeField(
430 RtcTestEvent::optional64_params,
431 ExtractRtcEventMember(batch_, &RtcTestEvent::optional_signed64_));
432 encoder.EncodeField(
433 RtcTestEvent::wrapping21_params,
434 ExtractRtcEventMember(batch_, &RtcTestEvent::wrapping21_));
435 encoder.EncodeField(RtcTestEvent::string_params,
436 ExtractRtcEventMember(batch_, &RtcTestEvent::string_));
437 std::string s = encoder.AsString();
438
439 // Optional debug printing
440 // PrintBytes(s);
441
442 ParseEventHeader(s);
443 ParseAndVerifyTimestamps();
444 ParseAndVerifyField(RtcTestEvent::bool_params, bool_values,
445 /*no deltas*/ 0);
446 ParseAndVerifyField(RtcTestEvent::signed32_params, signed32_values,
447 /*no deltas*/ 0);
448 ParseAndVerifyField(RtcTestEvent::unsigned32_params, unsigned32_values,
449 /*no deltas*/ 0);
450 ParseAndVerifyField(RtcTestEvent::signed64_params, signed64_values,
451 /*no deltas*/ 0);
452 ParseAndVerifyField(RtcTestEvent::unsigned64_params, unsigned64_values,
453 /*no deltas*/ 0);
454 ParseAndVerifyOptionalField(RtcTestEvent::optional32_params,
455 optional32_values, /*no deltas*/ 0);
456 ParseAndVerifyOptionalField(RtcTestEvent::optional64_params,
457 optional64_values, /*no deltas*/ 0);
458 ParseAndVerifyField(RtcTestEvent::wrapping21_params, wrapping21_values,
459 /*no deltas*/ 0);
460 ParseAndVerifyStringField(RtcTestEvent::string_params, string_values);
461 EXPECT_EQ(parser_.RemainingBytes(), 0u);
462 }
463
TEST_F(RtcEventFieldTest,EqualElements)464 TEST_F(RtcEventFieldTest, EqualElements) {
465 std::vector<bool> bool_values = {true, true, true, true};
466 std::vector<int32_t> signed32_values = {-2, -2, -2, -2};
467 std::vector<uint32_t> unsigned32_values = {123456789, 123456789, 123456789,
468 123456789};
469 std::vector<int64_t> signed64_values = {-9876543210, -9876543210, -9876543210,
470 -9876543210};
471 std::vector<uint64_t> unsigned64_values = {9876543210, 9876543210, 9876543210,
472 9876543210};
473 std::vector<absl::optional<int32_t>> optional32_values = {
474 kInt32Min, kInt32Min, kInt32Min, kInt32Min};
475 std::vector<absl::optional<int64_t>> optional64_values = {
476 kInt64Max, kInt64Max, kInt64Max, kInt64Max};
477 std::vector<uint32_t> wrapping21_values = {(1 << 21) - 1, (1 << 21) - 1,
478 (1 << 21) - 1, (1 << 21) - 1};
479 std::vector<std::string> string_values = {"foo", "foo", "foo", "foo"};
480
481 CreateFullEvents(bool_values, signed32_values, unsigned32_values,
482 signed64_values, unsigned64_values, optional32_values,
483 optional64_values, wrapping21_values, string_values);
484 EventEncoder encoder(RtcTestEvent::event_params, batch_);
485 encoder.EncodeField(RtcTestEvent::bool_params,
486 ExtractRtcEventMember(batch_, &RtcTestEvent::b_));
487 encoder.EncodeField(RtcTestEvent::signed32_params,
488 ExtractRtcEventMember(batch_, &RtcTestEvent::signed32_));
489 encoder.EncodeField(
490 RtcTestEvent::unsigned32_params,
491 ExtractRtcEventMember(batch_, &RtcTestEvent::unsigned32_));
492 encoder.EncodeField(RtcTestEvent::signed64_params,
493 ExtractRtcEventMember(batch_, &RtcTestEvent::signed64_));
494 encoder.EncodeField(
495 RtcTestEvent::unsigned64_params,
496 ExtractRtcEventMember(batch_, &RtcTestEvent::unsigned64_));
497 encoder.EncodeField(
498 RtcTestEvent::optional32_params,
499 ExtractRtcEventMember(batch_, &RtcTestEvent::optional_signed32_));
500 encoder.EncodeField(
501 RtcTestEvent::optional64_params,
502 ExtractRtcEventMember(batch_, &RtcTestEvent::optional_signed64_));
503 encoder.EncodeField(
504 RtcTestEvent::wrapping21_params,
505 ExtractRtcEventMember(batch_, &RtcTestEvent::wrapping21_));
506 encoder.EncodeField(RtcTestEvent::string_params,
507 ExtractRtcEventMember(batch_, &RtcTestEvent::string_));
508 std::string s = encoder.AsString();
509
510 // Optional debug printing
511 // PrintBytes(s);
512
513 ParseEventHeader(s);
514 ParseAndVerifyTimestamps();
515 ParseAndVerifyField(RtcTestEvent::bool_params, bool_values,
516 /*no deltas*/ 0);
517 ParseAndVerifyField(RtcTestEvent::signed32_params, signed32_values,
518 /*no deltas*/ 0);
519 ParseAndVerifyField(RtcTestEvent::unsigned32_params, unsigned32_values,
520 /*no deltas*/ 0);
521 ParseAndVerifyField(RtcTestEvent::signed64_params, signed64_values,
522 /*no deltas*/ 0);
523 ParseAndVerifyField(RtcTestEvent::unsigned64_params, unsigned64_values,
524 /*no deltas*/ 0);
525 ParseAndVerifyOptionalField(RtcTestEvent::optional32_params,
526 optional32_values, /*no deltas*/ 0);
527 ParseAndVerifyOptionalField(RtcTestEvent::optional64_params,
528 optional64_values, /*no deltas*/ 0);
529 ParseAndVerifyField(RtcTestEvent::wrapping21_params, wrapping21_values,
530 /*no deltas*/ 0);
531 ParseAndVerifyStringField(RtcTestEvent::string_params, string_values);
532 EXPECT_EQ(parser_.RemainingBytes(), 0u);
533 }
534
TEST_F(RtcEventFieldTest,Increasing)535 TEST_F(RtcEventFieldTest, Increasing) {
536 std::vector<bool> bool_values = {false, true, false, true};
537 std::vector<int32_t> signed32_values = {-2, -1, 0, 1};
538 std::vector<uint32_t> unsigned32_values = {kUint32Max - 1, kUint32Max, 0, 1};
539 std::vector<int64_t> signed64_values = {kInt64Max - 1, kInt64Max, kInt64Min,
540 kInt64Min + 1};
541 std::vector<uint64_t> unsigned64_values = {kUint64Max - 1, kUint64Max, 0, 1};
542 std::vector<absl::optional<int32_t>> optional32_values = {
543 kInt32Max - 1, kInt32Max, kInt32Min, kInt32Min + 1};
544 std::vector<absl::optional<int64_t>> optional64_values = {
545 kInt64Max - 1, kInt64Max, kInt64Min, kInt64Min + 1};
546 std::vector<uint32_t> wrapping21_values = {(1 << 21) - 2, (1 << 21) - 1, 0,
547 1};
548 std::vector<std::string> string_values = {
549 "", "a", "bc", "def"}; // No special compression of strings.
550
551 CreateFullEvents(bool_values, signed32_values, unsigned32_values,
552 signed64_values, unsigned64_values, optional32_values,
553 optional64_values, wrapping21_values, string_values);
554
555 EventEncoder encoder(RtcTestEvent::event_params, batch_);
556 encoder.EncodeField(RtcTestEvent::bool_params,
557 ExtractRtcEventMember(batch_, &RtcTestEvent::b_));
558 encoder.EncodeField(RtcTestEvent::signed32_params,
559 ExtractRtcEventMember(batch_, &RtcTestEvent::signed32_));
560 encoder.EncodeField(
561 RtcTestEvent::unsigned32_params,
562 ExtractRtcEventMember(batch_, &RtcTestEvent::unsigned32_));
563 encoder.EncodeField(RtcTestEvent::signed64_params,
564 ExtractRtcEventMember(batch_, &RtcTestEvent::signed64_));
565 encoder.EncodeField(
566 RtcTestEvent::unsigned64_params,
567 ExtractRtcEventMember(batch_, &RtcTestEvent::unsigned64_));
568 encoder.EncodeField(
569 RtcTestEvent::optional32_params,
570 ExtractRtcEventMember(batch_, &RtcTestEvent::optional_signed32_));
571 encoder.EncodeField(
572 RtcTestEvent::optional64_params,
573 ExtractRtcEventMember(batch_, &RtcTestEvent::optional_signed64_));
574 encoder.EncodeField(
575 RtcTestEvent::wrapping21_params,
576 ExtractRtcEventMember(batch_, &RtcTestEvent::wrapping21_));
577 encoder.EncodeField(RtcTestEvent::string_params,
578 ExtractRtcEventMember(batch_, &RtcTestEvent::string_));
579 std::string s = encoder.AsString();
580
581 // Optional debug printing
582 // PrintBytes(s);
583
584 ParseEventHeader(s);
585 ParseAndVerifyTimestamps();
586 ParseAndVerifyField(RtcTestEvent::bool_params, bool_values,
587 /*delta bits*/ 1);
588 ParseAndVerifyField(RtcTestEvent::signed32_params, signed32_values,
589 /*delta bits*/ 1);
590 ParseAndVerifyField(RtcTestEvent::unsigned32_params, unsigned32_values,
591 /*delta bits*/ 1);
592 ParseAndVerifyField(RtcTestEvent::signed64_params, signed64_values,
593 /*delta bits*/ 1);
594 ParseAndVerifyField(RtcTestEvent::unsigned64_params, unsigned64_values,
595 /*delta bits*/ 1);
596 ParseAndVerifyOptionalField(RtcTestEvent::optional32_params,
597 optional32_values, /*delta bits*/ 1);
598 ParseAndVerifyOptionalField(RtcTestEvent::optional64_params,
599 optional64_values, /*delta bits*/ 1);
600 ParseAndVerifyField(RtcTestEvent::wrapping21_params, wrapping21_values,
601 /*delta bits*/ 1);
602 ParseAndVerifyStringField(RtcTestEvent::string_params, string_values);
603 EXPECT_EQ(parser_.RemainingBytes(), 0u);
604 }
605
TEST_F(RtcEventFieldTest,Decreasing)606 TEST_F(RtcEventFieldTest, Decreasing) {
607 std::vector<bool> bool_values = {true, false, true, false};
608 std::vector<int32_t> signed32_values = {2, 1, 0, -1};
609 std::vector<uint32_t> unsigned32_values = {1, 0, kUint32Max, kUint32Max - 1};
610 std::vector<int64_t> signed64_values = {kInt64Min + 1, kInt64Min, kInt64Max,
611 kInt64Max - 1};
612 std::vector<uint64_t> unsigned64_values = {1, 0, kUint64Max, kUint64Max - 1};
613 std::vector<absl::optional<int32_t>> optional32_values = {
614 kInt32Min + 1, kInt32Min, kInt32Max, kInt32Max - 1};
615 std::vector<absl::optional<int64_t>> optional64_values = {
616 kInt64Min + 1, kInt64Min, kInt64Max, kInt64Max - 1};
617 std::vector<uint32_t> wrapping21_values = {1, 0, (1 << 21) - 1,
618 (1 << 21) - 2};
619 std::vector<std::string> string_values = {
620 "def", "bc", "a", ""}; // No special compression of strings.
621
622 CreateFullEvents(bool_values, signed32_values, unsigned32_values,
623 signed64_values, unsigned64_values, optional32_values,
624 optional64_values, wrapping21_values, string_values);
625
626 EventEncoder encoder(RtcTestEvent::event_params, batch_);
627 encoder.EncodeField(RtcTestEvent::bool_params,
628 ExtractRtcEventMember(batch_, &RtcTestEvent::b_));
629 encoder.EncodeField(RtcTestEvent::signed32_params,
630 ExtractRtcEventMember(batch_, &RtcTestEvent::signed32_));
631 encoder.EncodeField(
632 RtcTestEvent::unsigned32_params,
633 ExtractRtcEventMember(batch_, &RtcTestEvent::unsigned32_));
634 encoder.EncodeField(RtcTestEvent::signed64_params,
635 ExtractRtcEventMember(batch_, &RtcTestEvent::signed64_));
636 encoder.EncodeField(
637 RtcTestEvent::unsigned64_params,
638 ExtractRtcEventMember(batch_, &RtcTestEvent::unsigned64_));
639 encoder.EncodeField(
640 RtcTestEvent::optional32_params,
641 ExtractRtcEventMember(batch_, &RtcTestEvent::optional_signed32_));
642 encoder.EncodeField(
643 RtcTestEvent::optional64_params,
644 ExtractRtcEventMember(batch_, &RtcTestEvent::optional_signed64_));
645 encoder.EncodeField(
646 RtcTestEvent::wrapping21_params,
647 ExtractRtcEventMember(batch_, &RtcTestEvent::wrapping21_));
648 encoder.EncodeField(RtcTestEvent::string_params,
649 ExtractRtcEventMember(batch_, &RtcTestEvent::string_));
650 std::string s = encoder.AsString();
651
652 // Optional debug printing
653 // PrintBytes(s);
654
655 ParseEventHeader(s);
656 ParseAndVerifyTimestamps();
657 ParseAndVerifyField(RtcTestEvent::bool_params, bool_values,
658 /*delta bits*/ 1);
659 ParseAndVerifyField(RtcTestEvent::signed32_params, signed32_values,
660 /*delta bits*/ 1);
661 ParseAndVerifyField(RtcTestEvent::unsigned32_params, unsigned32_values,
662 /*delta bits*/ 1);
663 ParseAndVerifyField(RtcTestEvent::signed64_params, signed64_values,
664 /*delta bits*/ 1);
665 ParseAndVerifyField(RtcTestEvent::unsigned64_params, unsigned64_values,
666 /*delta bits*/ 1);
667 ParseAndVerifyOptionalField(RtcTestEvent::optional32_params,
668 optional32_values, /*delta bits*/ 1);
669 ParseAndVerifyOptionalField(RtcTestEvent::optional64_params,
670 optional64_values, /*delta bits*/ 1);
671 ParseAndVerifyField(RtcTestEvent::wrapping21_params, wrapping21_values,
672 /*delta bits*/ 1);
673 ParseAndVerifyStringField(RtcTestEvent::string_params, string_values);
674 EXPECT_EQ(parser_.RemainingBytes(), 0u);
675 }
676
TEST_F(RtcEventFieldTest,SkipsDeprecatedFields)677 TEST_F(RtcEventFieldTest, SkipsDeprecatedFields) {
678 // Expect parser to skip fields it doesn't recognize, but find subsequent
679 // fields.
680 std::vector<bool> bool_values = {true, false};
681 std::vector<int32_t> signed32_values = {kInt32Min / 2, kInt32Max / 2};
682 std::vector<uint32_t> unsigned32_values = {0, kUint32Max / 2};
683 std::vector<int64_t> signed64_values = {kInt64Min / 2, kInt64Max / 2};
684 std::vector<uint64_t> unsigned64_values = {0, kUint64Max / 2};
685 std::vector<absl::optional<int32_t>> optional32_values = {kInt32Max / 2,
686 kInt32Min / 2};
687 std::vector<absl::optional<int64_t>> optional64_values = {kInt64Min / 2,
688 kInt64Max / 2};
689 std::vector<uint32_t> wrapping21_values = {0, 1 << 20};
690 std::vector<std::string> string_values = {"foo", "bar"};
691
692 size_t signed32_encoding_size =
693 /*tag*/ 1 + /* varint base*/ 5 + /* delta_header*/ 1 + /*deltas*/ 4;
694 size_t signed64_encoding_size =
695 /*tag*/ 1 + /* fixed64 base*/ 8 + /* delta_header*/ 1 + /*deltas*/ 8;
696 size_t optional32_encoding_size =
697 /*tag*/ 1 + /* fixed32 base*/ 4 + /* delta_header*/ 1 + /*deltas*/ 4;
698
699 CreateFullEvents(bool_values, signed32_values, unsigned32_values,
700 signed64_values, unsigned64_values, optional32_values,
701 optional64_values, wrapping21_values, string_values);
702
703 EventEncoder encoder(RtcTestEvent::event_params, batch_);
704 encoder.EncodeField(RtcTestEvent::bool_params,
705 ExtractRtcEventMember(batch_, &RtcTestEvent::b_));
706 encoder.EncodeField(RtcTestEvent::signed32_params,
707 ExtractRtcEventMember(batch_, &RtcTestEvent::signed32_));
708 encoder.EncodeField(
709 RtcTestEvent::unsigned32_params,
710 ExtractRtcEventMember(batch_, &RtcTestEvent::unsigned32_));
711 encoder.EncodeField(RtcTestEvent::signed64_params,
712 ExtractRtcEventMember(batch_, &RtcTestEvent::signed64_));
713 encoder.EncodeField(
714 RtcTestEvent::unsigned64_params,
715 ExtractRtcEventMember(batch_, &RtcTestEvent::unsigned64_));
716 encoder.EncodeField(
717 RtcTestEvent::optional32_params,
718 ExtractRtcEventMember(batch_, &RtcTestEvent::optional_signed32_));
719 encoder.EncodeField(
720 RtcTestEvent::optional64_params,
721 ExtractRtcEventMember(batch_, &RtcTestEvent::optional_signed64_));
722 encoder.EncodeField(
723 RtcTestEvent::wrapping21_params,
724 ExtractRtcEventMember(batch_, &RtcTestEvent::wrapping21_));
725 encoder.EncodeField(RtcTestEvent::string_params,
726 ExtractRtcEventMember(batch_, &RtcTestEvent::string_));
727 std::string s = encoder.AsString();
728
729 // Optional debug printing
730 // PrintBytes(s);
731
732 ParseEventHeader(s);
733 ParseAndVerifyTimestamps();
734 ParseAndVerifyField(RtcTestEvent::bool_params, bool_values,
735 /*delta_bits=*/1);
736 // Skips parsing the `signed32_values`. The following unsigned fields should
737 // still be found.
738 ParseAndVerifyField(RtcTestEvent::unsigned32_params, unsigned32_values,
739 /*delta_bits=*/31,
740 /*expected_skipped_bytes=*/signed32_encoding_size);
741 // Skips parsing the `signed64_values`. The following unsigned fields should
742 // still be found.
743 ParseAndVerifyField(RtcTestEvent::unsigned64_params, unsigned64_values,
744 /*delta_bits=*/63, signed64_encoding_size);
745 // Skips parsing the `optional32_values`. The following unsigned fields should
746 // still be found.
747 ParseAndVerifyOptionalField(RtcTestEvent::optional64_params,
748 optional64_values,
749 /*delta_bits=*/63, optional32_encoding_size);
750 ParseAndVerifyField(RtcTestEvent::wrapping21_params, wrapping21_values,
751 /*delta_bits=*/20);
752 ParseAndVerifyStringField(RtcTestEvent::string_params, string_values);
753 EXPECT_EQ(parser_.RemainingBytes(), 0u);
754 }
755
TEST_F(RtcEventFieldTest,SkipsMissingFields)756 TEST_F(RtcEventFieldTest, SkipsMissingFields) {
757 // Expect parsing of missing field to succeed but return an empty list.
758
759 std::vector<bool> bool_values = {true, false};
760 std::vector<int32_t> signed32_values = {kInt32Min / 2, kInt32Max / 2};
761 std::vector<uint32_t> unsigned32_values = {0, kUint32Max / 2};
762 std::vector<int64_t> signed64_values = {kInt64Min / 2, kInt64Max / 2};
763 std::vector<uint64_t> unsigned64_values = {0, kUint64Max / 2};
764 std::vector<absl::optional<int32_t>> optional32_values = {kInt32Max / 2,
765 kInt32Min / 2};
766 std::vector<absl::optional<int64_t>> optional64_values = {kInt64Min / 2,
767 kInt64Max / 2};
768 std::vector<uint32_t> wrapping21_values = {0, 1 << 20};
769 std::vector<std::string> string_values = {"foo", "foo"};
770
771 CreateFullEvents(bool_values, signed32_values, unsigned32_values,
772 signed64_values, unsigned64_values, optional32_values,
773 optional64_values, wrapping21_values, string_values);
774
775 EventEncoder encoder(RtcTestEvent::event_params, batch_);
776 // Skip encoding the `bool_values`.
777 encoder.EncodeField(RtcTestEvent::signed32_params,
778 ExtractRtcEventMember(batch_, &RtcTestEvent::signed32_));
779 // Skip encoding the `unsigned32_values`.
780 encoder.EncodeField(RtcTestEvent::signed64_params,
781 ExtractRtcEventMember(batch_, &RtcTestEvent::signed64_));
782 // Skip encoding the `unsigned64_values`.
783 encoder.EncodeField(
784 RtcTestEvent::optional32_params,
785 ExtractRtcEventMember(batch_, &RtcTestEvent::optional_signed32_));
786 // Skip encoding the `optional64_values`.
787 encoder.EncodeField(
788 RtcTestEvent::wrapping21_params,
789 ExtractRtcEventMember(batch_, &RtcTestEvent::wrapping21_));
790 encoder.EncodeField(RtcTestEvent::string_params,
791 ExtractRtcEventMember(batch_, &RtcTestEvent::string_));
792 std::string s = encoder.AsString();
793
794 // Optional debug printing
795 // PrintBytes(s);
796
797 ParseEventHeader(s);
798 ParseAndVerifyTimestamps();
799 ParseAndVerifyMissingField(RtcTestEvent::bool_params);
800 ParseAndVerifyField(RtcTestEvent::signed32_params, signed32_values,
801 /*delta_bits=*/31);
802 ParseAndVerifyMissingField(RtcTestEvent::unsigned32_params);
803 ParseAndVerifyField(RtcTestEvent::signed64_params, signed64_values,
804 /*delta_bits=*/63);
805 ParseAndVerifyMissingField(RtcTestEvent::unsigned64_params);
806 ParseAndVerifyOptionalField(RtcTestEvent::optional32_params,
807 optional32_values, /*delta_bits=*/31);
808 ParseAndVerifyMissingOptionalField(RtcTestEvent::optional64_params);
809 ParseAndVerifyField(RtcTestEvent::wrapping21_params, wrapping21_values,
810 /*delta_bits=*/20);
811 ParseAndVerifyStringField(RtcTestEvent::string_params, string_values);
812 EXPECT_EQ(parser_.RemainingBytes(), 0u);
813 }
814
TEST_F(RtcEventFieldTest,OptionalFields)815 TEST_F(RtcEventFieldTest, OptionalFields) {
816 std::vector<absl::optional<int32_t>> optional32_values = {
817 2, absl::nullopt, 4, absl::nullopt, 6, absl::nullopt};
818 std::vector<absl::optional<int64_t>> optional64_values = {
819 absl::nullopt, 1024, absl::nullopt, 1025, absl::nullopt, 1026};
820 std::vector<uint32_t> wrapping21_values = {(1 << 21) - 3, 0, 2, 5, 5, 6};
821
822 for (size_t i = 0; i < optional32_values.size(); i++) {
823 batch_.push_back(new RtcTestEvent(0, 0, 0, 0, 0, optional32_values[i],
824 optional64_values[i],
825 wrapping21_values[i], ""));
826 }
827
828 EventEncoder encoder(RtcTestEvent::event_params, batch_);
829 encoder.EncodeField(
830 RtcTestEvent::optional32_params,
831 ExtractRtcEventMember(batch_, &RtcTestEvent::optional_signed32_));
832 encoder.EncodeField(
833 RtcTestEvent::optional64_params,
834 ExtractRtcEventMember(batch_, &RtcTestEvent::optional_signed64_));
835 encoder.EncodeField(
836 RtcTestEvent::wrapping21_params,
837 ExtractRtcEventMember(batch_, &RtcTestEvent::wrapping21_));
838 std::string s = encoder.AsString();
839
840 // Optional debug output
841 // PrintBytes(s);
842
843 ParseEventHeader(s);
844 ParseAndVerifyTimestamps();
845 ParseAndVerifyOptionalField(RtcTestEvent::optional32_params,
846 optional32_values, /*delta bits*/ 2);
847 ParseAndVerifyOptionalField(RtcTestEvent::optional64_params,
848 optional64_values, /*delta bits*/ 1);
849 ParseAndVerifyField(RtcTestEvent::wrapping21_params, wrapping21_values,
850 /*delta bits*/ 2);
851 EXPECT_EQ(parser_.RemainingBytes(), 0u);
852 }
853
TEST_F(RtcEventFieldTest,AllNulloptTreatedAsMissing)854 TEST_F(RtcEventFieldTest, AllNulloptTreatedAsMissing) {
855 std::vector<absl::optional<int32_t>> optional32_values = {
856 absl::nullopt, absl::nullopt, absl::nullopt,
857 absl::nullopt, absl::nullopt, absl::nullopt};
858 std::vector<absl::optional<int64_t>> optional64_values = {
859 absl::nullopt, 1024, absl::nullopt, 1025, absl::nullopt, 1026};
860
861 for (size_t i = 0; i < optional32_values.size(); i++) {
862 batch_.push_back(new RtcTestEvent(0, 0, 0, 0, 0, optional32_values[i],
863 optional64_values[i], 0, ""));
864 }
865
866 EventEncoder encoder(RtcTestEvent::event_params, batch_);
867 encoder.EncodeField(
868 RtcTestEvent::optional32_params,
869 ExtractRtcEventMember(batch_, &RtcTestEvent::optional_signed32_));
870 encoder.EncodeField(
871 RtcTestEvent::optional64_params,
872 ExtractRtcEventMember(batch_, &RtcTestEvent::optional_signed64_));
873 std::string s = encoder.AsString();
874
875 // Optional debug output
876 // PrintBytes(s);
877
878 ParseEventHeader(s);
879 ParseAndVerifyTimestamps();
880 ParseAndVerifyMissingOptionalField(RtcTestEvent::optional32_params);
881 ParseAndVerifyOptionalField(RtcTestEvent::optional64_params,
882 optional64_values, /*delta_bits=*/1);
883 EXPECT_EQ(parser_.RemainingBytes(), 0u);
884 }
885
886 } // namespace webrtc
887