Skip to content

Commit

Permalink
Merge pull request #53 from ipld/bugs/cbor-marshall
Browse files Browse the repository at this point in the history
Fix marshalling error
  • Loading branch information
warpfork authored Apr 28, 2020
2 parents 50e2df1 + 2edb45a commit bd40287
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions codec/dagcbor/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func marshal(n ipld.Node, tk *tok.Token, sink shared.TokenSink) error {
tk.Tagged = true
tk.Tag = linkTag
_, err = sink.Step(tk)
tk.Tagged = false
return err
default:
return fmt.Errorf("schemafree link emission only supported by this codec for CID type links!")
Expand Down
40 changes: 40 additions & 0 deletions codec/dagcbor/roundtrip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ package dagcbor

import (
"bytes"
"context"
"crypto/rand"
"io"
"testing"

cid "github.com/ipfs/go-cid"
. "github.com/warpfork/go-wish"

ipld "github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/fluent"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
)

Expand Down Expand Up @@ -62,3 +68,37 @@ func TestRoundtripScalar(t *testing.T) {
Wish(t, nb.Build(), ShouldEqual, simple)
})
}

func TestRoundtripLinksAndBytes(t *testing.T) {
lb := cidlink.LinkBuilder{cid.Prefix{
Version: 1,
Codec: 0x71,
MhType: 0x17,
MhLength: 4,
}}
buf := bytes.Buffer{}
lnk, err := lb.Build(context.Background(), ipld.LinkContext{}, n,
func(ipld.LinkContext) (io.Writer, ipld.StoreCommitter, error) {
return &buf, func(lnk ipld.Link) error { return nil }, nil
},
)
Require(t, err, ShouldEqual, nil)

var linkByteNode = fluent.MustBuildMap(basicnode.Style__Map{}, 4, func(na fluent.MapAssembler) {
nva := na.AssembleEntry("Link")
nva.AssignLink(lnk)
nva = na.AssembleEntry("Bytes")
bytes := make([]byte, 100)
_, _ = rand.Read(bytes)
nva.AssignBytes(bytes)
})

buf.Reset()
err = Encoder(linkByteNode, &buf)
Require(t, err, ShouldEqual, nil)
nb := basicnode.Style__Map{}.NewBuilder()
err = Decoder(nb, &buf)
Require(t, err, ShouldEqual, nil)
reconstructed := nb.Build()
Wish(t, reconstructed, ShouldEqual, linkByteNode)
}

0 comments on commit bd40287

Please sign in to comment.