namesys: properly attach path in name.Resolve
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Oct 26, 2018 at 18:41 UTC
f4e5625b2fc8d418031c70bccc36637f01973565
2 files changed
+146
-30
core/coreapi/name_test.go
+136
-29
@@ -2,9 +2,11 @@ package coreapi_test
2
3
import (
4
"context"
5
+ "github.com/ipfs/go-ipfs/core"
6
"io"
7
"io/ioutil"
8
"math/rand"
9
+ "path"
10
"testing"
11
"time"
12
@@ -21,45 +23,150 @@ func addTestObject(ctx context.Context, api coreiface.CoreAPI) (coreiface.Path,
23
return api.Unixfs().Add(ctx, files.NewReaderFile("", "", ioutil.NopCloser(&io.LimitedReader{R: rnd, N: 4092}), nil))
24
}
25
24
-func TestBasicPublishResolve(t *testing.T) {
25
- ctx := context.Background()
26
- nds, apis, err := makeAPISwarm(ctx, true, 5)
26
+func appendPath(p coreiface.Path, sub string) coreiface.Path {
27
+ p, err := coreiface.ParsePath(path.Join(p.String(), sub))
28
if err != nil {
28
- t.Fatal(err)
29
- return
29
+ panic(err)
30
}
31
- n := nds[0]
32
- api := apis[0]
31
+ return p
32
+}
33
34
- p, err := addTestObject(ctx, api)
35
- if err != nil {
36
- t.Fatal(err)
37
- return
38
- }
34
+func TestPublishResolve(t *testing.T) {
35
+ ctx := context.Background()
36
+ init := func() (*core.IpfsNode, coreiface.CoreAPI, coreiface.Path) {
37
+ nds, apis, err := makeAPISwarm(ctx, true, 5)
38
+ if err != nil {
39
+ t.Fatal(err)
40
+ return nil, nil, nil
41
+ }
42
+ n := nds[0]
43
+ api := apis[0]
44
40
- e, err := api.Name().Publish(ctx, p)
41
- if err != nil {
42
- t.Fatal(err)
43
- return
45
+ p, err := addTestObject(ctx, api)
46
+ if err != nil {
47
+ t.Fatal(err)
48
+ return nil, nil, nil
49
+ }
50
+ return n, api, p
51
}
52
46
- if e.Name() != n.Identity.Pretty() {
47
- t.Errorf("expected e.Name to equal '%s', got '%s'", n.Identity.Pretty(), e.Name())
48
- }
53
+ run := func(t *testing.T, ropts []opt.NameResolveOption) {
54
+ t.Run("basic", func(t *testing.T) {
55
+ n, api, p := init()
56
+ e, err := api.Name().Publish(ctx, p)
57
+ if err != nil {
58
+ t.Fatal(err)
59
+ return
60
+ }
61
50
- if e.Value().String() != p.String() {
51
- t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String())
52
- }
62
+ if e.Name() != n.Identity.Pretty() {
63
+ t.Errorf("expected e.Name to equal '%s', got '%s'", n.Identity.Pretty(), e.Name())
64
+ }
65
54
- resPath, err := api.Name().Resolve(ctx, e.Name())
55
- if err != nil {
56
- t.Fatal(err)
57
- return
58
- }
66
+ if e.Value().String() != p.String() {
67
+ t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String())
68
+ }
69
60
- if resPath.String() != p.String() {
61
- t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String())
70
+ resPath, err := api.Name().Resolve(ctx, e.Name(), ropts...)
71
+ if err != nil {
72
+ t.Fatal(err)
73
+ return
74
+ }
75
+
76
+ if resPath.String() != p.String() {
77
+ t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String())
78
+ }
79
+ })
80
+
81
+ t.Run("publishPath", func(t *testing.T) {
82
+ n, api, p := init()
83
+ e, err := api.Name().Publish(ctx, appendPath(p, "/test"))
84
+ if err != nil {
85
+ t.Fatal(err)
86
+ return
87
+ }
88
+
89
+ if e.Name() != n.Identity.Pretty() {
90
+ t.Errorf("expected e.Name to equal '%s', got '%s'", n.Identity.Pretty(), e.Name())
91
+ }
92
+
93
+ if e.Value().String() != p.String()+"/test" {
94
+ t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String())
95
+ }
96
+
97
+ resPath, err := api.Name().Resolve(ctx, e.Name(), ropts...)
98
+ if err != nil {
99
+ t.Fatal(err)
100
+ return
101
+ }
102
+
103
+ if resPath.String() != p.String()+"/test" {
104
+ t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String()+"/test")
105
+ }
106
+ })
107
+
108
+ t.Run("revolvePath", func(t *testing.T) {
109
+ n, api, p := init()
110
+ e, err := api.Name().Publish(ctx, p)
111
+ if err != nil {
112
+ t.Fatal(err)
113
+ return
114
+ }
115
+
116
+ if e.Name() != n.Identity.Pretty() {
117
+ t.Errorf("expected e.Name to equal '%s', got '%s'", n.Identity.Pretty(), e.Name())
118
+ }
119
+
120
+ if e.Value().String() != p.String() {
121
+ t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String())
122
+ }
123
+
124
+ resPath, err := api.Name().Resolve(ctx, e.Name()+"/test", ropts...)
125
+ if err != nil {
126
+ t.Fatal(err)
127
+ return
128
+ }
129
+
130
+ if resPath.String() != p.String()+"/test" {
131
+ t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String()+"/test")
132
+ }
133
+ })
134
+
135
+ t.Run("publishRevolvePath", func(t *testing.T) {
136
+ n, api, p := init()
137
+ e, err := api.Name().Publish(ctx, appendPath(p, "/a"))
138
+ if err != nil {
139
+ t.Fatal(err)
140
+ return
141
+ }
142
+
143
+ if e.Name() != n.Identity.Pretty() {
144
+ t.Errorf("expected e.Name to equal '%s', got '%s'", n.Identity.Pretty(), e.Name())
145
+ }
146
+
147
+ if e.Value().String() != p.String()+"/a" {
148
+ t.Errorf("expected paths to match, '%s'!='%s'", e.Value().String(), p.String())
149
+ }
150
+
151
+ resPath, err := api.Name().Resolve(ctx, e.Name()+"/b", ropts...)
152
+ if err != nil {
153
+ t.Fatal(err)
154
+ return
155
+ }
156
+
157
+ if resPath.String() != p.String()+"/a/b" {
158
+ t.Errorf("expected paths to match, '%s'!='%s'", resPath.String(), p.String()+"/a/b")
159
+ }
160
+ })
161
}
162
+
163
+ t.Run("default", func(t *testing.T) {
164
+ run(t, []opt.NameResolveOption{})
165
+ })
166
+
167
+ t.Run("nocache", func(t *testing.T) {
168
+ run(t, []opt.NameResolveOption{opt.Name.Cache(false)})
169
+ })
170
}
171
172
func TestBasicPublishResolveKey(t *testing.T) {
namesys/namesys.go
+10
-1
@@ -100,6 +100,14 @@ func (ns *mpns) resolveOnceAsync(ctx context.Context, name string, options opts.
100
key := segments[2]
101
102
if p, ok := ns.cacheGet(key); ok {
103
+ if len(segments) > 3 {
104
+ var err error
105
+ p, err = path.FromSegments("", strings.TrimRight(p.String(), "/"), segments[3])
106
+ if err != nil {
107
+ emitOnceResult(ctx, out, onceResult{value: p, err: err})
108
+ }
109
+ }
110
+
111
out <- onceResult{value: p}
112
close(out)
113
return out
@@ -139,7 +147,8 @@ func (ns *mpns) resolveOnceAsync(ctx context.Context, name string, options opts.
147
148
// Attach rest of the path
149
if len(segments) > 3 {
142
- p, err := path.FromSegments("", strings.TrimRight(p.String(), "/"), segments[3])
150
+ var err error
151
+ p, err = path.FromSegments("", strings.TrimRight(p.String(), "/"), segments[3])
152
if err != nil {
153
emitOnceResult(ctx, out, onceResult{value: p, ttl: res.ttl, err: err})
154
}