IdentifiersTests.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. func test_generateNewSessionID_generatesValidID() throws {
  45. identifiers.generateNewSessionID()
  46. XCTAssert(isValidSessionID(identifiers.sessionID))
  47. XCTAssert(identifiers.previousSessionID.count == 0)
  48. }
  49. /// Ensures that generating a Session ID multiple times results in the last Session ID being set in the previousSessionID field
  50. func test_generateNewSessionID_rotatesPreviousID() throws {
  51. identifiers.generateNewSessionID()
  52. let firstSessionID = identifiers.sessionID
  53. XCTAssert(isValidSessionID(identifiers.sessionID))
  54. XCTAssert(identifiers.previousSessionID.count == 0)
  55. identifiers.generateNewSessionID()
  56. XCTAssert(isValidSessionID(identifiers.sessionID))
  57. XCTAssert(isValidSessionID(identifiers.previousSessionID))
  58. // Ensure the new lastSessionID is equal to the sessionID from earlier
  59. XCTAssert(identifiers.previousSessionID.compare(firstSessionID) == ComparisonResult.orderedSame)
  60. }
  61. // Fetching FIIDs requires that we are on a background thread.
  62. func test_installationID_getsValidID() throws {
  63. // Make our mock return an ID
  64. let testID = "testID"
  65. installations.result = .success(testID)
  66. let expectation = XCTestExpectation(description: "Get the Installation ID Asynchronously")
  67. DispatchQueue.global().async { [self] in
  68. XCTAssertEqual(self.identifiers.installationID, testID)
  69. expectation.fulfill()
  70. }
  71. wait(for: [expectation], timeout: 1.0)
  72. }
  73. func test_installationID_handlesFailedFetch() throws {
  74. // Make our mock return an error
  75. installations.result = .failure(NSError(domain: "FestFailedFIIDErrorDomain", code: 0))
  76. let expectation = XCTestExpectation(description: "Get the Installation ID Asynchronously")
  77. DispatchQueue.global().async { [self] in
  78. XCTAssertEqual(self.identifiers.installationID, "")
  79. expectation.fulfill()
  80. }
  81. wait(for: [expectation], timeout: 1.0)
  82. }
  83. }