...
Run Format

Source file src/crypto/rand/util_test.go

Documentation: crypto/rand

     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  				var b bytes.Buffer
    88  				b.WriteByte(byte(i))
    89  				n, err := rand.Int(&b, big.NewInt(int64(max)))
    90  				if err != nil {
    91  					t.Fatalf("Can't generate random value: %d, %v", max, err)
    92  				}
    93  				if n.Int64() != int64(i) {
    94  					t.Errorf("Int(reader, %d) should have returned value of %d, but it returned: %v", max, i, n)
    95  				}
    96  			}
    97  		})
    98  	}
    99  }
   100  
   101  func testIntPanics(t *testing.T, b *big.Int) {
   102  	defer func() {
   103  		if err := recover(); err == nil {
   104  			t.Errorf("Int should panic when called with max <= 0: %v", b)
   105  		}
   106  	}()
   107  	rand.Int(rand.Reader, b)
   108  }
   109  
   110  // Test that passing a new big.Int as max causes Int to panic
   111  func TestIntEmptyMaxPanics(t *testing.T) {
   112  	b := new(big.Int)
   113  	testIntPanics(t, b)
   114  }
   115  
   116  // Test that passing a negative value as max causes Int to panic
   117  func TestIntNegativeMaxPanics(t *testing.T) {
   118  	b := new(big.Int).SetInt64(int64(-1))
   119  	testIntPanics(t, b)
   120  }
   121  
   122  func BenchmarkPrime(b *testing.B) {
   123  	r := mathrand.New(mathrand.NewSource(time.Now().UnixNano()))
   124  	for i := 0; i < b.N; i++ {
   125  		rand.Prime(r, 1024)
   126  	}
   127  }
   128  

View as plain text