Skip to content

Commit e546686

Browse files
Merge pull request #52 from misha-ssh/update-validate-alias
Update validate alias
2 parents 1c617a6 + 7bcd4a3 commit e546686

9 files changed

Lines changed: 286 additions & 12 deletions

File tree

internal/storage/storage.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
)
99

1010
var ErrEmptyDirectory = errors.New("empty directory")
11+
var ErrDeleteDirectory = errors.New("get dir, delete only file")
1112

1213
// Create creates a new file at the specified path, including parent directories if needed.
1314
// Returns error if file creation fails.
@@ -32,6 +33,16 @@ func Create(path string, filename string) error {
3233

3334
// Delete removes the specified file. Returns error if deletion fails.
3435
func Delete(path string, filename string) error {
36+
file := filepath.Join(path, filename)
37+
info, err := os.Stat(file)
38+
if err != nil {
39+
return err
40+
}
41+
42+
if info.IsDir() {
43+
return ErrDeleteDirectory
44+
}
45+
3546
return os.Remove(filepath.Join(path, filename))
3647
}
3748

pkg/connect/validate.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package connect
2+
3+
import (
4+
"errors"
5+
"regexp"
6+
"strings"
7+
)
8+
9+
var (
10+
errEmptyAlias = errors.New("alias is empty")
11+
errSpecialCharacters = errors.New("special characters are not allowed")
12+
13+
aliasPattern = regexp.MustCompile(`^[a-zA-Z0-9_]+$`)
14+
)
15+
16+
func (c *Connect) Validate() error {
17+
return validateAlias(c.Alias)
18+
}
19+
20+
func validateAlias(alias string) error {
21+
if strings.TrimSpace(alias) == "" {
22+
return errEmptyAlias
23+
}
24+
25+
if !aliasPattern.MatchString(alias) {
26+
return errSpecialCharacters
27+
}
28+
29+
return nil
30+
}

pkg/connect/validate_test.go

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package connect
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestConnectValidate(t *testing.T) {
8+
type args struct {
9+
connection *Connect
10+
}
11+
tests := []struct {
12+
name string
13+
args args
14+
wantErr bool
15+
}{
16+
{
17+
name: "success - simple chars",
18+
args: args{
19+
connection: &Connect{
20+
Alias: "aasd",
21+
},
22+
},
23+
wantErr: false,
24+
},
25+
{
26+
name: "success - numbers",
27+
args: args{
28+
connection: &Connect{
29+
Alias: "123123",
30+
},
31+
},
32+
wantErr: false,
33+
},
34+
{
35+
name: "fail - ru chars",
36+
args: args{
37+
connection: &Connect{
38+
Alias: "йцуйцу",
39+
},
40+
},
41+
wantErr: true,
42+
},
43+
{
44+
name: "fail - empty alias",
45+
args: args{
46+
connection: &Connect{
47+
Alias: "",
48+
},
49+
},
50+
wantErr: true,
51+
},
52+
{
53+
name: "fail - spaces",
54+
args: args{
55+
connection: &Connect{
56+
Alias: " ",
57+
},
58+
},
59+
wantErr: true,
60+
},
61+
{
62+
name: "fail - with /",
63+
args: args{
64+
connection: &Connect{
65+
Alias: "alias/",
66+
},
67+
},
68+
wantErr: true,
69+
},
70+
{
71+
name: "fail - with $",
72+
args: args{
73+
connection: &Connect{
74+
Alias: "$test",
75+
},
76+
},
77+
wantErr: true,
78+
},
79+
}
80+
for _, tt := range tests {
81+
t.Run(tt.name, func(t *testing.T) {
82+
if err := tt.args.connection.Validate(); (err != nil) != tt.wantErr {
83+
t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
84+
}
85+
})
86+
}
87+
}
88+
89+
func TestValidateAlias(t *testing.T) {
90+
tests := []struct {
91+
name string
92+
alias string
93+
wantErr bool
94+
}{
95+
{
96+
name: "success - simple chars",
97+
alias: "aasd",
98+
wantErr: false,
99+
},
100+
{
101+
name: "success - numbers",
102+
alias: "123123",
103+
wantErr: false,
104+
},
105+
{
106+
name: "fail - ru chars",
107+
alias: "ыфвдыджфлв",
108+
wantErr: true,
109+
},
110+
{
111+
name: "fail - empty alias",
112+
alias: "",
113+
wantErr: true,
114+
},
115+
{
116+
name: "fail - spaces",
117+
alias: " ",
118+
wantErr: true,
119+
},
120+
{
121+
name: "fail - with /",
122+
alias: "alias/",
123+
wantErr: true,
124+
},
125+
{
126+
name: "fail - with $",
127+
alias: "$test",
128+
wantErr: true,
129+
},
130+
}
131+
for _, tt := range tests {
132+
t.Run(tt.name, func(t *testing.T) {
133+
if err := validateAlias(tt.alias); (err != nil) != tt.wantErr {
134+
t.Errorf("validateAlias() error = %v, wantErr %v", err, tt.wantErr)
135+
}
136+
})
137+
}
138+
}

pkg/kernel/create.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ var (
2121
func Create(connection *connect.Connect) error {
2222
setup.Init()
2323

24+
err := connection.Validate()
25+
if err != nil {
26+
return err
27+
}
28+
2429
connections, err := store.GetConnections()
2530
if err != nil {
2631
logger.Error(ErrGetConnectionAtCreate.Error())

pkg/kernel/create_test.go

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestCreate(t *testing.T) {
2626
}
2727

2828
createdConnection := &connect.Connect{
29-
Alias: "created_alias",
29+
Alias: testutil.RandomString(),
3030
Login: "test",
3131
Address: "test",
3232
Password: "test",
@@ -45,7 +45,7 @@ func TestCreate(t *testing.T) {
4545
name: "success - create connection",
4646
args: args{
4747
connect: &connect.Connect{
48-
Alias: "new_alias",
48+
Alias: testutil.RandomString(),
4949
Login: "test",
5050
Address: "test",
5151
Password: "test",
@@ -77,7 +77,7 @@ func TestCreate(t *testing.T) {
7777
name: "success - add ssh options",
7878
args: args{
7979
connect: &connect.Connect{
80-
Alias: "new_2_alias",
80+
Alias: testutil.RandomString(),
8181
Login: "test",
8282
Address: "test",
8383
Password: "test",
@@ -96,7 +96,7 @@ func TestCreate(t *testing.T) {
9696
name: "success - save private key",
9797
args: args{
9898
connect: &connect.Connect{
99-
Alias: "new_3_alias",
99+
Alias: testutil.RandomString(),
100100
Login: "test",
101101
Address: "test",
102102
Password: "test",
@@ -115,7 +115,7 @@ func TestCreate(t *testing.T) {
115115
name: "fail - dont valid private key",
116116
args: args{
117117
connect: &connect.Connect{
118-
Alias: "new_4_alias",
118+
Alias: testutil.RandomString(),
119119
Login: "test",
120120
Address: "test",
121121
Password: "test",
@@ -130,6 +130,38 @@ func TestCreate(t *testing.T) {
130130
},
131131
wantErr: true,
132132
},
133+
{
134+
name: "fail - empty alias",
135+
args: args{
136+
connect: &connect.Connect{
137+
Alias: "",
138+
Login: "test",
139+
Address: "test",
140+
Password: "test",
141+
Type: connect.TypeSSH,
142+
CreatedAt: "time",
143+
UpdatedAt: "time",
144+
SshOptions: &connect.SshOptions{},
145+
},
146+
},
147+
wantErr: true,
148+
},
149+
{
150+
name: "fail - alias is invalid with /",
151+
args: args{
152+
connect: &connect.Connect{
153+
Alias: "test/alias",
154+
Login: "test",
155+
Address: "test",
156+
Password: "test",
157+
Type: connect.TypeSSH,
158+
CreatedAt: "time",
159+
UpdatedAt: "time",
160+
SshOptions: &connect.SshOptions{},
161+
},
162+
},
163+
wantErr: true,
164+
},
133165
}
134166

135167
if err = Create(createdConnection); err != nil {

pkg/kernel/list_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66

77
"github.com/misha-ssh/kernel/pkg/connect"
8+
"github.com/misha-ssh/kernel/testutil"
89
)
910

1011
func TestList(t *testing.T) {
@@ -15,23 +16,23 @@ func TestList(t *testing.T) {
1516
isCreateConnection bool
1617
}{
1718
{
18-
name: "list connections - get empty connections",
19+
name: "success - get empty connections",
1920
want: &connect.Connections{},
2021
wantErr: false,
2122
isCreateConnection: false,
2223
},
2324
{
24-
name: "list connections - create empty connections",
25+
name: "success - create empty connections",
2526
want: &connect.Connections{},
2627
wantErr: false,
2728
isCreateConnection: true,
2829
},
2930
{
30-
name: "list connections - create empty connections",
31+
name: "success - get exists connections",
3132
want: &connect.Connections{
3233
Connects: []connect.Connect{
3334
{
34-
Alias: t.TempDir(),
35+
Alias: testutil.RandomString(),
3536
Login: "test",
3637
Address: "test",
3738
Password: "test",
@@ -41,7 +42,7 @@ func TestList(t *testing.T) {
4142
SshOptions: &connect.SshOptions{},
4243
},
4344
{
44-
Alias: t.TempDir(),
45+
Alias: testutil.RandomString(),
4546
Login: "test",
4647
Address: "test",
4748
Password: "test",
@@ -51,7 +52,7 @@ func TestList(t *testing.T) {
5152
SshOptions: &connect.SshOptions{},
5253
},
5354
{
54-
Alias: t.TempDir(),
55+
Alias: testutil.RandomString(),
5556
Login: "test",
5657
Address: "test",
5758
Password: "test",

pkg/kernel/update.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ var (
2020
func Update(connection *connect.Connect, oldAlias string) error {
2121
setup.Init()
2222

23+
err := connection.Validate()
24+
if err != nil {
25+
return err
26+
}
27+
2328
connections, err := store.GetConnections()
2429
if err != nil {
2530
logger.Error(ErrGetConnectionAtUpdate.Error())

0 commit comments

Comments
 (0)