forked from ucan-wg/go-ucan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixtures_test.go
More file actions
118 lines (102 loc) · 2.68 KB
/
fixtures_test.go
File metadata and controls
118 lines (102 loc) · 2.68 KB
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
package invocation_test
import (
_ "embed"
"fmt"
"os"
"testing"
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/codec/dagcbor"
"github.com/ipld/go-ipld-prime/codec/dagjson"
"github.com/ipld/go-ipld-prime/schema"
"github.com/stretchr/testify/require"
"github.com/ucan-wg/go-ucan/token/delegation"
"github.com/ucan-wg/go-ucan/token/internal/envelope"
"github.com/ucan-wg/go-ucan/token/invocation"
)
//go:embed fixtures.ipldsch
var schemaBytes []byte
func fixturesType(t *testing.T) schema.Type {
ts, err := ipld.LoadSchemaBytes(schemaBytes)
require.NoError(t, err)
return ts.TypeByName("Fixtures")
}
type ErrorModel struct {
Name string
}
type VectorModel struct {
Name string
Description string
Invocation []byte
Proofs [][]byte
Error *ErrorModel
}
type FixturesModel struct {
Version string
Comments string
Valid []VectorModel
Invalid []VectorModel
}
type NamedError interface {
error
Name() string
}
func TestFixtures(t *testing.T) {
fixturesFile, err := os.Open("./testdata/invocations.json")
require.NoError(t, err)
var fixtures FixturesModel
_, err = ipld.UnmarshalStreaming(fixturesFile, dagjson.Decode, &fixtures, fixturesType(t))
require.NoError(t, err)
for _, vector := range fixtures.Valid {
t.Run("valid "+vector.Name, func(t *testing.T) {
err := decodeAndValidate(vector)
require.NoError(t, err, "execution should have been allowed for invocation with %s", vector.Description)
})
}
for _, vector := range fixtures.Invalid {
t.Run("invalid "+vector.Name, func(t *testing.T) {
err := decodeAndValidate(vector)
require.Error(t, err, "execution should not have been allowed for invocation because %s", vector.Description)
})
}
}
type mapDelegationLoader struct {
data map[cid.Cid]*delegation.Token
}
func (ml *mapDelegationLoader) GetDelegation(cid cid.Cid) (*delegation.Token, error) {
d, ok := ml.data[cid]
if !ok {
return nil, fmt.Errorf("delegation not found: %s", cid)
}
return d, nil
}
func decodeAndValidate(vector VectorModel) error {
inv, err := invocation.Decode(vector.Invocation, dagcbor.Decode)
if err != nil {
return err
}
proofs, err := decodeProofs(vector.Proofs)
if err != nil {
return err
}
err = inv.ExecutionAllowed(&mapDelegationLoader{proofs})
if err != nil {
return err
}
return nil
}
func decodeProofs(vectorProofs [][]byte) (map[cid.Cid]*delegation.Token, error) {
proofs := map[cid.Cid]*delegation.Token{}
for _, p := range vectorProofs {
dlg, err := delegation.Decode(p, dagcbor.Decode)
if err != nil {
return nil, err
}
c, err := envelope.CIDFromBytes(p)
if err != nil {
return nil, err
}
proofs[c] = dlg
}
return proofs, nil
}