@@ -31,6 +31,7 @@ import (
31
31
"github.com/hyperledger/fabric/protos/common"
32
32
"github.com/hyperledger/fabric/protos/peer"
33
33
"github.com/hyperledger/fabric/protos/utils"
34
+ "github.com/stretchr/testify/assert"
34
35
)
35
36
36
37
func getProposal () (* peer.Proposal , error ) {
@@ -223,16 +224,6 @@ func TestTXWithTwoActionsRejected(t *testing.T) {
223
224
}
224
225
}
225
226
226
- var r * rand.Rand
227
-
228
- func corrupt (bytes []byte ) {
229
- if r == nil {
230
- r = rand .New (rand .NewSource (time .Now ().Unix ()))
231
- }
232
-
233
- bytes [r .Int31n (int32 (len (bytes )))]--
234
- }
235
-
236
227
func TestBadProp (t * testing.T ) {
237
228
// get a toy proposal
238
229
prop , err := getProposal ()
@@ -249,13 +240,17 @@ func TestBadProp(t *testing.T) {
249
240
}
250
241
251
242
// mess with the signature
252
- corrupt (sProp .Signature )
253
-
254
- // validate it - it should fail
255
- _ , _ , _ , err = ValidateProposalMessage (sProp )
256
- if err == nil {
257
- t .Fatal ("ValidateProposalMessage should have failed" )
258
- return
243
+ sigOrig := sProp .Signature
244
+ for i := 0 ; i < len (sigOrig ); i ++ {
245
+ sigCopy := make ([]byte , len (sigOrig ))
246
+ copy (sigCopy , sigOrig )
247
+ sigCopy [i ] = byte (int (sigCopy [i ]+ 1 ) % 255 )
248
+ // validate it - it should fail
249
+ _ , _ , _ , err = ValidateProposalMessage (& peer.SignedProposal {ProposalBytes : sProp .ProposalBytes , Signature : sigCopy })
250
+ if err == nil {
251
+ t .Fatal ("ValidateProposalMessage should have failed" )
252
+ return
253
+ }
259
254
}
260
255
261
256
// sign it again
@@ -266,13 +261,17 @@ func TestBadProp(t *testing.T) {
266
261
}
267
262
268
263
// mess with the message
269
- corrupt (sProp .ProposalBytes )
270
-
271
- // validate it - it should fail
272
- _ , _ , _ , err = ValidateProposalMessage (sProp )
273
- if err == nil {
274
- t .Fatal ("ValidateProposalMessage should have failed" )
275
- return
264
+ pbytesOrig := sProp .ProposalBytes
265
+ for i := 0 ; i < len (pbytesOrig ); i ++ {
266
+ pbytesCopy := make ([]byte , len (pbytesOrig ))
267
+ copy (pbytesCopy , pbytesOrig )
268
+ pbytesCopy [i ] = byte (int (pbytesCopy [i ]+ 1 ) % 255 )
269
+ // validate it - it should fail
270
+ _ , _ , _ , err = ValidateProposalMessage (& peer.SignedProposal {ProposalBytes : pbytesCopy , Signature : sProp .Signature })
271
+ if err == nil {
272
+ t .Fatal ("ValidateProposalMessage should have failed" )
273
+ return
274
+ }
276
275
}
277
276
278
277
// get a bad signing identity
@@ -297,6 +296,11 @@ func TestBadProp(t *testing.T) {
297
296
}
298
297
}
299
298
299
+ func corrupt (bytes []byte ) {
300
+ rand .Seed (time .Now ().UnixNano ())
301
+ bytes [rand .Intn (len (bytes ))]--
302
+ }
303
+
300
304
func TestBadTx (t * testing.T ) {
301
305
// get a toy proposal
302
306
prop , err := getProposal ()
@@ -323,13 +327,17 @@ func TestBadTx(t *testing.T) {
323
327
}
324
328
325
329
// mess with the transaction payload
326
- corrupt (tx .Payload )
327
-
328
- // validate the transaction it should fail
329
- _ , txResult := ValidateTransaction (tx )
330
- if txResult == peer .TxValidationCode_VALID {
331
- t .Fatal ("ValidateTransaction should have failed" )
332
- return
330
+ paylOrig := tx .Payload
331
+ for i := 0 ; i < len (paylOrig ); i ++ {
332
+ paylCopy := make ([]byte , len (paylOrig ))
333
+ copy (paylCopy , paylOrig )
334
+ paylCopy [i ] = byte (int (paylCopy [i ]+ 1 ) % 255 )
335
+ // validate the transaction it should fail
336
+ _ , txResult := ValidateTransaction (& common.Envelope {Signature : tx .Signature , Payload : paylCopy })
337
+ if txResult == peer .TxValidationCode_VALID {
338
+ t .Fatal ("ValidateTransaction should have failed" )
339
+ return
340
+ }
333
341
}
334
342
335
343
// assemble a transaction from that proposal and endorsement
@@ -343,7 +351,7 @@ func TestBadTx(t *testing.T) {
343
351
corrupt (tx .Signature )
344
352
345
353
// validate the transaction it should fail
346
- _ , txResult = ValidateTransaction (tx )
354
+ _ , txResult : = ValidateTransaction (tx )
347
355
if txResult == peer .TxValidationCode_VALID {
348
356
t .Fatal ("ValidateTransaction should have failed" )
349
357
return
@@ -429,6 +437,35 @@ func Test2EndorsersDisagree(t *testing.T) {
429
437
}
430
438
}
431
439
440
+ func TestInvocationsBadArgs (t * testing.T ) {
441
+ _ , code := ValidateTransaction (nil )
442
+ assert .Equal (t , code , peer .TxValidationCode_NIL_ENVELOPE )
443
+ err := validateEndorserTransaction (nil , nil )
444
+ assert .Error (t , err )
445
+ err = validateConfigTransaction (nil , nil )
446
+ assert .Error (t , err )
447
+ _ , _ , err = validateCommonHeader (nil )
448
+ assert .Error (t , err )
449
+ err = validateChannelHeader (nil )
450
+ assert .Error (t , err )
451
+ err = validateChannelHeader (& common.ChannelHeader {})
452
+ assert .Error (t , err )
453
+ err = validateSignatureHeader (nil )
454
+ assert .Error (t , err )
455
+ err = validateSignatureHeader (& common.SignatureHeader {})
456
+ assert .Error (t , err )
457
+ err = validateSignatureHeader (& common.SignatureHeader {Nonce : []byte ("a" )})
458
+ assert .Error (t , err )
459
+ err = checkSignatureFromCreator (nil , nil , nil , "" )
460
+ assert .Error (t , err )
461
+ _ , _ , _ , err = ValidateProposalMessage (nil )
462
+ assert .Error (t , err )
463
+ _ , err = validateChaincodeProposalMessage (nil , nil )
464
+ assert .Error (t , err )
465
+ _ , err = validateChaincodeProposalMessage (& peer.Proposal {}, & common.Header {[]byte ("a" ), []byte ("a" )})
466
+ assert .Error (t , err )
467
+ }
468
+
432
469
var signer msp.SigningIdentity
433
470
var signerSerialized []byte
434
471
0 commit comments