Flutter Linux Embedder
fl_standard_method_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 
8 #include "flutter/shell/platform/linux/testing/fl_test.h"
9 #include "gtest/gtest.h"
10 
11 // NOTE(robert-ancell) These test cases assumes a little-endian architecture.
12 // These tests will need to be updated if tested on a big endian architecture.
13 
14 // Encodes a method call using StandardMethodCodec to a hex string.
15 static gchar* encode_method_call(const gchar* name, FlValue* args) {
16  g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
17  g_autoptr(GError) error = nullptr;
18  g_autoptr(GBytes) message = fl_method_codec_encode_method_call(
19  FL_METHOD_CODEC(codec), name, args, &error);
20  EXPECT_NE(message, nullptr);
21  EXPECT_EQ(error, nullptr);
22 
23  return bytes_to_hex_string(message);
24 }
25 
26 // Encodes a success envelope response using StandardMethodCodec to a hex
27 // string.
29  g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
30  g_autoptr(GError) error = nullptr;
31  g_autoptr(GBytes) message = fl_method_codec_encode_success_envelope(
32  FL_METHOD_CODEC(codec), result, &error);
33  EXPECT_NE(message, nullptr);
34  EXPECT_EQ(error, nullptr);
35 
36  return bytes_to_hex_string(message);
37 }
38 
39 // Encodes a error envelope response using StandardMethodCodec to a hex string.
40 static gchar* encode_error_envelope(const gchar* error_code,
41  const gchar* error_message,
42  FlValue* details) {
43  g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
44  g_autoptr(GError) error = nullptr;
45  g_autoptr(GBytes) message = fl_method_codec_encode_error_envelope(
46  FL_METHOD_CODEC(codec), error_code, error_message, details, &error);
47  EXPECT_NE(message, nullptr);
48  EXPECT_EQ(error, nullptr);
49 
50  return bytes_to_hex_string(message);
51 }
52 
53 // Decodes a method call using StandardMethodCodec with a hex string.
54 static void decode_method_call(const char* hex_string,
55  gchar** name,
56  FlValue** args) {
57  g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
58  g_autoptr(GBytes) message = hex_string_to_bytes(hex_string);
59  g_autoptr(GError) error = nullptr;
61  FL_METHOD_CODEC(codec), message, name, args, &error);
62  EXPECT_TRUE(result);
63  EXPECT_EQ(error, nullptr);
64 }
65 
66 // Decodes a method call using StandardMethodCodec. Expect the given error.
67 static void decode_error_method_call(const char* hex_string,
68  GQuark domain,
69  gint code) {
70  g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
71  g_autoptr(GBytes) message = hex_string_to_bytes(hex_string);
72  g_autoptr(GError) error = nullptr;
73  g_autofree gchar* name = nullptr;
74  g_autoptr(FlValue) args = nullptr;
76  FL_METHOD_CODEC(codec), message, &name, &args, &error);
77  EXPECT_FALSE(result);
78  EXPECT_EQ(name, nullptr);
79  EXPECT_EQ(args, nullptr);
80  EXPECT_TRUE(g_error_matches(error, domain, code));
81 }
82 
83 // Decodes a response using StandardMethodCodec. Expect the response is a
84 // result.
85 static void decode_response_with_success(const char* hex_string,
86  FlValue* result) {
87  g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
88  g_autoptr(GBytes) message = hex_string_to_bytes(hex_string);
89  g_autoptr(GError) error = nullptr;
90  g_autoptr(FlMethodResponse) response =
91  fl_method_codec_decode_response(FL_METHOD_CODEC(codec), message, &error);
92  ASSERT_NE(response, nullptr);
93  EXPECT_EQ(error, nullptr);
94  EXPECT_TRUE(FL_IS_METHOD_SUCCESS_RESPONSE(response));
96  FL_METHOD_SUCCESS_RESPONSE(response)),
97  result));
98 }
99 
100 // Decodes a response using StandardMethodCodec. Expect the response contains
101 // the given error.
102 static void decode_response_with_error(const char* hex_string,
103  const gchar* code,
104  const gchar* error_message,
105  FlValue* details) {
106  g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
107  g_autoptr(GBytes) message = hex_string_to_bytes(hex_string);
108  g_autoptr(GError) error = nullptr;
109  g_autoptr(FlMethodResponse) response =
110  fl_method_codec_decode_response(FL_METHOD_CODEC(codec), message, &error);
111  ASSERT_NE(response, nullptr);
112  EXPECT_EQ(error, nullptr);
113  EXPECT_TRUE(FL_IS_METHOD_ERROR_RESPONSE(response));
114  EXPECT_STREQ(
115  fl_method_error_response_get_code(FL_METHOD_ERROR_RESPONSE(response)),
116  code);
117  if (error_message == nullptr) {
119  FL_METHOD_ERROR_RESPONSE(response)),
120  nullptr);
121  } else {
123  FL_METHOD_ERROR_RESPONSE(response)),
124  error_message);
125  }
126  if (details == nullptr) {
128  FL_METHOD_ERROR_RESPONSE(response)),
129  nullptr);
130  } else {
132  FL_METHOD_ERROR_RESPONSE(response)),
133  details));
134  }
135 }
136 
137 static void decode_error_response(const char* hex_string,
138  GQuark domain,
139  gint code) {
140  g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
141  g_autoptr(GBytes) message = hex_string_to_bytes(hex_string);
142  g_autoptr(GError) error = nullptr;
143  g_autoptr(FlMethodResponse) response =
144  fl_method_codec_decode_response(FL_METHOD_CODEC(codec), message, &error);
145  EXPECT_EQ(response, nullptr);
146  EXPECT_TRUE(g_error_matches(error, domain, code));
147 }
148 
149 TEST(FlStandardMethodCodecTest, EncodeMethodCallNullptrArgs) {
150  g_autofree gchar* hex_string = encode_method_call("hello", nullptr);
151  EXPECT_STREQ(hex_string, "070568656c6c6f00");
152 }
153 
154 TEST(FlStandardMethodCodecTest, EncodeMethodCallNullArgs) {
155  g_autoptr(FlValue) value = fl_value_new_null();
156  g_autofree gchar* hex_string = encode_method_call("hello", value);
157  EXPECT_STREQ(hex_string, "070568656c6c6f00");
158 }
159 
160 TEST(FlStandardMethodCodecTest, EncodeMethodCallStringArgs) {
161  g_autoptr(FlValue) args = fl_value_new_string("world");
162  g_autofree gchar* hex_string = encode_method_call("hello", args);
163  EXPECT_STREQ(hex_string, "070568656c6c6f0705776f726c64");
164 }
165 
166 TEST(FlStandardMethodCodecTest, EncodeMethodCallListArgs) {
167  g_autoptr(FlValue) args = fl_value_new_list();
170  g_autofree gchar* hex_string = encode_method_call("hello", args);
171  EXPECT_STREQ(hex_string, "070568656c6c6f0c020705636f756e74032a000000");
172 }
173 
174 TEST(FlStandardMethodCodecTest, DecodeMethodCallNullArgs) {
175  g_autofree gchar* name = nullptr;
176  g_autoptr(FlValue) args = nullptr;
177  decode_method_call("070568656c6c6f00", &name, &args);
178  EXPECT_STREQ(name, "hello");
180 }
181 
182 TEST(FlStandardMethodCodecTest, DecodeMethodCallStringArgs) {
183  g_autofree gchar* name = nullptr;
184  g_autoptr(FlValue) args = nullptr;
185  decode_method_call("070568656c6c6f0705776f726c64", &name, &args);
186  EXPECT_STREQ(name, "hello");
188  EXPECT_STREQ(fl_value_get_string(args), "world");
189 }
190 
191 TEST(FlStandardMethodCodecTest, DecodeMethodCallListArgs) {
192  g_autofree gchar* name = nullptr;
193  g_autoptr(FlValue) args = nullptr;
194  decode_method_call("070568656c6c6f0c020705636f756e74032a000000", &name,
195  &args);
196  EXPECT_STREQ(name, "hello");
198  EXPECT_EQ(fl_value_get_length(args), static_cast<size_t>(2));
199 
201  ASSERT_EQ(fl_value_get_type(arg0), FL_VALUE_TYPE_STRING);
202  EXPECT_STREQ(fl_value_get_string(arg0), "count");
203 
205  ASSERT_EQ(fl_value_get_type(arg1), FL_VALUE_TYPE_INT);
206  EXPECT_EQ(fl_value_get_int(arg1), 42);
207 }
208 
209 TEST(FlStandardMethodCodecTest, DecodeMethodCallNoData) {
212 }
213 
214 TEST(FlStandardMethodCodecTest, DecodeMethodCallNullMethodName) {
217 }
218 
219 TEST(FlStandardMethodCodecTest, DecodeMethodCallMissingArgs) {
222 }
223 
224 TEST(FlStandardMethodCodecTest, EncodeSuccessEnvelopeNullptr) {
225  g_autofree gchar* hex_string = encode_success_envelope(nullptr);
226  EXPECT_STREQ(hex_string, "0000");
227 }
228 
229 TEST(FlStandardMethodCodecTest, EncodeSuccessEnvelopeNull) {
230  g_autoptr(FlValue) result = fl_value_new_null();
231  g_autofree gchar* hex_string = encode_success_envelope(result);
232  EXPECT_STREQ(hex_string, "0000");
233 }
234 
235 TEST(FlStandardMethodCodecTest, EncodeSuccessEnvelopeString) {
236  g_autoptr(FlValue) result = fl_value_new_string("hello");
237  g_autofree gchar* hex_string = encode_success_envelope(result);
238  EXPECT_STREQ(hex_string, "00070568656c6c6f");
239 }
240 
241 TEST(FlStandardMethodCodecTest, EncodeSuccessEnvelopeList) {
242  g_autoptr(FlValue) result = fl_value_new_list();
245  g_autofree gchar* hex_string = encode_success_envelope(result);
246  EXPECT_STREQ(hex_string, "000c020705636f756e74032a000000");
247 }
248 
249 TEST(FlStandardMethodCodecTest, EncodeErrorEnvelopeEmptyCode) {
250  g_autofree gchar* hex_string = encode_error_envelope("", nullptr, nullptr);
251  EXPECT_STREQ(hex_string, "0107000000");
252 }
253 
254 TEST(FlStandardMethodCodecTest, EncodeErrorEnvelopeNonMessageOrDetails) {
255  g_autofree gchar* hex_string =
256  encode_error_envelope("error", nullptr, nullptr);
257  EXPECT_STREQ(hex_string, "0107056572726f720000");
258 }
259 
260 TEST(FlStandardMethodCodecTest, EncodeErrorEnvelopeMessage) {
261  g_autofree gchar* hex_string =
262  encode_error_envelope("error", "message", nullptr);
263  EXPECT_STREQ(hex_string, "0107056572726f7207076d65737361676500");
264 }
265 
266 TEST(FlStandardMethodCodecTest, EncodeErrorEnvelopeDetails) {
267  g_autoptr(FlValue) details = fl_value_new_list();
268  fl_value_append_take(details, fl_value_new_string("count"));
270  g_autofree gchar* hex_string =
271  encode_error_envelope("error", nullptr, details);
272  EXPECT_STREQ(hex_string, "0107056572726f72000c020705636f756e74032a000000");
273 }
274 
275 TEST(FlStandardMethodCodecTest, EncodeErrorEnvelopeMessageAndDetails) {
276  g_autoptr(FlValue) details = fl_value_new_list();
277  fl_value_append_take(details, fl_value_new_string("count"));
279  g_autofree gchar* hex_string =
280  encode_error_envelope("error", "message", details);
281  EXPECT_STREQ(
282  hex_string,
283  "0107056572726f7207076d6573736167650c020705636f756e74032a000000");
284 }
285 
286 TEST(FlStandardMethodCodecTest, DecodeResponseSuccessNull) {
287  g_autoptr(FlValue) result = fl_value_new_null();
289 }
290 
291 TEST(FlStandardMethodCodecTest, DecodeResponseSuccessString) {
292  g_autoptr(FlValue) result = fl_value_new_string("hello");
293  decode_response_with_success("00070568656c6c6f", result);
294 }
295 
296 TEST(FlStandardMethodCodecTest, DecodeResponseSuccessList) {
297  g_autoptr(FlValue) result = fl_value_new_list();
300  decode_response_with_success("000c020705636f756e74032a000000", result);
301 }
302 
303 TEST(FlStandardMethodCodecTest, DecodeResponseErrorEmptyCode) {
304  decode_response_with_error("0107000000", "", nullptr, nullptr);
305 }
306 
307 TEST(FlStandardMethodCodecTest, DecodeResponseErrorNoMessageOrDetails) {
308  decode_response_with_error("0107056572726f720000", "error", nullptr, nullptr);
309 }
310 
311 TEST(FlStandardMethodCodecTest, DecodeResponseErrorMessage) {
312  decode_response_with_error("0107056572726f7207076d65737361676500", "error",
313  "message", nullptr);
314 }
315 
316 TEST(FlStandardMethodCodecTest, DecodeResponseErrorDetails) {
317  g_autoptr(FlValue) details = fl_value_new_list();
318  fl_value_append_take(details, fl_value_new_string("count"));
320  decode_response_with_error("0107056572726f72000c020705636f756e74032a000000",
321  "error", nullptr, details);
322 }
323 
324 TEST(FlStandardMethodCodecTest, DecodeResponseErrorMessageAndDetails) {
325  g_autoptr(FlValue) details = fl_value_new_list();
326  fl_value_append_take(details, fl_value_new_string("count"));
329  "0107056572726f7207076d6573736167650c020705636f756e74032a000000", "error",
330  "message", details);
331 }
332 
333 TEST(FlStandardMethodCodecTest, DecodeResponseSuccessNoData) {
336 }
337 
338 TEST(FlStandardMethodCodecTest, DecodeResponseSuccessExtraData) {
341 }
342 
343 TEST(FlStandardMethodCodecTest, DecodeResponseErrorNoData) {
346 }
347 
348 TEST(FlStandardMethodCodecTest, DecodeResponseErrorMissingMessageAndDetails) {
349  decode_error_response("0107056572726f72", FL_MESSAGE_CODEC_ERROR,
351 }
352 
353 TEST(FlStandardMethodCodecTest, DecodeResponseErrorMissingDetails) {
354  decode_error_response("0107056572726f7200", FL_MESSAGE_CODEC_ERROR,
356 }
357 
358 TEST(FlStandardMethodCodecTest, DecodeResponseErrorExtraData) {
359  decode_error_response("0107056572726f72000000", FL_MESSAGE_CODEC_ERROR,
361 }
362 
363 TEST(FlStandardMethodCodecTest, DecodeResponseNotImplemented) {
364  g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
365  g_autoptr(GBytes) message = g_bytes_new(nullptr, 0);
366  g_autoptr(GError) error = nullptr;
367  g_autoptr(FlMethodResponse) response =
368  fl_method_codec_decode_response(FL_METHOD_CODEC(codec), message, &error);
369  ASSERT_NE(response, nullptr);
370  EXPECT_EQ(error, nullptr);
371  EXPECT_TRUE(FL_IS_METHOD_NOT_IMPLEMENTED_RESPONSE(response));
372 }
373 
374 TEST(FlStandardMethodCodecTest, DecodeResponseUnknownEnvelope) {
377 }
fl_method_codec_encode_method_call
GBytes * fl_method_codec_encode_method_call(FlMethodCodec *self, const gchar *name, FlValue *args, GError **error)
Definition: fl_method_codec.cc:16
decode_error_response
static void decode_error_response(const char *hex_string, GQuark domain, gint code)
Definition: fl_standard_method_codec_test.cc:137
decode_response_with_success
static void decode_response_with_success(const char *hex_string, FlValue *result)
Definition: fl_standard_method_codec_test.cc:85
fl_standard_method_codec_new
G_MODULE_EXPORT FlStandardMethodCodec * fl_standard_method_codec_new()
Definition: fl_standard_method_codec.cc:248
encode_method_call
static gchar * encode_method_call(const gchar *name, FlValue *args)
Definition: fl_standard_method_codec_test.cc:15
fl_value_new_list
G_MODULE_EXPORT FlValue * fl_value_new_list()
Definition: fl_value.cc:338
FlValue
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:40
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
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_method_error_response_get_message
const G_MODULE_EXPORT gchar * fl_method_error_response_get_message(FlMethodErrorResponse *self)
Definition: fl_method_response.cc:166
FL_VALUE_TYPE_NULL
@ FL_VALUE_TYPE_NULL
Definition: fl_value.h:60
decode_method_call
static void decode_method_call(const char *hex_string, gchar **name, FlValue **args)
Definition: fl_standard_method_codec_test.cc:54
decode_error_method_call
static void decode_error_method_call(const char *hex_string, GQuark domain, gint code)
Definition: fl_standard_method_codec_test.cc:67
fl_value_get_int
G_MODULE_EXPORT int64_t fl_value_get_int(FlValue *self)
Definition: fl_value.cc:628
fl_method_error_response_get_details
G_MODULE_EXPORT FlValue * fl_method_error_response_get_details(FlMethodErrorResponse *self)
Definition: fl_method_response.cc:172
fl_value_get_type
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:428
TEST
TEST(FlStandardMethodCodecTest, EncodeMethodCallNullptrArgs)
Definition: fl_standard_method_codec_test.cc:149
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_STRING
@ FL_VALUE_TYPE_STRING
Definition: fl_value.h:64
fl_message_codec.h
fl_method_codec_decode_method_call
gboolean fl_method_codec_decode_method_call(FlMethodCodec *self, GBytes *message, gchar **name, FlValue **args, GError **error)
Definition: fl_method_codec.cc:27
fl_method_success_response_get_result
G_MODULE_EXPORT FlValue * fl_method_success_response_get_result(FlMethodSuccessResponse *self)
Definition: fl_method_response.cc:138
FL_MESSAGE_CODEC_ERROR_FAILED
@ FL_MESSAGE_CODEC_ERROR_FAILED
Definition: fl_message_codec.h:33
fl_value_append_take
G_MODULE_EXPORT void fl_value_append_take(FlValue *self, FlValue *value)
Definition: fl_value.cc:560
fl_method_codec_encode_success_envelope
GBytes * fl_method_codec_encode_success_envelope(FlMethodCodec *self, FlValue *result, GError **error)
Definition: fl_method_codec.cc:41
fl_value_get_length
G_MODULE_EXPORT size_t fl_value_get_length(FlValue *self)
Definition: fl_value.cc:684
fl_method_codec_decode_response
FlMethodResponse * fl_method_codec_decode_response(FlMethodCodec *self, GBytes *message, GError **error)
Definition: fl_method_codec.cc:62
fl_value_equal
G_MODULE_EXPORT bool fl_value_equal(FlValue *a, FlValue *b)
Definition: fl_value.cc:433
fl_standard_method_codec.h
FL_VALUE_TYPE_INT
@ FL_VALUE_TYPE_INT
Definition: fl_value.h:62
result
GAsyncResult * result
Definition: fl_text_input_plugin.cc:106
FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA
@ FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA
Definition: fl_message_codec.h:34
fl_method_error_response_get_code
const G_MODULE_EXPORT gchar * fl_method_error_response_get_code(FlMethodErrorResponse *self)
Definition: fl_method_response.cc:160
args
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
Definition: fl_event_channel.h:89
error
const uint8_t uint32_t uint32_t GError ** error
Definition: fl_pixel_buffer_texture_test.cc:40
decode_response_with_error
static void decode_response_with_error(const char *hex_string, const gchar *code, const gchar *error_message, FlValue *details)
Definition: fl_standard_method_codec_test.cc:102
encode_error_envelope
static gchar * encode_error_envelope(const gchar *error_code, const gchar *error_message, FlValue *details)
Definition: fl_standard_method_codec_test.cc:40
fl_method_codec_encode_error_envelope
GBytes * fl_method_codec_encode_error_envelope(FlMethodCodec *self, const gchar *code, const gchar *message, FlValue *details, GError **error)
Definition: fl_method_codec.cc:50
encode_success_envelope
static gchar * encode_success_envelope(FlValue *result)
Definition: fl_standard_method_codec_test.cc:28
fl_method_codec_private.h
value
uint8_t value
Definition: fl_standard_message_codec.cc:41
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