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