From 6b32c7ec68324f33f2b8fb0765213c74a6f86dd5 Mon Sep 17 00:00:00 2001 From: matthewpalmer Date: Tue, 16 Feb 2016 20:23:02 +1100 Subject: [PATCH] Fix regression in update method to fix #110 --- Locksmith.podspec | 2 +- Source/Locksmith.swift | 14 +++++++++----- Tests/LocksmithTests.swift | 22 ++++++++++++++++++++++ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/Locksmith.podspec b/Locksmith.podspec index efd9d6b..9e227ee 100644 --- a/Locksmith.podspec +++ b/Locksmith.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "Locksmith" - s.version = "2.0.7" + s.version = "2.0.8" s.summary = "Locksmith is a powerful, protocol-oriented library for working with the keychain in Swift." s.description = <<-DESC Locksmith is a powerful, protocol-oriented library for working with the iOS, Mac OS X, watchOS, and tvOS keychain in Swift. It provides extensive support for a lot of different keychain requests, and extensively uses Swift-native concepts. diff --git a/Source/Locksmith.swift b/Source/Locksmith.swift index de4b089..f28b6ae 100644 --- a/Source/Locksmith.swift +++ b/Source/Locksmith.swift @@ -517,11 +517,15 @@ extension CreateableSecureStorable { let status = SecItemUpdate(query, attributesToUpdate) if let error = LocksmithError(fromStatusCode: Int(status)) { - throw error - } - - if status != errSecSuccess { - throw LocksmithError.Undefined + if error == .NotFound || error == .NotAvailable { + try self.createInSecureStore() + } else { + throw error + } + } else { + if status != errSecSuccess { + throw LocksmithError.Undefined + } } } } diff --git a/Tests/LocksmithTests.swift b/Tests/LocksmithTests.swift index 8042aeb..440e38b 100644 --- a/Tests/LocksmithTests.swift +++ b/Tests/LocksmithTests.swift @@ -53,6 +53,12 @@ class LocksmithTests: XCTestCase { let loaded3 = Locksmith.loadDataForUserAccount(userAccount, inService: service)! as! TestingDictionaryType XCTAssertEqual(loaded3, updatedData) + + try! Locksmith.deleteDataForUserAccount(userAccount, inService: service) + + try! Locksmith.updateData(["some update": "data"], forUserAccount: userAccount, inService: service) + let updateResult = Locksmith.loadDataForUserAccount(userAccount, inService: service)! as! [String: String] + XCTAssertEqual(updateResult, ["some update": "data"]) } func testStaticMethodsForDefaultService() { @@ -94,6 +100,22 @@ class LocksmithTests: XCTestCase { createGenericPasswordWithData(data) } + func testUpdateCreatesIfNotExists() { + let data = ["some": "data"] + + struct CreateGenericPassword: CreateableSecureStorable, GenericPasswordSecureStorable, ReadableSecureStorable { + var data: [String: AnyObject] + let account: String + let service: String + } + + let update = CreateGenericPassword(data: data, account: userAccount, service: service) + try! update.updateInSecureStore() + + let read = update.readFromSecureStore()!.data as! [String: String] + XCTAssertEqual(read, ["some": "data"]) + } + func testUpdateForGenericPassword() { let data = ["some": "data"]