forked from 99designs/go-keychain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
macos_test.go
234 lines (204 loc) · 5.65 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// +build darwin,!ios
package keychain
import (
"fmt"
"os"
"path/filepath"
"testing"
"time"
)
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 TestAddingAndQueryingNewKeychain(t *testing.T) {
keychainPath := tempPath(t)
defer func() { _ = os.Remove(keychainPath) }()
service, account, label, accessGroup, password := "TestAddingAndQueryingNewKeychain", "test", "", "", "toomanysecrets"
k, err := NewKeychain(keychainPath, "my password")
if err != nil {
t.Fatal(err)
}
item := NewGenericPassword(service, account, label, []byte(password), accessGroup)
item.UseKeychain(k)
if err = AddItem(item); err != nil {
t.Fatal(err)
}
query := NewItem()
query.SetSecClass(SecClassGenericPassword)
query.SetMatchSearchList(k)
query.SetService(service)
query.SetAccount(account)
query.SetLabel(label)
query.SetAccessGroup(accessGroup)
query.SetMatchLimit(MatchLimitOne)
query.SetReturnData(true)
results, err := QueryItem(query)
if err != nil {
t.Fatal(err)
}
if len(results) != 1 {
t.Fatalf("Expected 1 result, got %d", len(results))
} else if string(results[0].Data) != password {
t.Fatalf("Expected password to be %s, got %s", password, results[0].Data)
}
// Search default keychain to make sure it's not there
queryDefault := NewItem()
queryDefault.SetSecClass(SecClassGenericPassword)
queryDefault.SetService(service)
queryDefault.SetMatchLimit(MatchLimitOne)
queryDefault.SetReturnData(true)
resultsDefault, err := QueryItem(queryDefault)
if err != nil {
t.Fatal(err)
}
if len(resultsDefault) != 0 {
t.Fatalf("Expected no results")
}
}
func tempPath(t *testing.T) string {
temp, err := RandomID("go-keychain-test-")
if err != nil {
t.Fatal(err)
}
return filepath.Join(os.TempDir(), temp+".keychain")
}
func TestNewWithPath(t *testing.T) {
path := tempPath(t)
defer func() { _ = os.Remove(path) }()
kc, newErr := NewKeychain(path, "testkeychainpassword")
if newErr != nil {
t.Fatal(newErr)
}
item := NewGenericPassword("MyService2", "gabriel2", "", []byte("toomanysecrets2"), "")
item.UseKeychain(kc)
if err := AddItem(item); err != nil {
t.Fatal(err)
}
kc2 := NewWithPath(path)
if lockErr := LockAtPath(path); lockErr != nil {
t.Fatal(lockErr)
}
if unlockErr := UnlockAtPath(path, "testkeychainpassword"); unlockErr != nil {
t.Fatal(unlockErr)
}
query := NewItem()
query.SetMatchSearchList(kc2)
query.SetService("MyService2")
query.SetAccount("gabriel2")
query.SetSecClass(SecClassGenericPassword)
query.SetMatchLimit(MatchLimitOne)
query.SetReturnAttributes(true)
query.SetReturnData(true)
results, err := QueryItem(query)
if err != nil {
t.Fatal(err)
}
if len(results) != 1 {
t.Fatalf("Should have 1 result, had %d", len(results))
}
if string(results[0].Data) != "toomanysecrets2" {
t.Fatalf("Invalid password: %s", results[0].Data)
}
if results[0].CreationDate.Equal(time.Time{}) {
t.Fatal("Got null creation date")
}
if results[0].ModificationDate.Equal(time.Time{}) {
t.Fatal("Got null creation date")
}
}
func TestStatus(t *testing.T) {
path := tempPath(t)
defer func() { _ = os.Remove(path) }()
k, newErr := NewKeychain(path, "testkeychainpassword")
if newErr != nil {
t.Fatal(newErr)
}
if err := k.Status(); err != nil {
t.Fatal(err)
}
nonexistent := NewWithPath(fmt.Sprintf("this_shouldnt_exist_%s", time.Now()))
if err := nonexistent.Status(); err != ErrorNoSuchKeychain {
t.Fatalf("Expected %v, get %v", ErrorNoSuchKeychain, err)
}
}
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)
}
}
}