forked from ipfs/go-ipld-format
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coding_test.go
50 lines (42 loc) · 1009 Bytes
/
coding_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
package format
import (
"errors"
"testing"
blocks "github.com/dms3-fs/go-block-format"
cid "github.com/dms3-fs/go-cid"
mh "github.com/dms3-mft/go-multihash"
)
func init() {
Register(cid.Raw, func(b blocks.Block) (Node, error) {
node := &EmptyNode{}
if b.RawData() != nil || !b.Cid().Equals(node.Cid()) {
return nil, errors.New("can only decode empty blocks")
}
return node, nil
})
}
func TestDecode(t *testing.T) {
id, err := cid.Prefix{
Version: 1,
Codec: cid.Raw,
MhType: mh.ID,
MhLength: 0,
}.Sum(nil)
if err != nil {
t.Fatalf("failed to create cid: %s", err)
}
block, err := blocks.NewBlockWithCid(nil, id)
if err != nil {
t.Fatalf("failed to create empty block: %s", err)
}
node, err := Decode(block)
if err != nil {
t.Fatalf("failed to decode empty node: %s", err)
}
if !node.Cid().Equals(id) {
t.Fatalf("empty node doesn't have the right cid")
}
if _, ok := node.(*EmptyNode); !ok {
t.Fatalf("empty node doesn't have the right type")
}
}