6 #include "flutter/shell/platform/linux/testing/fl_test.h"
7 #include "gtest/gtest.h"
16 g_autoptr(GError)
error =
nullptr;
17 g_autoptr(GBytes) message =
19 EXPECT_NE(message,
nullptr);
20 EXPECT_EQ(
error,
nullptr);
22 return bytes_to_hex_string(message);
29 g_autoptr(GBytes) data = hex_string_to_bytes(hex_string);
30 g_autoptr(GError)
error =
nullptr;
33 EXPECT_EQ(
error,
nullptr);
34 EXPECT_NE(
value,
nullptr);
44 g_autoptr(GBytes) data = hex_string_to_bytes(hex_string);
45 g_autoptr(GError)
error =
nullptr;
48 EXPECT_TRUE(
value ==
nullptr);
49 EXPECT_TRUE(g_error_matches(
error, domain, code));
52 TEST(FlStandardMessageCodecTest, EncodeNullptr) {
54 EXPECT_STREQ(hex_string,
"00");
57 TEST(FlStandardMessageCodecTest, EncodeNull) {
60 EXPECT_STREQ(hex_string,
"00");
63 TEST(FlStandardMessageCodecTest, DecodeNull) {
67 g_autoptr(GBytes) data = g_bytes_new(
nullptr, 0);
68 g_autoptr(GError)
error =
nullptr;
72 EXPECT_FALSE(
value ==
nullptr);
81 TEST(FlStandardMessageCodecTest, EncodeBoolFalse) {
83 EXPECT_STREQ(hex_string,
"02");
86 TEST(FlStandardMessageCodecTest, EncodeBoolTrue) {
88 EXPECT_STREQ(hex_string,
"01");
96 TEST(FlStandardMessageCodecTest, EncodeIntZero) {
98 EXPECT_STREQ(hex_string,
"0300000000");
101 TEST(FlStandardMessageCodecTest, EncodeIntOne) {
103 EXPECT_STREQ(hex_string,
"0301000000");
106 TEST(FlStandardMessageCodecTest, EncodeInt32) {
107 g_autofree gchar* hex_string =
encode_int(0x01234567);
108 EXPECT_STREQ(hex_string,
"0367452301");
111 TEST(FlStandardMessageCodecTest, EncodeInt32Min) {
112 g_autofree gchar* hex_string =
encode_int(G_MININT32);
113 EXPECT_STREQ(hex_string,
"0300000080");
116 TEST(FlStandardMessageCodecTest, EncodeInt32Max) {
117 g_autofree gchar* hex_string =
encode_int(G_MAXINT32);
118 EXPECT_STREQ(hex_string,
"03ffffff7f");
121 TEST(FlStandardMessageCodecTest, EncodeInt64) {
122 g_autofree gchar* hex_string =
encode_int(0x0123456789abcdef);
123 EXPECT_STREQ(hex_string,
"04efcdab8967452301");
126 TEST(FlStandardMessageCodecTest, EncodeInt64Min) {
127 g_autofree gchar* hex_string =
encode_int(G_MININT64);
128 EXPECT_STREQ(hex_string,
"040000000000000080");
131 TEST(FlStandardMessageCodecTest, EncodeInt64Max) {
132 g_autofree gchar* hex_string =
encode_int(G_MAXINT64);
133 EXPECT_STREQ(hex_string,
"04ffffffffffffff7f");
136 TEST(FlStandardMessageCodecTest, DecodeIntZero) {
142 TEST(FlStandardMessageCodecTest, DecodeIntOne) {
148 TEST(FlStandardMessageCodecTest, DecodeInt32) {
154 TEST(FlStandardMessageCodecTest, DecodeInt32Min) {
160 TEST(FlStandardMessageCodecTest, DecodeInt32Max) {
166 TEST(FlStandardMessageCodecTest, DecodeInt64) {
172 TEST(FlStandardMessageCodecTest, DecodeInt64Min) {
178 TEST(FlStandardMessageCodecTest, DecodeInt64Max) {
184 TEST(FlStandardMessageCodecTest, DecodeInt32NoData) {
189 TEST(FlStandardMessageCodecTest, DecodeIntShortData1) {
194 TEST(FlStandardMessageCodecTest, DecodeIntShortData2) {
199 TEST(FlStandardMessageCodecTest, DecodeInt64NoData) {
204 TEST(FlStandardMessageCodecTest, DecodeInt64ShortData1) {
209 TEST(FlStandardMessageCodecTest, DecodeInt64ShortData2) {
219 TEST(FlStandardMessageCodecTest, EncodeFloatZero) {
221 EXPECT_STREQ(hex_string,
"06000000000000000000000000000000");
224 TEST(FlStandardMessageCodecTest, EncodeFloatOne) {
226 EXPECT_STREQ(hex_string,
"0600000000000000000000000000f03f");
229 TEST(FlStandardMessageCodecTest, EncodeFloatMinusOne) {
231 EXPECT_STREQ(hex_string,
"0600000000000000000000000000f0bf");
234 TEST(FlStandardMessageCodecTest, EncodeFloatHalf) {
236 EXPECT_STREQ(hex_string,
"0600000000000000000000000000e03f");
239 TEST(FlStandardMessageCodecTest, EncodeFloatFraction) {
241 EXPECT_STREQ(hex_string,
"0600000000000000182d4454fb210940");
244 TEST(FlStandardMessageCodecTest, EncodeFloatMinusZero) {
246 EXPECT_STREQ(hex_string,
"06000000000000000000000000000080");
249 TEST(FlStandardMessageCodecTest, EncodeFloatNaN) {
251 EXPECT_STREQ(hex_string,
"0600000000000000000000000000f87f");
254 TEST(FlStandardMessageCodecTest, EncodeFloatInfinity) {
256 EXPECT_STREQ(hex_string,
"0600000000000000000000000000f07f");
259 TEST(FlStandardMessageCodecTest, DecodeFloatZero) {
265 TEST(FlStandardMessageCodecTest, DecodeFloatOne) {
271 TEST(FlStandardMessageCodecTest, DecodeFloatMinusOne) {
277 TEST(FlStandardMessageCodecTest, DecodeFloatHalf) {
283 TEST(FlStandardMessageCodecTest, DecodeFloatPi) {
289 TEST(FlStandardMessageCodecTest, DecodeFloatMinusZero) {
295 TEST(FlStandardMessageCodecTest, DecodeFloatNaN) {
301 TEST(FlStandardMessageCodecTest, DecodeFloatInfinity) {
307 TEST(FlStandardMessageCodecTest, DecodeFloatNoData) {
312 TEST(FlStandardMessageCodecTest, DecodeFloatShortData1) {
317 TEST(FlStandardMessageCodecTest, DecodeFloatShortData2) {
327 TEST(FlStandardMessageCodecTest, EncodeStringEmpty) {
329 EXPECT_STREQ(hex_string,
"0700");
332 TEST(FlStandardMessageCodecTest, EncodeStringHello) {
334 EXPECT_STREQ(hex_string,
"070568656c6c6f");
337 TEST(FlStandardMessageCodecTest, EncodeStringEmptySized) {
340 EXPECT_STREQ(hex_string,
"0700");
343 TEST(FlStandardMessageCodecTest, EncodeStringHelloSized) {
346 EXPECT_STREQ(hex_string,
"070548656c6c6f");
349 TEST(FlStandardMessageCodecTest, DecodeStringEmpty) {
355 TEST(FlStandardMessageCodecTest, DecodeStringHello) {
361 TEST(FlStandardMessageCodecTest, DecodeStringNoData) {
366 TEST(FlStandardMessageCodecTest, DecodeStringLengthNoData) {
371 TEST(FlStandardMessageCodecTest, DecodeStringShortData1) {
376 TEST(FlStandardMessageCodecTest, DecodeStringShortData2) {
381 TEST(FlStandardMessageCodecTest, EncodeUint8ListEmpty) {
384 EXPECT_STREQ(hex_string,
"0800");
387 TEST(FlStandardMessageCodecTest, EncodeUint8List) {
388 uint8_t data[] = {0, 1, 2, 3, 4};
391 EXPECT_STREQ(hex_string,
"08050001020304");
394 TEST(FlStandardMessageCodecTest, DecodeUint8ListEmpty) {
400 TEST(FlStandardMessageCodecTest, DecodeUint8List) {
405 EXPECT_EQ(data[0], 0);
406 EXPECT_EQ(data[1], 1);
407 EXPECT_EQ(data[2], 2);
408 EXPECT_EQ(data[3], 3);
409 EXPECT_EQ(data[4], 4);
412 TEST(FlStandardMessageCodecTest, DecodeUint8ListNoData) {
417 TEST(FlStandardMessageCodecTest, DecodeUint8ListLengthNoData) {
422 TEST(FlStandardMessageCodecTest, DecodeUint8ListShortData1) {
427 TEST(FlStandardMessageCodecTest, DecodeUint8ListShortData2) {
432 TEST(FlStandardMessageCodecTest, EncodeInt32ListEmpty) {
435 EXPECT_STREQ(hex_string,
"09000000");
438 TEST(FlStandardMessageCodecTest, EncodeInt32List) {
439 int32_t data[] = {0, -1, 2, -3, 4};
442 EXPECT_STREQ(hex_string,
"0905000000000000ffffffff02000000fdffffff04000000");
445 TEST(FlStandardMessageCodecTest, DecodeInt32ListEmpty) {
451 TEST(FlStandardMessageCodecTest, DecodeInt32List) {
453 decode_message(
"0905000000000000ffffffff02000000fdffffff04000000");
456 EXPECT_EQ(data[0], 0);
457 EXPECT_EQ(data[1], -1);
458 EXPECT_EQ(data[2], 2);
459 EXPECT_EQ(data[3], -3);
460 EXPECT_EQ(data[4], 4);
463 TEST(FlStandardMessageCodecTest, DecodeInt32ListNoData) {
468 TEST(FlStandardMessageCodecTest, DecodeInt32ListLengthNoData) {
473 TEST(FlStandardMessageCodecTest, DecodeInt32ListShortData1) {
478 TEST(FlStandardMessageCodecTest, DecodeInt32ListShortData2) {
484 TEST(FlStandardMessageCodecTest, EncodeInt64ListEmpty) {
487 EXPECT_STREQ(hex_string,
"0a00000000000000");
490 TEST(FlStandardMessageCodecTest, EncodeInt64List) {
491 int64_t data[] = {0, -1, 2, -3, 4};
496 "0a050000000000000000000000000000ffffffffffffffff0200000000000000fdffffff"
497 "ffffffff0400000000000000");
500 TEST(FlStandardMessageCodecTest, DecodeInt64ListEmpty) {
506 TEST(FlStandardMessageCodecTest, DecodeInt64List) {
508 "0a050000000000000000000000000000ffffffffffffffff0200000000000000fdffffff"
509 "ffffffff0400000000000000");
512 EXPECT_EQ(data[0], 0);
513 EXPECT_EQ(data[1], -1);
514 EXPECT_EQ(data[2], 2);
515 EXPECT_EQ(data[3], -3);
516 EXPECT_EQ(data[4], 4);
519 TEST(FlStandardMessageCodecTest, DecodeInt64ListNoData) {
524 TEST(FlStandardMessageCodecTest, DecodeInt64ListLengthNoData) {
529 TEST(FlStandardMessageCodecTest, DecodeInt64ListShortData1) {
534 TEST(FlStandardMessageCodecTest, DecodeInt64ListShortData2) {
536 "0a050000000000000000000000000000ffffffffffffffff0200000000000000fdffffff"
542 TEST(FlStandardMessageCodecTest, EncodeFloat32ListEmpty) {
545 EXPECT_STREQ(hex_string,
"0e000000");
548 TEST(FlStandardMessageCodecTest, EncodeFloat32List) {
549 float data[] = {0.0f, -0.5f, 0.25f, -0.125f, 0.00625f};
552 EXPECT_STREQ(hex_string,
"0e05000000000000000000bf0000803e000000becdcccc3b");
555 TEST(FlStandardMessageCodecTest, DecodeFloat32ListEmpty) {
561 TEST(FlStandardMessageCodecTest, DecodeFloat32List) {
563 decode_message(
"0e05000000000000000000bf0000803e000000becdcccc3b");
566 EXPECT_FLOAT_EQ(data[0], 0.0f);
567 EXPECT_FLOAT_EQ(data[1], -0.5f);
568 EXPECT_FLOAT_EQ(data[2], 0.25f);
569 EXPECT_FLOAT_EQ(data[3], -0.125f);
570 EXPECT_FLOAT_EQ(data[4], 0.00625f);
573 TEST(FlStandardMessageCodecTest, DecodeFloat32ListNoData) {
578 TEST(FlStandardMessageCodecTest, DecodeFloat32ListLengthNoData) {
583 TEST(FlStandardMessageCodecTest, DecodeFloat32ListShortData1) {
588 TEST(FlStandardMessageCodecTest, DecodeFloat32ListShortData2) {
594 TEST(FlStandardMessageCodecTest, EncodeFloatListEmpty) {
597 EXPECT_STREQ(hex_string,
"0b00000000000000");
600 TEST(FlStandardMessageCodecTest, EncodeFloatList) {
601 double data[] = {0, -0.5, 0.25, -0.125, 0.00625};
606 "0b050000000000000000000000000000000000000000e0bf000000000000d03f00000000"
607 "0000c0bf9a9999999999793f");
610 TEST(FlStandardMessageCodecTest, DecodeFloatListEmpty) {
616 TEST(FlStandardMessageCodecTest, DecodeFloatList) {
618 "0b050000000000000000000000000000000000000000e0bf000000000000d03f00000000"
619 "0000c0bf9a9999999999793f");
622 EXPECT_FLOAT_EQ(data[0], 0.0);
623 EXPECT_FLOAT_EQ(data[1], -0.5);
624 EXPECT_FLOAT_EQ(data[2], 0.25);
625 EXPECT_FLOAT_EQ(data[3], -0.125);
626 EXPECT_FLOAT_EQ(data[4], 0.00625);
629 TEST(FlStandardMessageCodecTest, DecodeFloatListNoData) {
634 TEST(FlStandardMessageCodecTest, DecodeFloatListLengthNoData) {
639 TEST(FlStandardMessageCodecTest, DecodeFloatListShortData1) {
644 TEST(FlStandardMessageCodecTest, DecodeFloatListShortData2) {
646 "0b050000000000000000000000000000000000000000e0bf000000000000d03f00000000"
652 TEST(FlStandardMessageCodecTest, EncodeListEmpty) {
655 EXPECT_STREQ(hex_string,
"0c00");
658 TEST(FlStandardMessageCodecTest, EncodeListTypes) {
670 "0c070001032a00000006000000000000182d4454fb210940070568656c6c6f0c000d00");
673 TEST(FlStandardMessageCodecTest, EncodeListNested) {
676 for (
int i = 0; i < 10; i++) {
687 EXPECT_STREQ(hex_string,
688 "0c020c05030000000003020000000304000000030600000003080000000c"
689 "0503010000000303000000030500000003070000000309000000");
692 TEST(FlStandardMessageCodecTest, DecodeListEmpty) {
698 TEST(FlStandardMessageCodecTest, DecodeListTypes) {
700 "0c070001032a00000006000000000000182d4454fb210940070568656c6c6f0c000d00");
720 static_cast<size_t>(0));
724 static_cast<size_t>(0));
727 TEST(FlStandardMessageCodecTest, DecodeListNested) {
729 "0c020c05030000000003020000000304000000030600000003080000000c"
730 "0503010000000303000000030500000003070000000309000000");
739 for (
int i = 0; i < 5; i++) {
750 TEST(FlStandardMessageCodecTest, DecodeListNoData) {
755 TEST(FlStandardMessageCodecTest, DecodeListLengthNoData) {
760 TEST(FlStandardMessageCodecTest, DecodeListShortData1) {
765 TEST(FlStandardMessageCodecTest, DecodeListShortData2) {
767 "0c070001032a00000006000000000000182d4454fb210940070568656c6c6f0c000d",
771 TEST(FlStandardMessageCodecTest, EncodeDecodeLargeList) {
775 for (
int i = 0; i < 65535; i++) {
779 g_autoptr(GError)
error =
nullptr;
780 g_autoptr(GBytes) message =
782 EXPECT_NE(message,
nullptr);
783 EXPECT_EQ(
error,
nullptr);
785 g_autoptr(
FlValue) decoded_value =
787 EXPECT_EQ(
error,
nullptr);
788 EXPECT_NE(
value,
nullptr);
793 TEST(FlStandardMessageCodecTest, EncodeMapEmpty) {
796 EXPECT_STREQ(hex_string,
"0d00");
799 TEST(FlStandardMessageCodecTest, EncodeMapKeyTypes) {
812 EXPECT_STREQ(hex_string,
813 "0d070007046e756c6c010704626f6f6c032a0000000703696e7406000000000"
814 "0182d4454fb2109400705666c6f6174070568656c6c6f0706737472696e670c"
815 "0007046c6973740d0007036d6170");
818 TEST(FlStandardMessageCodecTest, EncodeMapValueTypes) {
831 EXPECT_STREQ(hex_string,
832 "0d0707046e756c6c000704626f6f6c010703696e74032a0000000705666c6f6"
833 "17406000000000000182d4454fb2109400706737472696e67070568656c6c6f"
834 "07046c6973740c0007036d61700d00");
837 TEST(FlStandardMessageCodecTest, EncodeMapNested) {
840 const char* numbers[] = {
"zero",
"one",
"two",
"three",
nullptr};
841 for (
int i = 0; numbers[i] !=
nullptr; i++) {
851 EXPECT_STREQ(hex_string,
852 "0d02070a7374722d746f2d696e740d0407047a65726f030000000007036f6e6"
853 "50301000000070374776f0302000000070574687265650303000000070a696e"
854 "742d746f2d7374720d04030000000007047a65726f030100000007036f6e650"
855 "302000000070374776f030300000007057468726565");
858 TEST(FlStandardMessageCodecTest, DecodeMapEmpty) {
864 TEST(FlStandardMessageCodecTest, DecodeMapKeyTypes) {
866 "0d070007046e756c6c010704626f6f6c032a0000000703696e74060000000000182d4454"
867 "fb2109400705666c6f6174070568656c6c6f0706737472696e670c0007046c6973740d00"
909 static_cast<size_t>(0));
917 static_cast<size_t>(0));
923 TEST(FlStandardMessageCodecTest, DecodeMapValueTypes) {
925 "0d0707046e756c6c000704626f6f6c010703696e74032a0000000705666c6f6174060000"
926 "00000000182d4454fb2109400706737472696e67070568656c6c6f07046c6973740c0007"
971 static_cast<size_t>(0));
979 static_cast<size_t>(0));
982 TEST(FlStandardMessageCodecTest, DecodeMapNested) {
984 "0d02070a7374722d746f2d696e740d0407047a65726f030000000007036f6e6503010000"
985 "00070374776f0302000000070574687265650303000000070a696e742d746f2d7374720d"
986 "04030000000007047a65726f030100000007036f6e650302000000070374776f03030000"
1007 const char* numbers[] = {
"zero",
"one",
"two",
"three",
nullptr};
1008 for (
int i = 0; numbers[i] !=
nullptr; i++) {
1029 TEST(FlStandardMessageCodecTest, DecodeMapNoData) {
1034 TEST(FlStandardMessageCodecTest, DecodeMapLengthNoData) {
1039 TEST(FlStandardMessageCodecTest, DecodeMapShortData1) {
1044 TEST(FlStandardMessageCodecTest, DecodeMapShortData2) {
1046 "0d0707046e756c6c000704626f6f6c010703696e74032a0000000705666c6f6174060000"
1047 "00000000182d4454fb2109400706737472696e67070568656c6c6f07046c6973740c0007"
1052 TEST(FlStandardMessageCodecTest, EncodeDecodeLargeMap) {
1056 for (
int i = 0; i < 512; i++) {
1057 g_autofree gchar* key = g_strdup_printf(
"key%d", i);
1061 g_autoptr(GError)
error =
nullptr;
1062 g_autoptr(GBytes) message =
1064 EXPECT_NE(message,
nullptr);
1065 EXPECT_EQ(
error,
nullptr);
1067 g_autoptr(
FlValue) decoded_value =
1069 EXPECT_EQ(
error,
nullptr);
1070 EXPECT_NE(
value,
nullptr);
1075 TEST(FlStandardMessageCodecTest, DecodeUnknownType) {
1080 TEST(FlStandardMessageCodecTest, EncodeDecode) {
1092 g_autoptr(GError)
error =
nullptr;
1093 g_autoptr(GBytes) message =
1095 EXPECT_NE(message,
nullptr);
1096 EXPECT_EQ(
error,
nullptr);
1100 EXPECT_EQ(
error,
nullptr);
1101 EXPECT_NE(output,
nullptr);