Skip to content

Commit 7b7783f

Browse files
committed
Add test
1 parent 8bea54e commit 7b7783f

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

internal/gui/crypto.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,14 @@ func closeCryptoUi() {
270270
}
271271

272272
func sanitizeFilename(input string) string {
273-
re := regexp.MustCompile(`[^a-zA-Z0-9._-а-шА-Ш]+`)
273+
re := regexp.MustCompile(`[ $&+,:;=?@#|'<>.^*()%!-/\\]+`)
274274
filename := re.ReplaceAllString(input, "_")
275275

276+
re2 := regexp.MustCompile(`_+`)
277+
filename = re2.ReplaceAllString(filename, "_")
278+
279+
filename = strings.Trim(filename, "_")
280+
276281
if filename == "" {
277282
filename = "cert"
278283
}

internal/gui/crypto_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,58 @@ func Test_ExtractCertShortName(t *testing.T) {
3333
}
3434

3535
}
36+
37+
func Test_SanitizeFilename(t *testing.T) {
38+
testCases := []struct {
39+
input string
40+
expected string
41+
}{
42+
{
43+
input: "",
44+
expected: "cert",
45+
},
46+
{
47+
input: " ",
48+
expected: "cert",
49+
},
50+
{
51+
input: "!@#",
52+
expected: "cert",
53+
},
54+
{
55+
input: "Pera Miloš Perić",
56+
expected: "Pera_Miloš_Perić",
57+
},
58+
{
59+
input: "Пера Милош Перић",
60+
expected: "Пера_Милош_Перић",
61+
},
62+
{
63+
input: "ЖАРКО ЉУБА ПЕРИЋ",
64+
expected: "ЖАРКО_ЉУБА_ПЕРИЋ",
65+
},
66+
{
67+
input: "John Doe !!!#",
68+
expected: "John_Doe",
69+
},
70+
{
71+
input: "John Doe !!!#",
72+
expected: "John_Doe",
73+
},
74+
{
75+
input: "John Doe !!!#",
76+
expected: "John_Doe",
77+
},
78+
{
79+
input: "Аркадий Иванович Свидригайлов",
80+
expected: "Аркадий_Иванович_Свидригайлов",
81+
},
82+
}
83+
84+
for _, testCase := range testCases {
85+
result := sanitizeFilename(testCase.input)
86+
if testCase.expected != result {
87+
t.Errorf("Expected `%s` but got `%s`", testCase.expected, result)
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)