forked from theupdateframework/go-tuf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_store_test.go
70 lines (57 loc) · 1.9 KB
/
local_store_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
package tuf
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/theupdateframework/go-tuf/pkg/keys"
)
func TestLocalStoreSigners(t *testing.T) {
tmpdir, err := os.MkdirTemp("", "")
if err != nil {
t.Fatal(err)
}
defer func() {
os.RemoveAll(tmpdir)
}()
stores := map[string]LocalStore{
"MemoryStore": MemoryStore(nil, nil),
"FileSystemStore": FileSystemStore(tmpdir, nil),
}
for name, store := range stores {
t.Run(name, func(t *testing.T) {
signers, err := store.GetSigners("abc")
assert.NoError(t, err)
assert.Equal(t, len(signers), 0)
// Add two signers to role "a".
aSigner1, err := keys.GenerateEd25519Key()
assert.NoError(t, err)
err = store.SaveSigner("a", aSigner1)
assert.NoError(t, err)
aSigner2, err := keys.GenerateEd25519Key()
assert.NoError(t, err)
err = store.SaveSigner("a", aSigner2)
assert.NoError(t, err)
// Add one signer to role "b".
bSigner, err := keys.GenerateEd25519Key()
assert.NoError(t, err)
err = store.SaveSigner("b", bSigner)
assert.NoError(t, err)
// Add to b again to test deduplication.
err = store.SaveSigner("b", bSigner)
assert.NoError(t, err)
signers, err = store.GetSigners("a")
assert.NoError(t, err)
assert.ElementsMatch(t, []keys.Signer{aSigner1, aSigner2}, signers)
signers, err = store.GetSigners("b")
assert.NoError(t, err)
assert.ElementsMatch(t, []keys.Signer{bSigner}, signers)
a1KeyIDs := aSigner1.PublicData().IDs()
a2KeyIDs := aSigner2.PublicData().IDs()
bKeyIDs := bSigner.PublicData().IDs()
assert.Equal(t, []keys.Signer{aSigner1}, store.SignersForKeyIDs(a1KeyIDs))
assert.Equal(t, []keys.Signer{aSigner2}, store.SignersForKeyIDs(a2KeyIDs))
assert.ElementsMatch(t, []keys.Signer{aSigner1, aSigner2}, store.SignersForKeyIDs(append(a1KeyIDs, a2KeyIDs...)))
assert.Equal(t, []keys.Signer{bSigner}, store.SignersForKeyIDs(bKeyIDs))
})
}
}