-
Notifications
You must be signed in to change notification settings - Fork 122
/
macos_test.go
129 lines (111 loc) · 3.26 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
//go:build darwin && !ios
// +build darwin,!ios
package keychain
import (
"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 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)
}
}