Skip to content

Commit

Permalink
[FAB-3522] Increase test coverage for validation
Browse files Browse the repository at this point in the history
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 <ale.linux@sopit.net>
  • Loading branch information
ale-linux committed Apr 30, 2017
1 parent a0763aa commit 4b4a3d8
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 32 deletions.
101 changes: 69 additions & 32 deletions core/common/validation/fullflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/peer"
"github.com/hyperledger/fabric/protos/utils"
"github.com/stretchr/testify/assert"
)

func getProposal() (*peer.Proposal, error) {
Expand Down Expand Up @@ -223,16 +224,6 @@ func TestTXWithTwoActionsRejected(t *testing.T) {
}
}

var r *rand.Rand

func corrupt(bytes []byte) {
if r == nil {
r = rand.New(rand.NewSource(time.Now().Unix()))
}

bytes[r.Int31n(int32(len(bytes)))]--
}

func TestBadProp(t *testing.T) {
// get a toy proposal
prop, err := getProposal()
Expand All @@ -249,13 +240,17 @@ func TestBadProp(t *testing.T) {
}

// mess with the signature
corrupt(sProp.Signature)

// validate it - it should fail
_, _, _, err = ValidateProposalMessage(sProp)
if err == nil {
t.Fatal("ValidateProposalMessage should have failed")
return
sigOrig := sProp.Signature
for i := 0; i < len(sigOrig); i++ {
sigCopy := make([]byte, len(sigOrig))
copy(sigCopy, sigOrig)
sigCopy[i] = byte(int(sigCopy[i]+1) % 255)
// validate it - it should fail
_, _, _, err = ValidateProposalMessage(&peer.SignedProposal{ProposalBytes: sProp.ProposalBytes, Signature: sigCopy})
if err == nil {
t.Fatal("ValidateProposalMessage should have failed")
return
}
}

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

// mess with the message
corrupt(sProp.ProposalBytes)

// validate it - it should fail
_, _, _, err = ValidateProposalMessage(sProp)
if err == nil {
t.Fatal("ValidateProposalMessage should have failed")
return
pbytesOrig := sProp.ProposalBytes
for i := 0; i < len(pbytesOrig); i++ {
pbytesCopy := make([]byte, len(pbytesOrig))
copy(pbytesCopy, pbytesOrig)
pbytesCopy[i] = byte(int(pbytesCopy[i]+1) % 255)
// validate it - it should fail
_, _, _, err = ValidateProposalMessage(&peer.SignedProposal{ProposalBytes: pbytesCopy, Signature: sProp.Signature})
if err == nil {
t.Fatal("ValidateProposalMessage should have failed")
return
}
}

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

func corrupt(bytes []byte) {
rand.Seed(time.Now().UnixNano())
bytes[rand.Intn(len(bytes))]--
}

func TestBadTx(t *testing.T) {
// get a toy proposal
prop, err := getProposal()
Expand All @@ -323,13 +327,17 @@ func TestBadTx(t *testing.T) {
}

// mess with the transaction payload
corrupt(tx.Payload)

// validate the transaction it should fail
_, txResult := ValidateTransaction(tx)
if txResult == peer.TxValidationCode_VALID {
t.Fatal("ValidateTransaction should have failed")
return
paylOrig := tx.Payload
for i := 0; i < len(paylOrig); i++ {
paylCopy := make([]byte, len(paylOrig))
copy(paylCopy, paylOrig)
paylCopy[i] = byte(int(paylCopy[i]+1) % 255)
// validate the transaction it should fail
_, txResult := ValidateTransaction(&common.Envelope{Signature: tx.Signature, Payload: paylCopy})
if txResult == peer.TxValidationCode_VALID {
t.Fatal("ValidateTransaction should have failed")
return
}
}

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

// validate the transaction it should fail
_, txResult = ValidateTransaction(tx)
_, txResult := ValidateTransaction(tx)
if txResult == peer.TxValidationCode_VALID {
t.Fatal("ValidateTransaction should have failed")
return
Expand Down Expand Up @@ -429,6 +437,35 @@ func Test2EndorsersDisagree(t *testing.T) {
}
}

func TestInvocationsBadArgs(t *testing.T) {
_, code := ValidateTransaction(nil)
assert.Equal(t, code, peer.TxValidationCode_NIL_ENVELOPE)
err := validateEndorserTransaction(nil, nil)
assert.Error(t, err)
err = validateConfigTransaction(nil, nil)
assert.Error(t, err)
_, _, err = validateCommonHeader(nil)
assert.Error(t, err)
err = validateChannelHeader(nil)
assert.Error(t, err)
err = validateChannelHeader(&common.ChannelHeader{})
assert.Error(t, err)
err = validateSignatureHeader(nil)
assert.Error(t, err)
err = validateSignatureHeader(&common.SignatureHeader{})
assert.Error(t, err)
err = validateSignatureHeader(&common.SignatureHeader{Nonce: []byte("a")})
assert.Error(t, err)
err = checkSignatureFromCreator(nil, nil, nil, "")
assert.Error(t, err)
_, _, _, err = ValidateProposalMessage(nil)
assert.Error(t, err)
_, err = validateChaincodeProposalMessage(nil, nil)
assert.Error(t, err)
_, err = validateChaincodeProposalMessage(&peer.Proposal{}, &common.Header{[]byte("a"), []byte("a")})
assert.Error(t, err)
}

var signer msp.SigningIdentity
var signerSerialized []byte

Expand Down
8 changes: 8 additions & 0 deletions core/common/validation/msgvalidation.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ var putilsLogger = flogging.MustGetLogger("protoutils")

// validateChaincodeProposalMessage checks the validity of a Proposal message of type CHAINCODE
func validateChaincodeProposalMessage(prop *pb.Proposal, hdr *common.Header) (*pb.ChaincodeHeaderExtension, error) {
if prop == nil || hdr == nil {
return nil, errors.New("Nil arguments")
}

putilsLogger.Debugf("validateChaincodeProposalMessage starts for proposal %p, header %p", prop, hdr)

// 4) based on the header type (assuming it's CHAINCODE), look at the extensions
Expand Down Expand Up @@ -64,6 +68,10 @@ func validateChaincodeProposalMessage(prop *pb.Proposal, hdr *common.Header) (*p
// this function returns Header and ChaincodeHeaderExtension messages since they
// have been unmarshalled and validated
func ValidateProposalMessage(signedProp *pb.SignedProposal) (*pb.Proposal, *common.Header, *pb.ChaincodeHeaderExtension, error) {
if signedProp == nil {
return nil, nil, nil, errors.New("Nil arguments")
}

putilsLogger.Debugf("ValidateProposalMessage starts for signed proposal %p", signedProp)

// extract the Proposal message from signedProp
Expand Down

0 comments on commit 4b4a3d8

Please sign in to comment.