forked from firka/flutter
Log FlutterJSONMessageCodec decode errors before asserting (flutter/engine#50163)
Logging JSON decode errors on assertion will help track down bugs. https://github.com/flutter/flutter/issues/100891#issuecomment-1915946340 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
@@ -89,6 +89,7 @@ FLUTTER_ASSERT_ARC
|
||||
}
|
||||
BOOL isSimpleValue = NO;
|
||||
id decoded = nil;
|
||||
NSError* error;
|
||||
if (0 < message.length) {
|
||||
UInt8 first;
|
||||
[message getBytes:&first length:1];
|
||||
@@ -105,9 +106,9 @@ FLUTTER_ASSERT_ARC
|
||||
[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
|
||||
|
||||
@@ -44,11 +44,26 @@ TEST(FlutterStringCodec, CanEncodeAndDecodeNonBMPString) {
|
||||
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);
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user