Source File
rand.go
Belonging Package
testing/cryptotest
// Copyright 2025 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 cryptotest provides deterministic random source testing.package cryptotestimport (cryptorandmathrand// Import unsafe and crypto/rand, which imports crypto/internal/rand,// for the crypto/internal/rand.SetTestingReader go:linkname.__)//go:linkname randSetTestingReader crypto/internal/rand.SetTestingReaderfunc randSetTestingReader( io.Reader)//go:linkname testingCheckParallel testing.checkParallelfunc testingCheckParallel( *testing.T)// SetGlobalRandom sets a global, deterministic cryptographic randomness source// for the duration of test t. It affects crypto/rand, and all implicit sources// of cryptographic randomness in the crypto/... packages.//// SetGlobalRandom may be called multiple times in the same test to reset the// random stream or change the seed.//// Because SetGlobalRandom affects the whole process, it cannot be used in// parallel tests or tests with parallel ancestors.//// Note that the way cryptographic algorithms use randomness is generally not// specified and may change over time. Thus, if a test expects a specific output// from a cryptographic function, it may fail in the future even if it uses// SetGlobalRandom.//// SetGlobalRandom is not supported when building against the Go Cryptographic// Module v1.0.0 (i.e. when [crypto/fips140.Version] returns "v1.0.0").func ( *testing.T, uint64) {if == nil {panic("cryptotest: SetGlobalRandom called with a nil *testing.T")}if !testing.Testing() {panic("cryptotest: SetGlobalRandom used in a non-test binary")}testingCheckParallel()var [32]bytebyteorder.LEPutUint64([:8], ):= &lockedReader{r: mathrand.NewChaCha8()}randSetTestingReader():= cryptorand.Readercryptorand.Reader =.Cleanup(func() {cryptorand.Reader =randSetTestingReader(nil)})}type lockedReader struct {sync.Mutexr *mathrand.ChaCha8}func ( *lockedReader) ( []byte) ( int, error) {.Lock()defer .Unlock()return .r.Read()}
![]() |
The pages are generated with Golds v0.8.3-preview. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |