This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
forked from keybase/go-keychain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
macos_test.go
103 lines (91 loc) · 2.39 KB
/
macos_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//go:build darwin && !ios
// +build darwin,!ios
package keychain
import (
"fmt"
"testing"
)
func TestUpdateItem(t *testing.T) {
var err error
item := NewGenericPassword("TestAccess", "firsttest", "TestUpdateItem", []byte("toomanysecrets2"), "")
defer func() { _ = DeleteItem(item) }()
err = AddItem(item)
if err != nil {
t.Fatal(err)
}
data1, err := GetGenericPassword("TestAccess", "firsttest", "TestUpdateItem", "")
if err != nil {
t.Fatal(err)
}
if string(data1) != "toomanysecrets2" {
t.Fatal("TestUpdateItem: new password does not match")
}
updateItem := NewItem()
updateItem.SetSecClass(SecClassGenericPassword)
updateItem.SetService("TestAccess")
updateItem.SetAccount("firsttest")
updateItem.SetLabel("TestUpdateItem")
updateItem.SetData([]byte("toomanysecrets3"))
err = UpdateItem(item, updateItem)
if err != nil {
t.Fatal(err)
}
data2, err := GetGenericPassword("TestAccess", "firsttest", "TestUpdateItem", "")
if err != nil {
t.Fatal(err)
}
if string(data2) != "toomanysecrets3" {
t.Fatal("TestUpdateItem: updated password does not match")
}
}
func TestGenericPasswordRef(t *testing.T) {
service, account, label, accessGroup, password := "TestGenericPasswordRef", "test", "", "", "toomanysecrets"
item := NewGenericPassword(service, account, label, []byte(password), accessGroup)
defer func() { _ = DeleteItem(item) }()
err := AddItem(item)
if err != nil {
t.Fatal(err)
}
// Query reference and delete by reference
query := NewItem()
query.SetSecClass(SecClassGenericPassword)
query.SetService(service)
query.SetAccount(account)
query.SetMatchLimit(MatchLimitOne)
query.SetReturnRef(true)
ref, err := QueryItemRef(query)
if err != nil {
t.Fatal(err)
} else if ref == 0 {
t.Fatal("Missing result")
} else {
err = DeleteItemRef(ref)
if err != nil {
t.Fatal(err)
}
Release(ref)
}
passwordAfter, err := GetGenericPassword(service, account, label, accessGroup)
if err != nil {
t.Fatal(err)
}
if passwordAfter != nil {
t.Fatal("Shouldn't have password")
}
}
func TestInternetPassword(t *testing.T) {
query := NewItem()
query.SetSecClass(SecClassInternetPassword)
query.SetLabel("github.com")
query.SetMatchLimit(MatchLimitOne)
query.SetReturnAttributes(true)
results, err := QueryItem(query)
if err != nil {
// Error
t.Errorf("Query Error: %v", err)
} else {
for _, r := range results {
fmt.Printf("%#v\n", r.Account)
}
}
}