This repository has been archived by the owner on Sep 1, 2022. It is now read-only.
forked from ipfs/go-filestore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filestore_test.go
180 lines (150 loc) · 3.41 KB
/
filestore_test.go
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
package filestore
import (
"bytes"
"context"
"math/rand"
"os"
"testing"
dag "github.com/ipfs/go-merkledag"
cid "github.com/ipfs/go-cid"
ds "github.com/ipfs/go-datastore"
blockstore "github.com/ipfs/go-ipfs-blockstore"
posinfo "github.com/ipfs/go-ipfs-posinfo"
ipld "github.com/ipfs/go-ipld-format"
)
var bg = context.Background()
func newTestFilestore(t *testing.T) (string, *Filestore) {
mds := ds.NewMapDatastore()
testdir, err := os.MkdirTemp("", "filestore-test")
if err != nil {
t.Fatal(err)
}
fm := NewFileManager(mds, testdir)
fm.AllowFiles = true
bs := blockstore.NewBlockstore(mds)
fstore := NewFilestore(bs, fm)
return testdir, fstore
}
func makeFile(dir string, data []byte) (string, error) {
f, err := os.CreateTemp(dir, "file")
if err != nil {
return "", err
}
_, err = f.Write(data)
if err != nil {
return "", err
}
return f.Name(), nil
}
func TestBasicFilestore(t *testing.T) {
dir, fs := newTestFilestore(t)
buf := make([]byte, 1000)
rand.Read(buf)
fname, err := makeFile(dir, buf)
if err != nil {
t.Fatal(err)
}
var cids []cid.Cid
for i := 0; i < 100; i++ {
n := &posinfo.FilestoreNode{
PosInfo: &posinfo.PosInfo{
FullPath: fname,
Offset: uint64(i * 10),
},
Node: dag.NewRawNode(buf[i*10 : (i+1)*10]),
}
err := fs.Put(bg, n)
if err != nil {
t.Fatal(err)
}
cids = append(cids, n.Node.Cid())
}
for i, c := range cids {
blk, err := fs.Get(bg, c)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(blk.RawData(), buf[i*10:(i+1)*10]) {
t.Fatal("data didnt match on the way out")
}
}
kch, err := fs.AllKeysChan(context.Background())
if err != nil {
t.Fatal(err)
}
out := make(map[string]struct{})
for c := range kch {
out[c.KeyString()] = struct{}{}
}
if len(out) != len(cids) {
t.Fatal("mismatch in number of entries")
}
for _, c := range cids {
if _, ok := out[c.KeyString()]; !ok {
t.Fatal("missing cid: ", c)
}
}
}
func randomFileAdd(t *testing.T, fs *Filestore, dir string, size int) (string, []cid.Cid) {
buf := make([]byte, size)
rand.Read(buf)
fname, err := makeFile(dir, buf)
if err != nil {
t.Fatal(err)
}
var out []cid.Cid
for i := 0; i < size/10; i++ {
n := &posinfo.FilestoreNode{
PosInfo: &posinfo.PosInfo{
FullPath: fname,
Offset: uint64(i * 10),
},
Node: dag.NewRawNode(buf[i*10 : (i+1)*10]),
}
err := fs.Put(bg, n)
if err != nil {
t.Fatal(err)
}
out = append(out, n.Cid())
}
return fname, out
}
func TestDeletes(t *testing.T) {
dir, fs := newTestFilestore(t)
_, cids := randomFileAdd(t, fs, dir, 100)
todelete := cids[:4]
for _, c := range todelete {
err := fs.DeleteBlock(bg, c)
if err != nil {
t.Fatal(err)
}
}
deleted := make(map[string]bool)
for _, c := range todelete {
_, err := fs.Get(bg, c)
if !ipld.IsNotFound(err) {
t.Fatal("expected blockstore not found error")
}
deleted[c.KeyString()] = true
}
keys, err := fs.AllKeysChan(context.Background())
if err != nil {
t.Fatal(err)
}
for c := range keys {
if deleted[c.KeyString()] {
t.Fatal("shouldnt have reference to this key anymore")
}
}
}
func TestIsURL(t *testing.T) {
if !IsURL("http://www.example.com") {
t.Fatal("IsURL failed: http://www.example.com")
}
if !IsURL("https://www.example.com") {
t.Fatal("IsURL failed: https://www.example.com")
}
if IsURL("adir/afile") || IsURL("http:/ /afile") || IsURL("http:/a/file") {
t.Fatal("IsURL recognized non-url")
}
}