|
8 | 8 | "bytes"
|
9 | 9 | "compress/flate"
|
10 | 10 | "crypto/internal/boring"
|
| 11 | + "errors" |
11 | 12 | "internal/race"
|
| 13 | + "internal/testenv" |
12 | 14 | "io"
|
13 | 15 | "os"
|
14 | 16 | "runtime"
|
@@ -189,6 +191,44 @@ func TestNoUrandomFallback(t *testing.T) {
|
189 | 191 | }
|
190 | 192 | }
|
191 | 193 |
|
| 194 | +func TestReadError(t *testing.T) { |
| 195 | + if testing.Short() { |
| 196 | + t.Skip("skipping test in short mode") |
| 197 | + } |
| 198 | + testenv.MustHaveExec(t) |
| 199 | + |
| 200 | + // We run this test in a subprocess because it's expected to crash the |
| 201 | + // program unless the GODEBUG is set. |
| 202 | + if os.Getenv("GO_TEST_READ_ERROR") == "1" { |
| 203 | + defer func(r io.Reader) { Reader = r }(Reader) |
| 204 | + Reader = readerFunc(func([]byte) (int, error) { |
| 205 | + return 0, errors.New("error") |
| 206 | + }) |
| 207 | + if _, err := Read(make([]byte, 32)); err == nil { |
| 208 | + t.Error("Read did not return error") |
| 209 | + } |
| 210 | + return |
| 211 | + } |
| 212 | + |
| 213 | + cmd := testenv.Command(t, os.Args[0], "-test.run=TestReadError") |
| 214 | + cmd.Env = append(os.Environ(), "GO_TEST_READ_ERROR=1") |
| 215 | + out, err := cmd.CombinedOutput() |
| 216 | + if err == nil { |
| 217 | + t.Error("subprocess succeeded unexpectedly") |
| 218 | + } |
| 219 | + exp := "fatal error: crypto/rand: failed to read random data" |
| 220 | + if !bytes.Contains(out, []byte(exp)) { |
| 221 | + t.Errorf("subprocess output does not contain %q: %s", exp, out) |
| 222 | + } |
| 223 | + |
| 224 | + cmd = testenv.Command(t, os.Args[0], "-test.run=TestReadError") |
| 225 | + cmd.Env = append(os.Environ(), "GO_TEST_READ_ERROR=1", "GODEBUG=randcrash=0") |
| 226 | + out, err = cmd.CombinedOutput() |
| 227 | + if err != nil { |
| 228 | + t.Errorf("subprocess failed: %v\n%s", err, out) |
| 229 | + } |
| 230 | +} |
| 231 | + |
192 | 232 | func BenchmarkRead(b *testing.B) {
|
193 | 233 | b.Run("4", func(b *testing.B) {
|
194 | 234 | benchmarkRead(b, 4)
|
|
0 commit comments