From 3254bfc167b43bb564ec513aad24f8e54ad557b3 Mon Sep 17 00:00:00 2001 From: NidhiDixit09 <93544270+NidhiDixit09@users.noreply.github.com> Date: Mon, 6 Jan 2025 15:36:03 -0800 Subject: [PATCH] SDK-2533 Integration validator crash fix * Fixed function compareUriSchemes to handle empty strings * Added wrapper function for compareUriSchemes to make it testable. * Added unit test --- .../Branch-SDK-Tests/BNCSystemObserverTests.m | 35 +++++++++++++++++++ Sources/BranchSDK/BNCSystemObserver.m | 15 ++++++-- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/Branch-TestBed/Branch-SDK-Tests/BNCSystemObserverTests.m b/Branch-TestBed/Branch-SDK-Tests/BNCSystemObserverTests.m index 2eb3cb282..a54dbad02 100644 --- a/Branch-TestBed/Branch-SDK-Tests/BNCSystemObserverTests.m +++ b/Branch-TestBed/Branch-SDK-Tests/BNCSystemObserverTests.m @@ -9,6 +9,10 @@ #import #import "BNCSystemObserver.h" +@interface BNCSystemObserver () ++ (BOOL)compareUriSchemes:(NSString *)serverUriScheme With:(NSArray *)urlTypes; +@end + @interface BNCSystemObserverTests : XCTestCase @end @@ -100,4 +104,35 @@ - (void)testIsAppClip { XCTAssert(![BNCSystemObserver isAppClip]); } +- (void)testCompareURIScemes { + + NSString *serverUriScheme = @"bnctest://"; + NSArray *urlTypes = @[@{@"CFBundleURLSchemes" : @[@""]}, @{@"CFBundleURLSchemes" : @[@"bnctest", @"xyzs"]}]; + + XCTAssertTrue([BNCSystemObserver compareUriSchemes:serverUriScheme With:urlTypes]); + + XCTAssertFalse([BNCSystemObserver compareUriSchemes:serverUriScheme With:nil]); + + XCTAssertFalse([BNCSystemObserver compareUriSchemes:nil With:nil]); + + XCTAssertFalse([BNCSystemObserver compareUriSchemes:nil With:urlTypes]); + + serverUriScheme = @":/"; + XCTAssertFalse([BNCSystemObserver compareUriSchemes:serverUriScheme With:urlTypes]); + + serverUriScheme = @"bnctest"; + XCTAssertTrue([BNCSystemObserver compareUriSchemes:serverUriScheme With:urlTypes]); + + serverUriScheme = @"bnctest://"; + urlTypes = @[ @{@"CFBundleURLSchemes" : @[@"bnctestX", @"xyzs"]}]; + XCTAssertFalse([BNCSystemObserver compareUriSchemes:serverUriScheme With:urlTypes]); + + serverUriScheme = @"://"; + XCTAssertFalse([BNCSystemObserver compareUriSchemes:serverUriScheme With:urlTypes]); + + XCTAssertFalse([BNCSystemObserver compareUriSchemes:@"" With:urlTypes]); + + XCTAssertFalse([BNCSystemObserver compareUriSchemes:@"" With:@[@{}]]); +} + @end diff --git a/Sources/BranchSDK/BNCSystemObserver.m b/Sources/BranchSDK/BNCSystemObserver.m index 600e88922..ca8636269 100644 --- a/Sources/BranchSDK/BNCSystemObserver.m +++ b/Sources/BranchSDK/BNCSystemObserver.m @@ -150,14 +150,25 @@ + (NSString *)defaultURIScheme { + (BOOL)compareUriSchemes : (NSString *) serverUriScheme { NSArray *urlTypes = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleURLTypes"]; + return [self compareUriSchemes:serverUriScheme With:urlTypes]; +} + ++ (BOOL)compareUriSchemes:(NSString *)serverUriScheme With:(NSArray *)urlTypes { + NSString * serverUriSchemeWithoutSuffix ; + + if ([serverUriScheme hasSuffix:@"://"]) { + serverUriSchemeWithoutSuffix = [serverUriScheme substringToIndex:[serverUriScheme length] - 3]; + } else { + serverUriSchemeWithoutSuffix = serverUriScheme; + } for (NSDictionary *urlType in urlTypes) { NSArray *urlSchemes = [urlType objectForKey:@"CFBundleURLSchemes"]; for (NSString *uriScheme in urlSchemes) { - NSString * serverUriSchemeWithoutSuffix = [serverUriScheme substringToIndex:[serverUriScheme length] - 3]; if ([uriScheme isEqualToString:serverUriSchemeWithoutSuffix]) { - return true; } + return true; + } } } return false;