-
Notifications
You must be signed in to change notification settings - Fork 4
/
portchecker_test.go
58 lines (55 loc) · 4.04 KB
/
portchecker_test.go
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
package main
import (
"github.com/hackmdio/portchecker/internal"
"github.com/stretchr/testify/assert"
"testing"
)
func TestParseStringToNetPort(t *testing.T) {
tables := []struct {
data string
result []string
}{
{"tcp://127.0.0.0.1:1234", []string{"tcp://127.0.0.0.1:1234", "tcp", "127.0.0.0.1", "1234"}},
{"tcp://test.123.asda.asd:1234", []string{"tcp://test.123.asda.asd:1234", "tcp", "test.123.asda.asd", "1234"}},
{"test.123.asda.asd:1234", []string{"test.123.asda.asd:1234", "tcp", "test.123.asda.asd", "1234"}},
{"test.123.asda.asd", []string{"test.123.asda.asd", "tcp", "test.123.asda.asd", "80"}},
{"tcp://127.0.0.0.1", []string{"tcp://127.0.0.0.1", "tcp", "127.0.0.0.1", "80"}},
{"postgres://asdf:[email protected]:234", []string{"postgres://asdf:[email protected]:234", "tcp", "asdas.asdfasf.asdf.asdf", "234"}},
{"postgres://[email protected]:234", []string{"postgres://[email protected]:234", "tcp", "asdas.asdfasf.asdf.asdf", "234"}},
{"postgres://asdas.asdfasf.asdf.asdf:234", []string{"postgres://asdas.asdfasf.asdf.asdf:234", "tcp", "asdas.asdfasf.asdf.asdf", "234"}},
{"postgres://asdf:[email protected]", []string{"postgres://asdf:[email protected]", "tcp", "asdas.asdfasf.asdf.asdf", "5432"}},
{"postgres://[email protected]", []string{"postgres://[email protected]", "tcp", "asdas.asdfasf.asdf.asdf", "5432"}},
{"postgres://asdas.asdfasf.asdf.asdf", []string{"postgres://asdas.asdfasf.asdf.asdf", "tcp", "asdas.asdfasf.asdf.asdf", "5432"}},
{"postgres://localhost/db", []string{"postgres://localhost/db", "tcp", "localhost", "5432"}},
{"postgres://localhost:123/db", []string{"postgres://localhost:123/db", "tcp", "localhost", "123"}},
{"mysql://testuser:[email protected]", []string{"mysql://testuser:[email protected]", "tcp", "mysql-host.local", "3306"}},
{"mysql://testuser:[email protected]:1234", []string{"mysql://testuser:[email protected]:1234", "tcp", "mysql-host.local", "1234"}},
{"mysql://testuser:[email protected]/db", []string{"mysql://testuser:[email protected]/db", "tcp", "mysql-host.local", "3306"}},
{"mysql://testuser:[email protected]:1234/db", []string{"mysql://testuser:[email protected]:1234/db", "tcp", "mysql-host.local", "1234"}},
{"mysql://hackmd:password@host:3306/hackmd?charset=utf8mb4", []string{"mysql://hackmd:password@host:3306/hackmd?charset=utf8mb4", "tcp", "host", "3306"}},
{"mysql://u-s_er:p!%ass@hack-md_db.local:1234/h_a-ckmd", []string{"mysql://u-s_er:p!%ass@hack-md_db.local:1234/h_a-ckmd", "tcp", "hack-md_db.local", "1234"}},
{"redis://localhost:1234", []string{"redis://localhost:1234", "tcp", "localhost", "1234"}},
{"redis://localhost", []string{"redis://localhost", "tcp", "localhost", "6379"}},
{"redis://test-cache", []string{"redis://test-cache", "tcp", "test-cache", "6379"}},
{"redis://:@test-cache", []string{"redis://:@test-cache", "tcp", "test-cache", "6379"}},
{"redis://:@test-cache/10", []string{"redis://:@test-cache/10", "tcp", "test-cache", "6379"}},
{"redis://:@test-cache:6380/10", []string{"redis://:@test-cache:6380/10", "tcp", "test-cache", "6380"}},
{"redis://:passw0rd@test-cache", []string{"redis://:passw0rd@test-cache", "tcp", "test-cache", "6379"}},
{"redis://:passw0rd@test-cache/10", []string{"redis://:passw0rd@test-cache/10", "tcp", "test-cache", "6379"}},
{"redis://:passw0rd@test-cache:6380/10", []string{"redis://:passw0rd@test-cache:6380/10", "tcp", "test-cache", "6380"}},
{"", []string{"", "tcp", "localhost", "80"}},
}
for _, entry := range tables {
result := internal.ParseNetworkString(entry.data)
assert.Equal(t, result[0], entry.result[0])
assert.Equal(t, result[1], entry.result[1])
assert.Equal(t, result[2], entry.result[2])
assert.Equal(t, result[3], entry.result[3])
if result[0] != entry.result[0] ||
result[1] != entry.result[1] ||
result[2] != entry.result[2] ||
result[3] != entry.result[3] {
t.Errorf("fail %s, %s, %s", entry.data, result, entry.result)
}
}
}