Commit 99d23dfd authored by Shawn Smith's avatar Shawn Smith Committed by Dave Cheney

crypto/rand: add tests for Int, Prime

LGTM=rsc, dave
R=golang-codereviews, dave, josharian, rsc
CC=golang-codereviews
https://golang.org/cl/46490043
parent 502958ff
......@@ -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)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment