forked from danieljoos/wincred
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwincred_test.go
More file actions
118 lines (102 loc) · 3.13 KB
/
wincred_test.go
File metadata and controls
118 lines (102 loc) · 3.13 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
package wincred
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
const (
testTargetName = "github.com/danieljoos/wincred/testing"
testTargetNameMissing = "github.com/danieljoos/wincred/missing"
)
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. Delete it
err = cred.Delete()
assert.Nil(t, err)
// 5. Search it again in the 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)
// ERROR_NOT_FOUND (1168):
// MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681383(v=vs.85).aspx
assert.Equal(t, "Element not found.", err.Error())
}
func TestGetGenericCredential_Empty(t *testing.T) {
cred, err := GetGenericCredential("")
assert.Nil(t, cred)
assert.NotNil(t, err)
// ERROR_INVALID_PARAMETER (87):
// MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx
assert.Equal(t, "The parameter is incorrect.", err.Error())
}
func TestGenericCredential_WriteEmpty(t *testing.T) {
cred := NewGenericCredential("")
err := cred.Write()
assert.NotNil(t, err)
// ERROR_INVALID_PARAMETER (87):
// MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx.
assert.Equal(t, "The parameter is incorrect.", err.Error())
}
func TestGenericCredential_DeleteNotFound(t *testing.T) {
cred := NewGenericCredential(testTargetNameMissing)
err := cred.Delete()
assert.NotNil(t, err)
// ERROR_NOT_FOUND (1168):
// MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/ms681383(v=vs.85).aspx
assert.Equal(t, "Element not found.", err.Error())
}
func ExampleList() {
if creds, err := List(); err == nil {
for _, cred := range creds {
fmt.Println(cred.TargetName)
}
}
}
func ExampleGetGenericCredential() {
if cred, err := GetGenericCredential("myGoApplication"); err == nil {
fmt.Println(cred.TargetName, string(cred.CredentialBlob))
}
}
func ExampleGenericCredential_Delete() {
cred, _ := GetGenericCredential("myGoApplication")
if err := cred.Delete(); err == nil {
fmt.Println("Deleted")
}
}
func ExampleGenericCredential_Write() {
cred := NewGenericCredential("myGoApplication")
cred.CredentialBlob = []byte("my secret")
if err := cred.Write(); err == nil {
fmt.Println("Created")
}
}