@@ -18,7 +18,6 @@ package configtxfilter
18
18
19
19
import (
20
20
"fmt"
21
- "reflect"
22
21
"testing"
23
22
24
23
mockconfigtx "github.com/hyperledger/fabric/common/mocks/configtx"
@@ -27,16 +26,60 @@ import (
27
26
"github.com/hyperledger/fabric/protos/utils"
28
27
29
28
"github.com/golang/protobuf/proto"
29
+ "github.com/stretchr/testify/assert"
30
30
)
31
31
32
- func TestForwardNonConfig (t * testing.T ) {
32
+ func TestForwardOpaquePayload (t * testing.T ) {
33
33
cf := NewFilter (& mockconfigtx.Manager {})
34
34
result , _ := cf .Apply (& cb.Envelope {
35
35
Payload : []byte ("Opaque" ),
36
36
})
37
- if result != filter .Forward {
38
- t .Fatal ("Should have forwarded opaque message" )
39
- }
37
+ assert .EqualValues (t , filter .Forward , result , "Should have forwarded opaque message" )
38
+ }
39
+
40
+ func TestForwardNilHeader (t * testing.T ) {
41
+ cf := NewFilter (& mockconfigtx.Manager {})
42
+ result , _ := cf .Apply (& cb.Envelope {
43
+ Payload : utils .MarshalOrPanic (& cb.Payload {
44
+ Header : nil ,
45
+ }),
46
+ })
47
+ assert .EqualValues (t , filter .Forward , result , "Should have forwarded message with nil header" )
48
+ }
49
+
50
+ func TestForwardBadHeader (t * testing.T ) {
51
+ cf := NewFilter (& mockconfigtx.Manager {})
52
+ result , _ := cf .Apply (& cb.Envelope {
53
+ Payload : utils .MarshalOrPanic (& cb.Payload {
54
+ Header : & cb.Header {ChannelHeader : []byte ("Hello, world!" )},
55
+ }),
56
+ })
57
+ assert .EqualValues (t , filter .Forward , result , "Should have forwarded message with bad header" )
58
+ }
59
+
60
+ func TestForwardNonConfig (t * testing.T ) {
61
+ cf := NewFilter (& mockconfigtx.Manager {})
62
+ result , _ := cf .Apply (& cb.Envelope {
63
+ Payload : utils .MarshalOrPanic (& cb.Payload {
64
+ Header : & cb.Header {ChannelHeader : []byte {}},
65
+ }),
66
+ })
67
+ assert .EqualValues (t , filter .Forward , result , "Should have forwarded message with non-config message" )
68
+ }
69
+
70
+ func TestRejectMalformedData (t * testing.T ) {
71
+ cf := NewFilter (& mockconfigtx.Manager {})
72
+ result , _ := cf .Apply (& cb.Envelope {
73
+ Payload : utils .MarshalOrPanic (& cb.Payload {
74
+ Header : & cb.Header {
75
+ ChannelHeader : utils .MarshalOrPanic (& cb.ChannelHeader {
76
+ Type : int32 (cb .HeaderType_CONFIG ),
77
+ }),
78
+ },
79
+ Data : []byte ("Hello, world!" ),
80
+ }),
81
+ })
82
+ assert .EqualValues (t , filter .Reject , result , "Should have rejected message with malformed payload data" )
40
83
}
41
84
42
85
func TestAcceptGoodConfig (t * testing.T ) {
@@ -65,19 +108,43 @@ func TestAcceptGoodConfig(t *testing.T) {
65
108
Payload : configBytes ,
66
109
}
67
110
result , committer := cf .Apply (configEnvelope )
68
- if result != filter .Accept {
69
- t .Fatal ("Should have indicated a good config message causes a reconfig" )
70
- }
71
-
72
- if ! committer .Isolated () {
73
- t .Fatal ("Config transactions should be isolated to their own block" )
74
- }
111
+ assert .EqualValues (t , filter .Accept , result , "Should have indicated a good config message causes a reconfig" )
112
+ assert .True (t , committer .Isolated (), "Config transactions should be isolated to their own block" )
75
113
76
114
committer .Commit ()
115
+ assert .Equal (t , mcm .AppliedConfigUpdateEnvelope , configEnv , "Should have applied new config on commit got %+v and %+v" , mcm .AppliedConfigUpdateEnvelope , configEnv .LastUpdate )
116
+ }
77
117
78
- if ! reflect .DeepEqual (mcm .AppliedConfigUpdateEnvelope , configEnv ) {
79
- t .Fatalf ("Should have applied new config on commit got %+v and %+v" , mcm .AppliedConfigUpdateEnvelope , configEnv .LastUpdate )
118
+ func TestPanicApplyingValidatedConfig (t * testing.T ) {
119
+ mcm := & mockconfigtx.Manager {ApplyVal : fmt .Errorf ("Error applying config tx" )}
120
+ cf := NewFilter (mcm )
121
+ configGroup := cb .NewConfigGroup ()
122
+ configGroup .Values ["Foo" ] = & cb.ConfigValue {}
123
+ configUpdateEnv := & cb.ConfigUpdateEnvelope {
124
+ ConfigUpdate : utils .MarshalOrPanic (configGroup ),
125
+ }
126
+ configEnv := & cb.ConfigEnvelope {
127
+ LastUpdate : & cb.Envelope {
128
+ Payload : utils .MarshalOrPanic (& cb.Payload {
129
+ Header : & cb.Header {
130
+ ChannelHeader : utils .MarshalOrPanic (& cb.ChannelHeader {
131
+ Type : int32 (cb .HeaderType_CONFIG_UPDATE ),
132
+ }),
133
+ },
134
+ Data : utils .MarshalOrPanic (configUpdateEnv ),
135
+ }),
136
+ },
80
137
}
138
+ configEnvBytes := utils .MarshalOrPanic (configEnv )
139
+ configBytes := utils .MarshalOrPanic (& cb.Payload {Header : & cb.Header {ChannelHeader : utils .MarshalOrPanic (& cb.ChannelHeader {Type : int32 (cb .HeaderType_CONFIG )})}, Data : configEnvBytes })
140
+ configEnvelope := & cb.Envelope {
141
+ Payload : configBytes ,
142
+ }
143
+ result , committer := cf .Apply (configEnvelope )
144
+
145
+ assert .EqualValues (t , filter .Accept , result , "Should have indicated a good config message causes a reconfig" )
146
+ assert .True (t , committer .Isolated (), "Config transactions should be isolated to their own block" )
147
+ assert .Panics (t , func () { committer .Commit () }, "Should panic upon error applying a validated config tx" )
81
148
}
82
149
83
150
func TestRejectBadConfig (t * testing.T ) {
@@ -87,7 +154,6 @@ func TestRejectBadConfig(t *testing.T) {
87
154
result , _ := cf .Apply (& cb.Envelope {
88
155
Payload : configBytes ,
89
156
})
90
- if result != filter .Reject {
91
- t .Fatal ("Should have rejected bad config message" )
92
- }
157
+
158
+ assert .EqualValues (t , filter .Reject , result , "Should have rejected bad config message" )
93
159
}
0 commit comments