github.com/lzy4123/fabric@v2.1.1+incompatible/gossip/util/misc_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package util 8 9 import ( 10 "crypto/rand" 11 "errors" 12 "testing" 13 "time" 14 15 "github.com/spf13/viper" 16 "github.com/stretchr/testify/assert" 17 "github.com/stretchr/testify/mock" 18 ) 19 20 func testHappyPath(t *testing.T) { 21 n1 := RandomInt(10000) 22 n2 := RandomInt(10000) 23 assert.NotEqual(t, n1, n2) 24 n3 := RandomUInt64() 25 n4 := RandomUInt64() 26 assert.NotEqual(t, n3, n4) 27 } 28 29 func TestContains(t *testing.T) { 30 assert.True(t, Contains("foo", []string{"bar", "foo", "baz"})) 31 assert.False(t, Contains("foo", []string{"bar", "baz"})) 32 } 33 34 func TestGetRandomInt(t *testing.T) { 35 testHappyPath(t) 36 } 37 38 func TestNonNegativeValues(t *testing.T) { 39 assert.True(t, RandomInt(1000000) >= 0) 40 } 41 42 func TestGetRandomIntBadInput(t *testing.T) { 43 f1 := func() { 44 RandomInt(0) 45 } 46 f2 := func() { 47 RandomInt(-500) 48 } 49 assert.Panics(t, f1) 50 assert.Panics(t, f2) 51 } 52 53 type reader struct { 54 mock.Mock 55 } 56 57 func (r *reader) Read(p []byte) (int, error) { 58 args := r.Mock.Called(p) 59 n := args.Get(0).(int) 60 err := args.Get(1) 61 if err == nil { 62 return n, nil 63 } 64 return n, err.(error) 65 } 66 67 func TestGetRandomIntNoEntropy(t *testing.T) { 68 rr := rand.Reader 69 defer func() { 70 rand.Reader = rr 71 }() 72 r := &reader{} 73 r.On("Read", mock.Anything).Return(0, errors.New("Not enough entropy")) 74 rand.Reader = r 75 // Make sure randomness still works even when we have no entropy 76 testHappyPath(t) 77 } 78 79 func TestRandomIndices(t *testing.T) { 80 // not enough choices as needed 81 assert.Nil(t, GetRandomIndices(10, 5)) 82 // exact number of choices as available 83 assert.Len(t, GetRandomIndices(10, 9), 10) 84 // more choices available than needed 85 assert.Len(t, GetRandomIndices(10, 90), 10) 86 } 87 88 func TestGetIntOrDefault(t *testing.T) { 89 viper.Set("N", 100) 90 n := GetIntOrDefault("N", 100) 91 assert.Equal(t, 100, n) 92 m := GetIntOrDefault("M", 101) 93 assert.Equal(t, 101, m) 94 } 95 96 func TestGetDurationOrDefault(t *testing.T) { 97 viper.Set("foo", time.Second) 98 foo := GetDurationOrDefault("foo", time.Second*2) 99 assert.Equal(t, time.Second, foo) 100 bar := GetDurationOrDefault("bar", time.Second*2) 101 assert.Equal(t, time.Second*2, bar) 102 } 103 104 func TestPrintStackTrace(t *testing.T) { 105 PrintStackTrace() 106 } 107 108 func TestGetLogger(t *testing.T) { 109 l1 := GetLogger("foo", "bar") 110 l2 := GetLogger("foo", "bar") 111 assert.Equal(t, l1, l2) 112 } 113 114 func TestSet(t *testing.T) { 115 s := NewSet() 116 assert.Len(t, s.ToArray(), 0) 117 assert.Equal(t, s.Size(), 0) 118 assert.False(t, s.Exists(42)) 119 s.Add(42) 120 assert.True(t, s.Exists(42)) 121 assert.Len(t, s.ToArray(), 1) 122 assert.Equal(t, s.Size(), 1) 123 s.Remove(42) 124 assert.False(t, s.Exists(42)) 125 s.Add(42) 126 assert.True(t, s.Exists(42)) 127 s.Clear() 128 assert.False(t, s.Exists(42)) 129 } 130 131 func TestStringsToBytesToStrings(t *testing.T) { 132 strings := []string{"foo", "bar"} 133 assert.Equal(t, strings, BytesToStrings(StringsToBytes(strings))) 134 }