-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathutilities_test.go
More file actions
261 lines (218 loc) · 7.17 KB
/
utilities_test.go
File metadata and controls
261 lines (218 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package gateway
import (
"context"
"errors"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"time"
"github.com/ipfs/boxo/blockservice"
offline "github.com/ipfs/boxo/exchange/offline"
"github.com/ipfs/boxo/files"
"github.com/ipfs/boxo/namesys"
"github.com/ipfs/boxo/path"
"github.com/ipfs/go-cid"
carblockstore "github.com/ipld/go-car/v2/blockstore"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/routing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func mustNewRequest(t *testing.T, method string, path string, body io.Reader) *http.Request {
r, err := http.NewRequest(method, path, body)
require.NoError(t, err)
return r
}
func mustDoWithoutRedirect(t *testing.T, req *http.Request) *http.Response {
errNoRedirect := errors.New("without-redirect")
c := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return errNoRedirect
},
}
res, err := c.Do(req)
require.True(t, err == nil || errors.Is(err, errNoRedirect))
return res
}
func mustDo(t *testing.T, req *http.Request) *http.Response {
c := &http.Client{}
res, err := c.Do(req)
require.NoError(t, err)
return res
}
type mockNamesysItem struct {
path path.Path
ttl time.Duration
}
func newMockNamesysItem(p path.Path, ttl time.Duration) *mockNamesysItem {
return &mockNamesysItem{path: p, ttl: ttl}
}
type mockNamesys map[string]*mockNamesysItem
func (m mockNamesys) Resolve(ctx context.Context, p path.Path, opts ...namesys.ResolveOption) (result namesys.Result, err error) {
cfg := namesys.DefaultResolveOptions()
for _, o := range opts {
o(&cfg)
}
depth := cfg.Depth
if depth == namesys.UnlimitedDepth {
// max uint
depth = ^uint(0)
}
var (
value path.Path
ttl time.Duration
)
name := path.SegmentsToString(p.Segments()[:2]...)
for strings.HasPrefix(name, "/ipns/") {
if depth == 0 {
return namesys.Result{Path: value, TTL: ttl}, namesys.ErrResolveRecursion
}
depth--
v, ok := m[name]
if !ok {
return namesys.Result{}, namesys.ErrResolveFailed
}
value = v.path
ttl = v.ttl
name = value.String()
}
value, err = path.Join(value, p.Segments()[2:]...)
return namesys.Result{Path: value, TTL: ttl}, err
}
func (m mockNamesys) ResolveAsync(ctx context.Context, p path.Path, opts ...namesys.ResolveOption) <-chan namesys.AsyncResult {
out := make(chan namesys.AsyncResult, 1)
res, err := m.Resolve(ctx, p, opts...)
out <- namesys.AsyncResult{Path: res.Path, TTL: res.TTL, LastMod: res.LastMod, Err: err}
close(out)
return out
}
func (m mockNamesys) Publish(ctx context.Context, name crypto.PrivKey, value path.Path, opts ...namesys.PublishOption) error {
return errors.New("not implemented for mockNamesys")
}
func (m mockNamesys) GetResolver(subs string) (namesys.Resolver, bool) {
return nil, false
}
type mockBackend struct {
gw IPFSBackend
namesys mockNamesys
}
var _ IPFSBackend = (*mockBackend)(nil)
func newMockBackend(t *testing.T, fixturesFile string) (*mockBackend, cid.Cid) {
r, err := os.Open(filepath.Join("./testdata", fixturesFile))
assert.NoError(t, err)
blockStore, err := carblockstore.NewReadOnly(r, nil)
assert.NoError(t, err)
t.Cleanup(func() {
blockStore.Close()
r.Close()
})
cids, err := blockStore.Roots()
assert.NoError(t, err)
assert.Len(t, cids, 1)
blockService := blockservice.New(blockStore, offline.Exchange(blockStore))
n := mockNamesys{}
backend, err := NewBlocksBackend(blockService, WithNameSystem(n))
if err != nil {
t.Fatal(err)
}
return &mockBackend{
gw: backend,
namesys: n,
}, cids[0]
}
func (mb *mockBackend) Get(ctx context.Context, immutablePath path.ImmutablePath, ranges ...ByteRange) (ContentPathMetadata, *GetResponse, error) {
return mb.gw.Get(ctx, immutablePath, ranges...)
}
func (mb *mockBackend) GetAll(ctx context.Context, immutablePath path.ImmutablePath) (ContentPathMetadata, files.Node, error) {
return mb.gw.GetAll(ctx, immutablePath)
}
func (mb *mockBackend) GetBlock(ctx context.Context, immutablePath path.ImmutablePath) (ContentPathMetadata, files.File, error) {
return mb.gw.GetBlock(ctx, immutablePath)
}
func (mb *mockBackend) Head(ctx context.Context, immutablePath path.ImmutablePath) (ContentPathMetadata, *HeadResponse, error) {
return mb.gw.Head(ctx, immutablePath)
}
func (mb *mockBackend) GetCAR(ctx context.Context, immutablePath path.ImmutablePath, params CarParams) (ContentPathMetadata, io.ReadCloser, error) {
return mb.gw.GetCAR(ctx, immutablePath, params)
}
func (mb *mockBackend) ResolveMutable(ctx context.Context, p path.Path) (path.ImmutablePath, time.Duration, time.Time, error) {
return mb.gw.ResolveMutable(ctx, p)
}
func (mb *mockBackend) GetIPNSRecord(ctx context.Context, c cid.Cid) ([]byte, error) {
return nil, routing.ErrNotSupported
}
func (mb *mockBackend) GetDNSLinkRecord(ctx context.Context, hostname string) (path.Path, error) {
if mb.namesys != nil {
p, err := path.NewPath("/ipns/" + hostname)
if err != nil {
return nil, err
}
res, err := mb.namesys.Resolve(ctx, p, namesys.ResolveWithDepth(1))
if err == namesys.ErrResolveRecursion {
err = nil
}
p = res.Path
return p, err
}
return nil, errors.New("not implemented")
}
func (mb *mockBackend) IsCached(ctx context.Context, p path.Path) bool {
return mb.gw.IsCached(ctx, p)
}
func (mb *mockBackend) ResolvePath(ctx context.Context, immutablePath path.ImmutablePath) (ContentPathMetadata, error) {
return mb.gw.ResolvePath(ctx, immutablePath)
}
func (mb *mockBackend) resolvePathNoRootsReturned(ctx context.Context, ip path.Path) (path.ImmutablePath, error) {
var imPath path.ImmutablePath
var err error
if ip.Mutable() {
imPath, _, _, err = mb.ResolveMutable(ctx, ip)
if err != nil {
return path.ImmutablePath{}, err
}
} else {
imPath, err = path.NewImmutablePath(ip)
if err != nil {
return path.ImmutablePath{}, err
}
}
md, err := mb.ResolvePath(ctx, imPath)
if err != nil {
return path.ImmutablePath{}, err
}
return md.LastSegment, nil
}
func newTestServerAndNode(t *testing.T, fixturesFile string) (*httptest.Server, *mockBackend, cid.Cid) {
backend, root := newMockBackend(t, fixturesFile)
ts := newTestServer(t, backend)
return ts, backend, root
}
func newTestServer(t *testing.T, backend IPFSBackend) *httptest.Server {
return newTestServerWithConfig(t, backend, Config{
DeserializedResponses: true,
})
}
func newTestServerWithConfig(t *testing.T, backend IPFSBackend, config Config) *httptest.Server {
return newTestServerWithConfigAndHeaders(t, backend, config, map[string][]string{})
}
func newTestServerWithConfigAndHeaders(t *testing.T, backend IPFSBackend, config Config, headers map[string][]string) *httptest.Server {
handler := NewHandler(config, backend)
mux := http.NewServeMux()
mux.Handle("/ipfs/", handler)
mux.Handle("/ipns/", handler)
handler = NewHostnameHandler(config, backend, mux)
handler = NewHeaders(headers).ApplyCors().Wrap(handler)
ts := httptest.NewServer(handler)
t.Cleanup(func() { ts.Close() })
t.Logf("test server url: %s", ts.URL)
return ts
}
func matchPathOrBreadcrumbs(s string, expected string) bool {
matched, _ := regexp.MatchString("Index of(\n|\r\n)[\t ]*"+regexp.QuoteMeta(expected), s)
return matched
}