IdentifiersTests.swift 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. class IdentifiersTests: XCTestCase {
  18. var identifiers: Identifiers!
  19. override func setUp() {
  20. // Clear all UserDefaults
  21. if let appDomain = Bundle.main.bundleIdentifier {
  22. UserDefaults.standard.removePersistentDomain(forName: appDomain)
  23. }
  24. identifiers = Identifiers()
  25. }
  26. func isValidSessionID(_ sessionID: String) -> Bool {
  27. if sessionID.count != 32 {
  28. assertionFailure("Session ID isn't 32 characters long")
  29. return false
  30. }
  31. if sessionID.contains("-") {
  32. assertionFailure("Session ID contains a dash")
  33. return false
  34. }
  35. if sessionID.lowercased().compare(sessionID) != ComparisonResult.orderedSame {
  36. assertionFailure("Session ID is not lowercase")
  37. return false
  38. }
  39. return true
  40. }
  41. // This test case isn't important behavior. When Crash and Perf integrate
  42. // with the Sessions SDK, we may want to move to a lazy solution where
  43. // sessionID can never be empty
  44. func test_sessionID_beforeGenerateReturnsNothing() throws {
  45. XCTAssert(identifiers.sessionID.count == 0)
  46. XCTAssertNil(identifiers.previousSessionID)
  47. }
  48. func test_generateNewSessionID_generatesValidID() throws {
  49. identifiers.generateNewSessionID()
  50. XCTAssert(isValidSessionID(identifiers.sessionID))
  51. XCTAssertNil(identifiers.previousSessionID)
  52. }
  53. /// Ensures that generating a Session ID multiple times results in the last Session ID being set in the previousSessionID field
  54. func test_generateNewSessionID_rotatesPreviousID() throws {
  55. identifiers.generateNewSessionID()
  56. let firstSessionID = identifiers.sessionID
  57. XCTAssert(isValidSessionID(identifiers.sessionID))
  58. XCTAssertNil(identifiers.previousSessionID)
  59. identifiers.generateNewSessionID()
  60. XCTAssert(isValidSessionID(identifiers.sessionID))
  61. XCTAssert(isValidSessionID(identifiers.previousSessionID!))
  62. // Ensure the new lastSessionID is equal to the sessionID from earlier
  63. XCTAssertEqual(identifiers.previousSessionID, firstSessionID)
  64. }
  65. }