updated random generator tests

This commit is contained in:
Gani Georgiev 2025-08-22 21:20:30 +03:00
parent 819ec1ad5c
commit a2b1b19342
3 changed files with 26 additions and 22 deletions

View File

@ -4,6 +4,7 @@ package core
import "errors" import "errors"
// https://github.com/pocketbase/pocketbase/pull/7116
func execve(argv0 string, argv []string, envv []string) error { func execve(argv0 string, argv []string, envv []string) error {
return errors.ErrUnsupported return errors.ErrUnsupported
} }

View File

@ -23,13 +23,13 @@ func TestRandomStringByRegex(t *testing.T) {
{`\d+`, []syntax.Flags{syntax.POSIX}, true}, {`\d+`, []syntax.Flags{syntax.POSIX}, true},
{`\d+`, nil, false}, {`\d+`, nil, false},
{`\d*`, nil, false}, {`\d*`, nil, false},
{`\d{1,10}`, nil, false}, {`\d{1,20}`, nil, false},
{`\d{3}`, nil, false}, {`\d{5}`, nil, false},
{`\d{0,}-abc`, nil, false}, {`\d{0,}-abc`, nil, false},
{`[a-zA-Z_]*`, nil, false}, {`[a-zA-Z_]*`, nil, false},
{`[^a-zA-Z]{5,30}`, nil, false}, {`[^a-zA-Z]{5,30}`, nil, false},
{`\w+_abc`, nil, false}, {`\w+_abc`, nil, false},
{`[2-9]{5}-\w+`, nil, false}, {`[2-9]{10}-\w+`, nil, false},
{`(a|b|c)`, nil, false}, {`(a|b|c)`, nil, false},
} }

View File

@ -1,6 +1,7 @@
package security_test package security_test
import ( import (
"fmt"
"regexp" "regexp"
"testing" "testing"
@ -36,29 +37,31 @@ func testRandomStringWithAlphabet(t *testing.T, randomFunc func(n int, alphabet
} }
for i, s := range scenarios { for i, s := range scenarios {
generated := make([]string, 0, 1000) t.Run(fmt.Sprintf("%d_%q", i, s.alphabet), func(t *testing.T) {
length := 10 generated := make([]string, 0, 1000)
length := 10
for j := 0; j < 1000; j++ { for j := 0; j < 1000; j++ {
result := randomFunc(length, s.alphabet) result := randomFunc(length, s.alphabet)
if len(result) != length { if len(result) != length {
t.Fatalf("(%d:%d) Expected the length of the string to be %d, got %d", i, j, length, len(result)) t.Fatalf("(%d) Expected the length of the string to be %d, got %d", j, length, len(result))
}
reg := regexp.MustCompile(s.expectPattern)
if match := reg.MatchString(result); !match {
t.Fatalf("(%d:%d) The generated string should have only %s characters, got %q", i, j, s.expectPattern, result)
}
for _, str := range generated {
if str == result {
t.Fatalf("(%d:%d) Repeating random string - found %q in %q", i, j, result, generated)
} }
}
generated = append(generated, result) reg := regexp.MustCompile(s.expectPattern)
} if match := reg.MatchString(result); !match {
t.Fatalf("(%d) The generated string should have only %s characters, got %q", j, s.expectPattern, result)
}
for _, str := range generated {
if str == result {
t.Fatalf("(%d) Repeating random string - found %q in %q", j, result, generated)
}
}
generated = append(generated, result)
}
})
} }
} }