github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/orderer/common/filter/filter_test.go (about)

     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 filter
    18  
    19  import (
    20  	"testing"
    21  
    22  	cb "github.com/hyperledger/fabric/protos/common"
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  var RejectRule = Rule(rejectRule{})
    27  
    28  type rejectRule struct{}
    29  
    30  func (r rejectRule) Apply(message *cb.Envelope) (Action, Committer) {
    31  	return Reject, nil
    32  }
    33  
    34  var ForwardRule = Rule(forwardRule{})
    35  
    36  type forwardRule struct{}
    37  
    38  func (r forwardRule) Apply(message *cb.Envelope) (Action, Committer) {
    39  	return Forward, nil
    40  }
    41  
    42  func TestNoopCommitter(t *testing.T) {
    43  	var nc noopCommitter
    44  	assert.False(t, nc.Isolated(), "Should return false")
    45  }
    46  
    47  func TestEmptyRejectRule(t *testing.T) {
    48  	result, _ := EmptyRejectRule.Apply(&cb.Envelope{})
    49  	if result != Reject {
    50  		t.Fatalf("Should have rejected")
    51  	}
    52  	result, _ = EmptyRejectRule.Apply(&cb.Envelope{Payload: []byte("fakedata")})
    53  	if result != Forward {
    54  		t.Fatalf("Should have forwarded")
    55  	}
    56  }
    57  
    58  func TestAcceptReject(t *testing.T) {
    59  	rs := NewRuleSet([]Rule{AcceptRule, RejectRule})
    60  	_, err := rs.Apply(&cb.Envelope{})
    61  	if err != nil {
    62  		t.Fatalf("Should have accepted: %s", err)
    63  	}
    64  }
    65  
    66  func TestRejectAccept(t *testing.T) {
    67  	rs := NewRuleSet([]Rule{RejectRule, AcceptRule})
    68  	_, err := rs.Apply(&cb.Envelope{})
    69  	if err == nil {
    70  		t.Fatalf("Should have rejected")
    71  	}
    72  }
    73  
    74  func TestForwardAccept(t *testing.T) {
    75  	rs := NewRuleSet([]Rule{ForwardRule, AcceptRule})
    76  	_, err := rs.Apply(&cb.Envelope{})
    77  	if err != nil {
    78  		t.Fatalf("Should have accepted: %s ", err)
    79  	}
    80  }
    81  
    82  func TestForward(t *testing.T) {
    83  	rs := NewRuleSet([]Rule{ForwardRule})
    84  	_, err := rs.Apply(&cb.Envelope{})
    85  	if err == nil {
    86  		t.Fatalf("Should have rejected")
    87  	}
    88  }
    89  
    90  func TestNoRule(t *testing.T) {
    91  	rs := NewRuleSet([]Rule{})
    92  	_, err := rs.Apply(&cb.Envelope{})
    93  	if err == nil {
    94  		t.Fatalf("Should have rejected")
    95  	}
    96  }