Flutter Linux Embedder
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 
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 the supplied codec. Return a hex string with
13 // the encoded binary output.
14 static gchar* encode_message_with_codec(FlValue* value, FlMessageCodec* codec) {
15  g_autoptr(GError) error = nullptr;
16  g_autoptr(GBytes) message =
18  EXPECT_NE(message, nullptr);
19  EXPECT_EQ(error, nullptr);
20 
21  return bytes_to_hex_string(message);
22 }
23 
24 // Encodes a message using a FlStandardMessageCodec. Return a hex string with
25 // the encoded binary output.
26 static gchar* encode_message(FlValue* value) {
27  g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
28  return encode_message_with_codec(value, FL_MESSAGE_CODEC(codec));
29 }
30 
31 // Decodes a message using the supplied codec. The binary data is given in
32 // the form of a hex string.
33 static FlValue* decode_message_with_codec(const char* hex_string,
34  FlMessageCodec* codec) {
35  g_autoptr(GBytes) data = hex_string_to_bytes(hex_string);
36  g_autoptr(GError) error = nullptr;
37  g_autoptr(FlValue) value =
39  EXPECT_EQ(error, nullptr);
40  EXPECT_NE(value, nullptr);
41  return fl_value_ref(value);
42 }
43 
44 // Decodes a message using a FlStandardMessageCodec. The binary data is given in
45 // the form of a hex string.
46 static FlValue* decode_message(const char* hex_string) {
47  g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
48  return decode_message_with_codec(hex_string, FL_MESSAGE_CODEC(codec));
49 }
50 
51 // Decodes a message using a FlStandardMessageCodec. The binary data is given in
52 // the form of a hex string. Expect the given error.
53 static void decode_error_value(const char* hex_string,
54  GQuark domain,
55  gint code) {
56  g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
57  g_autoptr(GBytes) data = hex_string_to_bytes(hex_string);
58  g_autoptr(GError) error = nullptr;
59  g_autoptr(FlValue) value =
60  fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), data, &error);
61  EXPECT_TRUE(value == nullptr);
62  EXPECT_TRUE(g_error_matches(error, domain, code));
63 }
64 
65 TEST(FlStandardMessageCodecTest, EncodeNullptr) {
66  g_autofree gchar* hex_string = encode_message(nullptr);
67  EXPECT_STREQ(hex_string, "00");
68 }
69 
70 TEST(FlStandardMessageCodecTest, EncodeNull) {
71  g_autoptr(FlValue) value = fl_value_new_null();
72  g_autofree gchar* hex_string = encode_message(value);
73  EXPECT_STREQ(hex_string, "00");
74 }
75 
76 TEST(FlStandardMessageCodecTest, DecodeNull) {
77  // Regression test for https://github.com/flutter/flutter/issues/128704.
78 
79  g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
80  g_autoptr(GBytes) data = g_bytes_new(nullptr, 0);
81  g_autoptr(GError) error = nullptr;
82  g_autoptr(FlValue) value =
83  fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), data, &error);
84 
85  EXPECT_FALSE(value == nullptr);
87 }
88 
89 static gchar* encode_bool(gboolean value) {
90  g_autoptr(FlValue) v = fl_value_new_bool(value);
91  return encode_message(v);
92 }
93 
94 TEST(FlStandardMessageCodecTest, EncodeBoolFalse) {
95  g_autofree gchar* hex_string = encode_bool(FALSE);
96  EXPECT_STREQ(hex_string, "02");
97 }
98 
99 TEST(FlStandardMessageCodecTest, EncodeBoolTrue) {
100  g_autofree gchar* hex_string = encode_bool(TRUE);
101  EXPECT_STREQ(hex_string, "01");
102 }
103 
104 static gchar* encode_int(int64_t value) {
105  g_autoptr(FlValue) v = fl_value_new_int(value);
106  return encode_message(v);
107 }
108 
109 TEST(FlStandardMessageCodecTest, EncodeIntZero) {
110  g_autofree gchar* hex_string = encode_int(0);
111  EXPECT_STREQ(hex_string, "0300000000");
112 }
113 
114 TEST(FlStandardMessageCodecTest, EncodeIntOne) {
115  g_autofree gchar* hex_string = encode_int(1);
116  EXPECT_STREQ(hex_string, "0301000000");
117 }
118 
119 TEST(FlStandardMessageCodecTest, EncodeInt32) {
120  g_autofree gchar* hex_string = encode_int(0x01234567);
121  EXPECT_STREQ(hex_string, "0367452301");
122 }
123 
124 TEST(FlStandardMessageCodecTest, EncodeInt32Min) {
125  g_autofree gchar* hex_string = encode_int(G_MININT32);
126  EXPECT_STREQ(hex_string, "0300000080");
127 }
128 
129 TEST(FlStandardMessageCodecTest, EncodeInt32Max) {
130  g_autofree gchar* hex_string = encode_int(G_MAXINT32);
131  EXPECT_STREQ(hex_string, "03ffffff7f");
132 }
133 
134 TEST(FlStandardMessageCodecTest, EncodeInt64) {
135  g_autofree gchar* hex_string = encode_int(0x0123456789abcdef);
136  EXPECT_STREQ(hex_string, "04efcdab8967452301");
137 }
138 
139 TEST(FlStandardMessageCodecTest, EncodeInt64Min) {
140  g_autofree gchar* hex_string = encode_int(G_MININT64);
141  EXPECT_STREQ(hex_string, "040000000000000080");
142 }
143 
144 TEST(FlStandardMessageCodecTest, EncodeInt64Max) {
145  g_autofree gchar* hex_string = encode_int(G_MAXINT64);
146  EXPECT_STREQ(hex_string, "04ffffffffffffff7f");
147 }
148 
149 TEST(FlStandardMessageCodecTest, DecodeIntZero) {
150  g_autoptr(FlValue) value = decode_message("0300000000");
152  EXPECT_EQ(fl_value_get_int(value), 0);
153 }
154 
155 TEST(FlStandardMessageCodecTest, DecodeIntOne) {
156  g_autoptr(FlValue) value = decode_message("0301000000");
158  EXPECT_EQ(fl_value_get_int(value), 1);
159 }
160 
161 TEST(FlStandardMessageCodecTest, DecodeInt32) {
162  g_autoptr(FlValue) value = decode_message("0367452301");
164  EXPECT_EQ(fl_value_get_int(value), 0x01234567);
165 }
166 
167 TEST(FlStandardMessageCodecTest, DecodeInt32Min) {
168  g_autoptr(FlValue) value = decode_message("0300000080");
170  EXPECT_EQ(fl_value_get_int(value), G_MININT32);
171 }
172 
173 TEST(FlStandardMessageCodecTest, DecodeInt32Max) {
174  g_autoptr(FlValue) value = decode_message("03ffffff7f");
176  EXPECT_EQ(fl_value_get_int(value), G_MAXINT32);
177 }
178 
179 TEST(FlStandardMessageCodecTest, DecodeInt64) {
180  g_autoptr(FlValue) value = decode_message("04efcdab8967452301");
182  EXPECT_EQ(fl_value_get_int(value), 0x0123456789abcdef);
183 }
184 
185 TEST(FlStandardMessageCodecTest, DecodeInt64Min) {
186  g_autoptr(FlValue) value = decode_message("040000000000000080");
188  EXPECT_EQ(fl_value_get_int(value), G_MININT64);
189 }
190 
191 TEST(FlStandardMessageCodecTest, DecodeInt64Max) {
192  g_autoptr(FlValue) value = decode_message("04ffffffffffffff7f");
194  EXPECT_EQ(fl_value_get_int(value), G_MAXINT64);
195 }
196 
197 TEST(FlStandardMessageCodecTest, DecodeInt32NoData) {
200 }
201 
202 TEST(FlStandardMessageCodecTest, DecodeIntShortData1) {
205 }
206 
207 TEST(FlStandardMessageCodecTest, DecodeIntShortData2) {
210 }
211 
212 TEST(FlStandardMessageCodecTest, DecodeInt64NoData) {
215 }
216 
217 TEST(FlStandardMessageCodecTest, DecodeInt64ShortData1) {
220 }
221 
222 TEST(FlStandardMessageCodecTest, DecodeInt64ShortData2) {
223  decode_error_value("04efcdab89674523", FL_MESSAGE_CODEC_ERROR,
225 }
226 
227 static gchar* encode_float(double value) {
228  g_autoptr(FlValue) v = fl_value_new_float(value);
229  return encode_message(v);
230 }
231 
232 TEST(FlStandardMessageCodecTest, EncodeFloatZero) {
233  g_autofree gchar* hex_string = encode_float(0);
234  EXPECT_STREQ(hex_string, "06000000000000000000000000000000");
235 }
236 
237 TEST(FlStandardMessageCodecTest, EncodeFloatOne) {
238  g_autofree gchar* hex_string = encode_float(1);
239  EXPECT_STREQ(hex_string, "0600000000000000000000000000f03f");
240 }
241 
242 TEST(FlStandardMessageCodecTest, EncodeFloatMinusOne) {
243  g_autofree gchar* hex_string = encode_float(-1);
244  EXPECT_STREQ(hex_string, "0600000000000000000000000000f0bf");
245 }
246 
247 TEST(FlStandardMessageCodecTest, EncodeFloatHalf) {
248  g_autofree gchar* hex_string = encode_float(0.5);
249  EXPECT_STREQ(hex_string, "0600000000000000000000000000e03f");
250 }
251 
252 TEST(FlStandardMessageCodecTest, EncodeFloatFraction) {
253  g_autofree gchar* hex_string = encode_float(M_PI);
254  EXPECT_STREQ(hex_string, "0600000000000000182d4454fb210940");
255 }
256 
257 TEST(FlStandardMessageCodecTest, EncodeFloatMinusZero) {
258  g_autofree gchar* hex_string = encode_float(-0.0);
259  EXPECT_STREQ(hex_string, "06000000000000000000000000000080");
260 }
261 
262 TEST(FlStandardMessageCodecTest, EncodeFloatNaN) {
263  g_autofree gchar* hex_string = encode_float(NAN);
264  EXPECT_STREQ(hex_string, "0600000000000000000000000000f87f");
265 }
266 
267 TEST(FlStandardMessageCodecTest, EncodeFloatInfinity) {
268  g_autofree gchar* hex_string = encode_float(INFINITY);
269  EXPECT_STREQ(hex_string, "0600000000000000000000000000f07f");
270 }
271 
272 TEST(FlStandardMessageCodecTest, DecodeFloatZero) {
273  g_autoptr(FlValue) value = decode_message("06000000000000000000000000000000");
275  EXPECT_EQ(fl_value_get_float(value), 0.0);
276 }
277 
278 TEST(FlStandardMessageCodecTest, DecodeFloatOne) {
279  g_autoptr(FlValue) value = decode_message("0600000000000000000000000000f03f");
281  EXPECT_EQ(fl_value_get_float(value), 1.0);
282 }
283 
284 TEST(FlStandardMessageCodecTest, DecodeFloatMinusOne) {
285  g_autoptr(FlValue) value = decode_message("0600000000000000000000000000f0bf");
287  EXPECT_EQ(fl_value_get_float(value), -1.0);
288 }
289 
290 TEST(FlStandardMessageCodecTest, DecodeFloatHalf) {
291  g_autoptr(FlValue) value = decode_message("0600000000000000000000000000e03f");
293  EXPECT_EQ(fl_value_get_float(value), 0.5);
294 }
295 
296 TEST(FlStandardMessageCodecTest, DecodeFloatPi) {
297  g_autoptr(FlValue) value = decode_message("0600000000000000182d4454fb210940");
299  EXPECT_EQ(fl_value_get_float(value), M_PI);
300 }
301 
302 TEST(FlStandardMessageCodecTest, DecodeFloatMinusZero) {
303  g_autoptr(FlValue) value = decode_message("06000000000000000000000000000080");
305  EXPECT_EQ(fl_value_get_float(value), -0.0);
306 }
307 
308 TEST(FlStandardMessageCodecTest, DecodeFloatNaN) {
309  g_autoptr(FlValue) value = decode_message("0600000000000000000000000000f87f");
311  EXPECT_TRUE(isnan(fl_value_get_float(value)));
312 }
313 
314 TEST(FlStandardMessageCodecTest, DecodeFloatInfinity) {
315  g_autoptr(FlValue) value = decode_message("0600000000000000000000000000f07f");
317  EXPECT_TRUE(isinf(fl_value_get_float(value)));
318 }
319 
320 TEST(FlStandardMessageCodecTest, DecodeFloatNoData) {
321  decode_error_value("060000000000000000", FL_MESSAGE_CODEC_ERROR,
323 }
324 
325 TEST(FlStandardMessageCodecTest, DecodeFloatShortData1) {
326  decode_error_value("060000000000000000", FL_MESSAGE_CODEC_ERROR,
328 }
329 
330 TEST(FlStandardMessageCodecTest, DecodeFloatShortData2) {
331  decode_error_value("060000000000000000000000000000", FL_MESSAGE_CODEC_ERROR,
333 }
334 
335 static gchar* encode_string(const gchar* value) {
336  g_autoptr(FlValue) v = fl_value_new_string(value);
337  return encode_message(v);
338 }
339 
340 TEST(FlStandardMessageCodecTest, EncodeStringEmpty) {
341  g_autofree gchar* hex_string = encode_string("");
342  EXPECT_STREQ(hex_string, "0700");
343 }
344 
345 TEST(FlStandardMessageCodecTest, EncodeStringHello) {
346  g_autofree gchar* hex_string = encode_string("hello");
347  EXPECT_STREQ(hex_string, "070568656c6c6f");
348 }
349 
350 TEST(FlStandardMessageCodecTest, EncodeStringEmptySized) {
351  g_autoptr(FlValue) value = fl_value_new_string_sized(nullptr, 0);
352  g_autofree gchar* hex_string = encode_message(value);
353  EXPECT_STREQ(hex_string, "0700");
354 }
355 
356 TEST(FlStandardMessageCodecTest, EncodeStringHelloSized) {
357  g_autoptr(FlValue) value = fl_value_new_string_sized("Hello World", 5);
358  g_autofree gchar* hex_string = encode_message(value);
359  EXPECT_STREQ(hex_string, "070548656c6c6f");
360 }
361 
362 TEST(FlStandardMessageCodecTest, DecodeStringEmpty) {
363  g_autoptr(FlValue) value = decode_message("0700");
365  EXPECT_STREQ(fl_value_get_string(value), "");
366 }
367 
368 TEST(FlStandardMessageCodecTest, DecodeStringHello) {
369  g_autoptr(FlValue) value = decode_message("070568656c6c6f");
371  EXPECT_STREQ(fl_value_get_string(value), "hello");
372 }
373 
374 TEST(FlStandardMessageCodecTest, DecodeStringNoData) {
377 }
378 
379 TEST(FlStandardMessageCodecTest, DecodeStringLengthNoData) {
382 }
383 
384 TEST(FlStandardMessageCodecTest, DecodeStringShortData1) {
387 }
388 
389 TEST(FlStandardMessageCodecTest, DecodeStringShortData2) {
392 }
393 
394 TEST(FlStandardMessageCodecTest, EncodeUint8ListEmpty) {
395  g_autoptr(FlValue) value = fl_value_new_uint8_list(nullptr, 0);
396  g_autofree gchar* hex_string = encode_message(value);
397  EXPECT_STREQ(hex_string, "0800");
398 }
399 
400 TEST(FlStandardMessageCodecTest, EncodeUint8List) {
401  uint8_t data[] = {0, 1, 2, 3, 4};
402  g_autoptr(FlValue) value = fl_value_new_uint8_list(data, 5);
403  g_autofree gchar* hex_string = encode_message(value);
404  EXPECT_STREQ(hex_string, "08050001020304");
405 }
406 
407 TEST(FlStandardMessageCodecTest, DecodeUint8ListEmpty) {
408  g_autoptr(FlValue) value = decode_message("0800");
410  EXPECT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
411 }
412 
413 TEST(FlStandardMessageCodecTest, DecodeUint8List) {
414  g_autoptr(FlValue) value = decode_message("08050001020304");
416  EXPECT_EQ(fl_value_get_length(value), static_cast<size_t>(5));
417  const uint8_t* data = fl_value_get_uint8_list(value);
418  EXPECT_EQ(data[0], 0);
419  EXPECT_EQ(data[1], 1);
420  EXPECT_EQ(data[2], 2);
421  EXPECT_EQ(data[3], 3);
422  EXPECT_EQ(data[4], 4);
423 }
424 
425 TEST(FlStandardMessageCodecTest, DecodeUint8ListNoData) {
428 }
429 
430 TEST(FlStandardMessageCodecTest, DecodeUint8ListLengthNoData) {
433 }
434 
435 TEST(FlStandardMessageCodecTest, DecodeUint8ListShortData1) {
438 }
439 
440 TEST(FlStandardMessageCodecTest, DecodeUint8ListShortData2) {
443 }
444 
445 TEST(FlStandardMessageCodecTest, EncodeInt32ListEmpty) {
446  g_autoptr(FlValue) value = fl_value_new_int32_list(nullptr, 0);
447  g_autofree gchar* hex_string = encode_message(value);
448  EXPECT_STREQ(hex_string, "09000000");
449 }
450 
451 TEST(FlStandardMessageCodecTest, EncodeInt32List) {
452  int32_t data[] = {0, -1, 2, -3, 4};
453  g_autoptr(FlValue) value = fl_value_new_int32_list(data, 5);
454  g_autofree gchar* hex_string = encode_message(value);
455  EXPECT_STREQ(hex_string, "0905000000000000ffffffff02000000fdffffff04000000");
456 }
457 
458 TEST(FlStandardMessageCodecTest, DecodeInt32ListEmpty) {
459  g_autoptr(FlValue) value = decode_message("09000000");
461  EXPECT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
462 }
463 
464 TEST(FlStandardMessageCodecTest, DecodeInt32List) {
465  g_autoptr(FlValue) value =
466  decode_message("0905000000000000ffffffff02000000fdffffff04000000");
468  const int32_t* data = fl_value_get_int32_list(value);
469  EXPECT_EQ(data[0], 0);
470  EXPECT_EQ(data[1], -1);
471  EXPECT_EQ(data[2], 2);
472  EXPECT_EQ(data[3], -3);
473  EXPECT_EQ(data[4], 4);
474 }
475 
476 TEST(FlStandardMessageCodecTest, DecodeInt32ListNoData) {
479 }
480 
481 TEST(FlStandardMessageCodecTest, DecodeInt32ListLengthNoData) {
484 }
485 
486 TEST(FlStandardMessageCodecTest, DecodeInt32ListShortData1) {
489 }
490 
491 TEST(FlStandardMessageCodecTest, DecodeInt32ListShortData2) {
492  decode_error_value("090500000000ffffffff02000000fdffffff040000",
495 }
496 
497 TEST(FlStandardMessageCodecTest, EncodeInt64ListEmpty) {
498  g_autoptr(FlValue) value = fl_value_new_int64_list(nullptr, 0);
499  g_autofree gchar* hex_string = encode_message(value);
500  EXPECT_STREQ(hex_string, "0a00000000000000");
501 }
502 
503 TEST(FlStandardMessageCodecTest, EncodeInt64List) {
504  int64_t data[] = {0, -1, 2, -3, 4};
505  g_autoptr(FlValue) value = fl_value_new_int64_list(data, 5);
506  g_autofree gchar* hex_string = encode_message(value);
507  EXPECT_STREQ(
508  hex_string,
509  "0a050000000000000000000000000000ffffffffffffffff0200000000000000fdffffff"
510  "ffffffff0400000000000000");
511 }
512 
513 TEST(FlStandardMessageCodecTest, DecodeInt64ListEmpty) {
514  g_autoptr(FlValue) value = decode_message("0a00000000000000");
516  EXPECT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
517 }
518 
519 TEST(FlStandardMessageCodecTest, DecodeInt64List) {
520  g_autoptr(FlValue) value = decode_message(
521  "0a050000000000000000000000000000ffffffffffffffff0200000000000000fdffffff"
522  "ffffffff0400000000000000");
524  const int64_t* data = fl_value_get_int64_list(value);
525  EXPECT_EQ(data[0], 0);
526  EXPECT_EQ(data[1], -1);
527  EXPECT_EQ(data[2], 2);
528  EXPECT_EQ(data[3], -3);
529  EXPECT_EQ(data[4], 4);
530 }
531 
532 TEST(FlStandardMessageCodecTest, DecodeInt64ListNoData) {
535 }
536 
537 TEST(FlStandardMessageCodecTest, DecodeInt64ListLengthNoData) {
538  decode_error_value("0a05000000000000", FL_MESSAGE_CODEC_ERROR,
540 }
541 
542 TEST(FlStandardMessageCodecTest, DecodeInt64ListShortData1) {
543  decode_error_value("0a0500000000000000", FL_MESSAGE_CODEC_ERROR,
545 }
546 
547 TEST(FlStandardMessageCodecTest, DecodeInt64ListShortData2) {
549  "0a050000000000000000000000000000ffffffffffffffff0200000000000000fdffffff"
550  "ffffffff0400"
551  "0000000000",
553 }
554 
555 TEST(FlStandardMessageCodecTest, EncodeFloat32ListEmpty) {
556  g_autoptr(FlValue) value = fl_value_new_float32_list(nullptr, 0);
557  g_autofree gchar* hex_string = encode_message(value);
558  EXPECT_STREQ(hex_string, "0e000000");
559 }
560 
561 TEST(FlStandardMessageCodecTest, EncodeFloat32List) {
562  float data[] = {0.0f, -0.5f, 0.25f, -0.125f, 0.00625f};
563  g_autoptr(FlValue) value = fl_value_new_float32_list(data, 5);
564  g_autofree gchar* hex_string = encode_message(value);
565  EXPECT_STREQ(hex_string, "0e05000000000000000000bf0000803e000000becdcccc3b");
566 }
567 
568 TEST(FlStandardMessageCodecTest, DecodeFloat32ListEmpty) {
569  g_autoptr(FlValue) value = decode_message("0e000000");
571  EXPECT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
572 }
573 
574 TEST(FlStandardMessageCodecTest, DecodeFloat32List) {
575  g_autoptr(FlValue) value =
576  decode_message("0e05000000000000000000bf0000803e000000becdcccc3b");
578  const float* data = fl_value_get_float32_list(value);
579  EXPECT_FLOAT_EQ(data[0], 0.0f);
580  EXPECT_FLOAT_EQ(data[1], -0.5f);
581  EXPECT_FLOAT_EQ(data[2], 0.25f);
582  EXPECT_FLOAT_EQ(data[3], -0.125f);
583  EXPECT_FLOAT_EQ(data[4], 0.00625f);
584 }
585 
586 TEST(FlStandardMessageCodecTest, DecodeFloat32ListNoData) {
589 }
590 
591 TEST(FlStandardMessageCodecTest, DecodeFloat32ListLengthNoData) {
594 }
595 
596 TEST(FlStandardMessageCodecTest, DecodeFloat32ListShortData1) {
599 }
600 
601 TEST(FlStandardMessageCodecTest, DecodeFloat32ListShortData2) {
602  decode_error_value("0e05000000000000000000bf0000803e000000becdcccc",
605 }
606 
607 TEST(FlStandardMessageCodecTest, EncodeFloatListEmpty) {
608  g_autoptr(FlValue) value = fl_value_new_float_list(nullptr, 0);
609  g_autofree gchar* hex_string = encode_message(value);
610  EXPECT_STREQ(hex_string, "0b00000000000000");
611 }
612 
613 TEST(FlStandardMessageCodecTest, EncodeFloatList) {
614  double data[] = {0, -0.5, 0.25, -0.125, 0.00625};
615  g_autoptr(FlValue) value = fl_value_new_float_list(data, 5);
616  g_autofree gchar* hex_string = encode_message(value);
617  EXPECT_STREQ(
618  hex_string,
619  "0b050000000000000000000000000000000000000000e0bf000000000000d03f00000000"
620  "0000c0bf9a9999999999793f");
621 }
622 
623 TEST(FlStandardMessageCodecTest, DecodeFloatListEmpty) {
624  g_autoptr(FlValue) value = decode_message("0b00000000000000");
626  EXPECT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
627 }
628 
629 TEST(FlStandardMessageCodecTest, DecodeFloatList) {
630  g_autoptr(FlValue) value = decode_message(
631  "0b050000000000000000000000000000000000000000e0bf000000000000d03f00000000"
632  "0000c0bf9a9999999999793f");
634  const double* data = fl_value_get_float_list(value);
635  EXPECT_FLOAT_EQ(data[0], 0.0);
636  EXPECT_FLOAT_EQ(data[1], -0.5);
637  EXPECT_FLOAT_EQ(data[2], 0.25);
638  EXPECT_FLOAT_EQ(data[3], -0.125);
639  EXPECT_FLOAT_EQ(data[4], 0.00625);
640 }
641 
642 TEST(FlStandardMessageCodecTest, DecodeFloatListNoData) {
645 }
646 
647 TEST(FlStandardMessageCodecTest, DecodeFloatListLengthNoData) {
648  decode_error_value("0b05000000000000", FL_MESSAGE_CODEC_ERROR,
650 }
651 
652 TEST(FlStandardMessageCodecTest, DecodeFloatListShortData1) {
653  decode_error_value("0b0500000000000000", FL_MESSAGE_CODEC_ERROR,
655 }
656 
657 TEST(FlStandardMessageCodecTest, DecodeFloatListShortData2) {
659  "0b050000000000000000000000000000000000000000e0bf000000000000d03f00000000"
660  "0000c0bf9a99"
661  "9999999979",
663 }
664 
665 TEST(FlStandardMessageCodecTest, EncodeListEmpty) {
666  g_autoptr(FlValue) value = fl_value_new_list();
667  g_autofree gchar* hex_string = encode_message(value);
668  EXPECT_STREQ(hex_string, "0c00");
669 }
670 
671 TEST(FlStandardMessageCodecTest, EncodeListTypes) {
672  g_autoptr(FlValue) value = fl_value_new_list();
680  g_autofree gchar* hex_string = encode_message(value);
681  EXPECT_STREQ(
682  hex_string,
683  "0c070001032a00000006000000000000182d4454fb210940070568656c6c6f0c000d00");
684 }
685 
686 TEST(FlStandardMessageCodecTest, EncodeListNested) {
687  g_autoptr(FlValue) even_numbers = fl_value_new_list();
688  g_autoptr(FlValue) odd_numbers = fl_value_new_list();
689  for (int i = 0; i < 10; i++) {
690  if (i % 2 == 0) {
691  fl_value_append_take(even_numbers, fl_value_new_int(i));
692  } else {
693  fl_value_append_take(odd_numbers, fl_value_new_int(i));
694  }
695  }
696  g_autoptr(FlValue) value = fl_value_new_list();
697  fl_value_append(value, even_numbers);
698  fl_value_append(value, odd_numbers);
699  g_autofree gchar* hex_string = encode_message(value);
700  EXPECT_STREQ(hex_string,
701  "0c020c05030000000003020000000304000000030600000003080000000c"
702  "0503010000000303000000030500000003070000000309000000");
703 }
704 
705 TEST(FlStandardMessageCodecTest, DecodeListEmpty) {
706  g_autoptr(FlValue) value = decode_message("0c00");
708  EXPECT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
709 }
710 
711 TEST(FlStandardMessageCodecTest, DecodeListTypes) {
712  g_autoptr(FlValue) value = decode_message(
713  "0c070001032a00000006000000000000182d4454fb210940070568656c6c6f0c000d00");
715  ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(7));
723  EXPECT_EQ(fl_value_get_int(fl_value_get_list_value(value, 2)), 42);
726  EXPECT_FLOAT_EQ(fl_value_get_float(fl_value_get_list_value(value, 3)), M_PI);
729  EXPECT_STREQ(fl_value_get_string(fl_value_get_list_value(value, 4)), "hello");
733  static_cast<size_t>(0));
737  static_cast<size_t>(0));
738 }
739 
740 TEST(FlStandardMessageCodecTest, DecodeListNested) {
741  g_autoptr(FlValue) value = decode_message(
742  "0c020c05030000000003020000000304000000030600000003080000000c"
743  "0503010000000303000000030500000003070000000309000000");
745  ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(2));
746  FlValue* even_list = fl_value_get_list_value(value, 0);
747  ASSERT_EQ(fl_value_get_type(even_list), FL_VALUE_TYPE_LIST);
748  ASSERT_EQ(fl_value_get_length(even_list), static_cast<size_t>(5));
749  FlValue* odd_list = fl_value_get_list_value(value, 1);
750  ASSERT_EQ(fl_value_get_type(odd_list), FL_VALUE_TYPE_LIST);
751  ASSERT_EQ(fl_value_get_length(odd_list), static_cast<size_t>(5));
752  for (int i = 0; i < 5; i++) {
753  FlValue* v = fl_value_get_list_value(even_list, i);
754  ASSERT_EQ(fl_value_get_type(v), FL_VALUE_TYPE_INT);
755  EXPECT_EQ(fl_value_get_int(v), i * 2);
756 
757  v = fl_value_get_list_value(odd_list, i);
758  ASSERT_EQ(fl_value_get_type(v), FL_VALUE_TYPE_INT);
759  EXPECT_EQ(fl_value_get_int(v), i * 2 + 1);
760  }
761 }
762 
763 TEST(FlStandardMessageCodecTest, DecodeListNoData) {
766 }
767 
768 TEST(FlStandardMessageCodecTest, DecodeListLengthNoData) {
771 }
772 
773 TEST(FlStandardMessageCodecTest, DecodeListShortData1) {
776 }
777 
778 TEST(FlStandardMessageCodecTest, DecodeListShortData2) {
780  "0c070001032a00000006000000000000182d4454fb210940070568656c6c6f0c000d",
782 }
783 
784 TEST(FlStandardMessageCodecTest, EncodeDecodeLargeList) {
785  g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
786 
787  g_autoptr(FlValue) value = fl_value_new_list();
788  for (int i = 0; i < 65535; i++) {
790  }
791 
792  g_autoptr(GError) error = nullptr;
793  g_autoptr(GBytes) message =
794  fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), value, &error);
795  EXPECT_NE(message, nullptr);
796  EXPECT_EQ(error, nullptr);
797 
798  g_autoptr(FlValue) decoded_value =
799  fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), message, &error);
800  EXPECT_EQ(error, nullptr);
801  EXPECT_NE(value, nullptr);
802 
803  ASSERT_TRUE(fl_value_equal(value, decoded_value));
804 }
805 
806 TEST(FlStandardMessageCodecTest, EncodeMapEmpty) {
807  g_autoptr(FlValue) value = fl_value_new_map();
808  g_autofree gchar* hex_string = encode_message(value);
809  EXPECT_STREQ(hex_string, "0d00");
810 }
811 
812 TEST(FlStandardMessageCodecTest, EncodeMapKeyTypes) {
813  g_autoptr(FlValue) value = fl_value_new_map();
816  fl_value_new_string("bool"));
819  fl_value_new_string("float"));
821  fl_value_new_string("string"));
824  g_autofree gchar* hex_string = encode_message(value);
825  EXPECT_STREQ(hex_string,
826  "0d070007046e756c6c010704626f6f6c032a0000000703696e7406000000000"
827  "0182d4454fb2109400705666c6f6174070568656c6c6f0706737472696e670c"
828  "0007046c6973740d0007036d6170");
829 }
830 
831 TEST(FlStandardMessageCodecTest, EncodeMapValueTypes) {
832  g_autoptr(FlValue) value = fl_value_new_map();
838  fl_value_new_float(M_PI));
840  fl_value_new_string("hello"));
843  g_autofree gchar* hex_string = encode_message(value);
844  EXPECT_STREQ(hex_string,
845  "0d0707046e756c6c000704626f6f6c010703696e74032a0000000705666c6f6"
846  "17406000000000000182d4454fb2109400706737472696e67070568656c6c6f"
847  "07046c6973740c0007036d61700d00");
848 }
849 
850 TEST(FlStandardMessageCodecTest, EncodeMapNested) {
851  g_autoptr(FlValue) str_to_int = fl_value_new_map();
852  g_autoptr(FlValue) int_to_str = fl_value_new_map();
853  const char* numbers[] = {"zero", "one", "two", "three", nullptr};
854  for (int i = 0; numbers[i] != nullptr; i++) {
855  fl_value_set_take(str_to_int, fl_value_new_string(numbers[i]),
857  fl_value_set_take(int_to_str, fl_value_new_int(i),
858  fl_value_new_string(numbers[i]));
859  }
860  g_autoptr(FlValue) value = fl_value_new_map();
861  fl_value_set_string(value, "str-to-int", str_to_int);
862  fl_value_set_string(value, "int-to-str", int_to_str);
863  g_autofree gchar* hex_string = encode_message(value);
864  EXPECT_STREQ(hex_string,
865  "0d02070a7374722d746f2d696e740d0407047a65726f030000000007036f6e6"
866  "50301000000070374776f0302000000070574687265650303000000070a696e"
867  "742d746f2d7374720d04030000000007047a65726f030100000007036f6e650"
868  "302000000070374776f030300000007057468726565");
869 }
870 
871 TEST(FlStandardMessageCodecTest, DecodeMapEmpty) {
872  g_autoptr(FlValue) value = decode_message("0d00");
874  ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
875 }
876 
877 TEST(FlStandardMessageCodecTest, DecodeMapKeyTypes) {
878  g_autoptr(FlValue) value = decode_message(
879  "0d070007046e756c6c010704626f6f6c032a0000000703696e74060000000000182d4454"
880  "fb2109400705666c6f6174070568656c6c6f0706737472696e670c0007046c6973740d00"
881  "07036d6170");
883  ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(7));
884 
889  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 0)), "null");
890 
893  EXPECT_TRUE(fl_value_get_bool(fl_value_get_map_key(value, 1)));
896  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 1)), "bool");
897 
900  EXPECT_EQ(fl_value_get_int(fl_value_get_map_key(value, 2)), 42);
903  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 2)), "int");
904 
907  EXPECT_FLOAT_EQ(fl_value_get_float(fl_value_get_map_key(value, 3)), M_PI);
910  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 3)), "float");
911 
914  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 4)), "hello");
917  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 4)), "string");
918 
922  static_cast<size_t>(0));
925  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 5)), "list");
926 
930  static_cast<size_t>(0));
933  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 6)), "map");
934 }
935 
936 TEST(FlStandardMessageCodecTest, DecodeMapValueTypes) {
937  g_autoptr(FlValue) value = decode_message(
938  "0d0707046e756c6c000704626f6f6c010703696e74032a0000000705666c6f6174060000"
939  "00000000182d4454fb2109400706737472696e67070568656c6c6f07046c6973740c0007"
940  "036d61700d00");
942  ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(7));
943 
946  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 0)), "null");
949 
952  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 1)), "bool");
956 
959  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 2)), "int");
962  EXPECT_EQ(fl_value_get_int(fl_value_get_map_value(value, 2)), 42);
963 
966  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 3)), "float");
969  EXPECT_FLOAT_EQ(fl_value_get_float(fl_value_get_map_value(value, 3)), M_PI);
970 
973  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 4)), "string");
976  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 4)), "hello");
977 
980  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 5)), "list");
984  static_cast<size_t>(0));
985 
988  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 6)), "map");
992  static_cast<size_t>(0));
993 }
994 
995 TEST(FlStandardMessageCodecTest, DecodeMapNested) {
996  g_autoptr(FlValue) value = decode_message(
997  "0d02070a7374722d746f2d696e740d0407047a65726f030000000007036f6e6503010000"
998  "00070374776f0302000000070574687265650303000000070a696e742d746f2d7374720d"
999  "04030000000007047a65726f030100000007036f6e650302000000070374776f03030000"
1000  "0007057468726565");
1002  ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(2));
1003 
1007  "str-to-int");
1008  FlValue* str_to_int = fl_value_get_map_value(value, 0);
1009  ASSERT_EQ(fl_value_get_type(str_to_int), FL_VALUE_TYPE_MAP);
1010  ASSERT_EQ(fl_value_get_length(str_to_int), static_cast<size_t>(4));
1011 
1015  "int-to-str");
1016  FlValue* int_to_str = fl_value_get_map_value(value, 1);
1017  ASSERT_EQ(fl_value_get_type(int_to_str), FL_VALUE_TYPE_MAP);
1018  ASSERT_EQ(fl_value_get_length(int_to_str), static_cast<size_t>(4));
1019 
1020  const char* numbers[] = {"zero", "one", "two", "three", nullptr};
1021  for (int i = 0; numbers[i] != nullptr; i++) {
1022  ASSERT_EQ(fl_value_get_type(fl_value_get_map_key(str_to_int, i)),
1024  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(str_to_int, i)),
1025  numbers[i]);
1026 
1027  ASSERT_EQ(fl_value_get_type(fl_value_get_map_value(str_to_int, i)),
1029  EXPECT_EQ(fl_value_get_int(fl_value_get_map_value(str_to_int, i)), i);
1030 
1031  ASSERT_EQ(fl_value_get_type(fl_value_get_map_key(int_to_str, i)),
1033  EXPECT_EQ(fl_value_get_int(fl_value_get_map_key(int_to_str, i)), i);
1034 
1035  ASSERT_EQ(fl_value_get_type(fl_value_get_map_value(int_to_str, i)),
1037  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(int_to_str, i)),
1038  numbers[i]);
1039  }
1040 }
1041 
1042 TEST(FlStandardMessageCodecTest, DecodeMapNoData) {
1045 }
1046 
1047 TEST(FlStandardMessageCodecTest, DecodeMapLengthNoData) {
1050 }
1051 
1052 TEST(FlStandardMessageCodecTest, DecodeMapShortData1) {
1055 }
1056 
1057 TEST(FlStandardMessageCodecTest, DecodeMapShortData2) {
1059  "0d0707046e756c6c000704626f6f6c010703696e74032a0000000705666c6f6174060000"
1060  "00000000182d4454fb2109400706737472696e67070568656c6c6f07046c6973740c0007"
1061  "036d61700d",
1063 }
1064 
1065 TEST(FlStandardMessageCodecTest, EncodeDecodeLargeMap) {
1066  g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
1067 
1068  g_autoptr(FlValue) value = fl_value_new_map();
1069  for (int i = 0; i < 512; i++) {
1070  g_autofree gchar* key = g_strdup_printf("key%d", i);
1072  }
1073 
1074  g_autoptr(GError) error = nullptr;
1075  g_autoptr(GBytes) message =
1076  fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), value, &error);
1077  EXPECT_NE(message, nullptr);
1078  EXPECT_EQ(error, nullptr);
1079 
1080  g_autoptr(FlValue) decoded_value =
1081  fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), message, &error);
1082  EXPECT_EQ(error, nullptr);
1083  EXPECT_NE(value, nullptr);
1084 
1085  ASSERT_TRUE(fl_value_equal(value, decoded_value));
1086 }
1087 
1088 TEST(FlStandardMessageCodecTest, DecodeUnknownType) {
1091 }
1092 
1093 G_DECLARE_FINAL_TYPE(FlTestStandardMessageCodec,
1094  fl_test_standard_message_codec,
1095  FL,
1096  TEST_STANDARD_MESSAGE_CODEC,
1097  FlStandardMessageCodec)
1098 
1099 struct _FlTestStandardMessageCodec {
1100  FlStandardMessageCodec parent_instance;
1101 };
1102 
1103 G_DEFINE_TYPE(FlTestStandardMessageCodec,
1104  fl_test_standard_message_codec,
1105  fl_standard_message_codec_get_type())
1106 
1107 static gboolean write_custom_value1(FlStandardMessageCodec* codec,
1108  GByteArray* buffer,
1110  GError** error) {
1111  const gchar* text =
1112  static_cast<const gchar*>(fl_value_get_custom_value(value));
1113  size_t length = strlen(text);
1114 
1115  uint8_t type = 128;
1116  g_byte_array_append(buffer, &type, sizeof(uint8_t));
1118  g_byte_array_append(buffer, reinterpret_cast<const uint8_t*>(text), length);
1119  return TRUE;
1120 }
1121 
1122 static gboolean write_custom_value2(FlStandardMessageCodec* codec,
1123  GByteArray* buffer,
1124  FlValue* value,
1125  GError** error) {
1126  uint8_t type = 129;
1127  g_byte_array_append(buffer, &type, sizeof(uint8_t));
1128  return TRUE;
1129 }
1130 
1132  FlStandardMessageCodec* codec,
1133  GByteArray* buffer,
1134  FlValue* value,
1135  GError** error) {
1137  fl_value_get_custom_type(value) == 128) {
1138  return write_custom_value1(codec, buffer, value, error);
1139  } else if (fl_value_get_type(value) == FL_VALUE_TYPE_CUSTOM &&
1140  fl_value_get_custom_type(value) == 129) {
1141  return write_custom_value2(codec, buffer, value, error);
1142  } else {
1143  return FL_STANDARD_MESSAGE_CODEC_CLASS(
1144  fl_test_standard_message_codec_parent_class)
1145  ->write_value(codec, buffer, value, error);
1146  }
1147 }
1148 
1149 static FlValue* read_custom_value1(FlStandardMessageCodec* codec,
1150  GBytes* buffer,
1151  size_t* offset,
1152  GError** error) {
1153  uint32_t length;
1154  if (!fl_standard_message_codec_read_size(codec, buffer, offset, &length,
1155  error)) {
1156  return nullptr;
1157  }
1158  if (*offset + length > g_bytes_get_size(buffer)) {
1159  g_set_error(error, FL_MESSAGE_CODEC_ERROR,
1160  FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA, "Unexpected end of data");
1161  return nullptr;
1162  }
1164  128,
1165  g_strndup(static_cast<const gchar*>(g_bytes_get_data(buffer, nullptr)) +
1166  *offset,
1167  length),
1168  g_free);
1169  *offset += length;
1170 
1171  return value;
1172 }
1173 
1174 static FlValue* read_custom_value2(FlStandardMessageCodec* codec,
1175  GBytes* buffer,
1176  size_t* offset,
1177  GError** error) {
1178  return fl_value_new_custom(129, nullptr, nullptr);
1179 }
1180 
1182  FlStandardMessageCodec* codec,
1183  GBytes* buffer,
1184  size_t* offset,
1185  int type,
1186  GError** error) {
1187  if (type == 128) {
1188  return read_custom_value1(codec, buffer, offset, error);
1189  } else if (type == 129) {
1190  return read_custom_value2(codec, buffer, offset, error);
1191  } else {
1192  return FL_STANDARD_MESSAGE_CODEC_CLASS(
1193  fl_test_standard_message_codec_parent_class)
1194  ->read_value_of_type(codec, buffer, offset, type, error);
1195  }
1196 }
1197 
1199  FlTestStandardMessageCodecClass* klass) {
1200  FL_STANDARD_MESSAGE_CODEC_CLASS(klass)->write_value =
1202  FL_STANDARD_MESSAGE_CODEC_CLASS(klass)->read_value_of_type =
1204 }
1205 
1207  FlTestStandardMessageCodec* self) {
1208  // The following line suppresses a warning for unused function
1209  FL_IS_TEST_STANDARD_MESSAGE_CODEC(self);
1210 }
1211 
1212 static FlTestStandardMessageCodec* fl_test_standard_message_codec_new() {
1213  return FL_TEST_STANDARD_MESSAGE_CODEC(
1214  g_object_new(fl_test_standard_message_codec_get_type(), nullptr));
1215 }
1216 
1217 TEST(FlStandardMessageCodecTest, DecodeCustomType) {
1218  g_autoptr(FlTestStandardMessageCodec) codec =
1220  g_autoptr(FlValue) value =
1221  decode_message_with_codec("800568656c6c6f", FL_MESSAGE_CODEC(codec));
1223  ASSERT_EQ(fl_value_get_custom_type(value), 128);
1224  EXPECT_STREQ(static_cast<const gchar*>(fl_value_get_custom_value(value)),
1225  "hello");
1226 }
1227 
1228 TEST(FlStandardMessageCodecTest, DecodeCustomTypes) {
1229  g_autoptr(FlTestStandardMessageCodec) codec =
1231  g_autoptr(FlValue) value = decode_message_with_codec("0c02800568656c6c6f81",
1232  FL_MESSAGE_CODEC(codec));
1234  EXPECT_EQ(fl_value_get_length(value), static_cast<size_t>(2));
1235  FlValue* value1 = fl_value_get_list_value(value, 0);
1236  ASSERT_EQ(fl_value_get_type(value1), FL_VALUE_TYPE_CUSTOM);
1237  ASSERT_EQ(fl_value_get_custom_type(value1), 128);
1238  EXPECT_STREQ(static_cast<const gchar*>(fl_value_get_custom_value(value1)),
1239  "hello");
1240  FlValue* value2 = fl_value_get_list_value(value, 1);
1241  ASSERT_EQ(fl_value_get_type(value2), FL_VALUE_TYPE_CUSTOM);
1242  ASSERT_EQ(fl_value_get_custom_type(value2), 129);
1243 }
1244 
1245 TEST(FlStandardMessageCodecTest, EncodeCustomType) {
1246  g_autoptr(FlValue) value = fl_value_new_custom(128, "hello", nullptr);
1247  g_autoptr(FlTestStandardMessageCodec) codec =
1249  g_autofree gchar* hex_string =
1250  encode_message_with_codec(value, FL_MESSAGE_CODEC(codec));
1251  EXPECT_STREQ(hex_string, "800568656c6c6f");
1252 }
1253 
1254 TEST(FlStandardMessageCodecTest, EncodeCustomTypes) {
1255  g_autoptr(FlValue) value = fl_value_new_list();
1256  fl_value_append_take(value, fl_value_new_custom(128, "hello", nullptr));
1257  fl_value_append_take(value, fl_value_new_custom(129, nullptr, nullptr));
1258  g_autoptr(FlTestStandardMessageCodec) codec =
1260  g_autofree gchar* hex_string =
1261  encode_message_with_codec(value, FL_MESSAGE_CODEC(codec));
1262  EXPECT_STREQ(hex_string, "0c02800568656c6c6f81");
1263 }
1264 
1265 TEST(FlStandardMessageCodecTest, EncodeDecode) {
1266  g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
1267 
1268  g_autoptr(FlValue) input = fl_value_new_list();
1273  fl_value_append_take(input, fl_value_new_string("hello"));
1276 
1277  g_autoptr(GError) error = nullptr;
1278  g_autoptr(GBytes) message =
1279  fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), input, &error);
1280  EXPECT_NE(message, nullptr);
1281  EXPECT_EQ(error, nullptr);
1282 
1283  g_autoptr(FlValue) output =
1284  fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), message, &error);
1285  EXPECT_EQ(error, nullptr);
1286  EXPECT_NE(output, nullptr);
1287 
1288  ASSERT_TRUE(fl_value_equal(input, output));
1289 }
fl_test_standard_message_codec_write_value
static gboolean fl_test_standard_message_codec_write_value(FlStandardMessageCodec *codec, GByteArray *buffer, FlValue *value, GError **error)
Definition: fl_standard_message_codec_test.cc:1131
fl_value_get_int32_list
const G_MODULE_EXPORT int32_t * fl_value_get_int32_list(FlValue *self)
Definition: fl_value.cc:696
FL_VALUE_TYPE_UINT8_LIST
@ FL_VALUE_TYPE_UINT8_LIST
Definition: fl_value.h:70
fl_value_new_string_sized
G_MODULE_EXPORT FlValue * fl_value_new_string_sized(const gchar *value, size_t value_length)
Definition: fl_value.cc:283
FL_VALUE_TYPE_MAP
@ FL_VALUE_TYPE_MAP
Definition: fl_value.h:75
fl_standard_message_codec_new
G_MODULE_EXPORT FlStandardMessageCodec * fl_standard_message_codec_new()
Definition: fl_standard_message_codec.cc:637
fl_test_standard_message_codec_new
static FlTestStandardMessageCodec * fl_test_standard_message_codec_new()
Definition: fl_standard_message_codec_test.cc:1212
type
uint8_t type
Definition: fl_standard_message_codec_test.cc:1115
fl_value_set_string_take
G_MODULE_EXPORT void fl_value_set_string_take(FlValue *self, const gchar *key, FlValue *value)
Definition: fl_value.cc:650
fl_value_new_list
G_MODULE_EXPORT FlValue * fl_value_new_list()
Definition: fl_value.cc:349
fl_value_new_float32_list
G_MODULE_EXPORT FlValue * fl_value_new_float32_list(const float *data, size_t data_length)
Definition: fl_value.cc:329
i
int i
Definition: fl_socket_accessible.cc:18
fl_value_get_float_list
const G_MODULE_EXPORT double * fl_value_get_float_list(FlValue *self)
Definition: fl_value.cc:717
fl_value_new_bool
G_MODULE_EXPORT FlValue * fl_value_new_bool(bool value)
Definition: fl_value.cc:255
FlValue
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
fl_test_standard_message_codec_init
static void fl_test_standard_message_codec_init(FlTestStandardMessageCodec *self)
Definition: fl_standard_message_codec_test.cc:1206
encode_string
static gchar * encode_string(const gchar *value)
Definition: fl_standard_message_codec_test.cc:335
fl_value_set_take
G_MODULE_EXPORT void fl_value_set_take(FlValue *self, FlValue *key, FlValue *value)
Definition: fl_value.cc:618
fl_value_new_custom
G_MODULE_EXPORT FlValue * fl_value_new_custom(int type, gconstpointer value, GDestroyNotify destroy_notify)
Definition: fl_value.cc:374
fl_value_new_null
G_MODULE_EXPORT FlValue * fl_value_new_null()
Definition: fl_value.cc:251
FL_VALUE_TYPE_LIST
@ FL_VALUE_TYPE_LIST
Definition: fl_value.h:74
encode_int
static gchar * encode_int(int64_t value)
Definition: fl_standard_message_codec_test.cc:104
read_custom_value1
static FlValue * read_custom_value1(FlStandardMessageCodec *codec, GBytes *buffer, size_t *offset, GError **error)
Definition: fl_standard_message_codec_test.cc:1149
fl_value_get_bool
G_MODULE_EXPORT bool fl_value_get_bool(FlValue *self)
Definition: fl_value.cc:661
fl_value_get_uint8_list
const G_MODULE_EXPORT uint8_t * fl_value_get_uint8_list(FlValue *self)
Definition: fl_value.cc:689
fl_value_new_int
G_MODULE_EXPORT FlValue * fl_value_new_int(int64_t value)
Definition: fl_value.cc:262
fl_value_get_string
const G_MODULE_EXPORT gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:682
fl_value_get_float32_list
const G_MODULE_EXPORT float * fl_value_get_float32_list(FlValue *self)
Definition: fl_value.cc:710
FL_VALUE_TYPE_NULL
@ FL_VALUE_TYPE_NULL
Definition: fl_value.h:65
buffer
GByteArray * buffer
Definition: fl_standard_message_codec_test.cc:1108
fl_standard_message_codec_write_size
fl_standard_message_codec_write_size(codec, buffer, length)
fl_value_get_int
G_MODULE_EXPORT int64_t fl_value_get_int(FlValue *self)
Definition: fl_value.cc:668
fl_value_ref
G_MODULE_EXPORT FlValue * fl_value_ref(FlValue *self)
Definition: fl_value.cc:394
fl_message_codec_decode_message
G_MODULE_EXPORT FlValue * fl_message_codec_decode_message(FlMessageCodec *self, GBytes *message, GError **error)
Definition: fl_message_codec.cc:33
FL_VALUE_TYPE_CUSTOM
@ FL_VALUE_TYPE_CUSTOM
Definition: fl_value.h:77
TEST
TEST(FlStandardMessageCodecTest, EncodeNullptr)
Definition: fl_standard_message_codec_test.cc:65
g_byte_array_append
g_byte_array_append(buffer, &type, sizeof(uint8_t))
fl_value_new_map
G_MODULE_EXPORT FlValue * fl_value_new_map()
Definition: fl_value.cc:366
fl_value_get_type
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:466
fl_value_get_list_value
G_MODULE_EXPORT FlValue * fl_value_get_list_value(FlValue *self, size_t index)
Definition: fl_value.cc:776
fl_test_standard_message_codec_class_init
static void fl_test_standard_message_codec_class_init(FlTestStandardMessageCodecClass *klass)
Definition: fl_standard_message_codec_test.cc:1198
FL_VALUE_TYPE_FLOAT32_LIST
@ FL_VALUE_TYPE_FLOAT32_LIST
Definition: fl_value.h:76
fl_test_standard_message_codec_read_value_of_type
static FlValue * fl_test_standard_message_codec_read_value_of_type(FlStandardMessageCodec *codec, GBytes *buffer, size_t *offset, int type, GError **error)
Definition: fl_standard_message_codec_test.cc:1181
FL_VALUE_TYPE_STRING
@ FL_VALUE_TYPE_STRING
Definition: fl_value.h:69
read_custom_value2
static FlValue * read_custom_value2(FlStandardMessageCodec *codec, GBytes *buffer, size_t *offset, GError **error)
Definition: fl_standard_message_codec_test.cc:1174
fl_value_get_custom_value
G_MODULE_EXPORT gconstpointer fl_value_get_custom_value(FlValue *self)
Definition: fl_value.cc:830
fl_value_new_float_list
G_MODULE_EXPORT FlValue * fl_value_new_float_list(const double *data, size_t data_length)
Definition: fl_value.cc:339
fl_value_new_int32_list
G_MODULE_EXPORT FlValue * fl_value_new_int32_list(const int32_t *data, size_t data_length)
Definition: fl_value.cc:309
fl_value_get_float
G_MODULE_EXPORT double fl_value_get_float(FlValue *self)
Definition: fl_value.cc:675
fl_value_append_take
G_MODULE_EXPORT void fl_value_append_take(FlValue *self, FlValue *value)
Definition: fl_value.cc:600
fl_value_get_length
G_MODULE_EXPORT size_t fl_value_get_length(FlValue *self)
Definition: fl_value.cc:724
FL_VALUE_TYPE_INT64_LIST
@ FL_VALUE_TYPE_INT64_LIST
Definition: fl_value.h:72
FL
FL
Definition: fl_binary_messenger.cc:27
fl_value_equal
G_MODULE_EXPORT bool fl_value_equal(FlValue *a, FlValue *b)
Definition: fl_value.cc:471
fl_value_set_string
G_MODULE_EXPORT void fl_value_set_string(FlValue *self, const gchar *key, FlValue *value)
Definition: fl_value.cc:639
fl_standard_message_codec_read_size
G_MODULE_EXPORT gboolean fl_standard_message_codec_read_size(FlStandardMessageCodec *codec, GBytes *buffer, size_t *offset, uint32_t *value, GError **error)
Definition: fl_standard_message_codec.cc:657
FL_VALUE_TYPE_INT
@ FL_VALUE_TYPE_INT
Definition: fl_value.h:67
encode_message
static gchar * encode_message(FlValue *value)
Definition: fl_standard_message_codec_test.cc:26
value
GByteArray FlValue * value
Definition: fl_standard_message_codec_test.cc:1109
TRUE
return TRUE
Definition: fl_standard_message_codec_test.cc:1119
fl_value_new_int64_list
G_MODULE_EXPORT FlValue * fl_value_new_int64_list(const int64_t *data, size_t data_length)
Definition: fl_value.cc:319
encode_bool
static gchar * encode_bool(gboolean value)
Definition: fl_standard_message_codec_test.cc:89
FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA
@ FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA
Definition: fl_message_codec.h:35
write_custom_value2
static gboolean write_custom_value2(FlStandardMessageCodec *codec, GByteArray *buffer, FlValue *value, GError **error)
Definition: fl_standard_message_codec_test.cc:1122
fl_value_get_custom_type
G_MODULE_EXPORT int fl_value_get_custom_type(FlValue *self)
Definition: fl_value.cc:822
FL_VALUE_TYPE_FLOAT_LIST
@ FL_VALUE_TYPE_FLOAT_LIST
Definition: fl_value.h:73
fl_value_new_uint8_list
G_MODULE_EXPORT FlValue * fl_value_new_uint8_list(const uint8_t *data, size_t data_length)
Definition: fl_value.cc:292
fl_message_codec_encode_message
G_MODULE_EXPORT GBytes * fl_message_codec_encode_message(FlMessageCodec *self, FlValue *message, GError **error)
Definition: fl_message_codec.cc:17
fl_value_append
G_MODULE_EXPORT void fl_value_append(FlValue *self, FlValue *value)
Definition: fl_value.cc:592
FL_VALUE_TYPE_FLOAT
@ FL_VALUE_TYPE_FLOAT
Definition: fl_value.h:68
FL_VALUE_TYPE_INT32_LIST
@ FL_VALUE_TYPE_INT32_LIST
Definition: fl_value.h:71
FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE
@ FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE
Definition: fl_message_codec.h:37
fl_value_new_float
G_MODULE_EXPORT FlValue * fl_value_new_float(double value)
Definition: fl_value.cc:269
fl_standard_message_codec.h
fl_value_get_map_key
G_MODULE_EXPORT FlValue * fl_value_get_map_key(FlValue *self, size_t index)
Definition: fl_value.cc:784
G_DEFINE_TYPE
G_DEFINE_TYPE(FlTestStandardMessageCodec, fl_test_standard_message_codec, fl_standard_message_codec_get_type()) static gboolean write_custom_value1(FlStandardMessageCodec *codec
error
GByteArray FlValue GError ** error
Definition: fl_standard_message_codec_test.cc:1110
fl_value_get_int64_list
const G_MODULE_EXPORT int64_t * fl_value_get_int64_list(FlValue *self)
Definition: fl_value.cc:703
decode_error_value
static void decode_error_value(const char *hex_string, GQuark domain, gint code)
Definition: fl_standard_message_codec_test.cc:53
FL_VALUE_TYPE_BOOL
@ FL_VALUE_TYPE_BOOL
Definition: fl_value.h:66
decode_message_with_codec
static FlValue * decode_message_with_codec(const char *hex_string, FlMessageCodec *codec)
Definition: fl_standard_message_codec_test.cc:33
decode_message
static FlValue * decode_message(const char *hex_string)
Definition: fl_standard_message_codec_test.cc:46
encode_float
static gchar * encode_float(double value)
Definition: fl_standard_message_codec_test.cc:227
length
size_t length
Definition: fl_standard_message_codec_test.cc:1113
G_DECLARE_FINAL_TYPE
G_DECLARE_FINAL_TYPE(FlTestStandardMessageCodec, fl_test_standard_message_codec, FL, TEST_STANDARD_MESSAGE_CODEC, FlStandardMessageCodec) struct _FlTestStandardMessageCodec
Definition: fl_standard_message_codec_test.cc:1093
fl_value_get_map_value
G_MODULE_EXPORT FlValue * fl_value_get_map_value(FlValue *self, size_t index)
Definition: fl_value.cc:792
encode_message_with_codec
static gchar * encode_message_with_codec(FlValue *value, FlMessageCodec *codec)
Definition: fl_standard_message_codec_test.cc:14
fl_value_new_string
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:276
FL_MESSAGE_CODEC_ERROR
#define FL_MESSAGE_CODEC_ERROR
Definition: fl_message_codec.h:30