|
5 | 5 | package mail |
6 | 6 |
|
7 | 7 | import ( |
| 8 | + "crypto/rand" |
| 9 | + "errors" |
8 | 10 | "strings" |
9 | 11 | "testing" |
10 | 12 | ) |
11 | 13 |
|
12 | 14 | // TestRandomStringSecure tests the randomStringSecure method |
13 | 15 | func TestRandomStringSecure(t *testing.T) { |
14 | | - tt := []struct { |
15 | | - testName string |
16 | | - length int |
17 | | - mustNotMatch string |
18 | | - }{ |
19 | | - {"20 chars", 20, "'"}, |
20 | | - {"100 chars", 100, "'"}, |
21 | | - {"1000 chars", 1000, "'"}, |
22 | | - } |
| 16 | + t.Run("randomStringSecure with varying length", func(t *testing.T) { |
| 17 | + tt := []struct { |
| 18 | + testName string |
| 19 | + length int |
| 20 | + mustNotMatch string |
| 21 | + }{ |
| 22 | + {"20 chars", 20, "'"}, |
| 23 | + {"100 chars", 100, "'"}, |
| 24 | + {"1000 chars", 1000, "'"}, |
| 25 | + } |
23 | 26 |
|
24 | | - for _, tc := range tt { |
25 | | - t.Run(tc.testName, func(t *testing.T) { |
26 | | - rs, err := randomStringSecure(tc.length) |
27 | | - if err != nil { |
28 | | - t.Errorf("random string generation failed: %s", err) |
29 | | - } |
30 | | - if strings.Contains(rs, tc.mustNotMatch) { |
31 | | - t.Errorf("random string contains unexpected character. got: %s, not-expected: %s", |
32 | | - rs, tc.mustNotMatch) |
33 | | - } |
34 | | - if len(rs) != tc.length { |
35 | | - t.Errorf("random string length does not match. expected: %d, got: %d", tc.length, len(rs)) |
36 | | - } |
37 | | - }) |
38 | | - } |
| 27 | + for _, tc := range tt { |
| 28 | + t.Run(tc.testName, func(t *testing.T) { |
| 29 | + rs, err := randomStringSecure(tc.length) |
| 30 | + if err != nil { |
| 31 | + t.Errorf("random string generation failed: %s", err) |
| 32 | + } |
| 33 | + if strings.Contains(rs, tc.mustNotMatch) { |
| 34 | + t.Errorf("random string contains unexpected character. got: %s, not-expected: %s", |
| 35 | + rs, tc.mustNotMatch) |
| 36 | + } |
| 37 | + if len(rs) != tc.length { |
| 38 | + t.Errorf("random string length does not match. expected: %d, got: %d", tc.length, len(rs)) |
| 39 | + } |
| 40 | + }) |
| 41 | + } |
| 42 | + }) |
| 43 | + t.Run("randomStringSecure fails on broken rand Reader (first read)", func(t *testing.T) { |
| 44 | + defaultRandReader := rand.Reader |
| 45 | + t.Cleanup(func() { rand.Reader = defaultRandReader }) |
| 46 | + rand.Reader = &randReader{failon: 1} |
| 47 | + if _, err := randomStringSecure(22); err == nil { |
| 48 | + t.Fatalf("expected failure on broken rand Reader") |
| 49 | + } |
| 50 | + }) |
| 51 | + t.Run("randomStringSecure fails on broken rand Reader (second read)", func(t *testing.T) { |
| 52 | + defaultRandReader := rand.Reader |
| 53 | + t.Cleanup(func() { rand.Reader = defaultRandReader }) |
| 54 | + rand.Reader = &randReader{failon: 0} |
| 55 | + if _, err := randomStringSecure(22); err == nil { |
| 56 | + t.Fatalf("expected failure on broken rand Reader") |
| 57 | + } |
| 58 | + }) |
39 | 59 | } |
40 | 60 |
|
41 | 61 | func BenchmarkGenerator_RandomStringSecure(b *testing.B) { |
42 | 62 | b.ReportAllocs() |
43 | 63 | for i := 0; i < b.N; i++ { |
44 | | - _, err := randomStringSecure(22) |
| 64 | + _, err := randomStringSecure(10) |
45 | 65 | if err != nil { |
46 | 66 | b.Errorf("RandomStringFromCharRange() failed: %s", err) |
47 | 67 | } |
48 | 68 | } |
49 | 69 | } |
| 70 | + |
| 71 | +// randReader is type that satisfies the io.Reader interface. It can fail on a specific read |
| 72 | +// operations and is therefore useful to test consecutive reads with errors |
| 73 | +type randReader struct { |
| 74 | + failon uint8 |
| 75 | + call uint8 |
| 76 | +} |
| 77 | + |
| 78 | +// Read implements the io.Reader interface for the randReader type |
| 79 | +func (r *randReader) Read(p []byte) (int, error) { |
| 80 | + if r.call == r.failon { |
| 81 | + r.call++ |
| 82 | + return len(p), nil |
| 83 | + } |
| 84 | + return 0, errors.New("broken reader") |
| 85 | +} |
0 commit comments