-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfixtures.go
128 lines (118 loc) · 3.08 KB
/
fixtures.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
package codec_fixtures
import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/ipfs/go-cid"
_ "github.com/ipld/go-codec-dagpb"
"github.com/ipld/go-ipld-prime"
_ "github.com/ipld/go-ipld-prime/codec/dagcbor"
_ "github.com/ipld/go-ipld-prime/codec/dagjson"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
)
type codecName = string
type codecFixture struct {
codec codecName
cid cid.Cid
value ipld.Node
}
type fixtureSet = map[codecName]codecFixture
var dagPbLp = cidlink.LinkPrototype{Prefix: cid.Prefix{
Version: 1,
Codec: 0x70, // "dag-pb"
MhType: 0x12, // "sha2-256"
MhLength: 32,
}}
var dagCborLp = cidlink.LinkPrototype{Prefix: cid.Prefix{
Version: 1,
Codec: 0x71, // "dag-cbor"
MhType: 0x12, // "sha2-256"
MhLength: 32,
}}
var dagJsonLp = cidlink.LinkPrototype{Prefix: cid.Prefix{
Version: 1,
Codec: 0x0129, // "dag-json"
MhType: 0x12, // "sha2-256"
MhLength: 32,
}}
var codecs = map[codecName]ipld.LinkPrototype{
"dag-pb": dagPbLp,
"dag-cbor": dagCborLp,
"dag-json": dagJsonLp,
}
var linkSystem = cidlink.DefaultLinkSystem()
func loadFixture(dir string) (fixtureSet, error) {
files, err := os.ReadDir("../fixtures/" + dir)
fixtures := make(fixtureSet)
if err != nil {
return fixtures, err
}
for _, file := range files {
if file.IsDir() {
return fixtures, fmt.Errorf("%v is a directory", file.Name())
}
// fmt.Printf("Loading file %v\n", file.Name())
ext := filepath.Ext(file.Name())
cid, err := cid.Decode(strings.TrimSuffix(file.Name(), ext))
if err != nil {
return fixtures, err
}
byts, err := os.ReadFile("../fixtures/" + dir + "/" + file.Name())
if err != nil {
return fixtures, err
}
ext = strings.TrimLeft(ext, ".")
na := basicnode.Prototype.Any.NewBuilder()
lp, ok := codecs[ext]
if !ok {
fmt.Printf("unknown codec '%v' for fixture '%v'\n", ext, dir)
}
decoder, err := linkSystem.DecoderChooser(lp.BuildLink(make([]byte, 32)))
if err != nil {
return fixtures, err
}
err = decoder(na, bytes.NewReader(byts))
if err != nil {
return fixtures, fmt.Errorf("failed to decode using %s: %w", ext, err)
}
fixtures[ext] = codecFixture{
codec: ext,
cid: cid,
value: na.Build(),
}
}
return fixtures, nil
}
func nodeToCid(lp ipld.LinkPrototype, node ipld.Node) (cid.Cid, error) {
encoder, err := linkSystem.EncoderChooser(lp)
if err != nil {
return cid.Cid{}, fmt.Errorf("could not choose an encoder: %v", err)
}
hasher, err := linkSystem.HasherChooser(lp)
if err != nil {
return cid.Cid{}, fmt.Errorf("could not choose a hasher: %v", err)
}
err = encoder(node, hasher)
if err != nil {
return cid.Cid{}, err
}
lnk := lp.BuildLink(hasher.Sum(nil))
cidLink, ok := lnk.(cidlink.Link)
if !ok {
return cid.Cid{}, err
}
return cidLink.Cid, nil
}
type negativeFixtureEncode struct {
Name string `json:"name"`
DagJson interface{} `json:"dag-json,omitempty"`
Error string `json:"error"`
}
type negativeFixtureDecode struct {
Name string `json:"name"`
Hex string `json:"hex"`
Error string `json:"error"`
}