github.com/yacovm/fabric@v2.0.0-alpha.0.20191128145320-c5d4087dc723+incompatible/common/configtx/util_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  	"math/rand"
    11  	"testing"
    12  
    13  	cb "github.com/hyperledger/fabric-protos-go/common"
    14  	"github.com/hyperledger/fabric/protoutil"
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  // TestValidConfigID checks that the constraints on channel IDs are enforced properly
    19  func TestValidConfigID(t *testing.T) {
    20  	acceptMsg := "Should have accepted valid config ID"
    21  	rejectMsg := "Should have rejected invalid config ID"
    22  
    23  	t.Run("ZeroLength", func(t *testing.T) {
    24  		if err := validateConfigID(""); err == nil {
    25  			t.Fatal(rejectMsg)
    26  		}
    27  	})
    28  
    29  	t.Run("LongerThanMaxAllowed", func(t *testing.T) {
    30  		if err := validateConfigID(randomAlphaString(MaxLength + 1)); err == nil {
    31  			t.Fatal(rejectMsg)
    32  		}
    33  	})
    34  
    35  	t.Run("HasIllegalName", func(t *testing.T) {
    36  		for illegalName := range illegalNames {
    37  			if err := validateConfigID(illegalName); err == nil {
    38  				t.Fatal(rejectMsg)
    39  			}
    40  		}
    41  	})
    42  
    43  	t.Run("ContainsIllegalCharacter", func(t *testing.T) {
    44  		if err := validateConfigID("foo_bar"); err == nil {
    45  			t.Fatal(rejectMsg)
    46  		}
    47  	})
    48  
    49  	t.Run("ValidName", func(t *testing.T) {
    50  		if err := validateConfigID("foo.bar"); err != nil {
    51  			t.Fatal(acceptMsg)
    52  		}
    53  	})
    54  }
    55  
    56  // TestValidChannelID checks that the constraints on channel IDs are enforced properly
    57  func TestValidChannelID(t *testing.T) {
    58  	acceptMsg := "Should have accepted valid channel ID"
    59  	rejectMsg := "Should have rejected invalid channel ID"
    60  
    61  	t.Run("ZeroLength", func(t *testing.T) {
    62  		if err := ValidateChannelID(""); err == nil {
    63  			t.Fatal(rejectMsg)
    64  		}
    65  	})
    66  
    67  	t.Run("LongerThanMaxAllowed", func(t *testing.T) {
    68  		if err := ValidateChannelID(randomLowerAlphaString(MaxLength + 1)); err == nil {
    69  			t.Fatal(rejectMsg)
    70  		}
    71  	})
    72  
    73  	t.Run("ContainsIllegalCharacter", func(t *testing.T) {
    74  		if err := ValidateChannelID("foo_bar"); err == nil {
    75  			t.Fatal(rejectMsg)
    76  		}
    77  	})
    78  
    79  	t.Run("StartsWithNumber", func(t *testing.T) {
    80  		if err := ValidateChannelID("8foo"); err == nil {
    81  			t.Fatal(rejectMsg)
    82  		}
    83  	})
    84  
    85  	t.Run("StartsWithDot", func(t *testing.T) {
    86  		if err := ValidateChannelID(".foo"); err == nil {
    87  			t.Fatal(rejectMsg)
    88  		}
    89  	})
    90  
    91  	t.Run("ValidName", func(t *testing.T) {
    92  		if err := ValidateChannelID("f-oo.bar"); err != nil {
    93  			t.Fatal(acceptMsg)
    94  		}
    95  	})
    96  }
    97  
    98  // Helper functions
    99  
   100  func randomLowerAlphaString(size int) string {
   101  	letters := []rune("abcdefghijklmnopqrstuvwxyz")
   102  	output := make([]rune, size)
   103  	for i := range output {
   104  		output[i] = letters[rand.Intn(len(letters))]
   105  	}
   106  	return string(output)
   107  }
   108  
   109  func randomAlphaString(size int) string {
   110  	letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
   111  	output := make([]rune, size)
   112  	for i := range output {
   113  		output[i] = letters[rand.Intn(len(letters))]
   114  	}
   115  	return string(output)
   116  }
   117  
   118  func TestUnmarshalConfig(t *testing.T) {
   119  	goodConfigBytes := protoutil.MarshalOrPanic(&cb.Config{})
   120  	badConfigBytes := []byte("garbage")
   121  
   122  	t.Run("GoodUnmarshalNormal", func(t *testing.T) {
   123  		_, err := UnmarshalConfig(goodConfigBytes)
   124  		assert.NoError(t, err)
   125  	})
   126  
   127  	t.Run("GoodUnmarshalOrpanic", func(t *testing.T) {
   128  		assert.NotPanics(t, func() { UnmarshalConfigOrPanic(goodConfigBytes) })
   129  	})
   130  
   131  	t.Run("BadUnmarshalNormal", func(t *testing.T) {
   132  		_, err := UnmarshalConfig(badConfigBytes)
   133  		assert.Error(t, err)
   134  	})
   135  
   136  	t.Run("BadUnmarshalOrpanic", func(t *testing.T) {
   137  		assert.Panics(t, func() { UnmarshalConfigOrPanic(badConfigBytes) })
   138  	})
   139  }
   140  
   141  func TestUnmarshalConfigEnvelope(t *testing.T) {
   142  	goodConfigEnvelopeBytes := protoutil.MarshalOrPanic(&cb.ConfigEnvelope{})
   143  	badConfigEnvelopeBytes := []byte("garbage")
   144  
   145  	t.Run("GoodUnmarshalNormal", func(t *testing.T) {
   146  		_, err := UnmarshalConfigEnvelope(goodConfigEnvelopeBytes)
   147  		assert.NoError(t, err)
   148  	})
   149  
   150  	t.Run("GoodUnmarshalOrpanic", func(t *testing.T) {
   151  		assert.NotPanics(t, func() { UnmarshalConfigEnvelopeOrPanic(goodConfigEnvelopeBytes) })
   152  	})
   153  
   154  	t.Run("BadUnmarshalNormal", func(t *testing.T) {
   155  		_, err := UnmarshalConfigEnvelope(badConfigEnvelopeBytes)
   156  		assert.Error(t, err)
   157  	})
   158  
   159  	t.Run("BadUnmarshalOrpanic", func(t *testing.T) {
   160  		assert.Panics(t, func() { UnmarshalConfigEnvelopeOrPanic(badConfigEnvelopeBytes) })
   161  	})
   162  }
   163  
   164  func TestUnmarshalConfigUpdate(t *testing.T) {
   165  	goodConfigUpdateBytes := protoutil.MarshalOrPanic(&cb.ConfigUpdate{})
   166  	badConfigUpdateBytes := []byte("garbage")
   167  
   168  	t.Run("GoodUnmarshalNormal", func(t *testing.T) {
   169  		_, err := UnmarshalConfigUpdate(goodConfigUpdateBytes)
   170  		assert.NoError(t, err)
   171  	})
   172  
   173  	t.Run("GoodUnmarshalOrpanic", func(t *testing.T) {
   174  		assert.NotPanics(t, func() { UnmarshalConfigUpdateOrPanic(goodConfigUpdateBytes) })
   175  	})
   176  
   177  	t.Run("BadUnmarshalNormal", func(t *testing.T) {
   178  		_, err := UnmarshalConfigUpdate(badConfigUpdateBytes)
   179  		assert.Error(t, err)
   180  	})
   181  
   182  	t.Run("BadUnmarshalOrpanic", func(t *testing.T) {
   183  		assert.Panics(t, func() { UnmarshalConfigUpdateOrPanic(badConfigUpdateBytes) })
   184  	})
   185  }
   186  
   187  func TestUnmarshalConfigUpdateEnvelope(t *testing.T) {
   188  	goodConfigUpdateEnvelopeBytes := protoutil.MarshalOrPanic(&cb.ConfigUpdateEnvelope{})
   189  	badConfigUpdateEnvelopeBytes := []byte("garbage")
   190  
   191  	t.Run("GoodUnmarshalNormal", func(t *testing.T) {
   192  		_, err := UnmarshalConfigUpdateEnvelope(goodConfigUpdateEnvelopeBytes)
   193  		assert.NoError(t, err)
   194  	})
   195  
   196  	t.Run("GoodUnmarshalOrpanic", func(t *testing.T) {
   197  		assert.NotPanics(t, func() { UnmarshalConfigUpdateEnvelopeOrPanic(goodConfigUpdateEnvelopeBytes) })
   198  	})
   199  
   200  	t.Run("BadUnmarshalNormal", func(t *testing.T) {
   201  		_, err := UnmarshalConfigUpdateEnvelope(badConfigUpdateEnvelopeBytes)
   202  		assert.Error(t, err)
   203  	})
   204  
   205  	t.Run("BadUnmarshalOrpanic", func(t *testing.T) {
   206  		assert.Panics(t, func() { UnmarshalConfigUpdateEnvelopeOrPanic(badConfigUpdateEnvelopeBytes) })
   207  	})
   208  }