Flutter Engine
fl_standard_message_codec_test.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "flutter/shell/platform/linux/public/flutter_linux/fl_standard_message_codec.h"
6 #include "flutter/shell/platform/linux/testing/fl_test.h"
7 #include "gtest/gtest.h"
8 
9 // NOTE(robert-ancell) These test cases assumes a little-endian architecture.
10 // These tests will need to be updated if tested on a big endian architecture.
11 
12 // Encodes a message using a FlStandardMessageCodec. Return a hex string with
13 // the encoded binary output.
14 static gchar* encode_message(FlValue* value) {
15  g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
16  g_autoptr(GError) error = nullptr;
17  g_autoptr(GBytes) message =
18  fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), value, &error);
19  EXPECT_NE(message, nullptr);
20  EXPECT_EQ(error, nullptr);
21 
22  return bytes_to_hex_string(message);
23 }
24 
25 // Decodes a message using a FlStandardMessageCodec. The binary data is given in
26 // the form of a hex string.
27 static FlValue* decode_message(const char* hex_string) {
28  g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
29  g_autoptr(GBytes) data = hex_string_to_bytes(hex_string);
30  g_autoptr(GError) error = nullptr;
31  g_autoptr(FlValue) value =
32  fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), data, &error);
33  EXPECT_EQ(error, nullptr);
34  EXPECT_NE(value, nullptr);
35  return fl_value_ref(value);
36 }
37 
38 // Decodes a message using a FlStandardMessageCodec. The binary data is given in
39 // the form of a hex string. Expect the given error.
40 static void decode_error_value(const char* hex_string,
41  GQuark domain,
42  gint code) {
43  g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
44  g_autoptr(GBytes) data = hex_string_to_bytes(hex_string);
45  g_autoptr(GError) error = nullptr;
46  g_autoptr(FlValue) value =
47  fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), data, &error);
48  EXPECT_TRUE(value == nullptr);
49  EXPECT_TRUE(g_error_matches(error, domain, code));
50 }
51 
52 TEST(FlStandardMessageCodecTest, EncodeNullptr) {
53  g_autofree gchar* hex_string = encode_message(nullptr);
54  EXPECT_STREQ(hex_string, "00");
55 }
56 
57 TEST(FlStandardMessageCodecTest, EncodeNull) {
58  g_autoptr(FlValue) value = fl_value_new_null();
59  g_autofree gchar* hex_string = encode_message(value);
60  EXPECT_STREQ(hex_string, "00");
61 }
62 
63 static gchar* encode_bool(gboolean value) {
64  g_autoptr(FlValue) v = fl_value_new_bool(value);
65  return encode_message(v);
66 }
67 
68 TEST(FlStandardMessageCodecTest, EncodeBoolFalse) {
69  g_autofree gchar* hex_string = encode_bool(FALSE);
70  EXPECT_STREQ(hex_string, "02");
71 }
72 
73 TEST(FlStandardMessageCodecTest, EncodeBoolTrue) {
74  g_autofree gchar* hex_string = encode_bool(TRUE);
75  EXPECT_STREQ(hex_string, "01");
76 }
77 
78 static gchar* encode_int(int64_t value) {
79  g_autoptr(FlValue) v = fl_value_new_int(value);
80  return encode_message(v);
81 }
82 
83 TEST(FlStandardMessageCodecTest, EncodeIntZero) {
84  g_autofree gchar* hex_string = encode_int(0);
85  EXPECT_STREQ(hex_string, "0300000000");
86 }
87 
88 TEST(FlStandardMessageCodecTest, EncodeIntOne) {
89  g_autofree gchar* hex_string = encode_int(1);
90  EXPECT_STREQ(hex_string, "0301000000");
91 }
92 
93 TEST(FlStandardMessageCodecTest, EncodeInt32) {
94  g_autofree gchar* hex_string = encode_int(0x01234567);
95  EXPECT_STREQ(hex_string, "0367452301");
96 }
97 
98 TEST(FlStandardMessageCodecTest, EncodeInt32Min) {
99  g_autofree gchar* hex_string = encode_int(G_MININT32);
100  EXPECT_STREQ(hex_string, "0300000080");
101 }
102 
103 TEST(FlStandardMessageCodecTest, EncodeInt32Max) {
104  g_autofree gchar* hex_string = encode_int(G_MAXINT32);
105  EXPECT_STREQ(hex_string, "03ffffff7f");
106 }
107 
108 TEST(FlStandardMessageCodecTest, EncodeInt64) {
109  g_autofree gchar* hex_string = encode_int(0x0123456789abcdef);
110  EXPECT_STREQ(hex_string, "04efcdab8967452301");
111 }
112 
113 TEST(FlStandardMessageCodecTest, EncodeInt64Min) {
114  g_autofree gchar* hex_string = encode_int(G_MININT64);
115  EXPECT_STREQ(hex_string, "040000000000000080");
116 }
117 
118 TEST(FlStandardMessageCodecTest, EncodeInt64Max) {
119  g_autofree gchar* hex_string = encode_int(G_MAXINT64);
120  EXPECT_STREQ(hex_string, "04ffffffffffffff7f");
121 }
122 
123 TEST(FlStandardMessageCodecTest, DecodeIntZero) {
124  g_autoptr(FlValue) value = decode_message("0300000000");
126  EXPECT_EQ(fl_value_get_int(value), 0);
127 }
128 
129 TEST(FlStandardMessageCodecTest, DecodeIntOne) {
130  g_autoptr(FlValue) value = decode_message("0301000000");
132  EXPECT_EQ(fl_value_get_int(value), 1);
133 }
134 
135 TEST(FlStandardMessageCodecTest, DecodeInt32) {
136  g_autoptr(FlValue) value = decode_message("0367452301");
138  EXPECT_EQ(fl_value_get_int(value), 0x01234567);
139 }
140 
141 TEST(FlStandardMessageCodecTest, DecodeInt32Min) {
142  g_autoptr(FlValue) value = decode_message("0300000080");
144  EXPECT_EQ(fl_value_get_int(value), G_MININT32);
145 }
146 
147 TEST(FlStandardMessageCodecTest, DecodeInt32Max) {
148  g_autoptr(FlValue) value = decode_message("03ffffff7f");
150  EXPECT_EQ(fl_value_get_int(value), G_MAXINT32);
151 }
152 
153 TEST(FlStandardMessageCodecTest, DecodeInt64) {
154  g_autoptr(FlValue) value = decode_message("04efcdab8967452301");
156  EXPECT_EQ(fl_value_get_int(value), 0x0123456789abcdef);
157 }
158 
159 TEST(FlStandardMessageCodecTest, DecodeInt64Min) {
160  g_autoptr(FlValue) value = decode_message("040000000000000080");
162  EXPECT_EQ(fl_value_get_int(value), G_MININT64);
163 }
164 
165 TEST(FlStandardMessageCodecTest, DecodeInt64Max) {
166  g_autoptr(FlValue) value = decode_message("04ffffffffffffff7f");
168  EXPECT_EQ(fl_value_get_int(value), G_MAXINT64);
169 }
170 
171 TEST(FlStandardMessageCodecTest, DecodeInt32NoData) {
174 }
175 
176 TEST(FlStandardMessageCodecTest, DecodeIntShortData1) {
179 }
180 
181 TEST(FlStandardMessageCodecTest, DecodeIntShortData2) {
184 }
185 
186 TEST(FlStandardMessageCodecTest, DecodeInt64NoData) {
189 }
190 
191 TEST(FlStandardMessageCodecTest, DecodeInt64ShortData1) {
194 }
195 
196 TEST(FlStandardMessageCodecTest, DecodeInt64ShortData2) {
197  decode_error_value("04efcdab89674523", FL_MESSAGE_CODEC_ERROR,
199 }
200 
201 static gchar* encode_float(double value) {
202  g_autoptr(FlValue) v = fl_value_new_float(value);
203  return encode_message(v);
204 }
205 
206 TEST(FlStandardMessageCodecTest, EncodeFloatZero) {
207  g_autofree gchar* hex_string = encode_float(0);
208  EXPECT_STREQ(hex_string, "06000000000000000000000000000000");
209 }
210 
211 TEST(FlStandardMessageCodecTest, EncodeFloatOne) {
212  g_autofree gchar* hex_string = encode_float(1);
213  EXPECT_STREQ(hex_string, "0600000000000000000000000000f03f");
214 }
215 
216 TEST(FlStandardMessageCodecTest, EncodeFloatMinusOne) {
217  g_autofree gchar* hex_string = encode_float(-1);
218  EXPECT_STREQ(hex_string, "0600000000000000000000000000f0bf");
219 }
220 
221 TEST(FlStandardMessageCodecTest, EncodeFloatHalf) {
222  g_autofree gchar* hex_string = encode_float(0.5);
223  EXPECT_STREQ(hex_string, "0600000000000000000000000000e03f");
224 }
225 
226 TEST(FlStandardMessageCodecTest, EncodeFloatFraction) {
227  g_autofree gchar* hex_string = encode_float(M_PI);
228  EXPECT_STREQ(hex_string, "0600000000000000182d4454fb210940");
229 }
230 
231 TEST(FlStandardMessageCodecTest, EncodeFloatMinusZero) {
232  g_autofree gchar* hex_string = encode_float(-0.0);
233  EXPECT_STREQ(hex_string, "06000000000000000000000000000080");
234 }
235 
236 TEST(FlStandardMessageCodecTest, EncodeFloatNaN) {
237  g_autofree gchar* hex_string = encode_float(NAN);
238  EXPECT_STREQ(hex_string, "0600000000000000000000000000f87f");
239 }
240 
241 TEST(FlStandardMessageCodecTest, EncodeFloatInfinity) {
242  g_autofree gchar* hex_string = encode_float(INFINITY);
243  EXPECT_STREQ(hex_string, "0600000000000000000000000000f07f");
244 }
245 
246 TEST(FlStandardMessageCodecTest, DecodeFloatZero) {
247  g_autoptr(FlValue) value = decode_message("06000000000000000000000000000000");
249  EXPECT_EQ(fl_value_get_float(value), 0.0);
250 }
251 
252 TEST(FlStandardMessageCodecTest, DecodeFloatOne) {
253  g_autoptr(FlValue) value = decode_message("0600000000000000000000000000f03f");
255  EXPECT_EQ(fl_value_get_float(value), 1.0);
256 }
257 
258 TEST(FlStandardMessageCodecTest, DecodeFloatMinusOne) {
259  g_autoptr(FlValue) value = decode_message("0600000000000000000000000000f0bf");
261  EXPECT_EQ(fl_value_get_float(value), -1.0);
262 }
263 
264 TEST(FlStandardMessageCodecTest, DecodeFloatHalf) {
265  g_autoptr(FlValue) value = decode_message("0600000000000000000000000000e03f");
267  EXPECT_EQ(fl_value_get_float(value), 0.5);
268 }
269 
270 TEST(FlStandardMessageCodecTest, DecodeFloatPi) {
271  g_autoptr(FlValue) value = decode_message("0600000000000000182d4454fb210940");
273  EXPECT_EQ(fl_value_get_float(value), M_PI);
274 }
275 
276 TEST(FlStandardMessageCodecTest, DecodeFloatMinusZero) {
277  g_autoptr(FlValue) value = decode_message("06000000000000000000000000000080");
279  EXPECT_EQ(fl_value_get_float(value), -0.0);
280 }
281 
282 TEST(FlStandardMessageCodecTest, DecodeFloatNaN) {
283  g_autoptr(FlValue) value = decode_message("0600000000000000000000000000f87f");
285  EXPECT_TRUE(isnan(fl_value_get_float(value)));
286 }
287 
288 TEST(FlStandardMessageCodecTest, DecodeFloatInfinity) {
289  g_autoptr(FlValue) value = decode_message("0600000000000000000000000000f07f");
291  EXPECT_TRUE(isinf(fl_value_get_float(value)));
292 }
293 
294 TEST(FlStandardMessageCodecTest, DecodeFloatNoData) {
295  decode_error_value("060000000000000000", FL_MESSAGE_CODEC_ERROR,
297 }
298 
299 TEST(FlStandardMessageCodecTest, DecodeFloatShortData1) {
300  decode_error_value("060000000000000000", FL_MESSAGE_CODEC_ERROR,
302 }
303 
304 TEST(FlStandardMessageCodecTest, DecodeFloatShortData2) {
305  decode_error_value("060000000000000000000000000000", FL_MESSAGE_CODEC_ERROR,
307 }
308 
309 static gchar* encode_string(const gchar* value) {
310  g_autoptr(FlValue) v = fl_value_new_string(value);
311  return encode_message(v);
312 }
313 
314 TEST(FlStandardMessageCodecTest, EncodeStringEmpty) {
315  g_autofree gchar* hex_string = encode_string("");
316  EXPECT_STREQ(hex_string, "0700");
317 }
318 
319 TEST(FlStandardMessageCodecTest, EncodeStringHello) {
320  g_autofree gchar* hex_string = encode_string("hello");
321  EXPECT_STREQ(hex_string, "070568656c6c6f");
322 }
323 
324 TEST(FlStandardMessageCodecTest, EncodeStringEmptySized) {
325  g_autoptr(FlValue) value = fl_value_new_string_sized(nullptr, 0);
326  g_autofree gchar* hex_string = encode_message(value);
327  EXPECT_STREQ(hex_string, "0700");
328 }
329 
330 TEST(FlStandardMessageCodecTest, EncodeStringHelloSized) {
331  g_autoptr(FlValue) value = fl_value_new_string_sized("Hello World", 5);
332  g_autofree gchar* hex_string = encode_message(value);
333  EXPECT_STREQ(hex_string, "070548656c6c6f");
334 }
335 
336 TEST(FlStandardMessageCodecTest, DecodeStringEmpty) {
337  g_autoptr(FlValue) value = decode_message("0700");
339  EXPECT_STREQ(fl_value_get_string(value), "");
340 }
341 
342 TEST(FlStandardMessageCodecTest, DecodeStringHello) {
343  g_autoptr(FlValue) value = decode_message("070568656c6c6f");
345  EXPECT_STREQ(fl_value_get_string(value), "hello");
346 }
347 
348 TEST(FlStandardMessageCodecTest, DecodeStringNoData) {
351 }
352 
353 TEST(FlStandardMessageCodecTest, DecodeStringLengthNoData) {
356 }
357 
358 TEST(FlStandardMessageCodecTest, DecodeStringShortData1) {
361 }
362 
363 TEST(FlStandardMessageCodecTest, DecodeStringShortData2) {
366 }
367 
368 TEST(FlStandardMessageCodecTest, EncodeUint8ListEmpty) {
369  g_autoptr(FlValue) value = fl_value_new_uint8_list(nullptr, 0);
370  g_autofree gchar* hex_string = encode_message(value);
371  EXPECT_STREQ(hex_string, "0800");
372 }
373 
374 TEST(FlStandardMessageCodecTest, EncodeUint8List) {
375  uint8_t data[] = {0, 1, 2, 3, 4};
376  g_autoptr(FlValue) value = fl_value_new_uint8_list(data, 5);
377  g_autofree gchar* hex_string = encode_message(value);
378  EXPECT_STREQ(hex_string, "08050001020304");
379 }
380 
381 TEST(FlStandardMessageCodecTest, DecodeUint8ListEmpty) {
382  g_autoptr(FlValue) value = decode_message("0800");
384  EXPECT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
385 }
386 
387 TEST(FlStandardMessageCodecTest, DecodeUint8List) {
388  g_autoptr(FlValue) value = decode_message("08050001020304");
390  EXPECT_EQ(fl_value_get_length(value), static_cast<size_t>(5));
391  const uint8_t* data = fl_value_get_uint8_list(value);
392  EXPECT_EQ(data[0], 0);
393  EXPECT_EQ(data[1], 1);
394  EXPECT_EQ(data[2], 2);
395  EXPECT_EQ(data[3], 3);
396  EXPECT_EQ(data[4], 4);
397 }
398 
399 TEST(FlStandardMessageCodecTest, DecodeUint8ListNoData) {
402 }
403 
404 TEST(FlStandardMessageCodecTest, DecodeUint8ListLengthNoData) {
407 }
408 
409 TEST(FlStandardMessageCodecTest, DecodeUint8ListShortData1) {
412 }
413 
414 TEST(FlStandardMessageCodecTest, DecodeUint8ListShortData2) {
417 }
418 
419 TEST(FlStandardMessageCodecTest, EncodeInt32ListEmpty) {
420  g_autoptr(FlValue) value = fl_value_new_int32_list(nullptr, 0);
421  g_autofree gchar* hex_string = encode_message(value);
422  EXPECT_STREQ(hex_string, "09000000");
423 }
424 
425 TEST(FlStandardMessageCodecTest, EncodeInt32List) {
426  int32_t data[] = {0, -1, 2, -3, 4};
427  g_autoptr(FlValue) value = fl_value_new_int32_list(data, 5);
428  g_autofree gchar* hex_string = encode_message(value);
429  EXPECT_STREQ(hex_string, "0905000000000000ffffffff02000000fdffffff04000000");
430 }
431 
432 TEST(FlStandardMessageCodecTest, DecodeInt32ListEmpty) {
433  g_autoptr(FlValue) value = decode_message("09000000");
435  EXPECT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
436 }
437 
438 TEST(FlStandardMessageCodecTest, DecodeInt32List) {
439  g_autoptr(FlValue) value =
440  decode_message("0905000000000000ffffffff02000000fdffffff04000000");
442  const int32_t* data = fl_value_get_int32_list(value);
443  EXPECT_EQ(data[0], 0);
444  EXPECT_EQ(data[1], -1);
445  EXPECT_EQ(data[2], 2);
446  EXPECT_EQ(data[3], -3);
447  EXPECT_EQ(data[4], 4);
448 }
449 
450 TEST(FlStandardMessageCodecTest, DecodeInt32ListNoData) {
453 }
454 
455 TEST(FlStandardMessageCodecTest, DecodeInt32ListLengthNoData) {
458 }
459 
460 TEST(FlStandardMessageCodecTest, DecodeInt32ListShortData1) {
463 }
464 
465 TEST(FlStandardMessageCodecTest, DecodeInt32ListShortData2) {
466  decode_error_value("090500000000ffffffff02000000fdffffff040000",
469 }
470 
471 TEST(FlStandardMessageCodecTest, EncodeInt64ListEmpty) {
472  g_autoptr(FlValue) value = fl_value_new_int64_list(nullptr, 0);
473  g_autofree gchar* hex_string = encode_message(value);
474  EXPECT_STREQ(hex_string, "0a00000000000000");
475 }
476 
477 TEST(FlStandardMessageCodecTest, EncodeInt64List) {
478  int64_t data[] = {0, -1, 2, -3, 4};
479  g_autoptr(FlValue) value = fl_value_new_int64_list(data, 5);
480  g_autofree gchar* hex_string = encode_message(value);
481  EXPECT_STREQ(
482  hex_string,
483  "0a050000000000000000000000000000ffffffffffffffff0200000000000000fdffffff"
484  "ffffffff0400000000000000");
485 }
486 
487 TEST(FlStandardMessageCodecTest, DecodeInt64ListEmpty) {
488  g_autoptr(FlValue) value = decode_message("0a00000000000000");
490  EXPECT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
491 }
492 
493 TEST(FlStandardMessageCodecTest, DecodeInt64List) {
494  g_autoptr(FlValue) value = decode_message(
495  "0a050000000000000000000000000000ffffffffffffffff0200000000000000fdffffff"
496  "ffffffff0400000000000000");
498  const int64_t* data = fl_value_get_int64_list(value);
499  EXPECT_EQ(data[0], 0);
500  EXPECT_EQ(data[1], -1);
501  EXPECT_EQ(data[2], 2);
502  EXPECT_EQ(data[3], -3);
503  EXPECT_EQ(data[4], 4);
504 }
505 
506 TEST(FlStandardMessageCodecTest, DecodeInt64ListNoData) {
509 }
510 
511 TEST(FlStandardMessageCodecTest, DecodeInt64ListLengthNoData) {
512  decode_error_value("0a05000000000000", FL_MESSAGE_CODEC_ERROR,
514 }
515 
516 TEST(FlStandardMessageCodecTest, DecodeInt64ListShortData1) {
517  decode_error_value("0a0500000000000000", FL_MESSAGE_CODEC_ERROR,
519 }
520 
521 TEST(FlStandardMessageCodecTest, DecodeInt64ListShortData2) {
523  "0a050000000000000000000000000000ffffffffffffffff0200000000000000fdffffff"
524  "ffffffff0400"
525  "0000000000",
527 }
528 
529 TEST(FlStandardMessageCodecTest, EncodeFloatListEmpty) {
530  g_autoptr(FlValue) value = fl_value_new_float_list(nullptr, 0);
531  g_autofree gchar* hex_string = encode_message(value);
532  EXPECT_STREQ(hex_string, "0b00000000000000");
533 }
534 
535 TEST(FlStandardMessageCodecTest, EncodeFloatList) {
536  double data[] = {0, -0.5, 0.25, -0.125, 0.00625};
537  g_autoptr(FlValue) value = fl_value_new_float_list(data, 5);
538  g_autofree gchar* hex_string = encode_message(value);
539  EXPECT_STREQ(
540  hex_string,
541  "0b050000000000000000000000000000000000000000e0bf000000000000d03f00000000"
542  "0000c0bf9a9999999999793f");
543 }
544 
545 TEST(FlStandardMessageCodecTest, DecodeFloatListEmpty) {
546  g_autoptr(FlValue) value = decode_message("0b00000000000000");
548  EXPECT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
549 }
550 
551 TEST(FlStandardMessageCodecTest, DecodeFloatList) {
552  g_autoptr(FlValue) value = decode_message(
553  "0b050000000000000000000000000000000000000000e0bf000000000000d03f00000000"
554  "0000c0bf9a9999999999793f");
556  const double* data = fl_value_get_float_list(value);
557  EXPECT_FLOAT_EQ(data[0], 0.0);
558  EXPECT_FLOAT_EQ(data[1], -0.5);
559  EXPECT_FLOAT_EQ(data[2], 0.25);
560  EXPECT_FLOAT_EQ(data[3], -0.125);
561  EXPECT_FLOAT_EQ(data[4], 0.00625);
562 }
563 
564 TEST(FlStandardMessageCodecTest, DecodeFloatListNoData) {
567 }
568 
569 TEST(FlStandardMessageCodecTest, DecodeFloatListLengthNoData) {
570  decode_error_value("0b05000000000000", FL_MESSAGE_CODEC_ERROR,
572 }
573 
574 TEST(FlStandardMessageCodecTest, DecodeFloatListShortData1) {
575  decode_error_value("0b0500000000000000", FL_MESSAGE_CODEC_ERROR,
577 }
578 
579 TEST(FlStandardMessageCodecTest, DecodeFloatListShortData2) {
581  "0b050000000000000000000000000000000000000000e0bf000000000000d03f00000000"
582  "0000c0bf9a99"
583  "9999999979",
585 }
586 
587 TEST(FlStandardMessageCodecTest, EncodeListEmpty) {
588  g_autoptr(FlValue) value = fl_value_new_list();
589  g_autofree gchar* hex_string = encode_message(value);
590  EXPECT_STREQ(hex_string, "0c00");
591 }
592 
593 TEST(FlStandardMessageCodecTest, EncodeListTypes) {
594  g_autoptr(FlValue) value = fl_value_new_list();
602  g_autofree gchar* hex_string = encode_message(value);
603  EXPECT_STREQ(
604  hex_string,
605  "0c070001032a00000006000000000000182d4454fb210940070568656c6c6f0c000d00");
606 }
607 
608 TEST(FlStandardMessageCodecTest, EncodeListNested) {
609  g_autoptr(FlValue) even_numbers = fl_value_new_list();
610  g_autoptr(FlValue) odd_numbers = fl_value_new_list();
611  for (int i = 0; i < 10; i++) {
612  if (i % 2 == 0)
613  fl_value_append_take(even_numbers, fl_value_new_int(i));
614  else
615  fl_value_append_take(odd_numbers, fl_value_new_int(i));
616  }
617  g_autoptr(FlValue) value = fl_value_new_list();
618  fl_value_append(value, even_numbers);
619  fl_value_append(value, odd_numbers);
620  g_autofree gchar* hex_string = encode_message(value);
621  EXPECT_STREQ(hex_string,
622  "0c020c05030000000003020000000304000000030600000003080000000c"
623  "0503010000000303000000030500000003070000000309000000");
624 }
625 
626 TEST(FlStandardMessageCodecTest, DecodeListEmpty) {
627  g_autoptr(FlValue) value = decode_message("0c00");
629  EXPECT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
630 }
631 
632 TEST(FlStandardMessageCodecTest, DecodeListTypes) {
633  g_autoptr(FlValue) value = decode_message(
634  "0c070001032a00000006000000000000182d4454fb210940070568656c6c6f0c000d00");
636  ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(7));
644  EXPECT_EQ(fl_value_get_int(fl_value_get_list_value(value, 2)), 42);
647  EXPECT_FLOAT_EQ(fl_value_get_float(fl_value_get_list_value(value, 3)), M_PI);
650  EXPECT_STREQ(fl_value_get_string(fl_value_get_list_value(value, 4)), "hello");
654  static_cast<size_t>(0));
658  static_cast<size_t>(0));
659 }
660 
661 TEST(FlStandardMessageCodecTest, DecodeListNested) {
662  g_autoptr(FlValue) value = decode_message(
663  "0c020c05030000000003020000000304000000030600000003080000000c"
664  "0503010000000303000000030500000003070000000309000000");
666  ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(2));
667  FlValue* even_list = fl_value_get_list_value(value, 0);
668  ASSERT_EQ(fl_value_get_type(even_list), FL_VALUE_TYPE_LIST);
669  ASSERT_EQ(fl_value_get_length(even_list), static_cast<size_t>(5));
670  FlValue* odd_list = fl_value_get_list_value(value, 1);
671  ASSERT_EQ(fl_value_get_type(odd_list), FL_VALUE_TYPE_LIST);
672  ASSERT_EQ(fl_value_get_length(odd_list), static_cast<size_t>(5));
673  for (int i = 0; i < 5; i++) {
674  FlValue* v = fl_value_get_list_value(even_list, i);
675  ASSERT_EQ(fl_value_get_type(v), FL_VALUE_TYPE_INT);
676  EXPECT_EQ(fl_value_get_int(v), i * 2);
677 
678  v = fl_value_get_list_value(odd_list, i);
679  ASSERT_EQ(fl_value_get_type(v), FL_VALUE_TYPE_INT);
680  EXPECT_EQ(fl_value_get_int(v), i * 2 + 1);
681  }
682 }
683 
684 TEST(FlStandardMessageCodecTest, DecodeListNoData) {
687 }
688 
689 TEST(FlStandardMessageCodecTest, DecodeListLengthNoData) {
692 }
693 
694 TEST(FlStandardMessageCodecTest, DecodeListShortData1) {
697 }
698 
699 TEST(FlStandardMessageCodecTest, DecodeListShortData2) {
701  "0c070001032a00000006000000000000182d4454fb210940070568656c6c6f0c000d",
703 }
704 
705 TEST(FlStandardMessageCodecTest, EncodeDecodeLargeList) {
706  g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
707 
708  g_autoptr(FlValue) value = fl_value_new_list();
709  for (int i = 0; i < 65535; i++)
711 
712  g_autoptr(GError) error = nullptr;
713  g_autoptr(GBytes) message =
714  fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), value, &error);
715  EXPECT_NE(message, nullptr);
716  EXPECT_EQ(error, nullptr);
717 
718  g_autoptr(FlValue) decoded_value =
719  fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), message, &error);
720  EXPECT_EQ(error, nullptr);
721  EXPECT_NE(value, nullptr);
722 
723  ASSERT_TRUE(fl_value_equal(value, decoded_value));
724 }
725 
726 TEST(FlStandardMessageCodecTest, EncodeMapEmpty) {
727  g_autoptr(FlValue) value = fl_value_new_map();
728  g_autofree gchar* hex_string = encode_message(value);
729  EXPECT_STREQ(hex_string, "0d00");
730 }
731 
732 TEST(FlStandardMessageCodecTest, EncodeMapKeyTypes) {
733  g_autoptr(FlValue) value = fl_value_new_map();
736  fl_value_new_string("bool"));
739  fl_value_new_string("float"));
741  fl_value_new_string("string"));
744  g_autofree gchar* hex_string = encode_message(value);
745  EXPECT_STREQ(hex_string,
746  "0d070007046e756c6c010704626f6f6c032a0000000703696e7406000000000"
747  "0182d4454fb2109400705666c6f6174070568656c6c6f0706737472696e670c"
748  "0007046c6973740d0007036d6170");
749 }
750 
751 TEST(FlStandardMessageCodecTest, EncodeMapValueTypes) {
752  g_autoptr(FlValue) value = fl_value_new_map();
758  fl_value_new_float(M_PI));
760  fl_value_new_string("hello"));
763  g_autofree gchar* hex_string = encode_message(value);
764  EXPECT_STREQ(hex_string,
765  "0d0707046e756c6c000704626f6f6c010703696e74032a0000000705666c6f6"
766  "17406000000000000182d4454fb2109400706737472696e67070568656c6c6f"
767  "07046c6973740c0007036d61700d00");
768 }
769 
770 TEST(FlStandardMessageCodecTest, EncodeMapNested) {
771  g_autoptr(FlValue) str_to_int = fl_value_new_map();
772  g_autoptr(FlValue) int_to_str = fl_value_new_map();
773  const char* numbers[] = {"zero", "one", "two", "three", nullptr};
774  for (int i = 0; numbers[i] != nullptr; i++) {
775  fl_value_set_take(str_to_int, fl_value_new_string(numbers[i]),
776  fl_value_new_int(i));
777  fl_value_set_take(int_to_str, fl_value_new_int(i),
778  fl_value_new_string(numbers[i]));
779  }
780  g_autoptr(FlValue) value = fl_value_new_map();
781  fl_value_set_string(value, "str-to-int", str_to_int);
782  fl_value_set_string(value, "int-to-str", int_to_str);
783  g_autofree gchar* hex_string = encode_message(value);
784  EXPECT_STREQ(hex_string,
785  "0d02070a7374722d746f2d696e740d0407047a65726f030000000007036f6e6"
786  "50301000000070374776f0302000000070574687265650303000000070a696e"
787  "742d746f2d7374720d04030000000007047a65726f030100000007036f6e650"
788  "302000000070374776f030300000007057468726565");
789 }
790 
791 TEST(FlStandardMessageCodecTest, DecodeMapEmpty) {
792  g_autoptr(FlValue) value = decode_message("0d00");
794  ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
795 }
796 
797 TEST(FlStandardMessageCodecTest, DecodeMapKeyTypes) {
798  g_autoptr(FlValue) value = decode_message(
799  "0d070007046e756c6c010704626f6f6c032a0000000703696e74060000000000182d4454"
800  "fb2109400705666c6f6174070568656c6c6f0706737472696e670c0007046c6973740d00"
801  "07036d6170");
803  ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(7));
804 
809  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 0)), "null");
810 
813  EXPECT_TRUE(fl_value_get_bool(fl_value_get_map_key(value, 1)));
816  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 1)), "bool");
817 
820  EXPECT_EQ(fl_value_get_int(fl_value_get_map_key(value, 2)), 42);
823  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 2)), "int");
824 
827  EXPECT_FLOAT_EQ(fl_value_get_float(fl_value_get_map_key(value, 3)), M_PI);
830  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 3)), "float");
831 
834  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 4)), "hello");
837  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 4)), "string");
838 
842  static_cast<size_t>(0));
845  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 5)), "list");
846 
850  static_cast<size_t>(0));
853  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 6)), "map");
854 }
855 
856 TEST(FlStandardMessageCodecTest, DecodeMapValueTypes) {
857  g_autoptr(FlValue) value = decode_message(
858  "0d0707046e756c6c000704626f6f6c010703696e74032a0000000705666c6f6174060000"
859  "00000000182d4454fb2109400706737472696e67070568656c6c6f07046c6973740c0007"
860  "036d61700d00");
862  ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(7));
863 
866  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 0)), "null");
869 
872  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 1)), "bool");
876 
879  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 2)), "int");
882  EXPECT_EQ(fl_value_get_int(fl_value_get_map_value(value, 2)), 42);
883 
886  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 3)), "float");
889  EXPECT_FLOAT_EQ(fl_value_get_float(fl_value_get_map_value(value, 3)), M_PI);
890 
893  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 4)), "string");
896  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 4)), "hello");
897 
900  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 5)), "list");
904  static_cast<size_t>(0));
905 
908  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 6)), "map");
912  static_cast<size_t>(0));
913 }
914 
915 TEST(FlStandardMessageCodecTest, DecodeMapNested) {
916  g_autoptr(FlValue) value = decode_message(
917  "0d02070a7374722d746f2d696e740d0407047a65726f030000000007036f6e6503010000"
918  "00070374776f0302000000070574687265650303000000070a696e742d746f2d7374720d"
919  "04030000000007047a65726f030100000007036f6e650302000000070374776f03030000"
920  "0007057468726565");
922  ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(2));
923 
927  "str-to-int");
928  FlValue* str_to_int = fl_value_get_map_value(value, 0);
929  ASSERT_EQ(fl_value_get_type(str_to_int), FL_VALUE_TYPE_MAP);
930  ASSERT_EQ(fl_value_get_length(str_to_int), static_cast<size_t>(4));
931 
935  "int-to-str");
936  FlValue* int_to_str = fl_value_get_map_value(value, 1);
937  ASSERT_EQ(fl_value_get_type(int_to_str), FL_VALUE_TYPE_MAP);
938  ASSERT_EQ(fl_value_get_length(int_to_str), static_cast<size_t>(4));
939 
940  const char* numbers[] = {"zero", "one", "two", "three", nullptr};
941  for (int i = 0; numbers[i] != nullptr; i++) {
942  ASSERT_EQ(fl_value_get_type(fl_value_get_map_key(str_to_int, i)),
944  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(str_to_int, i)),
945  numbers[i]);
946 
947  ASSERT_EQ(fl_value_get_type(fl_value_get_map_value(str_to_int, i)),
949  EXPECT_EQ(fl_value_get_int(fl_value_get_map_value(str_to_int, i)), i);
950 
951  ASSERT_EQ(fl_value_get_type(fl_value_get_map_key(int_to_str, i)),
953  EXPECT_EQ(fl_value_get_int(fl_value_get_map_key(int_to_str, i)), i);
954 
955  ASSERT_EQ(fl_value_get_type(fl_value_get_map_value(int_to_str, i)),
957  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(int_to_str, i)),
958  numbers[i]);
959  }
960 }
961 
962 TEST(FlStandardMessageCodecTest, DecodeMapNoData) {
965 }
966 
967 TEST(FlStandardMessageCodecTest, DecodeMapLengthNoData) {
970 }
971 
972 TEST(FlStandardMessageCodecTest, DecodeMapShortData1) {
975 }
976 
977 TEST(FlStandardMessageCodecTest, DecodeMapShortData2) {
979  "0d0707046e756c6c000704626f6f6c010703696e74032a0000000705666c6f6174060000"
980  "00000000182d4454fb2109400706737472696e67070568656c6c6f07046c6973740c0007"
981  "036d61700d",
983 }
984 
985 TEST(FlStandardMessageCodecTest, EncodeDecodeLargeMap) {
986  g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
987 
988  g_autoptr(FlValue) value = fl_value_new_map();
989  for (int i = 0; i < 512; i++) {
990  g_autofree gchar* key = g_strdup_printf("key%d", i);
992  }
993 
994  g_autoptr(GError) error = nullptr;
995  g_autoptr(GBytes) message =
996  fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), value, &error);
997  EXPECT_NE(message, nullptr);
998  EXPECT_EQ(error, nullptr);
999 
1000  g_autoptr(FlValue) decoded_value =
1001  fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), message, &error);
1002  EXPECT_EQ(error, nullptr);
1003  EXPECT_NE(value, nullptr);
1004 
1005  ASSERT_TRUE(fl_value_equal(value, decoded_value));
1006 }
1007 
1008 TEST(FlStandardMessageCodecTest, DecodeUnknownType) {
1011 }
1012 
1013 TEST(FlStandardMessageCodecTest, EncodeDecode) {
1014  g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
1015 
1016  g_autoptr(FlValue) input = fl_value_new_list();
1021  fl_value_append_take(input, fl_value_new_string("hello"));
1024 
1025  g_autoptr(GError) error = nullptr;
1026  g_autoptr(GBytes) message =
1027  fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), input, &error);
1028  EXPECT_NE(message, nullptr);
1029  EXPECT_EQ(error, nullptr);
1030 
1031  g_autoptr(FlValue) output =
1032  fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), message, &error);
1033  EXPECT_EQ(error, nullptr);
1034  EXPECT_NE(output, nullptr);
1035 
1036  ASSERT_TRUE(fl_value_equal(input, output));
1037 }
G_MODULE_EXPORT FlValue * fl_value_new_list()
Definition: fl_value.cc:310
G_MODULE_EXPORT FlValue * fl_value_new_float(double value)
Definition: fl_value.cc:240
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:395
G_MODULE_EXPORT FlValue * fl_value_new_uint8_list(const uint8_t *data, size_t data_length)
Definition: fl_value.cc:263
G_MODULE_EXPORT FlValue * fl_value_new_int64_list(const int64_t *data, size_t data_length)
Definition: fl_value.cc:290
static FlValue * decode_message(const char *hex_string)
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:39
static gchar * encode_message(FlValue *value)
FlMethodResponse GError ** error
G_MODULE_EXPORT FlValue * fl_value_new_map()
Definition: fl_value.cc:327
G_MODULE_EXPORT FlValue * fl_value_new_int32_list(const int32_t *data, size_t data_length)
Definition: fl_value.cc:280
G_MODULE_EXPORT const double * fl_value_get_float_list(FlValue *self)
Definition: fl_value.cc:624
G_MODULE_EXPORT int64_t fl_value_get_int(FlValue *self)
Definition: fl_value.cc:582
gchar * bytes_to_hex_string(GBytes *bytes)
Definition: fl_test.cc:33
#define FL_MESSAGE_CODEC_ERROR
G_MODULE_EXPORT FlStandardMessageCodec * fl_standard_message_codec_new()
G_MODULE_EXPORT const int64_t * fl_value_get_int64_list(FlValue *self)
Definition: fl_value.cc:617
G_MODULE_EXPORT FlValue * fl_value_new_bool(bool value)
Definition: fl_value.cc:226
static gchar * encode_bool(gboolean value)
G_MODULE_EXPORT const uint8_t * fl_value_get_uint8_list(FlValue *self)
Definition: fl_value.cc:603
G_MODULE_EXPORT bool fl_value_equal(FlValue *a, FlValue *b)
Definition: fl_value.cc:400
G_MODULE_EXPORT void fl_value_append(FlValue *self, FlValue *value)
Definition: fl_value.cc:506
static gchar * encode_string(const gchar *value)
G_MODULE_EXPORT double fl_value_get_float(FlValue *self)
Definition: fl_value.cc:589
G_MODULE_EXPORT void fl_value_append_take(FlValue *self, FlValue *value)
Definition: fl_value.cc:514
TEST(FlStandardMessageCodecTest, EncodeNullptr)
uint8_t value
G_MODULE_EXPORT void fl_value_set_take(FlValue *self, FlValue *key, FlValue *value)
Definition: fl_value.cc:532
static gchar * encode_int(int64_t value)
G_MODULE_EXPORT FlValue * fl_value_get_list_value(FlValue *self, size_t index)
Definition: fl_value.cc:677
G_MODULE_EXPORT FlValue * fl_value_new_int(int64_t value)
Definition: fl_value.cc:233
G_MODULE_EXPORT FlValue * fl_value_get_map_value(FlValue *self, size_t index)
Definition: fl_value.cc:693
GBytes * hex_string_to_bytes(const gchar *hex_string)
Definition: fl_test.cc:24
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:247
static void decode_error_value(const char *hex_string, GQuark domain, gint code)
G_MODULE_EXPORT FlValue * fl_value_ref(FlValue *self)
Definition: fl_value.cc:335
static gchar * encode_float(double value)
G_MODULE_EXPORT FlValue * fl_value_get_map_key(FlValue *self, size_t index)
Definition: fl_value.cc:685
G_MODULE_EXPORT const int32_t * fl_value_get_int32_list(FlValue *self)
Definition: fl_value.cc:610
G_MODULE_EXPORT GBytes * fl_message_codec_encode_message(FlMessageCodec *self, FlValue *message, GError **error)
return TRUE
Definition: fl_view.cc:107
G_MODULE_EXPORT const gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:596
G_MODULE_EXPORT FlValue * fl_message_codec_decode_message(FlMessageCodec *self, GBytes *message, GError **error)
G_MODULE_EXPORT void fl_value_set_string(FlValue *self, const gchar *key, FlValue *value)
Definition: fl_value.cc:553
G_MODULE_EXPORT void fl_value_set_string_take(FlValue *self, const gchar *key, FlValue *value)
Definition: fl_value.cc:564
G_MODULE_EXPORT FlValue * fl_value_new_float_list(const double *data, size_t data_length)
Definition: fl_value.cc:300
G_MODULE_EXPORT FlValue * fl_value_new_string_sized(const gchar *value, size_t value_length)
Definition: fl_value.cc:254
G_MODULE_EXPORT size_t fl_value_get_length(FlValue *self)
Definition: fl_value.cc:631
G_MODULE_EXPORT FlValue * fl_value_new_null()
Definition: fl_value.cc:222
G_MODULE_EXPORT bool fl_value_get_bool(FlValue *self)
Definition: fl_value.cc:575