package resolver_test import ( "context" "fmt" "math/rand" "testing" "time" path "github.com/ipfs/go-path" "github.com/ipfs/go-path/resolver" ipld "github.com/ipfs/go-ipld-format" merkledag "github.com/ipfs/go-merkledag" dagmock "github.com/ipfs/go-merkledag/test" ) func randNode() *merkledag.ProtoNode { node := new(merkledag.ProtoNode) node.SetData(make([]byte, 32)) r := rand.New(rand.NewSource(time.Now().UnixNano())) r.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())) } }