Skip to content

Commit

Permalink
Fix a set of wrong string format issue
Browse files Browse the repository at this point in the history
It will fix FAB-1482.

Change-Id: I88777803f37e06f01d8c8578dd61699206873d26
Signed-off-by: grapebaba <281165273@qq.com>
  • Loading branch information
GrapeBaBa committed Dec 28, 2016
1 parent 5adaef7 commit c44a833
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 51 deletions.
20 changes: 10 additions & 10 deletions common/cauthdsl/cauthdsl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ func TestSimpleSignature(t *testing.T) {
}

if !spe.Authenticate(nil, [][]byte{signers[0]}, [][]byte{validSignature}) {
t.Errorf("Expected authentication to succeed with valid signatures")
t.Error("Expected authentication to succeed with valid signatures")
}
if spe.Authenticate(nil, [][]byte{signers[0]}, [][]byte{invalidSignature}) {
t.Errorf("Expected authentication to fail given the invalid signature")
t.Error("Expected authentication to fail given the invalid signature")
}
if spe.Authenticate(nil, [][]byte{signers[1]}, [][]byte{validSignature}) {
t.Errorf("Expected authentication to fail because signers[1] is not authorized in the policy, despite his valid signature")
t.Error("Expected authentication to fail because signers[1] is not authorized in the policy, despite his valid signature")
}
}

Expand All @@ -65,13 +65,13 @@ func TestMultipleSignature(t *testing.T) {
}

if !spe.Authenticate(nil, signers, [][]byte{validSignature, validSignature}) {
t.Errorf("Expected authentication to succeed with valid signatures")
t.Error("Expected authentication to succeed with valid signatures")
}
if spe.Authenticate(nil, signers, [][]byte{validSignature, invalidSignature}) {
t.Errorf("Expected authentication to fail given one of two invalid signatures")
t.Error("Expected authentication to fail given one of two invalid signatures")
}
if spe.Authenticate(nil, [][]byte{signers[0], signers[0]}, [][]byte{validSignature, validSignature}) {
t.Errorf("Expected authentication to fail because although there were two valid signatures, one was duplicated")
t.Error("Expected authentication to fail because although there were two valid signatures, one was duplicated")
}
}

Expand All @@ -85,13 +85,13 @@ func TestComplexNestedSignature(t *testing.T) {
}

if !spe.Authenticate(nil, signers, [][]byte{validSignature, validSignature}) {
t.Errorf("Expected authentication to succeed with valid signatures")
t.Error("Expected authentication to succeed with valid signatures")
}
if spe.Authenticate(nil, signers, [][]byte{invalidSignature, validSignature}) {
t.Errorf("Expected authentication failure as only the signature of signer[1] was valid")
t.Error("Expected authentication failure as only the signature of signer[1] was valid")
}
if !spe.Authenticate(nil, [][]byte{signers[0], signers[0]}, [][]byte{validSignature, validSignature}) {
t.Errorf("Expected authentication to succeed because the rule allows duplicated signatures for signer[0]")
t.Error("Expected authentication to succeed because the rule allows duplicated signatures for signer[0]")
}
}

Expand All @@ -104,6 +104,6 @@ func TestNegatively(t *testing.T) {
_ = proto.Unmarshal(b, policy)
_, err := NewSignaturePolicyEvaluator(policy, mch)
if err == nil {
t.Fatalf("Should have errored compiling because the Type field was nil")
t.Fatal("Should have errored compiling because the Type field was nil")
}
}
2 changes: 1 addition & 1 deletion common/configtx/bytes_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
cb "github.com/hyperledger/fabric/protos/common"
)

// BytesHandler is a trivial ConfigHandler which simpy tracks the bytes stores in a config
// BytesHandler is a trivial ConfigHandler which simply tracks the bytes stores in a config
type BytesHandler struct {
config map[string][]byte
proposed map[string][]byte
Expand Down
8 changes: 4 additions & 4 deletions common/configtx/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestForwardNonConfig(t *testing.T) {
Payload: []byte("Opaque"),
})
if result != filter.Forward {
t.Fatalf("Should have forwarded opaque message")
t.Fatal("Should have forwarded opaque message")
}
}

