|
| 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 "flutter/shell/platform/linux/testing/fl_test.h" |
| 7 | +#include "gtest/gtest.h" |
| 8 | + |
| 9 | +// Encode a message using a FlBinaryCodec. Return a hex string with the encoded |
| 10 | +// binary output. |
| 11 | +static gchar* encode_message(FlValue* value) { |
| 12 | + g_autoptr(FlBinaryCodec) codec = fl_binary_codec_new(); |
| 13 | + g_autoptr(GError) error = nullptr; |
| 14 | + g_autoptr(GBytes) message = |
| 15 | + fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), value, &error); |
| 16 | + EXPECT_NE(message, nullptr); |
| 17 | + EXPECT_EQ(error, nullptr); |
| 18 | + |
| 19 | + return bytes_to_hex_string(message); |
| 20 | +} |
| 21 | + |
| 22 | +// Encode a message using a FlBinaryCodec. Expect the given error. |
| 23 | +static void encode_message_error(FlValue* value, GQuark domain, int code) { |
| 24 | + g_autoptr(FlBinaryCodec) codec = fl_binary_codec_new(); |
| 25 | + g_autoptr(GError) error = nullptr; |
| 26 | + g_autoptr(GBytes) message = |
| 27 | + fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), value, &error); |
| 28 | + EXPECT_EQ(message, nullptr); |
| 29 | + EXPECT_TRUE(g_error_matches(error, domain, code)); |
| 30 | +} |
| 31 | + |
| 32 | +// Decode a message using a FlBinaryCodec. The binary data is given in the form |
| 33 | +// of a hex string. |
| 34 | +static FlValue* decode_message(const char* hex_string) { |
| 35 | + g_autoptr(FlBinaryCodec) codec = fl_binary_codec_new(); |
| 36 | + g_autoptr(GBytes) data = hex_string_to_bytes(hex_string); |
| 37 | + g_autoptr(GError) error = nullptr; |
| 38 | + g_autoptr(FlValue) value = |
| 39 | + fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), data, &error); |
| 40 | + EXPECT_EQ(error, nullptr); |
| 41 | + EXPECT_NE(value, nullptr); |
| 42 | + return fl_value_ref(value); |
| 43 | +} |
| 44 | + |
| 45 | +TEST(FlBinaryCodecTest, EncodeData) { |
| 46 | + uint8_t data[] = {0x00, 0x01, 0x02, 0xFD, 0xFE, 0xFF}; |
| 47 | + g_autoptr(FlValue) value = fl_value_new_uint8_list(data, 6); |
| 48 | + g_autofree gchar* hex_string = encode_message(value); |
| 49 | + EXPECT_STREQ(hex_string, "000102fdfeff"); |
| 50 | +} |
| 51 | + |
| 52 | +TEST(FlBinaryCodecTest, EncodeEmpty) { |
| 53 | + g_autoptr(FlValue) value = fl_value_new_uint8_list(nullptr, 0); |
| 54 | + g_autofree gchar* hex_string = encode_message(value); |
| 55 | + EXPECT_STREQ(hex_string, ""); |
| 56 | +} |
| 57 | + |
| 58 | +TEST(FlBinaryCodecTest, EncodeNULL) { |
| 59 | + encode_message_error(nullptr, FL_MESSAGE_CODEC_ERROR, |
| 60 | + FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE); |
| 61 | +} |
| 62 | + |
| 63 | +TEST(FlBinaryCodecTest, EncodeUnknownType) { |
| 64 | + g_autoptr(FlValue) value = fl_value_new_null(); |
| 65 | + encode_message_error(value, FL_MESSAGE_CODEC_ERROR, |
| 66 | + FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE); |
| 67 | +} |
| 68 | + |
| 69 | +TEST(FlBinaryCodecTest, DecodeData) { |
| 70 | + g_autoptr(FlValue) value = decode_message("000102fdfeff"); |
| 71 | + ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_UINT8_LIST); |
| 72 | + ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(6)); |
| 73 | + EXPECT_EQ(fl_value_get_uint8_list(value)[0], 0x00); |
| 74 | + EXPECT_EQ(fl_value_get_uint8_list(value)[1], 0x01); |
| 75 | + EXPECT_EQ(fl_value_get_uint8_list(value)[2], 0x02); |
| 76 | + EXPECT_EQ(fl_value_get_uint8_list(value)[3], 0xFD); |
| 77 | + EXPECT_EQ(fl_value_get_uint8_list(value)[4], 0xFE); |
| 78 | + EXPECT_EQ(fl_value_get_uint8_list(value)[5], 0xFF); |
| 79 | +} |
| 80 | + |
| 81 | +TEST(FlBinaryCodecTest, DecodeEmpty) { |
| 82 | + g_autoptr(FlValue) value = decode_message(""); |
| 83 | + ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_UINT8_LIST); |
| 84 | + ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(0)); |
| 85 | +} |
| 86 | + |
| 87 | +TEST(FlBinaryCodecTest, EncodeDecode) { |
| 88 | + g_autoptr(FlBinaryCodec) codec = fl_binary_codec_new(); |
| 89 | + |
| 90 | + uint8_t data[] = {0x00, 0x01, 0x02, 0xFD, 0xFE, 0xFF}; |
| 91 | + g_autoptr(FlValue) input = fl_value_new_uint8_list(data, 6); |
| 92 | + |
| 93 | + g_autoptr(GError) error = nullptr; |
| 94 | + g_autoptr(GBytes) message = |
| 95 | + fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), input, &error); |
| 96 | + EXPECT_NE(message, nullptr); |
| 97 | + EXPECT_EQ(error, nullptr); |
| 98 | + |
| 99 | + g_autoptr(FlValue) output = |
| 100 | + fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), message, &error); |
| 101 | + EXPECT_EQ(error, nullptr); |
| 102 | + EXPECT_NE(output, nullptr); |
| 103 | + |
| 104 | + ASSERT_TRUE(fl_value_equal(input, output)); |
| 105 | +} |
0 commit comments