-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathmacos_test.go
More file actions
206 lines (179 loc) · 5.59 KB
/
Copy pathmacos_test.go
File metadata and controls
206 lines (179 loc) · 5.59 KB
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
//go:build darwin && !ios
// +build darwin,!ios
package keychain
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
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 TestGenericPassword(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)
}
err = DeleteItem(item)
if err != nil {
t.Fatal(err)
}
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) {
item := NewItem()
item.SetSecClass(SecClassInternetPassword)
// Internet password-specific attributes
item.SetProtocol("htps")
item.SetServer("8xs8h5x5dfc0AI5EzT81l.com")
item.SetPort(1234)
item.SetPath("/this/is/the/path")
item.SetAccount("this-is-the-username")
item.SetLabel("this is the label")
item.SetData([]byte("this is the password"))
item.SetComment("this is the comment")
defer func() { _ = DeleteItem(item) }()
err := AddItem(item)
if err != nil {
t.Fatal(err)
}
query := NewItem()
query.SetSecClass(SecClassInternetPassword)
query.SetServer("8xs8h5x5dfc0AI5EzT81l.com")
query.SetMatchLimit(MatchLimitOne)
query.SetReturnAttributes(true)
results, err := QueryItem(query)
if err != nil {
t.Fatalf("Query Error: %v", err)
}
if len(results) != 1 {
t.Fatalf("expected 1 result, got %d", len(results))
}
r := results[0]
if r.Protocol != "htps" {
t.Errorf("expected protocol 'htps' but got %q", r.Protocol)
}
if r.Server != "8xs8h5x5dfc0AI5EzT81l.com" {
t.Errorf("expected server '8xs8h5x5dfc0AI5EzT81l.com' but got %q", r.Server)
}
if r.Port != 1234 {
t.Errorf("expected port '1234' but got %d", r.Port)
}
if r.Path != "/this/is/the/path" {
t.Errorf("expected path '/this/is/the/path' but got %q", r.Path)
}
if r.Account != "this-is-the-username" {
t.Errorf("expected account 'this-is-the-username' but got %q", r.Account)
}
if r.Label != "this is the label" {
t.Errorf("expected label 'this is the label' but got %q", r.Label)
}
if r.Comment != "this is the comment" {
t.Errorf("expected comment 'this is the comment' but got %q", r.Comment)
}
}
func TestGenericAttributes(t *testing.T) {
t.Run("generic password can have generic attributes", func(t *testing.T) {
service, account, label, accessGroup, password := "TestGenericPasswordRef", "test2", "generic-password", "TestGenericAttributes", "toomanysecrets"
item := NewGenericPassword(service, account, label, []byte(password), accessGroup)
attributes := map[string]any{
"color": "green",
"large": string(bytes.Repeat([]byte{'a'}, 1024*1024)),
"score": 10,
}
t.Cleanup(func() {
queryDelete := NewItem()
queryDelete.SetAccessGroup(accessGroup)
queryDelete.SetAccount(account)
queryDelete.SetService(service)
queryDelete.SetSecClass(SecClassGenericPassword)
assert.NoError(t, DeleteItem(queryDelete))
})
item.SetGenericMetadata(attributes)
require.NoError(t, AddItem(item))
query := NewItem()
query.SetReturnAttributes(true)
query.SetSecClass(SecClassGenericPassword)
query.SetMatchLimit(MatchLimitOne)
query.SetService(service)
query.SetAccount(account)
query.SetAccessGroup(accessGroup)
query.SetLabel(label)
results, err := QueryItem(query)
require.NoError(t, err)
assert.Len(t, results, 1)
assert.EqualValues(t, attributes, results[0].Attributes)
})
t.Run("internet password cannot set generic attributes", func(t *testing.T) {
item := NewItem()
item.SetSecClass(SecClassInternetPassword)
// Internet password-specific attributes
item.SetProtocol("htps")
item.SetServer("8xs8h5x5dfc0AI5EzT81l.com")
item.SetPort(1234)
item.SetPath("/this/is/the/path")
item.SetAccount("this-is-the-username")
item.SetLabel("this is the label")
item.SetData([]byte("this is the password"))
item.SetGenericMetadata(map[string]any{
"anything": "really",
})
t.Cleanup(func() {
assert.NoError(t, DeleteItem(item))
})
require.NoError(t, AddItem(item))
query := NewItem()
query.SetSecClass(SecClassInternetPassword)
query.SetProtocol("htps")
query.SetServer("8xs8h5x5dfc0AI5EzT81l.com")
query.SetPort(1234)
query.SetPath("/this/is/the/path")
query.SetAccount("this-is-the-username")
query.SetLabel("this is the label")
query.SetReturnAttributes(true)
result, err := QueryItem(query)
assert.Len(t, result, 1)
assert.NoError(t, err)
assert.Empty(t, result[0].Attributes)
})
}