Expand All @@ -66,11 +66,11 @@ func TestAcceptGoodConfig(t *testing.T) {
}
result, committer := cf.Apply(configEnvelope)
if result != filter.Accept {
t.Fatalf("Should have indicated a good config message causes a reconfiguration")
t.Fatal("Should have indicated a good config message causes a reconfiguration")
}

if !committer.Isolated() {
t.Fatalf("Configuration transactions should be isolated to their own block")
t.Fatal("Configuration transactions should be isolated to their own block")
}

committer.Commit()
Expand All @@ -88,6 +88,6 @@ func TestRejectBadConfig(t *testing.T) {
Payload: configBytes,
})
if result != filter.Reject {
t.Fatalf("Should have rejected bad config message")
t.Fatal("Should have rejected bad config message")
}
}
5 changes: 3 additions & 2 deletions common/configtx/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
cb "github.com/hyperledger/fabric/protos/common"

"github.com/golang/protobuf/proto"
"errors"
)

// Handler provides a hook which allows other pieces of code to participate in config proposals
Expand Down Expand Up @@ -74,7 +75,7 @@ type configurationManager struct {
// or an error if there is a problem with the configuration envelope
func computeChainIDAndSequence(configtx *cb.ConfigurationEnvelope) (string, uint64, error) {
if len(configtx.Items) == 0 {
return "", 0, fmt.Errorf("Empty envelope unsupported")
return "", 0, errors.New("Empty envelope unsupported")
}

m := uint64(0) //configtx.Items[0].LastModified
Expand Down Expand Up @@ -115,7 +116,7 @@ func computeChainIDAndSequence(configtx *cb.ConfigurationEnvelope) (string, uint
func NewConfigurationManager(configtx *cb.ConfigurationEnvelope, pm policies.Manager, handlers map[cb.ConfigurationItem_ConfigurationType]Handler) (Manager, error) {
for ctype := range cb.ConfigurationItem_ConfigurationType_name {
if _, ok := handlers[cb.ConfigurationItem_ConfigurationType(ctype)]; !ok {
return nil, fmt.Errorf("Must supply a handler for all known types")
return nil, errors.New("Must supply a handler for all known types")
}
}

Expand Down
53 changes: 27 additions & 26 deletions common/configtx/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
cb "github.com/hyperledger/fabric/protos/common"

"github.com/golang/protobuf/proto"
"errors"
)

var defaultChain = "DefaultChainID"
Expand All @@ -43,7 +44,7 @@ type mockPolicy struct {

func (mp *mockPolicy) Evaluate(headers [][]byte, payload []byte, identities [][]byte, signatures [][]byte) error {
if mp == nil {
return fmt.Errorf("Invoked nil policy")
return errors.New("Invoked nil policy")
}
return mp.policyResult
}
Expand Down Expand Up @@ -85,7 +86,7 @@ func TestOmittedHandler(t *testing.T) {
}, &mockPolicyManager{&mockPolicy{}}, map[cb.ConfigurationItem_ConfigurationType]Handler{})

if err == nil {
t.Fatalf("Should have failed to construct manager because handlers were missing")
t.Fatal("Should have failed to construct manager because handlers were missing")
}
}

Expand All @@ -105,12 +106,12 @@ func TestWrongChainID(t *testing.T) {

err = cm.Validate(newConfig)
if err == nil {
t.Errorf("Should have errored when validating a new configuration set the wrong chain ID")
t.Error("Should have errored when validating a new configuration set the wrong chain ID")
}

err = cm.Apply(newConfig)
if err == nil {
t.Errorf("Should have errored when applying a new configuration with the wrong chain ID")
t.Error("Should have errored when applying a new configuration with the wrong chain ID")
}
}

Expand All @@ -130,12 +131,12 @@ func TestOldConfigReplay(t *testing.T) {

err = cm.Validate(newConfig)
if err == nil {
t.Errorf("Should have errored when validating a configuration that is not a newer sequence number")
t.Error("Should have errored when validating a configuration that is not a newer sequence number")
}

err = cm.Apply(newConfig)
if err == nil {
t.Errorf("Should have errored when applying a configuration that is not a newer sequence number")
t.Error("Should have errored when applying a configuration that is not a newer sequence number")
}
}

Expand All @@ -148,7 +149,7 @@ func TestInvalidInitialConfigByStructure(t *testing.T) {
}, &mockPolicyManager{&mockPolicy{}}, defaultHandlers())

if err == nil {
t.Fatalf("Should have failed to construct configuration by policy")
t.Fatal("Should have failed to construct configuration by policy")
}
}

