Signal-iOS/SignalServiceKit/src/TestUtils/TestKeychainStorage.swift
Evan Hahn 29c0ddf60e Fix violations of SwiftLint's attributes rule
_I recommend reviewing this with whitespace changes disabled._

Most of these were `@objc` being on the same line, which I fixed with [a
silly script][1]—probably could've used `sed` if I were wiser. The rest
were manual fixes.

See [the SwiftLint documentation for this rule][0].

[0]: https://realm.github.io/SwiftLint/attributes.html
[1]: https://gist.github.com/EvanHahn-Signal/d353c93fa269c82b96baca0a1086521f
2022-05-14 09:07:42 -05:00

70 lines
1.9 KiB
Swift

//
// Copyright (c) 2022 Open Whisper Systems. All rights reserved.
//
import Foundation
#if TESTABLE_BUILD
@objc
public class SSKTestKeychainStorage: NSObject, SSKKeychainStorage {
private let lock = UnfairLock()
private var dataMap = [String: Data]()
@objc
public override init() {
super.init()
}
@objc
public func string(forService service: String, key: String) throws -> String {
let data = try self.data(forService: service, key: key)
guard let string = String(bytes: data, encoding: String.Encoding.utf8) else {
throw KeychainStorageError.failure(description: "\(logTag) could not retrieve string")
}
return string
}
@objc
public func set(string: String, service: String, key: String) throws {
guard let data = string.data(using: String.Encoding.utf8) else {
throw KeychainStorageError.failure(description: "\(logTag) could not store data")
}
try set(data: data, service: service, key: key)
}
private func key(forService service: String, key: String) -> String {
return "\(service) \(key)"
}
@objc
public func data(forService service: String, key: String) throws -> Data {
try lock.withLock {
let key = self.key(forService: service, key: key)
guard let data = dataMap[key] else {
throw KeychainStorageError.failure(description: "\(logTag) could not retrieve data")
}
return data
}
}
@objc
public func set(data: Data, service: String, key: String) throws {
lock.withLock {
let key = self.key(forService: service, key: key)
dataMap[key] = data
}
}
@objc
public func remove(service: String, key: String) throws {
lock.withLock {
let key = self.key(forService: service, key: key)
dataMap.removeValue(forKey: key)
}
}
}
#endif