Skip to content

Commit

Permalink
[FAB-11830] Code hygiene for key-level validation tests
Browse files Browse the repository at this point in the history
-) avoid using sleeps
-) use keyed fields
-) log seed of randomness in case of assert violations

Change-Id: I9fffa24159bfede8d8ce74cef680f271a6a9694b
Signed-off-by: Alessandro Sorniotti <ale.linux@sopit.net>
Signed-off-by: Matthias Neugschwandtner <eug@zurich.ibm.com>
  • Loading branch information
ale-linux committed Aug 31, 2018
1 parent 5388376 commit 2d67d38
Show file tree
Hide file tree
Showing 6 changed files with 329 additions and 333 deletions.
2 changes: 1 addition & 1 deletion core/common/validation/statebased/validator_keylevel.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (p *policyChecker) checkSBAndCCEP(cc, coll, key string, blockNum, txNum uin
vp, err := p.vpmgr.GetValidationParameterForKey(cc, coll, key, blockNum, txNum)
if err != nil {
switch err := err.(type) {
case *ValidationParameterUpdatedErr:
case *ValidationParameterUpdatedError:
return policyErr(err)
default:
return &commonerrors.VSCCExecutionFailureError{
Expand Down
60 changes: 30 additions & 30 deletions core/common/validation/statebased/validator_keylevel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ func TestKeylevelValidation(t *testing.T) {
// We simulate policy check success and failure

vpMetadataKey := pb.MetaDataKeys_VALIDATION_PARAMETER.String()
mr := &mockState{map[string][]byte{vpMetadataKey: []byte("EP")}, nil, map[string][]byte{vpMetadataKey: []byte("EP")}, nil, false}
ms := &mockStateFetcher{mr, nil}
pm := &KeyLevelValidationParameterManagerImpl{Support: ms}
mr := &mockState{GetStateMetadataRv: map[string][]byte{vpMetadataKey: []byte("EP")}, GetPrivateDataMetadataRv: map[string][]byte{vpMetadataKey: []byte("EP")}}
ms := &mockStateFetcher{FetchStateRv: mr}
pm := &KeyLevelValidationParameterManagerImpl{StateFetcher: ms}
pe := &mockPolicyEvaluator{}
validator := NewKeyLevelValidator(pe, pm)

Expand Down Expand Up @@ -128,9 +128,9 @@ func TestKeylevelValidationPvtData(t *testing.T) {
// We simulate policy check success and failure

vpMetadataKey := pb.MetaDataKeys_VALIDATION_PARAMETER.String()
mr := &mockState{map[string][]byte{vpMetadataKey: []byte("EP")}, nil, map[string][]byte{vpMetadataKey: []byte("EP")}, nil, false}
ms := &mockStateFetcher{mr, nil}
pm := &KeyLevelValidationParameterManagerImpl{Support: ms}
mr := &mockState{GetStateMetadataRv: map[string][]byte{vpMetadataKey: []byte("EP")}, GetPrivateDataMetadataRv: map[string][]byte{vpMetadataKey: []byte("EP")}}
ms := &mockStateFetcher{FetchStateRv: mr}
pm := &KeyLevelValidationParameterManagerImpl{StateFetcher: ms}
pe := &mockPolicyEvaluator{}
validator := NewKeyLevelValidator(pe, pm)

Expand Down Expand Up @@ -166,9 +166,9 @@ func TestKeylevelValidationMetaUpdate(t *testing.T) {
// We simulate policy check success and failure

vpMetadataKey := pb.MetaDataKeys_VALIDATION_PARAMETER.String()
mr := &mockState{map[string][]byte{vpMetadataKey: []byte("EP")}, nil, map[string][]byte{vpMetadataKey: []byte("EP")}, nil, false}
ms := &mockStateFetcher{mr, nil}
pm := &KeyLevelValidationParameterManagerImpl{Support: ms}
mr := &mockState{GetStateMetadataRv: map[string][]byte{vpMetadataKey: []byte("EP")}, GetPrivateDataMetadataRv: map[string][]byte{vpMetadataKey: []byte("EP")}}
ms := &mockStateFetcher{FetchStateRv: mr}
pm := &KeyLevelValidationParameterManagerImpl{StateFetcher: ms}
pe := &mockPolicyEvaluator{}
validator := NewKeyLevelValidator(pe, pm)

Expand Down Expand Up @@ -204,9 +204,9 @@ func TestKeylevelValidationPvtMetaUpdate(t *testing.T) {
// We simulate policy check success and failure

vpMetadataKey := pb.MetaDataKeys_VALIDATION_PARAMETER.String()
mr := &mockState{map[string][]byte{vpMetadataKey: []byte("EP")}, nil, map[string][]byte{vpMetadataKey: []byte("EP")}, nil, false}
ms := &mockStateFetcher{mr, nil}
pm := &KeyLevelValidationParameterManagerImpl{Support: ms}
mr := &mockState{GetStateMetadataRv: map[string][]byte{vpMetadataKey: []byte("EP")}, GetPrivateDataMetadataRv: map[string][]byte{vpMetadataKey: []byte("EP")}}
ms := &mockStateFetcher{FetchStateRv: mr}
pm := &KeyLevelValidationParameterManagerImpl{StateFetcher: ms}
pe := &mockPolicyEvaluator{}
validator := NewKeyLevelValidator(pe, pm)

Expand Down Expand Up @@ -242,9 +242,9 @@ func TestKeylevelValidationPolicyRetrievalFailure(t *testing.T) {
// we simulate the case where we fail to retrieve
// the validation parameters from the ledger.

mr := &mockState{nil, fmt.Errorf("metadata retrieval failure"), nil, nil, false}
ms := &mockStateFetcher{mr, nil}
pm := &KeyLevelValidationParameterManagerImpl{Support: ms}
mr := &mockState{GetStateMetadataErr: fmt.Errorf("metadata retrieval failure")}
ms := &mockStateFetcher{FetchStateRv: mr}
pm := &KeyLevelValidationParameterManagerImpl{StateFetcher: ms}
validator := NewKeyLevelValidator(&mockPolicyEvaluator{}, pm)

rwsb := rwsetBytes(t, "cc")
Expand All @@ -269,9 +269,9 @@ func TestCCEPValidation(t *testing.T) {
// touch any key with a state-based endorsement policy;
// we expect to check the normal cc-endorsement policy.

mr := &mockState{map[string][]byte{}, nil, map[string][]byte{}, nil, false}
ms := &mockStateFetcher{mr, nil}
pm := &KeyLevelValidationParameterManagerImpl{Support: ms}
mr := &mockState{GetStateMetadataRv: map[string][]byte{}, GetPrivateDataMetadataRv: map[string][]byte{}}
ms := &mockStateFetcher{FetchStateRv: mr}
pm := &KeyLevelValidationParameterManagerImpl{StateFetcher: ms}
pe := &mockPolicyEvaluator{}
validator := NewKeyLevelValidator(pe, pm)

Expand Down Expand Up @@ -309,9 +309,9 @@ func TestCCEPValidationReads(t *testing.T) {
// touch any key with a state-based endorsement policy;
// we expect to check the normal cc-endorsement policy.

mr := &mockState{map[string][]byte{}, nil, map[string][]byte{}, nil, false}
ms := &mockStateFetcher{mr, nil}
pm := &KeyLevelValidationParameterManagerImpl{Support: ms}
mr := &mockState{GetStateMetadataRv: map[string][]byte{}, GetPrivateDataMetadataRv: map[string][]byte{}}
ms := &mockStateFetcher{FetchStateRv: mr}
pm := &KeyLevelValidationParameterManagerImpl{StateFetcher: ms}
pe := &mockPolicyEvaluator{}
validator := NewKeyLevelValidator(pe, pm)

Expand Down Expand Up @@ -349,9 +349,9 @@ func TestOnlySBEPChecked(t *testing.T) {
// but the state-based one, and expect successful evaluation

vpMetadataKey := pb.MetaDataKeys_VALIDATION_PARAMETER.String()
mr := &mockState{map[string][]byte{vpMetadataKey: []byte("SBEP")}, nil, map[string][]byte{}, nil, false}
ms := &mockStateFetcher{mr, nil}
pm := &KeyLevelValidationParameterManagerImpl{Support: ms}
mr := &mockState{GetStateMetadataRv: map[string][]byte{vpMetadataKey: []byte("SBEP")}}
ms := &mockStateFetcher{FetchStateRv: mr}
pm := &KeyLevelValidationParameterManagerImpl{StateFetcher: ms}
pe := &mockPolicyEvaluator{}
validator := NewKeyLevelValidator(pe, pm)

Expand Down Expand Up @@ -391,9 +391,9 @@ func TestCCEPValidationPvtReads(t *testing.T) {
// touch any key with a state-based endorsement policy;
// we expect to check the normal cc-endorsement policy.

mr := &mockState{map[string][]byte{}, nil, map[string][]byte{}, nil, false}
ms := &mockStateFetcher{mr, nil}
pm := &KeyLevelValidationParameterManagerImpl{Support: ms}
mr := &mockState{GetStateMetadataRv: map[string][]byte{}, GetPrivateDataMetadataRv: map[string][]byte{}}
ms := &mockStateFetcher{FetchStateRv: mr}
pm := &KeyLevelValidationParameterManagerImpl{StateFetcher: ms}
pe := &mockPolicyEvaluator{}
validator := NewKeyLevelValidator(pe, pm)

Expand Down Expand Up @@ -431,9 +431,9 @@ func TestKeylevelValidationFailure(t *testing.T) {
// for that very same key.

vpMetadataKey := pb.MetaDataKeys_VALIDATION_PARAMETER.String()
mr := &mockState{map[string][]byte{vpMetadataKey: []byte("EP")}, nil, map[string][]byte{vpMetadataKey: []byte("EP")}, nil, false}
ms := &mockStateFetcher{mr, nil}
pm := &KeyLevelValidationParameterManagerImpl{Support: ms}
mr := &mockState{GetStateMetadataRv: map[string][]byte{vpMetadataKey: []byte("EP")}, GetPrivateDataMetadataRv: map[string][]byte{vpMetadataKey: []byte("EP")}}
ms := &mockStateFetcher{FetchStateRv: mr}
pm := &KeyLevelValidationParameterManagerImpl{StateFetcher: ms}
validator := NewKeyLevelValidator(&mockPolicyEvaluator{}, pm)

rwsb := rwsetBytes(t, "cc")
Expand Down
4 changes: 2 additions & 2 deletions core/common/validation/statebased/vpmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import (
// ValidationParameterUpdatedErr is returned whenever
// Validation Parameters for a key could not be
// supplied because they are being updated
type ValidationParameterUpdatedErr struct {
type ValidationParameterUpdatedError struct {
Key string
Height uint64
}

func (f *ValidationParameterUpdatedErr) Error() string {
func (f *ValidationParameterUpdatedError) Error() string {
return fmt.Sprintf("validation parameters for key %s have been changed in a transaction in block %d", f.Key, f.Height)
}

Expand Down
Loading

0 comments on commit 2d67d38

Please sign in to comment.