-
Notifications
You must be signed in to change notification settings - Fork 19
/
wincred_test.go
182 lines (157 loc) · 4.79 KB
/
wincred_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
//go:build windows
// +build windows
package wincred
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
const (
testTargetName = "github.com/danieljoos/wincred/testing"
testTargetNameMissing = "github.com/danieljoos/wincred/missing"
testListFilter = "github.com/danieljoos*"
testDomainTargetName = "emea.acme-corp.net"
testDomainTargetNameMissing = "unknown-corp.net"
)
func TestGenericCredential_EndToEnd(t *testing.T) {
// 1. Create new credential `foo`
cred := NewGenericCredential(testTargetName)
cred.CredentialBlob = []byte("my secret")
cred.Persist = PersistSession
err := cred.Write()
assert.Nil(t, err)
// 2. Get the credential from the store
cred, err = GetGenericCredential(testTargetName)
assert.Nil(t, err)
assert.NotNil(t, cred)
assert.Equal(t, "my secret", string(cred.CredentialBlob))
// 3. Search it in the list
creds, err := List()
assert.Nil(t, err)
assert.NotNil(t, creds)
assert.NotEqual(t, 0, len(creds))
found := false
for i := range creds {
found = found || creds[i].TargetName == testTargetName
}
assert.True(t, found)
// 4. Search it also in a filtered list
creds, err = FilteredList(testListFilter)
assert.Nil(t, err)
assert.NotNil(t, creds)
assert.NotEqual(t, 0, len(creds))
found = false
for i := range creds {
found = found || creds[i].TargetName == testTargetName
}
assert.True(t, found)
// 5. Delete it
err = cred.Delete()
assert.Nil(t, err)
// 6. Search it again in the complete list. It should be gone.
creds, err = List()
assert.Nil(t, err)
assert.NotNil(t, creds)
found = false
for i := range creds {
found = found || creds[i].TargetName == testTargetName
}
assert.False(t, found)
}
func TestGetGenericCredential_NotFound(t *testing.T) {
cred, err := GetGenericCredential(testTargetNameMissing)
assert.Nil(t, cred)
assert.NotNil(t, err)
assert.True(t, errors.Is(err, ErrElementNotFound))
}
func TestGetGenericCredential_Empty(t *testing.T) {
cred, err := GetGenericCredential("")
assert.Nil(t, cred)
assert.NotNil(t, err)
assert.True(t, errors.Is(err, ErrInvalidParameter))
}
func TestGenericCredential_WriteEmpty(t *testing.T) {
cred := NewGenericCredential("")
err := cred.Write()
assert.NotNil(t, err)
assert.True(t, errors.Is(err, ErrInvalidParameter))
}
func TestGenericCredential_DeleteNotFound(t *testing.T) {
cred := NewGenericCredential(testTargetNameMissing)
err := cred.Delete()
assert.NotNil(t, err)
assert.True(t, errors.Is(err, ErrElementNotFound))
}
func TestDomainPassword_EndToEnd(t *testing.T) {
// 1. Create new domain password credentials
cred := NewDomainPassword(testDomainTargetName)
cred.UserName = "johndoe"
cred.SetPassword("s3cr3t!")
err := cred.Write()
assert.Nil(t, err)
// 2. Get the new credentials from the store.
// For domain passwords, we're not allowed to fetch the actual password data.
cred, err = GetDomainPassword(testDomainTargetName)
assert.NotNil(t, cred)
assert.Nil(t, err)
assert.Equal(t, cred.UserName, "johndoe")
// 3. Search it in the list
creds, err := List()
assert.Nil(t, err)
assert.NotNil(t, creds)
assert.NotEqual(t, 0, len(creds))
found := false
for i := range creds {
found = found || creds[i].TargetName == testDomainTargetName
}
assert.True(t, found)
// 4. Delete it
err = cred.Delete()
assert.Nil(t, err)
// 5. Try to get it again - should be gone now
cred, err = GetDomainPassword(testDomainTargetName)
assert.Nil(t, cred)
assert.NotNil(t, err)
assert.True(t, errors.Is(err, ErrElementNotFound))
}
func TestGetDomainPassword_NotFound(t *testing.T) {
cred, err := GetDomainPassword(testDomainTargetNameMissing)
assert.NotNil(t, err)
assert.Nil(t, cred)
assert.True(t, errors.Is(err, ErrElementNotFound))
}
func TestGetDomainPassword_InvalidTargetName(t *testing.T) {
invalidTargetNames := []string{"", "Invalid\\Domain\\Target"}
for _, targetName := range invalidTargetNames {
t.Run(targetName, func(t *testing.T) {
cred, err := GetDomainPassword(targetName)
assert.NotNil(t, err)
assert.Nil(t, cred)
assert.True(t, errors.Is(err, ErrInvalidParameter))
})
}
}
func TestDomainPassword_WriteEmpty(t *testing.T) {
cred := NewDomainPassword("")
err := cred.Write()
assert.NotNil(t, err)
assert.True(t, errors.Is(err, ErrInvalidParameter))
}
func TestDomainPassword_BadUsername(t *testing.T) {
cred := NewDomainPassword(testDomainTargetName)
cred.UserName = ""
err := cred.Write()
assert.NotNil(t, err)
assert.True(t, errors.Is(err, ErrBadUsername))
}
func TestDomainPassword_DeleteNotFound(t *testing.T) {
cred := NewDomainPassword(testDomainTargetName)
err := cred.Delete()
assert.NotNil(t, err)
assert.True(t, errors.Is(err, ErrElementNotFound))
}
func TestFilteredList_Empty(t *testing.T) {
list, err := FilteredList(testDomainTargetNameMissing)
assert.Nil(t, err)
assert.Empty(t, list)
}