mirror of
https://github.com/ipfs/kubo.git
synced 2025-08-06 11:31:54 +08:00

Also change existing 'Node' type to 'ProtoNode' and use that most everywhere for now. As we move forward with the integration we will try and use the Node interface in more places that we're currently using ProtoNode. License: MIT Signed-off-by: Jeromy <why@ipfs.io>
130 lines
2.1 KiB
Go
130 lines
2.1 KiB
Go
package merkledag_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
. "github.com/ipfs/go-ipfs/merkledag"
|
|
mdtest "github.com/ipfs/go-ipfs/merkledag/test"
|
|
|
|
"context"
|
|
)
|
|
|
|
func TestRemoveLink(t *testing.T) {
|
|
nd := &ProtoNode{}
|
|
nd.SetLinks([]*Link{
|
|
&Link{Name: "a"},
|
|
&Link{Name: "b"},
|
|
&Link{Name: "a"},
|
|
&Link{Name: "a"},
|
|
&Link{Name: "c"},
|
|
&Link{Name: "a"},
|
|
})
|
|
|
|
err := nd.RemoveNodeLink("a")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if len(nd.Links()) != 2 {
|
|
t.Fatal("number of links incorrect")
|
|
}
|
|
|
|
if nd.Links()[0].Name != "b" {
|
|
t.Fatal("link order wrong")
|
|
}
|
|
|
|
if nd.Links()[1].Name != "c" {
|
|
t.Fatal("link order wrong")
|
|
}
|
|
|
|
// should fail
|
|
err = nd.RemoveNodeLink("a")
|
|
if err != ErrNotFound {
|
|
t.Fatal("should have failed to remove link")
|
|
}
|
|
|
|
// ensure nothing else got touched
|
|
if len(nd.Links()) != 2 {
|
|
t.Fatal("number of links incorrect")
|
|
}
|
|
|
|
if nd.Links()[0].Name != "b" {
|
|
t.Fatal("link order wrong")
|
|
}
|
|
|
|
if nd.Links()[1].Name != "c" {
|
|
t.Fatal("link order wrong")
|
|
}
|
|
}
|
|
|
|
func TestFindLink(t *testing.T) {
|
|
ds := mdtest.Mock()
|
|
k, err := ds.Add(new(ProtoNode))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
nd := &ProtoNode{}
|
|
nd.SetLinks([]*Link{
|
|
&Link{Name: "a", Cid: k},
|
|
&Link{Name: "c", Cid: k},
|
|
&Link{Name: "b", Cid: k},
|
|
})
|
|
|
|
_, err = ds.Add(nd)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
lnk, err := nd.GetNodeLink("b")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if lnk.Name != "b" {
|
|
t.Fatal("got wrong link back")
|
|
}
|
|
|
|
_, err = nd.GetNodeLink("f")
|
|
if err != ErrLinkNotFound {
|
|
t.Fatal("shouldnt have found link")
|
|
}
|
|
|
|
_, err = nd.GetLinkedNode(context.Background(), ds, "b")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
outnd, err := nd.UpdateNodeLink("b", nd)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
olnk, err := outnd.GetNodeLink("b")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if olnk.Cid.String() == k.String() {
|
|
t.Fatal("new link should have different hash")
|
|
}
|
|
}
|
|
|
|
func TestNodeCopy(t *testing.T) {
|
|
nd := &ProtoNode{}
|
|
nd.SetLinks([]*Link{
|
|
&Link{Name: "a"},
|
|
&Link{Name: "c"},
|
|
&Link{Name: "b"},
|
|
})
|
|
|
|
nd.SetData([]byte("testing"))
|
|
|
|
ond := nd.Copy()
|
|
ond.SetData(nil)
|
|
|
|
if nd.Data() == nil {
|
|
t.Fatal("should be different objects")
|
|
}
|
|
}
|