github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/common/configtx/util_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 configtx 18 19 import ( 20 "math/rand" 21 "testing" 22 ) 23 24 // TestValidchainID checks that the constraints on chain IDs are enforced properly 25 func TestValidChainID(t *testing.T) { 26 acceptMsg := "Should have accepted valid chain ID" 27 rejectMsg := "Should have rejected invalid chain ID" 28 29 t.Run("ZeroLength", func(t *testing.T) { 30 if err := validateChainID(""); err == nil { 31 t.Fatal(rejectMsg) 32 } 33 }) 34 35 t.Run("LongerThanMaxAllowed", func(t *testing.T) { 36 if err := validateChainID(randomAlphaString(maxLength + 1)); err == nil { 37 t.Fatal(rejectMsg) 38 } 39 }) 40 41 t.Run("HasIllegalName", func(t *testing.T) { 42 for illegalName := range illegalNames { 43 if err := validateChainID(illegalName); err == nil { 44 t.Fatal(rejectMsg) 45 } 46 } 47 }) 48 49 t.Run("ContainsIllegalCharacter", func(t *testing.T) { 50 if err := validateChainID("foo_bar"); err == nil { 51 t.Fatal(rejectMsg) 52 } 53 }) 54 55 t.Run("ValidName", func(t *testing.T) { 56 if err := validateChainID("foo.bar"); err != nil { 57 t.Fatal(acceptMsg) 58 } 59 }) 60 } 61 62 // Helper functions 63 64 func randomAlphaString(size int) string { 65 letters := []rune("abcdefghijklmnopqrstuvwxyz") 66 output := make([]rune, size) 67 for i := range output { 68 output[i] = letters[rand.Intn(len(letters))] 69 } 70 return string(output) 71 }