|
| 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_binary_codec.h" |
| 6 | +#include "gtest/gtest.h" |
| 7 | + |
| 8 | +static uint8_t hex_digit_to_int(char value) { |
| 9 | + if (value >= '0' && value <= '9') |
| 10 | + return value - '0'; |
| 11 | + else if (value >= 'a' && value <= 'f') |
| 12 | + return value - 'a' + 10; |
| 13 | + else if (value >= 'F' && value <= 'F') |
| 14 | + return value - 'A' + 10; |
| 15 | + else |
| 16 | + return 0; |
| 17 | +} |
| 18 | + |
| 19 | +static uint8_t parse_hex8(const gchar* hex_string) { |
| 20 | + if (hex_string[0] == '\0') |
| 21 | + return 0x00; |
| 22 | + return hex_digit_to_int(hex_string[0]) << 4 | hex_digit_to_int(hex_string[1]); |
| 23 | +} |
| 24 | + |
| 25 | +static GBytes* hex_string_to_bytes(const gchar* hex_string) { |
| 26 | + GByteArray* buffer = g_byte_array_new(); |
| 27 | + for (int i = 0; hex_string[i] != '\0' && hex_string[i + 1] != '\0'; i += 2) { |
| 28 | + uint8_t value = parse_hex8(hex_string + i); |
| 29 | + g_byte_array_append(buffer, &value, 1); |
| 30 | + } |
| 31 | + return g_byte_array_free_to_bytes(buffer); |
| 32 | +} |
| 33 | + |
| 34 | +static gchar* encode_value(FlValue* value) { |
| 35 | + g_autoptr(FlBinaryCodec) codec = fl_binary_codec_new(); |
| 36 | + g_autoptr(GByteArray) buffer = g_byte_array_new(); |
| 37 | + g_autoptr(GError) error = nullptr; |
| 38 | + gboolean result = |
| 39 | + fl_codec_write_value(FL_CODEC(codec), buffer, value, &error); |
| 40 | + EXPECT_TRUE(result); |
| 41 | + EXPECT_EQ(error, nullptr); |
| 42 | + |
| 43 | + GString* hex_string = g_string_new(""); |
| 44 | + for (guint i = 0; i < buffer->len; i++) |
| 45 | + g_string_append_printf(hex_string, "%02x", buffer->data[i]); |
| 46 | + return g_string_free(hex_string, FALSE); |
| 47 | +} |
| 48 | + |
| 49 | +static void encode_value_error(FlValue* value, GQuark domain, int code) { |
| 50 | + g_autoptr(FlBinaryCodec) codec = fl_binary_codec_new(); |
| 51 | + g_autoptr(GByteArray) buffer = g_byte_array_new(); |
| 52 | + g_autoptr(GError) error = nullptr; |
| 53 | + gboolean result = |
| 54 | + fl_codec_write_value(FL_CODEC(codec), buffer, value, &error); |
| 55 | + EXPECT_FALSE(result); |
| 56 | + EXPECT_TRUE(g_error_matches(error, domain, code)); |
| 57 | + EXPECT_EQ(buffer->len, static_cast<guint>(0)); |
| 58 | +} |
| 59 | + |
| 60 | +static FlValue* decode_value(const char* hex_string) { |
| 61 | + g_autoptr(FlBinaryCodec) codec = fl_binary_codec_new(); |
| 62 | + g_autoptr(GBytes) data = hex_string_to_bytes(hex_string); |
| 63 | + size_t offset = 0; |
| 64 | + g_autoptr(GError) error = nullptr; |
| 65 | + g_autoptr(FlValue) value = |
| 66 | + fl_codec_read_value(FL_CODEC(codec), data, &offset, &error); |
| 67 | + EXPECT_EQ(offset, g_bytes_get_size(data)); |
| 68 | + EXPECT_EQ(error, nullptr); |
| 69 | + EXPECT_NE(value, nullptr); |
| 70 | + return fl_value_ref(value); |
| 71 | +} |
| 72 | + |
| 73 | +TEST(FlBinaryCodecTest, EncodeData) { |
| 74 | + uint8_t data[] = {0x00, 0x01, 0x02, 0xFD, 0xFE, 0xFF}; |
| 75 | + g_autoptr(FlValue) value = fl_value_uint8_list_new(data, 6); |
| 76 | + g_autofree gchar* hex_string = encode_value(value); |
| 77 | + EXPECT_STREQ(hex_string, "000102fdfeff"); |
| 78 | +} |
| 79 | + |
| 80 | +TEST(FlBinaryCodecTest, EncodeEmpty) { |
| 81 | + g_autoptr(FlValue) value = fl_value_uint8_list_new(nullptr, 0); |
| 82 | + g_autofree gchar* hex_string = encode_value(value); |
| 83 | + EXPECT_STREQ(hex_string, ""); |
| 84 | +} |
| 85 | + |
| 86 | +TEST(FlBinaryCodecTest, EncodeNULL) { |
| 87 | + encode_value_error(nullptr, FL_CODEC_ERROR, FL_CODEC_ERROR_UNSUPPORTED_TYPE); |
| 88 | +} |
| 89 | + |
| 90 | +TEST(FlBinaryCodecTest, EncodeUnknownType) { |
| 91 | + g_autoptr(FlValue) value = fl_value_null_new(); |
| 92 | + encode_value_error(value, FL_CODEC_ERROR, FL_CODEC_ERROR_UNSUPPORTED_TYPE); |
| 93 | +} |
| 94 | + |
| 95 | +TEST(FlBinaryCodecTest, DecodeData) { |
| 96 | + g_autoptr(FlValue) value = decode_value("000102fdfeff"); |
| 97 | + ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_UINT8_LIST); |
| 98 | + ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(6)); |
| 99 | + EXPECT_EQ(fl_value_get_uint8_list(value)[0], 0x00); |
| 100 | + EXPECT_EQ(fl_value_get_uint8_list(value)[1], 0x01); |
| 101 | + EXPECT_EQ(fl_value_get_uint8_list(value)[2], 0x02); |
| 102 | + EXPECT_EQ(fl_value_get_uint8_list(value)[3], 0xFD); |
| 103 | + EXPECT_EQ(fl_value_get_uint8_list(value)[4], 0xFE); |
| 104 | + EXPECT_EQ(fl_value_get_uint8_list(value)[5], 0xFF); |
| 105 | +} |
| 106 | + |
| 107 | +TEST(FlBinaryCodecTest, DecodeEmpty) { |
| 108 | + g_autoptr(FlValue) value = decode_value(""); |
| 109 | + ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_UINT8_LIST); |
| 110 | + ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(0)); |
| 111 | +} |
0 commit comments