|
| 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_codec.h" |
| 6 | +#include "gtest/gtest.h" |
| 7 | + |
| 8 | +G_DECLARE_FINAL_TYPE(FlTestCodec, fl_test_codec, FL, TEST_CODEC, FlCodec) |
| 9 | + |
| 10 | +struct _FlTestCodec { |
| 11 | + FlCodec parent_instance; |
| 12 | +}; |
| 13 | + |
| 14 | +G_DEFINE_TYPE(FlTestCodec, fl_test_codec, fl_codec_get_type()) |
| 15 | + |
| 16 | +static gboolean fl_test_codec_write_value(FlCodec* codec, |
| 17 | + GByteArray* buffer, |
| 18 | + FlValue* value, |
| 19 | + GError** error) { |
| 20 | + EXPECT_TRUE(FL_IS_TEST_CODEC(codec)); |
| 21 | + |
| 22 | + if (fl_value_get_type(value) == FL_VALUE_TYPE_INT) { |
| 23 | + char c = '0' + fl_value_get_int(value); |
| 24 | + g_byte_array_append(buffer, reinterpret_cast<const guint8*>(&c), 1); |
| 25 | + return TRUE; |
| 26 | + } else { |
| 27 | + g_set_error(error, FL_CODEC_ERROR, FL_CODEC_ERROR_FAILED, "ERROR"); |
| 28 | + return FALSE; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +static FlValue* fl_test_codec_read_value(FlCodec* codec, |
| 33 | + GBytes* message, |
| 34 | + size_t* offset, |
| 35 | + GError** error) { |
| 36 | + EXPECT_TRUE(FL_IS_TEST_CODEC(codec)); |
| 37 | + EXPECT_TRUE(*offset < g_bytes_get_size(message)); |
| 38 | + |
| 39 | + size_t data_length; |
| 40 | + const uint8_t* data = |
| 41 | + static_cast<const uint8_t*>(g_bytes_get_data(message, &data_length)); |
| 42 | + if (data_length == 0) { |
| 43 | + g_set_error(error, FL_CODEC_ERROR, FL_CODEC_ERROR_FAILED, "ERROR"); |
| 44 | + return FALSE; |
| 45 | + } |
| 46 | + |
| 47 | + g_autoptr(FlValue) value = fl_value_int_new(data[*offset] - '0'); |
| 48 | + (*offset)++; |
| 49 | + return fl_value_ref(value); |
| 50 | +} |
| 51 | + |
| 52 | +static void fl_test_codec_class_init(FlTestCodecClass* klass) { |
| 53 | + FL_CODEC_CLASS(klass)->write_value = fl_test_codec_write_value; |
| 54 | + FL_CODEC_CLASS(klass)->read_value = fl_test_codec_read_value; |
| 55 | +} |
| 56 | + |
| 57 | +static void fl_test_codec_init(FlTestCodec* self) {} |
| 58 | + |
| 59 | +static FlTestCodec* fl_test_codec_new() { |
| 60 | + return FL_TEST_CODEC(g_object_new(fl_test_codec_get_type(), nullptr)); |
| 61 | +} |
| 62 | + |
| 63 | +TEST(FlCodecTest, WriteValue) { |
| 64 | + g_autoptr(FlTestCodec) codec = fl_test_codec_new(); |
| 65 | + g_autoptr(GByteArray) buffer = g_byte_array_new(); |
| 66 | + |
| 67 | + g_autoptr(FlValue) value = fl_value_int_new(1); |
| 68 | + g_autoptr(GError) error = nullptr; |
| 69 | + gboolean result = |
| 70 | + fl_codec_write_value(FL_CODEC(codec), buffer, value, &error); |
| 71 | + EXPECT_TRUE(result); |
| 72 | + EXPECT_EQ(error, nullptr); |
| 73 | + EXPECT_EQ(buffer->len, static_cast<unsigned int>(1)); |
| 74 | + EXPECT_EQ(buffer->data[0], '1'); |
| 75 | +} |
| 76 | + |
| 77 | +TEST(FlCodecTest, WriteValues) { |
| 78 | + g_autoptr(FlTestCodec) codec = fl_test_codec_new(); |
| 79 | + g_autoptr(GByteArray) buffer = g_byte_array_new(); |
| 80 | + |
| 81 | + for (int i = 1; i <= 5; i++) { |
| 82 | + g_autoptr(FlValue) value = fl_value_int_new(i); |
| 83 | + g_autoptr(GError) error = nullptr; |
| 84 | + gboolean result = |
| 85 | + fl_codec_write_value(FL_CODEC(codec), buffer, value, &error); |
| 86 | + EXPECT_TRUE(result); |
| 87 | + EXPECT_EQ(error, nullptr); |
| 88 | + } |
| 89 | + |
| 90 | + EXPECT_EQ(buffer->len, static_cast<unsigned int>(5)); |
| 91 | + for (int i = 1; i <= 5; i++) |
| 92 | + EXPECT_EQ(buffer->data[i - 1], '0' + i); |
| 93 | +} |
| 94 | + |
| 95 | +TEST(FlCodecTest, WriteValueError) { |
| 96 | + g_autoptr(FlTestCodec) codec = fl_test_codec_new(); |
| 97 | + g_autoptr(GByteArray) buffer = g_byte_array_new(); |
| 98 | + |
| 99 | + g_autoptr(FlValue) value = fl_value_null_new(); |
| 100 | + g_autoptr(GError) error = nullptr; |
| 101 | + gboolean result = |
| 102 | + fl_codec_write_value(FL_CODEC(codec), buffer, value, &error); |
| 103 | + EXPECT_FALSE(result); |
| 104 | + EXPECT_TRUE(g_error_matches(error, FL_CODEC_ERROR, FL_CODEC_ERROR_FAILED)); |
| 105 | + EXPECT_EQ(buffer->len, static_cast<unsigned int>(0)); |
| 106 | +} |
| 107 | + |
| 108 | +TEST(FlCodecTest, ReadValueEmpty) { |
| 109 | + g_autoptr(FlTestCodec) codec = fl_test_codec_new(); |
| 110 | + g_autoptr(GBytes) message = g_bytes_new(nullptr, 0); |
| 111 | + |
| 112 | + size_t offset = 0; |
| 113 | + g_autoptr(GError) error = nullptr; |
| 114 | + g_autoptr(FlValue) value = |
| 115 | + fl_codec_read_value(FL_CODEC(codec), message, &offset, &error); |
| 116 | + EXPECT_EQ(value, nullptr); |
| 117 | + EXPECT_TRUE( |
| 118 | + g_error_matches(error, FL_CODEC_ERROR, FL_CODEC_ERROR_OUT_OF_DATA)); |
| 119 | + EXPECT_EQ(offset, static_cast<size_t>(0)); |
| 120 | +} |
| 121 | + |
| 122 | +TEST(FlCodecTest, ReadValue) { |
| 123 | + g_autoptr(FlTestCodec) codec = fl_test_codec_new(); |
| 124 | + uint8_t data[] = {'1'}; |
| 125 | + g_autoptr(GBytes) message = g_bytes_new(data, 1); |
| 126 | + |
| 127 | + size_t offset = 0; |
| 128 | + g_autoptr(GError) error = nullptr; |
| 129 | + g_autoptr(FlValue) value = |
| 130 | + fl_codec_read_value(FL_CODEC(codec), message, &offset, &error); |
| 131 | + EXPECT_NE(value, nullptr); |
| 132 | + EXPECT_EQ(error, nullptr); |
| 133 | + EXPECT_EQ(offset, static_cast<size_t>(1)); |
| 134 | + |
| 135 | + ASSERT_TRUE(fl_value_get_type(value) == FL_VALUE_TYPE_INT); |
| 136 | + EXPECT_EQ(fl_value_get_int(value), 1); |
| 137 | +} |
| 138 | + |
| 139 | +TEST(FlCodecTest, ReadValues) { |
| 140 | + g_autoptr(FlTestCodec) codec = fl_test_codec_new(); |
| 141 | + uint8_t data[] = {'1', '2', '3', '4', '5'}; |
| 142 | + g_autoptr(GBytes) message = g_bytes_new(data, 5); |
| 143 | + |
| 144 | + size_t offset = 0; |
| 145 | + for (int i = 1; i <= 5; i++) { |
| 146 | + g_autoptr(GError) error = nullptr; |
| 147 | + g_autoptr(FlValue) value = |
| 148 | + fl_codec_read_value(FL_CODEC(codec), message, &offset, &error); |
| 149 | + EXPECT_NE(value, nullptr); |
| 150 | + EXPECT_EQ(error, nullptr); |
| 151 | + ASSERT_TRUE(fl_value_get_type(value) == FL_VALUE_TYPE_INT); |
| 152 | + EXPECT_EQ(fl_value_get_int(value), i); |
| 153 | + } |
| 154 | + |
| 155 | + EXPECT_EQ(offset, static_cast<size_t>(5)); |
| 156 | +} |
| 157 | + |
| 158 | +TEST(FlCodecTest, ReadValueNullOffset) { |
| 159 | + g_autoptr(FlTestCodec) codec = fl_test_codec_new(); |
| 160 | + uint8_t data[] = {'1'}; |
| 161 | + g_autoptr(GBytes) message = g_bytes_new(data, 1); |
| 162 | + |
| 163 | + g_autoptr(GError) error = nullptr; |
| 164 | + g_autoptr(FlValue) value = |
| 165 | + fl_codec_read_value(FL_CODEC(codec), message, NULL, &error); |
| 166 | + EXPECT_NE(value, nullptr); |
| 167 | + EXPECT_EQ(error, nullptr); |
| 168 | + |
| 169 | + ASSERT_TRUE(fl_value_get_type(value) == FL_VALUE_TYPE_INT); |
| 170 | + EXPECT_EQ(fl_value_get_int(value), 1); |
| 171 | +} |
| 172 | + |
| 173 | +TEST(FlCodecTest, ReadValueInvalidOffset) { |
| 174 | + g_autoptr(FlTestCodec) codec = fl_test_codec_new(); |
| 175 | + uint8_t data[] = {'1', '2', '3', '4', '5'}; |
| 176 | + g_autoptr(GBytes) message = g_bytes_new(data, 5); |
| 177 | + |
| 178 | + size_t offset = 9999; |
| 179 | + g_autoptr(GError) error = nullptr; |
| 180 | + g_autoptr(FlValue) value = |
| 181 | + fl_codec_read_value(FL_CODEC(codec), message, &offset, &error); |
| 182 | + EXPECT_EQ(value, nullptr); |
| 183 | + EXPECT_TRUE( |
| 184 | + g_error_matches(error, FL_CODEC_ERROR, FL_CODEC_ERROR_OUT_OF_DATA)); |
| 185 | + EXPECT_EQ(offset, static_cast<size_t>(9999)); |
| 186 | +} |
0 commit comments