github.com/yacovm/fabric@v2.0.0-alpha.0.20191128145320-c5d4087dc723+incompatible/common/configtx/compare_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package configtx
     8  
     9  import (
    10  	"testing"
    11  
    12  	cb "github.com/hyperledger/fabric-protos-go/common"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestCompareConfigValue(t *testing.T) {
    17  	// Normal equality
    18  	assert.True(t, comparable{
    19  		ConfigValue: &cb.ConfigValue{
    20  			Version:   0,
    21  			ModPolicy: "foo",
    22  			Value:     []byte("bar"),
    23  		}}.equals(comparable{
    24  		ConfigValue: &cb.ConfigValue{
    25  			Version:   0,
    26  			ModPolicy: "foo",
    27  			Value:     []byte("bar"),
    28  		}}), "Should have found identical config values to be identical")
    29  
    30  	// Different Mod Policy
    31  	assert.False(t, comparable{
    32  		ConfigValue: &cb.ConfigValue{
    33  			Version:   0,
    34  			ModPolicy: "foo",
    35  			Value:     []byte("bar"),
    36  		}}.equals(comparable{
    37  		ConfigValue: &cb.ConfigValue{
    38  			Version:   0,
    39  			ModPolicy: "bar",
    40  			Value:     []byte("bar"),
    41  		}}), "Should have detected different mod policy")
    42  
    43  	// Different Value
    44  	assert.False(t, comparable{
    45  		ConfigValue: &cb.ConfigValue{
    46  			Version:   0,
    47  			ModPolicy: "foo",
    48  			Value:     []byte("bar"),
    49  		}}.equals(comparable{
    50  		ConfigValue: &cb.ConfigValue{
    51  			Version:   0,
    52  			ModPolicy: "foo",
    53  			Value:     []byte("foo"),
    54  		}}), "Should have detected different value")
    55  
    56  	// Different Version
    57  	assert.False(t, comparable{
    58  		ConfigValue: &cb.ConfigValue{
    59  			Version:   0,
    60  			ModPolicy: "foo",
    61  			Value:     []byte("bar"),
    62  		}}.equals(comparable{
    63  		ConfigValue: &cb.ConfigValue{
    64  			Version:   1,
    65  			ModPolicy: "foo",
    66  			Value:     []byte("bar"),
    67  		}}), "Should have detected different version")
    68  
    69  	// One nil value
    70  	assert.False(t, comparable{
    71  		ConfigValue: &cb.ConfigValue{
    72  			Version:   0,
    73  			ModPolicy: "foo",
    74  			Value:     []byte("bar"),
    75  		}}.equals(comparable{}), "Should have detected nil other value")
    76  
    77  }
    78  
    79  func TestCompareConfigPolicy(t *testing.T) {
    80  	// Normal equality
    81  	assert.True(t, comparable{
    82  		ConfigPolicy: &cb.ConfigPolicy{
    83  			Version:   0,
    84  			ModPolicy: "foo",
    85  			Policy: &cb.Policy{
    86  				Type:  1,
    87  				Value: []byte("foo"),
    88  			},
    89  		}}.equals(comparable{
    90  		ConfigPolicy: &cb.ConfigPolicy{
    91  			Version:   0,
    92  			ModPolicy: "foo",
    93  			Policy: &cb.Policy{
    94  				Type:  1,
    95  				Value: []byte("foo"),
    96  			},
    97  		}}), "Should have found identical config policies to be identical")
    98  
    99  	// Different mod policy
   100  	assert.False(t, comparable{
   101  		ConfigPolicy: &cb.ConfigPolicy{
   102  			Version:   0,
   103  			ModPolicy: "foo",
   104  			Policy: &cb.Policy{
   105  				Type:  1,
   106  				Value: []byte("foo"),
   107  			},
   108  		}}.equals(comparable{
   109  		ConfigPolicy: &cb.ConfigPolicy{
   110  			Version:   0,
   111  			ModPolicy: "bar",
   112  			Policy: &cb.Policy{
   113  				Type:  1,
   114  				Value: []byte("foo"),
   115  			},
   116  		}}), "Should have detected different mod policy")
   117  
   118  	// Different version
   119  	assert.False(t, comparable{
   120  		ConfigPolicy: &cb.ConfigPolicy{
   121  			Version:   0,
   122  			ModPolicy: "foo",
   123  			Policy: &cb.Policy{
   124  				Type:  1,
   125  				Value: []byte("foo"),
   126  			},
   127  		}}.equals(comparable{
   128  		ConfigPolicy: &cb.ConfigPolicy{
   129  			Version:   1,
   130  			ModPolicy: "foo",
   131  			Policy: &cb.Policy{
   132  				Type:  1,
   133  				Value: []byte("foo"),
   134  			},
   135  		}}), "Should have detected different version")
   136  
   137  	// Different policy type
   138  	assert.False(t, comparable{
   139  		ConfigPolicy: &cb.ConfigPolicy{
   140  			Version:   0,
   141  			ModPolicy: "foo",
   142  			Policy: &cb.Policy{
   143  				Type:  1,
   144  				Value: []byte("foo"),
   145  			},
   146  		}}.equals(comparable{
   147  		ConfigPolicy: &cb.ConfigPolicy{
   148  			Version:   0,
   149  			ModPolicy: "foo",
   150  			Policy: &cb.Policy{
   151  				Type:  2,
   152  				Value: []byte("foo"),
   153  			},
   154  		}}), "Should have detected different policy type")
   155  
   156  	// Different policy value
   157  	assert.False(t, comparable{
   158  		ConfigPolicy: &cb.ConfigPolicy{
   159  			Version:   0,
   160  			ModPolicy: "foo",
   161  			Policy: &cb.Policy{
   162  				Type:  1,
   163  				Value: []byte("foo"),
   164  			},
   165  		}}.equals(comparable{
   166  		ConfigPolicy: &cb.ConfigPolicy{
   167  			Version:   0,
   168  			ModPolicy: "foo",
   169  			Policy: &cb.Policy{
   170  				Type:  1,
   171  				Value: []byte("bar"),
   172  			},
   173  		}}), "Should have detected different policy value")
   174  
   175  	// One nil value
   176  	assert.False(t, comparable{
   177  		ConfigPolicy: &cb.ConfigPolicy{
   178  			Version:   0,
   179  			ModPolicy: "foo",
   180  			Policy: &cb.Policy{
   181  				Type:  1,
   182  				Value: []byte("foo"),
   183  			},
   184  		}}.equals(comparable{}), "Should have detected one nil value")
   185  
   186  	// One nil policy
   187  	assert.False(t, comparable{
   188  		ConfigPolicy: &cb.ConfigPolicy{
   189  			Version:   0,
   190  			ModPolicy: "foo",
   191  			Policy: &cb.Policy{
   192  				Type:  1,
   193  				Value: []byte("foo"),
   194  			},
   195  		}}.equals(comparable{
   196  		ConfigPolicy: &cb.ConfigPolicy{
   197  			Version:   0,
   198  			ModPolicy: "foo",
   199  			Policy: &cb.Policy{
   200  				Type: 1,
   201  			},
   202  		}}), "Should have detected one nil policy")
   203  }
   204  
   205  func TestCompareConfigGroup(t *testing.T) {
   206  	// Normal equality
   207  	assert.True(t, comparable{
   208  		ConfigGroup: &cb.ConfigGroup{
   209  			Version:   0,
   210  			ModPolicy: "foo",
   211  			Groups:    map[string]*cb.ConfigGroup{"Foo1": nil, "Bar1": nil},
   212  			Values:    map[string]*cb.ConfigValue{"Foo2": nil, "Bar2": nil},
   213  			Policies:  map[string]*cb.ConfigPolicy{"Foo3": nil, "Bar3": nil},
   214  		}}.equals(comparable{
   215  		ConfigGroup: &cb.ConfigGroup{
   216  			Version:   0,
   217  			ModPolicy: "foo",
   218  			Groups:    map[string]*cb.ConfigGroup{"Foo1": nil, "Bar1": nil},
   219  			Values:    map[string]*cb.ConfigValue{"Foo2": nil, "Bar2": nil},
   220  			Policies:  map[string]*cb.ConfigPolicy{"Foo3": nil, "Bar3": nil},
   221  		}}), "Should have found identical config groups to be identical")
   222  
   223  	// Different mod policy
   224  	assert.False(t, comparable{
   225  		ConfigGroup: &cb.ConfigGroup{
   226  			Version:   0,
   227  			ModPolicy: "foo",
   228  		}}.equals(comparable{
   229  		ConfigGroup: &cb.ConfigGroup{
   230  			Version:   0,
   231  			ModPolicy: "bar",
   232  		}}), "Should have detected different mod policy")
   233  
   234  	// Different version
   235  	assert.False(t, comparable{
   236  		ConfigGroup: &cb.ConfigGroup{
   237  			Version:   0,
   238  			ModPolicy: "foo",
   239  		}}.equals(comparable{
   240  		ConfigGroup: &cb.ConfigGroup{
   241  			Version:   1,
   242  			ModPolicy: "foo",
   243  		}}), "Should have detected different version")
   244  
   245  	// Different groups
   246  	assert.False(t, comparable{
   247  		ConfigGroup: &cb.ConfigGroup{
   248  			Version:   0,
   249  			ModPolicy: "foo",
   250  			Groups:    map[string]*cb.ConfigGroup{"Foo1": nil},
   251  			Values:    map[string]*cb.ConfigValue{"Foo2": nil, "Bar2": nil},
   252  			Policies:  map[string]*cb.ConfigPolicy{"Foo3": nil, "Bar3": nil},
   253  		}}.equals(comparable{
   254  		ConfigGroup: &cb.ConfigGroup{
   255  			Version:   0,
   256  			ModPolicy: "foo",
   257  			Groups:    map[string]*cb.ConfigGroup{"Foo1": nil, "Bar1": nil},
   258  			Values:    map[string]*cb.ConfigValue{"Foo2": nil, "Bar2": nil},
   259  			Policies:  map[string]*cb.ConfigPolicy{"Foo3": nil, "Bar3": nil},
   260  		}}), "Should have detected different groups entries")
   261  
   262  	// Different values
   263  	assert.False(t, comparable{
   264  		ConfigGroup: &cb.ConfigGroup{
   265  			Version:   0,
   266  			ModPolicy: "foo",
   267  			Groups:    map[string]*cb.ConfigGroup{"Foo1": nil, "Bar1": nil},
   268  			Values:    map[string]*cb.ConfigValue{"Foo2": nil, "Bar2": nil},
   269  			Policies:  map[string]*cb.ConfigPolicy{"Foo3": nil, "Bar3": nil},
   270  		}}.equals(comparable{
   271  		ConfigGroup: &cb.ConfigGroup{
   272  			Version:   0,
   273  			ModPolicy: "foo",
   274  			Groups:    map[string]*cb.ConfigGroup{"Foo1": nil, "Bar1": nil},
   275  			Values:    map[string]*cb.ConfigValue{"Foo2": nil},
   276  			Policies:  map[string]*cb.ConfigPolicy{"Foo3": nil, "Bar3": nil},
   277  		}}), "Should have detected fifferent values entries")
   278  
   279  	// Different policies
   280  	assert.False(t, comparable{
   281  		ConfigGroup: &cb.ConfigGroup{
   282  			Version:   0,
   283  			ModPolicy: "foo",
   284  			Groups:    map[string]*cb.ConfigGroup{"Foo1": nil, "Bar1": nil},
   285  			Values:    map[string]*cb.ConfigValue{"Foo2": nil, "Bar2": nil},
   286  			Policies:  map[string]*cb.ConfigPolicy{"Foo3": nil, "Bar3": nil},
   287  		}}.equals(comparable{
   288  		ConfigGroup: &cb.ConfigGroup{
   289  			Version:   0,
   290  			ModPolicy: "foo",
   291  			Groups:    map[string]*cb.ConfigGroup{"Foo1": nil, "Bar1": nil},
   292  			Values:    map[string]*cb.ConfigValue{"Foo2": nil, "Bar2": nil},
   293  			Policies:  map[string]*cb.ConfigPolicy{"Foo3": nil, "Bar4": nil},
   294  		}}), "Should have detected fifferent policies entries")
   295  }