|
| 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 | +} |
0 commit comments