Skip to content

Commit a73d5ff

Browse files
test: Add tests for encodeObject with NSString (#5283)
GH-4424 reports a crash when calling dataUsingEncoding which is related to invalid memory. This PR adds two more tests to ensure we can encode Japanese characters and even an invalid JSONString, which makes dataUsingEncoding return nil.
1 parent ab197ed commit a73d5ff

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Tests/SentryTests/SentryCrash/SentryCrashJSONCodec_Tests.m

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#import "FileBasedTestCase.h"
3232
#import "SentryCrashJSONCodec.h"
3333
#import "SentryCrashJSONCodecObjC.h"
34+
#import "SentryInvalidJSONString.h"
3435

3536
@interface SentryCrashJSONCodec_Tests : FileBasedTestCase
3637
@end
@@ -196,6 +197,39 @@ - (void)testSerializeDeserializeArrayString
196197
XCTAssertEqualObjects(result, original, @"");
197198
}
198199

200+
- (void)testSerializeJapaneseArrayString
201+
{
202+
NSError *error = (NSError *)self;
203+
NSString *expected = @"[\"こんにちは\"]"; // "Hello" in Japanese
204+
id original = [NSArray arrayWithObjects:@"こんにちは", nil];
205+
NSString *jsonString = toString([SentryCrashJSONCodec encode:original
206+
options:SentryCrashJSONEncodeOptionSorted
207+
error:&error]);
208+
XCTAssertNotNil(jsonString, @"");
209+
XCTAssertNil(error, @"");
210+
XCTAssertEqualObjects(jsonString, expected, @"");
211+
id result = [SentryCrashJSONCodec decode:toData(jsonString) options:0 error:&error];
212+
XCTAssertNotNil(result, @"");
213+
XCTAssertNil(error, @"");
214+
XCTAssertEqualObjects(result, original, @"");
215+
}
216+
217+
- (void)testSerializeInvalidJSONString
218+
{
219+
220+
NSString *string = [[SentryInvalidJSONString alloc] initWithLengthInvocationsToBeInvalid:0];
221+
NSError *error = (NSError *)self;
222+
223+
id original = [NSArray arrayWithObjects:string, nil];
224+
NSString *jsonString = toString([SentryCrashJSONCodec encode:original
225+
options:SentryCrashJSONEncodeOptionSorted
226+
error:&error]);
227+
228+
XCTAssertNil(error);
229+
XCTAssertEqualObjects(jsonString, @"[null]", @"");
230+
XCTAssertNotNil(jsonString, @"");
231+
}
232+
199233
- (void)testSerializeDeserializeArrayStringIntl
200234
{
201235
NSError *error = (NSError *)self;

0 commit comments

Comments
 (0)