-
Notifications
You must be signed in to change notification settings - Fork 17.7k
/
rand_test.go
216 lines (191 loc) · 4.64 KB
/
rand_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package rand
import (
"bytes"
"compress/flate"
"crypto/internal/cryptotest"
"errors"
"internal/testenv"
"io"
"os"
"sync"
"testing"
)
// These tests are mostly duplicates of the tests in crypto/internal/sysrand,
// and testing both the Reader and Read is pretty redundant when one calls the
// other, but better safe than sorry.
func testReadAndReader(t *testing.T, f func(*testing.T, func([]byte) (int, error))) {
t.Run("Read", func(t *testing.T) {
f(t, Read)
})
t.Run("Reader.Read", func(t *testing.T) {
f(t, Reader.Read)
})
}
func TestRead(t *testing.T) {
testReadAndReader(t, testRead)
}
func testRead(t *testing.T, Read func([]byte) (int, error)) {
var n int = 4e6
if testing.Short() {
n = 1e5
}
b := make([]byte, n)
n, err := Read(b)
if n != len(b) || err != nil {
t.Fatalf("Read(buf) = %d, %s", n, err)
}
var z bytes.Buffer
f, _ := flate.NewWriter(&z, 5)
f.Write(b)
f.Close()
if z.Len() < len(b)*99/100 {
t.Fatalf("Compressed %d -> %d", len(b), z.Len())
}
}
func TestReadByteValues(t *testing.T) {
testReadAndReader(t, testReadByteValues)
}
func testReadByteValues(t *testing.T, Read func([]byte) (int, error)) {
b := make([]byte, 1)
v := make(map[byte]bool)
for {
n, err := Read(b)
if n != 1 || err != nil {
t.Fatalf("Read(b) = %d, %v", n, err)
}
v[b[0]] = true
if len(v) == 256 {
break
}
}
}
func TestLargeRead(t *testing.T) {
testReadAndReader(t, testLargeRead)
}
func testLargeRead(t *testing.T, Read func([]byte) (int, error)) {
// 40MiB, more than the documented maximum of 32Mi-1 on Linux 32-bit.
b := make([]byte, 40<<20)
if n, err := Read(b); err != nil {
t.Fatal(err)
} else if n != len(b) {
t.Fatalf("Read(b) = %d, want %d", n, len(b))
}
}
func TestReadEmpty(t *testing.T) {
testReadAndReader(t, testReadEmpty)
}
func testReadEmpty(t *testing.T, Read func([]byte) (int, error)) {
n, err := Read(make([]byte, 0))
if n != 0 || err != nil {
t.Fatalf("Read(make([]byte, 0)) = %d, %v", n, err)
}
n, err = Read(nil)
if n != 0 || err != nil {
t.Fatalf("Read(nil) = %d, %v", n, err)
}
}
type readerFunc func([]byte) (int, error)
func (f readerFunc) Read(b []byte) (int, error) {
return f(b)
}
func TestReadUsesReader(t *testing.T) {
var called bool
defer func(r io.Reader) { Reader = r }(Reader)
Reader = readerFunc(func(b []byte) (int, error) {
called = true
return len(b), nil
})
n, err := Read(make([]byte, 32))
if n != 32 || err != nil {
t.Fatalf("Read(make([]byte, 32)) = %d, %v", n, err)
}
if !called {
t.Error("Read did not use Reader")
}
}
func TestConcurrentRead(t *testing.T) {
testReadAndReader(t, testConcurrentRead)
}
func testConcurrentRead(t *testing.T, Read func([]byte) (int, error)) {
if testing.Short() {
t.Skip("skipping in short mode")
}
const N = 100
const M = 1000
var wg sync.WaitGroup
wg.Add(N)
for i := 0; i < N; i++ {
go func() {
defer wg.Done()
for i := 0; i < M; i++ {
b := make([]byte, 32)
n, err := Read(b)
if n != 32 || err != nil {
t.Errorf("Read = %d, %v", n, err)
}
}
}()
}
wg.Wait()
}
var sink byte
func TestAllocations(t *testing.T) {
cryptotest.SkipTestAllocations(t)
n := int(testing.AllocsPerRun(10, func() {
buf := make([]byte, 32)
Read(buf)
sink ^= buf[0]
}))
if n > 0 {
t.Errorf("allocs = %d, want 0", n)
}
}
func TestReadError(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode")
}
testenv.MustHaveExec(t)
// We run this test in a subprocess because it's expected to crash.
if os.Getenv("GO_TEST_READ_ERROR") == "1" {
defer func(r io.Reader) { Reader = r }(Reader)
Reader = readerFunc(func([]byte) (int, error) {
return 0, errors.New("error")
})
Read(make([]byte, 32))
t.Error("Read did not crash")
return
}
cmd := testenv.Command(t, os.Args[0], "-test.run=TestReadError")
cmd.Env = append(os.Environ(), "GO_TEST_READ_ERROR=1")
out, err := cmd.CombinedOutput()
if err == nil {
t.Error("subprocess succeeded unexpectedly")
}
exp := "fatal error: crypto/rand: failed to read random data"
if !bytes.Contains(out, []byte(exp)) {
t.Errorf("subprocess output does not contain %q: %s", exp, out)
}
}
func BenchmarkRead(b *testing.B) {
b.Run("4", func(b *testing.B) {
benchmarkRead(b, 4)
})
b.Run("32", func(b *testing.B) {
benchmarkRead(b, 32)
})
b.Run("4K", func(b *testing.B) {
benchmarkRead(b, 4<<10)
})
}
func benchmarkRead(b *testing.B, size int) {
b.SetBytes(int64(size))
buf := make([]byte, size)
for i := 0; i < b.N; i++ {
if _, err := Read(buf); err != nil {
b.Fatal(err)
}
}
}