forked from 99designs/keyring
-
Notifications
You must be signed in to change notification settings - Fork 9
/
file_test.go
115 lines (93 loc) · 2.46 KB
/
file_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
package keyring
import (
"os"
"testing"
)
func TestFileKeyringSetWhenEmpty(t *testing.T) {
k := &fileKeyring{
dir: os.TempDir(),
passwordFunc: FixedStringPrompt("no more secrets"),
}
item := Item{Key: "llamas", Data: []byte("llamas are great")}
if err := k.Set(item); err != nil {
t.Fatal(err)
}
foundItem, err := k.Get("llamas")
if err != nil {
t.Fatal(err)
}
if string(foundItem.Data) != "llamas are great" {
t.Fatalf("Value stored was not the value retrieved: %q", foundItem.Data)
}
if foundItem.Key != "llamas" {
t.Fatalf("Key wasn't persisted: %q", foundItem.Key)
}
}
func TestFileKeyringGetWithSlashes(t *testing.T) {
k := &fileKeyring{
dir: os.TempDir(),
passwordFunc: FixedStringPrompt("no more secrets"),
}
item := Item{Key: "https://aws-sso-portal.awsapps.com/start", Data: []byte("https://aws-sso-portal.awsapps.com/start")}
if err := k.Set(item); err != nil {
t.Fatal(err)
}
if err := k.Remove(item.Key); err != nil {
t.Fatal(err)
}
}
func TestFilenameWithBadChars(t *testing.T) {
a := `abc/.././123`
e := filenameEscape(a)
if e != `abc%2F..%2F.%2F123` {
t.Fatalf("Unexpected result from filenameEscape: %s", e)
}
b := filenameUnescape(e)
if b != a {
t.Fatal("Unexpected filenameEscape")
}
}
type ChangePwGenerator struct {
changePwIndex int
changePwStrings []string
}
func (generator *ChangePwGenerator) nextPassword() string {
nextPassword := generator.changePwStrings[generator.changePwIndex]
generator.changePwIndex++
return nextPassword
}
func ChangePwStringPrompt(values []string) PromptFunc {
generator := &ChangePwGenerator{
changePwStrings: values,
}
return func(_ string) (string, error) {
return generator.nextPassword(), nil
}
}
func TestFileKeyringChangepw(t *testing.T) {
k := &fileKeyring{
dir: os.TempDir(),
passwordFunc: ChangePwStringPrompt([]string{"no more secrets", "new secrets"}),
}
item := Item{Key: "llamas", Data: []byte("llamas are great")}
if err := k.Set(item); err != nil {
t.Fatal(err)
}
if err := k.Changepw(item.Key); err != nil {
t.Fatal(err)
}
k2 := &fileKeyring{
dir: os.TempDir(),
passwordFunc: FixedStringPrompt("new secrets"),
}
foundItem, err := k2.Get(`llamas`)
if err != nil {
t.Fatal(err)
}
if string(foundItem.Data) != "llamas are great" {
t.Fatalf("Value stored was not the value retrieved: %q", foundItem.Data)
}
if foundItem.Key != "llamas" {
t.Fatalf("Key wasn't persisted: %q", foundItem.Key)
}
}