Skip to content

Commit

Permalink
test: prevent infinite loop if sdk forwards push event back to itself (
Browse files Browse the repository at this point in the history
  • Loading branch information
levibostian committed Feb 14, 2024
1 parent 216341c commit cdf2f1f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ class PushEventHandlerProxyImpl: PushEventHandlerProxy {
private var nestedDelegates: [String: PushEventHandler] = [:]

func addPushEventHandler(_ newHandler: PushEventHandler) {
// this line below seems fragile. If we change the class name, this could break.
// could digraph inject instance of the SDK's intance before setting singleton?
let doesDelegateBelongToCio = newHandler is IOSPushEventListener

guard !doesDelegateBelongToCio else {
return
}

nestedDelegates[String(describing: newHandler)] = newHandler
}

Expand Down
41 changes: 41 additions & 0 deletions Tests/MessagingPush/PushHandling/PushEventHandlerTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
@testable import CioMessagingPush
import Foundation
import SharedTests
import XCTest

class PushEventHandlerTest: UnitTest {
private var pushEventHandler: PushEventHandler!

private var pushEventHandlerProxy: PushEventHandlerProxy {
diGraph.pushEventHandlerProxy
}

private var pushClickHandler = PushClickHandlerMock()

override func setUp() {
super.setUp()

pushEventHandler = IOSPushEventListener(jsonAdapter: diGraph.jsonAdapter, pushEventHandlerProxy: pushEventHandlerProxy, moduleConfig: MessagingPushConfigOptions(), pushClickHandler: pushClickHandler, pushHistory: diGraph.pushHistory, logger: log)
}

// MARK: onPushAction

/*
The CIO SDK push event handler forwards push notifications to other push event handlers in the host app.
But, what if the CIO SDK event handler forwards a push event back to the CIO SDK event handler? This could cause an infinite loop.
This test simulates that scenario to make sure that an inifinite loop would not happen.
*/
func test_onPushAction_expectNoInfiniteLoopIfSdkForwardsPushEventBacktoSdkAgain() {
// The push event handler proxy is what forwards push events to other push event handlers in the app.
// To test if an infinite loop would happen, add ourself as a push event handler to get events forwarded to.
pushEventHandlerProxy.addPushEventHandler(pushEventHandler)

// Make sure push is not from CIO otherwise event does not get forwarded.
let givenPush = PushNotificationStub.getPushNotSentFromCIO()

// Send the CIO SDK push event handler an event. If an infinite loop occurs, this test will timeout or crash.
pushEventHandler.onPushAction(PushNotificationActionStub(push: givenPush, didClickOnPush: true)) {}
}
}

0 comments on commit cdf2f1f

Please sign in to comment.