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 ;).
W. Trevor King committed
May 8, 2015 at 21:43 UTC
19823c6704c89f305613316e873460f9347ed19a
1 file changed
+83
path/resolver_test.go
new
+83
@@ -0,0 +1,83 @@
1
+package path_test
2
+
3
+import (
4
+ "fmt"
5
+ "testing"
6
+
7
+ datastore "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
8
+ sync "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
9
+ context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
10
+
11
+ blockstore "github.com/ipfs/go-ipfs/blocks/blockstore"
12
+ blockservice "github.com/ipfs/go-ipfs/blockservice"
13
+ offline "github.com/ipfs/go-ipfs/exchange/offline"
14
+ merkledag "github.com/ipfs/go-ipfs/merkledag"
15
+ path "github.com/ipfs/go-ipfs/path"
16
+ util "github.com/ipfs/go-ipfs/util"
17
+)
18
+
19
+func randNode() (*merkledag.Node, util.Key) {
20
+ node := new(merkledag.Node)
21
+ node.Data = make([]byte, 32)
22
+ util.NewTimeSeededRand().Read(node.Data)
23
+ k, _ := node.Key()
24
+ return node, k
25
+}
26
+
27
+func TestRecurivePathResolution(t *testing.T) {
28
+ ctx := context.Background()
29
+ dstore := sync.MutexWrap(datastore.NewMapDatastore())
30
+ bstore := blockstore.NewBlockstore(dstore)
31
+ bserv, err := blockservice.New(bstore, offline.Exchange(bstore))
32
+ if err != nil {
33
+ t.Fatal(err)
34
+ }
35
+
36
+ dagService := merkledag.NewDAGService(bserv)
37
+
38
+ a, _ := randNode()
39
+ b, _ := randNode()
40
+ c, cKey := randNode()
41
+
42
+ err = b.AddNodeLink("grandchild", c)
43
+ if err != nil {
44
+ t.Fatal(err)
45
+ }
46
+
47
+ err = a.AddNodeLink("child", b)
48
+ if err != nil {
49
+ t.Fatal(err)
50
+ }
51
+
52
+ err = dagService.AddRecursive(a)
53
+ if err != nil {
54
+ t.Fatal(err)
55
+ }
56
+
57
+ aKey, err := a.Key()
58
+ if err != nil {
59
+ t.Fatal(err)
60
+ }
61
+
62
+ segments := []string{"", "ipfs", aKey.String(), "child", "grandchild"}
63
+ p, err := path.FromSegments(segments...)
64
+ if err != nil {
65
+ t.Fatal(err)
66
+ }
67
+
68
+ resolver := &path.Resolver{DAG: dagService}
69
+ node, err := resolver.ResolvePath(ctx, p)
70
+ if err != nil {
71
+ t.Fatal(err)
72
+ }
73
+
74
+ key, err := node.Key()
75
+ if err != nil {
76
+ t.Fatal(err)
77
+ }
78
+ if key.String() != cKey.String() {
79
+ t.Fatal(fmt.Errorf(
80
+ "recursive path resolution failed for %s: %s != %s",
81
+ p.String(), key.String(), cKey.String()))
82
+ }
83
+}