From 7b9ed3c580b256d251ba8ecf0ef791b1c63e117c Mon Sep 17 00:00:00 2001 From: Jenn Magder Date: Mon, 29 Jan 2024 18:15:00 -0800 Subject: [PATCH] Log FlutterJSONMessageCodec decode errors before asserting --- .../common/framework/Source/FlutterCodecs.mm | 5 +++-- .../framework/Source/flutter_codecs_unittest.mm | 15 +++++++++++++++ testing/run_all_unittests.cc | 1 + 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/shell/platform/darwin/common/framework/Source/FlutterCodecs.mm b/shell/platform/darwin/common/framework/Source/FlutterCodecs.mm index 57ef1eb752a4a..a8653477e2c03 100644 --- a/shell/platform/darwin/common/framework/Source/FlutterCodecs.mm +++ b/shell/platform/darwin/common/framework/Source/FlutterCodecs.mm @@ -89,6 +89,7 @@ - (id)decode:(NSData*)message { } BOOL isSimpleValue = NO; id decoded = nil; + NSError* error; if (0 < message.length) { UInt8 first; [message getBytes:&first length:1]; @@ -105,9 +106,9 @@ - (id)decode:(NSData*)message { [expandedMessage replaceBytesInRange:NSMakeRange(message.length + 1, 1) withBytes:&end]; message = expandedMessage; } - decoded = [NSJSONSerialization JSONObjectWithData:message options:0 error:nil]; + decoded = [NSJSONSerialization JSONObjectWithData:message options:0 error:&error]; } - NSAssert(decoded, @"Invalid JSON message, decoding failed"); + NSAssert(decoded, @"Invalid JSON message, decoding failed: %@", error); return isSimpleValue ? ((NSArray*)decoded)[0] : decoded; } @end diff --git a/shell/platform/darwin/common/framework/Source/flutter_codecs_unittest.mm b/shell/platform/darwin/common/framework/Source/flutter_codecs_unittest.mm index f0298a0c268f5..c9ab8fdaaac52 100644 --- a/shell/platform/darwin/common/framework/Source/flutter_codecs_unittest.mm +++ b/shell/platform/darwin/common/framework/Source/flutter_codecs_unittest.mm @@ -44,11 +44,26 @@ ASSERT_TRUE([value isEqualTo:decoded]); } +TEST(FlutterJSONCodec, ThrowsOnInvalidEncode) { + NSString* value = [[NSString alloc] initWithBytes:"\xdf\xff" + length:2 + encoding:NSUTF16StringEncoding]; + FlutterJSONMessageCodec* codec = [FlutterJSONMessageCodec sharedInstance]; + EXPECT_EXIT([codec encode:value], testing::KilledBySignal(SIGABRT), "failed to convert to UTF8"); +} + TEST(FlutterJSONCodec, CanDecodeZeroLength) { FlutterJSONMessageCodec* codec = [FlutterJSONMessageCodec sharedInstance]; ASSERT_TRUE([codec decode:[NSData data]] == nil); } +TEST(FlutterJSONCodec, ThrowsOnInvalidDecode) { + NSString* value = @"{{{"; + FlutterJSONMessageCodec* codec = [FlutterJSONMessageCodec sharedInstance]; + EXPECT_EXIT([codec decode:[value dataUsingEncoding:value.fastestEncoding]], + testing::KilledBySignal(SIGABRT), "No string key for value in object around line 1"); +} + TEST(FlutterJSONCodec, CanEncodeAndDecodeNil) { FlutterJSONMessageCodec* codec = [FlutterJSONMessageCodec sharedInstance]; ASSERT_TRUE([codec encode:nil] == nil); diff --git a/testing/run_all_unittests.cc b/testing/run_all_unittests.cc index 4213a9c754d9a..48f4c0d5eb816 100644 --- a/testing/run_all_unittests.cc +++ b/testing/run_all_unittests.cc @@ -49,6 +49,7 @@ int main(int argc, char** argv) { #endif // FML_OS_IOS ::testing::InitGoogleTest(&argc, argv); + GTEST_FLAG_SET(death_test_style, "threadsafe"); // Check if the user has specified a timeout. const auto timeout = GetTestTimeout();