mirror of
https://github.com/ipfs/kubo.git
synced 2025-09-10 14:34:24 +08:00

Currently the "path" module does two very different things: * Defines how ipfs paths look like and provides tools to parse/split etc. * Provides a resolver to resolve paths. This moves the resolver stuff to `path/resolver` and leaves the path utilities in `path`. The result is that now the IPFS `path` package just defines what a path looks like and becomes a module that can be exported/re-used without problems. Currently there are circular dependency cycles (resolve_test -> merkledag/utils, merkledag->path), which the prevent the export of merkledag itself. License: MIT Signed-off-by: Hector Sanjuan <hector@protocol.ai>
71 lines
1.4 KiB
Go
71 lines
1.4 KiB
Go
package resolver_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
|
|
merkledag "github.com/ipfs/go-ipfs/merkledag"
|
|
dagmock "github.com/ipfs/go-ipfs/merkledag/test"
|
|
path "github.com/ipfs/go-ipfs/path"
|
|
"github.com/ipfs/go-ipfs/path/resolver"
|
|
|
|
util "gx/ipfs/QmNiJuT8Ja3hMVpBHXv3Q6dwmperaQ6JjLtpMQgMCD7xvx/go-ipfs-util"
|
|
ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
|
|
)
|
|
|
|
func randNode() *merkledag.ProtoNode {
|
|
node := new(merkledag.ProtoNode)
|
|
node.SetData(make([]byte, 32))
|
|
util.NewTimeSeededRand().Read(node.Data())
|
|
return node
|
|
}
|
|
|
|
func TestRecurivePathResolution(t *testing.T) {
|
|
ctx := context.Background()
|
|
dagService := dagmock.Mock()
|
|
|
|
a := randNode()
|
|
b := randNode()
|
|
c := randNode()
|
|
|
|
err := b.AddNodeLink("grandchild", c)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
err = a.AddNodeLink("child", b)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
for _, n := range []ipld.Node{a, b, c} {
|
|
err = dagService.Add(ctx, n)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
aKey := a.Cid()
|
|
|
|
segments := []string{aKey.String(), "child", "grandchild"}
|
|
p, err := path.FromSegments("/ipfs/", segments...)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
resolver := resolver.NewBasicResolver(dagService)
|
|
node, err := resolver.ResolvePath(ctx, p)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
cKey := c.Cid()
|
|
key := node.Cid()
|
|
if key.String() != cKey.String() {
|
|
t.Fatal(fmt.Errorf(
|
|
"recursive path resolution failed for %s: %s != %s",
|
|
p.String(), key.String(), cKey.String()))
|
|
}
|
|
}
|