-
Notifications
You must be signed in to change notification settings - Fork 2
/
crabfs_test.go
118 lines (87 loc) · 2.42 KB
/
crabfs_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
package crabfs
import (
"bytes"
"context"
"io/ioutil"
"testing"
"time"
"github.com/runletapp/crabfs/options"
"github.com/stretchr/testify/assert"
)
func TestBasic(t *testing.T) {
assert := assert.New(t)
fs, err := New()
assert.Nil(err)
assert.NotEmpty(fs.GetID())
assert.True(len(fs.GetAddrs()) > 0)
assert.NotNil(fs.Blockstore())
assert.NotNil(fs.Host())
assert.NotNil(fs.GarbageCollector())
assert.NotNil(fs.GetIdentity())
assert.Nil(fs.Close())
}
func TestBucket(t *testing.T) {
assert := assert.New(t)
fs, err := New()
assert.Nil(err)
defer fs.Close()
privKey, err := GenerateKeyPair()
assert.Nil(err)
bucket, err := fs.WithBucket(privKey, "bkt")
assert.Nil(err)
assert.NotNil(bucket)
}
func TestBucketChroot(t *testing.T) {
assert := assert.New(t)
fs, err := New()
assert.Nil(err)
defer fs.Close()
privKey, err := GenerateKeyPair()
assert.Nil(err)
bucket, err := fs.WithBucketRoot(privKey, "bkt", "/data")
assert.Nil(err)
assert.NotNil(bucket)
}
func TestPut(t *testing.T) {
assertParent := assert.New(t)
privKey, err := GenerateKeyPair()
assertParent.Nil(err)
relay, err := RelayNew(context.Background(), 0, []string{}, nil)
assertParent.Nil(err)
t.Run("NoPeers", func(t *testing.T) {
assert := assert.New(t)
fs, err := New()
assert.Nil(err)
defer fs.Close()
reader := bytes.NewReader([]byte("data"))
assert.Nil(fs.Put(context.Background(), privKey, "nopeers", "test.txt", reader, time.Now()))
fetcher, err := fs.Get(context.Background(), privKey, "nopeers", "test.txt")
assert.Nil(err)
defer fetcher.Close()
data, err := ioutil.ReadAll(fetcher)
assert.Nil(err)
assert.Equal([]byte("data"), data)
})
t.Run("relay p2p", func(t *testing.T) {
assert := assert.New(t)
fs1, err := New(options.BootstrapPeers(relay.GetAddrs()))
assert.Nil(err)
defer fs1.Close()
assert.Nil(fs1.PublishPublicKey(privKey.GetPublic()))
fs2, err := New(options.BootstrapPeers(relay.GetAddrs()))
assert.Nil(err)
defer fs2.Close()
assert.Nil(fs2.PublishPublicKey(privKey.GetPublic()))
reader := bytes.NewReader([]byte("data"))
assert.Nil(fs1.Put(context.Background(), privKey, "relay p2p", "test.txt", reader, time.Now()))
fetcher, err := fs2.Get(context.Background(), privKey, "relay p2p", "test.txt")
assert.Nil(err)
if err != nil {
t.FailNow()
}
defer fetcher.Close()
data, err := ioutil.ReadAll(fetcher)
assert.Nil(err)
assert.Equal([]byte("data"), data)
})
}