1// Copyright 2013 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package rand_test
6
7import (
8	"bytes"
9	"crypto/rand"
10	"fmt"
11	"io"
12	"math/big"
13	mathrand "math/rand"
14	"testing"
15	"time"
16)
17
18// https://golang.org/issue/6849.
19func TestPrimeSmall(t *testing.T) {
20	for n := 2; n < 10; n++ {
21		p, err := rand.Prime(rand.Reader, n)
22		if err != nil {
23			t.Fatalf("Can't generate %d-bit prime: %v", n, err)
24		}
25		if p.BitLen() != n {
26			t.Fatalf("%v is not %d-bit", p, n)
27		}
28		if !p.ProbablyPrime(32) {
29			t.Fatalf("%v is not prime", p)
30		}
31	}
32}
33
34// Test that passing bits < 2 causes Prime to return nil, error
35func TestPrimeBitsLt2(t *testing.T) {
36	if p, err := rand.Prime(rand.Reader, 1); p != nil || err == nil {
37		t.Errorf("Prime should return nil, error when called with bits < 2")
38	}
39}
40
41func TestPrimeNondeterministic(t *testing.T) {
42	r := mathrand.New(mathrand.NewSource(42))
43	p0, err := rand.Prime(r, 32)
44	if err != nil {
45		t.Fatal(err)
46	}
47	for i := 0; i < 128; i++ {
48		r.Seed(42)
49		p, err := rand.Prime(r, 32)
50		if err != nil {
51			t.Fatal(err)
52		}
53		if p.Cmp(p0) != 0 {
54			return
55		}
56	}
57	t.Error("Prime always generated the same prime given the same input")
58}
59
60func TestInt(t *testing.T) {
61	// start at 128 so the case of (max.BitLen() % 8) == 0 is covered
62	for n := 128; n < 140; n++ {
63		b := new(big.Int).SetInt64(int64(n))
64		if i, err := rand.Int(rand.Reader, b); err != nil {
65			t.Fatalf("Can't generate random value: %v, %v", i, err)
66		}
67	}
68}
69
70type countingReader struct {
71	r io.Reader
72	n int
73}
74
75func (r *countingReader) Read(p []byte) (n int, err error) {
76	n, err = r.r.Read(p)
77	r.n += n
78	return n, err
79}
80
81// Test that Int reads only the necessary number of bytes from the reader for
82// max at each bit length
83func TestIntReads(t *testing.T) {
84	for i := 0; i < 32; i++ {
85		max := int64(1 << uint64(i))
86		t.Run(fmt.Sprintf("max=%d", max), func(t *testing.T) {
87			reader := &countingReader{r: rand.Reader}
88
89			_, err := rand.Int(reader, big.NewInt(max))
90			if err != nil {
91				t.Fatalf("Can't generate random value: %d, %v", max, err)
92			}
93			expected := (i + 7) / 8
94			if reader.n != expected {
95				t.Errorf("Int(reader, %d) should read %d bytes, but it read: %d", max, expected, reader.n)
96			}
97		})
98	}
99}
100
101// Test that Int does not mask out valid return values
102func TestIntMask(t *testing.T) {
103	for max := 1; max <= 256; max++ {
104		t.Run(fmt.Sprintf("max=%d", max), func(t *testing.T) {
105			for i := 0; i < max; i++ {
106				if testing.Short() && i == 0 {
107					i = max - 1
108				}
109				var b bytes.Buffer
110				b.WriteByte(byte(i))
111				n, err := rand.Int(&b, big.NewInt(int64(max)))
112				if err != nil {
113					t.Fatalf("Can't generate random value: %d, %v", max, err)
114				}
115				if n.Int64() != int64(i) {
116					t.Errorf("Int(reader, %d) should have returned value of %d, but it returned: %v", max, i, n)
117				}
118			}
119		})
120	}
121}
122
123func testIntPanics(t *testing.T, b *big.Int) {
124	defer func() {
125		if err := recover(); err == nil {
126			t.Errorf("Int should panic when called with max <= 0: %v", b)
127		}
128	}()
129	rand.Int(rand.Reader, b)
130}
131
132// Test that passing a new big.Int as max causes Int to panic
133func TestIntEmptyMaxPanics(t *testing.T) {
134	b := new(big.Int)
135	testIntPanics(t, b)
136}
137
138// Test that passing a negative value as max causes Int to panic
139func TestIntNegativeMaxPanics(t *testing.T) {
140	b := new(big.Int).SetInt64(int64(-1))
141	testIntPanics(t, b)
142}
143
144func BenchmarkPrime(b *testing.B) {
145	r := mathrand.New(mathrand.NewSource(time.Now().UnixNano()))
146	for i := 0; i < b.N; i++ {
147		rand.Prime(r, 1024)
148	}
149}
150