Skip to content

Commit 6b7c2fe

Browse files
committed
improve test coverage on merkledag package
License: MIT Signed-off-by: Jeromy <[email protected]>
1 parent 8830aae commit 6b7c2fe

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

merkledag/merkledag_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
imp "github.com/ipfs/go-ipfs/importer"
1818
chunk "github.com/ipfs/go-ipfs/importer/chunk"
1919
. "github.com/ipfs/go-ipfs/merkledag"
20+
mdpb "github.com/ipfs/go-ipfs/merkledag/pb"
2021
dstest "github.com/ipfs/go-ipfs/merkledag/test"
2122
uio "github.com/ipfs/go-ipfs/unixfs/io"
2223
u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
@@ -354,3 +355,27 @@ func TestFetchFailure(t *testing.T) {
354355
}
355356
}
356357
}
358+
359+
func TestUnmarshalFailure(t *testing.T) {
360+
badData := []byte("hello world")
361+
362+
_, err := DecodeProtobuf(badData)
363+
if err == nil {
364+
t.Fatal("shouldnt succeed to parse this")
365+
}
366+
367+
// now with a bad link
368+
pbn := &mdpb.PBNode{Links: []*mdpb.PBLink{{Hash: []byte("not a multihash")}}}
369+
badlink, err := pbn.Marshal()
370+
if err != nil {
371+
t.Fatal(err)
372+
}
373+
374+
_, err = DecodeProtobuf(badlink)
375+
if err == nil {
376+
t.Fatal("should have failed to parse node with bad link")
377+
}
378+
379+
n := &Node{}
380+
n.Marshal()
381+
}

merkledag/node_test.go

+78-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
package merkledag
1+
package merkledag_test
22

33
import (
44
"testing"
5+
6+
. "github.com/ipfs/go-ipfs/merkledag"
7+
mdtest "github.com/ipfs/go-ipfs/merkledag/test"
8+
9+
"gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
510
)
611

712
func TestRemoveLink(t *testing.T) {
@@ -52,3 +57,75 @@ func TestRemoveLink(t *testing.T) {
5257
t.Fatal("link order wrong")
5358
}
5459
}
60+
61+
func TestFindLink(t *testing.T) {
62+
ds := mdtest.Mock()
63+
k, err := ds.Add(new(Node))
64+
if err != nil {
65+
t.Fatal(err)
66+
}
67+
68+
nd := &Node{
69+
Links: []*Link{
70+
&Link{Name: "a", Hash: k.ToMultihash()},
71+
&Link{Name: "c", Hash: k.ToMultihash()},
72+
&Link{Name: "b", Hash: k.ToMultihash()},
73+
},
74+
}
75+
76+
_, err = ds.Add(nd)
77+
if err != nil {
78+
t.Fatal(err)
79+
}
80+
81+
lnk, err := nd.GetNodeLink("b")
82+
if err != nil {
83+
t.Fatal(err)
84+
}
85+
86+
if lnk.Name != "b" {
87+
t.Fatal("got wrong link back")
88+
}
89+
90+
_, err = nd.GetNodeLink("f")
91+
if err != ErrLinkNotFound {
92+
t.Fatal("shouldnt have found link")
93+
}
94+
95+
_, err = nd.GetLinkedNode(context.Background(), ds, "b")
96+
if err != nil {
97+
t.Fatal(err)
98+
}
99+
100+
outnd, err := nd.UpdateNodeLink("b", nd)
101+
if err != nil {
102+
t.Fatal(err)
103+
}
104+
105+
olnk, err := outnd.GetNodeLink("b")
106+
if err != nil {
107+
t.Fatal(err)
108+
}
109+
110+
if olnk.Hash.B58String() == k.B58String() {
111+
t.Fatal("new link should have different hash")
112+
}
113+
}
114+
115+
func TestNodeCopy(t *testing.T) {
116+
nd := &Node{
117+
Links: []*Link{
118+
&Link{Name: "a"},
119+
&Link{Name: "c"},
120+
&Link{Name: "b"},
121+
},
122+
}
123+
nd.SetData([]byte("testing"))
124+
125+
ond := nd.Copy()
126+
ond.SetData(nil)
127+
128+
if nd.Data() == nil {
129+
t.Fatal("should be different objects")
130+
}
131+
}

0 commit comments

Comments
 (0)