Skip to content

Commit

Permalink
osxkeychain: Delete(): return typed errors
Browse files Browse the repository at this point in the history
This allows a Delete for non-existing credentials to be handled.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed May 27, 2023
1 parent 5e57010 commit 0a405fb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
12 changes: 9 additions & 3 deletions osxkeychain/osxkeychain_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,16 @@ func (h Osxkeychain) Delete(serverURL string) error {
}
defer freeServer(s)

errMsg := C.keychain_delete(s)
if errMsg != nil {
if errMsg := C.keychain_delete(s); errMsg != nil {
defer C.free(unsafe.Pointer(errMsg))
return errors.New(C.GoString(errMsg))
switch goMsg := C.GoString(errMsg); goMsg {
case errCredentialsNotFound:
return credentials.NewErrCredentialsNotFound()
case errInteractionNotAllowed:
return ErrInteractionNotAllowed
default:
return errors.New(goMsg)
}
}

return nil
Expand Down
9 changes: 7 additions & 2 deletions osxkeychain/osxkeychain_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,14 @@ func TestOSXKeychainHelperStoreRetrieve(t *testing.T) {
}

func TestMissingCredentials(t *testing.T) {
const nonExistingCred = "https://adsfasdf.invalid/asdfsdddd"
helper := Osxkeychain{}
_, _, err := helper.Get("https://adsfasdf.wrewerwer.com/asdfsdddd")
_, _, err := helper.Get(nonExistingCred)
if !credentials.IsErrCredentialsNotFound(err) {
t.Fatalf("expected ErrCredentialsNotFound, got %v", err)
t.Errorf("expected ErrCredentialsNotFound, got %v", err)
}
err = helper.Delete(nonExistingCred)
if !credentials.IsErrCredentialsNotFound(err) {
t.Errorf("expected ErrCredentialsNotFound, got %v", err)
}
}

0 comments on commit 0a405fb

Please sign in to comment.