-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test/mock: implement mock for crypto [rand.Reader]
The RandReader implement [io.Reader]. To provide predictable result, the RandReader is seeded with slice of bytes. A call to Read will fill the passed bytes with those seed. For example, given seed as "abc" (length is three), calling Read with bytes length five will return "abcab".
- Loading branch information
Showing
3 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2024, Shulhan <[email protected]>. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package mock | ||
|
||
// RandReader implement [io.Reader] for mocking crypto [rand.Reader]. | ||
// To provide predictable result, the RandReader is seeded with the same | ||
// slice of bytes. | ||
// A call to Read will fill the passed bytes with those seed. | ||
type RandReader struct { | ||
seed []byte | ||
counter int | ||
} | ||
|
||
// NewRandReader create new random reader using seed as generator. | ||
// The longer the seed, the longer the random values become unique. | ||
func NewRandReader(seed []byte) (r *RandReader) { | ||
r = &RandReader{ | ||
seed: seed, | ||
} | ||
return r | ||
} | ||
|
||
// Read fill the raw bytes with seed. | ||
// If raw length larger than the seed, it will be filled with the same seed | ||
// until all bytes filled. | ||
// | ||
// For example, given seed as "abc" (length is three), and raw length is | ||
// five, then Read will return "abcab". | ||
func (rr *RandReader) Read(raw []byte) (n int, err error) { | ||
var ( | ||
expn = len(raw) | ||
|
||
nwrite int | ||
) | ||
|
||
for n < expn { | ||
nwrite = copy(raw[n:], rr.seed[rr.counter:]) | ||
n += nwrite | ||
} | ||
|
||
// Increment the counter to make the seed start from next byte, so | ||
// the next Read will return different result but still predictable. | ||
rr.counter++ | ||
if rr.counter == len(rr.seed) { | ||
rr.counter = 0 | ||
} | ||
|
||
return n, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2024, Shulhan <[email protected]>. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package mock_test | ||
|
||
import ( | ||
"crypto/rand" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/shuLhan/share/lib/test/mock" | ||
) | ||
|
||
func ExampleRandReader() { | ||
var ( | ||
seed = []byte(`123`) | ||
rr = mock.NewRandReader(seed) | ||
b = make([]byte, 8) | ||
|
||
x int | ||
n int | ||
err error | ||
) | ||
|
||
rand.Reader = rr | ||
|
||
for x = 0; x <= len(seed); x++ { | ||
n, err = rand.Read(b) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Println(n, string(b)) | ||
} | ||
|
||
// Output: | ||
// 8 12312312 | ||
// 8 23232323 | ||
// 8 33333333 | ||
// 8 12312312 | ||
} |