| 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
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 TEST(FlStandardMessageCodecTest, DecodeNull) {
64 // Regression test for https://github.com/flutter/flutter/issues/128704.
65
66 g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
67 g_autoptr(GBytes) data = g_bytes_new(nullptr, 0);
68 g_autoptr(GError) error = nullptr;
69 g_autoptr(FlValue) value =
70 fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), data, &error);
71
72 EXPECT_FALSE(value == nullptr);
73 EXPECT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_NULL);
74 }
75
76 static gchar* encode_bool(gboolean value) {
77 g_autoptr(FlValue) v = fl_value_new_bool(value);
78 return encode_message(v);
79 }
80
81 TEST(FlStandardMessageCodecTest, EncodeBoolFalse) {
82 g_autofree gchar* hex_string = encode_bool(FALSE);
83 EXPECT_STREQ(hex_string, "02");
84 }
85
86 TEST(FlStandardMessageCodecTest, EncodeBoolTrue) {
87 g_autofree gchar* hex_string = encode_bool(TRUE);
88 EXPECT_STREQ(hex_string, "01");
89 }
90
91 static gchar* encode_int(int64_t value) {
92 g_autoptr(FlValue) v = fl_value_new_int(value);
93 return encode_message(v);
94 }
95
96 TEST(FlStandardMessageCodecTest, EncodeIntZero) {
97 g_autofree gchar* hex_string = encode_int(0);
98 EXPECT_STREQ(hex_string, "0300000000");
99 }
100
101 TEST(FlStandardMessageCodecTest, EncodeIntOne) {
102 g_autofree gchar* hex_string = encode_int(1);
103 EXPECT_STREQ(hex_string, "0301000000");
104 }
105
106 TEST(FlStandardMessageCodecTest, EncodeInt32) {
107 g_autofree gchar* hex_string = encode_int(0x01234567);
108 EXPECT_STREQ(hex_string, "0367452301");
109 }
110
111 TEST(FlStandardMessageCodecTest, EncodeInt32Min) {
112 g_autofree gchar* hex_string = encode_int(G_MININT32);
113 EXPECT_STREQ(hex_string, "0300000080");
114 }
115
116 TEST(FlStandardMessageCodecTest, EncodeInt32Max) {
117 g_autofree gchar* hex_string = encode_int(G_MAXINT32);
118 EXPECT_STREQ(hex_string, "03ffffff7f");
119 }
120
121 TEST(FlStandardMessageCodecTest, EncodeInt64) {
122 g_autofree gchar* hex_string = encode_int(0x0123456789abcdef);
123 EXPECT_STREQ(hex_string, "04efcdab8967452301");
124 }
125
126 TEST(FlStandardMessageCodecTest, EncodeInt64Min) {
127 g_autofree gchar* hex_string = encode_int(G_MININT64);
128 EXPECT_STREQ(hex_string, "040000000000000080");
129 }
130
131 TEST(FlStandardMessageCodecTest, EncodeInt64Max) {
132 g_autofree gchar* hex_string = encode_int(G_MAXINT64);
133 EXPECT_STREQ(hex_string, "04ffffffffffffff7f");
134 }
135
136 TEST(FlStandardMessageCodecTest, DecodeIntZero) {
137 g_autoptr(FlValue) value = decode_message("0300000000");
138 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_INT);
139 EXPECT_EQ(fl_value_get_int(value), 0);
140 }
141
142 TEST(FlStandardMessageCodecTest, DecodeIntOne) {
143 g_autoptr(FlValue) value = decode_message("0301000000");
144 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_INT);
145 EXPECT_EQ(fl_value_get_int(value), 1);
146 }
147
148 TEST(FlStandardMessageCodecTest, DecodeInt32) {
149 g_autoptr(FlValue) value = decode_message("0367452301");
150 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_INT);
151 EXPECT_EQ(fl_value_get_int(value), 0x01234567);
152 }
153
154 TEST(FlStandardMessageCodecTest, DecodeInt32Min) {
155 g_autoptr(FlValue) value = decode_message("0300000080");
156 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_INT);
157 EXPECT_EQ(fl_value_get_int(value), G_MININT32);
158 }
159
160 TEST(FlStandardMessageCodecTest, DecodeInt32Max) {
161 g_autoptr(FlValue) value = decode_message("03ffffff7f");
162 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_INT);
163 EXPECT_EQ(fl_value_get_int(value), G_MAXINT32);
164 }
165
166 TEST(FlStandardMessageCodecTest, DecodeInt64) {
167 g_autoptr(FlValue) value = decode_message("04efcdab8967452301");
168 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_INT);
169 EXPECT_EQ(fl_value_get_int(value), 0x0123456789abcdef);
170 }
171
172 TEST(FlStandardMessageCodecTest, DecodeInt64Min) {
173 g_autoptr(FlValue) value = decode_message("040000000000000080");
174 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_INT);
175 EXPECT_EQ(fl_value_get_int(value), G_MININT64);
176 }
177
178 TEST(FlStandardMessageCodecTest, DecodeInt64Max) {
179 g_autoptr(FlValue) value = decode_message("04ffffffffffffff7f");
180 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_INT);
181 EXPECT_EQ(fl_value_get_int(value), G_MAXINT64);
182 }
183
184 TEST(FlStandardMessageCodecTest, DecodeInt32NoData) {
185 decode_error_value("03", FL_MESSAGE_CODEC_ERROR,
186 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
187 }
188
189 TEST(FlStandardMessageCodecTest, DecodeIntShortData1) {
190 decode_error_value("0367", FL_MESSAGE_CODEC_ERROR,
191 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
192 }
193
194 TEST(FlStandardMessageCodecTest, DecodeIntShortData2) {
195 decode_error_value("03674523", FL_MESSAGE_CODEC_ERROR,
196 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
197 }
198
199 TEST(FlStandardMessageCodecTest, DecodeInt64NoData) {
200 decode_error_value("04", FL_MESSAGE_CODEC_ERROR,
201 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
202 }
203
204 TEST(FlStandardMessageCodecTest, DecodeInt64ShortData1) {
205 decode_error_value("04ef", FL_MESSAGE_CODEC_ERROR,
206 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
207 }
208
209 TEST(FlStandardMessageCodecTest, DecodeInt64ShortData2) {
210 decode_error_value("04efcdab89674523", FL_MESSAGE_CODEC_ERROR,
211 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
212 }
213
214 static gchar* encode_float(double value) {
215 g_autoptr(FlValue) v = fl_value_new_float(value);
216 return encode_message(v);
217 }
218
219 TEST(FlStandardMessageCodecTest, EncodeFloatZero) {
220 g_autofree gchar* hex_string = encode_float(0);
221 EXPECT_STREQ(hex_string, "06000000000000000000000000000000");
222 }
223
224 TEST(FlStandardMessageCodecTest, EncodeFloatOne) {
225 g_autofree gchar* hex_string = encode_float(1);
226 EXPECT_STREQ(hex_string, "0600000000000000000000000000f03f");
227 }
228
229 TEST(FlStandardMessageCodecTest, EncodeFloatMinusOne) {
230 g_autofree gchar* hex_string = encode_float(-1);
231 EXPECT_STREQ(hex_string, "0600000000000000000000000000f0bf");
232 }
233
234 TEST(FlStandardMessageCodecTest, EncodeFloatHalf) {
235 g_autofree gchar* hex_string = encode_float(0.5);
236 EXPECT_STREQ(hex_string, "0600000000000000000000000000e03f");
237 }
238
239 TEST(FlStandardMessageCodecTest, EncodeFloatFraction) {
240 g_autofree gchar* hex_string = encode_float(M_PI);
241 EXPECT_STREQ(hex_string, "0600000000000000182d4454fb210940");
242 }
243
244 TEST(FlStandardMessageCodecTest, EncodeFloatMinusZero) {
245 g_autofree gchar* hex_string = encode_float(-0.0);
246 EXPECT_STREQ(hex_string, "06000000000000000000000000000080");
247 }
248
249 TEST(FlStandardMessageCodecTest, EncodeFloatNaN) {
250 g_autofree gchar* hex_string = encode_float(NAN);
251 EXPECT_STREQ(hex_string, "0600000000000000000000000000f87f");
252 }
253
254 TEST(FlStandardMessageCodecTest, EncodeFloatInfinity) {
255 g_autofree gchar* hex_string = encode_float(INFINITY);
256 EXPECT_STREQ(hex_string, "0600000000000000000000000000f07f");
257 }
258
259 TEST(FlStandardMessageCodecTest, DecodeFloatZero) {
260 g_autoptr(FlValue) value = decode_message("06000000000000000000000000000000");
261 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_FLOAT);
262 EXPECT_EQ(fl_value_get_float(value), 0.0);
263 }
264
265 TEST(FlStandardMessageCodecTest, DecodeFloatOne) {
266 g_autoptr(FlValue) value = decode_message("0600000000000000000000000000f03f");
267 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_FLOAT);
268 EXPECT_EQ(fl_value_get_float(value), 1.0);
269 }
270
271 TEST(FlStandardMessageCodecTest, DecodeFloatMinusOne) {
272 g_autoptr(FlValue) value = decode_message("0600000000000000000000000000f0bf");
273 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_FLOAT);
274 EXPECT_EQ(fl_value_get_float(value), -1.0);
275 }
276
277 TEST(FlStandardMessageCodecTest, DecodeFloatHalf) {
278 g_autoptr(FlValue) value = decode_message("0600000000000000000000000000e03f");
279 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_FLOAT);
280 EXPECT_EQ(fl_value_get_float(value), 0.5);
281 }
282
283 TEST(FlStandardMessageCodecTest, DecodeFloatPi) {
284 g_autoptr(FlValue) value = decode_message("0600000000000000182d4454fb210940");
285 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_FLOAT);
286 EXPECT_EQ(fl_value_get_float(value), M_PI);
287 }
288
289 TEST(FlStandardMessageCodecTest, DecodeFloatMinusZero) {
290 g_autoptr(FlValue) value = decode_message("06000000000000000000000000000080");
291 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_FLOAT);
292 EXPECT_EQ(fl_value_get_float(value), -0.0);
293 }
294
295 TEST(FlStandardMessageCodecTest, DecodeFloatNaN) {
296 g_autoptr(FlValue) value = decode_message("0600000000000000000000000000f87f");
297 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_FLOAT);
298 EXPECT_TRUE(isnan(fl_value_get_float(value)));
299 }
300
301 TEST(FlStandardMessageCodecTest, DecodeFloatInfinity) {
302 g_autoptr(FlValue) value = decode_message("0600000000000000000000000000f07f");
303 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_FLOAT);
304 EXPECT_TRUE(isinf(fl_value_get_float(value)));
305 }
306
307 TEST(FlStandardMessageCodecTest, DecodeFloatNoData) {
308 decode_error_value("060000000000000000", FL_MESSAGE_CODEC_ERROR,
309 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
310 }
311
312 TEST(FlStandardMessageCodecTest, DecodeFloatShortData1) {
313 decode_error_value("060000000000000000", FL_MESSAGE_CODEC_ERROR,
314 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
315 }
316
317 TEST(FlStandardMessageCodecTest, DecodeFloatShortData2) {
318 decode_error_value("060000000000000000000000000000", FL_MESSAGE_CODEC_ERROR,
319 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
320 }
321
322 static gchar* encode_string(const gchar* value) {
323 g_autoptr(FlValue) v = fl_value_new_string(value);
324 return encode_message(v);
325 }
326
327 TEST(FlStandardMessageCodecTest, EncodeStringEmpty) {
328 g_autofree gchar* hex_string = encode_string("");
329 EXPECT_STREQ(hex_string, "0700");
330 }
331
332 TEST(FlStandardMessageCodecTest, EncodeStringHello) {
333 g_autofree gchar* hex_string = encode_string("hello");
334 EXPECT_STREQ(hex_string, "070568656c6c6f");
335 }
336
337 TEST(FlStandardMessageCodecTest, EncodeStringEmptySized) {
338 g_autoptr(FlValue) value = fl_value_new_string_sized(nullptr, 0);
339 g_autofree gchar* hex_string = encode_message(value);
340 EXPECT_STREQ(hex_string, "0700");
341 }
342
343 TEST(FlStandardMessageCodecTest, EncodeStringHelloSized) {
344 g_autoptr(FlValue) value = fl_value_new_string_sized("Hello World", 5);
345 g_autofree gchar* hex_string = encode_message(value);
346 EXPECT_STREQ(hex_string, "070548656c6c6f");
347 }
348
349 TEST(FlStandardMessageCodecTest, DecodeStringEmpty) {
350 g_autoptr(FlValue) value = decode_message("0700");
351 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_STRING);
352 EXPECT_STREQ(fl_value_get_string(value), "");
353 }
354
355 TEST(FlStandardMessageCodecTest, DecodeStringHello) {
356 g_autoptr(FlValue) value = decode_message("070568656c6c6f");
357 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_STRING);
358 EXPECT_STREQ(fl_value_get_string(value), "hello");
359 }
360
361 TEST(FlStandardMessageCodecTest, DecodeStringNoData) {
362 decode_error_value("07", FL_MESSAGE_CODEC_ERROR,
363 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
364 }
365
366 TEST(FlStandardMessageCodecTest, DecodeStringLengthNoData) {
367 decode_error_value("0705", FL_MESSAGE_CODEC_ERROR,
368 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
369 }
370
371 TEST(FlStandardMessageCodecTest, DecodeStringShortData1) {
372 decode_error_value("070568", FL_MESSAGE_CODEC_ERROR,
373 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
374 }
375
376 TEST(FlStandardMessageCodecTest, DecodeStringShortData2) {
377 decode_error_value("070568656c6c", FL_MESSAGE_CODEC_ERROR,
378 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
379 }
380
381 TEST(FlStandardMessageCodecTest, EncodeUint8ListEmpty) {
382 g_autoptr(FlValue) value = fl_value_new_uint8_list(nullptr, 0);
383 g_autofree gchar* hex_string = encode_message(value);
384 EXPECT_STREQ(hex_string, "0800");
385 }
386
387 TEST(FlStandardMessageCodecTest, EncodeUint8List) {
388 uint8_t data[] = {0, 1, 2, 3, 4};
389 g_autoptr(FlValue) value = fl_value_new_uint8_list(data, 5);
390 g_autofree gchar* hex_string = encode_message(value);
391 EXPECT_STREQ(hex_string, "08050001020304");
392 }
393
394 TEST(FlStandardMessageCodecTest, DecodeUint8ListEmpty) {
395 g_autoptr(FlValue) value = decode_message("0800");
396 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_UINT8_LIST);
397 EXPECT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
398 }
399
400 TEST(FlStandardMessageCodecTest, DecodeUint8List) {
401 g_autoptr(FlValue) value = decode_message("08050001020304");
402 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_UINT8_LIST);
403 EXPECT_EQ(fl_value_get_length(value), static_cast<size_t>(5));
404 const uint8_t* data = fl_value_get_uint8_list(value);
405 EXPECT_EQ(data[0], 0);
406 EXPECT_EQ(data[1], 1);
407 EXPECT_EQ(data[2], 2);
408 EXPECT_EQ(data[3], 3);
409 EXPECT_EQ(data[4], 4);
410 }
411
412 TEST(FlStandardMessageCodecTest, DecodeUint8ListNoData) {
413 decode_error_value("08", FL_MESSAGE_CODEC_ERROR,
414 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
415 }
416
417 TEST(FlStandardMessageCodecTest, DecodeUint8ListLengthNoData) {
418 decode_error_value("0805", FL_MESSAGE_CODEC_ERROR,
419 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
420 }
421
422 TEST(FlStandardMessageCodecTest, DecodeUint8ListShortData1) {
423 decode_error_value("080500", FL_MESSAGE_CODEC_ERROR,
424 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
425 }
426
427 TEST(FlStandardMessageCodecTest, DecodeUint8ListShortData2) {
428 decode_error_value("080500010203", FL_MESSAGE_CODEC_ERROR,
429 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
430 }
431
432 TEST(FlStandardMessageCodecTest, EncodeInt32ListEmpty) {
433 g_autoptr(FlValue) value = fl_value_new_int32_list(nullptr, 0);
434 g_autofree gchar* hex_string = encode_message(value);
435 EXPECT_STREQ(hex_string, "09000000");
436 }
437
438 TEST(FlStandardMessageCodecTest, EncodeInt32List) {
439 int32_t data[] = {0, -1, 2, -3, 4};
440 g_autoptr(FlValue) value = fl_value_new_int32_list(data, 5);
441 g_autofree gchar* hex_string = encode_message(value);
442 EXPECT_STREQ(hex_string, "0905000000000000ffffffff02000000fdffffff04000000");
443 }
444
445 TEST(FlStandardMessageCodecTest, DecodeInt32ListEmpty) {
446 g_autoptr(FlValue) value = decode_message("09000000");
447 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_INT32_LIST);
448 EXPECT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
449 }
450
451 TEST(FlStandardMessageCodecTest, DecodeInt32List) {
452 g_autoptr(FlValue) value =
453 decode_message("0905000000000000ffffffff02000000fdffffff04000000");
454 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_INT32_LIST);
455 const int32_t* data = fl_value_get_int32_list(value);
456 EXPECT_EQ(data[0], 0);
457 EXPECT_EQ(data[1], -1);
458 EXPECT_EQ(data[2], 2);
459 EXPECT_EQ(data[3], -3);
460 EXPECT_EQ(data[4], 4);
461 }
462
463 TEST(FlStandardMessageCodecTest, DecodeInt32ListNoData) {
464 decode_error_value("09", FL_MESSAGE_CODEC_ERROR,
465 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
466 }
467
468 TEST(FlStandardMessageCodecTest, DecodeInt32ListLengthNoData) {
469 decode_error_value("09050000", FL_MESSAGE_CODEC_ERROR,
470 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
471 }
472
473 TEST(FlStandardMessageCodecTest, DecodeInt32ListShortData1) {
474 decode_error_value("0905000000", FL_MESSAGE_CODEC_ERROR,
475 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
476 }
477
478 TEST(FlStandardMessageCodecTest, DecodeInt32ListShortData2) {
479 decode_error_value("090500000000ffffffff02000000fdffffff040000",
480 FL_MESSAGE_CODEC_ERROR,
481 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
482 }
483
484 TEST(FlStandardMessageCodecTest, EncodeInt64ListEmpty) {
485 g_autoptr(FlValue) value = fl_value_new_int64_list(nullptr, 0);
486 g_autofree gchar* hex_string = encode_message(value);
487 EXPECT_STREQ(hex_string, "0a00000000000000");
488 }
489
490 TEST(FlStandardMessageCodecTest, EncodeInt64List) {
491 int64_t data[] = {0, -1, 2, -3, 4};
492 g_autoptr(FlValue) value = fl_value_new_int64_list(data, 5);
493 g_autofree gchar* hex_string = encode_message(value);
494 EXPECT_STREQ(
495 hex_string,
496 "0a050000000000000000000000000000ffffffffffffffff0200000000000000fdffffff"
497 "ffffffff0400000000000000");
498 }
499
500 TEST(FlStandardMessageCodecTest, DecodeInt64ListEmpty) {
501 g_autoptr(FlValue) value = decode_message("0a00000000000000");
502 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_INT64_LIST);
503 EXPECT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
504 }
505
506 TEST(FlStandardMessageCodecTest, DecodeInt64List) {
507 g_autoptr(FlValue) value = decode_message(
508 "0a050000000000000000000000000000ffffffffffffffff0200000000000000fdffffff"
509 "ffffffff0400000000000000");
510 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_INT64_LIST);
511 const int64_t* data = fl_value_get_int64_list(value);
512 EXPECT_EQ(data[0], 0);
513 EXPECT_EQ(data[1], -1);
514 EXPECT_EQ(data[2], 2);
515 EXPECT_EQ(data[3], -3);
516 EXPECT_EQ(data[4], 4);
517 }
518
519 TEST(FlStandardMessageCodecTest, DecodeInt64ListNoData) {
520 decode_error_value("0a", FL_MESSAGE_CODEC_ERROR,
521 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
522 }
523
524 TEST(FlStandardMessageCodecTest, DecodeInt64ListLengthNoData) {
525 decode_error_value("0a05000000000000", FL_MESSAGE_CODEC_ERROR,
526 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
527 }
528
529 TEST(FlStandardMessageCodecTest, DecodeInt64ListShortData1) {
530 decode_error_value("0a0500000000000000", FL_MESSAGE_CODEC_ERROR,
531 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
532 }
533
534 TEST(FlStandardMessageCodecTest, DecodeInt64ListShortData2) {
535 decode_error_value(
536 "0a050000000000000000000000000000ffffffffffffffff0200000000000000fdffffff"
537 "ffffffff0400"
538 "0000000000",
539 FL_MESSAGE_CODEC_ERROR, FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
540 }
541
542 TEST(FlStandardMessageCodecTest, EncodeFloat32ListEmpty) {
543 g_autoptr(FlValue) value = fl_value_new_float32_list(nullptr, 0);
544 g_autofree gchar* hex_string = encode_message(value);
545 EXPECT_STREQ(hex_string, "0e000000");
546 }
547
548 TEST(FlStandardMessageCodecTest, EncodeFloat32List) {
549 float data[] = {0.0f, -0.5f, 0.25f, -0.125f, 0.00625f};
550 g_autoptr(FlValue) value = fl_value_new_float32_list(data, 5);
551 g_autofree gchar* hex_string = encode_message(value);
552 EXPECT_STREQ(hex_string, "0e05000000000000000000bf0000803e000000becdcccc3b");
553 }
554
555 TEST(FlStandardMessageCodecTest, DecodeFloat32ListEmpty) {
556 g_autoptr(FlValue) value = decode_message("0e000000");
557 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_FLOAT32_LIST);
558 EXPECT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
559 }
560
561 TEST(FlStandardMessageCodecTest, DecodeFloat32List) {
562 g_autoptr(FlValue) value =
563 decode_message("0e05000000000000000000bf0000803e000000becdcccc3b");
564 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_FLOAT32_LIST);
565 const float* data = fl_value_get_float32_list(value);
566 EXPECT_FLOAT_EQ(data[0], 0.0f);
567 EXPECT_FLOAT_EQ(data[1], -0.5f);
568 EXPECT_FLOAT_EQ(data[2], 0.25f);
569 EXPECT_FLOAT_EQ(data[3], -0.125f);
570 EXPECT_FLOAT_EQ(data[4], 0.00625f);
571 }
572
573 TEST(FlStandardMessageCodecTest, DecodeFloat32ListNoData) {
574 decode_error_value("0e", FL_MESSAGE_CODEC_ERROR,
575 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
576 }
577
578 TEST(FlStandardMessageCodecTest, DecodeFloat32ListLengthNoData) {
579 decode_error_value("0e050000", FL_MESSAGE_CODEC_ERROR,
580 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
581 }
582
583 TEST(FlStandardMessageCodecTest, DecodeFloat32ListShortData1) {
584 decode_error_value("0e05000000", FL_MESSAGE_CODEC_ERROR,
585 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
586 }
587
588 TEST(FlStandardMessageCodecTest, DecodeFloat32ListShortData2) {
589 decode_error_value("0e05000000000000000000bf0000803e000000becdcccc",
590 FL_MESSAGE_CODEC_ERROR,
591 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
592 }
593
594 TEST(FlStandardMessageCodecTest, EncodeFloatListEmpty) {
595 g_autoptr(FlValue) value = fl_value_new_float_list(nullptr, 0);
596 g_autofree gchar* hex_string = encode_message(value);
597 EXPECT_STREQ(hex_string, "0b00000000000000");
598 }
599
600 TEST(FlStandardMessageCodecTest, EncodeFloatList) {
601 double data[] = {0, -0.5, 0.25, -0.125, 0.00625};
602 g_autoptr(FlValue) value = fl_value_new_float_list(data, 5);
603 g_autofree gchar* hex_string = encode_message(value);
604 EXPECT_STREQ(
605 hex_string,
606 "0b050000000000000000000000000000000000000000e0bf000000000000d03f00000000"
607 "0000c0bf9a9999999999793f");
608 }
609
610 TEST(FlStandardMessageCodecTest, DecodeFloatListEmpty) {
611 g_autoptr(FlValue) value = decode_message("0b00000000000000");
612 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_FLOAT_LIST);
613 EXPECT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
614 }
615
616 TEST(FlStandardMessageCodecTest, DecodeFloatList) {
617 g_autoptr(FlValue) value = decode_message(
618 "0b050000000000000000000000000000000000000000e0bf000000000000d03f00000000"
619 "0000c0bf9a9999999999793f");
620 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_FLOAT_LIST);
621 const double* data = fl_value_get_float_list(value);
622 EXPECT_FLOAT_EQ(data[0], 0.0);
623 EXPECT_FLOAT_EQ(data[1], -0.5);
624 EXPECT_FLOAT_EQ(data[2], 0.25);
625 EXPECT_FLOAT_EQ(data[3], -0.125);
626 EXPECT_FLOAT_EQ(data[4], 0.00625);
627 }
628
629 TEST(FlStandardMessageCodecTest, DecodeFloatListNoData) {
630 decode_error_value("0b", FL_MESSAGE_CODEC_ERROR,
631 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
632 }
633
634 TEST(FlStandardMessageCodecTest, DecodeFloatListLengthNoData) {
635 decode_error_value("0b05000000000000", FL_MESSAGE_CODEC_ERROR,
636 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
637 }
638
639 TEST(FlStandardMessageCodecTest, DecodeFloatListShortData1) {
640 decode_error_value("0b0500000000000000", FL_MESSAGE_CODEC_ERROR,
641 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
642 }
643
644 TEST(FlStandardMessageCodecTest, DecodeFloatListShortData2) {
645 decode_error_value(
646 "0b050000000000000000000000000000000000000000e0bf000000000000d03f00000000"
647 "0000c0bf9a99"
648 "9999999979",
649 FL_MESSAGE_CODEC_ERROR, FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
650 }
651
652 TEST(FlStandardMessageCodecTest, EncodeListEmpty) {
653 g_autoptr(FlValue) value = fl_value_new_list();
654 g_autofree gchar* hex_string = encode_message(value);
655 EXPECT_STREQ(hex_string, "0c00");
656 }
657
658 TEST(FlStandardMessageCodecTest, EncodeListTypes) {
659 g_autoptr(FlValue) value = fl_value_new_list();
660 fl_value_append_take(value, fl_value_new_null());
661 fl_value_append_take(value, fl_value_new_bool(TRUE));
662 fl_value_append_take(value, fl_value_new_int(42));
663 fl_value_append_take(value, fl_value_new_float(M_PI));
664 fl_value_append_take(value, fl_value_new_string("hello"));
665 fl_value_append_take(value, fl_value_new_list());
666 fl_value_append_take(value, fl_value_new_map());
667 g_autofree gchar* hex_string = encode_message(value);
668 EXPECT_STREQ(
669 hex_string,
670 "0c070001032a00000006000000000000182d4454fb210940070568656c6c6f0c000d00");
671 }
672
673 TEST(FlStandardMessageCodecTest, EncodeListNested) {
674 g_autoptr(FlValue) even_numbers = fl_value_new_list();
675 g_autoptr(FlValue) odd_numbers = fl_value_new_list();
676 for (int i = 0; i < 10; i++) {
677 if (i % 2 == 0) {
678 fl_value_append_take(even_numbers, fl_value_new_int(i));
679 } else {
680 fl_value_append_take(odd_numbers, fl_value_new_int(i));
681 }
682 }
683 g_autoptr(FlValue) value = fl_value_new_list();
684 fl_value_append(value, even_numbers);
685 fl_value_append(value, odd_numbers);
686 g_autofree gchar* hex_string = encode_message(value);
687 EXPECT_STREQ(hex_string,
688 "0c020c05030000000003020000000304000000030600000003080000000c"
689 "0503010000000303000000030500000003070000000309000000");
690 }
691
692 TEST(FlStandardMessageCodecTest, DecodeListEmpty) {
693 g_autoptr(FlValue) value = decode_message("0c00");
694 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_LIST);
695 EXPECT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
696 }
697
698 TEST(FlStandardMessageCodecTest, DecodeListTypes) {
699 g_autoptr(FlValue) value = decode_message(
700 "0c070001032a00000006000000000000182d4454fb210940070568656c6c6f0c000d00");
701 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_LIST);
702 ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(7));
703 ASSERT_EQ(fl_value_get_type(fl_value_get_list_value(value, 0)),
704 FL_VALUE_TYPE_NULL);
705 ASSERT_EQ(fl_value_get_type(fl_value_get_list_value(value, 1)),
706 FL_VALUE_TYPE_BOOL);
707 EXPECT_TRUE(fl_value_get_bool(fl_value_get_list_value(value, 1)));
708 ASSERT_EQ(fl_value_get_type(fl_value_get_list_value(value, 2)),
709 FL_VALUE_TYPE_INT);
710 EXPECT_EQ(fl_value_get_int(fl_value_get_list_value(value, 2)), 42);
711 ASSERT_EQ(fl_value_get_type(fl_value_get_list_value(value, 3)),
712 FL_VALUE_TYPE_FLOAT);
713 EXPECT_FLOAT_EQ(fl_value_get_float(fl_value_get_list_value(value, 3)), M_PI);
714 ASSERT_EQ(fl_value_get_type(fl_value_get_list_value(value, 4)),
715 FL_VALUE_TYPE_STRING);
716 EXPECT_STREQ(fl_value_get_string(fl_value_get_list_value(value, 4)), "hello");
717 ASSERT_EQ(fl_value_get_type(fl_value_get_list_value(value, 5)),
718 FL_VALUE_TYPE_LIST);
719 ASSERT_EQ(fl_value_get_length(fl_value_get_list_value(value, 5)),
720 static_cast<size_t>(0));
721 ASSERT_EQ(fl_value_get_type(fl_value_get_list_value(value, 6)),
722 FL_VALUE_TYPE_MAP);
723 ASSERT_EQ(fl_value_get_length(fl_value_get_list_value(value, 6)),
724 static_cast<size_t>(0));
725 }
726
727 TEST(FlStandardMessageCodecTest, DecodeListNested) {
728 g_autoptr(FlValue) value = decode_message(
729 "0c020c05030000000003020000000304000000030600000003080000000c"
730 "0503010000000303000000030500000003070000000309000000");
731 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_LIST);
732 ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(2));
733 FlValue* even_list = fl_value_get_list_value(value, 0);
734 ASSERT_EQ(fl_value_get_type(even_list), FL_VALUE_TYPE_LIST);
735 ASSERT_EQ(fl_value_get_length(even_list), static_cast<size_t>(5));
736 FlValue* odd_list = fl_value_get_list_value(value, 1);
737 ASSERT_EQ(fl_value_get_type(odd_list), FL_VALUE_TYPE_LIST);
738 ASSERT_EQ(fl_value_get_length(odd_list), static_cast<size_t>(5));
739 for (int i = 0; i < 5; i++) {
740 FlValue* v = fl_value_get_list_value(even_list, i);
741 ASSERT_EQ(fl_value_get_type(v), FL_VALUE_TYPE_INT);
742 EXPECT_EQ(fl_value_get_int(v), i * 2);
743
744 v = fl_value_get_list_value(odd_list, i);
745 ASSERT_EQ(fl_value_get_type(v), FL_VALUE_TYPE_INT);
746 EXPECT_EQ(fl_value_get_int(v), i * 2 + 1);
747 }
748 }
749
750 TEST(FlStandardMessageCodecTest, DecodeListNoData) {
751 decode_error_value("0c", FL_MESSAGE_CODEC_ERROR,
752 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
753 }
754
755 TEST(FlStandardMessageCodecTest, DecodeListLengthNoData) {
756 decode_error_value("0c07", FL_MESSAGE_CODEC_ERROR,
757 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
758 }
759
760 TEST(FlStandardMessageCodecTest, DecodeListShortData1) {
761 decode_error_value("0c0700", FL_MESSAGE_CODEC_ERROR,
762 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
763 }
764
765 TEST(FlStandardMessageCodecTest, DecodeListShortData2) {
766 decode_error_value(
767 "0c070001032a00000006000000000000182d4454fb210940070568656c6c6f0c000d",
768 FL_MESSAGE_CODEC_ERROR, FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
769 }
770
771 TEST(FlStandardMessageCodecTest, EncodeDecodeLargeList) {
772 g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
773
774 g_autoptr(FlValue) value = fl_value_new_list();
775 for (int i = 0; i < 65535; i++) {
776 fl_value_append_take(value, fl_value_new_int(i));
777 }
778
779 g_autoptr(GError) error = nullptr;
780 g_autoptr(GBytes) message =
781 fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), value, &error);
782 EXPECT_NE(message, nullptr);
783 EXPECT_EQ(error, nullptr);
784
785 g_autoptr(FlValue) decoded_value =
786 fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), message, &error);
787 EXPECT_EQ(error, nullptr);
788 EXPECT_NE(value, nullptr);
789
790 ASSERT_TRUE(fl_value_equal(value, decoded_value));
791 }
792
793 TEST(FlStandardMessageCodecTest, EncodeMapEmpty) {
794 g_autoptr(FlValue) value = fl_value_new_map();
795 g_autofree gchar* hex_string = encode_message(value);
796 EXPECT_STREQ(hex_string, "0d00");
797 }
798
799 TEST(FlStandardMessageCodecTest, EncodeMapKeyTypes) {
800 g_autoptr(FlValue) value = fl_value_new_map();
801 fl_value_set_take(value, fl_value_new_null(), fl_value_new_string("null"));
802 fl_value_set_take(value, fl_value_new_bool(TRUE),
803 fl_value_new_string("bool"));
804 fl_value_set_take(value, fl_value_new_int(42), fl_value_new_string("int"));
805 fl_value_set_take(value, fl_value_new_float(M_PI),
806 fl_value_new_string("float"));
807 fl_value_set_take(value, fl_value_new_string("hello"),
808 fl_value_new_string("string"));
809 fl_value_set_take(value, fl_value_new_list(), fl_value_new_string("list"));
810 fl_value_set_take(value, fl_value_new_map(), fl_value_new_string("map"));
811 g_autofree gchar* hex_string = encode_message(value);
812 EXPECT_STREQ(hex_string,
813 "0d070007046e756c6c010704626f6f6c032a0000000703696e7406000000000"
814 "0182d4454fb2109400705666c6f6174070568656c6c6f0706737472696e670c"
815 "0007046c6973740d0007036d6170");
816 }
817
818 TEST(FlStandardMessageCodecTest, EncodeMapValueTypes) {
819 g_autoptr(FlValue) value = fl_value_new_map();
820 fl_value_set_take(value, fl_value_new_string("null"), fl_value_new_null());
821 fl_value_set_take(value, fl_value_new_string("bool"),
822 fl_value_new_bool(TRUE));
823 fl_value_set_take(value, fl_value_new_string("int"), fl_value_new_int(42));
824 fl_value_set_take(value, fl_value_new_string("float"),
825 fl_value_new_float(M_PI));
826 fl_value_set_take(value, fl_value_new_string("string"),
827 fl_value_new_string("hello"));
828 fl_value_set_take(value, fl_value_new_string("list"), fl_value_new_list());
829 fl_value_set_take(value, fl_value_new_string("map"), fl_value_new_map());
830 g_autofree gchar* hex_string = encode_message(value);
831 EXPECT_STREQ(hex_string,
832 "0d0707046e756c6c000704626f6f6c010703696e74032a0000000705666c6f6"
833 "17406000000000000182d4454fb2109400706737472696e67070568656c6c6f"
834 "07046c6973740c0007036d61700d00");
835 }
836
837 TEST(FlStandardMessageCodecTest, EncodeMapNested) {
838 g_autoptr(FlValue) str_to_int = fl_value_new_map();
839 g_autoptr(FlValue) int_to_str = fl_value_new_map();
840 const char* numbers[] = {"zero", "one", "two", "three", nullptr};
841 for (int i = 0; numbers[i] != nullptr; i++) {
842 fl_value_set_take(str_to_int, fl_value_new_string(numbers[i]),
843 fl_value_new_int(i));
844 fl_value_set_take(int_to_str, fl_value_new_int(i),
845 fl_value_new_string(numbers[i]));
846 }
847 g_autoptr(FlValue) value = fl_value_new_map();
848 fl_value_set_string(value, "str-to-int", str_to_int);
849 fl_value_set_string(value, "int-to-str", int_to_str);
850 g_autofree gchar* hex_string = encode_message(value);
851 EXPECT_STREQ(hex_string,
852 "0d02070a7374722d746f2d696e740d0407047a65726f030000000007036f6e6"
853 "50301000000070374776f0302000000070574687265650303000000070a696e"
854 "742d746f2d7374720d04030000000007047a65726f030100000007036f6e650"
855 "302000000070374776f030300000007057468726565");
856 }
857
858 TEST(FlStandardMessageCodecTest, DecodeMapEmpty) {
859 g_autoptr(FlValue) value = decode_message("0d00");
860 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_MAP);
861 ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
862 }
863
864 TEST(FlStandardMessageCodecTest, DecodeMapKeyTypes) {
865 g_autoptr(FlValue) value = decode_message(
866 "0d070007046e756c6c010704626f6f6c032a0000000703696e74060000000000182d4454"
867 "fb2109400705666c6f6174070568656c6c6f0706737472696e670c0007046c6973740d00"
868 "07036d6170");
869 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_MAP);
870 ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(7));
871
872 ASSERT_EQ(fl_value_get_type(fl_value_get_map_key(value, 0)),
873 FL_VALUE_TYPE_NULL);
874 ASSERT_EQ(fl_value_get_type(fl_value_get_map_value(value, 0)),
875 FL_VALUE_TYPE_STRING);
876 EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 0)), "null");
877
878 ASSERT_EQ(fl_value_get_type(fl_value_get_map_key(value, 1)),
879 FL_VALUE_TYPE_BOOL);
880 EXPECT_TRUE(fl_value_get_bool(fl_value_get_map_key(value, 1)));
881 ASSERT_EQ(fl_value_get_type(fl_value_get_map_value(value, 1)),
882 FL_VALUE_TYPE_STRING);
883 EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 1)), "bool");
884
885 ASSERT_EQ(fl_value_get_type(fl_value_get_map_key(value, 2)),
886 FL_VALUE_TYPE_INT);
887 EXPECT_EQ(fl_value_get_int(fl_value_get_map_key(value, 2)), 42);
888 ASSERT_EQ(fl_value_get_type(fl_value_get_map_value(value, 2)),
889 FL_VALUE_TYPE_STRING);
890 EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 2)), "int");
891
892 ASSERT_EQ(fl_value_get_type(fl_value_get_map_key(value, 3)),
893 FL_VALUE_TYPE_FLOAT);
894 EXPECT_FLOAT_EQ(fl_value_get_float(fl_value_get_map_key(value, 3)), M_PI);
895 ASSERT_EQ(fl_value_get_type(fl_value_get_map_value(value, 3)),
896 FL_VALUE_TYPE_STRING);
897 EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 3)), "float");
898
899 ASSERT_EQ(fl_value_get_type(fl_value_get_map_key(value, 4)),
900 FL_VALUE_TYPE_STRING);
901 EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 4)), "hello");
902 ASSERT_EQ(fl_value_get_type(fl_value_get_map_value(value, 4)),
903 FL_VALUE_TYPE_STRING);
904 EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 4)), "string");
905
906 ASSERT_EQ(fl_value_get_type(fl_value_get_map_key(value, 5)),
907 FL_VALUE_TYPE_LIST);
908 ASSERT_EQ(fl_value_get_length(fl_value_get_map_key(value, 5)),
909 static_cast<size_t>(0));
910 ASSERT_EQ(fl_value_get_type(fl_value_get_map_value(value, 5)),
911 FL_VALUE_TYPE_STRING);
912 EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 5)), "list");
913
914 ASSERT_EQ(fl_value_get_type(fl_value_get_map_key(value, 6)),
915 FL_VALUE_TYPE_MAP);
916 ASSERT_EQ(fl_value_get_length(fl_value_get_map_key(value, 6)),
917 static_cast<size_t>(0));
918 ASSERT_EQ(fl_value_get_type(fl_value_get_map_value(value, 6)),
919 FL_VALUE_TYPE_STRING);
920 EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 6)), "map");
921 }
922
923 TEST(FlStandardMessageCodecTest, DecodeMapValueTypes) {
924 g_autoptr(FlValue) value = decode_message(
925 "0d0707046e756c6c000704626f6f6c010703696e74032a0000000705666c6f6174060000"
926 "00000000182d4454fb2109400706737472696e67070568656c6c6f07046c6973740c0007"
927 "036d61700d00");
928 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_MAP);
929 ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(7));
930
931 ASSERT_EQ(fl_value_get_type(fl_value_get_map_key(value, 0)),
932 FL_VALUE_TYPE_STRING);
933 EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 0)), "null");
934 ASSERT_EQ(fl_value_get_type(fl_value_get_map_value(value, 0)),
935 FL_VALUE_TYPE_NULL);
936
937 ASSERT_EQ(fl_value_get_type(fl_value_get_map_key(value, 1)),
938 FL_VALUE_TYPE_STRING);
939 EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 1)), "bool");
940 ASSERT_EQ(fl_value_get_type(fl_value_get_map_value(value, 1)),
941 FL_VALUE_TYPE_BOOL);
942 EXPECT_TRUE(fl_value_get_bool(fl_value_get_map_value(value, 1)));
943
944 ASSERT_EQ(fl_value_get_type(fl_value_get_map_key(value, 2)),
945 FL_VALUE_TYPE_STRING);
946 EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 2)), "int");
947 ASSERT_EQ(fl_value_get_type(fl_value_get_map_value(value, 2)),
948 FL_VALUE_TYPE_INT);
949 EXPECT_EQ(fl_value_get_int(fl_value_get_map_value(value, 2)), 42);
950
951 ASSERT_EQ(fl_value_get_type(fl_value_get_map_key(value, 3)),
952 FL_VALUE_TYPE_STRING);
953 EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 3)), "float");
954 ASSERT_EQ(fl_value_get_type(fl_value_get_map_value(value, 3)),
955 FL_VALUE_TYPE_FLOAT);
956 EXPECT_FLOAT_EQ(fl_value_get_float(fl_value_get_map_value(value, 3)), M_PI);
957
958 ASSERT_EQ(fl_value_get_type(fl_value_get_map_key(value, 4)),
959 FL_VALUE_TYPE_STRING);
960 EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 4)), "string");
961 ASSERT_EQ(fl_value_get_type(fl_value_get_map_value(value, 4)),
962 FL_VALUE_TYPE_STRING);
963 EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(value, 4)), "hello");
964
965 ASSERT_EQ(fl_value_get_type(fl_value_get_map_key(value, 5)),
966 FL_VALUE_TYPE_STRING);
967 EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 5)), "list");
968 ASSERT_EQ(fl_value_get_type(fl_value_get_map_value(value, 5)),
969 FL_VALUE_TYPE_LIST);
970 ASSERT_EQ(fl_value_get_length(fl_value_get_map_value(value, 5)),
971 static_cast<size_t>(0));
972
973 ASSERT_EQ(fl_value_get_type(fl_value_get_map_key(value, 6)),
974 FL_VALUE_TYPE_STRING);
975 EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 6)), "map");
976 ASSERT_EQ(fl_value_get_type(fl_value_get_map_value(value, 6)),
977 FL_VALUE_TYPE_MAP);
978 ASSERT_EQ(fl_value_get_length(fl_value_get_map_value(value, 6)),
979 static_cast<size_t>(0));
980 }
981
982 TEST(FlStandardMessageCodecTest, DecodeMapNested) {
983 g_autoptr(FlValue) value = decode_message(
984 "0d02070a7374722d746f2d696e740d0407047a65726f030000000007036f6e6503010000"
985 "00070374776f0302000000070574687265650303000000070a696e742d746f2d7374720d"
986 "04030000000007047a65726f030100000007036f6e650302000000070374776f03030000"
987 "0007057468726565");
988 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_MAP);
989 ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(2));
990
991 ASSERT_EQ(fl_value_get_type(fl_value_get_map_key(value, 0)),
992 FL_VALUE_TYPE_STRING);
993 EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 0)),
994 "str-to-int");
995 FlValue* str_to_int = fl_value_get_map_value(value, 0);
996 ASSERT_EQ(fl_value_get_type(str_to_int), FL_VALUE_TYPE_MAP);
997 ASSERT_EQ(fl_value_get_length(str_to_int), static_cast<size_t>(4));
998
999 ASSERT_EQ(fl_value_get_type(fl_value_get_map_key(value, 1)),
1000 FL_VALUE_TYPE_STRING);
1001 EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(value, 1)),
1002 "int-to-str");
1003 FlValue* int_to_str = fl_value_get_map_value(value, 1);
1004 ASSERT_EQ(fl_value_get_type(int_to_str), FL_VALUE_TYPE_MAP);
1005 ASSERT_EQ(fl_value_get_length(int_to_str), static_cast<size_t>(4));
1006
1007 const char* numbers[] = {"zero", "one", "two", "three", nullptr};
1008 for (int i = 0; numbers[i] != nullptr; i++) {
1009 ASSERT_EQ(fl_value_get_type(fl_value_get_map_key(str_to_int, i)),
1010 FL_VALUE_TYPE_STRING);
1011 EXPECT_STREQ(fl_value_get_string(fl_value_get_map_key(str_to_int, i)),
1012 numbers[i]);
1013
1014 ASSERT_EQ(fl_value_get_type(fl_value_get_map_value(str_to_int, i)),
1015 FL_VALUE_TYPE_INT);
1016 EXPECT_EQ(fl_value_get_int(fl_value_get_map_value(str_to_int, i)), i);
1017
1018 ASSERT_EQ(fl_value_get_type(fl_value_get_map_key(int_to_str, i)),
1019 FL_VALUE_TYPE_INT);
1020 EXPECT_EQ(fl_value_get_int(fl_value_get_map_key(int_to_str, i)), i);
1021
1022 ASSERT_EQ(fl_value_get_type(fl_value_get_map_value(int_to_str, i)),
1023 FL_VALUE_TYPE_STRING);
1024 EXPECT_STREQ(fl_value_get_string(fl_value_get_map_value(int_to_str, i)),
1025 numbers[i]);
1026 }
1027 }
1028
1029 TEST(FlStandardMessageCodecTest, DecodeMapNoData) {
1030 decode_error_value("0d", FL_MESSAGE_CODEC_ERROR,
1031 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
1032 }
1033
1034 TEST(FlStandardMessageCodecTest, DecodeMapLengthNoData) {
1035 decode_error_value("0d07", FL_MESSAGE_CODEC_ERROR,
1036 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
1037 }
1038
1039 TEST(FlStandardMessageCodecTest, DecodeMapShortData1) {
1040 decode_error_value("0d0707", FL_MESSAGE_CODEC_ERROR,
1041 FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
1042 }
1043
1044 TEST(FlStandardMessageCodecTest, DecodeMapShortData2) {
1045 decode_error_value(
1046 "0d0707046e756c6c000704626f6f6c010703696e74032a0000000705666c6f6174060000"
1047 "00000000182d4454fb2109400706737472696e67070568656c6c6f07046c6973740c0007"
1048 "036d61700d",
1049 FL_MESSAGE_CODEC_ERROR, FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA);
1050 }
1051
1052 TEST(FlStandardMessageCodecTest, EncodeDecodeLargeMap) {
1053 g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
1054
1055 g_autoptr(FlValue) value = fl_value_new_map();
1056 for (int i = 0; i < 512; i++) {
1057 g_autofree gchar* key = g_strdup_printf("key%d", i);
1058 fl_value_set_string_take(value, key, fl_value_new_int(i));
1059 }
1060
1061 g_autoptr(GError) error = nullptr;
1062 g_autoptr(GBytes) message =
1063 fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), value, &error);
1064 EXPECT_NE(message, nullptr);
1065 EXPECT_EQ(error, nullptr);
1066
1067 g_autoptr(FlValue) decoded_value =
1068 fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), message, &error);
1069 EXPECT_EQ(error, nullptr);
1070 EXPECT_NE(value, nullptr);
1071
1072 ASSERT_TRUE(fl_value_equal(value, decoded_value));
1073 }
1074
1075 TEST(FlStandardMessageCodecTest, DecodeUnknownType) {
1076 decode_error_value("0f", FL_MESSAGE_CODEC_ERROR,
1077 FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE);
1078 }
1079
1080 TEST(FlStandardMessageCodecTest, EncodeDecode) {
1081 g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
1082
1083 g_autoptr(FlValue) input = fl_value_new_list();
1084 fl_value_append_take(input, fl_value_new_null());
1085 fl_value_append_take(input, fl_value_new_bool(TRUE));
1086 fl_value_append_take(input, fl_value_new_int(42));
1087 fl_value_append_take(input, fl_value_new_float(M_PI));
1088 fl_value_append_take(input, fl_value_new_string("hello"));
1089 fl_value_append_take(input, fl_value_new_list());
1090 fl_value_append_take(input, fl_value_new_map());
1091
1092 g_autoptr(GError) error = nullptr;
1093 g_autoptr(GBytes) message =
1094 fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), input, &error);
1095 EXPECT_NE(message, nullptr);
1096 EXPECT_EQ(error, nullptr);
1097
1098 g_autoptr(FlValue) output =
1099 fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), message, &error);
1100 EXPECT_EQ(error, nullptr);
1101 EXPECT_NE(output, nullptr);
1102
1103 ASSERT_TRUE(fl_value_equal(input, output));
1104 }
fl_value_get_int32_list
const G_MODULE_EXPORT int32_t * fl_value_get_int32_list(FlValue *self)
Definition: fl_value.cc:656
FL_VALUE_TYPE_UINT8_LIST
@ FL_VALUE_TYPE_UINT8_LIST
Definition: fl_value.h:65
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:272
FL_VALUE_TYPE_MAP
@ FL_VALUE_TYPE_MAP
Definition: fl_value.h:70
fl_standard_message_codec_new
G_MODULE_EXPORT FlStandardMessageCodec * fl_standard_message_codec_new()
Definition: fl_standard_message_codec.cc:458
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:610
fl_value_new_list
G_MODULE_EXPORT FlValue * fl_value_new_list()
Definition: fl_value.cc:338
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:318
fl_value_get_float_list
const G_MODULE_EXPORT double * fl_value_get_float_list(FlValue *self)
Definition: fl_value.cc:677
fl_value_new_bool
G_MODULE_EXPORT FlValue * fl_value_new_bool(bool value)
Definition: fl_value.cc:244
FlValue
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:40
encode_string
static gchar * encode_string(const gchar *value)
Definition: fl_standard_message_codec_test.cc:322
fl_value_set_take
G_MODULE_EXPORT void fl_value_set_take(FlValue *self, FlValue *key, FlValue *value)
Definition: fl_value.cc:578
fl_value_new_null
G_MODULE_EXPORT FlValue * fl_value_new_null()
Definition: fl_value.cc:240
FL_VALUE_TYPE_LIST
@ FL_VALUE_TYPE_LIST
Definition: fl_value.h:69
encode_int
static gchar * encode_int(int64_t value)
Definition: fl_standard_message_codec_test.cc:91
fl_value_get_bool
G_MODULE_EXPORT bool fl_value_get_bool(FlValue *self)
Definition: fl_value.cc:621
fl_value_get_uint8_list
const G_MODULE_EXPORT uint8_t * fl_value_get_uint8_list(FlValue *self)
Definition: fl_value.cc:649
fl_value_new_int
G_MODULE_EXPORT FlValue * fl_value_new_int(int64_t value)
Definition: fl_value.cc:251
fl_value_get_string
const G_MODULE_EXPORT gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:642
fl_value_get_float32_list
const G_MODULE_EXPORT float * fl_value_get_float32_list(FlValue *self)
Definition: fl_value.cc:670
FL_VALUE_TYPE_NULL
@ FL_VALUE_TYPE_NULL
Definition: fl_value.h:60
fl_value_get_int
G_MODULE_EXPORT int64_t fl_value_get_int(FlValue *self)
Definition: fl_value.cc:628
fl_value_ref
G_MODULE_EXPORT FlValue * fl_value_ref(FlValue *self)
Definition: fl_value.cc:363
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
TEST
TEST(FlStandardMessageCodecTest, EncodeNullptr)
Definition: fl_standard_message_codec_test.cc:52
fl_value_new_map
G_MODULE_EXPORT FlValue * fl_value_new_map()
Definition: fl_value.cc:355
fl_value_get_type
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:428
fl_value_get_list_value
G_MODULE_EXPORT FlValue * fl_value_get_list_value(FlValue *self, size_t index)
Definition: fl_value.cc:735
FL_VALUE_TYPE_FLOAT32_LIST
@ FL_VALUE_TYPE_FLOAT32_LIST
Definition: fl_value.h:71
FL_VALUE_TYPE_STRING
@ FL_VALUE_TYPE_STRING
Definition: fl_value.h:64
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:328
TRUE
return TRUE
Definition: fl_pixel_buffer_texture_test.cc:53
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:298
fl_value_get_float
G_MODULE_EXPORT double fl_value_get_float(FlValue *self)
Definition: fl_value.cc:635
fl_value_append_take
G_MODULE_EXPORT void fl_value_append_take(FlValue *self, FlValue *value)
Definition: fl_value.cc:560
fl_value_get_length
G_MODULE_EXPORT size_t fl_value_get_length(FlValue *self)
Definition: fl_value.cc:684
FL_VALUE_TYPE_INT64_LIST
@ FL_VALUE_TYPE_INT64_LIST
Definition: fl_value.h:67
fl_value_equal
G_MODULE_EXPORT bool fl_value_equal(FlValue *a, FlValue *b)
Definition: fl_value.cc:433
fl_value_set_string
G_MODULE_EXPORT void fl_value_set_string(FlValue *self, const gchar *key, FlValue *value)
Definition: fl_value.cc:599
FL_VALUE_TYPE_INT
@ FL_VALUE_TYPE_INT
Definition: fl_value.h:62
encode_message
static gchar * encode_message(FlValue *value)
Definition: fl_standard_message_codec_test.cc:14
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:308
encode_bool
static gchar * encode_bool(gboolean value)
Definition: fl_standard_message_codec_test.cc:76
FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA
@ FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA
Definition: fl_message_codec.h:34
error
const uint8_t uint32_t uint32_t GError ** error
Definition: fl_pixel_buffer_texture_test.cc:40
FL_VALUE_TYPE_FLOAT_LIST
@ FL_VALUE_TYPE_FLOAT_LIST
Definition: fl_value.h:68
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:281
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:552
FL_VALUE_TYPE_FLOAT
@ FL_VALUE_TYPE_FLOAT
Definition: fl_value.h:63
FL_VALUE_TYPE_INT32_LIST
@ FL_VALUE_TYPE_INT32_LIST
Definition: fl_value.h:66
FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE
@ FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE
Definition: fl_message_codec.h:36
fl_value_new_float
G_MODULE_EXPORT FlValue * fl_value_new_float(double value)
Definition: fl_value.cc:258
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:743
fl_value_get_int64_list
const G_MODULE_EXPORT int64_t * fl_value_get_int64_list(FlValue *self)
Definition: fl_value.cc:663
decode_error_value
static void decode_error_value(const char *hex_string, GQuark domain, gint code)
Definition: fl_standard_message_codec_test.cc:40
FL_VALUE_TYPE_BOOL
@ FL_VALUE_TYPE_BOOL
Definition: fl_value.h:61
decode_message
static FlValue * decode_message(const char *hex_string)
Definition: fl_standard_message_codec_test.cc:27
value
uint8_t value
Definition: fl_standard_message_codec.cc:41
encode_float
static gchar * encode_float(double value)
Definition: fl_standard_message_codec_test.cc:214
fl_value_get_map_value
G_MODULE_EXPORT FlValue * fl_value_get_map_value(FlValue *self, size_t index)
Definition: fl_value.cc:751
fl_value_new_string
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:265
FL_MESSAGE_CODEC_ERROR
#define FL_MESSAGE_CODEC_ERROR
Definition: fl_message_codec.h:30