github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/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 // TestValidConfigID checks that the constraints on chain IDs are enforced properly 25 func TestValidConfigID(t *testing.T) { 26 acceptMsg := "Should have accepted valid config ID" 27 rejectMsg := "Should have rejected invalid config ID" 28 29 t.Run("ZeroLength", func(t *testing.T) { 30 if err := validateConfigID(""); err == nil { 31 t.Fatal(rejectMsg) 32 } 33 }) 34 35 t.Run("LongerThanMaxAllowed", func(t *testing.T) { 36 if err := validateConfigID(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 := validateConfigID(illegalName); err == nil { 44 t.Fatal(rejectMsg) 45 } 46 } 47 }) 48 49 t.Run("ContainsIllegalCharacter", func(t *testing.T) { 50 if err := validateConfigID("foo_bar"); err == nil { 51 t.Fatal(rejectMsg) 52 } 53 }) 54 55 t.Run("ValidName", func(t *testing.T) { 56 if err := validateConfigID("foo.bar"); err != nil { 57 t.Fatal(acceptMsg) 58 } 59 }) 60 } 61 62 // TestValidChannelID checks that the constraints on chain IDs are enforced properly 63 func TestValidChannelID(t *testing.T) { 64 acceptMsg := "Should have accepted valid channel ID" 65 rejectMsg := "Should have rejected invalid channel ID" 66 67 t.Run("ZeroLength", func(t *testing.T) { 68 if err := validateChannelID(""); err == nil { 69 t.Fatal(rejectMsg) 70 } 71 }) 72 73 t.Run("LongerThanMaxAllowed", func(t *testing.T) { 74 if err := validateChannelID(randomLowerAlphaString(maxLength + 1)); err == nil { 75 t.Fatal(rejectMsg) 76 } 77 }) 78 79 t.Run("ContainsIllegalCharacter", func(t *testing.T) { 80 if err := validateChannelID("foo_bar"); err == nil { 81 t.Fatal(rejectMsg) 82 } 83 }) 84 85 t.Run("StartsWithNumber", func(t *testing.T) { 86 if err := validateChannelID("8foo"); err == nil { 87 t.Fatal(rejectMsg) 88 } 89 }) 90 91 t.Run("StartsWithDot", func(t *testing.T) { 92 if err := validateChannelID(".foo"); err == nil { 93 t.Fatal(rejectMsg) 94 } 95 }) 96 97 t.Run("ValidName", func(t *testing.T) { 98 if err := validateChannelID("f-oo.bar"); err != nil { 99 t.Fatal(acceptMsg) 100 } 101 }) 102 } 103 104 // Helper functions 105 106 func randomLowerAlphaString(size int) string { 107 letters := []rune("abcdefghijklmnopqrstuvwxyz") 108 output := make([]rune, size) 109 for i := range output { 110 output[i] = letters[rand.Intn(len(letters))] 111 } 112 return string(output) 113 } 114 115 func randomAlphaString(size int) string { 116 letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") 117 output := make([]rune, size) 118 for i := range output { 119 output[i] = letters[rand.Intn(len(letters))] 120 } 121 return string(output) 122 }