mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-29 01:12:24 +08:00
path/resolver_test: Test recursive Link resolution
Setup a three-level graph: a -(child)-> b -(grandchild)-> c and then try and resolve: /ipfs/<hash-of-a>/child/grandchild Before 10669e8b (path/resolver: Fix recursive path resolution, 2015-05-08) this failed with: resolver_test.go:71: no link named "grandchild" under QmSomeRandomHash The boilerplate for this test is from pin/pin_test.go, and I make no claims that it's the best way to setup the test graph ;).
This commit is contained in:
83
path/resolver_test.go
Normal file
83
path/resolver_test.go
Normal file
@ -0,0 +1,83 @@
|
||||
package path_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
datastore "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
|
||||
sync "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
|
||||
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
|
||||
|
||||
blockstore "github.com/ipfs/go-ipfs/blocks/blockstore"
|
||||
blockservice "github.com/ipfs/go-ipfs/blockservice"
|
||||
offline "github.com/ipfs/go-ipfs/exchange/offline"
|
||||
merkledag "github.com/ipfs/go-ipfs/merkledag"
|
||||
path "github.com/ipfs/go-ipfs/path"
|
||||
util "github.com/ipfs/go-ipfs/util"
|
||||
)
|
||||
|
||||
func randNode() (*merkledag.Node, util.Key) {
|
||||
node := new(merkledag.Node)
|
||||
node.Data = make([]byte, 32)
|
||||
util.NewTimeSeededRand().Read(node.Data)
|
||||
k, _ := node.Key()
|
||||
return node, k
|
||||
}
|
||||
|
||||
func TestRecurivePathResolution(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
dstore := sync.MutexWrap(datastore.NewMapDatastore())
|
||||
bstore := blockstore.NewBlockstore(dstore)
|
||||
bserv, err := blockservice.New(bstore, offline.Exchange(bstore))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
dagService := merkledag.NewDAGService(bserv)
|
||||
|
||||
a, _ := randNode()
|
||||
b, _ := randNode()
|
||||
c, cKey := randNode()
|
||||
|
||||
err = b.AddNodeLink("grandchild", c)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = a.AddNodeLink("child", b)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = dagService.AddRecursive(a)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
aKey, err := a.Key()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
segments := []string{"", "ipfs", aKey.String(), "child", "grandchild"}
|
||||
p, err := path.FromSegments(segments...)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
resolver := &path.Resolver{DAG: dagService}
|
||||
node, err := resolver.ResolvePath(ctx, p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
key, err := node.Key()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if key.String() != cKey.String() {
|
||||
t.Fatal(fmt.Errorf(
|
||||
"recursive path resolution failed for %s: %s != %s",
|
||||
p.String(), key.String(), cKey.String()))
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user