Skip to content

Commit

Permalink
Merge "[FAB-4323] Improve code coverage of orderer filter"
Browse files Browse the repository at this point in the history
  • Loading branch information
christo4ferris authored and Gerrit Code Review committed Jun 4, 2017
2 parents ae71b2f + 4ae322b commit 38d3879
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 17 deletions.
100 changes: 83 additions & 17 deletions orderer/common/configtxfilter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package configtxfilter

import (
"fmt"
"reflect"
"testing"

mockconfigtx "github.com/hyperledger/fabric/common/mocks/configtx"
Expand All @@ -27,16 +26,60 @@ import (
"github.com/hyperledger/fabric/protos/utils"

"github.com/golang/protobuf/proto"
"github.com/stretchr/testify/assert"
)

func TestForwardNonConfig(t *testing.T) {
func TestForwardOpaquePayload(t *testing.T) {
cf := NewFilter(&mockconfigtx.Manager{})
result, _ := cf.Apply(&cb.Envelope{
Payload: []byte("Opaque"),
})
if result != filter.Forward {
t.Fatal("Should have forwarded opaque message")
}
assert.EqualValues(t, filter.Forward, result, "Should have forwarded opaque message")
}

func TestForwardNilHeader(t *testing.T) {
cf := NewFilter(&mockconfigtx.Manager{})
result, _ := cf.Apply(&cb.Envelope{
Payload: utils.MarshalOrPanic(&cb.Payload{
Header: nil,
}),
})
assert.EqualValues(t, filter.Forward, result, "Should have forwarded message with nil header")
}

func TestForwardBadHeader(t *testing.T) {
cf := NewFilter(&mockconfigtx.Manager{})
result, _ := cf.Apply(&cb.Envelope{
Payload: utils.MarshalOrPanic(&cb.Payload{
Header: &cb.Header{ChannelHeader: []byte("Hello, world!")},
}),
})
assert.EqualValues(t, filter.Forward, result, "Should have forwarded message with bad header")
}

func TestForwardNonConfig(t *testing.T) {
cf := NewFilter(&mockconfigtx.Manager{})
result, _ := cf.Apply(&cb.Envelope{
Payload: utils.MarshalOrPanic(&cb.Payload{
Header: &cb.Header{ChannelHeader: []byte{}},
}),
})
assert.EqualValues(t, filter.Forward, result, "Should have forwarded message with non-config message")
}

func TestRejectMalformedData(t *testing.T) {
cf := NewFilter(&mockconfigtx.Manager{})
result, _ := cf.Apply(&cb.Envelope{
Payload: utils.MarshalOrPanic(&cb.Payload{
Header: &cb.Header{
ChannelHeader: utils.MarshalOrPanic(&cb.ChannelHeader{
Type: int32(cb.HeaderType_CONFIG),
}),
},
Data: []byte("Hello, world!"),
}),
})
assert.EqualValues(t, filter.Reject, result, "Should have rejected message with malformed payload data")
}

func TestAcceptGoodConfig(t *testing.T) {
Expand Down Expand Up @@ -65,19 +108,43 @@ func TestAcceptGoodConfig(t *testing.T) {
Payload: configBytes,
}
result, committer := cf.Apply(configEnvelope)
if result != filter.Accept {
t.Fatal("Should have indicated a good config message causes a reconfig")
}

if !committer.Isolated() {
t.Fatal("Config transactions should be isolated to their own block")
}
assert.EqualValues(t, filter.Accept, result, "Should have indicated a good config message causes a reconfig")
assert.True(t, committer.Isolated(), "Config transactions should be isolated to their own block")

committer.Commit()
assert.Equal(t, mcm.AppliedConfigUpdateEnvelope, configEnv, "Should have applied new config on commit got %+v and %+v", mcm.AppliedConfigUpdateEnvelope, configEnv.LastUpdate)
}

if !reflect.DeepEqual(mcm.AppliedConfigUpdateEnvelope, configEnv) {
t.Fatalf("Should have applied new config on commit got %+v and %+v", mcm.AppliedConfigUpdateEnvelope, configEnv.LastUpdate)
func TestPanicApplyingValidatedConfig(t *testing.T) {
mcm := &mockconfigtx.Manager{ApplyVal: fmt.Errorf("Error applying config tx")}
cf := NewFilter(mcm)
configGroup := cb.NewConfigGroup()
configGroup.Values["Foo"] = &cb.ConfigValue{}
configUpdateEnv := &cb.ConfigUpdateEnvelope{
ConfigUpdate: utils.MarshalOrPanic(configGroup),
}
configEnv := &cb.ConfigEnvelope{
LastUpdate: &cb.Envelope{
Payload: utils.MarshalOrPanic(&cb.Payload{
Header: &cb.Header{
ChannelHeader: utils.MarshalOrPanic(&cb.ChannelHeader{
Type: int32(cb.HeaderType_CONFIG_UPDATE),
}),
},
Data: utils.MarshalOrPanic(configUpdateEnv),
}),
},
}
configEnvBytes := utils.MarshalOrPanic(configEnv)
configBytes := utils.MarshalOrPanic(&cb.Payload{Header: &cb.Header{ChannelHeader: utils.MarshalOrPanic(&cb.ChannelHeader{Type: int32(cb.HeaderType_CONFIG)})}, Data: configEnvBytes})
configEnvelope := &cb.Envelope{
Payload: configBytes,
}
result, committer := cf.Apply(configEnvelope)

assert.EqualValues(t, filter.Accept, result, "Should have indicated a good config message causes a reconfig")
assert.True(t, committer.Isolated(), "Config transactions should be isolated to their own block")
assert.Panics(t, func() { committer.Commit() }, "Should panic upon error applying a validated config tx")
}

func TestRejectBadConfig(t *testing.T) {
Expand All @@ -87,7 +154,6 @@ func TestRejectBadConfig(t *testing.T) {
result, _ := cf.Apply(&cb.Envelope{
Payload: configBytes,
})
if result != filter.Reject {
t.Fatal("Should have rejected bad config message")
}

assert.EqualValues(t, filter.Reject, result, "Should have rejected bad config message")
}
6 changes: 6 additions & 0 deletions orderer/common/filter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"

cb "github.com/hyperledger/fabric/protos/common"
"github.com/stretchr/testify/assert"
)

var RejectRule = Rule(rejectRule{})
Expand All @@ -38,6 +39,11 @@ func (r forwardRule) Apply(message *cb.Envelope) (Action, Committer) {
return Forward, nil
}

func TestNoopCommitter(t *testing.T) {
var nc noopCommitter
assert.False(t, nc.Isolated(), "Should return false")
}

func TestEmptyRejectRule(t *testing.T) {
result, _ := EmptyRejectRule.Apply(&cb.Envelope{})
if result != Reject {
Expand Down

0 comments on commit 38d3879

Please sign in to comment.