forked from 99designs/keyring
-
Notifications
You must be signed in to change notification settings - Fork 9
/
secretservice_test.go
168 lines (136 loc) · 3.6 KB
/
secretservice_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
//go:build linux
// +build linux
package keyring
import (
"os"
"sort"
"testing"
"github.com/gsterjov/go-libsecret"
)
// NOTE: These tests are not runnable from a headless environment such as
// Docker or a CI pipeline due to the DBus "prompt" interface being called
// by the underlying go-libsecret when creating and unlocking a keychain.
//
// TODO: Investigate a way to automate the prompting. Some ideas:
//
// 1. I've looked extensively but have not found a headless CLI tool that
// could be run in the background of eg: a docker container
// 2. It might be possible to make a mock prompter that connects to DBus
// and provides the Prompt interface using the go-libsecret library.
func libSecretSetup(t *testing.T) (Keyring, func(t *testing.T)) {
t.Helper()
if os.Getenv("GITHUB_ACTIONS") != "" {
t.Skip("Skipping testing in CI environment")
}
service, err := libsecret.NewService()
if err != nil {
t.Fatal(err)
}
kr := &secretsKeyring{
name: "keyring-test",
service: service,
}
return kr, func(t *testing.T) {
t.Helper()
if err := kr.deleteCollection(); err != nil {
t.Fatal(err)
}
}
}
func TestLibSecretKeysWhenEmpty(t *testing.T) {
kr, _ := libSecretSetup(t)
keys, err := kr.Keys()
if err != nil {
t.Fatal(err)
}
if len(keys) != 0 {
t.Fatalf("Expected 0 keys, got %d", len(keys))
}
}
func TestLibSecretKeysWhenNotEmpty(t *testing.T) {
kr, teardown := libSecretSetup(t)
defer teardown(t)
item := Item{Key: "llamas", Data: []byte("llamas are great")}
item2 := Item{Key: "alpacas", Data: []byte("alpacas are better")}
if err := kr.Set(item); err != nil {
t.Fatal(err)
}
if err := kr.Set(item2); err != nil {
t.Fatal(err)
}
keys, err := kr.Keys()
if err != nil {
t.Fatal(err)
}
if len(keys) != 2 {
t.Fatalf("Expected 2 keys, got %d", len(keys))
}
sort.Strings(keys)
if keys[0] != "alpacas" {
t.Fatalf("Expected alpacas")
}
if keys[1] != "llamas" {
t.Fatalf("Expected llamas")
}
}
func TestLibSecretGetWhenEmpty(t *testing.T) {
kr, _ := libSecretSetup(t)
_, err := kr.Get("llamas")
if err != ErrKeyNotFound {
t.Fatalf("Expected ErrKeyNotFound, got: %s", err)
}
}
func TestLibSecretGetWhenNotEmpty(t *testing.T) {
kr, teardown := libSecretSetup(t)
defer teardown(t)
item := Item{Key: "llamas", Data: []byte("llamas are great")}
if err := kr.Set(item); err != nil {
t.Fatal(err)
}
it, err := kr.Get(item.Key)
if err != nil {
t.Fatal(err)
}
if it.Key != item.Key {
t.Fatal("Expected item not returned")
}
}
func TestLibSecretRemoveWhenEmpty(t *testing.T) {
kr, _ := libSecretSetup(t)
err := kr.Remove("no-such-key")
if err != ErrKeyNotFound {
t.Fatalf("Expected ErrKeyNotFound, got: %s", err)
}
}
func TestLibSecretRemoveWhenNotEmpty(t *testing.T) {
kr, teardown := libSecretSetup(t)
defer teardown(t)
item := Item{Key: "llamas", Data: []byte("llamas are great")}
if err := kr.Set(item); err != nil {
t.Fatal(err)
}
if _, err := kr.Get("llamas"); err != nil {
t.Fatal(err)
}
if err := kr.Remove("llamas"); err != nil {
t.Fatal(err)
}
}
func TestLibSpecialCharacters(t *testing.T) {
decoded := decodeKeyringString("keyring_2dtest")
if decoded != "keyring-test" {
t.Fatal("incorrect decodeKeyringString")
}
decoded = decodeKeyringString("keyring_2d_2dtest")
if decoded != "keyring--test" {
t.Fatal("incorrect decodeKeyringString")
}
decoded = decodeKeyringString("keyring_2dtest_2d_2d")
if decoded != "keyring-test--" {
t.Fatal("incorrect decodeKeyringString")
}
decoded = decodeKeyringString("_2d_2dkeyring_2dtest_2d_2d")
if decoded != "--keyring-test--" {
t.Fatal("incorrect decodeKeyringString")
}
}