~seirdy/moac

6ff0419c9ac9ed0512bbd6fd4ab23fb41a07ed23 — Rohan Kumar 7 months ago 8e2dbd7
Test: add stress test for long pw

Add a test case for generating a super long password whose length is the
output of getLoops squared. Could be useful for detecting crashes, esp.
with CGO+sanitizers enabled.

Since this new case along with the previous empty-charset case aren't
part of the other pairwise pwgen test tables, move them to their own
file.
2 files changed, 42 insertions(+), 17 deletions(-)

M pwgen/genpw_test.go
A pwgen/special_test.go
M pwgen/genpw_test.go => pwgen/genpw_test.go +4 -17
@@ 447,7 447,7 @@ func TestGenPw(t *testing.T) {
			t.Parallel()

			tooLongCount := 0
			runTestCaseGroup(t, group, &tooLongCount, allowedPercentWithOverage, loops)
			runTestCaseGroup(t, group, &tooLongCount, allowedPercentWithOverage)
			log.Print(
				"number of too-long passwords for charset " +
					groupInfo.name +


@@ 463,25 463,12 @@ func TestGenPw(t *testing.T) {
	}
}

func TestGenPwHandlesSingleEmptyCharset(t *testing.T) {
	t.Parallel()

	pwr := pwgen.PwRequirements{
		CharsetsWanted: []charsets.Charset{charsets.CustomCharset(make([]rune, 0))},
		TargetEntropy:  128,
	}

	_, err := pwgen.GenPW(pwr)

	if !errors.Is(err, pwgen.ErrInvalidLenBounds) {
		t.Errorf("expected error %s from GenPW, got %s", pwgen.ErrInvalidLenBounds.Error(), err.Error())
	}
}

func runTestCaseGroup(
	t *testing.T, testCaseGroup []pwgenTestCase, tooLongCount *int, overageAllowed float64, loops int) {
	t *testing.T, testCaseGroup []pwgenTestCase, tooLongCount *int, overageAllowed float64) {
	t.Helper()

	loops := getLoops()

	for i := range testCaseGroup {
		testCase := testCaseGroup[i]
		cs := charsets.ParseCharsets(testCase.charsetsWanted)

A pwgen/special_test.go => pwgen/special_test.go +38 -0
@@ 0,0 1,38 @@
package pwgen_test

import (
	"errors"
	"testing"

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

// TestGenPwHandlesSuperLongPw runs few slow tests, so run it first.
func TestGenPwHandlesSuperLongPw(t *testing.T) {
	t.Parallel()

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

	if _, err := pwgen.GenPW(pwr); err != nil {
		t.Errorf("error in GenPW: %s", err.Error())
	}
}

func TestGenPwHandlesSingleEmptyCharset(t *testing.T) {
	t.Parallel()

	pwr := pwgen.PwRequirements{
		CharsetsWanted: []charsets.Charset{charsets.CustomCharset(make([]rune, 0))},
		TargetEntropy:  128,
	}

	_, err := pwgen.GenPW(pwr)

	if !errors.Is(err, pwgen.ErrInvalidLenBounds) {
		t.Errorf("expected error %s from GenPW, got %s", pwgen.ErrInvalidLenBounds.Error(), err.Error())
	}
}