From b879b9f3e14a33bc2604e06b54004f1b7dadd9b4 Mon Sep 17 00:00:00 2001 From: Nan Date: Wed, 25 Oct 2023 15:30:21 -0700 Subject: [PATCH 1/2] Fix crash if influenceType is direct but there is no directId set cherry-pick of https://github.com/OneSignal/OneSignal-iOS-SDK/pull/1311 --- .../Source/Influence/OSChannelTracker.m | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/iOS_SDK/OneSignalSDK/OneSignalOutcomes/Source/Influence/OSChannelTracker.m b/iOS_SDK/OneSignalSDK/OneSignalOutcomes/Source/Influence/OSChannelTracker.m index cdade0b23..d712247aa 100644 --- a/iOS_SDK/OneSignalSDK/OneSignalOutcomes/Source/Influence/OSChannelTracker.m +++ b/iOS_SDK/OneSignalSDK/OneSignalOutcomes/Source/Influence/OSChannelTracker.m @@ -139,9 +139,13 @@ - (OSInfluence *)currentSessionInfluence { if (_influenceType == DIRECT) { if ([self isDirectSessionEnabled]) { - NSArray *ids = [NSArray arrayWithObject:_directId]; - builder.ids = ids; - builder.influenceType = DIRECT; + if (_directId) { + NSArray *ids = [NSArray arrayWithObject:_directId]; + builder.ids = ids; + builder.influenceType = DIRECT; + } else { + [OneSignalLog onesignalLog:ONE_S_LL_ERROR message:@"OSChannelTracker:currentSessionInfluence found a direct influence without a direct id."]; + } } } else if (_influenceType == INDIRECT) { if ([self isIndirectSessionEnabled]) { From 4de4431f012667e92ec32afedc4f883d19803296 Mon Sep 17 00:00:00 2001 From: Nan Date: Wed, 25 Oct 2023 15:32:32 -0700 Subject: [PATCH 2/2] unit test for crash protection cherry-pick of https://github.com/OneSignal/OneSignal-iOS-SDK/pull/1311 --- .../UnitTests/ChannelTrackersTests.m | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/iOS_SDK/OneSignalSDK/UnitTests/ChannelTrackersTests.m b/iOS_SDK/OneSignalSDK/UnitTests/ChannelTrackersTests.m index a2a14a315..94fe34f25 100644 --- a/iOS_SDK/OneSignalSDK/UnitTests/ChannelTrackersTests.m +++ b/iOS_SDK/OneSignalSDK/UnitTests/ChannelTrackersTests.m @@ -32,6 +32,7 @@ #import "OneSignalHelper.h" #import "UnitTestCommonMethods.h" +#import "OSNotificationTracker.h" #import "CommonAsserts.h" @interface ChannelTrackersTests : XCTestCase @@ -167,4 +168,19 @@ - (void)testGetChannelToResetByEntryAction { XCTAssertEqualObjects(@"iam_id", [[[trackerFactory channelsToResetByEntryAction:NOTIFICATION_CLICK] objectAtIndex:0] idTag]); } +- (void)testDirectInfluenceWithNullId { + [self setOutcomesParamsEnabled]; + OSNotificationTracker *channelTracker = [[OSNotificationTracker alloc] initWithRepository:[OSInfluenceDataRepository sharedInfluenceDataRepository]]; + // Set the influence type to direct but do not set the direct id + channelTracker.influenceType = DIRECT; + OSInfluence *influence = [channelTracker currentSessionInfluence]; + // The current influence was invalid so the type should be disabled + XCTAssertEqual(influence.influenceType, DISABLED); + // Set the directId + channelTracker.directId = @"testid"; + influence = [channelTracker currentSessionInfluence]; + // Now that the directId is set the influence should be valid. + XCTAssertEqual(influence.influenceType, DIRECT); +} + @end