IdentifiersTests.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // Copyright 2022 Google LLC
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. import XCTest
  16. @testable import FirebaseSessions
  17. @testable import FirebaseInstallations
  18. class IdentifiersTests: XCTestCase {
  19. var installations: MockInstallationsProtocol!
  20. var identifiers: Identifiers!
  21. override func setUp() {
  22. // Clear all UserDefaults
  23. if let appDomain = Bundle.main.bundleIdentifier {
  24. UserDefaults.standard.removePersistentDomain(forName: appDomain)
  25. }
  26. installations = MockInstallationsProtocol()
  27. identifiers = Identifiers(installations: installations)
  28. }
  29. func isValidSessionID(_ sessionID: String) -> Bool {
  30. if sessionID.count != 32 {
  31. assertionFailure("Session ID isn't 32 characters long")
  32. return false
  33. }
  34. if sessionID.contains("-") {
  35. assertionFailure("Session ID contains a dash")
  36. return false
  37. }
  38. if sessionID.lowercased().compare(sessionID) != ComparisonResult.orderedSame {
  39. assertionFailure("Session ID is not lowercase")
  40. return false
  41. }
  42. return true
  43. }
  44. // This test case isn't important behavior. When Crash and Perf integrate
  45. // with the Sessions SDK, we may want to move to a lazy solution where
  46. // sessionID can never be empty
  47. func test_sessionID_beforeGenerateReturnsNothing() throws {
  48. XCTAssert(identifiers.sessionID.count == 0)
  49. XCTAssertNil(identifiers.previousSessionID)
  50. }
  51. func test_generateNewSessionID_generatesValidID() throws {
  52. identifiers.generateNewSessionID()
  53. XCTAssert(isValidSessionID(identifiers.sessionID))
  54. XCTAssertNil(identifiers.previousSessionID)
  55. }
  56. /// Ensures that generating a Session ID multiple times results in the last Session ID being set in the previousSessionID field
  57. func test_generateNewSessionID_rotatesPreviousID() throws {
  58. identifiers.generateNewSessionID()
  59. let firstSessionID = identifiers.sessionID
  60. XCTAssert(isValidSessionID(identifiers.sessionID))
  61. XCTAssertNil(identifiers.previousSessionID)
  62. identifiers.generateNewSessionID()
  63. XCTAssert(isValidSessionID(identifiers.sessionID))
  64. XCTAssert(isValidSessionID(identifiers.previousSessionID!))
  65. // Ensure the new lastSessionID is equal to the sessionID from earlier
  66. XCTAssertEqual(identifiers.previousSessionID, firstSessionID)
  67. }
  68. // Fetching FIIDs requires that we are on a background thread.
  69. func test_installationID_getsValidID() throws {
  70. // Make our mock return an ID
  71. let testID = "testID"
  72. installations.result = .success(testID)
  73. let expectation = XCTestExpectation(description: "Get the Installation ID Asynchronously")
  74. DispatchQueue.global().async { [self] in
  75. XCTAssertEqual(self.identifiers.installationID, testID)
  76. expectation.fulfill()
  77. }
  78. wait(for: [expectation], timeout: 1.0)
  79. }
  80. func test_installationID_handlesFailedFetch() throws {
  81. // Make our mock return an error
  82. installations.result = .failure(NSError(domain: "FestFailedFIIDErrorDomain", code: 0))
  83. let expectation = XCTestExpectation(description: "Get the Installation ID Asynchronously")
  84. DispatchQueue.global().async { [self] in
  85. XCTAssertEqual(self.identifiers.installationID, "")
  86. expectation.fulfill()
  87. }
  88. wait(for: [expectation], timeout: 1.0)
  89. }
  90. }