Skip to content

Commit 4b4a3d8

Browse files
committed
[FAB-3522] Increase test coverage for validation
Test output for core/common/validation now is coverage: 85.9% of statements ok github.com/hyperledger/fabric/core/common/validation 1.432s Change-Id: If5a2b2b4946863c748fc59aca4920511f673eca1 Signed-off-by: Alessandro Sorniotti <[email protected]>
1 parent a0763aa commit 4b4a3d8

File tree

2 files changed

+77
-32
lines changed

2 files changed

+77
-32
lines changed

core/common/validation/fullflow_test.go

+69-32
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/hyperledger/fabric/protos/common"
3232
"github.com/hyperledger/fabric/protos/peer"
3333
"github.com/hyperledger/fabric/protos/utils"
34+
"github.com/stretchr/testify/assert"
3435
)
3536

3637
func getProposal() (*peer.Proposal, error) {
@@ -223,16 +224,6 @@ func TestTXWithTwoActionsRejected(t *testing.T) {
223224
}
224225
}
225226

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-
236227
func TestBadProp(t *testing.T) {
237228
// get a toy proposal
238229
prop, err := getProposal()
@@ -249,13 +240,17 @@ func TestBadProp(t *testing.T) {
249240
}
250241

251242
// 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+
}
259254
}
260255

261256
// sign it again
@@ -266,13 +261,17 @@ func TestBadProp(t *testing.T) {
266261
}
267262

268263
// 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+
}
276275
}
277276

278277
// get a bad signing identity
@@ -297,6 +296,11 @@ func TestBadProp(t *testing.T) {
297296
}
298297
}
299298

299+
func corrupt(bytes []byte) {
300+
rand.Seed(time.Now().UnixNano())
301+
bytes[rand.Intn(len(bytes))]--
302+
}
303+
300304
func TestBadTx(t *testing.T) {
301305
// get a toy proposal
302306
prop, err := getProposal()
@@ -323,13 +327,17 @@ func TestBadTx(t *testing.T) {
323327
}
324328

325329
// 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+
}
333341
}
334342

335343
// assemble a transaction from that proposal and endorsement
@@ -343,7 +351,7 @@ func TestBadTx(t *testing.T) {
343351
corrupt(tx.Signature)
344352

345353
// validate the transaction it should fail
346-
_, txResult = ValidateTransaction(tx)
354+
_, txResult := ValidateTransaction(tx)
347355
if txResult == peer.TxValidationCode_VALID {
348356
t.Fatal("ValidateTransaction should have failed")
349357
return
@@ -429,6 +437,35 @@ func Test2EndorsersDisagree(t *testing.T) {
429437
}
430438
}
431439

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+
432469
var signer msp.SigningIdentity
433470
var signerSerialized []byte
434471

core/common/validation/msgvalidation.go

+8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ var putilsLogger = flogging.MustGetLogger("protoutils")
3232

3333
// validateChaincodeProposalMessage checks the validity of a Proposal message of type CHAINCODE
3434
func validateChaincodeProposalMessage(prop *pb.Proposal, hdr *common.Header) (*pb.ChaincodeHeaderExtension, error) {
35+
if prop == nil || hdr == nil {
36+
return nil, errors.New("Nil arguments")
37+
}
38+
3539
putilsLogger.Debugf("validateChaincodeProposalMessage starts for proposal %p, header %p", prop, hdr)
3640

3741
// 4) based on the header type (assuming it's CHAINCODE), look at the extensions
@@ -64,6 +68,10 @@ func validateChaincodeProposalMessage(prop *pb.Proposal, hdr *common.Header) (*p
6468
// this function returns Header and ChaincodeHeaderExtension messages since they
6569
// have been unmarshalled and validated
6670
func ValidateProposalMessage(signedProp *pb.SignedProposal) (*pb.Proposal, *common.Header, *pb.ChaincodeHeaderExtension, error) {
71+
if signedProp == nil {
72+
return nil, nil, nil, errors.New("Nil arguments")
73+
}
74+
6775
putilsLogger.Debugf("ValidateProposalMessage starts for signed proposal %p", signedProp)
6876

6977
// extract the Proposal message from signedProp

0 commit comments

Comments
 (0)