github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/common/configvalues/channel/config_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 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 channel
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	cb "github.com/hyperledger/fabric/protos/common"
    24  
    25  	logging "github.com/op/go-logging"
    26  )
    27  
    28  func init() {
    29  	logging.SetLevel(logging.DEBUG, "")
    30  }
    31  
    32  func groupToKeyValue(configGroup *cb.ConfigGroup) (string, *cb.ConfigValue) {
    33  	for key, value := range configGroup.Values {
    34  		return key, value
    35  	}
    36  	panic("No value encoded")
    37  }
    38  
    39  func makeInvalidConfigValue() *cb.ConfigValue {
    40  	return &cb.ConfigValue{
    41  		Value: []byte("Garbage Data"),
    42  	}
    43  }
    44  
    45  func TestInterface(t *testing.T) {
    46  	_ = ConfigReader(NewConfig(nil, nil))
    47  }
    48  
    49  func TestDoubleBegin(t *testing.T) {
    50  	defer func() {
    51  		if err := recover(); err == nil {
    52  			t.Fatalf("Should have panicked on multiple begin configs")
    53  		}
    54  	}()
    55  
    56  	m := NewConfig(nil, nil)
    57  	m.BeginValueProposals(nil)
    58  	m.BeginValueProposals(nil)
    59  }
    60  
    61  func TestCommitWithoutBegin(t *testing.T) {
    62  	defer func() {
    63  		if err := recover(); err == nil {
    64  			t.Fatalf("Should have panicked on multiple begin configs")
    65  		}
    66  	}()
    67  
    68  	m := NewConfig(nil, nil)
    69  	m.CommitProposals()
    70  }
    71  
    72  func TestRollback(t *testing.T) {
    73  	m := NewConfig(nil, nil)
    74  	m.pending = &values{}
    75  	m.RollbackProposals()
    76  	if m.pending != nil {
    77  		t.Fatalf("Should have cleared pending config on rollback")
    78  	}
    79  }
    80  
    81  func TestHashingAlgorithm(t *testing.T) {
    82  	invalidMessage := makeInvalidConfigValue()
    83  	invalidAlgorithm := TemplateHashingAlgorithm("MD5")
    84  	validAlgorithm := DefaultHashingAlgorithm()
    85  
    86  	m := NewConfig(nil, nil)
    87  	m.BeginValueProposals(nil)
    88  
    89  	err := m.ProposeValue(HashingAlgorithmKey, invalidMessage)
    90  	if err == nil {
    91  		t.Fatalf("Should have failed on invalid message")
    92  	}
    93  
    94  	err = m.ProposeValue(groupToKeyValue(invalidAlgorithm))
    95  	if err == nil {
    96  		t.Fatalf("Should have failed on invalid algorithm")
    97  	}
    98  
    99  	err = m.ProposeValue(groupToKeyValue(validAlgorithm))
   100  	if err != nil {
   101  		t.Fatalf("Error applying valid config: %s", err)
   102  	}
   103  
   104  	m.CommitProposals()
   105  
   106  	if m.HashingAlgorithm() == nil {
   107  		t.Fatalf("Should have set default hashing algorithm")
   108  	}
   109  }
   110  
   111  func TestBlockDataHashingStructure(t *testing.T) {
   112  	invalidMessage := makeInvalidConfigValue()
   113  	invalidWidth := TemplateBlockDataHashingStructure(0)
   114  	validWidth := DefaultBlockDataHashingStructure()
   115  
   116  	m := NewConfig(nil, nil)
   117  	m.BeginValueProposals(nil)
   118  
   119  	err := m.ProposeValue(BlockDataHashingStructureKey, invalidMessage)
   120  	if err == nil {
   121  		t.Fatalf("Should have failed on invalid message")
   122  	}
   123  
   124  	err = m.ProposeValue(groupToKeyValue(invalidWidth))
   125  	if err == nil {
   126  		t.Fatalf("Should have failed on invalid width")
   127  	}
   128  
   129  	err = m.ProposeValue(groupToKeyValue(validWidth))
   130  	if err != nil {
   131  		t.Fatalf("Error applying valid config: %s", err)
   132  	}
   133  
   134  	m.CommitProposals()
   135  
   136  	if newWidth := m.BlockDataHashingStructureWidth(); newWidth != defaultBlockDataHashingStructureWidth {
   137  		t.Fatalf("Unexpected width, got %d expected %d", newWidth, defaultBlockDataHashingStructureWidth)
   138  	}
   139  }
   140  
   141  func TestOrdererAddresses(t *testing.T) {
   142  	invalidMessage := makeInvalidConfigValue()
   143  	validMessage := DefaultOrdererAddresses()
   144  	m := NewConfig(nil, nil)
   145  	m.BeginValueProposals(nil)
   146  
   147  	err := m.ProposeValue(OrdererAddressesKey, invalidMessage)
   148  	if err == nil {
   149  		t.Fatalf("Should have failed on invalid message")
   150  	}
   151  
   152  	err = m.ProposeValue(groupToKeyValue(validMessage))
   153  	if err != nil {
   154  		t.Fatalf("Error applying valid config: %s", err)
   155  	}
   156  
   157  	m.CommitProposals()
   158  
   159  	if newAddrs := m.OrdererAddresses(); !reflect.DeepEqual(newAddrs, defaultOrdererAddresses) {
   160  		t.Fatalf("Unexpected width, got %s expected %s", newAddrs, defaultOrdererAddresses)
   161  	}
   162  }