|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2016 All Rights Reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package broadcast |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "fmt" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "google.golang.org/grpc" |
| 25 | + |
| 26 | + "github.com/golang/protobuf/proto" |
| 27 | + "github.com/hyperledger/fabric/orderer/common/broadcastfilter" |
| 28 | + "github.com/hyperledger/fabric/orderer/common/configtx" |
| 29 | + cb "github.com/hyperledger/fabric/protos/common" |
| 30 | + ab "github.com/hyperledger/fabric/protos/orderer" |
| 31 | +) |
| 32 | + |
| 33 | +var configTx []byte |
| 34 | + |
| 35 | +func init() { |
| 36 | + var err error |
| 37 | + configTx, err = proto.Marshal(&cb.ConfigurationEnvelope{}) |
| 38 | + if err != nil { |
| 39 | + panic("Error marshaling empty config tx") |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +type mockConfigManager struct { |
| 44 | + validated bool |
| 45 | + applied bool |
| 46 | + validateErr error |
| 47 | + applyErr error |
| 48 | +} |
| 49 | + |
| 50 | +func (mcm *mockConfigManager) Validate(configtx *cb.ConfigurationEnvelope) error { |
| 51 | + mcm.validated = true |
| 52 | + return mcm.validateErr |
| 53 | +} |
| 54 | + |
| 55 | +func (mcm *mockConfigManager) Apply(message *cb.ConfigurationEnvelope) error { |
| 56 | + mcm.applied = true |
| 57 | + return mcm.applyErr |
| 58 | +} |
| 59 | + |
| 60 | +type mockConfigFilter struct { |
| 61 | + manager configtx.Manager |
| 62 | +} |
| 63 | + |
| 64 | +func (mcf *mockConfigFilter) Apply(msg *cb.Envelope) broadcastfilter.Action { |
| 65 | + if bytes.Equal(msg.Payload, configTx) { |
| 66 | + if mcf.manager == nil || mcf.manager.Validate(nil) != nil { |
| 67 | + return broadcastfilter.Reject |
| 68 | + } |
| 69 | + return broadcastfilter.Reconfigure |
| 70 | + } |
| 71 | + return broadcastfilter.Forward |
| 72 | +} |
| 73 | + |
| 74 | +type mockTarget struct { |
| 75 | + queue chan *cb.Envelope |
| 76 | + done bool |
| 77 | +} |
| 78 | + |
| 79 | +func (mt *mockTarget) Enqueue(env *cb.Envelope) bool { |
| 80 | + mt.queue <- env |
| 81 | + return !mt.done |
| 82 | +} |
| 83 | + |
| 84 | +func (mt *mockTarget) halt() { |
| 85 | + mt.done = true |
| 86 | + select { |
| 87 | + case <-mt.queue: |
| 88 | + default: |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +type mockB struct { |
| 93 | + grpc.ServerStream |
| 94 | + recvChan chan *cb.Envelope |
| 95 | + sendChan chan *ab.BroadcastResponse |
| 96 | +} |
| 97 | + |
| 98 | +func newMockB() *mockB { |
| 99 | + return &mockB{ |
| 100 | + recvChan: make(chan *cb.Envelope), |
| 101 | + sendChan: make(chan *ab.BroadcastResponse), |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func (m *mockB) Send(br *ab.BroadcastResponse) error { |
| 106 | + m.sendChan <- br |
| 107 | + return nil |
| 108 | +} |
| 109 | + |
| 110 | +func (m *mockB) Recv() (*cb.Envelope, error) { |
| 111 | + msg, ok := <-m.recvChan |
| 112 | + if !ok { |
| 113 | + return msg, fmt.Errorf("Channel closed") |
| 114 | + } |
| 115 | + return msg, nil |
| 116 | +} |
| 117 | + |
| 118 | +func getFiltersConfigMockTarget() (*broadcastfilter.RuleSet, *mockConfigManager, *mockTarget) { |
| 119 | + cm := &mockConfigManager{} |
| 120 | + filters := broadcastfilter.NewRuleSet([]broadcastfilter.Rule{ |
| 121 | + broadcastfilter.EmptyRejectRule, |
| 122 | + &mockConfigFilter{cm}, |
| 123 | + broadcastfilter.AcceptRule, |
| 124 | + }) |
| 125 | + mt := &mockTarget{queue: make(chan *cb.Envelope)} |
| 126 | + return filters, cm, mt |
| 127 | + |
| 128 | +} |
| 129 | + |
| 130 | +func TestQueueOverflow(t *testing.T) { |
| 131 | + filters, cm, mt := getFiltersConfigMockTarget() |
| 132 | + defer mt.halt() |
| 133 | + bh := NewHandlerImpl(2, mt, filters, cm) |
| 134 | + m := newMockB() |
| 135 | + defer close(m.recvChan) |
| 136 | + b := newBroadcaster(bh.(*handlerImpl)) |
| 137 | + go b.queueEnvelopes(m) |
| 138 | + |
| 139 | + for i := 0; i < 2; i++ { |
| 140 | + m.recvChan <- &cb.Envelope{Payload: []byte("Some bytes")} |
| 141 | + reply := <-m.sendChan |
| 142 | + if reply.Status != cb.Status_SUCCESS { |
| 143 | + t.Fatalf("Should have successfully queued the message") |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + m.recvChan <- &cb.Envelope{Payload: []byte("Some bytes")} |
| 148 | + reply := <-m.sendChan |
| 149 | + if reply.Status != cb.Status_SERVICE_UNAVAILABLE { |
| 150 | + t.Fatalf("Should not have successfully queued the message") |
| 151 | + } |
| 152 | + |
| 153 | +} |
| 154 | + |
| 155 | +func TestMultiQueueOverflow(t *testing.T) { |
| 156 | + filters, cm, mt := getFiltersConfigMockTarget() |
| 157 | + defer mt.halt() |
| 158 | + bh := NewHandlerImpl(2, mt, filters, cm) |
| 159 | + ms := []*mockB{newMockB(), newMockB(), newMockB()} |
| 160 | + |
| 161 | + for _, m := range ms { |
| 162 | + defer close(m.recvChan) |
| 163 | + b := newBroadcaster(bh.(*handlerImpl)) |
| 164 | + go b.queueEnvelopes(m) |
| 165 | + } |
| 166 | + |
| 167 | + for _, m := range ms { |
| 168 | + for i := 0; i < 2; i++ { |
| 169 | + m.recvChan <- &cb.Envelope{Payload: []byte("Some bytes")} |
| 170 | + reply := <-m.sendChan |
| 171 | + if reply.Status != cb.Status_SUCCESS { |
| 172 | + t.Fatalf("Should have successfully queued the message") |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + for _, m := range ms { |
| 178 | + m.recvChan <- &cb.Envelope{Payload: []byte("Some bytes")} |
| 179 | + reply := <-m.sendChan |
| 180 | + if reply.Status != cb.Status_SERVICE_UNAVAILABLE { |
| 181 | + t.Fatalf("Should not have successfully queued the message") |
| 182 | + } |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +func TestEmptyEnvelope(t *testing.T) { |
| 187 | + filters, cm, mt := getFiltersConfigMockTarget() |
| 188 | + defer mt.halt() |
| 189 | + bh := NewHandlerImpl(2, mt, filters, cm) |
| 190 | + m := newMockB() |
| 191 | + defer close(m.recvChan) |
| 192 | + go bh.Handle(m) |
| 193 | + |
| 194 | + m.recvChan <- &cb.Envelope{} |
| 195 | + reply := <-m.sendChan |
| 196 | + if reply.Status != cb.Status_BAD_REQUEST { |
| 197 | + t.Fatalf("Should have rejected the null message") |
| 198 | + } |
| 199 | + |
| 200 | +} |
| 201 | + |
| 202 | +func TestReconfigureAccept(t *testing.T) { |
| 203 | + filters, cm, mt := getFiltersConfigMockTarget() |
| 204 | + defer mt.halt() |
| 205 | + bh := NewHandlerImpl(2, mt, filters, cm) |
| 206 | + m := newMockB() |
| 207 | + defer close(m.recvChan) |
| 208 | + go bh.Handle(m) |
| 209 | + |
| 210 | + m.recvChan <- &cb.Envelope{Payload: configTx} |
| 211 | + |
| 212 | + reply := <-m.sendChan |
| 213 | + if reply.Status != cb.Status_SUCCESS { |
| 214 | + t.Fatalf("Should have successfully queued the message") |
| 215 | + } |
| 216 | + |
| 217 | + if !cm.validated { |
| 218 | + t.Errorf("ConfigTx should have been validated before processing") |
| 219 | + } |
| 220 | +} |
| 221 | + |
| 222 | +func TestReconfigureReject(t *testing.T) { |
| 223 | + filters, cm, mt := getFiltersConfigMockTarget() |
| 224 | + cm.validateErr = fmt.Errorf("Fail to validate") |
| 225 | + defer mt.halt() |
| 226 | + bh := NewHandlerImpl(2, mt, filters, cm) |
| 227 | + m := newMockB() |
| 228 | + defer close(m.recvChan) |
| 229 | + go bh.Handle(m) |
| 230 | + |
| 231 | + m.recvChan <- &cb.Envelope{Payload: configTx} |
| 232 | + |
| 233 | + reply := <-m.sendChan |
| 234 | + if reply.Status != cb.Status_BAD_REQUEST { |
| 235 | + t.Fatalf("Should have failed to queue the message because it was invalid config") |
| 236 | + } |
| 237 | +} |
0 commit comments