88
99 "github.com/ipfs/go-cid"
1010 "github.com/ipld/go-ipld-prime"
11+ "github.com/storacha/etracker/internal/db/consumer"
1112 "github.com/storacha/go-libstoracha/capabilities/space/content"
1213 "github.com/storacha/go-libstoracha/testutil"
1314 "github.com/storacha/go-ucanto/core/dag/blockstore"
@@ -21,13 +22,38 @@ import (
2122 "github.com/storacha/go-ucanto/core/receipt/ran"
2223 "github.com/storacha/go-ucanto/core/result"
2324 "github.com/storacha/go-ucanto/core/result/failure"
25+ "github.com/storacha/go-ucanto/did"
2426 "github.com/storacha/go-ucanto/principal/ed25519/verifier"
2527 "github.com/storacha/go-ucanto/ucan"
2628 "github.com/storacha/go-ucanto/validator"
2729 "github.com/stretchr/testify/assert"
2830 "github.com/stretchr/testify/require"
2931)
3032
33+ var _ consumer.ConsumerTable = (* mockConsumerTable )(nil )
34+
35+ type mockConsumerTable struct {
36+ t * testing.T
37+ provider did.DID
38+ }
39+
40+ func (m * mockConsumerTable ) Get (ctx context.Context , space string ) (consumer.Consumer , error ) {
41+ s , err := did .Parse (space )
42+ if err != nil {
43+ return consumer.Consumer {}, err
44+ }
45+
46+ return consumer.Consumer {
47+ ID : s ,
48+ Provider : m .provider ,
49+ Subscription : testutil .RandomCID (m .t ).String (),
50+ }, nil
51+ }
52+
53+ func (m * mockConsumerTable ) ListByCustomer (ctx context.Context , customer did.DID ) ([]did.DID , error ) {
54+ return []did.DID {}, nil
55+ }
56+
3157func TestValidateRetrievalReceipt (t * testing.T ) {
3258 vCtx := validator .NewValidationContext (
3359 testutil .Service .Verifier (),
@@ -45,6 +71,11 @@ func TestValidateRetrievalReceipt(t *testing.T) {
4571 },
4672 )
4773
74+ knownProvider , err := did .Parse ("did:web:up.test.storacha.network" )
75+ require .NoError (t , err )
76+
77+ consumerTable := & mockConsumerTable {t : t , provider : knownProvider }
78+
4879 space := testutil .RandomSigner (t )
4980 randBytes := testutil .RandomBytes (t , 256 )
5081 blob := struct {
@@ -95,7 +126,7 @@ func TestValidateRetrievalReceipt(t *testing.T) {
95126 )
96127 require .NoError (t , err )
97128
98- cap , err := validateRetrievalReceipt (context .Background (), storageNode .DID (), rcpt , vCtx )
129+ cap , err := validateRetrievalReceipt (context .Background (), storageNode .DID (), rcpt , vCtx , consumerTable , [] string { knownProvider . String ()} )
99130 require .NoError (t , err )
100131 assert .Equal (t , content .RetrieveAbility , cap .Can ())
101132 })
@@ -108,7 +139,7 @@ func TestValidateRetrievalReceipt(t *testing.T) {
108139 )
109140 require .NoError (t , err )
110141
111- _ , err = validateRetrievalReceipt (context .Background (), storageNode .DID (), rcpt , vCtx )
142+ _ , err = validateRetrievalReceipt (context .Background (), storageNode .DID (), rcpt , vCtx , consumerTable , [] string { knownProvider . String ()} )
112143 assert .ErrorContains (t , err , "receipt is a failure receipt" )
113144 })
114145
@@ -121,7 +152,7 @@ func TestValidateRetrievalReceipt(t *testing.T) {
121152 )
122153 require .NoError (t , err )
123154
124- _ , err = validateRetrievalReceipt (context .Background (), storageNode .DID (), rcpt , vCtx )
155+ _ , err = validateRetrievalReceipt (context .Background (), storageNode .DID (), rcpt , vCtx , consumerTable , [] string { knownProvider . String ()} )
125156 assert .ErrorContains (t , err , "receipt is not issued by the requester node" )
126157 })
127158
@@ -137,7 +168,7 @@ func TestValidateRetrievalReceipt(t *testing.T) {
137168 // Tamper with the receipt to change its result
138169 tamperReceiptResult (t , rcpt )
139170
140- _ , err = validateRetrievalReceipt (context .Background (), storageNode .DID (), rcpt , vCtx )
171+ _ , err = validateRetrievalReceipt (context .Background (), storageNode .DID (), rcpt , vCtx , consumerTable , [] string { knownProvider . String ()} )
141172 assert .ErrorContains (t , err , "receipt signature is invalid" )
142173 })
143174
@@ -149,7 +180,7 @@ func TestValidateRetrievalReceipt(t *testing.T) {
149180 )
150181 require .NoError (t , err )
151182
152- _ , err = validateRetrievalReceipt (context .Background (), storageNode .DID (), rcpt , vCtx )
183+ _ , err = validateRetrievalReceipt (context .Background (), storageNode .DID (), rcpt , vCtx , consumerTable , [] string { knownProvider . String ()} )
153184 assert .ErrorContains (t , err , "original retrieve invocation must be attached to the receipt" )
154185 })
155186
@@ -173,11 +204,28 @@ func TestValidateRetrievalReceipt(t *testing.T) {
173204 )
174205 require .NoError (t , err )
175206
176- _ , err = validateRetrievalReceipt (context .Background (), storageNode .DID (), rcpt , vCtx )
207+ _ , err = validateRetrievalReceipt (context .Background (), storageNode .DID (), rcpt , vCtx , consumerTable , [] string { knownProvider . String ()} )
177208 expectedErr := "original invocation is not a " + content .RetrieveAbility + " invocation, but a other/ability one"
178209 assert .ErrorContains (t , err , expectedErr )
179210 })
180211
212+ t .Run ("wrong space provider" , func (t * testing.T ) {
213+ rcpt , err := receipt .Issue (
214+ storageNode ,
215+ result.Ok [content.RetrieveOk , failure.IPLDBuilderFailure ](content.RetrieveOk {}),
216+ ran .FromInvocation (inv ),
217+ )
218+ require .NoError (t , err )
219+
220+ otherProvider , err := did .Parse ("did:web:up.other.net" )
221+ require .NoError (t , err )
222+
223+ consumerTable := & mockConsumerTable {t : t , provider : otherProvider }
224+
225+ _ , err = validateRetrievalReceipt (context .Background (), storageNode .DID (), rcpt , vCtx , consumerTable , []string {knownProvider .String ()})
226+ assert .ErrorContains (t , err , "unknown space provider" )
227+ })
228+
181229 t .Run ("invalid delegation chain" , func (t * testing.T ) {
182230 // Bob invokes on the space, but the proof is from the space to Alice
183231 bobInvokes , err := invocation .Invoke (
@@ -201,7 +249,7 @@ func TestValidateRetrievalReceipt(t *testing.T) {
201249 )
202250 require .NoError (t , err )
203251
204- _ , err = validateRetrievalReceipt (context .Background (), storageNode .DID (), rcpt , vCtx )
252+ _ , err = validateRetrievalReceipt (context .Background (), storageNode .DID (), rcpt , vCtx , consumerTable , [] string { knownProvider . String ()} )
205253 assert .ErrorContains (t , err , "invalid delegation chain" )
206254 })
207255}
0 commit comments