-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathbind.go
183 lines (154 loc) · 4.79 KB
/
bind.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
//go:build darwin || ios
// +build darwin ios
package bind
import (
"fmt"
"reflect"
"github.com/keybase/go-keychain"
)
// Test is a bind interface for the test
type Test interface {
Fail(s string)
}
// AddGenericPassword adds generic password
func AddGenericPassword(service string, account string, label string, password string, accessGroup string) error {
item := keychain.NewGenericPassword(service, account, label, []byte(password), accessGroup)
return keychain.AddItem(item)
}
// DeleteGenericPassword deletes generic password
func DeleteGenericPassword(service string, account string, accessGroup string) error {
item := keychain.NewItem()
item.SetSecClass(keychain.SecClassGenericPassword)
item.SetService(service)
item.SetAccount(account)
item.SetAccessGroup(accessGroup)
return keychain.DeleteItem(item)
}
// GenericPasswordTest runs test code for generic password keychain item.
// This is here so we can export using gomobile bind and run this method on iOS simulator and device.
// Access groups aren't supported in iOS simulator.
func GenericPasswordTest(t Test, service string, accessGroup string) {
var err error
account := "Testing account with unicode テスト"
item := keychain.NewGenericPassword(service, account, "", []byte("toomanysecrets"), accessGroup)
item.SetSynchronizable(keychain.SynchronizableNo)
item.SetAccessible(keychain.AccessibleWhenUnlocked)
account2 := "Testing account #2"
item2 := keychain.NewGenericPassword(service, account2, "", []byte("toomanysecrets2"), accessGroup)
// Cleanup
defer func() { _ = keychain.DeleteItem(item) }()
defer func() { _ = keychain.DeleteItem(item2) }()
// Test account names empty
accounts, err := keychain.GetGenericPasswordAccounts(service)
if err != nil {
t.Fail(err.Error())
}
if len(accounts) != 0 {
t.Fail("Should have no accounts")
}
// Test add
err = keychain.AddItem(item)
if err != nil {
t.Fail(err.Error())
}
// Test dupe
err = keychain.AddItem(item)
if err != keychain.ErrorDuplicateItem {
t.Fail("Should error with duplicate item")
}
// Test add another
err = keychain.AddItem(item2)
if err != nil {
t.Fail(err.Error())
}
// Test querying attributes
query := keychain.NewItem()
query.SetSecClass(keychain.SecClassGenericPassword)
query.SetService(service)
query.SetAccount(account)
query.SetAccessGroup(accessGroup)
query.SetMatchLimit(keychain.MatchLimitAll)
query.SetReturnAttributes(true)
results, err := keychain.QueryItem(query)
if err != nil {
t.Fail(err.Error())
}
if len(results) != 1 {
t.Fail(fmt.Sprintf("Invalid results count: %d", len(results)))
}
if results[0].Service != service {
t.Fail(fmt.Sprintf("Invalid service, %v != %v, %v", results[0].Service, service, results))
}
if results[0].Account != account {
t.Fail(fmt.Sprintf("Invalid account, %v != %v, %v", results[0].Account, account, results))
}
if len(results[0].Data) != 0 {
t.Fail("Password shouldn't come back when returning attributes")
}
// Test querying data
queryData := keychain.NewItem()
queryData.SetSecClass(keychain.SecClassGenericPassword)
queryData.SetService(service)
queryData.SetAccount(account)
queryData.SetAccessGroup(accessGroup)
queryData.SetMatchLimit(keychain.MatchLimitOne)
queryData.SetReturnData(true)
resultsData, err := keychain.QueryItem(queryData)
if err != nil {
t.Fail(err.Error())
}
if len(resultsData) != 1 {
t.Fail("Too many results")
}
if string(resultsData[0].Data) != "toomanysecrets" {
t.Fail("Invalid password")
}
// Test account names
accounts2, err := keychain.GetGenericPasswordAccounts(service)
if err != nil {
t.Fail(err.Error())
}
if len(accounts2) != 2 {
t.Fail(fmt.Sprintf("Should have 2 accounts: %v", accounts2))
}
if !reflect.DeepEqual(accounts2, []string{account, account2}) {
t.Fail(fmt.Sprintf("Invalid accounts: %v", accounts2))
}
// Remove
queryDel := keychain.NewItem()
queryDel.SetSecClass(keychain.SecClassGenericPassword)
queryDel.SetService(service)
queryDel.SetAccount(account)
queryDel.SetAccessGroup(accessGroup)
err = keychain.DeleteItem(queryDel)
if err != nil {
t.Fail(err.Error())
}
// Test removed
query3 := keychain.NewItem()
query3.SetSecClass(keychain.SecClassGenericPassword)
query3.SetService(service)
query3.SetAccount(account)
query3.SetAccessGroup(accessGroup)
query3.SetMatchLimit(keychain.MatchLimitAll)
query3.SetReturnAttributes(true)
results3, err := keychain.QueryItem(query3)
if err != nil {
t.Fail(err.Error())
}
if len(results3) != 0 {
t.Fail("Results should have been empty")
}
accounts3, err := keychain.GetGenericPasswordAccounts(service)
if err != nil {
t.Fail(err.Error())
}
if len(accounts3) != 1 {
t.Fail("Should have an account")
}
// Test remove not found
err = keychain.DeleteItem(item)
if err != keychain.ErrorItemNotFound {
t.Fail("Error should be not found")
}
}