coreapi: fix resolved path root for some paths
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Apr 24, 2018 at 16:08 UTC
0f6bd2d17354489c3287b3d1101994743cfbd906
2 files changed
+41
-3
core/coreapi/path.go
+3
-3
@@ -108,9 +108,9 @@ func resolvePath(ctx context.Context, ng ipld.NodeGetter, nsys namesys.NameSyste
108
return nil, err
109
}
110
111
- var root *cid.Cid
112
- if ipath.IsJustAKey() {
113
- root = node.Cid()
111
+ root, err := cid.Parse(ipath.Segments()[1])
112
+ if err != nil {
113
+ return nil, err
114
}
115
116
return &resolvedPath{
core/coreapi/path_test.go
+38
@@ -4,6 +4,8 @@ import (
4
"context"
5
"strings"
6
"testing"
7
+
8
+ "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
9
)
10
11
func TestMutablePath(t *testing.T) {
@@ -113,3 +115,39 @@ func TestInvalidPathRemainder(t *testing.T) {
115
t.Fatalf("unexpected error: %s", err)
116
}
117
}
118
+
119
+func TestPathRoot(t *testing.T) {
120
+ ctx := context.Background()
121
+ _, api, err := makeAPI(ctx)
122
+ if err != nil {
123
+ t.Fatal(err)
124
+ }
125
+
126
+ blk, err := api.Block().Put(ctx, strings.NewReader(`foo`), options.Block.Format("raw"))
127
+ if err != nil {
128
+ t.Error(err)
129
+ }
130
+
131
+ obj, err := api.Dag().Put(ctx, strings.NewReader(`{"foo": {"/": "`+blk.Cid().String()+`"}}`))
132
+ if err != nil {
133
+ t.Fatal(err)
134
+ }
135
+
136
+ p1, err := api.ParsePath(obj.String() + "/foo")
137
+ if err != nil {
138
+ t.Error(err)
139
+ }
140
+
141
+ rp, err := api.ResolvePath(ctx, p1)
142
+ if err != nil {
143
+ t.Fatal(err)
144
+ }
145
+
146
+ if rp.Root().String() != obj.Cid().String() {
147
+ t.Error("unexpected path root")
148
+ }
149
+
150
+ if rp.Cid().String() != blk.Cid().String() {
151
+ t.Error("unexpected path cid")
152
+ }
153
+}