Detect errors encoding method channel responses (flutter/engine#21314)
This commit is contained in:
@@ -230,12 +230,18 @@ gboolean fl_method_channel_respond(
|
||||
FlMethodSuccessResponse* r = FL_METHOD_SUCCESS_RESPONSE(response);
|
||||
message = fl_method_codec_encode_success_envelope(
|
||||
self->codec, fl_method_success_response_get_result(r), error);
|
||||
if (message == nullptr) {
|
||||
return FALSE;
|
||||
}
|
||||
} else if (FL_IS_METHOD_ERROR_RESPONSE(response)) {
|
||||
FlMethodErrorResponse* r = FL_METHOD_ERROR_RESPONSE(response);
|
||||
message = fl_method_codec_encode_error_envelope(
|
||||
self->codec, fl_method_error_response_get_code(r),
|
||||
fl_method_error_response_get_message(r),
|
||||
fl_method_error_response_get_details(r), error);
|
||||
if (message == nullptr) {
|
||||
return FALSE;
|
||||
}
|
||||
} else if (FL_IS_METHOD_NOT_IMPLEMENTED_RESPONSE(response)) {
|
||||
message = nullptr;
|
||||
} else {
|
||||
|
||||
@@ -432,3 +432,185 @@ TEST(FlMethodChannelTest, ReceiveMethodCallRespondNotImplemented) {
|
||||
// Blocks here until method_call_not_implemented_response_cb is called.
|
||||
g_main_loop_run(loop);
|
||||
}
|
||||
|
||||
// A test method codec that always generates errors on responses.
|
||||
G_DECLARE_FINAL_TYPE(TestMethodCodec,
|
||||
test_method_codec,
|
||||
TEST,
|
||||
METHOD_CODEC,
|
||||
FlMethodCodec)
|
||||
|
||||
struct _TestMethodCodec {
|
||||
FlMethodCodec parent_instance;
|
||||
|
||||
FlStandardMethodCodec* wrapped_codec;
|
||||
};
|
||||
|
||||
G_DEFINE_TYPE(TestMethodCodec, test_method_codec, fl_method_codec_get_type())
|
||||
|
||||
static void test_method_codec_dispose(GObject* object) {
|
||||
TestMethodCodec* self = TEST_METHOD_CODEC(object);
|
||||
|
||||
g_clear_object(&self->wrapped_codec);
|
||||
|
||||
G_OBJECT_CLASS(test_method_codec_parent_class)->dispose(object);
|
||||
}
|
||||
|
||||
// Implements FlMethodCodec::encode_method_call.
|
||||
static GBytes* test_method_codec_encode_method_call(FlMethodCodec* codec,
|
||||
const gchar* name,
|
||||
FlValue* args,
|
||||
GError** error) {
|
||||
EXPECT_TRUE(TEST_IS_METHOD_CODEC(codec));
|
||||
TestMethodCodec* self = TEST_METHOD_CODEC(codec);
|
||||
return fl_method_codec_encode_method_call(
|
||||
FL_METHOD_CODEC(self->wrapped_codec), name, args, error);
|
||||
}
|
||||
|
||||
// Implements FlMethodCodec::decode_method_call.
|
||||
static gboolean test_method_codec_decode_method_call(FlMethodCodec* codec,
|
||||
GBytes* message,
|
||||
gchar** name,
|
||||
FlValue** args,
|
||||
GError** error) {
|
||||
EXPECT_TRUE(TEST_IS_METHOD_CODEC(codec));
|
||||
TestMethodCodec* self = TEST_METHOD_CODEC(codec);
|
||||
return fl_method_codec_decode_method_call(
|
||||
FL_METHOD_CODEC(self->wrapped_codec), message, name, args, error);
|
||||
}
|
||||
|
||||
// Implements FlMethodCodec::encode_success_envelope.
|
||||
static GBytes* test_method_codec_encode_success_envelope(FlMethodCodec* codec,
|
||||
FlValue* result,
|
||||
GError** error) {
|
||||
g_set_error(error, FL_MESSAGE_CODEC_ERROR, FL_MESSAGE_CODEC_ERROR_FAILED,
|
||||
"Unsupported type");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Implements FlMethodCodec::encode_error_envelope.
|
||||
static GBytes* test_method_codec_encode_error_envelope(FlMethodCodec* codec,
|
||||
const gchar* code,
|
||||
const gchar* message,
|
||||
FlValue* details,
|
||||
GError** error) {
|
||||
g_set_error(error, FL_MESSAGE_CODEC_ERROR, FL_MESSAGE_CODEC_ERROR_FAILED,
|
||||
"Unsupported type");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Implements FlMethodCodec::encode_decode_reponse.
|
||||
static FlMethodResponse* test_method_codec_decode_response(FlMethodCodec* codec,
|
||||
GBytes* message,
|
||||
GError** error) {
|
||||
EXPECT_TRUE(TEST_IS_METHOD_CODEC(codec));
|
||||
TestMethodCodec* self = TEST_METHOD_CODEC(codec);
|
||||
return fl_method_codec_decode_response(FL_METHOD_CODEC(self->wrapped_codec),
|
||||
message, error);
|
||||
}
|
||||
|
||||
static void test_method_codec_class_init(TestMethodCodecClass* klass) {
|
||||
G_OBJECT_CLASS(klass)->dispose = test_method_codec_dispose;
|
||||
FL_METHOD_CODEC_CLASS(klass)->encode_method_call =
|
||||
test_method_codec_encode_method_call;
|
||||
FL_METHOD_CODEC_CLASS(klass)->decode_method_call =
|
||||
test_method_codec_decode_method_call;
|
||||
FL_METHOD_CODEC_CLASS(klass)->encode_success_envelope =
|
||||
test_method_codec_encode_success_envelope;
|
||||
FL_METHOD_CODEC_CLASS(klass)->encode_error_envelope =
|
||||
test_method_codec_encode_error_envelope;
|
||||
FL_METHOD_CODEC_CLASS(klass)->decode_response =
|
||||
test_method_codec_decode_response;
|
||||
}
|
||||
|
||||
static void test_method_codec_init(TestMethodCodec* self) {
|
||||
self->wrapped_codec = fl_standard_method_codec_new();
|
||||
}
|
||||
|
||||
TestMethodCodec* test_method_codec_new() {
|
||||
return TEST_METHOD_CODEC(g_object_new(test_method_codec_get_type(), nullptr));
|
||||
}
|
||||
|
||||
// Called when a method call is received from the engine in the
|
||||
// ReceiveMethodCallRespondSuccessError test.
|
||||
static void method_call_success_error_cb(FlMethodChannel* channel,
|
||||
FlMethodCall* method_call,
|
||||
gpointer user_data) {
|
||||
g_autoptr(FlValue) result = fl_value_new_int(42);
|
||||
g_autoptr(GError) response_error = nullptr;
|
||||
EXPECT_FALSE(
|
||||
fl_method_call_respond_success(method_call, result, &response_error));
|
||||
EXPECT_NE(response_error, nullptr);
|
||||
|
||||
// Respond to stop a warning occuring about not responding.
|
||||
fl_method_call_respond_not_implemented(method_call, nullptr);
|
||||
|
||||
g_main_loop_quit(static_cast<GMainLoop*>(user_data));
|
||||
}
|
||||
|
||||
// Checks error correctly handled if provide an unsupported arg in a method call
|
||||
// response.
|
||||
TEST(FlMethodChannelTest, ReceiveMethodCallRespondSuccessError) {
|
||||
g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
|
||||
|
||||
g_autoptr(FlEngine) engine = make_mock_engine();
|
||||
FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
|
||||
g_autoptr(TestMethodCodec) codec = test_method_codec_new();
|
||||
g_autoptr(FlMethodChannel) channel = fl_method_channel_new(
|
||||
messenger, "test/standard-method", FL_METHOD_CODEC(codec));
|
||||
fl_method_channel_set_method_call_handler(
|
||||
channel, method_call_success_error_cb, loop, nullptr);
|
||||
|
||||
// Trigger the engine to make a method call.
|
||||
g_autoptr(FlValue) args = fl_value_new_list();
|
||||
fl_value_append_take(args, fl_value_new_string("test/standard-method"));
|
||||
fl_value_append_take(args, fl_value_new_string("Foo"));
|
||||
fl_value_append_take(args, fl_value_new_string("Marco!"));
|
||||
fl_method_channel_invoke_method(channel, "InvokeMethod", args, nullptr,
|
||||
nullptr, loop);
|
||||
|
||||
// Blocks here until method_call_success_error_cb is called.
|
||||
g_main_loop_run(loop);
|
||||
}
|
||||
|
||||
// Called when a method call is received from the engine in the
|
||||
// ReceiveMethodCallRespondErrorError test.
|
||||
static void method_call_error_error_cb(FlMethodChannel* channel,
|
||||
FlMethodCall* method_call,
|
||||
gpointer user_data) {
|
||||
g_autoptr(FlValue) details = fl_value_new_int(42);
|
||||
g_autoptr(GError) response_error = nullptr;
|
||||
EXPECT_FALSE(fl_method_call_respond_error(method_call, "error", "ERROR",
|
||||
details, &response_error));
|
||||
EXPECT_NE(response_error, nullptr);
|
||||
|
||||
// Respond to stop a warning occuring about not responding.
|
||||
fl_method_call_respond_not_implemented(method_call, nullptr);
|
||||
|
||||
g_main_loop_quit(static_cast<GMainLoop*>(user_data));
|
||||
}
|
||||
|
||||
// Checks error correctly handled if provide an unsupported arg in a method call
|
||||
// response.
|
||||
TEST(FlMethodChannelTest, ReceiveMethodCallRespondErrorError) {
|
||||
g_autoptr(GMainLoop) loop = g_main_loop_new(nullptr, 0);
|
||||
|
||||
g_autoptr(FlEngine) engine = make_mock_engine();
|
||||
FlBinaryMessenger* messenger = fl_binary_messenger_new(engine);
|
||||
g_autoptr(TestMethodCodec) codec = test_method_codec_new();
|
||||
g_autoptr(FlMethodChannel) channel = fl_method_channel_new(
|
||||
messenger, "test/standard-method", FL_METHOD_CODEC(codec));
|
||||
fl_method_channel_set_method_call_handler(channel, method_call_error_error_cb,
|
||||
loop, nullptr);
|
||||
|
||||
// Trigger the engine to make a method call.
|
||||
g_autoptr(FlValue) args = fl_value_new_list();
|
||||
fl_value_append_take(args, fl_value_new_string("test/standard-method"));
|
||||
fl_value_append_take(args, fl_value_new_string("Foo"));
|
||||
fl_value_append_take(args, fl_value_new_string("Marco!"));
|
||||
fl_method_channel_invoke_method(channel, "InvokeMethod", args, nullptr,
|
||||
nullptr, loop);
|
||||
|
||||
// Blocks here until method_call_error_error_cb is called.
|
||||
g_main_loop_run(loop);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user