1 /*
2  * Copyright 2019 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 #define PACKET_TESTING
18 #include "packet/parser/test/big_endian_test_packets.h"
19 #include "packet/parser/test/test_packets.h"
20 
21 #include <gtest/gtest.h>
22 #include <forward_list>
23 #include <memory>
24 
25 #include "os/log.h"
26 #include "packet/bit_inserter.h"
27 #include "packet/parser/test/six_bytes.h"
28 #include "packet/raw_builder.h"
29 
30 using ::bluetooth::packet::BitInserter;
31 using ::bluetooth::packet::kLittleEndian;
32 using ::bluetooth::packet::RawBuilder;
33 using ::bluetooth::packet::parser::test::SixBytes;
34 using std::vector;
35 
36 namespace {
37 vector<uint8_t> child_two_two_three = {
38     0x20 /* Reserved : 4, FourBits::TWO */,
39     0x03 /* FourBits::THREE, Reserved : 4 */,
40 };
41 vector<uint8_t> child = {
42     0x12 /* fixed */, 0x02 /* Size of the payload */, 0xa1 /* First byte of the payload */, 0xa2, 0xb1 /* footer */,
43 };
44 vector<uint8_t> child_with_six_bytes = {
45     0x34 /* TwoBytes */,
46     0x12,
47     0xa1 /* First byte of the six_bytes */,
48     0xa2,
49     0xa3,
50     0xa4,
51     0xa5,
52     0xa6,
53     0xb1 /* Second six_bytes*/,
54     0xb2,
55     0xb3,
56     0xb4,
57     0xb5,
58     0xb6,
59 };
60 
61 }  // namespace
62 
63 namespace bluetooth {
64 namespace packet {
65 namespace parser {
66 using namespace test;
67 
TEST(GeneratedPacketTest,testChildTwoTwoThree)68 TEST(GeneratedPacketTest, testChildTwoTwoThree) {
69   auto packet = ChildTwoTwoThreeBuilder::Create();
70 
71   ASSERT_EQ(child_two_two_three.size(), packet->size());
72 
73   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
74   BitInserter it(*packet_bytes);
75   packet->Serialize(it);
76 
77   ASSERT_EQ(packet_bytes->size(), child_two_two_three.size());
78   for (size_t i = 0; i < child_two_two_three.size(); i++) {
79     ASSERT_EQ(packet_bytes->at(i), child_two_two_three[i]);
80   }
81 
82   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
83   ParentView wrong_view = ParentView::Create(packet_bytes_view);
84   ASSERT_FALSE(wrong_view.IsValid());
85 
86   ParentTwoView parent_view = ParentTwoView::Create(packet_bytes_view);
87   ASSERT_TRUE(parent_view.IsValid());
88   ASSERT_EQ(FourBits::TWO, parent_view.GetFourBits());
89 
90   ChildTwoTwoView child_view = ChildTwoTwoView::Create(parent_view);
91   ASSERT_TRUE(child_view.IsValid());
92   ASSERT_EQ(FourBits::THREE, child_view.GetMoreBits());
93 
94   ChildTwoTwoThreeView grandchild_view = ChildTwoTwoThreeView::Create(child_view);
95   ASSERT_TRUE(grandchild_view.IsValid());
96 }
97 
TEST(GeneratedPacketTest,testChild)98 TEST(GeneratedPacketTest, testChild) {
99   uint16_t field_name = 0xa2a1;
100   uint8_t footer = 0xb1;
101   auto packet = ChildBuilder::Create(field_name, footer);
102 
103   ASSERT_EQ(child.size(), packet->size());
104 
105   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
106   BitInserter it(*packet_bytes);
107   packet->Serialize(it);
108 
109   ASSERT_EQ(packet_bytes->size(), child.size());
110   for (size_t i = 0; i < child.size(); i++) {
111     ASSERT_EQ(packet_bytes->at(i), child[i]);
112   }
113 
114   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
115   ParentView parent_view = ParentView::Create(packet_bytes_view);
116   ASSERT_TRUE(parent_view.IsValid());
117   auto payload = parent_view.GetPayload();
118 
119   ASSERT_EQ(child[1 /* skip fixed field */], payload.size());
120   for (size_t i = 0; i < payload.size(); i++) {
121     ASSERT_EQ(child[i + 2 /* fixed & size */], payload[i]);
122   }
123 
124   ChildView child_view = ChildView::Create(parent_view);
125   ASSERT_TRUE(child_view.IsValid());
126 
127   ASSERT_EQ(field_name, child_view.GetFieldName());
128 }
129 
TEST(GeneratedPacketTest,testValidateWayTooSmall)130 TEST(GeneratedPacketTest, testValidateWayTooSmall) {
131   std::vector<uint8_t> too_small_bytes = {0x34};
132   auto too_small = std::make_shared<std::vector<uint8_t>>(too_small_bytes.begin(), too_small_bytes.end());
133 
134   ParentWithSixBytesView invalid_parent = ParentWithSixBytesView::Create(PacketView<kLittleEndian>(too_small));
135   ASSERT_FALSE(invalid_parent.IsValid());
136   ChildWithSixBytesView invalid =
137       ChildWithSixBytesView::Create(ParentWithSixBytesView::Create(PacketView<kLittleEndian>(too_small)));
138   ASSERT_FALSE(invalid.IsValid());
139 }
140 
TEST(GeneratedPacketTest,testValidateTooSmall)141 TEST(GeneratedPacketTest, testValidateTooSmall) {
142   std::vector<uint8_t> too_small_bytes = {0x34, 0x12, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x11};
143   auto too_small = std::make_shared<std::vector<uint8_t>>(too_small_bytes.begin(), too_small_bytes.end());
144 
145   ParentWithSixBytesView valid_parent = ParentWithSixBytesView::Create(PacketView<kLittleEndian>(too_small));
146   ASSERT_TRUE(valid_parent.IsValid());
147   ChildWithSixBytesView invalid =
148       ChildWithSixBytesView::Create(ParentWithSixBytesView::Create(PacketView<kLittleEndian>(too_small)));
149   ASSERT_FALSE(invalid.IsValid());
150 }
151 
TEST(GeneratedPacketTest,testValidateJustRight)152 TEST(GeneratedPacketTest, testValidateJustRight) {
153   std::vector<uint8_t> just_right_bytes = {0x34, 0x12, 0x01, 0x02, 0x03, 0x04, 0x05,
154                                            0x06, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
155   auto just_right = std::make_shared<std::vector<uint8_t>>(just_right_bytes.begin(), just_right_bytes.end());
156 
157   ChildWithSixBytesView valid =
158       ChildWithSixBytesView::Create(ParentWithSixBytesView::Create(PacketView<kLittleEndian>(just_right)));
159   ASSERT_TRUE(valid.IsValid());
160 }
161 
TEST(GeneratedPacketTest,testValidateTooBig)162 TEST(GeneratedPacketTest, testValidateTooBig) {
163   std::vector<uint8_t> too_big_bytes = {0x34, 0x12, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
164                                         0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x20};
165   auto too_big = std::make_shared<std::vector<uint8_t>>(too_big_bytes.begin(), too_big_bytes.end());
166 
167   ChildWithSixBytesView lenient =
168       ChildWithSixBytesView::Create(ParentWithSixBytesView::Create(PacketView<kLittleEndian>(too_big)));
169   ASSERT_TRUE(lenient.IsValid());
170 }
171 
TEST(GeneratedPacketTest,testValidateDeath)172 TEST(GeneratedPacketTest, testValidateDeath) {
173   auto packet = ChildTwoTwoThreeBuilder::Create();
174 
175   ASSERT_EQ(child_two_two_three.size(), packet->size());
176 
177   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
178   BitInserter it(*packet_bytes);
179   packet->Serialize(it);
180 
181   ASSERT_EQ(packet_bytes->size(), child_two_two_three.size());
182   for (size_t i = 0; i < child_two_two_three.size(); i++) {
183     ASSERT_EQ(packet_bytes->at(i), child_two_two_three[i]);
184   }
185 
186   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
187   ParentView wrong_view = ParentView::Create(packet_bytes_view);
188   ASSERT_DEATH(wrong_view.GetPayload(), "validated");
189 }
190 
TEST(GeneratedPacketTest,testValidatedParentDeath)191 TEST(GeneratedPacketTest, testValidatedParentDeath) {
192   uint16_t field_name = 0xa2a1;
193   uint8_t footer = 0xb1;
194   auto packet = ChildBuilder::Create(field_name, footer);
195 
196   ASSERT_EQ(child.size(), packet->size());
197 
198   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
199   BitInserter it(*packet_bytes);
200   packet->Serialize(it);
201 
202   ASSERT_EQ(packet_bytes->size(), child.size());
203   for (size_t i = 0; i < child.size(); i++) {
204     ASSERT_EQ(packet_bytes->at(i), child[i]);
205   }
206 
207   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
208   ParentView parent_view = ParentView::Create(packet_bytes_view);
209   ASSERT_TRUE(parent_view.IsValid());
210   auto payload = parent_view.GetPayload();
211 
212   ASSERT_EQ(child[1 /* skip fixed field */], payload.size());
213   for (size_t i = 0; i < payload.size(); i++) {
214     ASSERT_EQ(child[i + 2 /* fixed & size */], payload[i]);
215   }
216 
217   ChildView child_view = ChildView::Create(parent_view);
218   ASSERT_DEATH(child_view.GetFieldName(), "validated");
219 }
220 
221 vector<uint8_t> middle_four_bits = {
222     0x95,  // low_two = ONE, next_four = FIVE, straddle = TEN
223     0x8a,  // straddle = TEN, four_more = TWO, high_two = TWO
224 };
225 
TEST(GeneratedPacketTest,testMiddleFourBitsPacket)226 TEST(GeneratedPacketTest, testMiddleFourBitsPacket) {
227   TwoBits low_two = TwoBits::ONE;
228   FourBits next_four = FourBits::FIVE;
229   FourBits straddle = FourBits::TEN;
230   FourBits four_more = FourBits::TWO;
231   TwoBits high_two = TwoBits::TWO;
232 
233   auto packet = MiddleFourBitsBuilder::Create(low_two, next_four, straddle, four_more, high_two);
234 
235   ASSERT_EQ(middle_four_bits.size(), packet->size());
236 
237   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
238   BitInserter it(*packet_bytes);
239   packet->Serialize(it);
240 
241   ASSERT_EQ(packet_bytes->size(), middle_four_bits.size());
242   for (size_t i = 0; i < middle_four_bits.size(); i++) {
243     ASSERT_EQ(packet_bytes->at(i), middle_four_bits[i]);
244   }
245 
246   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
247   MiddleFourBitsView view = MiddleFourBitsView::Create(packet_bytes_view);
248   ASSERT_TRUE(view.IsValid());
249   ASSERT_EQ(low_two, view.GetLowTwo());
250   ASSERT_EQ(next_four, view.GetNextFour());
251   ASSERT_EQ(straddle, view.GetStraddle());
252   ASSERT_EQ(four_more, view.GetFourMore());
253   ASSERT_EQ(high_two, view.GetHighTwo());
254 }
255 
TEST(GeneratedPacketTest,testChildWithSixBytes)256 TEST(GeneratedPacketTest, testChildWithSixBytes) {
257   SixBytes six_bytes_a{{0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6}};
258   SixBytes six_bytes_b{{0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6}};
259   auto packet = ChildWithSixBytesBuilder::Create(six_bytes_a, six_bytes_b);
260 
261   ASSERT_EQ(child_with_six_bytes.size(), packet->size());
262 
263   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
264   BitInserter it(*packet_bytes);
265   packet->Serialize(it);
266 
267   ASSERT_EQ(packet_bytes->size(), child_with_six_bytes.size());
268   for (size_t i = 0; i < child_with_six_bytes.size(); i++) {
269     ASSERT_EQ(packet_bytes->at(i), child_with_six_bytes[i]);
270   }
271 
272   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
273   ParentWithSixBytesView parent_view = ParentWithSixBytesView::Create(packet_bytes_view);
274   ASSERT_TRUE(parent_view.IsValid());
275   ASSERT_EQ(six_bytes_a, parent_view.GetSixBytes());
276 
277   ChildWithSixBytesView child_view = ChildWithSixBytesView::Create(parent_view);
278   ASSERT_TRUE(child_view.IsValid());
279 
280   ASSERT_EQ(six_bytes_a, child_view.GetSixBytes());
281   ASSERT_EQ(six_bytes_a, ((ParentWithSixBytesView)child_view).GetSixBytes());
282   ASSERT_EQ(six_bytes_b, child_view.GetChildSixBytes());
283 }
284 
285 namespace {
286 vector<uint8_t> parent_with_sum = {
287     0x11 /* TwoBytes */, 0x12, 0x21 /* Sum Bytes */, 0x22, 0x43 /* Sum, excluding TwoBytes */, 0x00,
288 };
289 
290 }  // namespace
291 
TEST(GeneratedPacketTest,testParentWithSum)292 TEST(GeneratedPacketTest, testParentWithSum) {
293   uint16_t two_bytes = 0x1211;
294   uint16_t sum_bytes = 0x2221;
295   auto packet = ParentWithSumBuilder::Create(two_bytes, sum_bytes, std::make_unique<packet::RawBuilder>());
296 
297   ASSERT_EQ(parent_with_sum.size(), packet->size());
298 
299   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
300   BitInserter it(*packet_bytes);
301   packet->Serialize(it);
302 
303   ASSERT_EQ(packet_bytes->size(), parent_with_sum.size());
304   for (size_t i = 0; i < parent_with_sum.size(); i++) {
305     ASSERT_EQ(packet_bytes->at(i), parent_with_sum[i]);
306   }
307 
308   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
309   ParentWithSumView parent_view = ParentWithSumView::Create(packet_bytes_view);
310   ASSERT_TRUE(parent_view.IsValid());
311   ASSERT_EQ(two_bytes, parent_view.GetTwoBytes());
312 
313   // Corrupt checksum
314   packet_bytes->back()++;
315   PacketView<kLittleEndian> corrupted_bytes_view(packet_bytes);
316   ParentWithSumView corrupted_view = ParentWithSumView::Create(corrupted_bytes_view);
317   ASSERT_FALSE(corrupted_view.IsValid());
318 }
319 
320 namespace {
321 vector<uint8_t> child_with_nested_sum = {
322     0x11 /* TwoBytes */,
323     0x12,
324     0x21 /* Sum Bytes */,
325     0x22,
326     0x31 /* More Bytes */,
327     0x32,
328     0x33,
329     0x34,
330     0xca /* Nested Sum */,
331     0x00,
332     0xd7 /* Sum, excluding TwoBytes */,
333     0x01,
334 };
335 
336 }  // namespace
337 
TEST(GeneratedPacketTest,testChildWithNestedSum)338 TEST(GeneratedPacketTest, testChildWithNestedSum) {
339   uint16_t two_bytes = 0x1211;
340   uint16_t sum_bytes = 0x2221;
341   uint32_t more_bytes = 0x34333231;
342   auto packet = ChildWithNestedSumBuilder::Create(two_bytes, sum_bytes, more_bytes);
343 
344   ASSERT_EQ(child_with_nested_sum.size(), packet->size());
345 
346   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
347   BitInserter it(*packet_bytes);
348   packet->Serialize(it);
349 
350   ASSERT_EQ(packet_bytes->size(), child_with_nested_sum.size());
351   for (size_t i = 0; i < child_with_nested_sum.size(); i++) {
352     ASSERT_EQ(packet_bytes->at(i), child_with_nested_sum[i]);
353   }
354 
355   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
356   ParentWithSumView parent_view = ParentWithSumView::Create(packet_bytes_view);
357   ASSERT_TRUE(parent_view.IsValid());
358   ASSERT_EQ(two_bytes, parent_view.GetTwoBytes());
359 
360   ChildWithNestedSumView child_view = ChildWithNestedSumView::Create(parent_view);
361   ASSERT_TRUE(child_view.IsValid());
362 
363   ASSERT_EQ(more_bytes, child_view.GetMoreBytes());
364 }
365 
366 namespace {
367 vector<uint8_t> parent_size_modifier = {
368     0x02 /* Size */,
369     0x11 /* TwoBytes */,
370     0x12,
371 };
372 
373 }  // namespace
374 
TEST(GeneratedPacketTest,testParentSizeModifier)375 TEST(GeneratedPacketTest, testParentSizeModifier) {
376   uint16_t two_bytes = 0x1211;
377   auto packet = ParentSizeModifierBuilder::Create(std::make_unique<RawBuilder>(), two_bytes);
378 
379   ASSERT_EQ(parent_size_modifier.size(), packet->size());
380 
381   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
382   BitInserter it(*packet_bytes);
383   packet->Serialize(it);
384 
385   ASSERT_EQ(parent_size_modifier.size(), packet_bytes->size());
386   for (size_t i = 0; i < parent_size_modifier.size(); i++) {
387     ASSERT_EQ(parent_size_modifier[i], packet_bytes->at(i));
388   }
389 
390   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
391   ParentSizeModifierView parent_view = ParentSizeModifierView::Create(packet_bytes_view);
392   ASSERT_TRUE(parent_view.IsValid());
393   ASSERT_EQ(two_bytes, parent_view.GetTwoBytes());
394 }
395 
396 namespace {
397 vector<uint8_t> child_size_modifier = {
398     0x06 /* PayloadSize (TwoBytes + MoreBytes)*/,
399     0x31 /* MoreBytes */,
400     0x32,
401     0x33,
402     0x34,
403     0x11 /* TwoBytes = 0x1211 */,
404     0x12,
405 };
406 
407 }  // namespace
408 
TEST(GeneratedPacketTest,testChildSizeModifier)409 TEST(GeneratedPacketTest, testChildSizeModifier) {
410   uint16_t two_bytes = 0x1211;
411   uint32_t more_bytes = 0x34333231;
412   auto packet = ChildSizeModifierBuilder::Create(more_bytes);
413 
414   ASSERT_EQ(child_size_modifier.size(), packet->size());
415 
416   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
417   BitInserter it(*packet_bytes);
418   packet->Serialize(it);
419 
420   ASSERT_EQ(child_size_modifier.size(), packet_bytes->size());
421   for (size_t i = 0; i < child_size_modifier.size(); i++) {
422     ASSERT_EQ(child_size_modifier[i], packet_bytes->at(i));
423   }
424 
425   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
426   ParentSizeModifierView parent_view = ParentSizeModifierView::Create(packet_bytes_view);
427   ASSERT_TRUE(parent_view.IsValid());
428   ASSERT_EQ(two_bytes, parent_view.GetTwoBytes());
429 
430   ChildSizeModifierView child_view = ChildSizeModifierView::Create(parent_view);
431   ASSERT_TRUE(child_view.IsValid());
432 
433   ASSERT_EQ(more_bytes, child_view.GetMoreBytes());
434 }
435 
436 namespace {
437 vector<uint8_t> fixed_array_enum{
438     0x01,  // ONE
439     0x00,
440     0x02,  // TWO
441     0x00,
442     0x01,  // ONE_TWO
443     0x02,
444     0x02,  // TWO_THREE
445     0x03,
446     0xff,  // FFFF
447     0xff,
448 };
449 }
450 
TEST(GeneratedPacketTest,testFixedArrayEnum)451 TEST(GeneratedPacketTest, testFixedArrayEnum) {
452   std::array<ForArrays, 5> fixed_array{
453       {ForArrays::ONE, ForArrays::TWO, ForArrays::ONE_TWO, ForArrays::TWO_THREE, ForArrays::FFFF}};
454   auto packet = FixedArrayEnumBuilder::Create(fixed_array);
455   ASSERT_EQ(fixed_array_enum.size(), packet->size());
456 
457   // Verify that the packet is independent from the array.
458   std::array<ForArrays, 5> copy_array(fixed_array);
459   fixed_array[1] = ForArrays::ONE;
460 
461   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
462   BitInserter it(*packet_bytes);
463   packet->Serialize(it);
464 
465   ASSERT_EQ(fixed_array_enum.size(), packet_bytes->size());
466   for (size_t i = 0; i < fixed_array_enum.size(); i++) {
467     ASSERT_EQ(fixed_array_enum[i], packet_bytes->at(i));
468   }
469 
470   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
471   auto view = FixedArrayEnumView::Create(packet_bytes_view);
472   ASSERT_TRUE(view.IsValid());
473   auto array = view.GetEnumArray();
474   ASSERT_EQ(copy_array.size(), array.size());
475   for (size_t i = 0; i < copy_array.size(); i++) {
476     ASSERT_EQ(array[i], copy_array[i]);
477   }
478 }
479 
480 namespace {
481 vector<uint8_t> sized_array_enum{
482     0x0a,  // _size_
483     0x00,
484     0x01,  // ONE
485     0x00,
486     0x02,  // TWO
487     0x00,
488     0x01,  // ONE_TWO
489     0x02,
490     0x02,  // TWO_THREE
491     0x03,
492     0xff,  // FFFF
493     0xff,
494 };
495 }
496 
TEST(GeneratedPacketTest,testSizedArrayEnum)497 TEST(GeneratedPacketTest, testSizedArrayEnum) {
498   std::vector<ForArrays> sized_array{
499       {ForArrays::ONE, ForArrays::TWO, ForArrays::ONE_TWO, ForArrays::TWO_THREE, ForArrays::FFFF}};
500   auto packet = SizedArrayEnumBuilder::Create(sized_array);
501   ASSERT_EQ(sized_array_enum.size(), packet->size());
502 
503   // Copy the original vector and modify it to make sure the packet is independent.
504   std::vector<ForArrays> copy_array(sized_array);
505   sized_array[1] = ForArrays::ONE;
506 
507   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
508   BitInserter it(*packet_bytes);
509   packet->Serialize(it);
510 
511   ASSERT_EQ(sized_array_enum.size(), packet_bytes->size());
512   for (size_t i = 0; i < sized_array_enum.size(); i++) {
513     ASSERT_EQ(sized_array_enum[i], packet_bytes->at(i));
514   }
515 
516   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
517   auto view = SizedArrayEnumView::Create(packet_bytes_view);
518   ASSERT_TRUE(view.IsValid());
519   auto array = view.GetEnumArray();
520   ASSERT_EQ(copy_array.size(), array.size());
521   for (size_t i = 0; i < copy_array.size(); i++) {
522     ASSERT_EQ(array[i], copy_array[i]);
523   }
524 }
525 
526 namespace {
527 vector<uint8_t> count_array_enum{
528     0x03,  // _count_
529     0x01,  // ONE
530     0x00,
531     0x02,  // TWO_THREE
532     0x03,
533     0xff,  // FFFF
534     0xff,
535 };
536 }
537 
TEST(GeneratedPacketTest,testCountArrayEnum)538 TEST(GeneratedPacketTest, testCountArrayEnum) {
539   std::vector<ForArrays> count_array{{ForArrays::ONE, ForArrays::TWO_THREE, ForArrays::FFFF}};
540   auto packet = CountArrayEnumBuilder::Create(count_array);
541   ASSERT_EQ(count_array_enum.size(), packet->size());
542 
543   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
544   BitInserter it(*packet_bytes);
545   packet->Serialize(it);
546 
547   ASSERT_EQ(count_array_enum.size(), packet_bytes->size());
548   for (size_t i = 0; i < count_array_enum.size(); i++) {
549     ASSERT_EQ(count_array_enum[i], packet_bytes->at(i));
550   }
551 
552   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
553   auto view = CountArrayEnumView::Create(packet_bytes_view);
554   ASSERT_TRUE(view.IsValid());
555   auto array = view.GetEnumArray();
556   ASSERT_EQ(count_array.size(), array.size());
557   for (size_t i = 0; i < count_array.size(); i++) {
558     ASSERT_EQ(array[i], count_array[i]);
559   }
560 }
561 
TEST(GeneratedPacketTest,testFixedSizeByteArray)562 TEST(GeneratedPacketTest, testFixedSizeByteArray) {
563   constexpr std::size_t byte_array_size = 32;
564   std::array<uint8_t, byte_array_size> byte_array;
565   for (uint8_t i = 0; i < byte_array_size; i++) byte_array[i] = i;
566 
567   constexpr int word_array_size = 8;
568   std::array<uint32_t, word_array_size> word_array;
569   for (uint32_t i = 0; i < word_array_size; i++) word_array[i] = i;
570 
571   auto packet = PacketWithFixedArraysOfBytesBuilder::Create(byte_array, word_array);
572   ASSERT_EQ(2 * (256 / 8), packet->size());
573 
574   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
575   BitInserter it(*packet_bytes);
576   packet->Serialize(it);
577 
578   ASSERT_EQ(byte_array_size + word_array_size * sizeof(uint32_t), packet_bytes->size());
579 
580   for (size_t i = 0; i < byte_array_size; i++) {
581     ASSERT_EQ(byte_array[i], packet_bytes->at(i));
582   }
583 
584   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
585   auto view = PacketWithFixedArraysOfBytesView::Create(packet_bytes_view);
586   ASSERT_TRUE(view.IsValid());
587   auto array = view.GetFixed256bitInBytes();
588   ASSERT_EQ(byte_array.size(), array.size());
589   for (size_t i = 0; i < array.size(); i++) {
590     ASSERT_EQ(array[i], byte_array[i]);
591   }
592 
593   auto decoded_word_array = view.GetFixed256bitInWords();
594   ASSERT_EQ(word_array.size(), decoded_word_array.size());
595   for (size_t i = 0; i < decoded_word_array.size(); i++) {
596     ASSERT_EQ(word_array[i], decoded_word_array[i]);
597   }
598 }
599 
600 vector<uint8_t> one_variable{
601     0x03, 'o', 'n', 'e',  // "one"
602 };
603 
TEST(GeneratedPacketTest,testOneVariableField)604 TEST(GeneratedPacketTest, testOneVariableField) {
605   Variable variable_one{"one"};
606 
607   auto packet = OneVariableBuilder::Create(variable_one);
608   ASSERT_EQ(one_variable.size(), packet->size());
609 
610   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
611   BitInserter it(*packet_bytes);
612   packet->Serialize(it);
613 
614   ASSERT_EQ(one_variable.size(), packet_bytes->size());
615   for (size_t i = 0; i < one_variable.size(); i++) {
616     ASSERT_EQ(one_variable[i], packet_bytes->at(i));
617   }
618 
619   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
620   auto view = OneVariableView::Create(packet_bytes_view);
621   ASSERT_TRUE(view.IsValid());
622   auto one = view.GetOne();
623   ASSERT_EQ(one->data, variable_one.data);
624 }
625 
626 vector<uint8_t> fou_variable{
627     0x04, 'f', 'o', 'u',  // too short
628 };
629 
TEST(GeneratedPacketTest,testOneVariableFieldTooShort)630 TEST(GeneratedPacketTest, testOneVariableFieldTooShort) {
631   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>(fou_variable);
632   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
633   auto view = OneVariableView::Create(packet_bytes_view);
634   ASSERT_TRUE(view.IsValid());
635   auto one = view.GetOne();
636   ASSERT_EQ(one, nullptr);
637 }
638 
639 vector<uint8_t> sized_array_variable{
640     0x0e,                           // _size_
641     0x03, 'o', 'n', 'e',            // "one"
642     0x03, 't', 'w', 'o',            // "two"
643     0x05, 't', 'h', 'r', 'e', 'e',  // "three"
644 };
645 
TEST(GeneratedPacketTest,testSizedArrayVariableLength)646 TEST(GeneratedPacketTest, testSizedArrayVariableLength) {
647   std::vector<Variable> sized_array;
648   sized_array.emplace_back("one");
649   sized_array.emplace_back("two");
650   sized_array.emplace_back("three");
651 
652   auto packet = SizedArrayVariableBuilder::Create(sized_array);
653   ASSERT_EQ(sized_array_variable.size(), packet->size());
654 
655   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
656   BitInserter it(*packet_bytes);
657   packet->Serialize(it);
658 
659   ASSERT_EQ(sized_array_variable.size(), packet_bytes->size());
660   for (size_t i = 0; i < sized_array_variable.size(); i++) {
661     ASSERT_EQ(sized_array_variable[i], packet_bytes->at(i));
662   }
663 
664   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
665   auto view = SizedArrayVariableView::Create(packet_bytes_view);
666   ASSERT_TRUE(view.IsValid());
667   auto array = view.GetVariableArray();
668   ASSERT_EQ(sized_array.size(), array.size());
669   for (size_t i = 0; i < sized_array.size(); i++) {
670     ASSERT_EQ(array[i].data, sized_array[i].data);
671   }
672 }
673 
674 vector<uint8_t> sized_array_variable_too_short{
675     0x0e,                           // _size_
676     0x03, 'o', 'n', 'e',            // "one"
677     0x03, 't', 'w', 'o',            // "two"
678     0x06, 't', 'h', 'r', 'e', 'e',  // "three" needs another letter to be length 6
679 };
680 
TEST(GeneratedPacketTest,testSizedArrayVariableLengthLastBad)681 TEST(GeneratedPacketTest, testSizedArrayVariableLengthLastBad) {
682   std::vector<Variable> sized_array;
683   sized_array.emplace_back("one");
684   sized_array.emplace_back("two");
685 
686   std::shared_ptr<std::vector<uint8_t>> packet_bytes =
687       std::make_shared<std::vector<uint8_t>>(sized_array_variable_too_short);
688 
689   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
690   auto view = SizedArrayVariableView::Create(packet_bytes_view);
691   ASSERT_TRUE(view.IsValid());
692   auto array = view.GetVariableArray();
693   ASSERT_EQ(sized_array.size(), array.size());
694   for (size_t i = 0; i < sized_array.size(); i++) {
695     ASSERT_EQ(array[i].data, sized_array[i].data);
696   }
697 }
698 
699 vector<uint8_t> sized_array_variable_first_too_short{
700     0x0e,                           // _size_
701     0x02, 'o', 'n', 'e',            // "on"
702     0x03, 't', 'w', 'o',            // "two"
703     0x05, 't', 'h', 'r', 'e', 'e',  // "three"
704 };
705 
TEST(GeneratedPacketTest,testSizedArrayVariableLengthFirstBad)706 TEST(GeneratedPacketTest, testSizedArrayVariableLengthFirstBad) {
707   std::vector<Variable> sized_array;
708   sized_array.emplace_back("on");
709 
710   std::shared_ptr<std::vector<uint8_t>> packet_bytes =
711       std::make_shared<std::vector<uint8_t>>(sized_array_variable_first_too_short);
712 
713   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
714   auto view = SizedArrayVariableView::Create(packet_bytes_view);
715   ASSERT_TRUE(view.IsValid());
716   auto array = view.GetVariableArray();
717   ASSERT_EQ(sized_array.size(), array.size());
718   for (size_t i = 0; i < sized_array.size(); i++) {
719     ASSERT_EQ(array[i].data, sized_array[i].data);
720   }
721 }
722 
723 vector<uint8_t> fixed_array_variable{
724     0x03, 'o', 'n', 'e',            // "one"
725     0x03, 't', 'w', 'o',            // "two"
726     0x05, 't', 'h', 'r', 'e', 'e',  // "three"
727     0x04, 'f', 'o', 'u', 'r',       // "four"
728     0x04, 'f', 'i', 'v', 'e',       // "five"
729 };
730 
TEST(GeneratedPacketTest,testFixedArrayVariableLength)731 TEST(GeneratedPacketTest, testFixedArrayVariableLength) {
732   std::array<Variable, 5> fixed_array{std::string("one"), std::string("two"), std::string("three"), std::string("four"),
733                                       std::string("five")};
734 
735   auto packet = FixedArrayVariableBuilder::Create(fixed_array);
736   ASSERT_EQ(fixed_array_variable.size(), packet->size());
737 
738   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
739   BitInserter it(*packet_bytes);
740   packet->Serialize(it);
741 
742   ASSERT_EQ(fixed_array_variable.size(), packet_bytes->size());
743   for (size_t i = 0; i < fixed_array_variable.size(); i++) {
744     ASSERT_EQ(fixed_array_variable[i], packet_bytes->at(i));
745   }
746 
747   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
748   auto view = FixedArrayVariableView::Create(packet_bytes_view);
749   ASSERT_TRUE(view.IsValid());
750   auto array = view.GetVariableArray();
751   ASSERT_EQ(fixed_array.size(), array.size());
752   for (size_t i = 0; i < fixed_array.size(); i++) {
753     ASSERT_EQ(array[i].data, fixed_array[i].data);
754   }
755 }
756 
757 vector<uint8_t> fixed_array_variable_too_short{
758     0x03, 'o', 'n', 'e',            // "one"
759     0x03, 't', 'w', 'o',            // "two"
760     0x05, 't', 'h', 'r', 'e', 'e',  // "three"
761     0x04, 'f', 'o', 'u', 'r',       // "four"
762     0x05, 'f', 'i', 'v', 'e',       // "five"
763 };
764 
TEST(GeneratedPacketTest,testFixedArrayVariableLengthTooShort)765 TEST(GeneratedPacketTest, testFixedArrayVariableLengthTooShort) {
766   std::array<Variable, 5> fixed_array{std::string("one"), std::string("two"), std::string("three"),
767                                       std::string("four")};
768 
769   std::shared_ptr<std::vector<uint8_t>> packet_bytes =
770       std::make_shared<std::vector<uint8_t>>(fixed_array_variable_too_short);
771 
772   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
773   auto view = FixedArrayVariableView::Create(packet_bytes_view);
774   ASSERT_TRUE(view.IsValid());
775   auto array = view.GetVariableArray();
776   ASSERT_EQ(fixed_array.size(), array.size());
777   for (size_t i = 0; i < fixed_array.size(); i++) {
778     ASSERT_EQ(array[i].data, fixed_array[i].data);
779   }
780 }
781 
782 vector<uint8_t> count_array_variable{
783     0x04,                           // _count_
784     0x03, 'o', 'n', 'e',            // "one"
785     0x03, 't', 'w', 'o',            // "two"
786     0x05, 't', 'h', 'r', 'e', 'e',  // "three"
787     0x04, 'f', 'o', 'u', 'r',       // "four"
788 };
789 
TEST(GeneratedPacketTest,testCountArrayVariableLength)790 TEST(GeneratedPacketTest, testCountArrayVariableLength) {
791   std::vector<Variable> count_array;
792   count_array.emplace_back("one");
793   count_array.emplace_back("two");
794   count_array.emplace_back("three");
795   count_array.emplace_back("four");
796 
797   auto packet = CountArrayVariableBuilder::Create(count_array);
798   ASSERT_EQ(count_array_variable.size(), packet->size());
799 
800   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
801   BitInserter it(*packet_bytes);
802   packet->Serialize(it);
803 
804   ASSERT_EQ(count_array_variable.size(), packet_bytes->size());
805   for (size_t i = 0; i < count_array_variable.size(); i++) {
806     ASSERT_EQ(count_array_variable[i], packet_bytes->at(i));
807   }
808 
809   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
810   auto view = CountArrayVariableView::Create(packet_bytes_view);
811   ASSERT_TRUE(view.IsValid());
812   auto array = view.GetVariableArray();
813   ASSERT_EQ(count_array.size(), array.size());
814   for (size_t i = 0; i < count_array.size(); i++) {
815     ASSERT_EQ(array[i].data, count_array[i].data);
816   }
817 }
818 
819 vector<uint8_t> count_array_variable_extra{
820     0x04,                           // _count_
821     0x03, 'o', 'n', 'e',            // "one"
822     0x03, 't', 'w', 'o',            // "two"
823     0x05, 't', 'h', 'r', 'e', 'e',  // "three"
824     0x04, 'f', 'o', 'u', 'r',       // "four"
825     0x04, 'x', 't', 'r', 'a',       // "xtra"
826 };
827 
TEST(GeneratedPacketTest,testCountArrayVariableLengthExtraData)828 TEST(GeneratedPacketTest, testCountArrayVariableLengthExtraData) {
829   std::vector<Variable> count_array;
830   count_array.emplace_back("one");
831   count_array.emplace_back("two");
832   count_array.emplace_back("three");
833   count_array.emplace_back("four");
834 
835   std::shared_ptr<std::vector<uint8_t>> packet_bytes =
836       std::make_shared<std::vector<uint8_t>>(count_array_variable_extra);
837 
838   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
839   auto view = CountArrayVariableView::Create(packet_bytes_view);
840   ASSERT_TRUE(view.IsValid());
841   auto array = view.GetVariableArray();
842   ASSERT_EQ(count_array.size(), array.size());
843   for (size_t i = 0; i < count_array.size(); i++) {
844     ASSERT_EQ(array[i].data, count_array[i].data);
845   }
846 }
847 
848 vector<uint8_t> count_array_variable_too_few{
849     0x04,                           // _count_
850     0x03, 'o', 'n', 'e',            // "one"
851     0x03, 't', 'w', 'o',            // "two"
852     0x05, 't', 'h', 'r', 'e', 'e',  // "three"
853 };
854 
TEST(GeneratedPacketTest,testCountArrayVariableLengthMissingData)855 TEST(GeneratedPacketTest, testCountArrayVariableLengthMissingData) {
856   std::vector<Variable> count_array;
857   count_array.emplace_back("one");
858   count_array.emplace_back("two");
859   count_array.emplace_back("three");
860 
861   std::shared_ptr<std::vector<uint8_t>> packet_bytes =
862       std::make_shared<std::vector<uint8_t>>(count_array_variable_too_few);
863 
864   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
865   auto view = CountArrayVariableView::Create(packet_bytes_view);
866   ASSERT_TRUE(view.IsValid());
867   auto array = view.GetVariableArray();
868   ASSERT_EQ(count_array.size(), array.size());
869   for (size_t i = 0; i < count_array.size(); i++) {
870     ASSERT_EQ(array[i].data, count_array[i].data);
871   }
872 }
873 
874 vector<uint8_t> one_struct{
875     0x01, 0x02, 0x03,  // id = 0x01, count = 0x0302
876 };
877 
TEST(GeneratedPacketTest,testOneStruct)878 TEST(GeneratedPacketTest, testOneStruct) {
879   TwoRelatedNumbers trn;
880   trn.id_ = 1;
881   trn.count_ = 0x0302;
882 
883   auto packet = OneStructBuilder::Create(trn);
884   ASSERT_EQ(one_struct.size(), packet->size());
885 
886   // Copy the original struct, then modify it to verify independence from the packet.
887   TwoRelatedNumbers copy_trn(trn);
888   trn.id_ = 2;
889 
890   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
891   BitInserter it(*packet_bytes);
892   packet->Serialize(it);
893 
894   ASSERT_EQ(one_struct.size(), packet_bytes->size());
895   for (size_t i = 0; i < one_struct.size(); i++) {
896     ASSERT_EQ(one_struct[i], packet_bytes->at(i));
897   }
898 
899   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
900   auto view = OneStructView::Create(packet_bytes_view);
901   ASSERT_TRUE(view.IsValid());
902   auto one = view.GetOne();
903   ASSERT_EQ(one.id_, copy_trn.id_);
904   ASSERT_EQ(one.count_, copy_trn.count_);
905 }
906 
907 vector<uint8_t> two_structs{
908     0x01, 0x01, 0x02,  // id, id * 0x0201
909     0x02, 0x02, 0x04,
910 };
911 
TEST(GeneratedPacketTest,testTwoStructs)912 TEST(GeneratedPacketTest, testTwoStructs) {
913   std::vector<TwoRelatedNumbers> count_array;
914   for (uint8_t i = 1; i < 3; i++) {
915     TwoRelatedNumbers trn;
916     trn.id_ = i;
917     trn.count_ = 0x0201 * i;
918     count_array.push_back(trn);
919   }
920 
921   auto packet = TwoStructsBuilder::Create(count_array[0], count_array[1]);
922   ASSERT_EQ(two_structs.size(), packet->size());
923 
924   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
925   BitInserter it(*packet_bytes);
926   packet->Serialize(it);
927 
928   ASSERT_EQ(two_structs.size(), packet_bytes->size());
929   for (size_t i = 0; i < two_structs.size(); i++) {
930     ASSERT_EQ(two_structs[i], packet_bytes->at(i));
931   }
932 
933   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
934   auto view = TwoStructsView::Create(packet_bytes_view);
935   ASSERT_TRUE(view.IsValid());
936   auto one = view.GetOne();
937   ASSERT_EQ(one.id_, count_array[0].id_);
938   ASSERT_EQ(one.count_, count_array[0].count_);
939   auto two = view.GetTwo();
940   ASSERT_EQ(two.id_, count_array[1].id_);
941   ASSERT_EQ(two.count_, count_array[1].count_);
942 }
943 
944 vector<uint8_t> array_or_vector_of_struct{
945     0x04,              // _count_
946     0x01, 0x01, 0x02,  // id, id * 0x0201
947     0x02, 0x02, 0x04, 0x03, 0x03, 0x06, 0x04, 0x04, 0x08,
948 };
949 
TEST(GeneratedPacketTest,testVectorOfStruct)950 TEST(GeneratedPacketTest, testVectorOfStruct) {
951   std::vector<TwoRelatedNumbers> count_array;
952   for (uint8_t i = 1; i < 5; i++) {
953     TwoRelatedNumbers trn;
954     trn.id_ = i;
955     trn.count_ = 0x0201 * i;
956     count_array.push_back(trn);
957   }
958 
959   // Make a copy
960   std::vector<TwoRelatedNumbers> copy_array(count_array);
961 
962   auto packet = VectorOfStructBuilder::Create(count_array);
963 
964   // Change the original vector to make sure a copy was made.
965   count_array[0].id_ = count_array[0].id_ + 1;
966 
967   ASSERT_EQ(array_or_vector_of_struct.size(), packet->size());
968 
969   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
970   BitInserter it(*packet_bytes);
971   packet->Serialize(it);
972 
973   ASSERT_EQ(array_or_vector_of_struct.size(), packet_bytes->size());
974   for (size_t i = 0; i < array_or_vector_of_struct.size(); i++) {
975     ASSERT_EQ(array_or_vector_of_struct[i], packet_bytes->at(i));
976   }
977 
978   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
979   auto view = VectorOfStructView::Create(packet_bytes_view);
980   ASSERT_TRUE(view.IsValid());
981   auto array = view.GetArray();
982   ASSERT_EQ(copy_array.size(), array.size());
983   for (size_t i = 0; i < copy_array.size(); i++) {
984     ASSERT_EQ(array[i].id_, copy_array[i].id_);
985     ASSERT_EQ(array[i].count_, copy_array[i].count_);
986   }
987 }
988 
TEST(GeneratedPacketTest,testArrayOfStruct)989 TEST(GeneratedPacketTest, testArrayOfStruct) {
990   std::array<TwoRelatedNumbers, 4> count_array;
991   for (uint8_t i = 1; i < 5; i++) {
992     TwoRelatedNumbers trn;
993     trn.id_ = i;
994     trn.count_ = 0x0201 * i;
995     count_array[i - 1] = trn;
996   }
997 
998   // Make a copy
999   std::array<TwoRelatedNumbers, 4> copy_array(count_array);
1000 
1001   auto packet = ArrayOfStructBuilder::Create(4, count_array);
1002 
1003   // Change the original vector to make sure a copy was made.
1004   count_array[0].id_ = count_array[0].id_ + 1;
1005 
1006   ASSERT_EQ(array_or_vector_of_struct.size(), packet->size());
1007 
1008   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
1009   BitInserter it(*packet_bytes);
1010   packet->Serialize(it);
1011 
1012   ASSERT_EQ(array_or_vector_of_struct.size(), packet_bytes->size());
1013   for (size_t i = 0; i < array_or_vector_of_struct.size(); i++) {
1014     ASSERT_EQ(array_or_vector_of_struct[i], packet_bytes->at(i));
1015   }
1016 
1017   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
1018   auto view = ArrayOfStructView::Create(packet_bytes_view);
1019   ASSERT_TRUE(view.IsValid());
1020   auto array = view.GetArray();
1021   ASSERT_EQ(copy_array.size(), array.size());
1022   for (size_t i = 0; i < copy_array.size(); i++) {
1023     ASSERT_EQ(array[i].id_, copy_array[i].id_);
1024     ASSERT_EQ(array[i].count_, copy_array[i].count_);
1025   }
1026 }
1027 
1028 vector<uint8_t> one_fixed_types_struct{
1029     0x05,                                // four_bits = FIVE, reserved
1030     0xf3,                                // _fixed_
1031     0x0d,                                // id = 0x0d
1032     0x01, 0x02, 0x03,                    // array = { 1, 2, 3}
1033     0x06, 0x01,                          // example_checksum
1034     0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6,  // six_bytes
1035 };
1036 
TEST(GeneratedPacketTest,testOneFixedTypesStruct)1037 TEST(GeneratedPacketTest, testOneFixedTypesStruct) {
1038   StructWithFixedTypes swf;
1039   swf.four_bits_ = FourBits::FIVE;
1040   swf.id_ = 0x0d;
1041   swf.array_ = {{0x01, 0x02, 0x03}};
1042   swf.six_bytes_ = SixBytes{{0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6}};
1043 
1044   auto packet = OneFixedTypesStructBuilder::Create(swf);
1045   ASSERT_EQ(one_fixed_types_struct.size(), packet->size());
1046 
1047   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
1048   BitInserter it(*packet_bytes);
1049   packet->Serialize(it);
1050 
1051   ASSERT_EQ(one_fixed_types_struct.size(), packet_bytes->size());
1052   for (size_t i = 0; i < one_fixed_types_struct.size(); i++) {
1053     ASSERT_EQ(one_fixed_types_struct[i], packet_bytes->at(i));
1054   }
1055 
1056   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
1057   auto view = OneFixedTypesStructView::Create(packet_bytes_view);
1058   ASSERT_TRUE(view.IsValid());
1059   auto one = view.GetOne();
1060   ASSERT_EQ(one.four_bits_, swf.four_bits_);
1061   ASSERT_EQ(one.id_, swf.id_);
1062   ASSERT_EQ(one.array_, swf.array_);
1063   ASSERT_EQ(one.six_bytes_, swf.six_bytes_);
1064 }
1065 
1066 vector<uint8_t> array_of_struct_and_another{
1067     0x03,              // _count_
1068     0x01, 0x01, 0x02,  // id, id * 0x0201
1069     0x02, 0x02, 0x04,  // 2
1070     0x03, 0x03, 0x06,  // 3
1071     0x04, 0x04, 0x08,  // Another
1072 };
1073 
TEST(GeneratedPacketTest,testArrayOfStructAndAnother)1074 TEST(GeneratedPacketTest, testArrayOfStructAndAnother) {
1075   std::vector<TwoRelatedNumbers> count_array;
1076   for (uint8_t i = 1; i < 4; i++) {
1077     TwoRelatedNumbers trn;
1078     trn.id_ = i;
1079     trn.count_ = 0x0201 * i;
1080     count_array.push_back(trn);
1081   }
1082   TwoRelatedNumbers another;
1083   another.id_ = 4;
1084   another.count_ = 0x0201 * 4;
1085 
1086   auto packet = ArrayOfStructAndAnotherBuilder::Create(count_array, another);
1087   ASSERT_EQ(array_or_vector_of_struct.size(), packet->size());
1088 
1089   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
1090   BitInserter it(*packet_bytes);
1091   packet->Serialize(it);
1092 
1093   ASSERT_EQ(array_of_struct_and_another.size(), packet_bytes->size());
1094   for (size_t i = 0; i < array_of_struct_and_another.size(); i++) {
1095     ASSERT_EQ(array_of_struct_and_another[i], packet_bytes->at(i));
1096   }
1097 
1098   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
1099   auto view = ArrayOfStructAndAnotherView::Create(packet_bytes_view);
1100   ASSERT_TRUE(view.IsValid());
1101   auto array = view.GetArray();
1102   ASSERT_EQ(count_array.size(), array.size());
1103   for (size_t i = 0; i < count_array.size(); i++) {
1104     ASSERT_EQ(array[i].id_, count_array[i].id_);
1105     ASSERT_EQ(array[i].count_, count_array[i].count_);
1106   }
1107   auto nother = view.GetAnother();
1108   ASSERT_EQ(nother.id_, another.id_);
1109   ASSERT_EQ(nother.count_, another.count_);
1110 }
1111 
1112 DEFINE_AND_INSTANTIATE_OneArrayOfStructAndAnotherStructReflectionTest(array_of_struct_and_another);
1113 
TEST(GeneratedPacketTest,testOneArrayOfStructAndAnotherStruct)1114 TEST(GeneratedPacketTest, testOneArrayOfStructAndAnotherStruct) {
1115   std::shared_ptr<std::vector<uint8_t>> packet_bytes =
1116       std::make_shared<std::vector<uint8_t>>(array_of_struct_and_another);
1117 
1118   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
1119   auto view = OneArrayOfStructAndAnotherStructView::Create(packet_bytes_view);
1120   ASSERT_TRUE(view.IsValid());
1121   auto one = view.GetOne();
1122   ASSERT_EQ(one.array_.size(), 3);
1123   ASSERT_EQ(one.another_.id_, 4);
1124   ASSERT_EQ(one.another_.count_, 0x0804);
1125 }
1126 
1127 vector<uint8_t> sized_array_of_struct_and_another{
1128     0x09,              // _size_
1129     0x01, 0x01, 0x02,  // id, id * 0x0201
1130     0x02, 0x02, 0x04,  // 2
1131     0x03, 0x03, 0x06,  // 3
1132     0x04, 0x04, 0x08,  // Another
1133 };
1134 
1135 DEFINE_AND_INSTANTIATE_OneSizedArrayOfStructAndAnotherStructReflectionTest(sized_array_of_struct_and_another);
1136 
1137 vector<uint8_t> bit_field_group_packet{
1138     // seven_bits_ = 0x77, straddle_ = 0x5, five_bits_ = 0x15
1139     0xf7,  // 0x77 | (0x5 & 0x1) << 7
1140     0xaa,  //  0x15 << 3 | (0x5 >> 1)
1141 };
1142 
TEST(GeneratedPacketTest,testBitFieldGroupPacket)1143 TEST(GeneratedPacketTest, testBitFieldGroupPacket) {
1144   uint8_t seven_bits = 0x77;
1145   uint8_t straddle = 0x5;
1146   uint8_t five_bits = 0x15;
1147 
1148   auto packet = BitFieldGroupPacketBuilder::Create(seven_bits, straddle, five_bits);
1149   ASSERT_EQ(bit_field_group_packet.size(), packet->size());
1150 
1151   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
1152   BitInserter it(*packet_bytes);
1153   packet->Serialize(it);
1154 
1155   ASSERT_EQ(bit_field_group_packet.size(), packet_bytes->size());
1156   for (size_t i = 0; i < bit_field_group_packet.size(); i++) {
1157     ASSERT_EQ(bit_field_group_packet[i], packet_bytes->at(i));
1158   }
1159 
1160   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
1161   auto view = BitFieldGroupPacketView::Create(packet_bytes_view);
1162   ASSERT_TRUE(view.IsValid());
1163   ASSERT_EQ(seven_bits, view.GetSevenBits());
1164   ASSERT_EQ(straddle, view.GetStraddle());
1165   ASSERT_EQ(five_bits, view.GetFiveBits());
1166 }
1167 
1168 vector<uint8_t> bit_field_packet{
1169     // seven_bits_ = 0x77, straddle_ = 0x5, five_bits_ = 0x15
1170     0xf7,  // 0x77 | (0x5 & 0x1) << 7
1171     0xaa,  //  0x15 << 3 | (0x5 >> 1)
1172 };
1173 
TEST(GeneratedPacketTest,testBitFieldPacket)1174 TEST(GeneratedPacketTest, testBitFieldPacket) {
1175   BitField bit_field;
1176   bit_field.seven_bits_ = 0x77;
1177   bit_field.straddle_ = 0x5;
1178   bit_field.five_bits_ = 0x15;
1179 
1180   auto packet = BitFieldPacketBuilder::Create(bit_field);
1181   ASSERT_EQ(bit_field_packet.size(), packet->size());
1182 
1183   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
1184   BitInserter it(*packet_bytes);
1185   packet->Serialize(it);
1186 
1187   ASSERT_EQ(bit_field_packet.size(), packet_bytes->size());
1188   for (size_t i = 0; i < bit_field_packet.size(); i++) {
1189     ASSERT_EQ(bit_field_packet[i], packet_bytes->at(i));
1190   }
1191 
1192   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
1193   auto view = BitFieldPacketView::Create(packet_bytes_view);
1194   ASSERT_TRUE(view.IsValid());
1195   BitField bf = view.GetBitField();
1196   ASSERT_EQ(bf.seven_bits_, bit_field.seven_bits_);
1197   ASSERT_EQ(bf.straddle_, bit_field.straddle_);
1198   ASSERT_EQ(bf.five_bits_, bit_field.five_bits_);
1199 }
1200 
1201 vector<uint8_t> bit_field_group_after_unsized_array_packet{
1202     0x01, 0x02, 0x03, 0x04,  // byte array
1203     // seven_bits_ = 0x77, straddle_ = 0x5, five_bits_ = 0x15
1204     0xf7,  // 0x77 | (0x5 & 0x1) << 7
1205     0xaa,  //  0x15 << 3 | (0x5 >> 1)
1206 };
1207 
TEST(GeneratedPacketTest,testBitFieldGroupAfterUnsizedArrayPacket)1208 TEST(GeneratedPacketTest, testBitFieldGroupAfterUnsizedArrayPacket) {
1209   std::vector<uint8_t> count_array;
1210   for (uint8_t i = 1; i < 5; i++) {
1211     count_array.push_back(i);
1212   }
1213   uint8_t seven_bits = 0x77;
1214   uint8_t straddle = 0x5;
1215   uint8_t five_bits = 0x15;
1216 
1217   auto packet = BitFieldGroupAfterUnsizedArrayPacketBuilder::Create(count_array, seven_bits, straddle, five_bits);
1218   ASSERT_EQ(bit_field_group_after_unsized_array_packet.size(), packet->size());
1219 
1220   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
1221   BitInserter it(*packet_bytes);
1222   packet->Serialize(it);
1223 
1224   ASSERT_EQ(bit_field_group_after_unsized_array_packet.size(), packet_bytes->size());
1225   for (size_t i = 0; i < bit_field_group_after_unsized_array_packet.size(); i++) {
1226     ASSERT_EQ(bit_field_group_after_unsized_array_packet[i], packet_bytes->at(i));
1227   }
1228 
1229   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
1230   auto payload_view = BitFieldGroupAfterPayloadPacketView::Create(packet_bytes_view);
1231   ASSERT_TRUE(payload_view.IsValid());
1232   EXPECT_EQ(seven_bits, payload_view.GetSevenBits());
1233   EXPECT_EQ(straddle, payload_view.GetStraddle());
1234   EXPECT_EQ(five_bits, payload_view.GetFiveBits());
1235 
1236   auto view = BitFieldGroupAfterUnsizedArrayPacketView::Create(payload_view);
1237   ASSERT_TRUE(view.IsValid());
1238   auto array = view.GetArray();
1239   ASSERT_EQ(count_array.size(), array.size());
1240   for (size_t i = 0; i < count_array.size(); i++) {
1241     ASSERT_EQ(array[i], count_array[i]);
1242   }
1243   ASSERT_EQ(seven_bits, view.GetSevenBits());
1244   ASSERT_EQ(straddle, view.GetStraddle());
1245   ASSERT_EQ(five_bits, view.GetFiveBits());
1246 }
1247 
1248 vector<uint8_t> bit_field_after_unsized_array_packet{
1249     0x01, 0x02, 0x03, 0x04,  // byte array
1250     // seven_bits_ = 0x77, straddle_ = 0x5, five_bits_ = 0x15
1251     0xf7,  // 0x77 | (0x5 & 0x1) << 7
1252     0xaa,  //  0x15 << 3 | (0x5 >> 1)
1253 };
1254 
TEST(GeneratedPacketTest,testBitFieldAfterUnsizedArrayPacket)1255 TEST(GeneratedPacketTest, testBitFieldAfterUnsizedArrayPacket) {
1256   std::vector<uint8_t> count_array;
1257   for (uint8_t i = 1; i < 5; i++) {
1258     count_array.push_back(i);
1259   }
1260   BitField bit_field;
1261   bit_field.seven_bits_ = 0x77;
1262   bit_field.straddle_ = 0x5;
1263   bit_field.five_bits_ = 0x15;
1264 
1265   auto packet = BitFieldAfterUnsizedArrayPacketBuilder::Create(count_array, bit_field);
1266   ASSERT_EQ(bit_field_after_unsized_array_packet.size(), packet->size());
1267 
1268   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
1269   BitInserter it(*packet_bytes);
1270   packet->Serialize(it);
1271 
1272   ASSERT_EQ(bit_field_after_unsized_array_packet.size(), packet_bytes->size());
1273   for (size_t i = 0; i < bit_field_after_unsized_array_packet.size(); i++) {
1274     ASSERT_EQ(bit_field_after_unsized_array_packet[i], packet_bytes->at(i));
1275   }
1276 
1277   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
1278   auto payload_view = BitFieldAfterPayloadPacketView::Create(packet_bytes_view);
1279   ASSERT_TRUE(payload_view.IsValid());
1280   BitField parent_bf = payload_view.GetBitField();
1281   ASSERT_EQ(parent_bf.seven_bits_, bit_field.seven_bits_);
1282   ASSERT_EQ(parent_bf.straddle_, bit_field.straddle_);
1283   ASSERT_EQ(parent_bf.five_bits_, bit_field.five_bits_);
1284 
1285   auto view = BitFieldAfterUnsizedArrayPacketView::Create(payload_view);
1286   ASSERT_TRUE(view.IsValid());
1287   auto array = view.GetArray();
1288   ASSERT_EQ(count_array.size(), array.size());
1289   for (size_t i = 0; i < count_array.size(); i++) {
1290     ASSERT_EQ(array[i], count_array[i]);
1291   }
1292   BitField bf = view.GetBitField();
1293   ASSERT_EQ(bf.seven_bits_, bit_field.seven_bits_);
1294   ASSERT_EQ(bf.straddle_, bit_field.straddle_);
1295   ASSERT_EQ(bf.five_bits_, bit_field.five_bits_);
1296 }
1297 
1298 vector<uint8_t> bit_field_array_packet{
1299     0x06,  // _size_(array)
1300     // seven_bits_ = 0x77, straddle_ = 0x5, five_bits_ = 0x15
1301     0xf7,  // 0x77 | (0x5 & 0x1) << 7
1302     0xaa,  //  0x15 << 3 | (0x5 >> 1)
1303 
1304     // seven_bits_ = 0x78, straddle_ = 0x6, five_bits_ = 0x16
1305     0x78,  // 0x78 | (0x6 & 0x1) << 7
1306     0xb3,  //  0x16 << 3 | (0x6 >> 1)
1307 
1308     // seven_bits_ = 0x79, straddle_ = 0x7, five_bits_ = 0x17
1309     0xf9,  // 0x79 | (0x7 & 0x1) << 7
1310     0xbb,  //  0x17 << 3 | (0x7 >> 1)
1311 };
1312 
TEST(GeneratedPacketTest,testBitFieldArrayPacket)1313 TEST(GeneratedPacketTest, testBitFieldArrayPacket) {
1314   std::vector<BitField> count_array;
1315   for (size_t i = 0; i < 3; i++) {
1316     BitField bf;
1317     bf.seven_bits_ = 0x77 + i;
1318     bf.straddle_ = 0x5 + i;
1319     bf.five_bits_ = 0x15 + i;
1320     count_array.push_back(bf);
1321   }
1322 
1323   auto packet = BitFieldArrayPacketBuilder::Create(count_array);
1324   ASSERT_EQ(bit_field_array_packet.size(), packet->size());
1325 
1326   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
1327   BitInserter it(*packet_bytes);
1328   packet->Serialize(it);
1329 
1330   ASSERT_EQ(bit_field_array_packet.size(), packet_bytes->size());
1331   for (size_t i = 0; i < bit_field_array_packet.size(); i++) {
1332     ASSERT_EQ(bit_field_array_packet[i], packet_bytes->at(i));
1333   }
1334 
1335   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
1336   auto view = BitFieldArrayPacketView::Create(packet_bytes_view);
1337   ASSERT_TRUE(view.IsValid());
1338   auto array = view.GetArray();
1339   ASSERT_EQ(count_array.size(), array.size());
1340   for (size_t i = 0; i < count_array.size(); i++) {
1341     ASSERT_EQ(array[i].seven_bits_, count_array[i].seven_bits_);
1342     ASSERT_EQ(array[i].straddle_, count_array[i].straddle_);
1343     ASSERT_EQ(array[i].five_bits_, count_array[i].five_bits_);
1344   }
1345 }
1346 
TEST(GeneratedPacketTest,testNewBitFieldArrayPacket)1347 TEST(GeneratedPacketTest, testNewBitFieldArrayPacket) {
1348   PacketView<kLittleEndian> packet_bytes_view(std::make_shared<std::vector<uint8_t>>(bit_field_array_packet));
1349   auto view = BitFieldArrayPacketView::Create(packet_bytes_view);
1350   ASSERT_TRUE(view.IsValid());
1351 
1352   auto packet = BitFieldArrayPacketBuilder::Create(view.GetArray());
1353   ASSERT_EQ(bit_field_array_packet.size(), packet->size());
1354 
1355   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
1356   BitInserter it(*packet_bytes);
1357   packet->Serialize(it);
1358 
1359   ASSERT_EQ(*packet_bytes, bit_field_array_packet);
1360 }
1361 
1362 std::vector<uint8_t> child_two_two_two_ = {0x20, 0x02};
1363 std::vector<uint8_t> child_two_two_three_ = {0x20, 0x03};
1364 std::vector<uint8_t> child_two_two_four_ = {0x20, 0x04};
1365 
1366 DEFINE_AND_INSTANTIATE_ParentTwoReflectionTest(child_two_two_two_, child_two_two_three_, child_two_two_four_);
1367 
1368 DEFINE_AND_INSTANTIATE_ChildTwoTwoReflectionTest(child_two_two_two_, child_two_two_three_, child_two_two_four_);
1369 
1370 DEFINE_AND_INSTANTIATE_ChildTwoTwoThreeReflectionTest(child_two_two_three_);
1371 
1372 std::vector<uint8_t> one_versionless_struct_packet = {0x01};
1373 std::vector<uint8_t> one_versioned_struct_packet = {0x02, 0x03 /* version */, 0x04, 0x05, 0x06};
1374 std::vector<uint8_t> one_version_one_struct_packet = {0x03, 0x01 /* version */, 0x02};
1375 std::vector<uint8_t> one_version_two_struct_packet = {0x03, 0x02 /* version */, 0x03, 0x04};
1376 DEFINE_AND_INSTANTIATE_OneVersionlessStructPacketReflectionTest(one_versionless_struct_packet,
1377                                                                 one_versioned_struct_packet,
1378                                                                 one_version_one_struct_packet,
1379                                                                 one_version_two_struct_packet);
1380 DEFINE_AND_INSTANTIATE_OneVersionedStructPacketReflectionTest(one_versioned_struct_packet,
1381                                                               one_version_one_struct_packet,
1382                                                               one_version_two_struct_packet);
1383 DEFINE_AND_INSTANTIATE_OneVersionOneStructPacketReflectionTest(one_version_one_struct_packet);
1384 DEFINE_AND_INSTANTIATE_OneVersionTwoStructPacketReflectionTest(one_version_two_struct_packet);
1385 
1386 vector<uint8_t> one_struct_be{
1387     0x01, 0x02, 0x03,  // id = 0x01, count = 0x0203
1388 };
1389 
TEST(GeneratedPacketTest,testOneStructBe)1390 TEST(GeneratedPacketTest, testOneStructBe) {
1391   TwoRelatedNumbersBe trn;
1392   trn.id_ = 1;
1393   trn.count_ = 0x0203;
1394 
1395   auto packet = OneStructBeBuilder::Create(trn);
1396   ASSERT_EQ(one_struct_be.size(), packet->size());
1397 
1398   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
1399   BitInserter it(*packet_bytes);
1400   packet->Serialize(it);
1401 
1402   ASSERT_EQ(one_struct_be.size(), packet_bytes->size());
1403   for (size_t i = 0; i < one_struct_be.size(); i++) {
1404     ASSERT_EQ(one_struct_be[i], packet_bytes->at(i));
1405   }
1406 
1407   PacketView<!kLittleEndian> packet_bytes_view(packet_bytes);
1408   auto view = OneStructBeView::Create(packet_bytes_view);
1409   ASSERT_TRUE(view.IsValid());
1410   auto one = view.GetOne();
1411   ASSERT_EQ(one.id_, trn.id_);
1412   ASSERT_EQ(one.count_, trn.count_);
1413 }
1414 
1415 vector<uint8_t> two_structs_be{
1416     0x01, 0x01, 0x02,  // id, id * 0x0102
1417     0x02, 0x02, 0x04,
1418 };
1419 
TEST(GeneratedPacketTest,testTwoStructsBe)1420 TEST(GeneratedPacketTest, testTwoStructsBe) {
1421   std::vector<TwoRelatedNumbersBe> count_array;
1422   for (uint8_t i = 1; i < 3; i++) {
1423     TwoRelatedNumbersBe trn;
1424     trn.id_ = i;
1425     trn.count_ = 0x0102 * i;
1426     count_array.push_back(trn);
1427   }
1428 
1429   auto packet = TwoStructsBeBuilder::Create(count_array[0], count_array[1]);
1430   ASSERT_EQ(two_structs_be.size(), packet->size());
1431 
1432   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
1433   BitInserter it(*packet_bytes);
1434   packet->Serialize(it);
1435 
1436   ASSERT_EQ(two_structs_be.size(), packet_bytes->size());
1437   for (size_t i = 0; i < two_structs_be.size(); i++) {
1438     ASSERT_EQ(two_structs_be[i], packet_bytes->at(i));
1439   }
1440 
1441   PacketView<!kLittleEndian> packet_bytes_view(packet_bytes);
1442   auto view = TwoStructsBeView::Create(packet_bytes_view);
1443   ASSERT_TRUE(view.IsValid());
1444   auto one = view.GetOne();
1445   ASSERT_EQ(one.id_, count_array[0].id_);
1446   ASSERT_EQ(one.count_, count_array[0].count_);
1447   auto two = view.GetTwo();
1448   ASSERT_EQ(two.id_, count_array[1].id_);
1449   ASSERT_EQ(two.count_, count_array[1].count_);
1450 }
1451 
1452 vector<uint8_t> array_of_struct_be{
1453     0x04,              // _count_
1454     0x01, 0x01, 0x02,  // id, id * 0x0102
1455     0x02, 0x02, 0x04, 0x03, 0x03, 0x06, 0x04, 0x04, 0x08,
1456 };
1457 
TEST(GeneratedPacketTest,testArrayOfStructBe)1458 TEST(GeneratedPacketTest, testArrayOfStructBe) {
1459   std::vector<TwoRelatedNumbersBe> count_array;
1460   for (uint8_t i = 1; i < 5; i++) {
1461     TwoRelatedNumbersBe trn;
1462     trn.id_ = i;
1463     trn.count_ = 0x0102 * i;
1464     count_array.push_back(trn);
1465   }
1466 
1467   auto packet = ArrayOfStructBeBuilder::Create(count_array);
1468 
1469   ASSERT_EQ(array_of_struct_be.size(), packet->size());
1470 
1471   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
1472   BitInserter it(*packet_bytes);
1473   packet->Serialize(it);
1474 
1475   ASSERT_EQ(array_of_struct_be.size(), packet_bytes->size());
1476   for (size_t i = 0; i < array_of_struct_be.size(); i++) {
1477     ASSERT_EQ(array_of_struct_be[i], packet_bytes->at(i));
1478   }
1479 
1480   PacketView<!kLittleEndian> packet_bytes_view(packet_bytes);
1481   auto view = ArrayOfStructBeView::Create(packet_bytes_view);
1482   ASSERT_TRUE(view.IsValid());
1483   auto array = view.GetArray();
1484   ASSERT_EQ(count_array.size(), array.size());
1485   for (size_t i = 0; i < count_array.size(); i++) {
1486     ASSERT_EQ(array[i].id_, count_array[i].id_);
1487     ASSERT_EQ(array[i].count_, count_array[i].count_);
1488   }
1489 }
1490 
1491 vector<uint8_t> one_four_byte_struct{
1492     0x04,                    // struct_type_ = FourByte
1493     0xd1, 0xd2, 0xd3, 0xd4,  // four_bytes_
1494 };
1495 
TEST(GeneratedPacketTest,testOneFourByteStruct)1496 TEST(GeneratedPacketTest, testOneFourByteStruct) {
1497   FourByteStruct four_byte_struct;
1498   four_byte_struct.four_bytes_ = 0xd4d3d2d1;
1499 
1500   auto packet = OneFourByteStructBuilder::Create(four_byte_struct);
1501   ASSERT_EQ(one_four_byte_struct.size(), packet->size());
1502 
1503   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
1504   BitInserter it(*packet_bytes);
1505   packet->Serialize(it);
1506 
1507   ASSERT_EQ(one_four_byte_struct.size(), packet_bytes->size());
1508   for (size_t i = 0; i < one_four_byte_struct.size(); i++) {
1509     ASSERT_EQ(one_four_byte_struct[i], packet_bytes->at(i));
1510   }
1511 
1512   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
1513   auto view = OneFourByteStructView::Create(packet_bytes_view);
1514   ASSERT_TRUE(view.IsValid());
1515   ASSERT_EQ(StructType::FOUR_BYTE, view.GetOneStruct().struct_type_);
1516   ASSERT_EQ(four_byte_struct.four_bytes_, view.GetOneStruct().four_bytes_);
1517 }
1518 
1519 vector<uint8_t> generic_struct_two{
1520     0x02,        // struct_type_ = TwoByte
1521     0x01, 0x02,  // two_bytes_
1522 };
1523 
TEST(GeneratedPacketTest,testOneGenericStructTwo)1524 TEST(GeneratedPacketTest, testOneGenericStructTwo) {
1525   TwoByteStruct two_byte_struct;
1526   two_byte_struct.two_bytes_ = 0x0201;
1527   std::unique_ptr<TwoByteStruct> two_byte_struct_ptr = std::make_unique<TwoByteStruct>(two_byte_struct);
1528 
1529   auto packet = OneGenericStructBuilder::Create(std::move(two_byte_struct_ptr));
1530   ASSERT_EQ(generic_struct_two.size(), packet->size());
1531 
1532   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
1533   BitInserter it(*packet_bytes);
1534   packet->Serialize(it);
1535 
1536   ASSERT_EQ(generic_struct_two.size(), packet_bytes->size());
1537   for (size_t i = 0; i < generic_struct_two.size(); i++) {
1538     ASSERT_EQ(generic_struct_two[i], packet_bytes->at(i));
1539   }
1540 
1541   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
1542   auto view = OneGenericStructView::Create(packet_bytes_view);
1543   ASSERT_TRUE(view.IsValid());
1544   auto base_struct = view.GetBaseStruct();
1545   ASSERT_NE(nullptr, base_struct);
1546   ASSERT_TRUE(TwoByteStruct::IsInstance(*base_struct));
1547   TwoByteStruct* two_byte = static_cast<TwoByteStruct*>(base_struct.get());
1548   ASSERT_NE(nullptr, two_byte);
1549   ASSERT_TRUE(TwoByteStruct::IsInstance(*two_byte));
1550   ASSERT_EQ(two_byte_struct.two_bytes_, 0x0201);
1551   uint16_t val = two_byte->two_bytes_;
1552   ASSERT_EQ(val, 0x0201);
1553   ASSERT_EQ(two_byte_struct.two_bytes_, ((TwoByteStruct*)base_struct.get())->two_bytes_);
1554 }
1555 
1556 vector<uint8_t> generic_struct_four{
1557     0x04,                    // struct_type_ = FourByte
1558     0x01, 0x02, 0x03, 0x04,  // four_bytes_
1559 };
1560 
TEST(GeneratedPacketTest,testOneGenericStructFour)1561 TEST(GeneratedPacketTest, testOneGenericStructFour) {
1562   FourByteStruct four_byte_struct;
1563   four_byte_struct.four_bytes_ = 0x04030201;
1564   std::unique_ptr<FourByteStruct> four_byte_struct_p = std::make_unique<FourByteStruct>(four_byte_struct);
1565   ASSERT_EQ(four_byte_struct.four_bytes_, four_byte_struct_p->four_bytes_);
1566 
1567   auto packet = OneGenericStructBuilder::Create(std::move(four_byte_struct_p));
1568   ASSERT_EQ(generic_struct_four.size(), packet->size());
1569 
1570   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
1571   BitInserter it(*packet_bytes);
1572   packet->Serialize(it);
1573 
1574   ASSERT_EQ(generic_struct_four.size(), packet_bytes->size());
1575   for (size_t i = 0; i < generic_struct_four.size(); i++) {
1576     ASSERT_EQ(generic_struct_four[i], packet_bytes->at(i));
1577   }
1578 
1579   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
1580   auto view = OneGenericStructView::Create(packet_bytes_view);
1581   ASSERT_TRUE(view.IsValid());
1582   auto base_struct = view.GetBaseStruct();
1583   ASSERT_NE(nullptr, base_struct);
1584   ASSERT_EQ(StructType::FOUR_BYTE, base_struct->struct_type_);
1585   ASSERT_EQ(four_byte_struct.four_bytes_, ((FourByteStruct*)base_struct.get())->four_bytes_);
1586 }
1587 
1588 vector<uint8_t> one_struct_array{
1589     0x04,                    // struct_type_ = FourByte
1590     0xa1, 0xa2, 0xa3, 0xa4,  // four_bytes_
1591     0x04,                    // struct_type_ = FourByte
1592     0xb2, 0xb2, 0xb3, 0xb4,  // four_bytes_
1593     0x02,                    // struct_type_ = TwoByte
1594     0xc3, 0xc2,              // two_bytes_
1595     0x04,                    // struct_type_ = TwoByte
1596     0xd4, 0xd2, 0xd3, 0xd4,  // four_bytes_
1597 };
1598 
TEST(GeneratedPacketTest,testOneGenericStructArray)1599 TEST(GeneratedPacketTest, testOneGenericStructArray) {
1600   std::vector<std::unique_ptr<UnusedParentStruct>> parent_vector;
1601   std::unique_ptr<FourByteStruct> fbs;
1602   std::unique_ptr<TwoByteStruct> tbs;
1603   fbs = std::make_unique<FourByteStruct>();
1604   fbs->four_bytes_ = 0xa4a3a2a1;
1605   parent_vector.push_back(std::move(fbs));
1606   fbs = std::make_unique<FourByteStruct>();
1607   fbs->four_bytes_ = 0xb4b3b2b2;
1608   parent_vector.push_back(std::move(fbs));
1609   tbs = std::make_unique<TwoByteStruct>();
1610   tbs->two_bytes_ = 0xc2c3;
1611   parent_vector.push_back(std::move(tbs));
1612   fbs = std::make_unique<FourByteStruct>();
1613   fbs->four_bytes_ = 0xd4d3d2d4;
1614   parent_vector.push_back(std::move(fbs));
1615 
1616   std::vector<std::unique_ptr<UnusedParentStruct>> vector_copy;
1617   for (auto& s : parent_vector) {
1618     if (s->struct_type_ == StructType::TWO_BYTE) {
1619       vector_copy.push_back(std::make_unique<TwoByteStruct>(*(TwoByteStruct*)s.get()));
1620     }
1621     if (s->struct_type_ == StructType::FOUR_BYTE) {
1622       vector_copy.push_back(std::make_unique<FourByteStruct>(*(FourByteStruct*)s.get()));
1623     }
1624   }
1625 
1626   auto packet = OneGenericStructArrayBuilder::Create(std::move(parent_vector));
1627   ASSERT_EQ(one_struct_array.size(), packet->size());
1628 
1629   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
1630   BitInserter it(*packet_bytes);
1631   packet->Serialize(it);
1632 
1633   ASSERT_EQ(one_struct_array.size(), packet_bytes->size());
1634   for (size_t i = 0; i < one_struct_array.size(); i++) {
1635     ASSERT_EQ(one_struct_array[i], packet_bytes->at(i));
1636   }
1637 
1638   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
1639   auto view = OneGenericStructArrayView::Create(packet_bytes_view);
1640   ASSERT_TRUE(view.IsValid());
1641   auto an_array = view.GetAnArray();
1642   ASSERT_EQ(vector_copy.size(), an_array.size());
1643   for (size_t i = 0; i < vector_copy.size(); i++) {
1644     ASSERT_NE(nullptr, an_array[i]);
1645     ASSERT_EQ(vector_copy[i]->struct_type_, an_array[i]->struct_type_);
1646     if (vector_copy[i]->struct_type_ == StructType::FOUR_BYTE) {
1647       ASSERT_EQ(FourByteStruct::Specialize(vector_copy[i].get())->four_bytes_,
1648                 FourByteStruct::Specialize(an_array[i].get())->four_bytes_);
1649     } else {
1650       ASSERT_EQ(TwoByteStruct::Specialize(vector_copy[i].get())->two_bytes_,
1651                 TwoByteStruct::Specialize(an_array[i].get())->two_bytes_);
1652     }
1653   }
1654 }
1655 
TEST(GeneratedPacketTest,testOneGenericStructFourArray)1656 TEST(GeneratedPacketTest, testOneGenericStructFourArray) {
1657   std::array<std::unique_ptr<UnusedParentStruct>, 4> parent_vector;
1658   std::unique_ptr<FourByteStruct> fbs;
1659   std::unique_ptr<TwoByteStruct> tbs;
1660   fbs = std::make_unique<FourByteStruct>();
1661   fbs->four_bytes_ = 0xa4a3a2a1;
1662   parent_vector[0] = std::move(fbs);
1663   fbs = std::make_unique<FourByteStruct>();
1664   fbs->four_bytes_ = 0xb4b3b2b2;
1665   parent_vector[1] = std::move(fbs);
1666   tbs = std::make_unique<TwoByteStruct>();
1667   tbs->two_bytes_ = 0xc2c3;
1668   parent_vector[2] = std::move(tbs);
1669   fbs = std::make_unique<FourByteStruct>();
1670   fbs->four_bytes_ = 0xd4d3d2d4;
1671   parent_vector[3] = std::move(fbs);
1672 
1673   std::array<std::unique_ptr<UnusedParentStruct>, 4> vector_copy;
1674   size_t index = 0;
1675   for (auto& s : parent_vector) {
1676     if (s->struct_type_ == StructType::TWO_BYTE) {
1677       vector_copy[index] = std::make_unique<TwoByteStruct>(*(TwoByteStruct*)s.get());
1678     }
1679     if (s->struct_type_ == StructType::FOUR_BYTE) {
1680       vector_copy[index] = std::make_unique<FourByteStruct>(*(FourByteStruct*)s.get());
1681     }
1682     index++;
1683   }
1684 
1685   auto packet = OneGenericStructFourArrayBuilder::Create(std::move(parent_vector));
1686   ASSERT_EQ(one_struct_array.size(), packet->size());
1687 
1688   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
1689   BitInserter it(*packet_bytes);
1690   packet->Serialize(it);
1691 
1692   ASSERT_EQ(one_struct_array.size(), packet_bytes->size());
1693   for (size_t i = 0; i < one_struct_array.size(); i++) {
1694     ASSERT_EQ(one_struct_array[i], packet_bytes->at(i));
1695   }
1696 
1697   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
1698   auto view = OneGenericStructFourArrayView::Create(packet_bytes_view);
1699   ASSERT_TRUE(view.IsValid());
1700   auto an_array = view.GetAnArray();
1701   ASSERT_EQ(vector_copy.size(), an_array.size());
1702   for (size_t i = 0; i < vector_copy.size(); i++) {
1703     ASSERT_NE(nullptr, an_array[i]);
1704     ASSERT_EQ(vector_copy[i]->struct_type_, an_array[i]->struct_type_);
1705     if (vector_copy[i]->struct_type_ == StructType::FOUR_BYTE) {
1706       ASSERT_EQ(FourByteStruct::Specialize(vector_copy[i].get())->four_bytes_,
1707                 FourByteStruct::Specialize(an_array[i].get())->four_bytes_);
1708     } else {
1709       ASSERT_EQ(TwoByteStruct::Specialize(vector_copy[i].get())->two_bytes_,
1710                 TwoByteStruct::Specialize(an_array[i].get())->two_bytes_);
1711     }
1712   }
1713 }
1714 
1715 vector<uint8_t> one_struct_array_after_fixed{
1716     0x01, 0x02,              // two_bytes = 0x0201
1717     0x04,                    // struct_type_ = FourByte
1718     0xa1, 0xa2, 0xa3, 0xa4,  // four_bytes_
1719     0x04,                    // struct_type_ = FourByte
1720     0xb2, 0xb2, 0xb3, 0xb4,  // four_bytes_
1721     0x02,                    // struct_type_ = TwoByte
1722     0xc3, 0xc2,              // two_bytes_
1723     0x04,                    // struct_type_ = TwoByte
1724     0xd4, 0xd2, 0xd3, 0xd4,  // four_bytes_
1725 };
1726 
1727 DEFINE_AND_INSTANTIATE_OneGenericStructArrayAfterFixedReflectionTest(one_struct_array_after_fixed);
1728 
1729 vector<uint8_t> one_length_type_value_struct{
1730     // _size_(value):16 type value
1731     0x04, 0x00, 0x01, 'o', 'n', 'e',            // ONE
1732     0x04, 0x00, 0x02, 't', 'w', 'o',            // TWO
1733     0x06, 0x00, 0x03, 't', 'h', 'r', 'e', 'e',  // THREE
1734 };
1735 
1736 DEFINE_AND_INSTANTIATE_OneLengthTypeValueStructReflectionTest(one_length_type_value_struct);
1737 
TEST(GeneratedPacketTest,testOneLengthTypeValueStruct)1738 TEST(GeneratedPacketTest, testOneLengthTypeValueStruct) {
1739   std::shared_ptr<std::vector<uint8_t>> packet_bytes =
1740       std::make_shared<std::vector<uint8_t>>(one_length_type_value_struct);
1741 
1742   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
1743   auto view = OneLengthTypeValueStructView::Create(packet_bytes_view);
1744   ASSERT_TRUE(view.IsValid());
1745   auto one = view.GetOneArray();
1746   size_t entry_id = 0;
1747   for (const auto& entry : one) {
1748     switch (entry_id++) {
1749       case 0:
1750         ASSERT_EQ(entry.type_, DataType::ONE);
1751         ASSERT_EQ(entry.value_, std::vector<uint8_t>({'o', 'n', 'e'}));
1752         break;
1753       case 1:
1754         ASSERT_EQ(entry.type_, DataType::TWO);
1755         ASSERT_EQ(entry.value_, std::vector<uint8_t>({'t', 'w', 'o'}));
1756         break;
1757       case 2:
1758         ASSERT_EQ(entry.type_, DataType::THREE);
1759         ASSERT_EQ(entry.value_, std::vector<uint8_t>({'t', 'h', 'r', 'e', 'e'}));
1760         break;
1761       default:
1762         ASSERT_EQ(entry.type_, DataType::UNUSED);
1763     }
1764   }
1765 }
1766 
1767 vector<uint8_t> one_length_type_value_struct_padded_20{
1768     0x27,  // _size_(payload),
1769     // _size_(value):16 type value
1770     0x04, 0x00, 0x01, 'o', 'n', 'e',                             // ONE
1771     0x04, 0x00, 0x02, 't', 'w', 'o',                             // TWO
1772     0x06, 0x00, 0x03, 't', 'h', 'r', 'e', 'e',                   // THREE
1773     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,        // padding to 30
1774     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  // padding to 40
1775 };
1776 
1777 vector<uint8_t> one_length_type_value_struct_padded_28{
1778     0x27,  // _size_(payload),
1779     // _size_(value):16 type value
1780     0x04, 0x00, 0x01, 'o', 'n', 'e',                             // ONE
1781     0x04, 0x00, 0x02, 't', 'w', 'o',                             // TWO
1782     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,                    // padding to 20
1783     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  // padding to 30
1784     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  // padding to 40
1785 };
1786 
1787 // TODO: Revisit LTV parsing.  Right now, the padding bytes are parsed
1788 // DEFINE_AND_INSTANTIATE_OneLengthTypeValueStructPaddedReflectionTest(one_length_type_value_struct_padded_20,
1789 // one_length_type_value_struct_padded_28);
1790 
TEST(GeneratedPacketTest,testOneLengthTypeValueStructPaddedGeneration)1791 TEST(GeneratedPacketTest, testOneLengthTypeValueStructPaddedGeneration) {
1792   std::vector<LengthTypeValueStruct> ltv_vector;
1793   LengthTypeValueStruct ltv;
1794   ltv.type_ = DataType::ONE;
1795   ltv.value_ = {
1796       'o',
1797       'n',
1798       'e',
1799   };
1800   ltv_vector.push_back(ltv);
1801   ltv.type_ = DataType::TWO;
1802   ltv.value_ = {
1803       't',
1804       'w',
1805       'o',
1806   };
1807   ltv_vector.push_back(ltv);
1808 
1809   auto packet = OneLengthTypeValueStructPaddedBuilder::Create(ltv_vector);
1810   ASSERT_EQ(one_length_type_value_struct_padded_28.size(), packet->size());
1811 
1812   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
1813   BitInserter it(*packet_bytes);
1814   packet->Serialize(it);
1815 
1816   ASSERT_EQ(one_length_type_value_struct_padded_28.size(), packet_bytes->size());
1817   for (size_t i = 0; i < one_length_type_value_struct_padded_28.size(); i++) {
1818     ASSERT_EQ(one_length_type_value_struct_padded_28[i], packet_bytes->at(i));
1819   }
1820 
1821   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
1822   auto view = OneLengthTypeValueStructPaddedView::Create(SizedParentView::Create(packet_bytes_view));
1823   ASSERT_TRUE(view.IsValid());
1824   auto an_array = view.GetOneArray();
1825   // TODO: Revisit LTV parsing.  Right now, the padding bytes are parsed
1826   // ASSERT_EQ(ltv_vector.size(), an_array.size());
1827   for (size_t i = 0; i < ltv_vector.size(); i++) {
1828     ASSERT_EQ(ltv_vector[i].type_, an_array[i].type_);
1829     ASSERT_EQ(ltv_vector[i].value_, an_array[i].value_);
1830   }
1831 }
1832 
1833 vector<uint8_t> byte_sized{
1834     0x11,                                            // 1
1835     0x21, 0x22,                                      // 2
1836     0x31, 0x32, 0x33,                                // 3
1837     0x41, 0x42, 0x43, 0x44,                          // 4
1838     0x51, 0x52, 0x53, 0x54, 0x55,                    // 5
1839     0x61, 0x62, 0x63, 0x64, 0x65, 0x66,              // 6
1840     0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,        // 7
1841     0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88,  // 8
1842 };
1843 
TEST(GeneratedPacketTest,testByteSizedFields)1844 TEST(GeneratedPacketTest, testByteSizedFields) {
1845   uint64_t array[9]{
1846       0xbadbadbad,
1847       0x11,                // 1
1848       0x2221,              // 2
1849       0x333231,            // 3
1850       0x44434241,          // 4
1851       0x5554535251,        // 5
1852       0x666564636261,      // 6
1853       0x77767574737271,    // 7
1854       0x8887868584838281,  // 8
1855   };
1856   auto packet =
1857       ByteSizedFieldsBuilder::Create(array[1], array[2], array[3], array[4], array[5], array[6], array[7], array[8]);
1858   ASSERT_EQ(byte_sized.size(), packet->size());
1859 
1860   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
1861   BitInserter it(*packet_bytes);
1862   packet->Serialize(it);
1863 
1864   ASSERT_EQ(byte_sized.size(), packet_bytes->size());
1865   for (size_t i = 0; i < byte_sized.size(); i++) {
1866     ASSERT_EQ(byte_sized[i], packet_bytes->at(i));
1867   }
1868 
1869   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
1870   auto view = ByteSizedFieldsView::Create(packet_bytes_view);
1871   ASSERT_TRUE(view.IsValid());
1872   ASSERT_EQ(array[1], view.GetOne());
1873   ASSERT_EQ(array[2], view.GetTwo());
1874   ASSERT_EQ(array[3], view.GetThree());
1875   ASSERT_EQ(array[4], view.GetFour());
1876   ASSERT_EQ(array[5], view.GetFive());
1877   ASSERT_EQ(array[6], view.GetSix());
1878   ASSERT_EQ(array[7], view.GetSeven());
1879   ASSERT_EQ(array[8], view.GetEight());
1880 }
1881 
1882 DEFINE_AND_INSTANTIATE_ByteSizedFieldsReflectionTest(byte_sized);
1883 
TEST(GeneratedPacketTest,testOneGenericStructArrayNoZeroEmpty)1884 TEST(GeneratedPacketTest, testOneGenericStructArrayNoZeroEmpty) {
1885   auto too_few_bytes = std::make_shared<std::vector<uint8_t>>(0);
1886   auto view = OneGenericStructArrayNoZeroView::Create(PacketView<kLittleEndian>(too_few_bytes));
1887   for (size_t i = 0; i < 10; i++) {
1888     if (view.IsValid()) {
1889       view.GetAnArray().size();
1890     }
1891     too_few_bytes->push_back(0);
1892     view = OneGenericStructArrayNoZeroView::Create(PacketView<kLittleEndian>(too_few_bytes));
1893   }
1894 
1895   std::vector<uint8_t> a_two_byte_struct = {
1896       static_cast<uint8_t>(StructTypeNoZero::TWO_BYTE),
1897       0x01,
1898       0x02,
1899   };
1900   too_few_bytes = std::make_shared<std::vector<uint8_t>>(a_two_byte_struct);
1901   view = OneGenericStructArrayNoZeroView::Create(PacketView<kLittleEndian>(too_few_bytes));
1902   ASSERT_TRUE(view.IsValid());
1903   ASSERT_EQ(1, view.GetAnArray().size());
1904 }
1905 
TEST(GeneratedPacketTest,testToStringOutput)1906 TEST(GeneratedPacketTest, testToStringOutput) {
1907   std::vector<TwoRelatedNumbersBe> count_array;
1908   for (uint8_t i = 1; i < 5; i++) {
1909     TwoRelatedNumbersBe trn;
1910     trn.id_ = i;
1911     trn.count_ = 0x0102 * i;
1912     count_array.push_back(trn);
1913   }
1914 
1915   auto packet = ArrayOfStructBeBuilder::Create(count_array);
1916 
1917   ASSERT_EQ(array_of_struct_be.size(), packet->size());
1918 
1919   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
1920   BitInserter it(*packet_bytes);
1921   packet->Serialize(it);
1922 
1923   ASSERT_EQ(array_of_struct_be.size(), packet_bytes->size());
1924   for (size_t i = 0; i < array_of_struct_be.size(); i++) {
1925     ASSERT_EQ(array_of_struct_be[i], packet_bytes->at(i));
1926   }
1927 
1928   PacketView<!kLittleEndian> packet_bytes_view(packet_bytes);
1929   auto view = ArrayOfStructBeView::Create(packet_bytes_view);
1930   ASSERT_TRUE(view.IsValid());
1931 
1932   ASSERT_EQ(
1933       "ArrayOfStructBe { array_count = 0x4, array = VECTOR[TwoRelatedNumbersBe { id = 0x1, count = 0x102 }, "
1934       "TwoRelatedNumbersBe { id = 0x2, count = 0x204 }, TwoRelatedNumbersBe { id = 0x3, count = 0x306 }, "
1935       "TwoRelatedNumbersBe { id = 0x4, count = 0x408 }] }",
1936       view.ToString());
1937 }
1938 
TEST(GeneratedPacketTest,testToStringOneFixedTypesStruct)1939 TEST(GeneratedPacketTest, testToStringOneFixedTypesStruct) {
1940   StructWithFixedTypes swf;
1941   swf.four_bits_ = FourBits::FIVE;
1942   swf.id_ = 0x0d;
1943   swf.array_ = {{0x01, 0x02, 0x03}};
1944   swf.six_bytes_ = SixBytes{{0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6}};
1945 
1946   auto packet = OneFixedTypesStructBuilder::Create(swf);
1947   ASSERT_EQ(one_fixed_types_struct.size(), packet->size());
1948 
1949   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
1950   BitInserter it(*packet_bytes);
1951   packet->Serialize(it);
1952 
1953   ASSERT_EQ(one_fixed_types_struct.size(), packet_bytes->size());
1954   for (size_t i = 0; i < one_fixed_types_struct.size(); i++) {
1955     ASSERT_EQ(one_fixed_types_struct[i], packet_bytes->at(i));
1956   }
1957 
1958   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
1959   auto view = OneFixedTypesStructView::Create(packet_bytes_view);
1960   ASSERT_TRUE(view.IsValid());
1961 
1962   ASSERT_EQ(
1963       "OneFixedTypesStruct { one = StructWithFixedTypes { four_bits = FIVE, id = 0xd, array = ARRAY[0x1, 0x2, 0x3], "
1964       "example_checksum = CHECKSUM, six_bytes = SixBytes } }",
1965       view.ToString());
1966 }
1967 
1968 }  // namespace parser
1969 }  // namespace packet
1970 }  // namespace bluetooth
1971