github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/utils/strutil/random_test.go (about) 1 package strutil 2 3 import ( 4 "math/rand" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestRandomCrypto(t *testing.T) { 11 for i := 0; i < 10; i++ { 12 length := 3 + rand.Intn(1024) 13 pwd := RandomCrypto(AlphaDigits, length) 14 assert.True(t, IsASCII(pwd)) 15 assert.Len(t, pwd, length) 16 } 17 18 for i := 0; i < 10; i++ { 19 length := 3 + rand.Intn(1024) 20 pwd := RandomCrypto(AlphaDigits+PasswordSpecial, length) 21 assert.True(t, IsASCII(pwd)) 22 assert.Len(t, pwd, length) 23 } 24 } 25 26 func TestRandom(t *testing.T) { 27 for i := 0; i < 10; i++ { 28 length := 3 + rand.Intn(1024) 29 letters := Random(Letters, length) 30 assert.True(t, IsPrintable(letters)) 31 assert.Len(t, letters, length) 32 } 33 34 for i := 0; i < 10; i++ { 35 length := 3 + rand.Intn(1024) 36 lower := Random(LowerLetters, length) 37 t.Log("lower:", lower) 38 assert.True(t, IsLower(lower)) 39 assert.Len(t, lower, length) 40 } 41 42 for i := 0; i < 10; i++ { 43 length := 3 + rand.Intn(1024) 44 upper := Random(UpperLetters, length) 45 assert.True(t, IsUpper(upper)) 46 assert.Len(t, upper, length) 47 } 48 49 for i := 0; i < 10; i++ { 50 length := 3 + rand.Intn(1024) 51 digits := Random(Digits, length) 52 assert.NotEqual(t, digits[0], '0') 53 assert.True(t, IsASCIIDigit(digits)) 54 assert.True(t, IsDigit(digits)) 55 assert.Len(t, digits, length) 56 } 57 58 for i := 0; i < 10; i++ { 59 length := 3 + rand.Intn(1024) 60 str := Random(AlphaDigits, length) 61 assert.True(t, IsASCII(str)) 62 assert.True(t, IsPrintable(str)) 63 assert.Len(t, str, length) 64 } 65 } 66 67 func TestRandomHex(t *testing.T) { 68 assert.Panics(t, func() { 69 _ = RandomHex(0) 70 }) 71 72 for i := 1; i < 1024; i += 1 { 73 got := RandomHex(i) 74 assert.Len(t, got, i) 75 } 76 }