~seirdy/moac

moac/pwgen/nocrand_test.go -rw-r--r-- 843 bytes
97035daeRohan Kumar Chore(deps): bump deps 7 months ago
                                                                                
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
package pwgen_test

import (
	"crypto/rand"
	"strings"
	"testing"

	"git.sr.ht/~seirdy/moac/v2/charsets"
	"git.sr.ht/~seirdy/moac/v2/pwgen"
)

func TestGenPwPanics(t *testing.T) {
	shouldPanic(t)
}

func shouldPanic(t *testing.T) {
	t.Helper()

	pwr := pwgen.PwRequirements{
		CharsetsWanted: charsets.ParseCharsets([]string{"ascii", "latin", "🦖؆ص😈"}),
		MinLen:         32,
	}

	csprng := rand.Reader
	rand.Reader = strings.NewReader("")

	defer func() {
		rand.Reader = csprng

		if out := recover(); out != "cannot generate passwords: crypto/rand unavailable: EOF" {
			t.Errorf("panic due to CSPRNG unavailability sent unexpected message: %v", out)
		}
	}()

	_, _ = pwgen.GenPW(pwr) //nolint:errcheck // we're checking for panics; errors are checked elsewhere

	t.Errorf("pwgen should have panicked without access to a CSPRNG")
}