-
Notifications
You must be signed in to change notification settings - Fork 162
/
store_test.go
134 lines (127 loc) · 3.93 KB
/
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
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
// Copyright 2019 Anapaya Systems
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package trust_test
import (
"context"
"io/ioutil"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/scionproto/scion/go/lib/infra"
"github.com/scionproto/scion/go/lib/infra/modules/trust/v2"
"github.com/scionproto/scion/go/lib/infra/modules/trust/v2/trustdbsqlite"
"github.com/scionproto/scion/go/lib/xtest"
)
func TestStoreLoadCryptoMaterial(t *testing.T) {
tests := map[string]struct {
Prepare func(t *testing.T, scratch string)
ErrAssertion assert.ErrorAssertionFunc
}{
"valid": {
Prepare: func(t *testing.T, scratch string) {
collectTRCs(t, tmpDir, scratch)
collectChains(t, tmpDir, scratch)
},
ErrAssertion: assert.NoError,
},
"unreadable TRC": {
Prepare: func(t *testing.T, scratch string) {
collectChains(t, tmpDir, scratch)
err := ioutil.WriteFile(filepath.Join(scratch, "ISD1-V1.trc"), []byte("no"), 0x000)
require.NoError(t, err)
},
ErrAssertion: assert.Error,
},
"garbage TRC": {
Prepare: func(t *testing.T, scratch string) {
collectChains(t, tmpDir, scratch)
err := ioutil.WriteFile(filepath.Join(scratch, "ISD1-V1.trc"), []byte("no"), 0x777)
require.NoError(t, err)
},
ErrAssertion: assert.Error,
},
"unreadable chain": {
Prepare: func(t *testing.T, scratch string) {
collectTRCs(t, tmpDir, scratch)
err := ioutil.WriteFile(filepath.Join(scratch, "ISD1-AS1-V1.crt"),
[]byte("no"), 0x000)
require.NoError(t, err)
},
ErrAssertion: assert.Error,
},
"garbage chain": {
Prepare: func(t *testing.T, scratch string) {
collectTRCs(t, tmpDir, scratch)
err := ioutil.WriteFile(filepath.Join(scratch, "ISD1-AS1-V1.crt"),
[]byte("no"), 0x777)
require.NoError(t, err)
},
ErrAssertion: assert.Error,
},
}
for n, tc := range tests {
name, test := n, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
scratch, clean := xtest.MustTempDir("", "trust-load-crypto")
defer clean()
db, err := trustdbsqlite.New(":memory:")
require.NoError(t, err)
store := trust.Store{
DB: db,
CryptoProvider: trust.Provider{
DB: db,
},
}
test.Prepare(t, scratch)
err = store.LoadCryptoMaterial(context.Background(), scratch)
test.ErrAssertion(t, err)
if err != nil {
return
}
id := trust.TRCID{ISD: 1, Version: 1}
opts := infra.TRCOpts{AllowInactive: true}
raw, err := store.GetRawTRC(context.Background(), id, opts)
assert.NoError(t, err)
assert.Equal(t, loadTRC(t, trc1v1).Raw, raw)
})
}
}
func collectTRCs(t *testing.T, origDir, outDir string) {
t.Helper()
trcs, err := filepath.Glob(filepath.Join(origDir, "ISD*/trcs/*.trc"))
require.NoError(t, err, help)
require.Greater(t, len(trcs), 0)
for _, trc := range trcs {
raw, err := ioutil.ReadFile(trc)
require.NoError(t, err)
_, file := filepath.Split(trc)
err = ioutil.WriteFile(filepath.Join(outDir, file), raw, 0x777)
require.NoError(t, err)
}
}
func collectChains(t *testing.T, origDir, outDir string) {
t.Helper()
chains, err := filepath.Glob(filepath.Join(origDir, "ISD*/AS*/certs/*.crt"))
require.NoError(t, err, help)
require.Greater(t, len(chains), 0)
for _, trc := range chains {
raw, err := ioutil.ReadFile(trc)
require.NoError(t, err)
_, file := filepath.Split(trc)
err = ioutil.WriteFile(filepath.Join(outDir, file), raw, 0x777)
require.NoError(t, err)
}
}