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, EncodeFloat32ListEmpty) {
530  g_autoptr(FlValue) value = fl_value_new_float32_list(nullptr, 0);
531  g_autofree gchar* hex_string = encode_message(value);
532  EXPECT_STREQ(hex_string, "0e000000");
533 }
534 
535 TEST(FlStandardMessageCodecTest, EncodeFloat32List) {
536  float data[] = {0.0f, -0.5f, 0.25f, -0.125f, 0.00625f};
537  g_autoptr(FlValue) value = fl_value_new_float32_list(data, 5);
538  g_autofree gchar* hex_string = encode_message(value);
539  EXPECT_STREQ(hex_string, "0e05000000000000000000bf0000803e000000becdcccc3b");
540 }
541 
542 TEST(FlStandardMessageCodecTest, DecodeFloat32ListEmpty) {
543  g_autoptr(FlValue) value = decode_message("0e000000");
545  EXPECT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
546 }
547 
548 TEST(FlStandardMessageCodecTest, DecodeFloat32List) {
549  g_autoptr(FlValue) value =
550  decode_message("0e05000000000000000000bf0000803e000000becdcccc3b");
552  const float* data = fl_value_get_float32_list(value);
553  EXPECT_FLOAT_EQ(data[0], 0.0f);
554  EXPECT_FLOAT_EQ(data[1], -0.5f);
555  EXPECT_FLOAT_EQ(data[2], 0.25f);
556  EXPECT_FLOAT_EQ(data[3], -0.125f);
557  EXPECT_FLOAT_EQ(data[4], 0.00625f);
558 }
559 
560 TEST(FlStandardMessageCodecTest, DecodeFloat32ListNoData) {
563 }
564 
565 TEST(FlStandardMessageCodecTest, DecodeFloat32ListLengthNoData) {
568 }
569 
570 TEST(FlStandardMessageCodecTest, DecodeFloat32ListShortData1) {
573 }
574 
575 TEST(FlStandardMessageCodecTest, DecodeFloat32ListShortData2) {
576  decode_error_value("0e05000000000000000000bf0000803e000000becdcccc",
579 }
580 
581 TEST(FlStandardMessageCodecTest, EncodeFloatListEmpty) {
582  g_autoptr(FlValue) value = fl_value_new_float_list(nullptr, 0);
583  g_autofree gchar* hex_string = encode_message(value);
584  EXPECT_STREQ(hex_string, "0b00000000000000");
585 }
586 
587 TEST(FlStandardMessageCodecTest, EncodeFloatList) {
588  double data[] = {0, -0.5, 0.25, -0.125, 0.00625};
589  g_autoptr(FlValue) value = fl_value_new_float_list(data, 5);
590  g_autofree gchar* hex_string = encode_message(value);
591  EXPECT_STREQ(
592  hex_string,
593  "0b050000000000000000000000000000000000000000e0bf000000000000d03f00000000"
594  "0000c0bf9a9999999999793f");
595 }
596 
597 TEST(FlStandardMessageCodecTest, DecodeFloatListEmpty) {
598  g_autoptr(FlValue) value = decode_message("0b00000000000000");
600  EXPECT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
601 }
602 
603 TEST(FlStandardMessageCodecTest, DecodeFloatList) {
604  g_autoptr(FlValue) value = decode_message(
605  "0b050000000000000000000000000000000000000000e0bf000000000000d03f00000000"
606  "0000c0bf9a9999999999793f");
608  const double* data = fl_value_get_float_list(value);
609  EXPECT_FLOAT_EQ(data[0], 0.0);
610  EXPECT_FLOAT_EQ(data[1], -0.5);
611  EXPECT_FLOAT_EQ(data[2], 0.25);
612  EXPECT_FLOAT_EQ(data[3], -0.125);
613  EXPECT_FLOAT_EQ(data[4], 0.00625);
614 }
615 
616 TEST(FlStandardMessageCodecTest, DecodeFloatListNoData) {
619 }
620 
621 TEST(FlStandardMessageCodecTest, DecodeFloatListLengthNoData) {
622  decode_error_value("0b05000000000000", FL_MESSAGE_CODEC_ERROR,
624 }
625 
626 TEST(FlStandardMessageCodecTest, DecodeFloatListShortData1) {
627  decode_error_value("0b0500000000000000", FL_MESSAGE_CODEC_ERROR,
629 }
630 
631 TEST(FlStandardMessageCodecTest, DecodeFloatListShortData2) {
633  "0b050000000000000000000000000000000000000000e0bf000000000000d03f00000000"
634  "0000c0bf9a99"
635  "9999999979",
637 }
638 
639 TEST(FlStandardMessageCodecTest, EncodeListEmpty) {
640  g_autoptr(FlValue) value = fl_value_new_list();
641  g_autofree gchar* hex_string = encode_message(value);
642  EXPECT_STREQ(hex_string, "0c00");
643 }
644 
645 TEST(FlStandardMessageCodecTest, EncodeListTypes) {
646  g_autoptr(FlValue) value = fl_value_new_list();
654  g_autofree gchar* hex_string = encode_message(value);
655  EXPECT_STREQ(
656  hex_string,
657  "0c070001032a00000006000000000000182d4454fb210940070568656c6c6f0c000d00");
658 }
659 
660 TEST(FlStandardMessageCodecTest, EncodeListNested) {
661  g_autoptr(FlValue) even_numbers = fl_value_new_list();
662  g_autoptr(FlValue) odd_numbers = fl_value_new_list();
663  for (int i = 0; i < 10; i++) {
664  if (i % 2 == 0)
665  fl_value_append_take(even_numbers, fl_value_new_int(i));
666  else
667  fl_value_append_take(odd_numbers, fl_value_new_int(i));
668  }
669  g_autoptr(FlValue) value = fl_value_new_list();
670  fl_value_append(value, even_numbers);
671  fl_value_append(value, odd_numbers);
672  g_autofree gchar* hex_string = encode_message(value);
673  EXPECT_STREQ(hex_string,
674  "0c020c05030000000003020000000304000000030600000003080000000c"
675  "0503010000000303000000030500000003070000000309000000");
676 }
677 
678 TEST(FlStandardMessageCodecTest, DecodeListEmpty) {
679  g_autoptr(FlValue) value = decode_message("0c00");
681  EXPECT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
682 }
683 
684 TEST(FlStandardMessageCodecTest, DecodeListTypes) {
685  g_autoptr(FlValue) value = decode_message(
686  "0c070001032a00000006000000000000182d4454fb210940070568656c6c6f0c000d00");
688  ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(7));
696  EXPECT_EQ(fl_value_get_int(fl_value_get_list_value(value, 2)), 42);
699  EXPECT_FLOAT_EQ(fl_value_get_float(fl_value_get_list_value(value, 3)), M_PI);
702  EXPECT_STREQ(fl_value_get_string(fl_value_get_list_value(value, 4)), "hello");
706  static_cast<size_t>(0));
710  static_cast<size_t>(0));
711 }
712 
713 TEST(FlStandardMessageCodecTest, DecodeListNested) {
714  g_autoptr(FlValue) value = decode_message(
715  "0c020c05030000000003020000000304000000030600000003080000000c"
716  "0503010000000303000000030500000003070000000309000000");
718  ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(2));
719  FlValue* even_list = fl_value_get_list_value(value, 0);
720  ASSERT_EQ(fl_value_get_type(even_list), FL_VALUE_TYPE_LIST);
721  ASSERT_EQ(fl_value_get_length(even_list), static_cast<size_t>(5));
722  FlValue* odd_list = fl_value_get_list_value(value, 1);
723  ASSERT_EQ(fl_value_get_type(odd_list), FL_VALUE_TYPE_LIST);
724  ASSERT_EQ(fl_value_get_length(odd_list), static_cast<size_t>(5));
725  for (int i = 0; i < 5; i++) {
726  FlValue* v = fl_value_get_list_value(even_list, i);
727  ASSERT_EQ(fl_value_get_type(v), FL_VALUE_TYPE_INT);
728  EXPECT_EQ(fl_value_get_int(v), i * 2);
729 
730  v = fl_value_get_list_value(odd_list, i);
731  ASSERT_EQ(fl_value_get_type(v), FL_VALUE_TYPE_INT);
732  EXPECT_EQ(fl_value_get_int(v), i * 2 + 1);
733  }
734 }
735 
736 TEST(FlStandardMessageCodecTest, DecodeListNoData) {
739 }
740 
741 TEST(FlStandardMessageCodecTest, DecodeListLengthNoData) {
744 }
745 
746 TEST(FlStandardMessageCodecTest, DecodeListShortData1) {
749 }
750 
751 TEST(FlStandardMessageCodecTest, DecodeListShortData2) {
753  "0c070001032a00000006000000000000182d4454fb210940070568656c6c6f0c000d",
755 }
756 
757 TEST(FlStandardMessageCodecTest, EncodeDecodeLargeList) {
758  g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
759 
760  g_autoptr(FlValue) value = fl_value_new_list();
761  for (int i = 0; i < 65535; i++)
763 
764  g_autoptr(GError) error = nullptr;
765  g_autoptr(GBytes) message =
766  fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), value, &error);
767  EXPECT_NE(message, nullptr);
768  EXPECT_EQ(error, nullptr);
769 
770  g_autoptr(FlValue) decoded_value =
771  fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), message, &error);
772  EXPECT_EQ(error, nullptr);
773  EXPECT_NE(value, nullptr);
774 
775  ASSERT_TRUE(fl_value_equal(value, decoded_value));
776 }
777 
778 TEST(FlStandardMessageCodecTest, EncodeMapEmpty) {
779  g_autoptr(FlValue) value = fl_value_new_map();
780  g_autofree gchar* hex_string = encode_message(value);
781  EXPECT_STREQ(hex_string, "0d00");
782 }
783 
784 TEST(FlStandardMessageCodecTest, EncodeMapKeyTypes) {
785  g_autoptr(FlValue) value = fl_value_new_map();
788  fl_value_new_string("bool"));
791  fl_value_new_string("float"));
793  fl_value_new_string("string"));
796  g_autofree gchar* hex_string = encode_message(value);
797  EXPECT_STREQ(hex_string,
798  "0d070007046e756c6c010704626f6f6c032a0000000703696e7406000000000"
799  "0182d4454fb2109400705666c6f6174070568656c6c6f0706737472696e670c"
800  "0007046c6973740d0007036d6170");
801 }
802 
803 TEST(FlStandardMessageCodecTest, EncodeMapValueTypes) {
804  g_autoptr(FlValue) value = fl_value_new_map();
810  fl_value_new_float(M_PI));
812  fl_value_new_string("hello"));
815  g_autofree gchar* hex_string = encode_message(value);
816  EXPECT_STREQ(hex_string,
817  "0d0707046e756c6c000704626f6f6c010703696e74032a0000000705666c6f6"
818  "17406000000000000182d4454fb2109400706737472696e67070568656c6c6f"
819  "07046c6973740c0007036d61700d00");
820 }
821 
822 TEST(FlStandardMessageCodecTest, EncodeMapNested) {
823  g_autoptr(FlValue) str_to_int = fl_value_new_map();
824  g_autoptr(FlValue) int_to_str = fl_value_new_map();
825  const char* numbers[] = {"zero", "one", "two", "three", nullptr};
826  for (int i = 0; numbers[i] != nullptr; i++) {
827  fl_value_set_take(str_to_int, fl_value_new_string(numbers[i]),
828  fl_value_new_int(i));
829  fl_value_set_take(int_to_str, fl_value_new_int(i),
830  fl_value_new_string(numbers[i]));
831  }
832  g_autoptr(FlValue) value = fl_value_new_map();
833  fl_value_set_string(value, "str-to-int", str_to_int);
834  fl_value_set_string(value, "int-to-str", int_to_str);
835  g_autofree gchar* hex_string = encode_message(value);
836  EXPECT_STREQ(hex_string,
837  "0d02070a7374722d746f2d696e740d0407047a65726f030000000007036f6e6"
838  "50301000000070374776f0302000000070574687265650303000000070a696e"
839  "742d746f2d7374720d04030000000007047a65726f030100000007036f6e650"
840  "302000000070374776f030300000007057468726565");
841 }
842 
843 TEST(FlStandardMessageCodecTest, DecodeMapEmpty) {
844  g_autoptr(FlValue) value = decode_message("0d00");
846  ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
847 }
848 
849 TEST(FlStandardMessageCodecTest, DecodeMapKeyTypes) {
850  g_autoptr(FlValue) value = decode_message(
851  "0d070007046e756c6c010704626f6f6c032a0000000703696e74060000000000182d4454"
852  "fb2109400705666c6f6174070568656c6c6f0706737472696e670c0007046c6973740d00"
853  "07036d6170");
855  ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(7));
856 
861  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 0)), "null");
862 
865  EXPECT_TRUE(fl_value_get_bool(fl_value_get_map_key(value, 1)));
868  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 1)), "bool");
869 
872  EXPECT_EQ(fl_value_get_int(fl_value_get_map_key(value, 2)), 42);
875  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 2)), "int");
876 
879  EXPECT_FLOAT_EQ(fl_value_get_float(fl_value_get_map_key(value, 3)), M_PI);
882  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 3)), "float");
883 
886  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 4)), "hello");
889  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 4)), "string");
890 
894  static_cast<size_t>(0));
897  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 5)), "list");
898 
902  static_cast<size_t>(0));
905  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 6)), "map");
906 }
907 
908 TEST(FlStandardMessageCodecTest, DecodeMapValueTypes) {
909  g_autoptr(FlValue) value = decode_message(
910  "0d0707046e756c6c000704626f6f6c010703696e74032a0000000705666c6f6174060000"
911  "00000000182d4454fb2109400706737472696e67070568656c6c6f07046c6973740c0007"
912  "036d61700d00");
914  ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(7));
915 
918  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 0)), "null");
921 
924  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 1)), "bool");
928 
931  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 2)), "int");
934  EXPECT_EQ(fl_value_get_int(fl_value_get_map_value(value, 2)), 42);
935 
938  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 3)), "float");
941  EXPECT_FLOAT_EQ(fl_value_get_float(fl_value_get_map_value(value, 3)), M_PI);
942 
945  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 4)), "string");
948  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 4)), "hello");
949 
952  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 5)), "list");
956  static_cast<size_t>(0));
957 
960  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 6)), "map");
964  static_cast<size_t>(0));
965 }
966 
967 TEST(FlStandardMessageCodecTest, DecodeMapNested) {
968  g_autoptr(FlValue) value = decode_message(
969  "0d02070a7374722d746f2d696e740d0407047a65726f030000000007036f6e6503010000"
970  "00070374776f0302000000070574687265650303000000070a696e742d746f2d7374720d"
971  "04030000000007047a65726f030100000007036f6e650302000000070374776f03030000"
972  "0007057468726565");
974  ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(2));
975 
979  "str-to-int");
980  FlValue* str_to_int = fl_value_get_map_value(value, 0);
981  ASSERT_EQ(fl_value_get_type(str_to_int), FL_VALUE_TYPE_MAP);
982  ASSERT_EQ(fl_value_get_length(str_to_int), static_cast<size_t>(4));
983 
987  "int-to-str");
988  FlValue* int_to_str = fl_value_get_map_value(value, 1);
989  ASSERT_EQ(fl_value_get_type(int_to_str), FL_VALUE_TYPE_MAP);
990  ASSERT_EQ(fl_value_get_length(int_to_str), static_cast<size_t>(4));
991 
992  const char* numbers[] = {"zero", "one", "two", "three", nullptr};
993  for (int i = 0; numbers[i] != nullptr; i++) {
994  ASSERT_EQ(fl_value_get_type(fl_value_get_map_key(str_to_int, i)),
996  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(str_to_int, i)),
997  numbers[i]);
998 
999  ASSERT_EQ(fl_value_get_type(fl_value_get_map_value(str_to_int, i)),
1001  EXPECT_EQ(fl_value_get_int(fl_value_get_map_value(str_to_int, i)), i);
1002 
1003  ASSERT_EQ(fl_value_get_type(fl_value_get_map_key(int_to_str, i)),
1005  EXPECT_EQ(fl_value_get_int(fl_value_get_map_key(int_to_str, i)), i);
1006 
1007  ASSERT_EQ(fl_value_get_type(fl_value_get_map_value(int_to_str, i)),
1009  EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(int_to_str, i)),
1010  numbers[i]);
1011  }
1012 }
1013 
1014 TEST(FlStandardMessageCodecTest, DecodeMapNoData) {
1017 }
1018 
1019 TEST(FlStandardMessageCodecTest, DecodeMapLengthNoData) {
1022 }
1023 
1024 TEST(FlStandardMessageCodecTest, DecodeMapShortData1) {
1027 }
1028 
1029 TEST(FlStandardMessageCodecTest, DecodeMapShortData2) {
1031  "0d0707046e756c6c000704626f6f6c010703696e74032a0000000705666c6f6174060000"
1032  "00000000182d4454fb2109400706737472696e67070568656c6c6f07046c6973740c0007"
1033  "036d61700d",
1035 }
1036 
1037 TEST(FlStandardMessageCodecTest, EncodeDecodeLargeMap) {
1038  g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
1039 
1040  g_autoptr(FlValue) value = fl_value_new_map();
1041  for (int i = 0; i < 512; i++) {
1042  g_autofree gchar* key = g_strdup_printf("key%d", i);
1044  }
1045 
1046  g_autoptr(GError) error = nullptr;
1047  g_autoptr(GBytes) message =
1048  fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), value, &error);
1049  EXPECT_NE(message, nullptr);
1050  EXPECT_EQ(error, nullptr);
1051 
1052  g_autoptr(FlValue) decoded_value =
1053  fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), message, &error);
1054  EXPECT_EQ(error, nullptr);
1055  EXPECT_NE(value, nullptr);
1056 
1057  ASSERT_TRUE(fl_value_equal(value, decoded_value));
1058 }
1059 
1060 TEST(FlStandardMessageCodecTest, DecodeUnknownType) {
1063 }
1064 
1065 TEST(FlStandardMessageCodecTest, EncodeDecode) {
1066  g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
1067 
1068  g_autoptr(FlValue) input = fl_value_new_list();
1073  fl_value_append_take(input, fl_value_new_string("hello"));
1076 
1077  g_autoptr(GError) error = nullptr;
1078  g_autoptr(GBytes) message =
1079  fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), input, &error);
1080  EXPECT_NE(message, nullptr);
1081  EXPECT_EQ(error, nullptr);
1082 
1083  g_autoptr(FlValue) output =
1084  fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), message, &error);
1085  EXPECT_EQ(error, nullptr);
1086  EXPECT_NE(output, nullptr);
1087 
1088  ASSERT_TRUE(fl_value_equal(input, output));
1089 }
G_MODULE_EXPORT FlValue * fl_value_new_list()
Definition: fl_value.cc:338
G_MODULE_EXPORT FlValue * fl_value_new_float(double value)
Definition: fl_value.cc:258
G_MODULE_EXPORT const float * fl_value_get_float32_list(FlValue *self)
Definition: fl_value.cc:670
const uint8_t uint32_t uint32_t GError ** error
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:428
G_MODULE_EXPORT FlValue * fl_value_new_uint8_list(const uint8_t *data, size_t data_length)
Definition: fl_value.cc:281
G_MODULE_EXPORT FlValue * fl_value_new_int64_list(const int64_t *data, size_t data_length)
Definition: fl_value.cc:308
static FlValue * decode_message(const char *hex_string)
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:40
static gchar * encode_message(FlValue *value)
G_MODULE_EXPORT FlValue * fl_value_new_map()
Definition: fl_value.cc:355
G_MODULE_EXPORT FlValue * fl_value_new_int32_list(const int32_t *data, size_t data_length)
Definition: fl_value.cc:298
G_MODULE_EXPORT const double * fl_value_get_float_list(FlValue *self)
Definition: fl_value.cc:677
G_MODULE_EXPORT int64_t fl_value_get_int(FlValue *self)
Definition: fl_value.cc:628
gchar * bytes_to_hex_string(GBytes *bytes)
Definition: fl_test.cc:40
#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:663
G_MODULE_EXPORT FlValue * fl_value_new_bool(bool value)
Definition: fl_value.cc:244
static gchar * encode_bool(gboolean value)
G_MODULE_EXPORT const uint8_t * fl_value_get_uint8_list(FlValue *self)
Definition: fl_value.cc:649
G_MODULE_EXPORT bool fl_value_equal(FlValue *a, FlValue *b)
Definition: fl_value.cc:433
G_MODULE_EXPORT void fl_value_append(FlValue *self, FlValue *value)
Definition: fl_value.cc:552
static gchar * encode_string(const gchar *value)
G_MODULE_EXPORT double fl_value_get_float(FlValue *self)
Definition: fl_value.cc:635
G_MODULE_EXPORT void fl_value_append_take(FlValue *self, FlValue *value)
Definition: fl_value.cc:560
TEST(FlStandardMessageCodecTest, EncodeNullptr)
uint8_t value
G_MODULE_EXPORT void fl_value_set_take(FlValue *self, FlValue *key, FlValue *value)
Definition: fl_value.cc:578
G_MODULE_EXPORT FlValue * fl_value_new_float32_list(const float *data, size_t data_length)
Definition: fl_value.cc:318
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:735
G_MODULE_EXPORT FlValue * fl_value_new_int(int64_t value)
Definition: fl_value.cc:251
G_MODULE_EXPORT FlValue * fl_value_get_map_value(FlValue *self, size_t index)
Definition: fl_value.cc:751
GBytes * hex_string_to_bytes(const gchar *hex_string)
Definition: fl_test.cc:31
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:265
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:363
static gchar * encode_float(double value)
G_MODULE_EXPORT FlValue * fl_value_get_map_key(FlValue *self, size_t index)
Definition: fl_value.cc:743
G_MODULE_EXPORT const int32_t * fl_value_get_int32_list(FlValue *self)
Definition: fl_value.cc:656
G_MODULE_EXPORT GBytes * fl_message_codec_encode_message(FlMessageCodec *self, FlValue *message, GError **error)
G_MODULE_EXPORT const gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:642
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:599
G_MODULE_EXPORT void fl_value_set_string_take(FlValue *self, const gchar *key, FlValue *value)
Definition: fl_value.cc:610
G_MODULE_EXPORT FlValue * fl_value_new_float_list(const double *data, size_t data_length)
Definition: fl_value.cc:328
return FALSE
G_MODULE_EXPORT FlValue * fl_value_new_string_sized(const gchar *value, size_t value_length)
Definition: fl_value.cc:272
G_MODULE_EXPORT size_t fl_value_get_length(FlValue *self)
Definition: fl_value.cc:684
G_MODULE_EXPORT FlValue * fl_value_new_null()
Definition: fl_value.cc:240
G_MODULE_EXPORT bool fl_value_get_bool(FlValue *self)
Definition: fl_value.cc:621