This repository was archived by the owner on Jan 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrandomx_test.go
More file actions
103 lines (89 loc) · 2.56 KB
/
randomx_test.go
File metadata and controls
103 lines (89 loc) · 2.56 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package randomx
import (
"strings"
"testing"
)
func TestProvider_GenerateRandomString(t *testing.T) {
rx := New().(*provider)
tests := []struct {
length int
charSet string
}{
{10, LowercaseLetters + UppercaseLetters + Digits + Symbols},
{5, LowercaseLetters},
{8, Digits},
}
for _, test := range tests {
randomStr := rx.GenerateRandomString(test.length, test.charSet)
if len(randomStr) != test.length {
t.Errorf("Generated string length does not match expected length for charSet %s", test.charSet)
}
for _, char := range randomStr {
if !strings.Contains(test.charSet, string(char)) {
t.Errorf("Generated string contains unexpected character: %s", string(char))
}
}
}
}
func TestProvider_GenerateRandomStringFromBuiltin(t *testing.T) {
rx := New().(*provider)
defaultOptions := GenerateOptions{
UseLowercase: true,
UseUppercase: true,
UseDigits: true,
UseSymbols: true,
}
randomDefault := rx.GenerateRandomStringFromBuiltin(15, defaultOptions)
for _, char := range randomDefault {
if !strings.Contains(LowercaseLetters+UppercaseLetters+Digits+Symbols, string(char)) {
t.Errorf("Generated string contains unexpected character: %s", string(char))
}
}
customOptions := GenerateOptions{
UseLowercase: true,
UseUppercase: true,
UseDigits: false,
UseSymbols: true,
}
randomCustom := rx.GenerateRandomStringFromBuiltin(12, customOptions)
for _, char := range randomCustom {
if !strings.Contains(LowercaseLetters+UppercaseLetters+Symbols, string(char)) {
t.Errorf("Generated string contains unexpected character: %s", string(char))
}
}
}
func TestProvider_GenerateRandomStringFromBuiltin_PredefinedOptions(t *testing.T) {
rx := New().(*provider)
tests := []struct {
length int
options GenerateOptions
}{
{15, OptionAllChars},
{10, OptionLetters},
{8, OptionDigitsOnly},
}
for _, test := range tests {
randomStr := rx.GenerateRandomStringFromBuiltin(test.length, test.options)
expectedCharSet := ""
if test.options.UseLowercase {
expectedCharSet += LowercaseLetters
}
if test.options.UseUppercase {
expectedCharSet += UppercaseLetters
}
if test.options.UseDigits {
expectedCharSet += Digits
}
if test.options.UseSymbols {
expectedCharSet += Symbols
}
if len(randomStr) != test.length {
t.Errorf("Generated string length does not match expected length for options %+v", test.options)
}
for _, char := range randomStr {
if !strings.Contains(expectedCharSet, string(char)) {
t.Errorf("Generated string contains unexpected character: %s", string(char))
}
}
}
}