1
|
|
|
package random_test |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"errors" |
5
|
|
|
"strings" |
6
|
|
|
"testing" |
7
|
|
|
|
8
|
|
|
"github.com/memnix/memnix-rest/pkg/random" |
9
|
|
|
) |
10
|
|
|
|
11
|
|
|
func TestGenerator_GenerateSecretCode(t *testing.T) { |
12
|
|
|
generator := random.GetRandomGeneratorInstance() |
13
|
|
|
|
14
|
|
|
testCases := []struct { |
15
|
|
|
expectedErr error |
16
|
|
|
name string |
17
|
|
|
n int |
18
|
|
|
expected int |
19
|
|
|
hasErr bool |
20
|
|
|
}{ |
21
|
|
|
{ |
22
|
|
|
name: "GenerateSecretCodeWithLength10", |
23
|
|
|
n: 10, |
24
|
|
|
expected: 10, |
25
|
|
|
expectedErr: nil, |
26
|
|
|
hasErr: false, |
27
|
|
|
}, |
28
|
|
|
{ |
29
|
|
|
name: "GenerateSecretCodeWithLength20", |
30
|
|
|
n: 20, |
31
|
|
|
expected: 20, |
32
|
|
|
expectedErr: nil, |
33
|
|
|
hasErr: false, |
34
|
|
|
}, |
35
|
|
|
{ |
36
|
|
|
name: "GenerateSecretCodeWithLength0", |
37
|
|
|
n: 0, |
38
|
|
|
expected: 0, |
39
|
|
|
expectedErr: nil, |
40
|
|
|
hasErr: false, |
41
|
|
|
}, |
42
|
|
|
{ |
43
|
|
|
name: "GenerateSecretCodeWithLengthNegative", |
44
|
|
|
n: -1, |
45
|
|
|
expected: 0, |
46
|
|
|
expectedErr: errors.New("invalid length"), |
47
|
|
|
hasErr: true, |
48
|
|
|
}, |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
for _, tc := range testCases { |
52
|
|
|
t.Run(tc.name, func(t *testing.T) { |
53
|
|
|
code, err := generator.GenerateSecretCode(tc.n) |
54
|
|
|
if tc.hasErr && err == nil { |
55
|
|
|
t.Fatalf("Expected error, but got nil") |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if !tc.hasErr && err != nil { |
59
|
|
|
t.Fatalf("Unexpected error: %v", err) |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if tc.hasErr && err != nil && err.Error() != tc.expectedErr.Error() { |
63
|
|
|
t.Fatalf("Expected error to be %v, but got %v", tc.expectedErr, err) |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if len(code) != tc.expected { |
67
|
|
|
t.Errorf("Expected code length to be %d, but got %d", tc.expected, len(code)) |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if !isValidCode(code) { |
71
|
|
|
t.Errorf("Generated code is not valid: %s", code) |
72
|
|
|
} |
73
|
|
|
}) |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
func isValidCode(code string) bool { |
78
|
|
|
// Define the valid characters for the secret code |
79
|
|
|
validChars := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" |
80
|
|
|
|
81
|
|
|
// Check if each character in the code is a valid character |
82
|
|
|
for _, c := range code { |
83
|
|
|
if !strings.ContainsRune(validChars, c) { |
84
|
|
|
return false |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return true |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
func FuzzGenerateSecretCode(f *testing.F) { |
92
|
|
|
generator := random.GetRandomGeneratorInstance() |
93
|
|
|
f.Add(uint(1000)) |
94
|
|
|
f.Fuzz(func(t *testing.T, size uint) { |
95
|
|
|
code, err := generator.GenerateSecretCode(int(size)) |
96
|
|
|
if err != nil { |
97
|
|
|
t.Fatalf("Unexpected error: %v", err) |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if len(code) != int(size) { |
101
|
|
|
t.Errorf("Expected code length to be %d, but got %d", size, len(code)) |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if !isValidCode(code) { |
105
|
|
|
t.Errorf("Generated code is not valid: %s", code) |
106
|
|
|
} |
107
|
|
|
}) |
108
|
|
|
} |
109
|
|
|
|