Expand Down Expand Up @@ -197,12 +198,12 @@ func TestConfigChangeRegressedSequence(t *testing.T) {

err = cm.Validate(newConfig)
if err == nil {
t.Errorf("Should have errored validating config because foo's sequence number regressed")
t.Error("Should have errored validating config because foo's sequence number regressed")
}

err = cm.Apply(newConfig)
if err == nil {
t.Errorf("Should have errored applying config because foo's sequence number regressed")
t.Error("Should have errored applying config because foo's sequence number regressed")
}
}

Expand All @@ -226,12 +227,12 @@ func TestConfigChangeOldSequence(t *testing.T) {

err = cm.Validate(newConfig)
if err == nil {
t.Errorf("Should have errored validating config because bar was new but its sequence number was old")
t.Error("Should have errored validating config because bar was new but its sequence number was old")
}

err = cm.Apply(newConfig)
if err == nil {
t.Errorf("Should have errored applying config because bar was new but its sequence number was old")
t.Error("Should have errored applying config because bar was new but its sequence number was old")
}
}

Expand All @@ -257,12 +258,12 @@ func TestConfigImplicitDelete(t *testing.T) {

err = cm.Validate(newConfig)
if err == nil {
t.Errorf("Should have errored validating config because foo was implicitly deleted")
t.Error("Should have errored validating config because foo was implicitly deleted")
}

err = cm.Apply(newConfig)
if err == nil {
t.Errorf("Should have errored applying config because foo was implicitly deleted")
t.Error("Should have errored applying config because foo was implicitly deleted")
}
}

Expand All @@ -280,12 +281,12 @@ func TestEmptyConfigUpdate(t *testing.T) {

err = cm.Validate(newConfig)
if err == nil {
t.Errorf("Should not errored validating config because new config is empty")
t.Error("Should not errored validating config because new config is empty")
}

err = cm.Apply(newConfig)
if err == nil {
t.Errorf("Should not errored applying config because new config is empty")
t.Error("Should not errored applying config because new config is empty")
}
}

Expand Down Expand Up @@ -313,12 +314,12 @@ func TestSilentConfigModification(t *testing.T) {

err = cm.Validate(newConfig)
if err == nil {
t.Errorf("Should not errored validating config because foo was silently modified (despite modification allowed by policy)")
t.Error("Should not errored validating config because foo was silently modified (despite modification allowed by policy)")
}

err = cm.Apply(newConfig)
if err == nil {
t.Errorf("Should not errored applying config because foo was silently modified (despite modification allowed by policy)")
t.Error("Should not errored applying config because foo was silently modified (despite modification allowed by policy)")
}
}

Expand All @@ -331,7 +332,7 @@ func TestInvalidInitialConfigByPolicy(t *testing.T) {
// mockPolicyManager will return non-validating defualt policy

if err == nil {
t.Fatalf("Should have failed to construct configuration by policy")
t.Fatal("Should have failed to construct configuration by policy")
}
}

