Skip to content

Commit

Permalink
Merge pull request #3 from trifork/tkc/adding-key-service-error-helper
Browse files Browse the repository at this point in the history
Key service error helper
  • Loading branch information
Thomas Kalhøj Clemensen authored Feb 24, 2021
2 parents 08e859d + 348730b commit fa7f83a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .azure-pipelines/azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
ios14:
IMAGE_NAME: 'macos-10.15'
XCODE_DEVELOPER_PATH: /Applications/Xcode_12.4.app
IOS_SIMULATORS: 'iPhone 11,OS=14.3'
IOS_SIMULATORS: 'iPhone 11,OS=14.4'
pool:
vmImage: $(IMAGE_NAME)
steps:
Expand Down
18 changes: 16 additions & 2 deletions Sources/TIM/Models/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,26 @@ public enum TIMStorageError: Error, LocalizedError {
isKeyServiceError(.badPassword)
}

private func isKeyServiceError(_ keyServiceError: TIMKeyServiceError) -> Bool {
/// Determines whether this error is an error thrown by the KeyService.
///
/// This might be useful for handling unexpected cases from the encryption part of the framework.
/// When the key service fails you don't want to do any drastic fallback, since the server might "just" be down or the user have no internet connection. You will be able to recover later on, from a key service error.
public func isKeyServiceError() -> Bool {
isKeyServiceError(nil)
}

/// Determines whether this error is a specific kind of key service error.
/// - Parameter keyServiceError: The key service error to look for. If `nil` is passed it will look for any kind of key service error.
private func isKeyServiceError(_ keyServiceError: TIMKeyServiceError?) -> Bool {
let isKeyServiceError: Bool
switch self {
case .encryptedStorageFailed(let error):
if case TIMEncryptedStorageError.keyServiceFailed(let ksError) = error {
isKeyServiceError = ksError == keyServiceError
if let keyServiceError = keyServiceError {
isKeyServiceError = ksError == keyServiceError
} else {
isKeyServiceError = true // Is any kind of key service error!
}
} else {
isKeyServiceError = false
}
Expand Down
8 changes: 0 additions & 8 deletions Tests/LinuxMain.swift

This file was deleted.

29 changes: 29 additions & 0 deletions Tests/TIMTests/TIMStorageErrorTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import XCTest
@testable import TIM

final class TIMStorageErrorTests: XCTestCase {

func testIsKeyLocked() {
XCTAssertFalse(TIMStorageError.failedToGetRefreshToken.isKeyLocked())
XCTAssertFalse(TIMStorageError.encryptedStorageFailed(.failedToLoadLongSecretViaBiometric).isKeyLocked())
XCTAssertFalse(TIMStorageError.encryptedStorageFailed(.keyServiceFailed(.badInternet)).isKeyLocked())
XCTAssertFalse(TIMStorageError.encryptedStorageFailed(.keyServiceFailed(.badPassword)).isKeyLocked())
XCTAssertTrue(TIMStorageError.encryptedStorageFailed(.keyServiceFailed(.keyLocked)).isKeyLocked())
}

func testIsWrongPassword() {
XCTAssertFalse(TIMStorageError.failedToGetRefreshToken.isWrongPassword())
XCTAssertFalse(TIMStorageError.encryptedStorageFailed(.failedToLoadLongSecretViaBiometric).isWrongPassword())
XCTAssertFalse(TIMStorageError.encryptedStorageFailed(.keyServiceFailed(.badInternet)).isWrongPassword())
XCTAssertFalse(TIMStorageError.encryptedStorageFailed(.keyServiceFailed(.keyLocked)).isWrongPassword())
XCTAssertTrue(TIMStorageError.encryptedStorageFailed(.keyServiceFailed(.badPassword)).isWrongPassword())
}

func testIsKeyServiceError() {
XCTAssertFalse(TIMStorageError.failedToGetRefreshToken.isKeyServiceError())
XCTAssertFalse(TIMStorageError.encryptedStorageFailed(.failedToLoadLongSecretViaBiometric).isKeyServiceError())
XCTAssertTrue(TIMStorageError.encryptedStorageFailed(.keyServiceFailed(.badInternet)).isKeyServiceError())
XCTAssertTrue(TIMStorageError.encryptedStorageFailed(.keyServiceFailed(.keyLocked)).isKeyServiceError())
XCTAssertTrue(TIMStorageError.encryptedStorageFailed(.keyServiceFailed(.badPassword)).isKeyServiceError())
}
}
10 changes: 0 additions & 10 deletions Tests/TIMTests/XCTestManifests.swift

This file was deleted.

0 comments on commit fa7f83a

Please sign in to comment.