1
0
mirror of https://github.com/golang/go synced 2024-11-23 12:20:12 -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:
Shawn Smith 2014-01-31 11:43:48 +11:00 committed by Dave Cheney
parent 502958ffa6
commit 99d23dfdfd

View File

@ -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)
}