Skip to content

Commit

Permalink
Merge pull request #52 from ipfs/fix/empty-link
Browse files Browse the repository at this point in the history
error when trying to encode an empty link
  • Loading branch information
Stebalien authored Nov 2, 2018
2 parents c89ffec + 8f864a9 commit a591956
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
3 changes: 3 additions & 0 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,5 +534,8 @@ func castBytesToCid(x []byte) (cid.Cid, error) {
}

func castCidToBytes(link cid.Cid) ([]byte, error) {
if !link.Defined() {
return nil, ErrEmptyLink
}
return append([]byte{0}, link.Bytes()...), nil
}
25 changes: 21 additions & 4 deletions node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ func TestBasicMarshal(t *testing.T) {
"name": "foo",
"bar": c,
}
fmt.Printf("cid: %s\n", c.String())
nd, err := WrapObject(obj, mh.SHA2_256, -1)
if err != nil {
t.Fatal(err)
Expand All @@ -103,9 +102,6 @@ func TestBasicMarshal(t *testing.T) {
t.Fatal(err)
}

fmt.Printf("before %v\n", nd.RawData())
fmt.Printf("after %v\n", back.RawData())

if err := assertCid(back.Cid(), "zdpuApUZEHofKXuTs2Yv2CLBeiASQrc9FojFLSZWcyZq6dZhb"); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -529,6 +525,27 @@ func TestCidAndBigInt(t *testing.T) {
}
}

func TestEmptyCid(t *testing.T) {
type Foo struct {
A cid.Cid
}
type Bar struct {
A cid.Cid `refmt:",omitempty"`
}
RegisterCborType(Foo{})
RegisterCborType(Bar{})

_, err := WrapObject(&Foo{}, mh.SHA2_256, -1)
if err == nil {
t.Fatal("should have failed to encode an object with an empty but non-omitted CID")
}

_, err = WrapObject(&Bar{}, mh.SHA2_256, -1)
if err != nil {
t.Fatal(err)
}
}

func TestCanonicalStructEncoding(t *testing.T) {
type Foo struct {
Zebra string
Expand Down

0 comments on commit a591956

Please sign in to comment.