Expand All @@ -355,12 +356,12 @@ func TestConfigChangeViolatesPolicy(t *testing.T) {

err = cm.Validate(newConfig)
if err == nil {
t.Errorf("Should have errored validating config because policy rejected modification")
t.Error("Should have errored validating config because policy rejected modification")
}

err = cm.Apply(newConfig)
if err == nil {
t.Errorf("Should have errored applying config because policy rejected modification")
t.Error("Should have errored applying config because policy rejected modification")
}
}

Expand All @@ -370,7 +371,7 @@ func (fh failHandler) BeginConfig() {}
func (fh failHandler) RollbackConfig() {}
func (fh failHandler) CommitConfig() {}
func (fh failHandler) ProposeConfig(item *cb.ConfigurationItem) error {
return fmt.Errorf("Fail")
return errors.New("Fail")
}

// TestInvalidProposal checks that even if the policy allows the transaction and the sequence etc. is well formed,
Expand All @@ -393,12 +394,12 @@ func TestInvalidProposal(t *testing.T) {

err = cm.Validate(newConfig)
if err == nil {
t.Errorf("Should have errored validating config because the handler rejected it")
t.Error("Should have errored validating config because the handler rejected it")
}

err = cm.Apply(newConfig)
if err == nil {
t.Errorf("Should have errored applying config because the handler rejected it")
t.Error("Should have errored applying config because the handler rejected it")
}
}

Expand All @@ -413,7 +414,7 @@ func TestMissingHeader(t *testing.T) {
}, &mockPolicyManager{&mockPolicy{}}, handlers)

if err == nil {
t.Errorf("Should have errored creating the configuration manager because of the missing header")
t.Error("Should have errored creating the configuration manager because of the missing header")
}
}

Expand All @@ -425,7 +426,7 @@ func TestMissingChainID(t *testing.T) {
}, &mockPolicyManager{&mockPolicy{}}, handlers)

if err == nil {
t.Errorf("Should have errored creating the configuration manager because of the missing header")
t.Error("Should have errored creating the configuration manager because of the missing header")
}
}

Expand All @@ -440,6 +441,6 @@ func TestMismatchedChainID(t *testing.T) {
}, &mockPolicyManager{&mockPolicy{}}, handlers)

if err == nil {
t.Errorf("Should have errored creating the configuration manager because of the missing header")
t.Error("Should have errored creating the configuration manager because of the missing header")
}
}
7 changes: 4 additions & 3 deletions common/policies/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
cb "github.com/hyperledger/fabric/protos/common"

"github.com/golang/protobuf/proto"
"errors"
)

// Policy is used to determine if a signature is valid
Expand Down Expand Up @@ -52,7 +53,7 @@ func newPolicy(policySource *cb.Policy, ch cauthdsl.CryptoHelper) (*policy, erro
}

if envelopeWrapper.SignaturePolicy == nil {
return nil, fmt.Errorf("Nil signature policy received")
return nil, errors.New("Nil signature policy received")
}

sigPolicy := envelopeWrapper.SignaturePolicy
Expand All @@ -73,12 +74,12 @@ func newPolicy(policySource *cb.Policy, ch cauthdsl.CryptoHelper) (*policy, erro
// verifies the corresponding signature.
func (p *policy) Evaluate(header [][]byte, payload []byte, identities [][]byte, signatures [][]byte) error {
if p == nil {
return fmt.Errorf("Evaluated default policy, results in reject")
return errors.New("Evaluated default policy, results in reject")
}

// XXX This is wrong, as the signatures are over the payload envelope, not the message, fix either here, or in cauthdsl once transaction is finalized
if !p.evaluator.Authenticate(payload, identities, signatures) {
return fmt.Errorf("Failed to authenticate policy")
return errors.New("Failed to authenticate policy")
}
return nil
}
Expand Down

1 comment on commit c44a833

@zuochuanmin
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does package name "cauthdsl" mean? It is not an English word

Please sign in to comment.