forked from jsipprell/keyctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ref_test.go
85 lines (74 loc) · 1.61 KB
/
ref_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
package keyctl
import (
"syscall"
"testing"
)
func mustInfo(r Reference) Info {
info, err := r.Info()
if err != nil {
if msg := err.Error(); msg == "key has expired" {
return Info{Name: msg}
}
}
return info
}
func helperTestKeyRefs(ring Keyring, t *testing.T) []Reference {
var err error
if ring == nil {
if ring, err = SessionKeyring(); err != nil {
t.Fatal(err)
}
}
refs, err := ListKeyring(ring)
if err != nil {
t.Fatal(err)
}
for _, r := range refs {
t.Logf("%d: %+v [%s]\n", r.Id, mustInfo(r), mustInfo(r).Permissions())
}
return refs
}
func filterErrno(e error, ignore ...syscall.Errno) error {
if en, ok := e.(syscall.Errno); ok {
for _, enok := range ignore {
if enok == en {
return nil
}
}
}
return e
}
func helperRecurseKeyringRefs(kr Keyring, t *testing.T) {
for _, r := range helperTestKeyRefs(kr, t) {
if !r.Valid() {
continue
}
key, err := r.Get()
if filterErrno(err, syscall.EPERM, syscall.EACCES) != nil {
t.Fatal(err)
}
if err != nil {
return
}
switch k := key.(type) {
case *namedKeyring:
t.Logf("keyring %v: %q, parent %v", k.id, k.Name(), k.parent)
helperRecurseKeyringRefs(k, t)
case *keyring:
t.Logf("keyring %v", k.id)
helperRecurseKeyringRefs(k, t)
case *Key:
t.Logf("key %v: %q, keyring %v", k.id, k.Name, k.ring)
data, err := k.Get()
if filterErrno(err, syscall.EPERM, syscall.EACCES) != nil {
t.Fatalf("%v %T(%d)", err, err, err)
}
t.Logf(" %v: %v", k.id, data)
default:
panic("unsupported type")
}
}
}
func TestSessionKeyringRefs(t *testing.T) {
helperRecurseKeyringRefs(nil, t)
}