This repository has been archived by the owner on Jan 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmulticodec_test.go
64 lines (56 loc) · 1.6 KB
/
multicodec_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
package dagpb_test
import (
"bytes"
"testing"
dag "github.com/ipfs/go-merkledag"
dagpb "github.com/ipld/go-ipld-prime-proto"
. "github.com/warpfork/go-wish"
)
func TestRoundTripRaw(t *testing.T) {
randBytes := randomBytes(256)
rawNode, err := makeRawNode(randBytes)
Wish(t, err, ShouldEqual, nil)
t.Run("encoding", func(t *testing.T) {
var buf bytes.Buffer
err := dagpb.RawEncoder(rawNode, &buf)
Wish(t, err, ShouldEqual, nil)
Wish(t, buf.Bytes(), ShouldEqual, randBytes)
})
t.Run("decoding", func(t *testing.T) {
buf := bytes.NewBuffer(randBytes)
nb := dagpb.Type.RawNode.NewBuilder()
err := dagpb.RawDecoder(nb, buf)
Wish(t, err, ShouldEqual, nil)
rawNode2 := nb.Build()
Wish(t, rawNode2, ShouldEqual, rawNode)
})
}
func TestRoundTripProtbuf(t *testing.T) {
a := dag.NewRawNode([]byte("aaaa"))
b := dag.NewRawNode([]byte("bbbb"))
c := dag.NewRawNode([]byte("cccc"))
nd1 := &dag.ProtoNode{}
nd1.AddNodeLink("cat", a)
nd2 := &dag.ProtoNode{}
nd2.AddNodeLink("first", nd1)
nd2.AddNodeLink("dog", b)
nd3 := &dag.ProtoNode{}
nd3.AddNodeLink("second", nd2)
nd3.AddNodeLink("bear", c)
data := nd3.RawData()
ibuf := bytes.NewBuffer(data)
nb := dagpb.Type.PBNode.NewBuilder()
err := dagpb.PBDecoder(nb, ibuf)
Wish(t, err, ShouldEqual, nil)
pbNode := nb.Build()
t.Run("encode/decode equivalency", func(t *testing.T) {
var buf bytes.Buffer
err := dagpb.PBEncoder(pbNode, &buf)
Wish(t, err, ShouldEqual, nil)
nb = dagpb.Type.PBNode.NewBuilder()
err = dagpb.PBDecoder(nb, &buf)
pbNode2 := nb.Build()
Wish(t, err, ShouldEqual, nil)
Wish(t, pbNode2, ShouldEqual, pbNode)
})
}