-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto_rand_test.go
132 lines (102 loc) · 3.53 KB
/
crypto_rand_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package random_test
import (
"crypto/rand"
"errors"
"io"
"math"
"math/big"
"testing"
"github.com/agiledragon/gomonkey"
"github.com/smartystreets/goconvey/convey"
"github.com/v8fg/kit4go/random"
)
func TestCryptoInt(t *testing.T) {
convey.Convey("TestCryptoInt", t, func() {
outputs := []gomonkey.OutputCell{
{Values: gomonkey.Params{big.NewInt(1), nil}, Times: 1},
{Values: gomonkey.Params{big.NewInt(math.MaxInt), nil}, Times: 1},
{Values: gomonkey.Params{nil, errors.New("err")}, Times: 1},
}
// rand.Int use crypto/rand
af := gomonkey.ApplyFuncSeq(rand.Int, outputs)
defer af.Reset()
ret, err := random.CryptoInt(2)
convey.So(ret, convey.ShouldResemble, big.NewInt(1))
convey.So(err, convey.ShouldBeNil)
ret, err = random.CryptoInt(math.MaxInt64)
convey.So(ret, convey.ShouldResemble, big.NewInt(math.MaxInt))
convey.So(err, convey.ShouldBeNil)
ret, err = random.CryptoInt(2)
convey.So(ret, convey.ShouldBeNil)
convey.So(err, convey.ShouldBeError)
})
}
func TestCryptoPrime(t *testing.T) {
convey.Convey("TestCryptoPrime", t, func() {
outputs := []gomonkey.OutputCell{
{Values: gomonkey.Params{big.NewInt(1), errors.New("crypto/rand: prime size must be at least 2-bit")}, Times: 1},
{Values: gomonkey.Params{big.NewInt(3), nil}, Times: 1},
{Values: gomonkey.Params{big.NewInt(7), nil}, Times: 1},
}
// rand.Int use crypto/rand
af := gomonkey.ApplyFuncSeq(rand.Prime, outputs)
defer af.Reset()
var ret *big.Int
var err error
ret, err = random.CryptoPrime(1)
convey.So(ret, convey.ShouldResemble, big.NewInt(1))
convey.So(err, convey.ShouldBeError)
ret, err = random.CryptoPrime(2)
convey.So(ret, convey.ShouldResemble, big.NewInt(3))
convey.So(err, convey.ShouldBeNil)
ret, err = random.CryptoPrime(3)
convey.So(ret, convey.ShouldResemble, big.NewInt(7))
convey.So(err, convey.ShouldBeNil)
})
}
func TestCryptoRead(t *testing.T) {
convey.Convey("TestCryptoRead", t, func() {
outputs := []gomonkey.OutputCell{
{Values: gomonkey.Params{0, nil}, Times: 1},
{Values: gomonkey.Params{1, nil}, Times: 1},
{Values: gomonkey.Params{2, nil}, Times: 1},
{Values: gomonkey.Params{4, nil}, Times: 1},
}
// rand.Int use crypto/rand
af := gomonkey.ApplyFuncSeq(rand.Read, outputs)
defer af.Reset()
ret, err := random.CryptoRead(nil)
convey.So(ret, convey.ShouldResemble, 0)
convey.So(err, convey.ShouldBeNil)
ret, err = random.CryptoRead([]byte{})
convey.So(ret, convey.ShouldResemble, 1)
convey.So(err, convey.ShouldBeNil)
ret, err = random.CryptoRead([]byte{1, 2})
convey.So(ret, convey.ShouldResemble, 2)
convey.So(err, convey.ShouldBeNil)
ret, err = random.CryptoRead([]byte{1, 2, 3, 5})
convey.So(ret, convey.ShouldResemble, 4)
convey.So(err, convey.ShouldBeNil)
})
}
func TestCryptoReadString(t *testing.T) {
convey.Convey("TestCryptoReadString", t, func() {
outputs := []gomonkey.OutputCell{
{Values: gomonkey.Params{0, nil}, Times: 1},
{Values: gomonkey.Params{1, nil}, Times: 1},
{Values: gomonkey.Params{2, nil}, Times: 1},
{Values: gomonkey.Params{4, nil}, Times: 1},
}
// rand.Int use crypto/rand
af := gomonkey.ApplyFuncSeq(io.Reader.Read, outputs)
defer af.Reset()
ret := random.CryptoReadString(nil)
convey.So(ret, convey.ShouldEqual, "")
ret = random.CryptoReadString([]byte{1})
convey.So(ret, convey.ShouldNotEqual, "")
ret = random.CryptoReadString([]byte{1, 2})
convey.So(ret, convey.ShouldNotEqual, "")
ret = random.CryptoReadString([]byte{1, 2, 3, 5})
convey.So(ret, convey.ShouldNotEqual, "")
})
}