Source file src/crypto/rand/util_test.go

     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  
     5  package rand_test
     6  
     7  import (
     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.
    19  func 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
    35  func 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  
    41  func TestInt(t *testing.T) {
    42  	// start at 128 so the case of (max.BitLen() % 8) == 0 is covered
    43  	for n := 128; n < 140; n++ {
    44  		b := new(big.Int).SetInt64(int64(n))
    45  		if i, err := rand.Int(rand.Reader, b); err != nil {
    46  			t.Fatalf("Can't generate random value: %v, %v", i, err)
    47  		}
    48  	}
    49  }
    50  
    51  type countingReader struct {
    52  	r io.Reader
    53  	n int
    54  }
    55  
    56  func (r *countingReader) Read(p []byte) (n int, err error) {
    57  	n, err = r.r.Read(p)
    58  	r.n += n
    59  	return n, err
    60  }
    61  
    62  // Test that Int reads only the necessary number of bytes from the reader for
    63  // max at each bit length
    64  func TestIntReads(t *testing.T) {
    65  	for i := 0; i < 32; i++ {
    66  		max := int64(1 << uint64(i))
    67  		t.Run(fmt.Sprintf("max=%d", max), func(t *testing.T) {
    68  			reader := &countingReader{r: rand.Reader}
    69  
    70  			_, err := rand.Int(reader, big.NewInt(max))
    71  			if err != nil {
    72  				t.Fatalf("Can't generate random value: %d, %v", max, err)
    73  			}
    74  			expected := (i + 7) / 8
    75  			if reader.n != expected {
    76  				t.Errorf("Int(reader, %d) should read %d bytes, but it read: %d", max, expected, reader.n)
    77  			}
    78  		})
    79  	}
    80  }
    81  
    82  // Test that Int does not mask out valid return values
    83  func TestIntMask(t *testing.T) {
    84  	for max := 1; max <= 256; max++ {
    85  		t.Run(fmt.Sprintf("max=%d", max), func(t *testing.T) {
    86  			for i := 0; i < max; i++ {
    87  				if testing.Short() && i == 0 {
    88  					i = max - 1
    89  				}
    90  				var b bytes.Buffer
    91  				b.WriteByte(byte(i))
    92  				n, err := rand.Int(&b, big.NewInt(int64(max)))
    93  				if err != nil {
    94  					t.Fatalf("Can't generate random value: %d, %v", max, err)
    95  				}
    96  				if n.Int64() != int64(i) {
    97  					t.Errorf("Int(reader, %d) should have returned value of %d, but it returned: %v", max, i, n)
    98  				}
    99  			}
   100  		})
   101  	}
   102  }
   103  
   104  func testIntPanics(t *testing.T, b *big.Int) {
   105  	defer func() {
   106  		if err := recover(); err == nil {
   107  			t.Errorf("Int should panic when called with max <= 0: %v", b)
   108  		}
   109  	}()
   110  	rand.Int(rand.Reader, b)
   111  }
   112  
   113  // Test that passing a new big.Int as max causes Int to panic
   114  func TestIntEmptyMaxPanics(t *testing.T) {
   115  	b := new(big.Int)
   116  	testIntPanics(t, b)
   117  }
   118  
   119  // Test that passing a negative value as max causes Int to panic
   120  func TestIntNegativeMaxPanics(t *testing.T) {
   121  	b := new(big.Int).SetInt64(int64(-1))
   122  	testIntPanics(t, b)
   123  }
   124  
   125  func BenchmarkPrime(b *testing.B) {
   126  	r := mathrand.New(mathrand.NewSource(time.Now().UnixNano()))
   127  	for i := 0; i < b.N; i++ {
   128  		rand.Prime(r, 1024)
   129  	}
   130  }
   131  

View as plain text