IdentifiersTests.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. var installations = MockInstallationsProtocol()
  19. var identifiers = Identifiers(installations: installations)
  20. class IdentifiersTests: XCTestCase {
  21. override func setUpWithError() throws {
  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 testInitialSessionIDGeneration() throws {
  45. identifiers.generateNewSessionID()
  46. assert(isValidSessionID(identifiers.sessionID))
  47. assert(identifiers.lastSessionID.count == 0)
  48. }
  49. func testRotateSessionID() throws {
  50. identifiers.generateNewSessionID()
  51. let firstSessionID = identifiers.sessionID
  52. assert(isValidSessionID(identifiers.sessionID))
  53. assert(identifiers.lastSessionID.count == 0)
  54. identifiers.generateNewSessionID()
  55. assert(isValidSessionID(identifiers.sessionID))
  56. assert(isValidSessionID(identifiers.lastSessionID))
  57. // Ensure the new lastSessionID is equal to the sessionID from earlier
  58. assert(identifiers.lastSessionID.compare(firstSessionID) == ComparisonResult.orderedSame)
  59. }
  60. // Fetching FIIDs requires that we are on a background thread.
  61. func testSuccessfulFIID() throws {
  62. // Make our mock return an ID
  63. let testID = "testID"
  64. installations.result = .success(testID)
  65. let expectation = XCTestExpectation(description: "Get the Installation ID Asynchronously")
  66. DispatchQueue.global().async {
  67. XCTAssertEqual(identifiers.installationID, testID)
  68. expectation.fulfill()
  69. }
  70. wait(for: [expectation], timeout: 1.0)
  71. }
  72. func testFailedFIID() throws {
  73. // Make our mock return an error
  74. installations.result = .failure(NSError(domain: "FestFailedFIIDErrorDomain", code: 0))
  75. let expectation = XCTestExpectation(description: "Get the Installation ID Asynchronously")
  76. DispatchQueue.global().async {
  77. XCTAssertEqual(identifiers.installationID, "")
  78. expectation.fulfill()
  79. }
  80. wait(for: [expectation], timeout: 1.0)
  81. }
  82. }