mirror of
https://github.com/golang/go
synced 2024-11-12 08:50:22 -07:00
crypto/rand: add tests for Int, Prime
LGTM=rsc, dave R=golang-codereviews, dave, josharian, rsc CC=golang-codereviews https://golang.org/cl/46490043
This commit is contained in:
parent
502958ffa6
commit
99d23dfdfd
@ -6,6 +6,7 @@ package rand_test
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"math/big"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@ -24,3 +25,41 @@ func TestPrimeSmall(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Test that passing bits < 2 causes Prime to return nil, error
|
||||
func TestPrimeBitsLt2(t *testing.T) {
|
||||
if p, err := rand.Prime(rand.Reader, 1); p != nil || err == nil {
|
||||
t.Errorf("Prime should return nil, error when called with bits < 2")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInt(t *testing.T) {
|
||||
// start at 128 so the case of (max.BitLen() % 8) == 0 is covered
|
||||
for n := 128; n < 140; n++ {
|
||||
b := new(big.Int).SetInt64(int64(n))
|
||||
if i, err := rand.Int(rand.Reader, b); err != nil {
|
||||
t.Fatalf("Can't generate random value: %v, %v", i, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testIntPanics(t *testing.T, b *big.Int) {
|
||||
defer func() {
|
||||
if err := recover(); err == nil {
|
||||
t.Errorf("Int should panic when called with max <= 0: %v", b)
|
||||
}
|
||||
}()
|
||||
rand.Int(rand.Reader, b)
|
||||
}
|
||||
|
||||
// Test that passing a new big.Int as max causes Int to panic
|
||||
func TestIntEmptyMaxPanics(t *testing.T) {
|
||||
b := new(big.Int)
|
||||
testIntPanics(t, b)
|
||||
}
|
||||
|
||||
// Test that passing a negative value as max causes Int to panic
|
||||
func TestIntNegativeMaxPanics(t *testing.T) {
|
||||
b := new(big.Int).SetInt64(int64(-1))
|
||||
testIntPanics(t, b)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user