/
providerutils_test.go
180 lines (172 loc) · 5.4 KB
/
providerutils_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 providerutils_test
import (
"bytes"
"context"
"errors"
"math/rand"
"testing"
"github.com/ipfs/go-cid"
"github.com/stretchr/testify/require"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/specs-actors/v8/actors/builtin/market"
"github.com/filecoin-project/go-fil-markets/filestore"
"github.com/filecoin-project/go-fil-markets/shared"
"github.com/filecoin-project/go-fil-markets/shared_testutil"
"github.com/filecoin-project/go-fil-markets/storagemarket/impl/blockrecorder"
"github.com/filecoin-project/go-fil-markets/storagemarket/impl/providerutils"
"github.com/filecoin-project/go-fil-markets/storagemarket/network"
)
func TestVerifyProposal(t *testing.T) {
tests := map[string]struct {
proposal market.ClientDealProposal
verifier providerutils.VerifyFunc
shouldErr bool
}{
"successful verification": {
proposal: *shared_testutil.MakeTestClientDealProposal(),
verifier: func(context.Context, crypto.Signature, address.Address, []byte, shared.TipSetToken) (bool, error) {
return true, nil
},
shouldErr: false,
},
"bad proposal": {
proposal: market.ClientDealProposal{
Proposal: market.DealProposal{},
ClientSignature: *shared_testutil.MakeTestSignature(),
},
verifier: func(context.Context, crypto.Signature, address.Address, []byte, shared.TipSetToken) (bool, error) {
return true, nil
},
shouldErr: true,
},
"verification fails": {
proposal: *shared_testutil.MakeTestClientDealProposal(),
verifier: func(context.Context, crypto.Signature, address.Address, []byte, shared.TipSetToken) (bool, error) {
return false, nil
},
shouldErr: true,
},
}
for name, data := range tests {
t.Run(name, func(t *testing.T) {
err := providerutils.VerifyProposal(context.Background(), data.proposal, shared.TipSetToken{}, data.verifier)
require.Equal(t, err != nil, data.shouldErr)
})
}
}
func TestSignMinerData(t *testing.T) {
ctx := context.Background()
successLookup := func(context.Context, address.Address, shared.TipSetToken) (address.Address, error) {
return address.TestAddress2, nil
}
successSign := func(context.Context, address.Address, []byte) (*crypto.Signature, error) {
return shared_testutil.MakeTestSignature(), nil
}
tests := map[string]struct {
data interface{}
workerLookup providerutils.WorkerLookupFunc
signBytes providerutils.SignFunc
shouldErr bool
}{
"succeeds": {
data: shared_testutil.MakeTestStorageAsk(),
workerLookup: successLookup,
signBytes: successSign,
shouldErr: false,
},
"cbor dump errors": {
data: &network.Response{},
workerLookup: successLookup,
signBytes: successSign,
shouldErr: true,
},
"worker lookup errors": {
data: shared_testutil.MakeTestStorageAsk(),
workerLookup: func(context.Context, address.Address, shared.TipSetToken) (address.Address, error) {
return address.Undef, errors.New("Something went wrong")
},
signBytes: successSign,
shouldErr: true,
},
"signing errors": {
data: shared_testutil.MakeTestStorageAsk(),
workerLookup: successLookup,
signBytes: func(context.Context, address.Address, []byte) (*crypto.Signature, error) {
return nil, errors.New("something went wrong")
},
shouldErr: true,
},
}
for name, data := range tests {
t.Run(name, func(t *testing.T) {
_, err := providerutils.SignMinerData(ctx, data.data, address.TestAddress, shared.TipSetToken{}, data.workerLookup, data.signBytes)
require.Equal(t, err != nil, data.shouldErr)
})
}
}
func TestLoadBlockLocations(t *testing.T) {
testData := shared_testutil.NewTestIPLDTree()
carBuf := new(bytes.Buffer)
blockLocationBuf := new(bytes.Buffer)
err := testData.DumpToCar(carBuf, blockrecorder.RecordEachBlockTo(blockLocationBuf))
require.NoError(t, err)
validPath := filestore.Path("valid.data")
validFile := shared_testutil.NewTestFile(shared_testutil.TestFileParams{
Buffer: blockLocationBuf,
Path: validPath,
})
missingPath := filestore.Path("missing.data")
invalidPath := filestore.Path("invalid.data")
invalidData := make([]byte, 512)
_, _ = rand.Read(invalidData)
invalidFile := shared_testutil.NewTestFile(shared_testutil.TestFileParams{
Buffer: bytes.NewBuffer(invalidData),
Path: invalidPath,
})
fs := shared_testutil.NewTestFileStore(shared_testutil.TestFileStoreParams{
Files: []filestore.File{validFile, invalidFile},
ExpectedOpens: []filestore.Path{validPath, invalidPath},
})
testCases := map[string]struct {
path filestore.Path
shouldErr bool
expectedCids []cid.Cid
}{
"valid data": {
path: validPath,
shouldErr: false,
expectedCids: []cid.Cid{
testData.LeafAlphaBlock.Cid(),
testData.LeafBetaBlock.Cid(),
testData.MiddleListBlock.Cid(),
testData.MiddleMapBlock.Cid(),
testData.RootBlock.Cid(),
},
},
"missing data": {
path: missingPath,
shouldErr: true,
},
"invalid data": {
path: invalidPath,
shouldErr: true,
},
}
for testCase, data := range testCases {
t.Run(testCase, func(t *testing.T) {
results, err := providerutils.LoadBlockLocations(fs, data.path)
if data.shouldErr {
require.Error(t, err)
require.Nil(t, results)
} else {
require.NoError(t, err)
for _, c := range data.expectedCids {
_, ok := results[c]
require.True(t, ok)
}
}
})
}
fs.VerifyExpectations(t)
}