| Flutter Linux Embedder |

fl_json_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_json_message_codec.h"
6 #include "gtest/gtest.h"
7
8 #include <cmath>
9
10 // Encodes a message using FlJsonMessageCodec to a UTF-8 string.
11 static gchar* encode_message(FlValue* value) {
12 g_autoptr(FlJsonMessageCodec) codec = fl_json_message_codec_new();
13 g_autoptr(GError) error = nullptr;
14 g_autofree gchar* result = fl_json_message_codec_encode(codec, value, &error);
15 EXPECT_EQ(error, nullptr);
16 return static_cast<gchar*>(g_steal_pointer(&result));
17 }
18
19 // Encodes a message using FlJsonMessageCodec to a UTF-8 string. Expect the
20 // given error.
21 static void encode_error_message(FlValue* value, GQuark domain, gint code) {
22 g_autoptr(FlJsonMessageCodec) codec = fl_json_message_codec_new();
23 g_autoptr(GError) error = nullptr;
24 g_autofree gchar* result = fl_json_message_codec_encode(codec, value, &error);
25 EXPECT_TRUE(g_error_matches(error, domain, code));
26 EXPECT_EQ(result, nullptr);
27 }
28
29 // Decodes a message using FlJsonMessageCodec from UTF-8 string.
30 static FlValue* decode_message(const char* text) {
31 g_autoptr(FlJsonMessageCodec) codec = fl_json_message_codec_new();
32 g_autoptr(GError) error = nullptr;
33 g_autoptr(FlValue) value = fl_json_message_codec_decode(codec, text, &error);
34 EXPECT_EQ(error, nullptr);
35 EXPECT_NE(value, nullptr);
36 return fl_value_ref(value);
37 }
38
39 // Decodes a message using FlJsonMessageCodec from UTF-8 string. Expect the
40 // given error.
41 static void decode_error_message(const char* text, GQuark domain, gint code) {
42 g_autoptr(FlJsonMessageCodec) codec = fl_json_message_codec_new();
43 g_autoptr(GError) error = nullptr;
44 g_autoptr(FlValue) value = fl_json_message_codec_decode(codec, text, &error);
45 EXPECT_TRUE(g_error_matches(error, domain, code));
46 EXPECT_EQ(value, nullptr);
47 }
48
49 TEST(FlJsonMessageCodecTest, EncodeNullptr) {
50 g_autofree gchar* text = encode_message(nullptr);
51 EXPECT_STREQ(text, "null");
52 }
53
54 TEST(FlJsonMessageCodecTest, EncodeNull) {
55 g_autoptr(FlValue) value = fl_value_new_null();
56 g_autofree gchar* text = encode_message(value);
57 EXPECT_STREQ(text, "null");
58 }
59
60 TEST(FlJsonMessageCodecTest, DecodeNull) {
61 g_autoptr(FlValue) value = decode_message("null");
62 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_NULL);
63 }
64
65 static gchar* encode_bool(gboolean value) {
66 g_autoptr(FlValue) v = fl_value_new_bool(value);
67 return encode_message(v);
68 }
69
70 TEST(FlJsonMessageCodecTest, EncodeBoolFalse) {
71 g_autofree gchar* text = encode_bool(FALSE);
72 EXPECT_STREQ(text, "false");
73 }
74
75 TEST(FlJsonMessageCodecTest, EncodeBoolTrue) {
76 g_autofree gchar* text = encode_bool(TRUE);
77 EXPECT_STREQ(text, "true");
78 }
79
80 TEST(FlJsonMessageCodecTest, DecodeBoolFalse) {
81 g_autoptr(FlValue) value = decode_message("false");
82 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_BOOL);
83 EXPECT_FALSE(fl_value_get_bool(value));
84 }
85
86 TEST(FlJsonMessageCodecTest, DecodeBoolTrue) {
87 g_autoptr(FlValue) value = decode_message("true");
88 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_BOOL);
89 EXPECT_TRUE(fl_value_get_bool(value));
90 }
91
92 static gchar* encode_int(int64_t value) {
93 g_autoptr(FlValue) v = fl_value_new_int(value);
94 return encode_message(v);
95 }
96
97 TEST(FlJsonMessageCodecTest, EncodeIntZero) {
98 g_autofree gchar* text = encode_int(0);
99 EXPECT_STREQ(text, "0");
100 }
101
102 TEST(FlJsonMessageCodecTest, EncodeIntOne) {
103 g_autofree gchar* text = encode_int(1);
104 EXPECT_STREQ(text, "1");
105 }
106
107 TEST(FlJsonMessageCodecTest, EncodeInt12345) {
108 g_autofree gchar* text = encode_int(12345);
109 EXPECT_STREQ(text, "12345");
110 }
111
112 TEST(FlJsonMessageCodecTest, EncodeIntMin) {
113 g_autofree gchar* text = encode_int(G_MININT64);
114 EXPECT_STREQ(text, "-9223372036854775808");
115 }
116
117 TEST(FlJsonMessageCodecTest, EncodeIntMax) {
118 g_autofree gchar* text = encode_int(G_MAXINT64);
119 EXPECT_STREQ(text, "9223372036854775807");
120 }
121
122 TEST(FlJsonMessageCodecTest, DecodeIntZero) {
123 g_autoptr(FlValue) value = decode_message("0");
124 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_INT);
125 EXPECT_EQ(fl_value_get_int(value), 0);
126 }
127
128 TEST(FlJsonMessageCodecTest, DecodeIntOne) {
129 g_autoptr(FlValue) value = decode_message("1");
130 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_INT);
131 EXPECT_EQ(fl_value_get_int(value), 1);
132 }
133
134 TEST(FlJsonMessageCodecTest, DecodeInt12345) {
135 g_autoptr(FlValue) value = decode_message("12345");
136 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_INT);
137 EXPECT_EQ(fl_value_get_int(value), 12345);
138 }
139
140 TEST(FlJsonMessageCodecTest, DecodeIntMin) {
141 g_autoptr(FlValue) value = decode_message("-9223372036854775808");
142 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_INT);
143 EXPECT_EQ(fl_value_get_int(value), G_MININT64);
144 }
145
146 TEST(FlJsonMessageCodecTest, DecodeIntMax) {
147 g_autoptr(FlValue) value = decode_message("9223372036854775807");
148 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_INT);
149 EXPECT_EQ(fl_value_get_int(value), G_MAXINT64);
150 }
151
152 TEST(FlJsonMessageCodecTest, DecodeUintMax) {
153 // This is bigger than an signed 64 bit integer, so we expect it to be
154 // represented as a double.
155 g_autoptr(FlValue) value = decode_message("18446744073709551615");
156 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_FLOAT);
157 EXPECT_EQ(fl_value_get_float(value), 1.8446744073709551615e+19);
158 }
159
160 TEST(FlJsonMessageCodecTest, DecodeHugeNumber) {
161 // This is bigger than an unsigned 64 bit integer, so we expect it to be
162 // represented as a double.
163 g_autoptr(FlValue) value = decode_message("184467440737095516150");
164 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_FLOAT);
165 EXPECT_EQ(fl_value_get_float(value), 1.84467440737095516150e+20);
166 }
167
168 TEST(FlJsonMessageCodecTest, DecodeIntLeadingZero1) {
169 decode_error_message("00", FL_JSON_MESSAGE_CODEC_ERROR,
170 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON);
171 }
172
173 TEST(FlJsonMessageCodecTest, DecodeIntLeadingZero2) {
174 decode_error_message("01", FL_JSON_MESSAGE_CODEC_ERROR,
175 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON);
176 }
177
178 TEST(FlJsonMessageCodecTest, DecodeIntDoubleNegative) {
179 decode_error_message("--1", FL_JSON_MESSAGE_CODEC_ERROR,
180 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON);
181 }
182
183 TEST(FlJsonMessageCodecTest, DecodeIntPositiveSign) {
184 decode_error_message("+1", FL_JSON_MESSAGE_CODEC_ERROR,
185 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON);
186 }
187
188 TEST(FlJsonMessageCodecTest, DecodeIntHexChar) {
189 decode_error_message("0a", FL_JSON_MESSAGE_CODEC_ERROR,
190 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON);
191 }
192
193 static gchar* encode_float(double value) {
194 g_autoptr(FlValue) v = fl_value_new_float(value);
195 return encode_message(v);
196 }
197
198 TEST(FlJsonMessageCodecTest, EncodeFloatZero) {
199 g_autofree gchar* text = encode_float(0);
200 EXPECT_STREQ(text, "0.0");
201 }
202
203 TEST(FlJsonMessageCodecTest, EncodeFloatOne) {
204 g_autofree gchar* text = encode_float(1);
205 EXPECT_STREQ(text, "1.0");
206 }
207
208 TEST(FlJsonMessageCodecTest, EncodeFloatMinusOne) {
209 g_autofree gchar* text = encode_float(-1);
210 EXPECT_STREQ(text, "-1.0");
211 }
212
213 TEST(FlJsonMessageCodecTest, EncodeFloatHalf) {
214 g_autofree gchar* text = encode_float(0.5);
215 EXPECT_STREQ(text, "0.5");
216 }
217
218 TEST(FlJsonMessageCodecTest, EncodeFloatPi) {
219 g_autofree gchar* text = encode_float(M_PI);
220 EXPECT_STREQ(text, "3.141592653589793");
221 }
222
223 TEST(FlJsonMessageCodecTest, EncodeFloatMinusZero) {
224 g_autofree gchar* text = encode_float(-0.0);
225 EXPECT_STREQ(text, "-0.0");
226 }
227
228 // NOTE(robert-ancell): JSON doesn't support encoding of NAN and INFINITY, but
229 // rapidjson doesn't seem to either encode them or treat them as an error.
230
231 TEST(FlJsonMessageCodecTest, DecodeFloatZero) {
232 g_autoptr(FlValue) value = decode_message("0.0");
233 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_FLOAT);
234 EXPECT_EQ(fl_value_get_float(value), 0.0);
235 }
236
237 TEST(FlJsonMessageCodecTest, DecodeFloatOne) {
238 g_autoptr(FlValue) value = decode_message("1.0");
239 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_FLOAT);
240 EXPECT_EQ(fl_value_get_float(value), 1.0);
241 }
242
243 TEST(FlJsonMessageCodecTest, DecodeFloatMinusOne) {
244 g_autoptr(FlValue) value = decode_message("-1.0");
245 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_FLOAT);
246 EXPECT_EQ(fl_value_get_float(value), -1.0);
247 }
248
249 TEST(FlJsonMessageCodecTest, DecodeFloatHalf) {
250 g_autoptr(FlValue) value = decode_message("0.5");
251 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_FLOAT);
252 EXPECT_EQ(fl_value_get_float(value), 0.5);
253 }
254
255 TEST(FlJsonMessageCodecTest, DecodeFloatPi) {
256 g_autoptr(FlValue) value = decode_message("3.1415926535897931");
257 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_FLOAT);
258 EXPECT_EQ(fl_value_get_float(value), M_PI);
259 }
260
261 TEST(FlJsonMessageCodecTest, DecodeFloatMinusZero) {
262 g_autoptr(FlValue) value = decode_message("-0.0");
263 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_FLOAT);
264 EXPECT_EQ(fl_value_get_float(value), -0.0);
265 }
266
267 TEST(FlJsonMessageCodecTest, DecodeFloatMissingFraction) {
268 decode_error_message("0.", FL_JSON_MESSAGE_CODEC_ERROR,
269 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON);
270 }
271
272 TEST(FlJsonMessageCodecTest, DecodeFloatInvalidFraction) {
273 decode_error_message("0.a", FL_JSON_MESSAGE_CODEC_ERROR,
274 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON);
275 }
276
277 static gchar* encode_string(const gchar* value) {
278 g_autoptr(FlValue) v = fl_value_new_string(value);
279 return encode_message(v);
280 }
281
282 TEST(FlJsonMessageCodecTest, EncodeStringEmpty) {
283 g_autofree gchar* text = encode_string("");
284 EXPECT_STREQ(text, "\"\"");
285 }
286
287 TEST(FlJsonMessageCodecTest, EncodeStringHello) {
288 g_autofree gchar* text = encode_string("hello");
289 EXPECT_STREQ(text, "\"hello\"");
290 }
291
292 TEST(FlJsonMessageCodecTest, EncodeStringEmptySized) {
293 g_autoptr(FlValue) value = fl_value_new_string_sized(nullptr, 0);
294 g_autofree gchar* text = encode_message(value);
295 EXPECT_STREQ(text, "\"\"");
296 }
297
298 TEST(FlJsonMessageCodecTest, EncodeStringHelloSized) {
299 g_autoptr(FlValue) value = fl_value_new_string_sized("Hello World", 5);
300 g_autofree gchar* text = encode_message(value);
301 EXPECT_STREQ(text, "\"Hello\"");
302 }
303
304 TEST(FlJsonMessageCodecTest, EncodeStringEscapeQuote) {
305 g_autofree gchar* text = encode_string("\"");
306 EXPECT_STREQ(text, "\"\\\"\"");
307 }
308
309 TEST(FlJsonMessageCodecTest, EncodeStringEscapeBackslash) {
310 g_autofree gchar* text = encode_string("\\");
311 EXPECT_STREQ(text, "\"\\\\\"");
312 }
313
314 TEST(FlJsonMessageCodecTest, EncodeStringEscapeBackspace) {
315 g_autofree gchar* text = encode_string("\b");
316 EXPECT_STREQ(text, "\"\\b\"");
317 }
318
319 TEST(FlJsonMessageCodecTest, EncodeStringEscapeFormFeed) {
320 g_autofree gchar* text = encode_string("\f");
321 EXPECT_STREQ(text, "\"\\f\"");
322 }
323
324 TEST(FlJsonMessageCodecTest, EncodeStringEscapeNewline) {
325 g_autofree gchar* text = encode_string("\n");
326 EXPECT_STREQ(text, "\"\\n\"");
327 }
328
329 TEST(FlJsonMessageCodecTest, EncodeStringEscapeCarriageReturn) {
330 g_autofree gchar* text = encode_string("\r");
331 EXPECT_STREQ(text, "\"\\r\"");
332 }
333
334 TEST(FlJsonMessageCodecTest, EncodeStringEscapeTab) {
335 g_autofree gchar* text = encode_string("\t");
336 EXPECT_STREQ(text, "\"\\t\"");
337 }
338
339 TEST(FlJsonMessageCodecTest, EncodeStringEscapeUnicode) {
340 g_autofree gchar* text = encode_string("\u0001");
341 EXPECT_STREQ(text, "\"\\u0001\"");
342 }
343
344 TEST(FlJsonMessageCodecTest, EncodeStringEmoji) {
345 g_autofree gchar* text = encode_string("😀");
346 EXPECT_STREQ(text, "\"😀\"");
347 }
348
349 TEST(FlJsonMessageCodecTest, DecodeStringEmpty) {
350 g_autoptr(FlValue) value = decode_message("\"\"");
351 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_STRING);
352 EXPECT_STREQ(fl_value_get_string(value), "");
353 }
354
355 TEST(FlJsonMessageCodecTest, DecodeStringHello) {
356 g_autoptr(FlValue) value = decode_message("\"hello\"");
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(FlJsonMessageCodecTest, DecodeStringEscapeQuote) {
362 g_autoptr(FlValue) value = decode_message("\"\\\"\"");
363 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_STRING);
364 EXPECT_STREQ(fl_value_get_string(value), "\"");
365 }
366
367 TEST(FlJsonMessageCodecTest, DecodeStringEscapeBackslash) {
368 g_autoptr(FlValue) value = decode_message("\"\\\\\"");
369 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_STRING);
370 EXPECT_STREQ(fl_value_get_string(value), "\\");
371 }
372
373 TEST(FlJsonMessageCodecTest, DecodeStringEscapeSlash) {
374 g_autoptr(FlValue) value = decode_message("\"\\/\"");
375 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_STRING);
376 EXPECT_STREQ(fl_value_get_string(value), "/");
377 }
378
379 TEST(FlJsonMessageCodecTest, DecodeStringEscapeBackspace) {
380 g_autoptr(FlValue) value = decode_message("\"\\b\"");
381 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_STRING);
382 EXPECT_STREQ(fl_value_get_string(value), "\b");
383 }
384
385 TEST(FlJsonMessageCodecTest, DecodeStringEscapeFormFeed) {
386 g_autoptr(FlValue) value = decode_message("\"\\f\"");
387 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_STRING);
388 EXPECT_STREQ(fl_value_get_string(value), "\f");
389 }
390
391 TEST(FlJsonMessageCodecTest, DecodeStringEscapeNewline) {
392 g_autoptr(FlValue) value = decode_message("\"\\n\"");
393 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_STRING);
394 EXPECT_STREQ(fl_value_get_string(value), "\n");
395 }
396
397 TEST(FlJsonMessageCodecTest, DecodeStringEscapeCarriageReturn) {
398 g_autoptr(FlValue) value = decode_message("\"\\r\"");
399 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_STRING);
400 EXPECT_STREQ(fl_value_get_string(value), "\r");
401 }
402
403 TEST(FlJsonMessageCodecTest, DecodeStringEscapeTab) {
404 g_autoptr(FlValue) value = decode_message("\"\\t\"");
405 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_STRING);
406 EXPECT_STREQ(fl_value_get_string(value), "\t");
407 }
408
409 TEST(FlJsonMessageCodecTest, DecodeStringEscapeUnicode) {
410 g_autoptr(FlValue) value = decode_message("\"\\u0001\"");
411 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_STRING);
412 EXPECT_STREQ(fl_value_get_string(value), "\u0001");
413 }
414
415 TEST(FlJsonMessageCodecTest, DecodeStringEmoji) {
416 g_autoptr(FlValue) value = decode_message("\"😀\"");
417 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_STRING);
418 EXPECT_STREQ(fl_value_get_string(value), "😀");
419 }
420
421 TEST(FlJsonMessageCodecTest, DecodeInvalidUTF8) {
422 decode_error_message("\xff", FL_JSON_MESSAGE_CODEC_ERROR,
423 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_UTF8);
424 }
425
426 TEST(FlJsonMessageCodecTest, DecodeStringInvalidUTF8) {
427 decode_error_message("\"\xff\"", FL_JSON_MESSAGE_CODEC_ERROR,
428 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_UTF8);
429 }
430
431 TEST(FlJsonMessageCodecTest, DecodeStringBinary) {
432 decode_error_message("\"Hello\x01World\"", FL_JSON_MESSAGE_CODEC_ERROR,
433 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON);
434 }
435
436 TEST(FlJsonMessageCodecTest, DecodeStringNewline) {
437 decode_error_message("\"Hello\nWorld\"", FL_JSON_MESSAGE_CODEC_ERROR,
438 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON);
439 }
440
441 TEST(FlJsonMessageCodecTest, DecodeStringCarriageReturn) {
442 decode_error_message("\"Hello\rWorld\"", FL_JSON_MESSAGE_CODEC_ERROR,
443 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON);
444 }
445
446 TEST(FlJsonMessageCodecTest, DecodeStringTab) {
447 decode_error_message("\"Hello\tWorld\"", FL_JSON_MESSAGE_CODEC_ERROR,
448 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON);
449 }
450
451 TEST(FlJsonMessageCodecTest, DecodeStringUnterminatedEmpty) {
452 decode_error_message("\"", FL_JSON_MESSAGE_CODEC_ERROR,
453 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON);
454 }
455
456 TEST(FlJsonMessageCodecTest, DecodeStringExtraQuote) {
457 decode_error_message("\"\"\"", FL_JSON_MESSAGE_CODEC_ERROR,
458 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON);
459 }
460
461 TEST(FlJsonMessageCodecTest, DecodeStringEscapedClosingQuote) {
462 decode_error_message("\"\\\"", FL_JSON_MESSAGE_CODEC_ERROR,
463 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON);
464 }
465
466 TEST(FlJsonMessageCodecTest, DecodeStringUnknownEscape) {
467 decode_error_message("\"\\z\"", FL_JSON_MESSAGE_CODEC_ERROR,
468 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON);
469 }
470
471 TEST(FlJsonMessageCodecTest, DecodeStringInvalidEscapeUnicode) {
472 decode_error_message("\"\\uxxxx\"", FL_JSON_MESSAGE_CODEC_ERROR,
473 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON);
474 }
475
476 TEST(FlJsonMessageCodecTest, DecodeStringEscapeUnicodeNoData) {
477 decode_error_message("\"\\u\"", FL_JSON_MESSAGE_CODEC_ERROR,
478 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON);
479 }
480
481 TEST(FlJsonMessageCodecTest, DecodeStringEscapeUnicodeShortData) {
482 decode_error_message("\"\\uxx\"", FL_JSON_MESSAGE_CODEC_ERROR,
483 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON);
484 }
485
486 TEST(FlJsonMessageCodecTest, EncodeUint8ListEmpty) {
487 g_autoptr(FlValue) value = fl_value_new_uint8_list(nullptr, 0);
488 g_autofree gchar* text = encode_message(value);
489 EXPECT_STREQ(text, "[]");
490 }
491
492 TEST(FlJsonMessageCodecTest, EncodeUint8List) {
493 uint8_t data[] = {0, 1, 2, 3, 4};
494 g_autoptr(FlValue) value = fl_value_new_uint8_list(data, 5);
495 g_autofree gchar* text = encode_message(value);
496 EXPECT_STREQ(text, "[0,1,2,3,4]");
497 }
498
499 TEST(FlJsonMessageCodecTest, EncodeInt32ListEmpty) {
500 g_autoptr(FlValue) value = fl_value_new_int32_list(nullptr, 0);
501 g_autofree gchar* text = encode_message(value);
502 EXPECT_STREQ(text, "[]");
503 }
504
505 TEST(FlJsonMessageCodecTest, EncodeInt32List) {
506 int32_t data[] = {0, -1, 2, -3, 4};
507 g_autoptr(FlValue) value = fl_value_new_int32_list(data, 5);
508 g_autofree gchar* text = encode_message(value);
509 EXPECT_STREQ(text, "[0,-1,2,-3,4]");
510 }
511
512 TEST(FlJsonMessageCodecTest, EncodeInt64ListEmpty) {
513 g_autoptr(FlValue) value = fl_value_new_int64_list(nullptr, 0);
514 g_autofree gchar* text = encode_message(value);
515 EXPECT_STREQ(text, "[]");
516 }
517
518 TEST(FlJsonMessageCodecTest, EncodeInt64List) {
519 int64_t data[] = {0, -1, 2, -3, 4};
520 g_autoptr(FlValue) value = fl_value_new_int64_list(data, 5);
521 g_autofree gchar* text = encode_message(value);
522 EXPECT_STREQ(text, "[0,-1,2,-3,4]");
523 }
524
525 TEST(FlJsonMessageCodecTest, EncodeFloatListEmpty) {
526 g_autoptr(FlValue) value = fl_value_new_float_list(nullptr, 0);
527 g_autofree gchar* text = encode_message(value);
528 EXPECT_STREQ(text, "[]");
529 }
530
531 TEST(FlJsonMessageCodecTest, EncodeFloatList) {
532 double data[] = {0, -0.5, 0.25, -0.125, 0.0625};
533 g_autoptr(FlValue) value = fl_value_new_float_list(data, 5);
534 g_autofree gchar* text = encode_message(value);
535 EXPECT_STREQ(text, "[0.0,-0.5,0.25,-0.125,0.0625]");
536 }
537
538 TEST(FlJsonMessageCodecTest, EncodeListEmpty) {
539 g_autoptr(FlValue) value = fl_value_new_list();
540 g_autofree gchar* text = encode_message(value);
541 EXPECT_STREQ(text, "[]");
542 }
543
544 TEST(FlJsonMessageCodecTest, EncodeListTypes) {
545 g_autoptr(FlValue) value = fl_value_new_list();
546 fl_value_append_take(value, fl_value_new_null());
547 fl_value_append_take(value, fl_value_new_bool(TRUE));
548 fl_value_append_take(value, fl_value_new_int(42));
549 fl_value_append_take(value, fl_value_new_float(-1.5));
550 fl_value_append_take(value, fl_value_new_string("hello"));
551 fl_value_append_take(value, fl_value_new_list());
552 fl_value_append_take(value, fl_value_new_map());
553 g_autofree gchar* text = encode_message(value);
554 EXPECT_STREQ(text, "[null,true,42,-1.5,\"hello\",[],{}]");
555 }
556
557 TEST(FlJsonMessageCodecTest, EncodeListNested) {
558 g_autoptr(FlValue) even_numbers = fl_value_new_list();
559 g_autoptr(FlValue) odd_numbers = fl_value_new_list();
560 for (int i = 0; i < 10; i++) {
561 if (i % 2 == 0) {
562 fl_value_append_take(even_numbers, fl_value_new_int(i));
563 } else {
564 fl_value_append_take(odd_numbers, fl_value_new_int(i));
565 }
566 }
567 g_autoptr(FlValue) value = fl_value_new_list();
568 fl_value_append(value, even_numbers);
569 fl_value_append(value, odd_numbers);
570 g_autofree gchar* text = encode_message(value);
571 EXPECT_STREQ(text, "[[0,2,4,6,8],[1,3,5,7,9]]");
572 }
573
574 TEST(FlJsonMessageCodecTest, DecodeListEmpty) {
575 g_autoptr(FlValue) value = decode_message("[]");
576 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_LIST);
577 EXPECT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
578 }
579
580 TEST(FlJsonMessageCodecTest, DecodeListNoComma) {
581 decode_error_message("[0,1,2,3 4]", FL_JSON_MESSAGE_CODEC_ERROR,
582 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON);
583 }
584
585 TEST(FlJsonMessageCodecTest, DecodeListUnterminatedEmpty) {
586 decode_error_message("[", FL_JSON_MESSAGE_CODEC_ERROR,
587 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON);
588 }
589
590 TEST(FlJsonMessageCodecTest, DecodeListStartUnterminate) {
591 decode_error_message("]", FL_JSON_MESSAGE_CODEC_ERROR,
592 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON);
593 }
594
595 TEST(FlJsonMessageCodecTest, DecodeListUnterminated) {
596 decode_error_message("[0,1,2,3,4", FL_JSON_MESSAGE_CODEC_ERROR,
597 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON);
598 }
599
600 TEST(FlJsonMessageCodecTest, DecodeListDoubleTerminated) {
601 decode_error_message("[0,1,2,3,4]]", FL_JSON_MESSAGE_CODEC_ERROR,
602 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON);
603 }
604
605 TEST(FlJsonMessageCodecTest, EncodeMapEmpty) {
606 g_autoptr(FlValue) value = fl_value_new_map();
607 g_autofree gchar* text = encode_message(value);
608 EXPECT_STREQ(text, "{}");
609 }
610
611 TEST(FlJsonMessageCodecTest, EncodeMapNullKey) {
612 g_autoptr(FlValue) value = fl_value_new_map();
613 fl_value_set_take(value, fl_value_new_null(), fl_value_new_string("null"));
614 encode_error_message(value, FL_JSON_MESSAGE_CODEC_ERROR,
615 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_OBJECT_KEY_TYPE);
616 }
617
618 TEST(FlJsonMessageCodecTest, EncodeMapBoolKey) {
619 g_autoptr(FlValue) value = fl_value_new_map();
620 fl_value_set_take(value, fl_value_new_bool(TRUE),
621 fl_value_new_string("bool"));
622 encode_error_message(value, FL_JSON_MESSAGE_CODEC_ERROR,
623 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_OBJECT_KEY_TYPE);
624 }
625
626 TEST(FlJsonMessageCodecTest, EncodeMapIntKey) {
627 g_autoptr(FlValue) value = fl_value_new_map();
628 fl_value_set_take(value, fl_value_new_int(42), fl_value_new_string("int"));
629 encode_error_message(value, FL_JSON_MESSAGE_CODEC_ERROR,
630 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_OBJECT_KEY_TYPE);
631 }
632
633 TEST(FlJsonMessageCodecTest, EncodeMapFloatKey) {
634 g_autoptr(FlValue) value = fl_value_new_map();
635 fl_value_set_take(value, fl_value_new_float(M_PI),
636 fl_value_new_string("float"));
637 encode_error_message(value, FL_JSON_MESSAGE_CODEC_ERROR,
638 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_OBJECT_KEY_TYPE);
639 }
640
641 TEST(FlJsonMessageCodecTest, EncodeMapUint8ListKey) {
642 g_autoptr(FlValue) value = fl_value_new_map();
643 fl_value_set_take(value, fl_value_new_uint8_list(nullptr, 0),
644 fl_value_new_string("uint8_list"));
645 encode_error_message(value, FL_JSON_MESSAGE_CODEC_ERROR,
646 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_OBJECT_KEY_TYPE);
647 }
648
649 TEST(FlJsonMessageCodecTest, EncodeMapInt32ListKey) {
650 g_autoptr(FlValue) value = fl_value_new_map();
651 fl_value_set_take(value, fl_value_new_int32_list(nullptr, 0),
652 fl_value_new_string("int32_list"));
653 encode_error_message(value, FL_JSON_MESSAGE_CODEC_ERROR,
654 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_OBJECT_KEY_TYPE);
655 }
656
657 TEST(FlJsonMessageCodecTest, EncodeMapInt64ListKey) {
658 g_autoptr(FlValue) value = fl_value_new_map();
659 fl_value_set_take(value, fl_value_new_int64_list(nullptr, 0),
660 fl_value_new_string("int64_list"));
661 encode_error_message(value, FL_JSON_MESSAGE_CODEC_ERROR,
662 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_OBJECT_KEY_TYPE);
663 }
664
665 TEST(FlJsonMessageCodecTest, EncodeMapFloatListKey) {
666 g_autoptr(FlValue) value = fl_value_new_map();
667 fl_value_set_take(value, fl_value_new_float_list(nullptr, 0),
668 fl_value_new_string("float_list"));
669 encode_error_message(value, FL_JSON_MESSAGE_CODEC_ERROR,
670 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_OBJECT_KEY_TYPE);
671 }
672
673 TEST(FlJsonMessageCodecTest, EncodeMapListKey) {
674 g_autoptr(FlValue) value = fl_value_new_map();
675 fl_value_set_take(value, fl_value_new_list(), fl_value_new_string("list"));
676 encode_error_message(value, FL_JSON_MESSAGE_CODEC_ERROR,
677 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_OBJECT_KEY_TYPE);
678 }
679
680 TEST(FlJsonMessageCodecTest, EncodeMapMapKey) {
681 g_autoptr(FlValue) value = fl_value_new_map();
682 fl_value_set_take(value, fl_value_new_map(), fl_value_new_string("map"));
683 encode_error_message(value, FL_JSON_MESSAGE_CODEC_ERROR,
684 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_OBJECT_KEY_TYPE);
685 }
686
687 TEST(FlJsonMessageCodecTest, EncodeMapValueTypes) {
688 g_autoptr(FlValue) value = fl_value_new_map();
689 fl_value_set_take(value, fl_value_new_string("null"), fl_value_new_null());
690 fl_value_set_take(value, fl_value_new_string("bool"),
691 fl_value_new_bool(TRUE));
692 fl_value_set_take(value, fl_value_new_string("int"), fl_value_new_int(42));
693 fl_value_set_take(value, fl_value_new_string("float"),
694 fl_value_new_float(-1.5));
695 fl_value_set_take(value, fl_value_new_string("string"),
696 fl_value_new_string("hello"));
697 fl_value_set_take(value, fl_value_new_string("list"), fl_value_new_list());
698 fl_value_set_take(value, fl_value_new_string("map"), fl_value_new_map());
699 g_autofree gchar* text = encode_message(value);
700 EXPECT_STREQ(text,
701 "{\"null\":null,\"bool\":true,\"int\":42,\"float\":-"
702 "1.5,\"string\":\"hello\",\"list\":[],\"map\":{}}");
703 }
704
705 TEST(FlJsonMessageCodecTest, EncodeMapNested) {
706 g_autoptr(FlValue) str_to_int = fl_value_new_map();
707 const char* numbers[] = {"zero", "one", "two", "three", nullptr};
708 for (int i = 0; numbers[i] != nullptr; i++) {
709 fl_value_set_take(str_to_int, fl_value_new_string(numbers[i]),
710 fl_value_new_int(i));
711 }
712 g_autoptr(FlValue) value = fl_value_new_map();
713 fl_value_set_string(value, "str-to-int", str_to_int);
714 g_autofree gchar* text = encode_message(value);
715 EXPECT_STREQ(text,
716 "{\"str-to-int\":{\"zero\":0,\"one\":1,\"two\":2,\"three\":3}}");
717 }
718
719 TEST(FlJsonMessageCodecTest, DecodeMapEmpty) {
720 g_autoptr(FlValue) value = decode_message("{}");
721 ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_MAP);
722 EXPECT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
723 }
724
725 TEST(FlJsonMessageCodecTest, DecodeMapUnterminatedEmpty) {
726 decode_error_message("{", FL_JSON_MESSAGE_CODEC_ERROR,
727 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON);
728 }
729
730 TEST(FlJsonMessageCodecTest, DecodeMapStartUnterminate) {
731 decode_error_message("}", FL_JSON_MESSAGE_CODEC_ERROR,
732 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON);
733 }
734
735 TEST(FlJsonMessageCodecTest, DecodeMapNoComma) {
736 decode_error_message("{\"zero\":0 \"one\":1}", FL_JSON_MESSAGE_CODEC_ERROR,
737 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON);
738 }
739
740 TEST(FlJsonMessageCodecTest, DecodeMapNoColon) {
741 decode_error_message("{\"zero\" 0,\"one\":1}", FL_JSON_MESSAGE_CODEC_ERROR,
742 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON);
743 }
744
745 TEST(FlJsonMessageCodecTest, DecodeMapUnterminated) {
746 decode_error_message("{\"zero\":0,\"one\":1", FL_JSON_MESSAGE_CODEC_ERROR,
747 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON);
748 }
749
750 TEST(FlJsonMessageCodecTest, DecodeMapDoubleTerminated) {
751 decode_error_message("{\"zero\":0,\"one\":1}}", FL_JSON_MESSAGE_CODEC_ERROR,
752 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON);
753 }
754
755 TEST(FlJsonMessageCodecTest, DecodeUnknownWord) {
756 decode_error_message("foo", FL_JSON_MESSAGE_CODEC_ERROR,
757 FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON);
758 }
759
760 TEST(FlJsonMessageCodecTest, EncodeDecode) {
761 g_autoptr(FlJsonMessageCodec) codec = fl_json_message_codec_new();
762
763 g_autoptr(FlValue) input = fl_value_new_list();
764 fl_value_append_take(input, fl_value_new_null());
765 fl_value_append_take(input, fl_value_new_bool(TRUE));
766 fl_value_append_take(input, fl_value_new_int(42));
767 fl_value_append_take(input, fl_value_new_float(M_PI));
768 fl_value_append_take(input, fl_value_new_string("hello"));
769 fl_value_append_take(input, fl_value_new_list());
770 fl_value_append_take(input, fl_value_new_map());
771
772 g_autoptr(GError) error = nullptr;
773 g_autofree gchar* message =
774 fl_json_message_codec_encode(codec, input, &error);
775 ASSERT_NE(message, nullptr);
776 EXPECT_EQ(error, nullptr);
777
778 g_autoptr(FlValue) output =
779 fl_json_message_codec_decode(codec, message, &error);
780 EXPECT_EQ(error, nullptr);
781 EXPECT_NE(output, nullptr);
782
783 EXPECT_TRUE(fl_value_equal(input, output));
784 }
fl_json_message_codec_encode
G_MODULE_EXPORT gchar * fl_json_message_codec_encode(FlJsonMessageCodec *codec, FlValue *value, GError **error)
Definition: fl_json_message_codec.cc:311
fl_json_message_codec_decode
G_MODULE_EXPORT FlValue * fl_json_message_codec_decode(FlJsonMessageCodec *codec, const gchar *text, GError **error)
Definition: fl_json_message_codec.cc:326
fl_json_message_codec_new
G_MODULE_EXPORT FlJsonMessageCodec * fl_json_message_codec_new()
Definition: fl_json_message_codec.cc:306
fl_json_message_codec.h
FL_JSON_MESSAGE_CODEC_ERROR_INVALID_OBJECT_KEY_TYPE
@ FL_JSON_MESSAGE_CODEC_ERROR_INVALID_OBJECT_KEY_TYPE
Definition: fl_json_message_codec.h:32
FL_JSON_MESSAGE_CODEC_ERROR_INVALID_UTF8
@ FL_JSON_MESSAGE_CODEC_ERROR_INVALID_UTF8
Definition: fl_json_message_codec.h:30
FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON
@ FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON
Definition: fl_json_message_codec.h:31
FL_JSON_MESSAGE_CODEC_ERROR
#define FL_JSON_MESSAGE_CODEC_ERROR
Definition: fl_json_message_codec.h:27
TEST
TEST(FlJsonMessageCodecTest, EncodeNullptr)
Definition: fl_json_message_codec_test.cc:49
decode_message
static FlValue * decode_message(const char *text)
Definition: fl_json_message_codec_test.cc:30
encode_string
static gchar * encode_string(const gchar *value)
Definition: fl_json_message_codec_test.cc:277
encode_message
static gchar * encode_message(FlValue *value)
Definition: fl_json_message_codec_test.cc:11
encode_float
static gchar * encode_float(double value)
Definition: fl_json_message_codec_test.cc:193
encode_bool
static gchar * encode_bool(gboolean value)
Definition: fl_json_message_codec_test.cc:65
encode_int
static gchar * encode_int(int64_t value)
Definition: fl_json_message_codec_test.cc:92
encode_error_message
static void encode_error_message(FlValue *value, GQuark domain, gint code)
Definition: fl_json_message_codec_test.cc:21
decode_error_message
static void decode_error_message(const char *text, GQuark domain, gint code)
Definition: fl_json_message_codec_test.cc:41
TRUE
return TRUE
Definition: fl_pixel_buffer_texture_test.cc:53
error
const uint8_t uint32_t uint32_t GError ** error
Definition: fl_pixel_buffer_texture_test.cc:40
i
int i
Definition: fl_socket_accessible.cc:18
value
uint8_t value
Definition: fl_standard_message_codec.cc:36
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_set_string
G_MODULE_EXPORT void fl_value_set_string(FlValue *self, const gchar *key, FlValue *value)
Definition: fl_value.cc:639
fl_value_new_float
G_MODULE_EXPORT FlValue * fl_value_new_float(double value)
Definition: fl_value.cc:269
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
fl_value_get_int
G_MODULE_EXPORT int64_t fl_value_get_int(FlValue *self)
Definition: fl_value.cc:668
fl_value_new_string
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:276
fl_value_get_type
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:466
fl_value_new_bool
G_MODULE_EXPORT FlValue * fl_value_new_bool(bool value)
Definition: fl_value.cc:255
fl_value_ref
G_MODULE_EXPORT FlValue * fl_value_ref(FlValue *self)
Definition: fl_value.cc:394
fl_value_append
G_MODULE_EXPORT void fl_value_append(FlValue *self, FlValue *value)
Definition: fl_value.cc:592
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_list
G_MODULE_EXPORT FlValue * fl_value_new_list()
Definition: fl_value.cc:349
fl_value_append_take
G_MODULE_EXPORT void fl_value_append_take(FlValue *self, FlValue *value)
Definition: fl_value.cc:600
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_get_bool
G_MODULE_EXPORT bool fl_value_get_bool(FlValue *self)
Definition: fl_value.cc:661
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_null
G_MODULE_EXPORT FlValue * fl_value_new_null()
Definition: fl_value.cc:251
fl_value_equal
G_MODULE_EXPORT bool fl_value_equal(FlValue *a, FlValue *b)
Definition: fl_value.cc:471
fl_value_get_string
G_MODULE_EXPORT const gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:682
fl_value_new_int
G_MODULE_EXPORT FlValue * fl_value_new_int(int64_t value)
Definition: fl_value.cc:262
fl_value_new_map
G_MODULE_EXPORT FlValue * fl_value_new_map()
Definition: fl_value.cc:366
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_value_get_float
G_MODULE_EXPORT double fl_value_get_float(FlValue *self)
Definition: fl_value.cc:675
fl_value_get_length
G_MODULE_EXPORT size_t fl_value_get_length(FlValue *self)
Definition: fl_value.cc:724
FlValue
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
FL_VALUE_TYPE_STRING
@ FL_VALUE_TYPE_STRING
Definition: fl_value.h:68
FL_VALUE_TYPE_NULL
@ FL_VALUE_TYPE_NULL
Definition: fl_value.h:64
FL_VALUE_TYPE_INT
@ FL_VALUE_TYPE_INT
Definition: fl_value.h:66
FL_VALUE_TYPE_BOOL
@ FL_VALUE_TYPE_BOOL
Definition: fl_value.h:65
FL_VALUE_TYPE_LIST
@ FL_VALUE_TYPE_LIST
Definition: fl_value.h:73
FL_VALUE_TYPE_MAP
@ FL_VALUE_TYPE_MAP
Definition: fl_value.h:74
FL_VALUE_TYPE_FLOAT
@ FL_VALUE_TYPE_FLOAT
Definition: fl_value.h:67